Skip to content

Add scoped named permissions and restricted user roles - #2607

Merged
nhoening merged 41 commits into
mainfrom
feat/security-permissions-more-detailled
Oct 8, 2026
Merged

nhoening merged 41 commits into
mainfrom
feat/security-permissions-more-detailled

Conversation

@nhoening

@nhoening nhoening commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Description

From discussion #2577. Closes #2608. This PR makes more fine-grained roles and permissions, which enables the read-only access to some account's resources that was the main goal, but also brings access rights management to a better-defined level.

Roles - same as before, plus:

  • "account-reader" (this one is fulfilling the original issue, just this role means you are read-only in your account)
  • "account-member" (this is what members had by default before)
  • "account-data-integrator" (preparing for the future, a user only allowed to read & write data, e.g. for API integrations)

Permissions:

  • "read",
  • "post-data",
  • "annotate",
  • "trigger-schedules",
  • "trigger-forecasts",
  • "trigger-reports",
  • "manage-automations",
  • "edit-flex-config",
  • "edit-assets",
  • "edit-sensors",
  • "delete-data",
  • "manage-users",
  • "edit-profile",
  • "reset-password",
  • "edit-account"

Added permissions (that were not part of the discussion):

  • edit-profile covers a user changing their own profile.
  • reset-password names the existing reset action: users can reset their own password, and authorised account admins can reset passwords within their organisation.

We could reduce this set by 3 if all trigger- permissions are wrapped into one trigger-calculations permission, and
edit-assets includes edit-flex-config.

These 3 legacy permissions remain available to plugin ACLs:.

  • "create-children",
  • "update",
  • "delete",

Roles define what the user's granted permissons are. ACLs define per resource if the grants apply to them (e.g. how a user with admin role differs from one with account-admin - they can only be admin on assets in their account). In some cases, our API is fine-tuning that, e-g- for consultants. That already happened before.

Here is how Role's new permissions property (which we did not have, but now do, being in line with Flask-Security design) maps from itself to permissions:

ROLE_PERMISSION_GRANTS = {
    ACCOUNT_READER_ROLE: frozenset({"read"}),
    ACCOUNT_DATA_INTEGRATOR_ROLE: frozenset({"read", "post-data", "reset-password"}),
    ACCOUNT_MEMBER_ROLE: PERMISSIONS
    - {"delete-data", "manage-users", "delete"},
    ACCOUNT_ADMIN_ROLE: PERMISSIONS,
    CONSULTANT_ROLE: PERMISSIONS,
    ADMIN_READER_ROLE: frozenset({"read"}),
    ADMIN_ROLE: PERMISSIONS,
}

You can see the three new roles getting a special set, while admins get all grants.

Felix’s scope rule is included: home roles apply in a user’s own organisation, while consultant applies through client organisation ACLs. Role.permissions is defined in code. The migration grants existing users member to preserve their former implicit access.

  • added 3 new roles: read-only, member, integration
  • permissions live on Role
  • fine-grained permissions: exoanx the permission set (see above)
  • migration to backfill account-member role to all existing users, and add new roles, and descriptions to all roles
  • User edit UI adds role descriptions to selection form and also in tooltips of selected roles
  • Added changelog item in documentation/changelog.rst

The db migration has no downgrade logic. We would have to persist member role associations on disk, I believe. Should we do that?

Look & Feel

image

How to test

Further Improvements

  • The permission set has expanded, but our business logic to grant them to roles not yet. Probably something we opened up now for later.
  • Should we reduce the number of permissions already now?

@nhoening nhoening self-assigned this Sep 27, 2026
@nhoening nhoening added this to the 1.1.0 milestone Sep 27, 2026
@nhoening
nhoening marked this pull request as ready for review September 29, 2026 10:36
…implify role list

Signed-off-by: Nicolas Höning <nicolas@seita.nl>
…:FlexMeasures/flexmeasures into feat/security-permissions-more-detailled
Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Signed-off-by: Nicolas Höning <nicolas@seita.nl>
@nhoening
nhoening requested a review from Flix6x September 29, 2026 15:50
@Flix6x

Flix6x commented Oct 3, 2026

Copy link
Copy Markdown
Member

Heads-up now that #2634 has merged: this branch's merge migration no longer merges all the heads.

8e9348063a5d_merging joins df847c1a72b0 and b63a02d5e184. Since #2634, b63a02d5e184 is no longer a head — c5e1a7b94d20 is its child. So after this branch is brought up to date there would be two heads again: this merge revision and c5e1a7b94d20. flexmeasures db upgrade refuses to run with more than one, and it fails in the Docker build rather than anywhere obvious.

The fix is to point the merge at the current head instead, so down_revision = ("df847c1a72b0", "c5e1a7b94d20"). Worth re-checking after any future merge into main that brings a migration with it, since a merge revision is pinned to the heads that existed when it was written.

flexmeasures db heads   # should print exactly one

is the check, and it is quick.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

#2634 added c5e1a7b94d20 on top of b63a02d5e184, so the revision this branch merges was no longer a head
and the database would have been left with two. The merge now names the current head instead.

One conflict needed a choice rather than both sides: this branch turns a sensor page into a 403 for a user of another organisation,
where main had grown assertions about which panels such a user is shown. A 403 answers that outright, so this branch's expectation stands.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Oct 3, 2026

Copy link
Copy Markdown
Member

Brought this branch up to date with main in 1c94510, and fixed the migration heads as flagged above: 8e9348063a5d now merges df847c1a72b0 with c5e1a7b94d20 rather than with b63a02d5e184, which stopped being a head when #2634 merged. flexmeasures db heads prints one.

One conflict needed a choice, and it is yours to confirm. In test_sensor_views.py, this branch turns a sensor page into a 403 for a user of another organisation — commit 667d946 changes two assertions from 200 to 403 deliberately. Meanwhile main grew assertions on the same tests about which panels such a user is shown, from the annotations work. The two cannot both hold: a 403 has no panels. I kept this branch's 403 and dropped main's panel assertions for those two tests, since that is what the branch set out to change.

Worth a conscious look, because it is a policy change rather than a test detail: a user of another organisation currently can open such a page and simply sees fewer controls, and after this they cannot open it at all. If that is intended, it deserves a line in the changelog; if the page should stay readable with the controls hidden, then those assertions should come back and the scoped permission needs to allow the read.

32 tests pass across test_sensor_views.py and test_sources_api.py on the merged branch.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

Context:
- Account-reader grants should not authorize data uploads to sensors in the same organisation.

Change:
- Add a valid same-account reader case to the existing sensor data authorization test and restore its shared fixture user afterward.
@nhoening

nhoening commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

On "A test that sweeps every write route as an account-reader" - I don't see why this role should be getting that treatment while others don't. I did add one test case for this new role specifically.

The home roles were renamed within this PR, so these retired-name expectations do not describe a released compatibility contract.
…:FlexMeasures/flexmeasures into feat/security-permissions-more-detailled
@nhoening
nhoening requested a review from Flix6x October 8, 2026 06:18
nhoening and others added 3 commits October 8, 2026 17:19
Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through all of it. I re-tested the changes rather than only reading them:

  • Custom roles in plugin ACLs: this is better than my suggestion. A custom role named in an ACL again grants the four older permissions, but none of the new named ones, and only within the ACL's scope. A side effect worth knowing: an ACL entry that combines every-logged-in-user with a custom role used to let every user through (the wildcard short-circuited the match), and now lets only that role's holders through. That seems to be what such an entry always meant.
  • Accepted suggestions: the empty user_roles, the downgrade and the docstrings landed exactly as I tested them.
  • Account annotations: back with account-admins, and now covered by a test.
  • Sensor editing: agreed. Keeping this PR from changing who can do what is a good principle, and the comment explains why.
  • Reader self-service and the route sweep: fair enough. Your targeted reader test covers what matters most.

I merged main twice more, to resolve two changelog conflicts (keeping both sides each time). On the merged tree, the full test suite passes apart from two failures unrelated to this PR: an order-dependent check in test_search_sources, and an index test that needs a newer timely-beliefs than my local venv has.

@nhoening
nhoening merged commit b8aa005 into main Oct 8, 2026
12 of 13 checks passed
@nhoening
nhoening deleted the feat/security-permissions-more-detailled branch October 8, 2026 19:48
@BelhsanHmida BelhsanHmida mentioned this pull request Oct 12, 2026
3 tasks done
BelhsanHmida added a commit that referenced this pull request Oct 12, 2026
…eir scoped permissions

Since #2607, a role grant and an ACL principal must match together, which user_has_scoped_permission checks and user_matches_principals does not.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@Flix6x

Flix6x commented Oct 16, 2026

Copy link
Copy Markdown
Member

@nhoening, one point from the merge on 2026-10-03 never got an answer: with this PR, a user from another organisation gets a 403 on a sensor page they could open before. Before, they saw the page with fewer controls. main now asserts the 403 (test_forecast_and_annotate_panels_hidden_for_other_account, test_get_timerange_not_called_without_permission).

Is that intended? If so, it deserves a changelog line, and the first test's name is out of date. If not, the read permission needs to let such users in again.

Flix6x added a commit that referenced this pull request Oct 16, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>
@nhoening

Copy link
Copy Markdown
Member Author

I believe it is intended.
I can't find a merge from 3 October though.
Got a link?

@Flix6x

Flix6x commented Oct 16, 2026

Copy link
Copy Markdown
Member

Here's the merge: 1c9451071 (on this branch only, since the PR was squash-merged), with the question raised in this comment. The 403 assertions themselves came from your 667d94685; the merge only kept them over main's panel assertions.

Since it's intended, I'll add a changelog line and rename the test in #2720.

Flix6x added a commit that referenced this pull request Oct 16, 2026
#2607 made a sensor page return 403 to users who cannot read it, where users of another organisation used to see it with fewer controls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>
nhoening pushed a commit that referenced this pull request Oct 18, 2026
* docs: drop the asset ACL docstring's outdated deletion rule

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>

* docs: add PR #2720 to the changelog entry of #2607

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>

* Note the sensor page 403 for other organisations, and rename its test

#2607 made a sensor page return 403 to users who cannot read it, where users of another organisation used to see it with fewer controls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SbBdAEchgGcaao3n3fsqWL
Signed-off-by: F.N. Claessen <felix@seita.nl>

---------

Signed-off-by: F.N. Claessen <felix@seita.nl>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Flix6x added a commit that referenced this pull request Oct 19, 2026
Both changelogs gained entries at the top of the same sections. Kept every
entry from main, including its newer version of the #2607 entry (extended
by #2720), with #2701's entry first in each section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Flix6x added a commit to bferanmi806-sketch/flexmeasures that referenced this pull request Nov 1, 2026
The scan came in with FlexMeasures#2607, which is not released yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Flix6x added a commit that referenced this pull request Nov 1, 2026
* fix: skip recursive data scans while rendering delete controls

Signed-off-by: Enoch <bferanmi806@gmail.com>

* Look up a subtree's sensors before probing whether they hold data

asset_contains_data probed timed_belief with a subquery for the subtree's sensors.
Postgres planned that as a merge join over the whole timed_belief index,
so on a table of 20 million beliefs the probe took over two seconds whenever the subtree held no beliefs, however small it was.
It now fetches the subtree's asset and sensor IDs first and names them in the probes,
which Postgres looks up in the index: 31 ms at most on the same database, for a tree of 1000 sensors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Check for data again before showing delete buttons, now that it is quick

With the probe looking up sensors by ID, the asset and sensor pages can again tell whether a user may delete what they show,
rather than offer the button to every member and let deletion fail on data.
So members keep a delete button for assets and sensors without data, as the API lets them delete those,
and users who may delete data skip the query altogether.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Drop the changelog entry for a slowdown that was never released

The scan came in with #2607, which is not released yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Stop telling users they cannot delete the copies they make

The copy buttons noted "you can copy but not delete" to whoever may not delete the asset shown.
But that asset may hold data, while a copy holds none,
and whoever may copy an asset may edit the assets where the copy lands,
so they may delete the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Run the data probe tests on a fresh database, as the rest of their module does

A module uses either the db or the fresh_db fixture, never both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

---------

Signed-off-by: Enoch <bferanmi806@gmail.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Flix6x added a commit that referenced this pull request Nov 1, 2026
…nd check the API's permissions on audit log pages (#2791)

* fix: skip recursive data scans while rendering delete controls

Signed-off-by: Enoch <bferanmi806@gmail.com>

* Look up a subtree's sensors before probing whether they hold data

asset_contains_data probed timed_belief with a subquery for the subtree's sensors.
Postgres planned that as a merge join over the whole timed_belief index,
so on a table of 20 million beliefs the probe took over two seconds whenever the subtree held no beliefs, however small it was.
It now fetches the subtree's asset and sensor IDs first and names them in the probes,
which Postgres looks up in the index: 31 ms at most on the same database, for a tree of 1000 sensors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Check for data again before showing delete buttons, now that it is quick

With the probe looking up sensors by ID, the asset and sensor pages can again tell whether a user may delete what they show,
rather than offer the button to every member and let deletion fail on data.
So members keep a delete button for assets and sensors without data, as the API lets them delete those,
and users who may delete data skip the query altogether.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Drop the changelog entry for a slowdown that was never released

The scan came in with #2607, which is not released yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Stop telling users they cannot delete the copies they make

The copy buttons noted "you can copy but not delete" to whoever may not delete the asset shown.
But that asset may hold data, while a copy holds none,
and whoever may copy an asset may edit the assets where the copy lands,
so they may delete the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Delete assets and reset passwords through the API

The asset page's delete button and the reset-password buttons now call the API's DELETE and PATCH endpoints,
which a link on another site cannot trigger,
and the UI routes that did the same on a GET are gone.
Resetting a password now asks for confirmation first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Check the API's permissions on the audit log pages and an organisation's asset page

The account audit log page now follows the audit log's own access rule, as the API does,
rather than letting anyone who may read the account see it.
The user audit log page and the page listing an organisation's assets now check access too,
so they no longer name users or organisations the visitor may not read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Name the delete button test after what it checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Link the changelog entry to its PR

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Run the data probe tests on a fresh database, as the rest of their module does

A module uses either the db or the fresh_db fixture, never both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v3qq916tT9BKtL4ji7
Signed-off-by: F.N. Claessen <claessen@seita.nl>

---------

Signed-off-by: Enoch <bferanmi806@gmail.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: Enoch <bferanmi806@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
nhoening pushed a commit that referenced this pull request Nov 19, 2026
* data/services/automations: record how schedule, report and plugin runs end

Context:
- Only forecast runs followed their jobs, so schedule, report and plugin runs stayed at execution state 'pending' whatever became of their jobs.

Change:
- record_automation_run_jobs records the jobs such a run queued, then takes the outcome of any job that already finished or failed from Redis; a run with no jobs has succeeded.
- The record_outcome_on_automation_run decorator records a job's start, success or failure on its run, found through the job's RQ ID, and wraps make_schedule, the sequential wrap-up job, run_report_job and execute_automation_job.
- Plugin jobs carry automation_run_id in their trigger.
- A schedule that fell back counts as a failed run, as the fallback job is not one of the run's recorded jobs.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/tests: cover how schedule, report and plugin runs end

Context:
- Schedule, report and plugin runs now record how their jobs ended.

Change:
- Schedule runs, single and sequential, succeed once their jobs did and fail with the job's error; a job queued outside a run is not recorded.
- A plugin run records its job's outcome, also when the job finished before the run recorded it.
- Report run tests check the run's outcome: succeeded, nothing to do, and refused.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* docs: every automation run records how its jobs ended

Context:
- Schedule, report and plugin runs now record how their jobs ended, as forecast runs did.

Change:
- Say which jobs each type of run records, that a run without jobs has succeeded, and that a schedule which fell back counts as failed.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* changelog: append PR #2670 to the durable automation runs entry

Context:
- Schedule, report and plugin runs now record how their jobs ended, not only forecast runs.

Change:
- Append PR #2670 to the #2457 entry, and drop "for its forecast jobs", which no longer holds.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/models: record when an automation run was reported by monitoring

Context:
- #2519: a monitor command will email a digest of failed automation runs,
  and has to report each failure once, also when a monitoring run is missed.

Change:
- Add a nullable alerted_at column to AutomationRun, with a migration.
- Add a partial index on the runs the monitor looks at
  (not reported yet, and failed, running or not fully dispatched),
  so that its hourly query does not scan the whole run history.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* utils/config: add FLEXMEASURES_MONITOR_AUTOMATIONS_STUCK_AFTER

Context:
- #2519: a run whose worker died stays "running" for good, so monitoring needs a threshold after which it counts as stuck.

Change:
- Add the setting, defaulting to six hours, well above any job timeout.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/services: find the automation runs that failed and were not reported yet

Context:
- #2519: monitoring reports runs whose execution failed, which are stuck, or whose dispatch failed for good.

Change:
- Add get_unalerted_failed_automation_runs, with one criterion for every automation type:
  only a forecast run with dispatch attempts to spare is left out, as it will be dispatched again,
  and so is a run that a runner still holds a live claim on.
- Add group_failed_runs_by_automation, collapsing failures per automation with their counts, latest run and latest error.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli: add flexmeasures monitor automations

Context:
- #2519: automations fail silently unless someone watches their asset's Automations page.

Change:
- Email one digest of the failed runs to the monitoring recipients (or --recipient),
  collapsed per automation, and mark the runs as reported once it is sent.
- Send nothing when nothing failed, and refuse to run without recipients,
  rather than marking runs as reported that nobody heard of.
- --stuck-after-minutes overrides FLEXMEASURES_MONITOR_AUTOMATIONS_STUCK_AFTER.
- Kept out of flexmeasures jobs run-automations, so a mail problem never holds up dispatching.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli/tests: test flexmeasures monitor automations

Context:
- #2519: cover the new monitor command.

Change:
- Drive forecast runs to a failed execution, a stuck execution and an exhausted dispatch through the real runner and worker,
  and check each is reported once, and not before it counts.
- Check which runs count as failed for other types, the threshold's config and CLI override,
  collapsing per automation, and refusing to run without recipients.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* docs: document flexmeasures monitor automations

Context:
- #2519: new monitor command and setting.

Change:
- Describe what the command reports in the automations feature docs, and point to it from the host's error monitoring page.
- List the command, document the stuck threshold setting, and add a CLI changelog entry.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* utils/config: add FLEXMEASURES_MONITOR_AUTOMATIONS_NOTIFY_USERS

Off by default, so that hosts decide when their users start receiving digests of failed automation runs.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/services: find the users to tell about failed automation runs

The account admins of each asset's organisation and the consultants of its consultancy organisation, if active and with an email address, each with the failed automations they may read.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli: email users their own digest of failed automation runs

With FLEXMEASURES_MONITOR_AUTOMATIONS_NOTIFY_USERS on, flexmeasures monitor automations also emails each account admin and consultant a digest of the failed automations they may read.
Runs are marked as reported once the host's digest is sent, so a user digest that cannot be sent is logged rather than retried.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli/tests: test the user digests of flexmeasures monitor automations

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* docs: document the user digests of flexmeasures monitor automations

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/migrations: revise main's latest migration with the alerted_at migration

Context:
- Main gained migrations since this branch started, which left two Alembic heads.

Change:
- Make d4b7e9a1c362 revise f7c2e8b1a409, main's head, rather than c5e1a7b94d20.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* tests: report failed schedule, report and plugin runs in the monitoring digest

Context:
- #2670 has schedule, report and plugin runs record how their jobs ended,
  so their failed executions can now reach the host digest.

Change:
- Fail a schedule, a report and a plugin job on the worker, and check the digest names the automation, the failed execution and the job's error.
- The schedule and plugin runs are marked dispatched, as a runner would, so that only their execution can make them count.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* docs: drop the note that only forecast runs are monitored for their execution

Context:
- Since #2670, every automation type records how its run's jobs ended.

Change:
- Remove the note from the monitoring section of the automations docs.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* changelog: add PR #2671 to the automations, in detail list

Context:
- PR #2671 adds flexmeasures monitor automations.

Change:
- Add one bullet for it to the per-PR automations list.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/services: check which failed automations a user may read with their scoped permissions

Since #2607, a role grant and an ACL principal must match together, which user_has_scoped_permission checks and user_matches_principals does not.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* changelog: add PR #2703 to the monitor automations entry

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* docs/api: run-stats records execution state for every automation type

Context:
- The v3.0-33 entry on run-stats said only forecast runs record their execution state, which PR #2670 makes untrue.

Change:
- Say execution state and job records are kept for every type of automation.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Record the error of a job that failed before its run recorded it by its stored summary

Every queue's exception handler now stores the type and message of a job's error in its meta (#2698),
so a run catching up on a job that already failed records that type, rather than a stand-in type around the last traceback line.
The meta is read afresh, as the run holds the job as it was when queued.
A job without such a summary is still described by its traceback, without a type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* data/migrations: revise main's latest migration with the alerted_at migration

Context:
- Main gained a merge migration since the last sync, which left two Alembic heads.

Change:
- Make d4b7e9a1c362 revise bf22b21d0990, main's head, rather than f7c2e8b1a409.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* utils/time_utils: move the exact duration formatting out of the monitor CLI

Requested in review, so other code can reuse it.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* data/migrations: count runs that failed before the upgrade as reported

Otherwise a host's first digest lists every run that ever failed.
The upgrade marks the runs that the monitor would report now, by its rules at this revision, and leaves out runs that may still be retried or finish.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli/monitor: link each automation in the digest to its info on the Automations page

The digest is sent from the CLI, where FlexMeasures does not know its own address, so the new FLEXMEASURES_BASE_URL setting gives it.
Without the setting, the digest carries no links, as before.
The Automations page opens the tab and info panel of the automation named in ?automation=<id>, which show its latest run, its error and its jobs.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli/monitor: build the digest's closing lines without an ambiguous variable name

Fixes flake8's E741 from the merge of #2671.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* cli/monitor: turn user digests on with --notify-users instead of a setting

A flag on the cron line can be switched without redeploying the app,
which changing FLEXMEASURES_MONITOR_AUTOMATIONS_NOTIFY_USERS required.
The setting was never released, so it is dropped without deprecation.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

---------

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
Co-authored-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Users with read-only permissions

2 participants