2019-01-15 15:46:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AirQuality\Repositories;
|
|
|
|
|
2019-06-22 20:08:12 +00:00
|
|
|
use \AirQuality\Constants;
|
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";
|
2019-06-22 10:50:23 +00:00
|
|
|
/**
|
|
|
|
* A coaleasce of storedon and recorded on.
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-06-23 13:42:47 +00:00
|
|
|
public static $column_metadata_datetime = "s_or_r";
|
2019-01-15 15:46:24 +00:00
|
|
|
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-06-13 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* Function that gets a static variable by it's name. Useful in preparing SQL queries.
|
|
|
|
* @var callable
|
|
|
|
*/
|
2019-01-15 17:02:24 +00:00
|
|
|
private $get_static;
|
2019-06-13 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* Function that gets a static variable by it's name & class. Useful in preparing SQL queries.
|
|
|
|
* @var callable
|
|
|
|
*/
|
2019-01-15 17:02:24 +00:00
|
|
|
private $get_static_extra;
|
|
|
|
|
2019-06-13 21:02:04 +00:00
|
|
|
/**
|
|
|
|
* Creates a new reepository instance.
|
|
|
|
* It is suggested that PHP-DI is utilised for instantiation via the associated interface.
|
|
|
|
* @param Database $in_database The database connection to use.
|
|
|
|
* @param TomlConfig $in_settings The settings to operate with.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2019-02-18 20:44:37 +00:00
|
|
|
public function get_readings_by_date(\DateTime $datetime, int $type_id) {
|
2019-06-20 21:12:33 +00:00
|
|
|
$max_reading_timediff = $this->settings->get("data.max_reading_timediff");
|
|
|
|
|
|
|
|
$start_datetime = (clone $datetime)->sub(new \DateInterval("PT${max_reading_timediff}S"));
|
|
|
|
$end_datetime = (clone $datetime)->add(new \DateInterval("PT${max_reading_timediff}S"));
|
|
|
|
|
2019-01-15 17:02:24 +00:00
|
|
|
$s = $this->get_static;
|
|
|
|
$o = $this->get_static_extra;
|
2019-06-13 21:02:04 +00:00
|
|
|
|
|
|
|
// OPTIMIZE: I think we can drastically improve the performance of this query by pre-calculating the start & end dates of the window
|
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-06-23 13:39:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_datetime")} AS datetime,
|
2019-01-17 16:51:37 +00:00
|
|
|
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
|
2019-06-20 21:12:33 +00:00
|
|
|
WHERE
|
2019-06-23 13:39:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_datetime")} >= :start_datetime AND
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_datetime")} <= :end_datetime
|
2019-01-15 17:27:35 +00:00
|
|
|
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")}
|
2019-06-23 13:39:24 +00:00
|
|
|
ORDER BY {$s("table_name_metadata")}.{$s("column_metadata_datetime")}
|
2019-01-15 17:05:21 +00:00
|
|
|
", [
|
2019-02-18 20:44:37 +00:00
|
|
|
"reading_type" => $type_id,
|
2019-06-20 21:12:33 +00:00
|
|
|
// The database likes strings, not PHP DateTime() instances
|
2019-06-22 20:08:12 +00:00
|
|
|
"start_datetime" => $start_datetime->format(Constants::DATETIME_FORMAT_SQL),
|
|
|
|
"end_datetime" => $end_datetime->format(Constants::DATETIME_FORMAT_SQL),
|
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
|
2019-06-23 13:39:24 +00:00
|
|
|
MIN({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) AS start,
|
|
|
|
MAX({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) AS end
|
2019-01-19 16:08:47 +00:00
|
|
|
FROM {$s("table_name_metadata")}
|
|
|
|
WHERE {$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id;", [
|
2019-01-19 21:12:11 +00:00
|
|
|
"device_id" => $device_id
|
2019-01-19 16:08:47 +00:00
|
|
|
]
|
|
|
|
)->fetch();
|
|
|
|
}
|
|
|
|
|
2019-02-18 20:44:37 +00:00
|
|
|
public function get_readings_by_device(int $device_id, int $type_id, \DateTime $start, \DateTime $end, int $average_seconds = 1) {
|
2019-01-19 21:12:11 +00:00
|
|
|
if($average_seconds < 1)
|
2019-06-13 21:02:04 +00:00
|
|
|
throw new \Exception("Error: average_seconds must be greater than 1, but '$average_seconds' was specified.");
|
2019-01-19 21:12:11 +00:00
|
|
|
$s = $this->get_static;
|
|
|
|
return $this->database->query(
|
|
|
|
"SELECT
|
|
|
|
AVG({$s("table_name_values")}.{$s("column_values_value")}) AS {$s("column_values_value")},
|
|
|
|
MIN({$s("table_name_values")}.{$s("column_values_reading_id")}) AS {$s("column_values_reading_id")},
|
|
|
|
|
2019-06-23 13:39:24 +00:00
|
|
|
MIN({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) AS datetime
|
2019-01-19 21:12:11 +00:00
|
|
|
FROM {$s("table_name_values")}
|
|
|
|
JOIN {$s("table_name_metadata")} ON
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_id")} = {$s("table_name_values")}.{$s("column_values_reading_id")}
|
|
|
|
WHERE
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id AND
|
2019-01-20 22:03:06 +00:00
|
|
|
{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type AND
|
|
|
|
|
2019-06-23 13:39:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_datetime")} >= :start_datetime AND
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_datetime")} <= :end_datetime
|
|
|
|
GROUP BY CEIL(UNIX_TIMESTAMP({$s("table_name_metadata")}.{$s("column_metadata_datetime")}) / :average_seconds);", [
|
2019-01-19 21:12:11 +00:00
|
|
|
"device_id" => $device_id,
|
2019-02-18 20:44:37 +00:00
|
|
|
"reading_type" => $type_id,
|
2019-06-22 20:08:12 +00:00
|
|
|
"start_datetime" => $start->format(Constants::DATETIME_FORMAT_SQL),
|
|
|
|
"end_datetime" => $end->format(Constants::DATETIME_FORMAT_SQL),
|
2019-01-19 21:12:11 +00:00
|
|
|
"average_seconds" => $average_seconds
|
|
|
|
]
|
|
|
|
)->fetchAll();
|
2019-01-19 16:08:47 +00:00
|
|
|
}
|
2019-06-13 21:02:04 +00:00
|
|
|
|
|
|
|
public function get_recent_readings(int $device_id, int $type_id, int $count) {
|
|
|
|
if($count <= 0) return [];
|
|
|
|
|
|
|
|
$s = $this->get_static;
|
|
|
|
return $this->database->query(
|
|
|
|
"SELECT
|
2019-06-13 21:10:14 +00:00
|
|
|
{$s("table_name_values")}.{$s("column_values_value")} AS {$s("column_values_value")},
|
|
|
|
{$s("table_name_values")}.{$s("column_values_reading_id")} AS {$s("column_values_reading_id")},
|
2019-06-13 21:02:04 +00:00
|
|
|
|
2019-06-23 13:39:24 +00:00
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_datetime")} AS datetime
|
2019-06-13 21:02:04 +00:00
|
|
|
FROM {$s("table_name_values")}
|
|
|
|
JOIN {$s("table_name_metadata")} ON
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_id")} = {$s("table_name_values")}.{$s("column_values_reading_id")}
|
|
|
|
WHERE
|
|
|
|
{$s("table_name_metadata")}.{$s("column_metadata_device_id")} = :device_id AND
|
|
|
|
{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type
|
2019-06-23 13:39:24 +00:00
|
|
|
ORDER BY {$s("table_name_metadata")}.{$s("column_metadata_datetime")} DESC
|
2019-06-13 21:02:04 +00:00
|
|
|
LIMIT :count;", [
|
|
|
|
"device_id" => $device_id,
|
|
|
|
"reading_type" => $type_id,
|
|
|
|
"count" => $count
|
|
|
|
]
|
|
|
|
)->fetchAll();
|
|
|
|
}
|
2019-01-15 15:46:24 +00:00
|
|
|
}
|