2019-01-15 15:46:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Repositories;
|
|
|
|
|
2019-01-15 17:27:35 +00:00
|
|
|
use \AirQuality\Database;
|
|
|
|
use \SBRL\TomlConfig;
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
/**
|
|
|
|
* Fetches measurement readings from a MariaDB database.
|
|
|
|
*/
|
|
|
|
class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
|
|
|
|
public static $table_name_metadata = "readings";
|
|
|
|
public static $table_name_values = "reading_values";
|
|
|
|
|
|
|
|
public static $column_values_id = "id";
|
|
|
|
public static $column_values_reading_id = "reading_id";
|
|
|
|
public static $column_values_value = "value";
|
2019-01-15 17:05:21 +00:00
|
|
|
public static $column_values_reading_type = "reading_value_types_id";
|
2019-01-15 15:46:24 +00:00
|
|
|
|
|
|
|
public static $column_metadata_id = "id";
|
|
|
|
public static $column_metadata_storedon = "storedon";
|
|
|
|
public static $column_metadata_recordedon = "recordedon";
|
|
|
|
public static $column_metadata_device_id = "device_id";
|
|
|
|
public static $column_metadata_lat = "reading_latitude";
|
|
|
|
public static $column_metadata_long = "reading_longitude";
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
|
2019-01-15 17:27:35 +00:00
|
|
|
/** @var TomlConfig */
|
|
|
|
private $settings;
|
|
|
|
|
2019-01-15 15:46:24 +00:00
|
|
|
/**
|
|
|
|
* The database connection.
|
2019-01-15 17:27:35 +00:00
|
|
|
* @var Database
|
2019-01-15 15:46:24 +00:00
|
|
|
*/
|
|
|
|
private $database;
|
|
|
|
|
2019-01-15 17:02:24 +00:00
|
|
|
/** Function that gets a static variable by it's name. Useful in preparing SQL queries. */
|
|
|
|
private $get_static;
|
|
|
|
private $get_static_extra;
|
|
|
|
|
2019-01-15 17:27:35 +00:00
|
|
|
function __construct(Database $in_database, TomlConfig $in_settings) {
|
2019-01-15 15:46:24 +00:00
|
|
|
$this->database = $in_database;
|
2019-01-15 17:27:35 +00:00
|
|
|
$this->settings = $in_settings;
|
|
|
|
|
2019-01-15 17:02:24 +00:00
|
|
|
$this->get_static = function($name) { return self::$$name; };
|
|
|
|
$this->get_static_extra = function($class_name, $name) {
|
|
|
|
return $class_name::$$name;
|
|
|
|
};
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_readings_by_date(\DateTime $datetime, string $reading_type) {
|
2019-01-15 17:02:24 +00:00
|
|
|
$s = $this->get_static;
|
|
|
|
$o = $this->get_static_extra;
|
2019-01-15 15:46:24 +00:00
|
|
|
return $this->database->query(
|
|
|
|
"SELECT
|
2019-01-17 17:02:22 +00:00
|
|
|
{$s("table_name_values")}.{$s("column_values_value")},
|
|
|
|
{$s("table_name_values")}.{$s("column_values_reading_id")},
|
|
|
|
|
2019-01-17 16:51:37 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_device_id")},
|
2019-01-15 15:46:24 +00:00
|
|
|
COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
2019-01-15 15:46:24 +00:00
|
|
|
) AS datetime,
|
|
|
|
COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_lat")},
|
|
|
|
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_lat")}
|
2019-01-15 15:46:24 +00:00
|
|
|
) AS latitude,
|
|
|
|
COALESCE(
|
2019-01-15 17:02:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_long")},
|
|
|
|
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_long")}
|
2019-01-17 16:51:37 +00:00
|
|
|
) AS longitude,
|
|
|
|
COUNT({$s("table_name_metadata")}.{$s("column_metadata_device_id")}) AS record_count
|
2019-01-15 17:02:24 +00:00
|
|
|
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")}
|
2019-01-15 17:27:35 +00:00
|
|
|
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
|
2019-01-15 17:05:21 +00:00
|
|
|
{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type
|
2019-01-17 16:51:37 +00:00
|
|
|
GROUP BY {$s("table_name_metadata")}.{$s("column_metadata_device_id")}
|
|
|
|
ORDER BY {$s("table_name_metadata")}.{$s("column_metadata_recordedon")}
|
2019-01-15 17:05:21 +00:00
|
|
|
", [
|
2019-01-15 17:02:24 +00:00
|
|
|
// The database likes strings, not PHP DateTime() instances
|
|
|
|
"datetime" => $datetime->format(\DateTime::ISO8601),
|
2019-01-15 17:27:35 +00:00
|
|
|
"reading_type" => $reading_type,
|
|
|
|
"max_reading_timediff" => $this->settings->get("data.max_reading_timediff")
|
2019-01-15 15:46:24 +00:00
|
|
|
]
|
2019-01-15 15:57:39 +00:00
|
|
|
)->fetchAll();
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|
2019-01-19 16:08:47 +00:00
|
|
|
|
|
|
|
public function get_device_reading_bounds(int $device_id) {
|
|
|
|
$s = $this->get_static;
|
|
|
|
return $this->database->query(
|
|
|
|
"SELECT
|
|
|
|
MIN(COALESCE(
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
|
|
|
)) AS start,
|
|
|
|
MAX(COALESCE(
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
|
|
|
)) AS end
|
|
|
|
FROM {$s("table_name_metadata")}
|
|
|
|
WHERE {$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id;", [
|
|
|
|
$device_id
|
|
|
|
]
|
|
|
|
)->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_readings_by_device(int $device_id, string $reading_type, \DateTime $start, \DateTime $end) {
|
|
|
|
|
|
|
|
}
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|