diff --git a/Weather/Service/Controllers/ReadingsController.cs b/Weather/Service/Controllers/ReadingsController.cs index 2bab50f..0403a0b 100644 --- a/Weather/Service/Controllers/ReadingsController.cs +++ b/Weather/Service/Controllers/ReadingsController.cs @@ -43,5 +43,17 @@ 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) + { + 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(); + } } } \ No newline at end of file diff --git a/Weather/Service/Data/Database.cs b/Weather/Service/Data/Database.cs index 3245248..c553b89 100644 --- a/Weather/Service/Data/Database.cs +++ b/Weather/Service/Data/Database.cs @@ -118,5 +118,22 @@ 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) + { + await using var connection = CreateConnection(); + + var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.GetWindSpeedHistory.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 }); + } } } \ No newline at end of file diff --git a/Weather/Service/Data/Resources/GetReadingHistoryGrouped.sql b/Weather/Service/Data/Resources/GetReadingHistoryGrouped.sql index 6bd819f..dd9cbc9 100644 --- a/Weather/Service/Data/Resources/GetReadingHistoryGrouped.sql +++ b/Weather/Service/Data/Resources/GetReadingHistoryGrouped.sql @@ -1,4 +1,4 @@ -SELECT bucket, +SELECT Bucket, AVG(HumidityTemperature) AS AverageHumidityTemperature, AVG(Humidity) AS AverageHumidity, AVG(PressureTemperature) AS AveragePressureTemperature, diff --git a/Weather/Service/Data/Resources/GetWindDirectionHistory.sql b/Weather/Service/Data/Resources/GetWindDirectionHistory.sql new file mode 100644 index 0000000..2a39787 --- /dev/null +++ b/Weather/Service/Data/Resources/GetWindDirectionHistory.sql @@ -0,0 +1,5 @@ +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/GetWindSpeedHistory.sql new file mode 100644 index 0000000..36f7862 --- /dev/null +++ b/Weather/Service/Data/Resources/GetWindSpeedHistory.sql @@ -0,0 +1,14 @@ +SELECT Bucket, + MIN(WindSpeed) AS Minimum, + AVG(WindSpeed) AS Average, + MAX(WindSpeed) AS Maximum +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 + 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/WindDirectionReadingGrouped.cs b/Weather/Service/Models/WindDirectionReadingGrouped.cs new file mode 100644 index 0000000..eacceb5 --- /dev/null +++ b/Weather/Service/Models/WindDirectionReadingGrouped.cs @@ -0,0 +1,12 @@ +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/WindSpeedReadingGrouped.cs b/Weather/Service/Models/WindSpeedReadingGrouped.cs new file mode 100644 index 0000000..f7507db --- /dev/null +++ b/Weather/Service/Models/WindSpeedReadingGrouped.cs @@ -0,0 +1,17 @@ +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 3fc8add..7367fe0 100644 --- a/Weather/Service/Service.csproj +++ b/Weather/Service/Service.csproj @@ -15,6 +15,8 @@ + + @@ -24,6 +26,8 @@ + +