mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2025-01-02 11:54:56 +00:00
Fix measurement data query, but it's not quite right yet
This commit is contained in:
parent
2787ea7c4c
commit
9f6bea64d4
8 changed files with 40 additions and 29 deletions
3
api.php
3
api.php
|
@ -30,7 +30,8 @@ $settings = $di_container->get(\SBRL\TomlConfig::class);
|
||||||
|
|
||||||
// 4: Database
|
// 4: Database
|
||||||
|
|
||||||
// Done via a static factory method & PHP-DI
|
// Done automagically by PHP-DI.
|
||||||
|
// PHP-DI autowires it, and doesn't create more than 1 instance of it either
|
||||||
|
|
||||||
// 5: Action
|
// 5: Action
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ class TomlConfig
|
||||||
/**
|
/**
|
||||||
* Determines whether the given property has been customised or explicitly specified in the customised settings file.
|
* Determines whether the given property has been customised or explicitly specified in the customised settings file.
|
||||||
* @param string $key The property, in dotted notation, to check.
|
* @param string $key The property, in dotted notation, to check.
|
||||||
* @return boolean Whether the specified properties file has been explicitly specified in the custom settings file.
|
* @return bool Whether the specified properties file has been explicitly specified in the custom settings file.
|
||||||
*/
|
*/
|
||||||
public function hasChanged($key) {
|
public function hasChanged($key) {
|
||||||
return static::hasPropertyByPath($this->customSettings, $key);
|
return static::hasPropertyByPath($this->customSettings, $key);
|
||||||
|
@ -112,7 +112,7 @@ class TomlConfig
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the customised settings back to the disk.
|
* Saves the customised settings back to the disk.
|
||||||
* @return boolean Whether the save was successful or not.
|
* @return bool Whether the save was successful or not.
|
||||||
*/
|
*/
|
||||||
public function save() {
|
public function save() {
|
||||||
$output_builder = new TomlBuilder();
|
$output_builder = new TomlBuilder();
|
||||||
|
@ -139,7 +139,7 @@ class TomlConfig
|
||||||
* Works out whether a given dotted path to a property exists on a given object.
|
* Works out whether a given dotted path to a property exists on a given object.
|
||||||
* @param stdClass $obj The object to search.
|
* @param stdClass $obj The object to search.
|
||||||
* @param string $path The dotted path to search with. e.g. `Network.Firewall.OpenPorts`
|
* @param string $path The dotted path to search with. e.g. `Network.Firewall.OpenPorts`
|
||||||
* @return boolean Whether the given property is present in the given object.
|
* @return bool Whether the given property is present in the given object.
|
||||||
*/
|
*/
|
||||||
public static function hasPropertyByPath($obj, $path) {
|
public static function hasPropertyByPath($obj, $path) {
|
||||||
$pathParts = explode(".", $path);
|
$pathParts = explode(".", $path);
|
||||||
|
|
|
@ -38,7 +38,7 @@ class FetchData implements IAction {
|
||||||
$this->validator = new Validator($_GET);
|
$this->validator = new Validator($_GET);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle() : boolean {
|
public function handle() : bool {
|
||||||
global $start_time;
|
global $start_time;
|
||||||
|
|
||||||
$start_handle = microtime(true);
|
$start_handle = microtime(true);
|
||||||
|
|
|
@ -3,5 +3,5 @@
|
||||||
namespace AirQuality\Actions;
|
namespace AirQuality\Actions;
|
||||||
|
|
||||||
interface IAction {
|
interface IAction {
|
||||||
public function handle() : boolean;
|
public function handle() : bool;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,14 +24,12 @@ class Database
|
||||||
];
|
];
|
||||||
|
|
||||||
function __construct(TomlConfig $in_settings) {
|
function __construct(TomlConfig $in_settings) {
|
||||||
error_log("Created database instance");
|
|
||||||
$this->settings = $in_settings;
|
$this->settings = $in_settings;
|
||||||
|
|
||||||
$this->connect(); // Connect automagically
|
$this->connect(); // Connect automagically
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connect() {
|
public function connect() {
|
||||||
error_log($this->get_connection_string());
|
|
||||||
$this->connection = new \PDO(
|
$this->connection = new \PDO(
|
||||||
$this->get_connection_string(),
|
$this->get_connection_string(),
|
||||||
$this->settings->get("database.username"),
|
$this->settings->get("database.username"),
|
||||||
|
@ -45,7 +43,9 @@ class Database
|
||||||
|
|
||||||
public function query($sql, $variables = []) {
|
public function query($sql, $variables = []) {
|
||||||
// FUTURE: Optionally cache prepared statements?
|
// FUTURE: Optionally cache prepared statements?
|
||||||
return $this->connection->prepare($sql)->execute($variables);
|
$statement = $this->connection->prepare($sql);
|
||||||
|
$statement->execute($variables);
|
||||||
|
return $statement; // fetchColumn(), fetchAll(), etc. are defined on the statement, not the return value of execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
private function get_connection_string() {
|
private function get_connection_string() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ interface IMeasurementTypeRepository {
|
||||||
/**
|
/**
|
||||||
* Returns whether the specified type is valid or not.
|
* Returns whether the specified type is valid or not.
|
||||||
* @param string $type_name The name of the type to validate.
|
* @param string $type_name The name of the type to validate.
|
||||||
* @return boolean Whether the specified type name is valid or not.
|
* @return bool Whether the specified type name is valid or not.
|
||||||
*/
|
*/
|
||||||
public function is_valid_type(string $type_name) : boolean;
|
public function is_valid_type(string $type_name) : bool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the friendly name for the specified type name.
|
* Gets the friendly name for the specified type name.
|
||||||
|
|
|
@ -29,36 +29,47 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
|
||||||
*/
|
*/
|
||||||
private $database;
|
private $database;
|
||||||
|
|
||||||
|
/** Function that gets a static variable by it's name. Useful in preparing SQL queries. */
|
||||||
|
private $get_static;
|
||||||
|
|
||||||
|
private $get_static_extra;
|
||||||
|
|
||||||
function __construct(\AirQuality\Database $in_database) {
|
function __construct(\AirQuality\Database $in_database) {
|
||||||
$this->database = $in_database;
|
$this->database = $in_database;
|
||||||
|
$this->get_static = function($name) { return self::$$name; };
|
||||||
|
$this->get_static_extra = function($class_name, $name) {
|
||||||
|
return $class_name::$$name;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_readings_by_date(\DateTime $datetime, string $reading_type) {
|
public function get_readings_by_date(\DateTime $datetime, string $reading_type) {
|
||||||
|
$s = $this->get_static;
|
||||||
|
$o = $this->get_static_extra;
|
||||||
return $this->database->query(
|
return $this->database->query(
|
||||||
"SELECT
|
"SELECT
|
||||||
$this->table_name_values.*,
|
{$s("table_name_values")}.*,
|
||||||
$this->table_name_metadata.device_id,
|
{$s("table_name_metadata")}.device_id,
|
||||||
COALESCE(
|
COALESCE(
|
||||||
$this->table_name_metadata.$this->column_metadata_recordedon,
|
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
||||||
$this->table_name_metadata.$this->column_metadata_storedon
|
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
||||||
) AS datetime,
|
) AS datetime,
|
||||||
COALESCE(
|
COALESCE(
|
||||||
$this->table_name_metadata.$this->column_metadata_lat,
|
{$s("table_name_metadata")}.{$s("column_metadata_lat")},
|
||||||
{MariaDBDeviceRepository::$table_name}.device_latitude
|
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_lat")}
|
||||||
) AS latitude,
|
) AS latitude,
|
||||||
COALESCE(
|
COALESCE(
|
||||||
$this->table_name_metadata.$this->column_metadata_long,
|
{$s("table_name_metadata")}.{$s("column_metadata_long")},
|
||||||
devices.device_longitude
|
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_long")}
|
||||||
) AS longitude
|
) AS longitude
|
||||||
FROM $this->table_name_values
|
FROM {$s("table_name_values")}
|
||||||
JOIN $this->table_name_metadata ON $this->table_name_values.$this->column_values_reading_id = $this->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 devices ON $this->table_name_metadata.$this->column_metadata_device_id = devices.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 COALESCE(
|
||||||
$this->table_name_metadata.$this->column_metadata_recordedon,
|
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
|
||||||
$this->table_name_metadata.$this->column_metadata_storedon
|
{$s("table_name_metadata")}.{$s("column_metadata_storedon")}
|
||||||
) = :datetime",
|
) = :datetime", [
|
||||||
[
|
// The database likes strings, not PHP DateTime() instances
|
||||||
"datetime" => $datetime,
|
"datetime" => $datetime->format(\DateTime::ISO8601),
|
||||||
"reading_type" => $reading_type
|
"reading_type" => $reading_type
|
||||||
]
|
]
|
||||||
)->fetchAll();
|
)->fetchAll();
|
||||||
|
|
|
@ -24,11 +24,10 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
|
||||||
|
|
||||||
function __construct(\AirQuality\Database $in_database) {
|
function __construct(\AirQuality\Database $in_database) {
|
||||||
$this->database = $in_database;
|
$this->database = $in_database;
|
||||||
|
|
||||||
$this->get_static = function($name) { return self::$$name; };
|
$this->get_static = function($name) { return self::$$name; };
|
||||||
}
|
}
|
||||||
|
|
||||||
public function is_valid_type(string $type_name) : boolean {
|
public function is_valid_type(string $type_name) : bool {
|
||||||
$s = $this->get_static;
|
$s = $this->get_static;
|
||||||
return !empty($this->database->query(
|
return !empty($this->database->query(
|
||||||
"SELECT {$s("column_id")} FROM {$s("table_name")};"
|
"SELECT {$s("column_id")} FROM {$s("table_name")};"
|
||||||
|
|
Loading…
Reference in a new issue