diff --git a/Remote/WeatherServiceCommon.cs b/Remote/WeatherServiceCommon.cs index 1660344..5e935b0 100644 --- a/Remote/WeatherServiceCommon.cs +++ b/Remote/WeatherServiceCommon.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.Data.Entity; using System.Linq; +using WeatherService.Data; using WeatherService.Values; using WeatherService.Devices; +using WeatherService.Reporting; namespace WeatherService.Remote { @@ -100,5 +103,36 @@ namespace WeatherService.Remote return grouped; } + + public static List GetDailySummary(int deviceId, int valueType, DateTime startDate, DateTime endDate) + { + var summaryList = new List(); + + for (var year = startDate.Year; year <= endDate.Year; year++) + { + using (var archiveData = new WeatherArchiveData(year)) + { + var groupedReadings = archiveData.Readings + .Where(r => r.ReadTime >= startDate && + r.ReadTime <= endDate && + r.DeviceId == deviceId && + r.Type == valueType) + .GroupBy(r => DbFunctions.TruncateTime(r.ReadTime)) + .Select(r => new DailySummary + { + Date = r.Key, + Count = r.Count(), + Minimum = r.Min(v => v.Value), + Maximum = r.Max(v => v.Value), + Average = r.Average(v => v.Value) + }) + .OrderBy(d => d.Date); + + summaryList.AddRange(groupedReadings); + } + } + + return summaryList; + } } } diff --git a/Reporting/DailySummary.cs b/Reporting/DailySummary.cs new file mode 100644 index 0000000..b63db47 --- /dev/null +++ b/Reporting/DailySummary.cs @@ -0,0 +1,13 @@ +using System; + +namespace WeatherService.Reporting +{ + public class DailySummary + { + public DateTimeOffset? Date { get; set; } + public int Count { get; set; } + public double Minimum { get; set; } + public double Maximum { get; set; } + public double Average { get; set; } + } +} diff --git a/SignalR/WeatherHub.cs b/SignalR/WeatherHub.cs index ef6cf9d..1c2d70e 100644 --- a/SignalR/WeatherHub.cs +++ b/SignalR/WeatherHub.cs @@ -1,8 +1,10 @@ -using System.Linq; +using System; +using System.Linq; using Microsoft.AspNet.SignalR; using System.Collections.Generic; using WeatherService.Devices; using WeatherService.Remote; +using WeatherService.Reporting; using WeatherService.Values; namespace WeatherService.SignalR @@ -38,5 +40,10 @@ namespace WeatherService.SignalR { return WeatherServiceCommon.GetWindDirectionHistory().ToList(); } + + public List GetDailySummary(int deviceId, int valueType, DateTime startDate, DateTime endDate) + { + return WeatherServiceCommon.GetDailySummary(deviceId, valueType, startDate, endDate); + } } } diff --git a/WeatherService.csproj b/WeatherService.csproj index b8cde52..a6ca4b3 100644 --- a/WeatherService.csproj +++ b/WeatherService.csproj @@ -157,6 +157,7 @@ +