Browse Source

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

pull/26/head
Starbeamrainbowlabs 4 years ago
parent
commit
6123e2c679
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
  1. 2
      logic/PerfFormatter.php
  2. 28
      logic/Repositories/MariaDBMeasurementDataRepository.php
  3. 12
      settings.default.toml

2
logic/PerfFormatter.php

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

28
logic/Repositories/MariaDBMeasurementDataRepository.php

@ -2,6 +2,9 @@ @@ -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 { @@ -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 { @@ -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 { @@ -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();
}

12
settings.default.toml

@ -23,3 +23,15 @@ password = "Define_in_custom_config_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

Loading…
Cancel
Save