Repository navigation
Feat: monitor automations user digests #2703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BelhsanHmida
wants to merge
40
commits into
main
Choose a base branch
from
feat/2519-monitor-automations-user-digests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
6c7798a
data/services/automations: record how schedule, report and plugin run…
BelhsanHmida 196ccc1
data/tests: cover how schedule, report and plugin runs end
BelhsanHmida da14723
docs: every automation run records how its jobs ended
BelhsanHmida 2441424
changelog: append PR #2670 to the durable automation runs entry
BelhsanHmida 56e5f83
data/models: record when an automation run was reported by monitoring
BelhsanHmida 39ca4a0
utils/config: add FLEXMEASURES_MONITOR_AUTOMATIONS_STUCK_AFTER
BelhsanHmida ddfbe7a
data/services: find the automation runs that failed and were not repo…
BelhsanHmida 3cf9ef6
cli: add flexmeasures monitor automations
BelhsanHmida 47e200c
cli/tests: test flexmeasures monitor automations
BelhsanHmida 379a8b8
docs: document flexmeasures monitor automations
BelhsanHmida e3821f1
utils/config: add FLEXMEASURES_MONITOR_AUTOMATIONS_NOTIFY_USERS
BelhsanHmida 7c32e86
data/services: find the users to tell about failed automation runs
BelhsanHmida 4568f8e
cli: email users their own digest of failed automation runs
BelhsanHmida bc831dc
cli/tests: test the user digests of flexmeasures monitor automations
BelhsanHmida ce51095
docs: document the user digests of flexmeasures monitor automations
BelhsanHmida 21fc37d
Merge origin/main into feat/automation-run-outcomes
BelhsanHmida 185502b
Merge feat/automation-run-outcomes (#2670) into feat/2519-monitor-aut…
BelhsanHmida f58aaa6
data/migrations: revise main's latest migration with the alerted_at m…
BelhsanHmida d37502e
tests: report failed schedule, report and plugin runs in the monitori…
BelhsanHmida 3734a4e
docs: drop the note that only forecast runs are monitored for their e…
BelhsanHmida d910da4
changelog: add PR #2671 to the automations, in detail list
BelhsanHmida 6a69905
Merge feat/2519-monitor-automations (#2671) into feat/2519-monitor-au…
BelhsanHmida ec3d95b
data/services: check which failed automations a user may read with th…
BelhsanHmida 2b7f08d
changelog: add PR #2703 to the monitor automations entry
BelhsanHmida 5f37108
docs/api: run-stats records execution state for every automation type
BelhsanHmida 627c635
Merge origin/main into feat/automation-run-outcomes
BelhsanHmida 94e4a52
Merge main, where #2657 records a schedule run's jobs before queueing…
Flix6x cc5295a
Record the error of a job that failed before its run recorded it by i…
Flix6x 0c85cd1
Merge feat/automation-run-outcomes (#2670's final tip) into feat/2519…
BelhsanHmida 84d04b0
Merge #2670's squash commit into feat/2519-monitor-automations
BelhsanHmida 576fa2a
Merge origin/main into feat/2519-monitor-automations
BelhsanHmida e83eb29
data/migrations: revise main's latest migration with the alerted_at m…
BelhsanHmida 5af4a3a
Merge branch 'feat/2519-monitor-automations' into feat/2519-monitor-a…
BelhsanHmida b57c036
utils/time_utils: move the exact duration formatting out of the monit…
BelhsanHmida ff3ec20
data/migrations: count runs that failed before the upgrade as reported
BelhsanHmida 644b604
cli/monitor: link each automation in the digest to its info on the Au…
BelhsanHmida d183ba9
Merge branch 'feat/2519-monitor-automations' into feat/2519-monitor-a…
BelhsanHmida 1d9751b
cli/monitor: build the digest's closing lines without an ambiguous va…
BelhsanHmida 8ed0a06
Merge remote-tracking branch 'origin/main' into feat/2519-monitor-aut…
BelhsanHmida 229d9a2
Merge branch 'feat/2519-monitor-automations' into feat/2519-monitor-a…
BelhsanHmida File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -878,11 +878,49 @@ Default: ``None`` | |
| FLEXMEASURES_DEFAULT_MONITORING_MAIL_RECIPIENTS | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| E-mail addresses to send monitoring alerts to from the CLI tasks ``flexmeasures monitor latest-run`` and ``flexmeasures monitor last-seen`` if no explicit user recipients are given. For example ``["[email protected]", "[email protected]"]``. | ||
| E-mail addresses to send monitoring alerts to from the CLI tasks ``flexmeasures monitor latest-run``, ``flexmeasures monitor last-seen`` and ``flexmeasures monitor automations`` if no explicit user recipients are given. For example ``["[email protected]", "[email protected]"]``. | ||
|
|
||
| Default: ``[]`` | ||
|
|
||
|
|
||
| .. _monitor_automations_stuck_after: | ||
|
|
||
| FLEXMEASURES_MONITOR_AUTOMATIONS_STUCK_AFTER | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| How long an automation run may execute before ``flexmeasures monitor automations`` reports it as stuck, for instance because the worker running its job died. | ||
| Its ``--stuck-after-minutes`` option overrides this setting. | ||
|
|
||
| Default: ``timedelta(hours=6)`` | ||
|
|
||
| .. _base_url: | ||
|
|
||
| FLEXMEASURES_BASE_URL | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Where this server is reached on the web, for instance ``"https://ems.example.com"``. | ||
| Emails sent from the CLI, outside of any request, use it to link to the web pages they are about. | ||
| For instance, the digest of ``flexmeasures monitor automations`` links each failing automation to the Automations page of its asset. | ||
| Without it, these emails carry no links. | ||
|
|
||
| Default: ``None`` | ||
|
|
||
|
|
||
| .. _monitor_automations_notify_users: | ||
|
|
||
| FLEXMEASURES_MONITOR_AUTOMATIONS_NOTIFY_USERS | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| Whether ``flexmeasures monitor automations`` also emails the people responsible for the assets whose automations failed: | ||
| the users with the ``account-admin`` role in the asset's organisation, and the users with the ``consultant`` role in its consultancy organisation. | ||
| Each of them receives one digest per monitoring run, listing only the automations they can see. | ||
| The host's digest is sent regardless, and still lists every failed run. | ||
|
|
||
| Switch this on once you want your users to start receiving these emails. | ||
|
|
||
| Default: ``False`` | ||
|
|
||
|
|
||
| .. _redis-config: | ||
|
|
||
| Redis | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -36,6 +36,17 @@ If you run distinct filters, such as separate checks per account, account group | |
| .. todo:: Adding roles and assigning them to accounts is not supported by the UI yet (user roles can be added in the UI). Account roles can be added with ``flexmeasures add account-role``. | ||
|
|
||
|
|
||
| Monitoring automations | ||
| ----------------------- | ||
|
|
||
| The CLI task ``flexmeasures monitor automations`` emails a digest of the automation runs that failed since its last check, with repeated failures collapsed per automation. | ||
| Run it from cron, for instance every hour; see :ref:`monitoring_automations` for what it reports. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ flexmeasures monitor automations --recipient [email protected] | ||
|
|
||
|
|
||
| Monitoring task runs | ||
| --------------------- | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1869,6 +1869,58 @@ def test_report_automation_refuses_a_sensor_nobody_checked( | |
| assert not app.redis_connection.get(f"automation-last-run:{automation.id}") | ||
|
|
||
|
|
||
| def test_a_failed_report_run_is_reported_by_monitoring( | ||
| app, | ||
| fresh_db, | ||
| setup_dummy_data_fresh_db, | ||
| clean_redis, | ||
| tmp_path, | ||
| freeze_server_now, | ||
| mocker, | ||
| ): | ||
| """A report run whose job failed on the worker shows up in the host's monitoring digest, with the job's error.""" | ||
| from flexmeasures.cli.data_add import add_automation | ||
| from flexmeasures.cli.jobs import run_automations | ||
| from flexmeasures.cli.monitor import monitor_automations | ||
| from flexmeasures.data.models.reporting.pandas_reporter import PandasReporter | ||
| from flexmeasures.utils.job_utils import work_on_rq | ||
|
|
||
| sensor1_id, sensor2_id, report_sensor_id, _ = setup_dummy_data_fresh_db | ||
| report_sensor = fresh_db.session.get(Sensor, report_sensor_id) | ||
| cli_input = _report_automation_cli_input( | ||
| tmp_path, | ||
| sensor1_id, | ||
| sensor2_id, | ||
| report_sensor_id, | ||
| parameters_extra={"start-offset": "DB"}, | ||
| asset_id=report_sensor.generic_asset_id, | ||
| ) | ||
| cli_input[cli_input.index("0 1 * * *")] = "* * * * *" # due every minute | ||
| cli_input += ["--timezone", "UTC"] | ||
| freeze_server_now(datetime(2023, 4, 10, 10, 0, 30, tzinfo=timezone.utc)) | ||
| runner = app.test_cli_runner() | ||
| result = runner.invoke(add_automation, cli_input) | ||
| assert "Successfully created" in result.output, result.output | ||
| automation = fresh_db.session.execute(select(Automation)).scalar_one() | ||
| result = runner.invoke(run_automations) | ||
| assert "queued 1 reporting job(s)" in result.output, result.output | ||
|
|
||
| mocker.patch.object( | ||
| PandasReporter, "compute", side_effect=KeyError("df_agg is missing") | ||
| ) | ||
| work_on_rq(app.queues["reporting"]) | ||
|
|
||
| with app.mail.record_messages() as outbox: | ||
| result = runner.invoke(monitor_automations, ["--recipient", "[email protected]"]) | ||
|
|
||
| assert result.exit_code == 0, result.output | ||
| assert len(outbox) == 1 | ||
| body = outbox[0].body | ||
| assert f"Automation {automation.id} '{automation.name}' (reporting)" in body | ||
| assert "failed once (execution failed: 1)" in body | ||
| assert "last error: KeyError: 'df_agg is missing'" in body | ||
|
|
||
|
|
||
| def _two_output_report_automation_cli_input( | ||
| tmp_path, | ||
| sensor1_id, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this would be easier to deal with if it was a parameter flag to the CLI command.
Changing a config setting means we have to re-deploy the app. Changing the call to a cron job command is much easier.