- Stop storing history in memory

- Add start/end dates to history requests
- Cleanup
This commit is contained in:
2015-03-29 17:57:05 -04:00
parent 340a486f95
commit 297a2914ad
14 changed files with 137 additions and 420 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using WeatherService.Devices;
using WeatherService.Values;
@@ -11,9 +12,6 @@ namespace WeatherService.Remote
[OperationContract]
List<DeviceBase> GetDevices();
[OperationContract]
ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType);
[OperationContract]
bool Subscribe();
@@ -21,15 +19,12 @@ namespace WeatherService.Remote
bool Unsubscribe();
[OperationContract]
DeviceHistory GetDeviceHistory(string deviceAddress);
Dictionary<DeviceBase, List<ReadingBase>> GetGenericHistory(WeatherValueType valueType, DateTimeOffset start, DateTimeOffset end);
[OperationContract]
Dictionary<DeviceBase, List<ReadingBase>> GetDeviceHistoryByValueType(WeatherValueType valueType);
Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes, DateTimeOffset start, DateTimeOffset end);
[OperationContract]
Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes);
[OperationContract]
Dictionary<string, int> GetWindDirectionHistory();
Dictionary<string, int> GetWindDirectionHistory(DateTimeOffset start, DateTimeOffset end);
}
}

View File

@@ -5,12 +5,30 @@ using System.Linq;
using WeatherService.Data;
using WeatherService.Values;
using WeatherService.Devices;
using WeatherService.Reporting;
namespace WeatherService.Remote
{
internal static class WeatherServiceCommon
{
private static List<ReadingBase> LoadHistory(WeatherValueType valueType, int deviceId, DateTimeOffset start, DateTimeOffset end)
{
var history = new List<ReadingBase>();
for (var year = start.Year; year <= end.Year; year++)
{
using (var archiveData = new WeatherArchiveData(year))
{
var readings = archiveData.Readings;
var yearlyHistory = readings.Where(r => r.DeviceId == deviceId && r.Type == (int) valueType && r.ReadTime >= start && r.ReadTime <= end).ToList();
history.AddRange(yearlyHistory.Select(r => ReadingBase.CreateReading(valueType, r.ReadTime.DateTime, r.Value)));
}
}
return history;
}
public static List<DeviceBase> GetDevices()
{
var deviceList = Program.Session.Devices.ToList();
@@ -18,48 +36,21 @@ namespace WeatherService.Remote
return deviceList;
}
public static ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType)
{
var deviceBase = Program.Session.Devices.FirstOrDefault(d => d.Address == deviceAddress);
if (deviceBase == null)
return null;
return deviceBase.GetValue(valueType).Current;
}
public static DeviceHistory GetDeviceHistory(string deviceAddress)
{
var deviceBase = Program.Session.Devices.FirstOrDefault(d => d.Address == deviceAddress);
if (deviceBase == null)
return null;
var deviceHistory = new DeviceHistory();
foreach (var valueEntry in deviceBase.Values)
{
deviceHistory[valueEntry.Key] = valueEntry.Value.History;
}
return deviceHistory;
}
public static Dictionary<DeviceBase, List<ReadingBase>> GetDeviceHistoryByValueType(WeatherValueType valueType)
public static Dictionary<DeviceBase, List<ReadingBase>> GetGenericHistory(WeatherValueType valueType, DateTimeOffset start, DateTimeOffset end)
{
var devices = Program.Session.Devices.Where(d => d.SupportedValues.Contains(valueType));
var deviceHistoryList = new Dictionary<DeviceBase, List<ReadingBase>>();
var deviceHistoryList = new Dictionary<DeviceBase, List<ReadingBase>>();
foreach (var device in devices)
{
deviceHistoryList[device] = device.GetValue(valueType).History;
deviceHistoryList[device] = LoadHistory(valueType, device.Id, start, end);
}
return deviceHistoryList;
}
public static Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes)
public static Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes, DateTimeOffset start, DateTimeOffset end)
{
var windSpeedHistory = new Dictionary<string, List<WindSpeedReading>>();
@@ -68,7 +59,7 @@ namespace WeatherService.Remote
if (device == null)
return null;
var values = device.GetValue(WeatherValueType.WindSpeed).History;
var values = LoadHistory(WeatherValueType.WindSpeed, device.Id, start, end);
var interval = new TimeSpan(0, groupIntervalMinutes, 0);
@@ -78,21 +69,23 @@ namespace WeatherService.Remote
Readings = d.Select(r => r.Value)
}).ToList();
windSpeedHistory["Average"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Average() }).ToList();
windSpeedHistory["Minimum"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Min() }).ToList();
windSpeedHistory["Maximum"] = groupList.Select(d => new WindSpeedReading(WeatherValueType.WindSpeed) { ReadTime = d.ReadTime, Value = d.Readings.Max() }).ToList();
windSpeedHistory["Average"] = groupList.Select(d => new WindSpeedReading() { ReadTime = d.ReadTime, Value = d.Readings.Average() }).ToList();
windSpeedHistory["Minimum"] = groupList.Select(d => new WindSpeedReading() { ReadTime = d.ReadTime, Value = d.Readings.Min() }).ToList();
windSpeedHistory["Maximum"] = groupList.Select(d => new WindSpeedReading() { ReadTime = d.ReadTime, Value = d.Readings.Max() }).ToList();
return windSpeedHistory;
}
public static Dictionary<string, int> GetWindDirectionHistory()
public static Dictionary<string, int> GetWindDirectionHistory(DateTimeOffset start, DateTimeOffset end)
{
var device = Program.Session.Devices.FirstOrDefault(d => d.SupportedValues.Contains(WeatherValueType.WindDirection));
if (device == null)
return null;
var history = device.GetValue(WeatherValueType.WindDirection).History
var values = LoadHistory(WeatherValueType.WindDirection, device.Id, start, end);
var history = values
.Cast<WindDirectionReading>()
.Where(r => r.WindDirectionValue != WindDirection.Unknown)
.OrderBy(r => r.Value);
@@ -104,31 +97,33 @@ namespace WeatherService.Remote
return grouped;
}
public static List<DailySummary> GetDailySummary(int deviceId, int valueType, DateTime startDate, DateTime endDate)
public static Dictionary<string, List<ReadingBase>> GetDailySummary(WeatherValueType valueType, int deviceId, DateTime startDate, DateTime endDate)
{
var summaryList = new List<DailySummary>();
var summaryList = new Dictionary<string, List<ReadingBase>>();
summaryList["Average"] = new List<ReadingBase>();
summaryList["Minimum"] = new List<ReadingBase>();
summaryList["Maximum"] = new List<ReadingBase>();
for (var year = startDate.Year; year <= endDate.Year; year++)
{
using (var archiveData = new WeatherArchiveData(year))
{
var groupedReadings = archiveData.Readings
var groupList = archiveData.Readings
.Where(r => r.ReadTime >= startDate &&
r.ReadTime <= endDate &&
r.DeviceId == deviceId &&
r.Type == valueType)
r.Type == (int) valueType)
.GroupBy(r => DbFunctions.TruncateTime(r.ReadTime))
.Select(r => new DailySummary
.Select(g => new
{
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);
ReadTime = g.Key,
Readings = g.Select(r => r.Value)
}).ToList();
summaryList.AddRange(groupedReadings);
summaryList["Average"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Average())));
summaryList["Minimum"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Min())));
summaryList["Maximum"].AddRange(groupList.Select(d => ReadingBase.CreateReading(valueType, d.ReadTime.Value.DateTime, d.Readings.Max())));
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using WeatherService.Devices;
using WeatherService.Values;
@@ -15,29 +16,19 @@ namespace WeatherService.Remote
return WeatherServiceCommon.GetDevices();
}
public ReadingBase GetLatestReading(string deviceAddress, WeatherValueType valueType)
public Dictionary<DeviceBase, List<ReadingBase>> GetGenericHistory(WeatherValueType valueType, DateTimeOffset start, DateTimeOffset end)
{
return WeatherServiceCommon.GetLatestReading(deviceAddress, valueType);
return WeatherServiceCommon.GetGenericHistory(valueType, start, end);
}
public DeviceHistory GetDeviceHistory(string deviceAddress)
public Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes, DateTimeOffset start, DateTimeOffset end)
{
return WeatherServiceCommon.GetDeviceHistory(deviceAddress);
return WeatherServiceCommon.GetWindSpeedHistory(groupIntervalMinutes, start, end);
}
public Dictionary<DeviceBase, List<ReadingBase>> GetDeviceHistoryByValueType(WeatherValueType valueType)
public Dictionary<string, int> GetWindDirectionHistory(DateTimeOffset start, DateTimeOffset end)
{
return WeatherServiceCommon.GetDeviceHistoryByValueType(valueType);
}
public Dictionary<string, List<WindSpeedReading>> GetWindSpeedHistory(int groupIntervalMinutes)
{
return WeatherServiceCommon.GetWindSpeedHistory(groupIntervalMinutes);
}
public Dictionary<string, int> GetWindDirectionHistory()
{
return WeatherServiceCommon.GetWindDirectionHistory();
return WeatherServiceCommon.GetWindDirectionHistory(start, end);
}
public bool Subscribe()