Use timediff -> time_to_sec -> abs to make datetime fuzzy

This commit is contained in:
Starbeamrainbowlabs 2019-01-15 17:27:35 +00:00
parent 102bd13864
commit 6123e2c679
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 35 additions and 7 deletions

View file

@ -1,5 +1,7 @@
<?php
namespace AirQuality;
/**
* Formats performance data to be sent in a HTTP header.
*/

View file

@ -2,6 +2,9 @@
namespace AirQuality\Repositories;
use \AirQuality\Database;
use \SBRL\TomlConfig;
/**
* Fetches measurement readings from a MariaDB database.
*/
@ -23,9 +26,12 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
// ------------------------------------------------------------------------
/** @var TomlConfig */
private $settings;
/**
* The database connection.
* @var \AirQuality\Database
* @var Database
*/
private $database;
@ -34,8 +40,10 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
private $get_static_extra;
function __construct(\AirQuality\Database $in_database) {
function __construct(Database $in_database, TomlConfig $in_settings) {
$this->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(
WHERE ABS(TIME_TO_SEC(TIMEDIFF(
:datetime,
COALESCE(
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
) = :datetime AND
)
))) < :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();
}

View file

@ -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