diff --git a/Environment/Service/Controllers/ReadingsController.cs b/Environment/Service/Controllers/ReadingsController.cs index da9b8f2..dbfeb3f 100644 --- a/Environment/Service/Controllers/ReadingsController.cs +++ b/Environment/Service/Controllers/ReadingsController.cs @@ -38,4 +38,10 @@ public class ReadingsController(Database database) : ControllerBase { return (await database.GetReadingsHistoryGrouped(start, end, bucketMinutes)).ToList(); } + + [HttpGet("aggregate")] + public async Task>> GetAggregate(DateTimeOffset start, DateTimeOffset end) + { + return (await database.GetReadingsAggregate(start, end)).ToList(); + } } \ No newline at end of file diff --git a/Environment/Service/Data/Database.cs b/Environment/Service/Data/Database.cs index a58fd8f..2f1b3e2 100644 --- a/Environment/Service/Data/Database.cs +++ b/Environment/Service/Data/Database.cs @@ -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(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }).ConfigureAwait(false); } + + public async Task> 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(query, new { Start = start, End = end }).ConfigureAwait(false); + } } \ No newline at end of file diff --git a/Environment/Service/Data/Queries/GetReadingsAggregate.psql b/Environment/Service/Data/Queries/GetReadingsAggregate.psql new file mode 100644 index 0000000..f210b26 --- /dev/null +++ b/Environment/Service/Data/Queries/GetReadingsAggregate.psql @@ -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; \ No newline at end of file diff --git a/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql b/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql index 0701758..20181b9 100644 --- a/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql +++ b/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql @@ -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, diff --git a/Environment/Service/Environment.http b/Environment/Service/Environment.http index 4291ce4..1b88651 100644 --- a/Environment/Service/Environment.http +++ b/Environment/Service/Environment.http @@ -1,4 +1,4 @@ -@Environment_HostAddress = http://localhost:5000 +@Environment_HostAddress = http://localhost:5050 GET {{Environment_HostAddress}}/readings/recent Accept: application/json @@ -10,5 +10,10 @@ Accept: application/json ### -GET {{Environment_HostAddress}}/readings/history-grouped?start=2024-02-02T06:00&end=2024-02-02T08:00&bucketMinutes=15 +GET {{Environment_HostAddress}}/readings/history-grouped?start=2024-03-11T00:00&end=2024-03-12T00:00&bucketMinutes=15 +Accept: application/json + +### + +GET {{Environment_HostAddress}}/readings/aggregate?start=2024-03-11T00:00&end=2024-03-12T00:00 Accept: application/json \ No newline at end of file diff --git a/Environment/Service/Models/ReadingsAggregate.cs b/Environment/Service/Models/ReadingsAggregate.cs new file mode 100644 index 0000000..b8fbb5c --- /dev/null +++ b/Environment/Service/Models/ReadingsAggregate.cs @@ -0,0 +1,36 @@ +using System.Text.Json.Serialization; + +namespace ChrisKaczor.HomeMonitor.Environment.Service.Models; + +public class ReadingsAggregate +{ + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("minimumTemperature")] + public decimal MinimumTemperature { get; set; } + + [JsonPropertyName("averageTemperature")] + public decimal AverageTemperature { get; set; } + + [JsonPropertyName("maximumTemperature")] + public decimal MaximumTemperature { get; set; } + + [JsonPropertyName("minimumPressure")] + public decimal MinimumPressure { get; set; } + + [JsonPropertyName("averagePressure")] + public decimal AveragePressure { get; set; } + + [JsonPropertyName("maximumPressure")] + public decimal MaximumPressure { get; set; } + + [JsonPropertyName("minimumHumidity")] + public decimal MinimumHumidity { get; set; } + + [JsonPropertyName("averageHumidity")] + public decimal AverageHumidity { get; set; } + + [JsonPropertyName("maximumHumidity")] + public decimal MaximumHumidity { get; set; } +} \ No newline at end of file diff --git a/Environment/Service/Service.csproj b/Environment/Service/Service.csproj index 07fe0a0..0d3d65c 100644 --- a/Environment/Service/Service.csproj +++ b/Environment/Service/Service.csproj @@ -11,12 +11,14 @@ + +