A report is saved in two places, so the two paths can drift apart.
flexmeasures add report persists the report itself when it computes one directly:
https://github.com/FlexMeasures/flexmeasures/blob/main/flexmeasures/cli/data_add.py#L2374-L2377
# save the report if it's not running in dry mode
if not dry_run:
click.echo(f"Saving report for sensor `{sensor}` to the database...")
save_to_db(data)
db.session.commit()
while run_report_job persists it when the same work runs on a worker:
https://github.com/FlexMeasures/flexmeasures/blob/main/flexmeasures/data/services/reporting.py#L100-L104
for result in results:
n_rows = _count_persistable_values(result["data"])
save_to_db(result["data"])
saved.append({"sensor_id": result["sensor"].id, "n_rows": n_rows})
db.session.commit()
Two implementations of the same step, reached depending on whether --as-job was given. They already differ: only the job path counts the values that will survive persistence, through _count_persistable_values, so the CLI cannot report how many rows it actually stored. Any future change to how a report is persisted has to be made twice, and missing one of them is silent.
Scheduling does not have this problem: the job runs make_schedule, which is the same function the CLI calls, so there is one save site for both paths. Forecasting likewise has one site, in its pipeline.
Suggested fix
Give reporting one function that computes and persists, and have both the CLI and the job call it, with whether to persist as an argument (the CLI already needs that for --dry-run). No behaviour change is intended; a test asserting the belief count before and after a report, on both paths, would pin it.
Notes
Found while making --dry-run behave the same across add forecasts, add schedule and add report (#2483). That PR does not touch this, since reporting's dry run only exists on the CLI path, which is the path that already has the flag.
🤖 Generated with Claude Code
https://claude.ai/code/session_01HeRZd2nDDShZTE8ZmbERzn
A report is saved in two places, so the two paths can drift apart.
flexmeasures add reportpersists the report itself when it computes one directly:https://github.com/FlexMeasures/flexmeasures/blob/main/flexmeasures/cli/data_add.py#L2374-L2377
while
run_report_jobpersists it when the same work runs on a worker:https://github.com/FlexMeasures/flexmeasures/blob/main/flexmeasures/data/services/reporting.py#L100-L104
Two implementations of the same step, reached depending on whether
--as-jobwas given. They already differ: only the job path counts the values that will survive persistence, through_count_persistable_values, so the CLI cannot report how many rows it actually stored. Any future change to how a report is persisted has to be made twice, and missing one of them is silent.Scheduling does not have this problem: the job runs
make_schedule, which is the same function the CLI calls, so there is one save site for both paths. Forecasting likewise has one site, in its pipeline.Suggested fix
Give reporting one function that computes and persists, and have both the CLI and the job call it, with whether to persist as an argument (the CLI already needs that for
--dry-run). No behaviour change is intended; a test asserting the belief count before and after a report, on both paths, would pin it.Notes
Found while making
--dry-runbehave the same acrossadd forecasts,add scheduleandadd report(#2483). That PR does not touch this, since reporting's dry run only exists on the CLI path, which is the path that already has the flag.🤖 Generated with Claude Code
https://claude.ai/code/session_01HeRZd2nDDShZTE8ZmbERzn