Skip to content

A report is saved in two places, so the CLI and the job can drift apart #2485

Description

@Flix6x

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions