api/list-reading-types: optimise

This uses the new sensor_reading_value_types table that was recently 
added, and also REMOVES the "count" property on returned objects from 
this API action.
This commit is contained in:
Starbeamrainbowlabs 2021-01-30 18:26:50 +00:00
parent 712e0602ed
commit 8ccc578e7b
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
8 changed files with 37 additions and 22 deletions

View File

@ -10,6 +10,10 @@ This is the changelog for the air quality web interface and its associated HTTP
- `[Docs]` refers to changes to the [documentation](https://aq.connectedhumber.org/__nightdocs/00-Welcome.html).
## v0.14
- [API] Optimise `list-reading-types` action, which necessitated a database schema update and the removal of the `count` property on returned objects
## v0.13.6
- [Code] Add option to allow cross-origin-request sharing HTTP API requests

2
build
View File

@ -28,7 +28,7 @@ cache_dir="./.cache";
build_output_folder="./app";
# Database settings for ssh port forwarding task
database_host="db.connectedhumber.org";
database_host="ch-kimsufi";
database_name="aq_db";
database_user="www-data";

View File

@ -37,7 +37,6 @@ class ListReadingTypes implements IAction {
// 1: Parse & validate parameters
$device_id = !empty($_GET["device-id"]) ? intval($_GET["device-id"]) : null;
$days_to_analyse = intval($_GET["days"] ?? "1") - 1;
$format = $_GET["format"] ?? "json";
if(!in_array($format, ["json", "csv"])) {
@ -54,7 +53,7 @@ class ListReadingTypes implements IAction {
if(!is_int($device_id))
$data = $this->types_repo->get_all_types();
else
$data = $this->types_repo->get_types_by_device($device_id, $days_to_analyse);
$data = $this->types_repo->get_types_by_device($device_id);
$this->perfcounter->end("sql");
// 1.5: Validate data from database

View File

@ -33,8 +33,7 @@ interface IMeasurementTypeRepository {
/**
* Gets the all the measurement types ever reported by a given device id.
* @param int $device_id The id of the device to get the reading types for.
* @param int $day_to_analyse The number of days worth fo data to analyse. Defaults to -1, which is everything. Set to 0 for readings recorded in the last 24 hours, which is much faster.
* @return string[] A list of device ids.
*/
public function get_types_by_device(int $device_id, int $days_to_analyse = -1);
public function get_types_by_device(int $device_id);
}

View File

@ -33,7 +33,6 @@ class MariaDBDeviceRepository implements IDeviceRepository {
public static $column_type_power = "power";
public static $column_type_software = "Software";
public static $column_type_notes = "Other";
// ------------------------------------------------------------------------

View File

@ -81,27 +81,37 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
)->fetchAll();
}
public function get_types_by_device($device_id, $days_to_analyse = -1) {
public function get_types_by_device($device_id) {
$data = [
"device_id" => $device_id
];
if($days_to_analyse >= 0)
$data["days"] = $days_to_analyse;
$s = $this->get_static;
$o = $this->get_static_extra;
$repo_device = MariaDBDeviceRepository::class;
$repo_sensor = MariaDBSensorRepository::class;
/*
SELECT reading_value_types.*
FROM devices
JOIN device_sensors ON device_sensors.device_id = devices.device_id
JOIN sensors ON sensors.id = device_sensors.sensors_id
JOIN sensor_reading_value_types ON sensor_reading_value_types.sensor_id = sensors.id
JOIN reading_value_types ON reading_value_types.id = sensor_reading_value_types.reading_value_types_id
WHERE devices.device_id=10
GROUP BY reading_value_types.id;
*/
return $this->database->query(
"SELECT
{$s("table_name")}.*,
COUNT({$s("table_name")}.{$s("column_id")}) AS count
FROM {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}
JOIN {$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")} ON
{$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")}.{$o(MariaDBMeasurementDataRepository::class, "column_metadata_id")} = {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}.{$o(MariaDBMeasurementDataRepository::class, "column_values_reading_id")}
JOIN {$s("table_name")} ON
{$s("table_name")}.{$s("column_id")} = {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}.{$o(MariaDBMeasurementDataRepository::class, "column_values_reading_type")}
WHERE
{$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")}.{$o(MariaDBMeasurementDataRepository::class, "column_metadata_device_id")} = :device_id
" . ($days_to_analyse >= 0 ? "AND DATEDIFF(NOW(),s_or_r) = :days" : "") . "
"SELECT {$s("table_name")}.*
FROM {$o($repo_device, "table_name")}
JOIN {$o($repo_sensor, "table_name_assoc")}
ON {$o($repo_sensor, "table_name_assoc")}.{$o($repo_sensor, "col_assoc_device_id")} = {$o($repo_device, "table_name")}.{$o($repo_device, "column_device_id")}
JOIN {$o($repo_sensor, "table_name")}
ON {$o($repo_sensor, "table_name_assoc")}.{$o($repo_sensor, "col_assoc_sensor_id")} = {$o($repo_sensor, "table_name")}.{$o($repo_sensor, "col_id")}
JOIN {$o($repo_sensor, "table_name_rtassoc")}
ON {$o($repo_sensor, "table_name")}.{$o($repo_sensor, "col_id")} = {$o($repo_sensor, "table_name_rtassoc")}.{$o($repo_sensor, "col_rtassoc_sensor_id")}
JOIN {$s("table_name")}
ON {$o($repo_sensor, "table_name_rtassoc")}.{$o($repo_sensor, "col_rtassoc_rvt_id")} = {$s("table_name")}.{$s("column_id")}
WHERE {$o($repo_device, "table_name")}.{$o($repo_device, "column_device_id")} = :device_id
GROUP BY {$s("table_name")}.{$s("column_id")};",
$data
)->fetchAll();

View File

@ -19,6 +19,10 @@ class MariaDBSensorRepository implements ISensorRepository {
public static $col_assoc_device_id = "device_id";
public static $col_assoc_sensor_id = "sensors_id";
public static $table_name_rtassoc = "sensor_reading_value_types";
public static $col_rtassoc_sensor_id = "sensor_id";
public static $col_rtassoc_rvt_id = "reading_value_types_id";
// ------------------------------------------------------------------------

View File

@ -1 +1 @@
v0.13.4-dev
v0.14-dev