-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SARC-332] Implémenter les alertes : un cluster ne répond pas depuis …
…X temps (#134) * [SARC-332] Implémenter les alertes : un cluster ne répond pas depuis X temps * Change date parsing * Set parsed date timezone to MTL * Parse date only with MTL timezone --------- Co-authored-by: Bruno Carrez <bruno.carrez@mila.quebec>
- Loading branch information
1 parent
8a49625
commit ec45280
Showing
11 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
from datetime import datetime, time, timedelta | ||
|
||
from sarc.client.job import get_available_clusters | ||
from sarc.config import MTL | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def check_cluster_response(time_interval: timedelta = timedelta(days=7)): | ||
""" | ||
Check if we scraped clusters recently. | ||
Log a warning for each cluster not scraped since `time_interval` from now. | ||
Parameters | ||
---------- | ||
time_interval: timedelta | ||
Interval of time (until current time) in which we want to see cluster scrapings. | ||
For each cluster, if the latest scraping occurred before this period, a warning will be logged. | ||
Default is 7 days. | ||
""" | ||
# Get current date | ||
current_date = datetime.now(tz=MTL) | ||
# Get the oldest date allowed from now | ||
oldest_allowed_date = current_date - time_interval | ||
# Check each available cluster | ||
for cluster in get_available_clusters(): | ||
if cluster.end_date is None: | ||
logger.warning( | ||
f"[{cluster.cluster_name}] no end_date available, cannot check last scraping" | ||
) | ||
else: | ||
# Cluster's latest scraping date should be in `cluster.end_date`. | ||
# NB: We assume cluster's `end_date` is stored as a date string, | ||
# so we must first convert it to a datetime object. | ||
# `en_date` is parsed the same way as start/end parameters in `get_jobs()`. | ||
cluster_end_date = datetime.combine( | ||
datetime.strptime(cluster.end_date, "%Y-%m-%d"), time.min | ||
).replace(tzinfo=MTL) | ||
# Now we can check. | ||
if cluster_end_date < oldest_allowed_date: | ||
logger.warning( | ||
f"[{cluster.cluster_name}] no scraping since {cluster_end_date}, " | ||
f"oldest required: {oldest_allowed_date}, " | ||
f"current time: {current_date}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/functional/usage_alerts/test_alert_cluster_response.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import re | ||
from datetime import timedelta | ||
|
||
import pytest | ||
|
||
from sarc.alerts.usage_alerts.cluster_response import check_cluster_response | ||
from tests.functional.jobs.test_func_load_job_series import MOCK_TIME | ||
|
||
PARAMETERS = { | ||
"default": dict(), # default is 7 days | ||
**{ | ||
f"{days}-days": dict(time_interval=timedelta(days=days)) | ||
for days in [365, 283, 282, 281, 280, 279] | ||
}, | ||
} | ||
|
||
|
||
@pytest.mark.freeze_time(MOCK_TIME) | ||
@pytest.mark.usefixtures("read_only_db_with_users", "tzlocal_is_mtl") | ||
@pytest.mark.parametrize("params", PARAMETERS.values(), ids=PARAMETERS.keys()) | ||
def test_check_cluster_response(params, caplog, file_regression): | ||
check_cluster_response(**params) | ||
file_regression.check( | ||
re.sub( | ||
r"WARNING +sarc\.alerts\.usage_alerts\.cluster_response:cluster_response.py:[0-9]+ +", | ||
"", | ||
caplog.text, | ||
) | ||
) |
4 changes: 4 additions & 0 deletions
4
...tional/usage_alerts/test_alert_cluster_response/test_check_cluster_response_279_days_.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[fromage] no scraping since 2023-02-15 00:00:00-05:00, oldest required: 2023-02-15 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[mila] no scraping since 2023-02-14 00:00:00-05:00, oldest required: 2023-02-15 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[patate] no scraping since 2023-02-13 00:00:00-05:00, oldest required: 2023-02-15 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[raisin] no scraping since 2023-02-12 00:00:00-05:00, oldest required: 2023-02-15 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 |
3 changes: 3 additions & 0 deletions
3
...tional/usage_alerts/test_alert_cluster_response/test_check_cluster_response_280_days_.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[mila] no scraping since 2023-02-14 00:00:00-05:00, oldest required: 2023-02-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[patate] no scraping since 2023-02-13 00:00:00-05:00, oldest required: 2023-02-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[raisin] no scraping since 2023-02-12 00:00:00-05:00, oldest required: 2023-02-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 |
2 changes: 2 additions & 0 deletions
2
...tional/usage_alerts/test_alert_cluster_response/test_check_cluster_response_281_days_.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[patate] no scraping since 2023-02-13 00:00:00-05:00, oldest required: 2023-02-13 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[raisin] no scraping since 2023-02-12 00:00:00-05:00, oldest required: 2023-02-13 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 |
1 change: 1 addition & 0 deletions
1
...tional/usage_alerts/test_alert_cluster_response/test_check_cluster_response_282_days_.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[raisin] no scraping since 2023-02-12 00:00:00-05:00, oldest required: 2023-02-12 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 |
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions
4
...ctional/usage_alerts/test_alert_cluster_response/test_check_cluster_response_default_.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[fromage] no scraping since 2023-02-15 00:00:00-05:00, oldest required: 2023-11-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[mila] no scraping since 2023-02-14 00:00:00-05:00, oldest required: 2023-11-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[patate] no scraping since 2023-02-13 00:00:00-05:00, oldest required: 2023-11-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 | ||
[raisin] no scraping since 2023-02-12 00:00:00-05:00, oldest required: 2023-11-14 19:00:00-05:00, current time: 2023-11-21 19:00:00-05:00 |