From 618688ed08a4669c92275687c946366e179fb3a0 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Fri, 11 Oct 2019 16:26:21 -0400 Subject: [PATCH] Rework wind history API --- .../Service/Controllers/ReadingsController.cs | 12 +++--------- Weather/Service/Data/Database.cs | 15 +++------------ .../Resources/GetWindDirectionHistory.sql | 5 ----- ...dHistory.sql => GetWindHistoryGrouped.sql} | 10 ++++++---- .../Models/WindDirectionReadingGrouped.cs | 12 ------------ Weather/Service/Models/WindHistoryGrouped.cs | 19 +++++++++++++++++++ .../Service/Models/WindSpeedReadingGrouped.cs | 17 ----------------- Weather/Service/Service.csproj | 6 ++---- 8 files changed, 33 insertions(+), 63 deletions(-) delete mode 100644 Weather/Service/Data/Resources/GetWindDirectionHistory.sql rename Weather/Service/Data/Resources/{GetWindSpeedHistory.sql => GetWindHistoryGrouped.sql} (62%) delete mode 100644 Weather/Service/Models/WindDirectionReadingGrouped.cs create mode 100644 Weather/Service/Models/WindHistoryGrouped.cs delete mode 100644 Weather/Service/Models/WindSpeedReadingGrouped.cs diff --git a/Weather/Service/Controllers/ReadingsController.cs b/Weather/Service/Controllers/ReadingsController.cs index 0403a0b..ebf0b32 100644 --- a/Weather/Service/Controllers/ReadingsController.cs +++ b/Weather/Service/Controllers/ReadingsController.cs @@ -44,16 +44,10 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Controllers return (await _database.GetReadingHistoryGrouped(start, end, bucketMinutes)).ToList(); } - [HttpGet("wind-speed-history-grouped")] - public async Task>> GetWindSpeedHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 2) + [HttpGet("wind-history-grouped")] + public async Task>> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes = 60) { - return (await _database.GetWindSpeedHistoryGrouped(start, end, bucketMinutes)).ToList(); - } - - [HttpGet("wind-direction-history-grouped")] - public async Task>> GetWindDirectionHistoryGrouped(DateTimeOffset start, DateTimeOffset end) - { - return (await _database.GetWindDirectionHistoryGrouped(start, end)).ToList(); + return (await _database.GetWindHistoryGrouped(start, end, bucketMinutes)).ToList(); } } } \ No newline at end of file diff --git a/Weather/Service/Data/Database.cs b/Weather/Service/Data/Database.cs index c553b89..60f1256 100644 --- a/Weather/Service/Data/Database.cs +++ b/Weather/Service/Data/Database.cs @@ -118,22 +118,13 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data return await connection.QueryAsync(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); } - public async Task> GetWindSpeedHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes) + public async Task> GetWindHistoryGrouped(DateTimeOffset start, DateTimeOffset end, int bucketMinutes) { await using var connection = CreateConnection(); - var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetWindSpeedHistory.sql"); + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetWindHistoryGrouped.sql"); - return await connection.QueryAsync(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); - } - - public async Task> GetWindDirectionHistoryGrouped(DateTimeOffset start, DateTimeOffset end) - { - await using var connection = CreateConnection(); - - var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetWindDirectionHistory.sql"); - - return await connection.QueryAsync(query, new { Start = start, End = end }); + return await connection.QueryAsync(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }); } } } \ No newline at end of file diff --git a/Weather/Service/Data/Resources/GetWindDirectionHistory.sql b/Weather/Service/Data/Resources/GetWindDirectionHistory.sql deleted file mode 100644 index 2a39787..0000000 --- a/Weather/Service/Data/Resources/GetWindDirectionHistory.sql +++ /dev/null @@ -1,5 +0,0 @@ -SELECT WindDirection, COUNT(WindDirection) AS Count -FROM Reading -WHERE Timestamp BETWEEN @Start AND @End - AND WindDirection != -1 -GROUP BY WindDirection \ No newline at end of file diff --git a/Weather/Service/Data/Resources/GetWindSpeedHistory.sql b/Weather/Service/Data/Resources/GetWindHistoryGrouped.sql similarity index 62% rename from Weather/Service/Data/Resources/GetWindSpeedHistory.sql rename to Weather/Service/Data/Resources/GetWindHistoryGrouped.sql index 36f7862..7059c8b 100644 --- a/Weather/Service/Data/Resources/GetWindSpeedHistory.sql +++ b/Weather/Service/Data/Resources/GetWindHistoryGrouped.sql @@ -1,12 +1,14 @@ SELECT Bucket, - MIN(WindSpeed) AS Minimum, - AVG(WindSpeed) AS Average, - MAX(WindSpeed) AS Maximum + MIN(WindSpeed) AS MinimumSpeed, + AVG(WindSpeed) AS AverageSpeed, + MAX(WindSpeed) AS MaximumSpeed, + AVG(WindDirection) AS AverageDirection 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, - WindSpeed + WindSpeed, + WindDirection FROM Reading WHERE Timestamp BETWEEN @Start AND @End ) AS Data diff --git a/Weather/Service/Models/WindDirectionReadingGrouped.cs b/Weather/Service/Models/WindDirectionReadingGrouped.cs deleted file mode 100644 index eacceb5..0000000 --- a/Weather/Service/Models/WindDirectionReadingGrouped.cs +++ /dev/null @@ -1,12 +0,0 @@ -using JetBrains.Annotations; - -namespace ChrisKaczor.HomeMonitor.Weather.Service.Models -{ - [PublicAPI] - public class WindDirectionReadingGrouped - { - public int WindDirection { get; set; } - - public int Count { get; set; } - } -} \ No newline at end of file diff --git a/Weather/Service/Models/WindHistoryGrouped.cs b/Weather/Service/Models/WindHistoryGrouped.cs new file mode 100644 index 0000000..0dd0941 --- /dev/null +++ b/Weather/Service/Models/WindHistoryGrouped.cs @@ -0,0 +1,19 @@ +using JetBrains.Annotations; +using System; + +namespace ChrisKaczor.HomeMonitor.Weather.Service.Models +{ + [PublicAPI] + public class WindHistoryGrouped + { + public DateTimeOffset Bucket { get; set; } + + public decimal MinimumSpeed { get; set; } + + public decimal AverageSpeed { get; set; } + + public decimal MaximumSpeed { get; set; } + + public decimal AverageDirection { get; set; } + } +} \ No newline at end of file diff --git a/Weather/Service/Models/WindSpeedReadingGrouped.cs b/Weather/Service/Models/WindSpeedReadingGrouped.cs deleted file mode 100644 index f7507db..0000000 --- a/Weather/Service/Models/WindSpeedReadingGrouped.cs +++ /dev/null @@ -1,17 +0,0 @@ -using JetBrains.Annotations; -using System; - -namespace ChrisKaczor.HomeMonitor.Weather.Service.Models -{ - [PublicAPI] - public class WindSpeedReadingGrouped - { - public DateTimeOffset Bucket { get; set; } - - public decimal Minimum { get; set; } - - public decimal Average { get; set; } - - public decimal Maximum { get; set; } - } -} \ No newline at end of file diff --git a/Weather/Service/Service.csproj b/Weather/Service/Service.csproj index 7367fe0..da96d6c 100644 --- a/Weather/Service/Service.csproj +++ b/Weather/Service/Service.csproj @@ -14,9 +14,8 @@ + - - @@ -26,8 +25,7 @@ - - +