mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-02-09 09:42:37 -05:00
Add aggregate API and fix (hack) time bucket
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
|
||||
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
||||
using Dapper;
|
||||
using DbUp;
|
||||
using Npgsql;
|
||||
using System.Reflection;
|
||||
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
||||
|
||||
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||
|
||||
@@ -67,6 +67,17 @@ public class Database(IConfiguration configuration)
|
||||
|
||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsHistoryGrouped.psql");
|
||||
|
||||
query = query.Replace("@BucketMinutes", bucketMinutes.ToString());
|
||||
|
||||
return await connection.QueryAsync<ReadingsGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ReadingsAggregate>> GetReadingsAggregate(DateTimeOffset start, DateTimeOffset end)
|
||||
{
|
||||
await using var connection = CreateConnection();
|
||||
|
||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsAggregate.psql");
|
||||
|
||||
return await connection.QueryAsync<ReadingsAggregate>(query, new { Start = start, End = end }).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
13
Environment/Service/Data/Queries/GetReadingsAggregate.psql
Normal file
13
Environment/Service/Data/Queries/GetReadingsAggregate.psql
Normal file
@@ -0,0 +1,13 @@
|
||||
SELECT name,
|
||||
ROUND(min(temperature), 2) AS minimumTemperature,
|
||||
ROUND(avg(temperature), 2) AS averageTemperature,
|
||||
ROUND(max(temperature), 2) AS maximumTemperature,
|
||||
ROUND(min(pressure), 2) AS minimumPressure,
|
||||
ROUND(avg(pressure), 2) AS averagePressure,
|
||||
ROUND(max(pressure), 2) AS maximumPressure,
|
||||
ROUND(min(humidity), 2) AS minimumHumidity,
|
||||
ROUND(avg(humidity), 2) AS averageHumidity,
|
||||
ROUND(max(humidity), 2) AS maximumHumidity
|
||||
FROM reading
|
||||
WHERE time BETWEEN @Start AND @End
|
||||
GROUP BY name;
|
||||
@@ -1,5 +1,5 @@
|
||||
SELECT
|
||||
time_bucket('15 minute', time) AS bucket,
|
||||
time_bucket('@BucketMinutes minute', time) AS bucket,
|
||||
name,
|
||||
ROUND(avg(temperature), 2) AS averageTemperature,
|
||||
ROUND(avg(pressure), 2) AS averagePressure,
|
||||
|
||||
Reference in New Issue
Block a user