Skip to content

Feat: automation select data source - #2584

Merged
Flix6x merged 24 commits into
mainfrom
feat/automation-data-source-picker
Oct 1, 2026
Merged

Flix6x merged 24 commits into
mainfrom
feat/automation-data-source-picker

Conversation

@BelhsanHmida

@BelhsanHmida BelhsanHmida commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

flexmeasures add automation has taken --source since the command was written, so several automations can share one data generator, one configuration and one lineage of data. The API could only be handed a class and a config, which reaches an existing source by accident — when what it builds happens to hash to the same attributes — and the UI had no way to say "the one that automation already uses".

This PR gives the API and the UI the choice the CLI already had.

  • POST /assets/(id)/automations accepts a source: the id of an existing data source whose data generator and configuration the automation reuses, and under which it records what it computes. The service already took source=; only the request schema and the permission check are new.
  • The source cannot be combined with data-generator or config, which a data source already determines (422).
  • A schedule automation cannot name one (422). Its data source is re-resolved from the asset and the flex config on every run, so the next run would move it off the named source again. This mirrors the CLI, which already refuses --source with --type scheduling.
  • The caller must be able to read the source (403), by the same rule GET /sources applies. Without it, a source is a way to compute under, and record on, a generator configuration belonging to another organisation. The CLI runs without a user and stays trusted, as it already is for sensors.
  • GET /sources accepts a filter of space-separated search terms — matched against a source's name, its model and its id prefix, the way the sensors listing already searches — and a type, which narrows the listing to one source type.
  • The rule for which sources a user may read moves out of the endpoint into the data-sources service (get_readable_source_account_ids, user_may_read_source), so it is stated once and applied in both places.
  • In the New automation form, a search box above the two generator fields lists the data sources of the automation's own type, matched by id, by name and by generator class, and lists them on focus so they can be browsed without guessing a term. Picking one shows its description and the configuration it stores, and disables the class and config fields, since the source determines both. Clearing hands them back.
  • DataSourceIdField now dumps None, so an optional source field's absent default serialises instead of failing the OpenAPI generation.
  • Added an API change log entry (v3.0-39) and extended documentation/features/automations.rst.
  • Added changelog item in documentation/changelog.rst

Built on top of #2554, which rebuilt the same creation form: the picker joins its .chooses-generator fields rather than carrying its own wrapper, clearing a selection folds into its type-change handler, and the submit path goes through its readJsonField.

Look & Feel

Creating a forecast automation that reuses data source 6:

POST /api/v3_0/assets/3/automations
{
  "name": "Day-ahead PV forecasts",
  "cron": "0 6 * * *",
  "type": "forecasting",
  "source": 6,
  "parameters": {"sensor": 2092}
}

The automation's generator_id is 6, so its forecasts are recorded under that source, alongside the data it already holds.

Naming both ways at once is refused:

{"message": {"source": ["data-generator cannot be combined with source: a data source already stores the data generator and the configuration it runs under."]}}

In the UI, clicking into the empty box lists the forecasters; typing narrows them:

Existing data source
[ 2                                        ] [Clear]
  FlexMeasures's TrainPredictPipeline forecaster (data source 2)

Selected data source
FlexMeasures's TrainPredictPipeline forecaster (data source 2)
Configuration
{
    "model": "CustomLGBM",
    "train-period": "P30D",
    "retrain-frequency": "P2D",
    ...
}

On an instance with many generators the list caps at 25 and says how many it leaves out, which is what makes searching by id or by model worth having.

How to test

New tests in flexmeasures/api/v3_0/tests/test_automations_api.py:

Run them with:

pytest flexmeasures/api/v3_0/tests/test_automations_api.py
pytest flexmeasures/api/v3_0/tests/test_sources_api.py
pytest flexmeasures/ui/tests
pytest flexmeasures/cli/tests/test_automations.py

manual test: in automation page press new automation and test selecting a datasource

closes #2587


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

…ne type

Context:
- The listing returned every data source the caller may read, with no way to
  search it or to ask for one kind. A picker offering a choice of forecasters
  therefore had to fetch them all and sift through them in the browser.

Change:
- Add a `filter` of space-separated search terms, matched against a source's
  name, its model and its id prefix, the way the sensors listing already
  searches, and a `type`, which narrows the listing to one source type.
- Move the rule about which sources a user may read into the data-sources
  service, as `get_readable_source_account_ids` and `user_may_read_source`,
  so that it can be applied outside this endpoint without being restated.

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

Context:
- A data source stores a data generator and the configuration it runs under, and
  the results computed under it are recorded on it. `flexmeasures add automation`
  has taken `--source` since the command was written, so several automations can
  share one generator and one lineage of data, but the API could only be handed a
  class and a config, which reaches an existing source by accident, when what it
  builds happens to hash to the same attributes.

Change:
- Accept a `source` on the creation endpoint, deserialized to the DataSource the
  service already knows how to take. It cannot be combined with `data-generator`
  or `config`, which the source itself determines, and a schedule automation
  cannot name one, since it re-resolves its source from the asset and the flex
  config on every run and would move off the named one at the next run.
- Require that the caller may read the source, by the same rule the sources
  listing applies. A source is otherwise a way to compute under, and record on,
  a generator configuration belonging to another organisation. The CLI runs
  without a user and stays trusted, as it already is for sensors.
- Let DataSourceIdField dump None, so that the field's absent default serializes
  rather than failing the OpenAPI generation.

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

Context:
- The creation form asked for a data generator class and a config as free text,
  with no way to say "the one that automation already uses", and nothing showed
  what a generator would be configured with until after the automation existed.

Change:
- Add a search box above those two fields, which lists the data sources of the
  automation's own type, matched by id, by name and by generator class, and
  lists them on focus so they can be browsed without guessing a term.
- Show the picked source's description and the configuration it stores, and
  disable the class and config fields while it is picked, since the source
  already determines both. Clearing it hands them back.
- Drop the whole section for a schedule automation, which has no generator to
  name, and clear a selection when the type changes, as the source type does.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- #2554 rebuilt the same creation form this branch extends, and #2565 renamed
  flexmeasures/api/dev to flexmeasures/api/ui.

Change:
- Take main's creation form and re-apply the picker onto it: the picker joins the
  .chooses-generator fields rather than carrying its own wrapper, clearing a
  selection folds into main's type-change handler, and the submit path goes
  through main's readJsonField.
- Assemble the POST payload before sending it, since a reused source replaces
  both the generator and the config, and follow test_asset_crud's payload
  assertions onto the new lines, adding one for the source branch.
- Keep both sides' tests in test_automations_api, restoring the two parametrize
  decorators the merge separated from their functions.
- Take v3.0-39 in the API change log, as main claimed v3.0-38.
- Share test_automation_actions.automation_script in the picker's browser checks,
  which renders all five of the page's Jinja values rather than three.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@BelhsanHmida BelhsanHmida mentioned this pull request Sep 25, 2026
3 tasks done
@Flix6x Flix6x added this to the 1.1.0 milestone Sep 25, 2026
Comment thread documentation/api/change_log.rst Resolved
Comment thread documentation/changelog.rst Resolved
Comment thread documentation/changelog.rst Resolved
Comment thread documentation/features/automations.rst Resolved
Comment thread flexmeasures/data/schemas/sources.py Resolved
BelhsanHmida and others added 9 commits September 25, 2026 09:00
… covers it

The automations feature has not shipped yet, so the changelog describes where it
ends up rather than the steps it took to get there. Someone reading the v1.1.0
notes never saw a version of the *New automation* form without a data source to
pick, and a bullet announcing that one arrived tells them about a past they have
no memory of.

The per-PR list under *Automations, in detail* is the place where a contribution
is still worth naming, so this PR's number joins the entry that already describes
the creation form and the data source it records under.

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

``POST /assets/<id>/automations`` arrived at v3.0-37, inside this same unreleased
cycle, so no API consumer has ever called it without a ``source``. The release
will simply introduce the endpoint, with that field on it, and an entry saying it
"now accepts" one describes a revision nobody integrated against.

The ``GET /sources`` entry stays. That listing predates this cycle and is already
in users' hands, so a new way to search and narrow it is a change they need told
about.

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

The field's description had grown into a paragraph, restating what a data source
stores, how the results are attributed to it, and why a schedule automation may
not name one. All three are already in the automations documentation, where they
have room to be explained, and none of them help someone reading the OpenAPI
reference to decide what to put in the field.

One sentence is left: it is the ID of a data source to reuse, in place of naming
a generator and a config.

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

Giving the field a ``load_default`` of None put a default into the OpenAPI spec,
which the generator then pushed back through the field's own serializer. A field
that turns an ID into a DataSource has nothing to do with None, so it raised, and
the way out taken at the time was to teach it to swallow None -- changing the
field for every schema that uses it, to accommodate one field's setup.

The field is simply not required, so say that instead. Nothing then asks for a
default, the serializer goes back to doing one thing, and the published schema
improves as well: ``source`` is now an integer that may be omitted, rather than a
nullable integer defaulting to null.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
The API changelog entries of the two lines each keep a section of their own, and the page entry is main's, which now covers what both say.

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>
The creation form carried nine blocks of help text, several of them three to
five lines, so the fields it was meant to explain were spread thin between
paragraphs and the modal read as a page of prose with inputs in it.

Each field's label now carries the info icon the sensor creation form already
uses, with the same text behind it on hover. Two hints stay on the page, because
they are the ones you act on while typing rather than read once: that the
generator config can be left empty, and the offset example worth copying into
the parameters. Bootstrap only turns those icons into tooltips when asked, so the
page now initialises them, as the chart pages do.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Bootstrap appends a tooltip to the document body, where it sits at a z-index of
1080. The theme gives a modal 999993. A tooltip opened from a field inside the
creation form was therefore built, shown and positioned correctly, and painted
behind the modal, so hovering an info icon appeared to do nothing at all.

Anchoring each tooltip to the modal that holds its icon puts the two in one
stacking context, and the tooltip comes out on top. Icons outside a modal keep
the body as their container.

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

The jobs column counts what the job cache returns, and that cache holds only the
jobs Redis still has: FLEXMEASURES_JOB_TTL expires them, and the cache drops the
ids it can no longer fetch. A daily automation two weeks old therefore shows a
week of jobs on an instance that keeps them for a week, which reads as runs that
never happened rather than as records that have been cleaned up.

The column and the info panel now say so, naming the window this instance is
configured for rather than pointing at the setting. The phrasing works for either
shape the value takes, since a one-day retention reads as "a day" and not "1 day".

The browser checks render the page's script through a bare Jinja environment, so
that environment now carries the filter and the configuration value the page
reads, keeping its promise that nothing is left unrendered.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
Flix6x and others added 5 commits October 1, 2026 11:12
…pes unnarrowed

A search from the automation form asked for every source a user may read, in whatever order Postgres returned them,
so an instance with many sources sent its whole source table to the browser,
and the sources shown could differ between two searches for the same term.
The listing is now ordered with the most recently created source first, and takes a limit.

The source types in the response are read from every source the user may read, rather than from the sources the call returns,
so that narrowing the listing no longer narrows the types a client can offer to narrow it by.

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>
The search now asks for one source more than the list shows, which is what tells the list that there are more to narrow down to,
and which keeps an instance with many sources from sending all of them to the browser on every pause in typing.
The list therefore says that it leaves sources out rather than how many, which is no longer a number it has.

A source picked and then left behind by closing the form used to still disable the data generator fields when the form was opened again,
with nothing on screen to say why, so closing the form now clears the picked source.

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>
A client which fills in the whole creation form and leaves the source field empty sends a null source,
which was refused with 'Field may not be null' rather than taken as the automation setting up its own data generator.
Our own form works around this by leaving the field out altogether.

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>
…urces

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>
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 1, 2026 •

Copy link
Copy Markdown
Member

I reviewed this and pushed the fixes I found, rather than handing back a list. Five commits, each on its own point:

0d7cb82 — GET /api/v3_0/sources: ordering, a limit, and unnarrowed types. The listing had neither ORDER BY nor LIMIT, while the 25-item cap lived in the browser. Focusing the empty search box searches with no term, so on an instance with many sources the whole source table went over the wire, and again on every pause in typing; without an order, the 25 shown could differ between two searches for the same term, and a source past the cap could stay out of reach however the search was narrowed. The listing is now ordered with the most recently created source first and takes an optional limit. The limit is opt-in rather than a default, so no existing client of this endpoint starts getting a truncated list.

The response's types were being read from the already-filtered rows, so ?type=forecaster returned only the default types and silently dropped any other type present in the database — awkward for a client using one call both to search and to offer the types to search by, which is what the endpoint's description invites. They are now read from every source the caller may read.

ea9a9c4 — the picker asks for one source more than it shows. That is what tells the list there are more to narrow down to, and it is what bounds the fetch. The list consequently says that it leaves sources out rather than how many, since the count behind the limit is no longer a number it has. Closing the creation form now also clears a picked source: one left behind used to disable Data generator and Data generator config when the form was opened again, with nothing on screen to say why.

74f98e0 — "source": null is read as no source. It was refused with Field may not be null, where data-generator beside it is allow_none. Our own form avoids this by leaving the key out entirely; a client that fills in the whole form and leaves the source empty should not have to know that.

68fa7d8 — the API change log. The new source field on POST /api/v3_0/assets/<id>/automations and the new filter, type and limit parameters on GET /api/v3_0/sources were undocumented (the file's only change was an added blank line, now gone).

cf2177c drops a local from marshmallow import ValidationError that shadows the module-level one.

Tests: three added (the limit and its ordering, the types surviving a filter, a null source accepted) plus two amended JavaScript checks, each confirmed to fail without the change it covers. test_sources_api.py, test_automations_api.py, flexmeasures/ui/tests/js/, test_automations_fresh_db.py, test_plugin_automations_fresh_db.py and test_asset_crud.py are green locally.

One thing I did not change here:

One thing worth knowing: source_search_term_filter's docstring claimed to match a source's description, which is a Python property rather than a column, so I corrected it in the first commit. sourceLabel() shows exactly that description, so retyping what the list showed you still matches nothing. Making the displayed label searchable is a separate change, which I have left alone.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

Flix6x and others added 2 commits October 1, 2026 12:20
…nisation alone

Almost every data generator's source belongs to no organisation, because nothing on the creation path passes one,
and a source naming neither an organisation nor a user was readable by every authenticated user.
The read check this pull request added therefore hardly ever refused anything.

A source is now readable when it belongs to an organisation the user may read,
when an automation they may read computes under it,
or when it has recorded data on a sensor they may read, which is what makes it possible to ask what produced a number one is allowed to see.
The first two are what makes a source one to work with: those are the sources the listing holds,
because reusing a source means recording under its lineage, which is more than reading what it computed.
The stored configuration follows the stricter rule as well, since a data generator's configuration names the sensors it runs on,
which can be sensors of an organisation whose data the user cannot see at all.

Two tests created sources belonging to no organisation to exercise version collapsing rather than visibility;
their fixtures name an organisation now.

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>
…o work with

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 1, 2026

Copy link
Copy Markdown
Member

Following up on the thread about which data sources a user may read (#2625), I have implemented the three conditions discussed there, in the shape we settled on: a source is readable when any of them holds, while the listing and the stored configuration need the stricter two.

The three conditions, and which question each one answers. They answer two different questions, and the stronger act deserves the stricter rule:

  • May I see what computed this data? — a source is readable when it belongs to an organisation you may read (1), when an automation you may read computes under it (3), or when it has recorded data on a sensor you may read (2). The last one is what makes "what produced this number?" answerable about data you are allowed to see.
  • May I reuse this generator? — only (1) and (3). Reusing a source means your automation records under someone else's lineage, which is more than reading what it computed. GET /api/v3_0/sources lists exactly these, because that listing is what a source is picked from.

attributes follow the stricter rule. A data generator's configuration names the sensors it runs on, which can be sensors of an organisation whose data you cannot see at all — something the data itself never reveals. So GET /api/v3_0/sources/<id> serves the record under (1), (2) or (3), but hands over attributes only under (1) or (3).

The two filters live in flexmeasures/data/services/data_sources.py as usable_source_filter() and readable_source_filter(), with user_may_use_source() and user_may_read_source() on top of them, so the same rule backs the listing, the single read and the automation's 403.

What changes in practice. Condition 1 is implemented as it was worded — coupled to an organisation you may read — so the previous blanket grant for a source naming neither an organisation nor a user is gone. That blanket was the reason the 403 on this PR hardly ever fired: every data generator's source is account-less today, because nothing on the creation path passes an organisation to get_or_create_source (automation_copies.py:283 is the sole exception). Two consequences worth stating plainly:

  • A source that backs one of your own automations stays reusable, through condition 3. That is the picker's main case: you reuse the setup of an automation you can already see.
  • A source created outside an automation — a flexmeasures add forecasts run, say — is account-less and backs no automation, so it is readable through condition 2 but no longer offered for reuse. Couple a data generator's data source to the organisation it runs for #2625's backfill is what gives such a source an organisation, after which condition 1 covers it again. The two are shipping together in v1.1.

Tests. Three added, each verified red without its own fix: a source belonging to no organisation is neither listed nor readable; a source of another organisation that recorded on a sensor you may read is readable without its attributes, and stays out of the listing; an automation of your own makes its source both listed and fully readable. Two existing tests created account-less sources to exercise version collapsing rather than visibility, so their fixtures now name an organisation.

Docs: the access rules on both endpoints, the API change log, the automations feature page, and a changelog entry under Infrastructure / Support, since this narrows who may read a data source.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

…ad it

The listing a source is picked from holds the sources the user may work with,
while reading one source is allowed more widely, for a source which recorded data on a sensor they may read.
Creating the automation checked the wider of the two, so a source that had recorded on one of the user's own sensors
could be named for an automation of theirs, which records under that source and so writes into another organisation's lineage.

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 1, 2026

Copy link
Copy Markdown
Member

One more, found while re-reading my own change: creating the automation was checking the readable rule, not the usable one.

That is the inconsistency the split exists to avoid. A source that had recorded data on one of your own sensors was readable, and the creation guard accepted it — so you could point an automation of yours at another organisation's source and record under its lineage, which is exactly what the listing refuses to offer you. _create_builtin_automation now calls user_may_use_source, and the message says what the rule is: not yours to work with, rather than which you cannot read yourself.

A test pins it: a foreign source that has recorded on the user's own sensor reads back with a 200 and is refused for an automation with a 403, verified red under the old check.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

It changes data-source access-control semantics (previously public sources become non-readable) across the API, UI, and a shared service, which is security-sensitive and warrants final human review.

Review effort: Balanced
Findings: 2 Low severity

Open (2)
What changed in this PR

This PR lets the API and UI reuse an existing data source when creating a forecast or report automation, bringing them in line with the CLI's long-standing --source option. Naming a source makes the automation reuse that source's data generator, configuration, and data lineage. It also generalizes the data-source read/reuse access rules into a shared service (data_sources.py) and extends GET /sources with filter/type/limit query parameters so the UI can offer a searchable source picker.

Changes:

  • Add a source field to AutomationCreationSchema (mutually exclusive with data-generator/config, refused for schedule automations), plus a service-side permission check that the caller may use the named source.
  • Centralize per-source access rules into usable_source_filter/readable_source_filter (and user_may_use_source/user_may_read_source), so a no-organisation source is no longer readable by everyone; attributes are only returned for usable sources.
  • Add a searchable/type-filtered/limited GET /sources, and a UI source picker in the New automation form, with tests, docs, and changelog entries.
File Description
flexmeasures/​data/​schemas/​automations.py Adds source field and cross-field validation refusing it alongside generator/config or for schedules
flexmeasures/​data/​services/​data_sources.py New usable/readable source filters and user_may_* helpers
flexmeasures/​api/​v3_0/​sources.py New filter/type/limit params; access rules moved to the service; per-source read/attribute gating
flexmeasures/​data/​services/​automations.py Permission check that a named source is the user's to work with
flexmeasures/​api/​v3_0/​assets.py OpenAPI example of reusing a source
flexmeasures/​ui/​templates/​assets/​asset_automations.html Source picker UI and submit logic; info-icon tooltips
flexmeasures/​ui/​static/​openapi-specs.json Regenerated spec for new params/field
flexmeasures/​ui/​tests/​..., flexmeasures/​api/​v3_0/​tests/​... Tests for the picker, access rules, and reuse behavior
documentation/​* Feature docs, main changelog, and API changelog entries

I verified the schema validator reads the correct post-load attribute names, the models/columns referenced by the new SQL filters exist, the permission check runs before any DB mutation, the api_message pattern is consumed by the auth error handler, and the test fixtures/imports resolve. I found no functional bugs; the two comments are minor (a mid-phrase comment wrap that the repo's comment convention disallows, and a slightly inaccurate inline comment).


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread flexmeasures/api/v3_0/sources.py Resolved
…omment

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 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.

The picker and the permission model both look right to me now.

Since my earlier review: the source listing is ordered and bounded, its types survive the filters, the picker asks for no more than it shows and opens on a clean slate, an empty source is read as no source, and the API change log covers the new surface. The access rules are the three conditions from the thread above, split so that reading what a source computed is weaker than working with the source, with the stored configuration and the creation guard both on the stricter side.

Two things deliberately left for their own issues: #2630, where automations of plugin types still bypass the source check, and #2625, which gives data generators' sources an organisation and so makes the first condition select anything at all.

@Flix6x
Flix6x merged commit d0903d0 into main Oct 1, 2026
13 checks passed
@Flix6x
Flix6x deleted the feat/automation-data-source-picker branch October 1, 2026 11:02
@Flix6x
Flix6x restored the feat/automation-data-source-picker branch October 1, 2026 11:53
Flix6x added a commit that referenced this pull request Oct 3, 2026
* api/v3_0/sources: search the data sources listing, and narrow it to one type

Context:
- The listing returned every data source the caller may read, with no way to
  search it or to ask for one kind. A picker offering a choice of forecasters
  therefore had to fetch them all and sift through them in the browser.

Change:
- Add a `filter` of space-separated search terms, matched against a source's
  name, its model and its id prefix, the way the sensors listing already
  searches, and a `type`, which narrows the listing to one source type.
- Move the rule about which sources a user may read into the data-sources
  service, as `get_readable_source_account_ids` and `user_may_read_source`,
  so that it can be applied outside this endpoint without being restated.

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

* api/v3_0/assets: let an automation name the data source it computes under

Context:
- A data source stores a data generator and the configuration it runs under, and
  the results computed under it are recorded on it. `flexmeasures add automation`
  has taken `--source` since the command was written, so several automations can
  share one generator and one lineage of data, but the API could only be handed a
  class and a config, which reaches an existing source by accident, when what it
  builds happens to hash to the same attributes.

Change:
- Accept a `source` on the creation endpoint, deserialized to the DataSource the
  service already knows how to take. It cannot be combined with `data-generator`
  or `config`, which the source itself determines, and a schedule automation
  cannot name one, since it re-resolves its source from the asset and the flex
  config on every run and would move off the named one at the next run.
- Require that the caller may read the source, by the same rule the sources
  listing applies. A source is otherwise a way to compute under, and record on,
  a generator configuration belonging to another organisation. The CLI runs
  without a user and stays trusted, as it already is for sensors.
- Let DataSourceIdField dump None, so that the field's absent default serializes
  rather than failing the OpenAPI generation.

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

* ui/assets: pick a data source for a new automation, and see what it stores

Context:
- The creation form asked for a data generator class and a config as free text,
  with no way to say "the one that automation already uses", and nothing showed
  what a generator would be configured with until after the automation existed.

Change:
- Add a search box above those two fields, which lists the data sources of the
  automation's own type, matched by id, by name and by generator class, and
  lists them on focus so they can be browsed without guessing a term.
- Show the picked source's description and the configuration it stores, and
  disable the class and config fields while it is picked, since the source
  already determines both. Clearing it hands them back.
- Drop the whole section for a schedule automation, which has no generator to
  name, and clear a selection when the type changes, as the source type does.

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

* ui/assets: copy an automation into the creation form

The Automations page could create an automation from scratch or edit one's
recurrence, but a variation on an existing automation, such as the same
forecast on a different schedule, had to be typed out again from its details.

Each row's Actions menu now offers Copy, which opens the creation form with
the original's type, recurrence, timezone and parameters filled in. Nothing
is created until the form is submitted, so the copy is a starting point that
can still be changed, rather than a command of its own.

The copy is filled in inactive, so that a variation being worked on does not
start running while it is still being edited, and so that two automations
writing the same results never appear from a single click. Its name carries a
(copy) suffix, cut to the 80 characters the field and the API allow, so that a
long name loses its tail rather than the suffix that marks it.

A forecast or report copy reuses the data source the original computes under,
which already stores the data generator and its configuration, rather than
naming that class and config again. A schedule automation resolves its source
afresh on every run and cannot name one, so a copy of one names none.

The parameters are carried over exactly as stored, which is safe here because
the copy stays on the same asset, so the sensors they name stay valid and the
creation endpoint checks them against the user's permissions as it would for
any new automation.

The creation form is now shared between New automation and Copy, so opening it
blank clears what a copy left in it, rather than inheriting the last copy's
settings.

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

* docs/changelog: point the data source picker entries at PR #2584

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

* docs/changelog: fold the copy button into the automations UI entry

Automations have not shipped, so the changelog says what the feature is
rather than how it got there: the entry that already covers creating and
managing automations in the UI names Copy among the row's controls and
carries this PR's number, instead of a bullet of its own.

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

* docs/changelog: carry this work on the automations entry that already covers it

The automations feature has not shipped yet, so the changelog describes where it
ends up rather than the steps it took to get there. Someone reading the v1.1.0
notes never saw a version of the *New automation* form without a data source to
pick, and a bullet announcing that one arrived tells them about a past they have
no memory of.

The per-PR list under *Automations, in detail* is the place where a contribution
is still worth naming, so this PR's number joins the entry that already describes
the creation form and the data source it records under.

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

* docs/api: leave the automations endpoint to the release that introduces it

``POST /assets/<id>/automations`` arrived at v3.0-37, inside this same unreleased
cycle, so no API consumer has ever called it without a ``source``. The release
will simply introduce the endpoint, with that field on it, and an entry saying it
"now accepts" one describes a revision nobody integrated against.

The ``GET /sources`` entry stays. That listing predates this cycle and is already
in users' hands, so a new way to search and narrow it is a change they need told
about.

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

* data/schemas/automations: say what the source field is, not what it implies

The field's description had grown into a paragraph, restating what a data source
stores, how the results are attributed to it, and why a schedule automation may
not name one. All three are already in the automations documentation, where they
have room to be explained, and none of them help someone reading the OpenAPI
reference to decide what to put in the field.

One sentence is left: it is the ID of a data source to reuse, in place of naming
a generator and a config.

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

* data/schemas: let the source field be plainly optional, and leave DataSourceIdField alone

Giving the field a ``load_default`` of None put a default into the OpenAPI spec,
which the generator then pushed back through the field's own serializer. A field
that turns an ID into a DataSource has nothing to do with None, so it raised, and
the way out taken at the time was to teach it to swallow None -- changing the
field for every schema that uses it, to accommodate one field's setup.

The field is simply not required, so say that instead. Nothing then asks for a
default, the serializer goes back to doing one thing, and the published schema
improves as well: ``source`` is now an integer that may be omitted, rather than a
nullable integer defaulting to null.

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

* ui/assets: move the automation form's reference text onto info icons

The creation form carried nine blocks of help text, several of them three to
five lines, so the fields it was meant to explain were spread thin between
paragraphs and the modal read as a page of prose with inputs in it.

Each field's label now carries the info icon the sensor creation form already
uses, with the same text behind it on hover. Two hints stay on the page, because
they are the ones you act on while typing rather than read once: that the
generator config can be left empty, and the offset example worth copying into
the parameters. Bootstrap only turns those icons into tooltips when asked, so the
page now initialises them, as the chart pages do.

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

* ui/assets: raise the form's tooltips above the modal they belong to

Bootstrap appends a tooltip to the document body, where it sits at a z-index of
1080. The theme gives a modal 999993. A tooltip opened from a field inside the
creation form was therefore built, shown and positioned correctly, and painted
behind the modal, so hovering an info icon appeared to do nothing at all.

Anchoring each tooltip to the modal that holds its icon puts the two in one
stacking context, and the tooltip comes out on top. Icons outside a modal keep
the body as their container.

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

* ui/assets: say that the job counts reach back only as far as Redis keeps jobs

The jobs column counts what the job cache returns, and that cache holds only the
jobs Redis still has: FLEXMEASURES_JOB_TTL expires them, and the cache drops the
ids it can no longer fetch. A daily automation two weeks old therefore shows a
week of jobs on an instance that keeps them for a week, which reads as runs that
never happened rather than as records that have been cleaned up.

The column and the info panel now say so, naming the window this instance is
configured for rather than pointing at the setting. The phrasing works for either
shape the value takes, since a one-day retention reads as "a day" and not "1 day".

The browser checks render the page's script through a bare Jinja environment, so
that environment now carries the filter and the configuration value the page
reads, keeping its promise that nothing is left unrendered.

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

* Order the data source listing, let a client limit it, and keep its types unnarrowed

A search from the automation form asked for every source a user may read, in whatever order Postgres returned them,
so an instance with many sources sent its whole source table to the browser,
and the sources shown could differ between two searches for the same term.
The listing is now ordered with the most recently created source first, and takes a limit.

The source types in the response are read from every source the user may read, rather than from the sources the call returns,
so that narrowing the listing no longer narrows the types a client can offer to narrow it by.

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>

* Bound the source search, and open the automation form on a clean slate

The search now asks for one source more than the list shows, which is what tells the list that there are more to narrow down to,
and which keeps an instance with many sources from sending all of them to the browser on every pause in typing.
The list therefore says that it leaves sources out rather than how many, which is no longer a number it has.

A source picked and then left behind by closing the form used to still disable the data generator fields when the form was opened again,
with nothing on screen to say why, so closing the form now clears the picked source.

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>

* Read an empty source on an automation as no source at all

A client which fills in the whole creation form and leaves the source field empty sends a null source,
which was refused with 'Field may not be null' rather than taken as the automation setting up its own data generator.
Our own form works around this by leaving the field out altogether.

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>

* Changelog: the source an automation reuses, and searching the data sources

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>

* Drop a local import of ValidationError which the module already has

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>

* Decide who may read a data source per source, rather than by its organisation alone

Almost every data generator's source belongs to no organisation, because nothing on the creation path passes one,
and a source naming neither an organisation nor a user was readable by every authenticated user.
The read check this pull request added therefore hardly ever refused anything.

A source is now readable when it belongs to an organisation the user may read,
when an automation they may read computes under it,
or when it has recorded data on a sensor they may read, which is what makes it possible to ask what produced a number one is allowed to see.
The first two are what makes a source one to work with: those are the sources the listing holds,
because reusing a source means recording under its lineage, which is more than reading what it computed.
The stored configuration follows the stricter rule as well, since a data generator's configuration names the sensors it runs on,
which can be sensors of an organisation whose data the user cannot see at all.

Two tests created sources belonging to no organisation to exercise version collapsing rather than visibility;
their fixtures name an organisation now.

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>

* Changelog and docs: which data sources are yours to read, and which to work with

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>

* Naming a source for an automation takes more than being allowed to read it

The listing a source is picked from holds the sources the user may work with,
while reading one source is allowed more widely, for a source which recorded data on a sensor they may read.
Creating the automation checked the wider of the two, so a source that had recorded on one of the user's own sensors
could be named for an automation of theirs, which records under that source and so writes into another organisation's lineage.

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>

* Address review: say which sources the types come from, and unwrap a comment

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>

* Changelog: one entry for the automations page's actions, not two

The merge of main brought in the entry PR #2563 had extended, beside this branch's own edit of the same line.
Neither conflicted with the other, so both survived: the section carried the same 900-character bullet twice,
differing only in whether it mentioned Copy and which pull requests it cited.

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>

---------

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 <noreply@anthropic.com>
@Flix6x
Flix6x deleted the feat/automation-data-source-picker branch October 3, 2026 17:42
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.

Let an automation reuse an existing data source, in the API and the UI

3 participants