Module sql_interface_heating_stats

Source
Expand description

Contains functions for execution of all queries relevant for heating statistics (thermal) Manages the aggregation and persistence of daily heating statistics.

This module provides a dedicated interface, SqlInterfaceHeatingStats, for handling all SQL operations related to the heatingstats table. It is responsible for tracking daily energy consumption, average temperatures, and heater runtime.

§Key Components

  • SqlInterfaceHeatingStats Struct: The primary interface that holds a database connection and provides methods for all heating statistics operations.

  • HeatingStatsEntry Struct: A data structure representing the aggregated statistics for a single day. It includes an update method for incrementally calculating running averages.

  • new() Constructor: A critical entry point that performs “fail-fast” validation at initialization. It checks the database to ensure:

    • The heatingstats table contains no NULL values.
    • The table’s row count does not exceed the configured maximum.
  • get_single_heating_stats_from_database(): Retrieves the statistics record for the current day.

  • insert_heating_stats_entry(): Writes a HeatingStatsEntry to the database using an INSERT ... ON DUPLICATE KEY UPDATE query to either create a new daily record or update an existing one.

§Design and Purpose

The main goal of this module is to provide a robust and encapsulated interface for managing the application’s historical heating data.

  • Encapsulation: All SQL logic specific to the heatingstats table is contained within this module, separating concerns.

  • Data Integrity: The constructor’s validation logic enforces data integrity rules at startup, preventing runtime errors caused by an invalid database state.

  • Time-Based Logic: It relies on a SqlInterfaceMidnightCalculatorTrait to determine when a day has ended, which is crucial for finalizing one day’s statistics and starting a new record.

Structs§

HeatingStatsEntry
Holds the statistical data of heating for one day. It is filled with post-processed data from a database query which is then updated by the control application.
SqlInterfaceHeatingStats
Contains the configuration and the implementation of the SQL interface for heating.