diff --git a/logic/PerfFormatter.php b/logic/PerfFormatter.php index 9ec313b..a511f95 100644 --- a/logic/PerfFormatter.php +++ b/logic/PerfFormatter.php @@ -1,5 +1,7 @@ database = $in_database; + $this->settings = $in_settings; + $this->get_static = function($name) { return self::$$name; }; $this->get_static_extra = function($class_name, $name) { return $class_name::$$name; @@ -64,15 +72,21 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository { FROM {$s("table_name_values")} JOIN {$s("table_name_metadata")} ON {$s("table_name_values")}.{$s("column_values_reading_id")} = {$s("table_name_metadata")}.id JOIN {$o(MariaDBDeviceRepository::class, "table_name")} ON {$s("table_name_metadata")}.{$s("column_metadata_device_id")} = {$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_device_id")} - WHERE COALESCE( - {$s("table_name_metadata")}.{$s("column_metadata_recordedon")}, - {$s("table_name_metadata")}.{$s("column_metadata_storedon")} - ) = :datetime AND + WHERE ABS(TIME_TO_SEC(TIMEDIFF( + :datetime, + COALESCE( + {$s("table_name_metadata")}.{$s("column_metadata_recordedon")}, + {$s("table_name_metadata")}.{$s("column_metadata_storedon")} + ) + ))) < :max_reading_timediff + AND {$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type + ", [ // The database likes strings, not PHP DateTime() instances "datetime" => $datetime->format(\DateTime::ISO8601), - "reading_type" => $reading_type + "reading_type" => $reading_type, + "max_reading_timediff" => $this->settings->get("data.max_reading_timediff") ] )->fetchAll(); } diff --git a/settings.default.toml b/settings.default.toml index 1e9cacc..8e4a860 100644 --- a/settings.default.toml +++ b/settings.default.toml @@ -23,3 +23,15 @@ password = "Define_in_custom_config_file" # The default action to take if no action is specified default-action = "fetch-data" + +[data] +# Settings relating to the data returned by the API. + +# The maximum number of seconds difference allowed before a reading is +# considered to be part of a different set. +# +# This is required, as not all the data items come in at the same time - so we +# need to be a bit 'fuzzy' about which readings we consider to be part of which +# time-step. Ideally, this value should be half of the actual data recording +# interval. +max_reading_timediff = 15