— Link to reschedule non-ok checks (fix #27)

This commit is contained in:
Luc Didry 2024-01-15 14:10:43 +01:00
parent a8cd9a9581
commit 098575f186
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C
4 changed files with 115 additions and 0 deletions

View file

@ -124,6 +124,14 @@ async def get_severity_counts(db: Session) -> dict:
return counts_dict
async def reschedule_all(db: Session):
"""Reschedule checks of all non OK tasks ASAP"""
db.query(Task) \
.filter(Task.severity.in_(['warning', 'critical', 'unknown'])) \
.update({Task.next_run: datetime.now() - timedelta(days=1)})
db.commit()
async def remove_old_results(db: Session, max_results: int):
tasks = db.query(Task).all()
deleted = 0

View file

@ -67,6 +67,13 @@ async def create_results(
return {"result_ids": [r.id for r in db_results]}
@route.post("/reschedule/all")
async def reschedule_all(request: Request, db: Session = Depends(get_db)):
"""Reschedule checks of all non OK tasks ASAP"""
await queries.reschedule_all(db)
return {"msg": "Non OK tasks reschuled"}
@route.get("/stats")
async def get_stats(db: Session = Depends(get_db)):
"""Get tasks statistics"""

View file

@ -22,6 +22,8 @@
<ul>
<li>
<img src="{{ url_for('static', path='/logo-64.png') }}"
width="64"
height="64"
alt="">
</li>
<li>
@ -45,11 +47,18 @@
role="button">
Agents
</a>
<a href="#"
id="reschedule-all"
class="outline"
title="Reschedule non-ok checks as soon as possible">
🕐
</a>
</li>
</ul>
</nav>
</header>
<main class="container">
<dialog id="msg"></dialog>
<div id="header">
{% block title %}
{% endblock title %}
@ -69,5 +78,21 @@
or
<a href="{{ url_for('get_severity_counts_view') }}redoc">Redoc</a>
</footer>
<script>
async function rescheduleAll() {
const response = await fetch('{{ url_for("reschedule_all") }}', {method: 'POST'});
const json = await response.json();
const dialog = document.getElementById('msg');
dialog.innerText = json.msg;
dialog.setAttribute('open', '');
setTimeout(() => {
dialog.removeAttribute('open');
}, 1500);
}
document.getElementById('reschedule-all').addEventListener('click', event => {
event.preventDefault();
rescheduleAll();
});
</script>
</body>
</html>

View file

@ -123,6 +123,21 @@ async def test_update_from_config_db_updates_existing_tasks(db, empty_config, ta
assert db.query(Task).count() == 1
@pytest.mark.asyncio
async def test_reschedule_all(db, ten_tasks, ten_warning_tasks, ten_critical_tasks, ten_ok_tasks):
assert db.query(Task).count() == 40
assert db.query(Task).filter(Task.severity == "unknown").count() == 10
assert db.query(Task).filter(Task.severity == "warning").count() == 10
assert db.query(Task).filter(Task.severity == "critical").count() == 10
assert db.query(Task).filter(Task.severity == "ok").count() == 10
one_hour_ago = datetime.now() - timedelta(hours=1)
assert db.query(Task).filter(Task.next_run <= one_hour_ago).count() == 0
await queries.reschedule_all(db)
assert db.query(Task).filter(Task.next_run <= one_hour_ago).count() == 30
@pytest.fixture
def task(db):
task = Task(
@ -215,3 +230,63 @@ def ten_tasks(db):
tasks.append(task)
db.commit()
return tasks
@pytest.fixture
def ten_warning_tasks(db):
now = datetime.now()
tasks = []
for i in range(10):
task = Task(
url="https://www.example.com",
domain="example.com",
check="body-contains",
expected="foo",
frequency=1,
next_run=now,
severity="warning"
)
db.add(task)
tasks.append(task)
db.commit()
return tasks
@pytest.fixture
def ten_critical_tasks(db):
now = datetime.now()
tasks = []
for i in range(10):
task = Task(
url="https://www.example.com",
domain="example.com",
check="body-contains",
expected="foo",
frequency=1,
next_run=now,
severity="critical"
)
db.add(task)
tasks.append(task)
db.commit()
return tasks
@pytest.fixture
def ten_ok_tasks(db):
now = datetime.now()
tasks = []
for i in range(10):
task = Task(
url="https://www.example.com",
domain="example.com",
check="body-contains",
expected="foo",
frequency=1,
next_run=now,
severity="ok"
)
db.add(task)
tasks.append(task)
db.commit()
return tasks