mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-12-22 10:25:01 +00:00
Use timediff -> time_to_sec -> abs to make datetime fuzzy
This commit is contained in:
parent
102bd13864
commit
6123e2c679
3 changed files with 35 additions and 7 deletions
|
@ -1,5 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
namespace AirQuality;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats performance data to be sent in a HTTP header.
|
* Formats performance data to be sent in a HTTP header.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
namespace AirQuality\Repositories;
|
namespace AirQuality\Repositories;
|
||||||
|
|
||||||
|
use \AirQuality\Database;
|
||||||
|
use \SBRL\TomlConfig;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches measurement readings from a MariaDB database.
|
* Fetches measurement readings from a MariaDB database.
|
||||||
*/
|
*/
|
||||||
|
@ -23,9 +26,12 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/** @var TomlConfig */
|
||||||
|
private $settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The database connection.
|
* The database connection.
|
||||||
* @var \AirQuality\Database
|
* @var Database
|
||||||
*/
|
*/
|
||||||
private $database;
|
private $database;
|
||||||
|
|
||||||
|
@ -34,8 +40,10 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
|
||||||
|
|
||||||
private $get_static_extra;
|
private $get_static_extra;
|
||||||
|
|
||||||
function __construct(\AirQuality\Database $in_database) {
|
function __construct(Database $in_database, TomlConfig $in_settings) {
|
||||||
$this->database = $in_database;
|
$this->database = $in_database;
|
||||||
|
$this->settings = $in_settings;
|
||||||
|
|
||||||
$this->get_static = function($name) { return self::$$name; };
|
$this->get_static = function($name) { return self::$$name; };
|
||||||
$this->get_static_extra = function($class_name, $name) {
|
$this->get_static_extra = function($class_name, $name) {
|
||||||
return $class_name::$$name;
|
return $class_name::$$name;
|
||||||
|
@ -64,15 +72,21 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
|
||||||
FROM {$s("table_name_values")}
|
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 {$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")}
|
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(
|
||||||
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
:datetime,
|
||||||
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
COALESCE(
|
||||||
) = :datetime AND
|
{$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
|
{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type
|
||||||
|
|
||||||
", [
|
", [
|
||||||
// The database likes strings, not PHP DateTime() instances
|
// The database likes strings, not PHP DateTime() instances
|
||||||
"datetime" => $datetime->format(\DateTime::ISO8601),
|
"datetime" => $datetime->format(\DateTime::ISO8601),
|
||||||
"reading_type" => $reading_type
|
"reading_type" => $reading_type,
|
||||||
|
"max_reading_timediff" => $this->settings->get("data.max_reading_timediff")
|
||||||
]
|
]
|
||||||
)->fetchAll();
|
)->fetchAll();
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,3 +23,15 @@ password = "Define_in_custom_config_file"
|
||||||
|
|
||||||
# The default action to take if no action is specified
|
# The default action to take if no action is specified
|
||||||
default-action = "fetch-data"
|
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
|
||||||
|
|
Loading…
Reference in a new issue