[server/fetch-data] Dedupe records & add property to indicate the # of records

This commit is contained in:
Starbeamrainbowlabs 2019-01-17 16:51:37 +00:00
parent f51cac03ca
commit 3a62ef08d9
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 6 additions and 4 deletions

View file

@ -55,7 +55,7 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
return $this->database->query( return $this->database->query(
"SELECT "SELECT
{$s("table_name_values")}.*, {$s("table_name_values")}.*,
{$s("table_name_metadata")}.device_id, {$s("table_name_metadata")}.{$s("column_metadata_device_id")},
COALESCE( COALESCE(
{$s("table_name_metadata")}.{$s("column_metadata_recordedon")}, {$s("table_name_metadata")}.{$s("column_metadata_recordedon")},
{$s("table_name_metadata")}.{$s("column_metadata_storedon")} {$s("table_name_metadata")}.{$s("column_metadata_storedon")}
@ -67,7 +67,8 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
COALESCE( COALESCE(
{$s("table_name_metadata")}.{$s("column_metadata_long")}, {$s("table_name_metadata")}.{$s("column_metadata_long")},
{$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_long")} {$o(MariaDBDeviceRepository::class, "table_name")}.{$o(MariaDBDeviceRepository::class, "column_long")}
) AS longitude ) AS longitude,
COUNT({$s("table_name_metadata")}.{$s("column_metadata_device_id")}) AS record_count
FROM {$s("table_name_values")} 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 {$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")} 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")}
@ -80,7 +81,8 @@ class MariaDBMeasurementDataRepository implements IMeasurementDataRepository {
))) < :max_reading_timediff ))) < :max_reading_timediff
AND AND
{$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type {$s("table_name_values")}.{$s("column_values_reading_type")} = :reading_type
GROUP BY {$s("table_name_metadata")}.{$s("column_metadata_device_id")}
ORDER BY {$s("table_name_metadata")}.{$s("column_metadata_recordedon")}
", [ ", [
// The database likes strings, not PHP DateTime() instances // The database likes strings, not PHP DateTime() instances
"datetime" => $datetime->format(\DateTime::ISO8601), "datetime" => $datetime->format(\DateTime::ISO8601),

View file

@ -44,4 +44,4 @@ max-age = 2592000 # 30 days
# need to be a bit 'fuzzy' about which readings we consider to be part of which # 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 # time-step. Ideally, this value should be half of the actual data recording
# interval. # interval.
max_reading_timediff = 15 max_reading_timediff = 180