From 1186f9179dde9f26b42204db79c3b7a8609af1c8 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Wed, 16 Oct 2019 20:21:08 -0400 Subject: [PATCH] Add grouped weather value API --- Weather/Service/Controllers/ReadingsController.cs | 6 ++++++ Weather/Service/Data/Database.cs | 11 +++++++++++ .../Resources/GetReadingValueHistoryGrouped.sql | 12 ++++++++++++ Weather/Service/Models/WeatherValueGrouped.cs | 13 +++++++++++++ Weather/Service/Service.csproj | 2 ++ 5 files changed, 44 insertions(+) create mode 100644 Weather/Service/Data/Resources/GetReadingValueHistoryGrouped.sql create mode 100644 Weather/Service/Models/WeatherValueGrouped.cs diff --git a/Weather/Service/Controllers/ReadingsController.cs b/Weather/Service/Controllers/ReadingsController.cs index ebf0b32..9607973 100644 --- a/Weather/Service/Controllers/ReadingsController.cs +++ b/Weather/Service/Controllers/ReadingsController.cs @@ -38,6 +38,12 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers return (await _database.GetReadingValueHistory(weatherValueType, start, end)).ToList(); } + [HttpGet("value-history-grouped")] + public async Task>> GetValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2) + { + return (await _database.GetReadingValueHistoryGrouped(weatherValueType, start, end, bucketMinutes)).ToList(); + } + [HttpGet("history-grouped")] public async Task>> GetHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2) { diff --git a/Weather/Service/Data/Database.cs b/Weather/Service/Data/Database.cs index 60f1256..664d698 100644 --- a/Weather/Service/Data/Database.cs +++ b/Weather/Service/Data/Database.cs @@ -109,6 +109,17 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data return await connection.QueryAsync(query, new { Start = start, End = end }); } + public async Task> GetReadingValueHistoryGrouped(WeatherValueType weatherValueType, DateTimeOffset start, DateTimeOffset end, int bucketMinutes) + { + await using var connection = CreateConnection(); + + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetReadingValueHistoryGrouped.sql"); + + query = query.Replace("@Value", weatherValueType.ToString()); + + return await connection.QueryAsync(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); + } + public async Task> GetReadingHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes) { await using var connection = CreateConnection(); diff --git a/Weather/Service/Data/Resources/GetReadingValueHistoryGrouped.sql b/Weather/Service/Data/Resources/GetReadingValueHistoryGrouped.sql new file mode 100644 index 0000000..1b33330 --- /dev/null +++ b/Weather/Service/Data/Resources/GetReadingValueHistoryGrouped.sql @@ -0,0 +1,12 @@ +SELECT Bucket, + AVG(Value) AS AverageValue +FROM ( + SELECT CAST(FORMAT(Timestamp, 'yyyy-MM-ddTHH:') + + RIGHT('00' + CAST(DATEPART(MINUTE, Timestamp) / @BucketMinutes * @BucketMinutes AS VARCHAR), 2) + + ':00+00:00' AS DATETIMEOFFSET) AS Bucket, + @Value AS Value + FROM Reading + WHERE Timestamp BETWEEN @Start AND @End + ) AS Data +GROUP BY Bucket +ORDER BY Bucket \ No newline at end of file diff --git a/Weather/Service/Models/WeatherValueGrouped.cs b/Weather/Service/Models/WeatherValueGrouped.cs new file mode 100644 index 0000000..c0b1daf --- /dev/null +++ b/Weather/Service/Models/WeatherValueGrouped.cs @@ -0,0 +1,13 @@ +using JetBrains.Annotations; +using System; + +namespace ChrisKaczor.HomeMonitor.Weather.Service.Models +{ + [PublicAPI] + public class WeatherValueGrouped + { + public DateTimeOffset Bucket { get; set; } + + public decimal AverageValue { get; set; } + } +} \ No newline at end of file diff --git a/Weather/Service/Service.csproj b/Weather/Service/Service.csproj index da96d6c..fe8e086 100644 --- a/Weather/Service/Service.csproj +++ b/Weather/Service/Service.csproj @@ -13,6 +13,7 @@ + @@ -21,6 +22,7 @@ +