Skip to content

feat: Fill in a report template from the New automation form - #2797

Merged
nhoening merged 279 commits into
mainfrom
feat/2642-report-template-ui
Dec 5, 2026
Merged

nhoening merged 279 commits into
mainfrom
feat/2642-report-template-ui

Conversation

@BelhsanHmida

Copy link
Copy Markdown
Contributor

Description

#2300 adds prepared report templates, but using one meant exporting the YAML, looking up sensor IDs elsewhere and replacing every FILL_IN by hand.

This PR lets the New automation form do that. For example, on the Reporting tab, New automation → Self consumption shows three sensor searches (production, consumption, output) and the window -1D,DB to DB. Picking the three sensors and pressing Create gives a working daily self-consumption report.

Report templates in the form

  • GET /api/v3_0/report-templates (and /report-templates/<name>) lists the templates, with a placeholders list describing each FILL_IN: its section, location, kind (sensor or value) and a label.
  • With type reporting, a Report template list offers the templates by name in words (Aggregate sensors, Dynamic tariff, Self consumption, …). Hovering over one shows what it does in a sentence, e.g. Dynamic tariff: "Your energy price: the day-ahead price plus taxes, a supplier surcharge and VAT."
  • Choosing one lays out a field per placeholder: a sensor search for sensors, listing those on the asset (and below) first, then the other sensors the user can read, such as a public price sensor. Other placeholders, like the bounds in valid-range, get a plain input with a label that says what to fill in ("Lowest valid value", not "0").
  • For Aggregate sensors, Add input adds another sensor before the output, and each input has a weight next to it: 1 adds it, -1 subtracts it, e.g. heat pump + lighting − EV charger. Weights other than 1 are stored in the config's weights.
  • The template's rolling window is pre-filled and can be changed. The name is filled in from the template when left empty.
  • Create is refused while a field is empty, naming it. The API refuses a reporting automation with a FILL_IN left as well, per section, e.g. config: Replace the 'FILL_IN' template placeholder at transformations[0].args[0].
  • Edit as JSON instead copies the template, as filled in so far, into the data generator, config and parameters fields.
  • How the form presents a template lives in the template itself, under form: (a summary, labels, and add_inputs/input_weights for Aggregate sensors). The CLI ignores it.

Type defaults in the New automation form

  • New automation starts with the type of the tab in front: the Reporting tab starts a report, the Scheduling tab a schedule. A plugin-defined type's tab, which the form cannot create, falls back to forecasting, as before.

  • The empty data generator, config and parameters fields show each type's own example. With reporting they showed a forecast's (TrainPredictPipeline, a CustomLGBM config, {"sensor": 2092}); now a PandasReporter with input and output sensors and a previous-day window. scheduling shows a next-day window, and a plugin type shows none. These are placeholders only: nothing is sent unless typed.

  • Closing the form keeps what was typed, so New automation reopens the draft, with its type, rather than emptying it, even from another tab. Clear form starts afresh with the tab's type. Choosing only a type does not make a draft, and a copy only becomes one once something in it is changed.

  • Docs: the template flow in the reporting docs, the tab type and draft in the automations docs.

Look and feel

Template list, with the summary on hover:

0-template-list-hover

Self consumption, with its sensors picked:

2-self-consumption-template

Sensor search, with the asset's own sensors first:

3-sensor-search

Aggregate sensors, with an added input weighted -1:

7-weighted-sum

How to test

pytest flexmeasures/api/v3_0/tests/test_report_templates_api.py flexmeasures/data/tests/test_report_template_placeholders.py
pytest flexmeasures/ui/tests/js/test_automation_new_form.py flexmeasures/ui/tests/js/test_automation_copy.py

Or by hand: open an asset's Automations page, go to the Reporting tab, press New automation, choose Self consumption, pick the three sensors and create it. Run now fills the output sensor with the share of production consumed on-site. Then try Aggregate sensors with an added input weighted -1, and close the form half-filled and reopen it from another tab.

Further improvements

  • The template YAML explains each placeholder in a comment (e.g. "The sensor recording produced power/energy (positive values)."), which is lost when the YAML is loaded. Moving those into form.labels or a help text would let the form show them.
  • self-consumption, daily-energy and regularise resample to a fixed 1D or 15min, and ask you to edit that to match the output sensor's resolution. The form can't do that yet.

Related Items

Stacked on #2300.

closes #2642


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

Verify that retrying a failed partial queueing attempt does not create duplicate jobs.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Convert YAML dates to ISO strings, accept empty files, and report non-object config or parameter files as usage errors.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Test YAML dates and timestamps, empty files, and invalid top-level list values for automation options.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Hide automation names and IDs from asset job responses when the current user cannot read the source automation.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Verify inaccessible automation provenance is redacted while authorized callers still receive the full identity.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Show a persistent API error instead of presenting failed automation requests as an empty list.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Check that the automations page renders the warning target and hides the table when loading fails.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Avoid interpreting cron wildcard asterisks as RST italic markup when generating OpenAPI documentation.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Verify cron wildcards remain unchanged while ordinary italic markup is still converted.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Document five-field cron expressions, at-most-once queueing attempts, and the automations API endpoint.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Record the automation endpoints, authorization-aware provenance, and five-field runner behavior.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Include the created_via field in the asset jobs OpenAPI example.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Add the trigger method required by the forecasting CLI to the regressor parsing test double.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Bring the forecast automation branch up to date with current main while preserving both branches' CLI, API, documentation, dependency lock, and generated OpenAPI changes.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Require every automation to reference a data generator and prevent deleting a data source while an automation still depends on it, matching the retention policy for belief and annotation sources.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Verify referenced generators cannot be deleted, generator references cannot be cleared, and automation API fixtures always use valid generators.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Allow forecast output only on the automation asset or its descendants and revalidate that relationship before every scheduled run, including explicit sensor-to-save targets.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Cover same-asset, child, grandchild, ancestor, and unrelated output targets, explicit sensor-to-save behavior, and runtime revalidation after an asset is moved.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Document output-sensor scope, runtime relationship checks, and the requirement to retain a generator while its automation exists.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Join the automation and main Alembic branches so installations have a single database upgrade target.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- PR #2293 is stacked on the reviewed forecast automation branch.

Change:
- Merge the reviewed parent and preserve schedule automation behavior across the five semantic conflicts.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Schedule automations do not use a data generator, but the reviewed forecast automation schema required one.

Change:
- Make the foreign key nullable while retaining a database check that forecasts always have a generator.
- Add a forward migration without weakening data-source deletion semantics.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Review uncovered untested forecast-only options, invalid durations, and DST start calculation.

Change:
- Add CLI and trigger-preparation regressions while retaining forecast sensor validation.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Persistence, inherited flex configuration, descendant statistics, and job counts lacked realistic coverage.

Change:
- Move automation service tests under the fresh-database fixture and add end-to-end schedule regressions.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- API provenance was not asserted across asset and sensor schedule jobs.

Change:
- Verify API trigger metadata on sequential descendants, wrap-up jobs, and sensor jobs.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- The asset automation page tests still targeted the former single table.

Change:
- Assert type-specific tables, error rendering, filters, and hidden-tab column adjustment.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Minimal asset schedules lost stored defaults, invalid timing could execute, provenance was incomplete, and descendant jobs were miscounted.

Change:
- Validate and floor fixed durations safely, inherit stored flex configuration, preserve provenance, and count the jobs actually dispatched.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- Schedule automation creation silently accepted forecaster settings that could never affect scheduling.

Change:
- Detect supplied forecast-only options and return a user-facing usage error.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- DataTables initialized in the hidden schedule tab could render with stale column widths.

Change:
- Add tab accessibility state and adjust initialized table columns when a tab becomes visible.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context:
- The feature guide linked only to the API root and omitted fixed-start and duration constraints.

Change:
- Document canonical fields, runtime start behavior, timing validation, and generator-free schedule automations.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…rom GET /api/v3_0/sensors

Units are free text, so the units in use are checked once each, and the sensors filtered on those that fit.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
A template's form block lists the unit or dimension a sensor field takes, and the fields whose sensors share a unit;
the placeholder descriptions carry this, for the New automation form to narrow its sensor searches to.

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

A field searches for the unit or dimension its template says it takes, and a field sharing a unit with others follows the sensor picked for one of them;
an input added to a sum takes what the template's own inputs take.

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

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

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

Copy link
Copy Markdown
Contributor Author

All five are in. The template key test lets parameters keep the reporters' own snake_case fields (check_output_resolution, exclude_source_types) through the DataGenerator list, since renaming those is an API change of its own.
Each sensor field now gets a unit or dimension from the template's form, filtered server-side with new compatible-unit and unit-dimension params on GET /api/v3_0/sensors, so paging still works.
same-unit means the exact unit, as PandasReporter takes values as they are, and only Aggregate sensors uses compatible-units, as the AggregatorReporter converts.
The CLI using the service check is in #2939.

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

Review of #2797 — fill in a report template from the New automation form

Reviewed live in demo session 20261204-100112-flexmeasures-2797-a-report-template-in-t
(script demo.script, base http://localhost:5097, toy battery asset 6).
PR: #2797 (branch feat/2642-report-template-ui, stacked on #2300, closes #2642).

What the PR does

#2300 added prepared report templates, but using one meant exporting the YAML, looking up sensor IDs
elsewhere and replacing every FILL_IN by hand. #2797 lets the New automation form do that:

  • GET /api/v3_0/report-templates and /report-templates/<name> list the packaged templates and
    describe each FILL_IN placeholder (section, path, location, kind sensor/value, label,
    and which sensors fit it) — flexmeasures/api/v3_0/report_templates.py,
    flexmeasures/data/services/report_templates.py.
  • With type reporting, the form offers the templates by name in words, one field per placeholder:
    a sensor search for sensor placeholders (the asset's own sensors first, narrowed to fitting units
    through the new compatible-unit / unit-dimension filters on GET /api/v3_0/sensors,
    flexmeasures/api/v3_0/sensors.py + unit_fits in flexmeasures/utils/unit_utils.py), a plain
    labelled input for value placeholders. Create is refused while a field is empty, naming it, and
    the API refuses a reporting automation with any FILL_IN left, naming each per section
    (refuse_unfilled_placeholders, called from _prepare_builtin_automation in
    flexmeasures/data/services/automations.py).
  • The template's rolling window is pre-filled, the name is filled in from the template, and
    Edit as JSON instead copies the filled-in template into the raw fields. The form presentation
    lives in the template's own form: block (summary, labels, units, same-unit,
    add_inputs/input_weights), which the CLI ignores.
  • Type defaults in the form: the tab's type, per-type example placeholders in the empty fields, and
    closing the form keeps the draft.

Seen working in the demo

On the toy battery (asset 6, sensors Production 500 kW, Consumption 200 kW, hourly, 3 days;
Self-consumption share, dimensionless, daily): New automation → reporting → self-consumption
laid out the three sensor searches, each listing this asset's sensors first; picking Production,
Consumption and Self-consumption share and pressing Create posted a reporting automation whose
parameters are the template with the three FILL_IN filled in and the window -1D,DB to DB.
Running it once (fm jobs run-automation --automation 1) succeeded live during the demo (the reviewer ran it
directly after the cli steps, which replay the rehearsal log): automation run 1 is succeeded, and the output
sensor Self-consumption share (12) holds the computed share 0.4 — 200 of 500 kW — at event_start
2026-12-03 00:00 Europe/Amsterdam (2026-12-02 23:00 UTC), i.e. exactly the previous day, the -1D,DB to DB
window the template pre-filled.

Requested changes

  1. No Create-time unit check across same-unit placeholders. The sensor searches are narrowed per
    placeholder (unit_fits: a sensor in kWh also "fits" kW, since a flow and its stock convert knowing
    the resolution), and while picking through the form a same-unit field follows the exact unit already
    picked in its group (e.g. in self-consumption.yaml, parameters.input[0].sensor and
    parameters.input[1].sensor). But nothing re-checks at Create time that same-unit placeholders ended up
    in the same unit — a path that bypasses the search (the JSON view, a reopened draft) can still submit
    mismatched units, which then only misbehave at run time in the elementwise clip/div.
    Decided in the demo (note n8): Create should refuse these, naming the fields (like it already refuses an
    empty field); unparseable value placeholders (e.g. valid-range bounds, cf. is_parseable_quantity in
    flexmeasures/utils/unit_utils.py) should be refused the same way.
  2. Value placeholders are typed by hand, and the template's config is not editable inline (only
    via Edit as JSON instead). The labels ("Lowest valid value") help, but nothing validates a value
    placeholder's format while typing (e.g. the valid-range bounds must parse as a quantity —
    is_parseable_quantity exists in flexmeasures/utils/unit_utils.py and could be reused). Please
    validate value placeholders client-side before Create, or say why hand-typed is the intended split.
  3. No help on the timing fields. The Start/End offset inputs (#automationTemplateTiming) have no tooltip or
    link, and the offset-chain syntax (-1D,DB = pandas offset aliases plus FlexMeasures' DB/HB day/hour-begin
    steps, applied by apply_offset_chain() in flexmeasures/utils/time_utils.py) is not common knowledge.
    We fix this ourselves after the demo (recorded in the follow-ups: a tooltip/help icon on both fields linking to
    the "Timing each run" section of the automations docs,
    https://flexmeasures.readthedocs.io/en/latest/features/automations.html#automation-timing, and the pandas offset
    aliases, https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases, pushed to the
    PR branch feat/2642-report-template-ui).
  4. The explanatory comments in the template YAML are lost when loaded (e.g. "The sensor recording
    produced power/energy (positive values)." in self-consumption.yaml), and self-consumption,
    daily-energy and regularise resample to a fixed 1D/15min that must match the output
    sensor's resolution, which the form cannot edit. Both are listed in the PR's own "Further
    improvements"; please either move the comments into form.labels/help text and let the form set
    the resample resolution, or track them as follow-up issues so they don't get lost.

Suggestions (not blocking)

  • Edit as JSON instead is one-way by design (#automationTemplateToJson copies the filled-in template into
    the raw fields and clears the template selection); the only way back to the template fields is re-picking
    the template, which lays them out afresh. Decided in the demo (note n8): fine for now, but the button should
    carry a tooltip saying the switch is one-way, so users can make a better choice. We can add that tooltip
    ourselves and push it to the PR branch after the demo (offered during the demo).
  • The placeholders descriptions are a good API; consider also exposing the template's form.summary
    in the listing response so clients need not load each template to show the hover text.

Notes from the demo (session 20261204-100112-flexmeasures-2797-a-report-template-in-t)

  • n1 (step 19): where the template asks for the same unit — form.same-unit / form.units in
    self-consumption.yaml, exposed per placeholder by GET /api/v3_0/report-templates/<name> and applied
    live by the narrowed sensor search (sensorUnitFilter).
  • n2/n3 (step 23): tooltip with docs link on the timing fields — agreed, recorded as a follow-up we push
    ourselves (item 3 above).
  • n4 (step 31): config and parameters are both stored in the DB — automation.parameters (JSONB) plus the
    generator data_source linked via generator_id.
  • n5/n6 (step 34): the cli steps replay the rehearsal log, so the live run had not happened yet; the reviewer
    ran the automation directly and the report was then validated live (0.4 on the output sensor).
  • n7/n8 (step 35, checkpoint): "same-unit placeholders" explained; decision — Create should also refuse
    invalid values across same-unit placeholders; JSON view one-way accepted, with a tooltip saying so.

nhoening

This comment was marked as duplicate.

Context:
- Review of PR #2797 (demo note n3) found the Start/End offset fields of a report template in the New automation form carry no help, though the offset-chain syntax is not common knowledge

Change:
- Add a help icon to both timing labels in asset_automations.html, whose tooltip explains the offset chain (pandas offset aliases such as -1D, plus the FlexMeasures-added DB day-begin and HB hour-begin steps, applied left to right by apply_offset_chain()) and links to the "Timing each run" section of the automations docs and to the pandas offset aliases
- Cover it with a browser check in test_automation_new_form.py, and add a changelog entry

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>

nhoening commented Dec 4, 2026

Copy link
Copy Markdown
Member

Follow-ups from the review demo (session 20261204-100112-flexmeasures-2797-a-report-template-in-t):

  • push-fix — tooltip with docs link on the report-template timing fields (go-ahead: n3 "yes make this a follow-up"): done. The Start offset and End offset fields (#automationTemplateTiming, #automationTemplateStartOffset, #automationTemplateEndOffset) in the New automation form now carry a help icon whose tooltip explains the offset-chain syntax (pandas offset aliases such as -1D, plus the FlexMeasures-added DB day-begin / HB hour-begin steps, applied left to right by apply_offset_chain(); -1D,DB to DB = the previous local day) and links to "Timing each run" (https://flexmeasures.readthedocs.io/en/latest/features/automations.html#automation-timing) and the pandas offset aliases (https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases). Pushed to this PR's branch feat/2642-report-template-ui as commit 9647032 (9647032), with a browser check in flexmeasures/ui/tests/js/test_automation_new_form.py that fails without the change (full JS UI suite: 209 passed) and a changelog entry.

Nothing from the follow-ups list is left undone.

nhoening and others added 5 commits December 4, 2026 15:35
…gelog entry

- The tooltip's docs link is https://flexmeasures.readthedocs.io/latest/... (the /en/latest form 404s).
- Delete test_the_template_timing_fields_explain_the_offset_chain_and_link_to_its_docs entirely.
- Remove the second changelog entry for PR #2797; the PR keeps its single entry.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
…late-ui

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

# Conflicts:
#	documentation/api/change_log.rst
#	flexmeasures/ui/templates/assets/asset_automations.html
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
@BelhsanHmida
BelhsanHmida requested a review from nhoening December 4, 2026 22:33

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

One thing deserves attention - the draft of the filled-in form is a JS variable (and not stored in localStorage or similar.

Navigating away from the Automations page entirely (or reloading it) can mean the draft is lost, because it's only in memory — it's never written to localStorage/sessionStorage or the server. So if you start filling in a template, click to the asset's Data page (or refresh, or leave and come back), the form starts afresh.

I believe that could be an easy fix - if not, at least document this danger.

The draft lived only in the page's memory, so a reload or a visit to another page of the asset lost what was typed into the New automation form.
It is now kept per asset in session storage, saved when the page is left or the form is closed,
and laid out again once the report templates are loaded, including a template's picked sensors, added inputs and weights.
Clear form and Create forget it.

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

Copy link
Copy Markdown
Contributor Author

The draft now outlasts a reload or a visit to another page: it's kept per asset in session storage, template, picked sensors, added inputs and weights included, and Clear form or Create drops it.
Your fixes for 1, 2 and 4 look good.

@BelhsanHmida
BelhsanHmida requested a review from nhoening December 5, 2026 00:31

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

Great, thanks!

@nhoening
nhoening merged commit 5b5829d into main Dec 5, 2026
10 of 11 checks passed
@nhoening
nhoening deleted the feat/2642-report-template-ui branch December 5, 2026 13:01
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.

Fill in a report template from the UI, picking its sensors through search

4 participants