aeat.application.aggregation._observation_window module

Shared set-replace algorithm for per-(modelo, filing_year, period) observation stores.

The percepciones (Modelo 190) and retención (Modelo 180/193) per-perceptor observation repositories each persist a distinct row type keyed by (modelo, filing_year, period) plus a per-record identity, and each needs the exact same SET-REPLACE discipline on a re-pull: clear every prior row for the window, then write the supplied set. Leaving the stale window behind (an additive upsert instead of a set-replace) inflates the next calculate’s distinct count with a record the operator no longer declares — a silent over-count. This module holds that one algorithm so PercepcionObservationRepository and RetencionObservationRepository both call it instead of each carrying their own copy of the loop.

replace_observation_window(repository, *, modelo, filing_year, period, observations, source_kind, save_observation, captured_at=None, source_metadata=None)[source]

Replace the FULL observation set for one (modelo, filing_year, period) window.

SET-REPLACE, not additive upsert: clears any prior rows for the exact key-tuple on repository, then writes the supplied observations through save_observation (each repository’s own typed save_observation method, which builds the correctly-typed envelope payload). An empty observations clears the window, matching each repository’s own no-silent-under-declaration contract on the caller side.

Return type:

None

Parameters:
  • repository (SecureBoundRepository[_WindowKeyedPayload])

  • modelo (str)

  • filing_year (int)

  • period (Period)

  • observations (Sequence[ObservationT])

  • source_kind (str)

  • save_observation (Callable[..., None])

  • captured_at (datetime | None)

  • source_metadata (Mapping[str, str] | None)