diff --git a/Environment/Service/Controllers/ReadingsController.cs b/Environment/Service/Controllers/ReadingsController.cs index 6f00a2e..da9b8f2 100644 --- a/Environment/Service/Controllers/ReadingsController.cs +++ b/Environment/Service/Controllers/ReadingsController.cs @@ -32,4 +32,10 @@ public class ReadingsController(Database database) : ControllerBase return Ok(recentReadings); } + + [HttpGet("history-grouped")] + public async Task>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 5) + { + return (await database.GetReadingsHistoryGrouped(start, end, bucketMinutes)).ToList(); + } } \ No newline at end of file diff --git a/Environment/Service/Data/Database.cs b/Environment/Service/Data/Database.cs index d08e882..a58fd8f 100644 --- a/Environment/Service/Data/Database.cs +++ b/Environment/Service/Data/Database.cs @@ -47,7 +47,7 @@ public class Database(IConfiguration configuration) { await using var connection = CreateConnection(); - var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.CreateReading.sql"); + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.CreateReading.psql"); await connection.QueryAsync(query, message); } @@ -56,8 +56,17 @@ public class Database(IConfiguration configuration) { await using var connection = CreateConnection(); - var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetRecentReadings.sql"); + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetRecentReadings.psql"); return await connection.QueryAsync(query).ConfigureAwait(false); } + + public async Task> GetReadingsHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes) + { + await using var connection = CreateConnection(); + + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsHistoryGrouped.psql"); + + return await connection.QueryAsync(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }).ConfigureAwait(false); + } } \ No newline at end of file diff --git a/Environment/Service/Data/Queries/CreateReading.sql b/Environment/Service/Data/Queries/CreateReading.psql similarity index 100% rename from Environment/Service/Data/Queries/CreateReading.sql rename to Environment/Service/Data/Queries/CreateReading.psql diff --git a/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql b/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql new file mode 100644 index 0000000..0701758 --- /dev/null +++ b/Environment/Service/Data/Queries/GetReadingsHistoryGrouped.psql @@ -0,0 +1,20 @@ +SELECT + time_bucket('15 minute', time) AS bucket, + name, + ROUND(avg(temperature), 2) AS averageTemperature, + ROUND(avg(pressure), 2) AS averagePressure, + ROUND(avg(humidity), 2) AS averageHumidity, + ROUND(avg(luminance), 2) AS averageLuminance, + ROUND(avg(gas_resistance), 2) AS averageGasResistance, + ROUND(avg(color_temperature), 2) AS averageColorTemperature, + ROUND(avg(air_quality_index), 2) AS averageAirQualityIndex +FROM + reading +WHERE + time BETWEEN @Start AND @End +GROUP BY + bucket, + name +ORDER BY + bucket ASC, + name; diff --git a/Environment/Service/Data/Queries/GetRecentReadings.sql b/Environment/Service/Data/Queries/GetRecentReadings.psql similarity index 100% rename from Environment/Service/Data/Queries/GetRecentReadings.sql rename to Environment/Service/Data/Queries/GetRecentReadings.psql diff --git a/Environment/Service/Data/Schema/1-Initial Schema.sql b/Environment/Service/Data/Schema/1-Initial Schema.psql similarity index 100% rename from Environment/Service/Data/Schema/1-Initial Schema.sql rename to Environment/Service/Data/Schema/1-Initial Schema.psql diff --git a/Environment/Service/Environment.http b/Environment/Service/Environment.http index 6c23683..4291ce4 100644 --- a/Environment/Service/Environment.http +++ b/Environment/Service/Environment.http @@ -7,3 +7,8 @@ Accept: application/json GET {{Environment_HostAddress}}/readings/recent?tz=America/New_York Accept: application/json + +### + +GET {{Environment_HostAddress}}/readings/history-grouped?start=2024-02-02T06:00&end=2024-02-02T08:00&bucketMinutes=15 +Accept: application/json \ No newline at end of file diff --git a/Environment/Service/MessageHandler.cs b/Environment/Service/MessageHandler.cs index 2a22964..027a41b 100644 --- a/Environment/Service/MessageHandler.cs +++ b/Environment/Service/MessageHandler.cs @@ -1,9 +1,9 @@ -using System.Text.Json; -using ChrisKaczor.HomeMonitor.Environment.Service.Data; +using ChrisKaczor.HomeMonitor.Environment.Service.Data; using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor; using Microsoft.AspNetCore.SignalR.Client; using MQTTnet; using MQTTnet.Client; +using System.Text.Json; namespace ChrisKaczor.HomeMonitor.Environment.Service; @@ -22,8 +22,6 @@ public class MessageHandler : IHostedService _configuration = configuration; _database = database; - _database.EnsureDatabase(); - _topic = _configuration["Mqtt:Topic"] ?? string.Empty; if (string.IsNullOrEmpty(_topic)) diff --git a/Environment/Service/Models/ReadingsGrouped.cs b/Environment/Service/Models/ReadingsGrouped.cs new file mode 100644 index 0000000..5c3d9bb --- /dev/null +++ b/Environment/Service/Models/ReadingsGrouped.cs @@ -0,0 +1,25 @@ +using JetBrains.Annotations; + +namespace ChrisKaczor.HomeMonitor.Environment.Service.Models; + +[PublicAPI] +public class ReadingsGrouped +{ + public DateTimeOffset Bucket { get; set; } + + public string? Name { get; set; } + + public decimal AverageTemperature { get; set; } + + public decimal AveragePressure { get; set; } + + public decimal AverageHumidity { get; set; } + + public decimal AverageLuminance { get; set; } + + public decimal AverageGasResistance { get; set; } + + public decimal AverageColorTemperature { get; set; } + + public decimal AverageAirQualityIndex { get; set; } +} \ No newline at end of file diff --git a/Environment/Service/Program.cs b/Environment/Service/Program.cs index 5c7901b..ec68c3d 100644 --- a/Environment/Service/Program.cs +++ b/Environment/Service/Program.cs @@ -25,6 +25,9 @@ public static class Program app.MapControllers(); + var database = app.Services.GetRequiredService(); + database.EnsureDatabase(); + app.Run(); } } \ No newline at end of file diff --git a/Environment/Service/Service.csproj b/Environment/Service/Service.csproj index a337ca3..07fe0a0 100644 --- a/Environment/Service/Service.csproj +++ b/Environment/Service/Service.csproj @@ -10,15 +10,17 @@ - - - + + + + - - - + + + +