public async Task <HttpResponseMessage> GetLatestAlertHistoryAsync() { Func <Task <AlertHistoryResultsModel> > loadHistoryItems = async() => { // Dates are stored internally as UTC and marked as such. // When parsed, they'll be made relative to the server's // time zone. This is only in an issue on servers machines, // not set to GMT. DateTime currentTime = DateTime.UtcNow; var historyItems = new List <AlertHistoryItemModel>(); var locationModels = new List <AlertHistoryLocationModel>(); var resultsModel = new AlertHistoryResultsModel(); IEnumerable <AlertHistoryItemModel> data = await _alertsLogic.LoadLatestAlertHistoryAsync( currentTime.Subtract(CautionAlertMaxDelta), DISPLAYED_HISTORY_ITEMS); if (data != null) { historyItems.AddRange(data); //get alert history List <DeviceModel> devices = await this.LoadAllDevicesAsync(); if (devices != null) { LocationJerkListModel locationJerkListModel = await _locationJerkLogic.GetLocationJerkListModel(); //DeviceListLocationsModel locationsModel = _deviceLogic.ExtractLocationsData(devices); if (locationJerkListModel != null) { resultsModel.MaxLatitude = locationJerkListModel.MaximumLatitude; resultsModel.MaxLongitude = locationJerkListModel.MaximumLongitude; resultsModel.MinLatitude = locationJerkListModel.MinimumLatitude; resultsModel.MinLongitude = locationJerkListModel.MinimumLongitude; if (locationJerkListModel.LocationJerkList != null) { //Func<string, DateTime?> getStatusTime = //_deviceTelemetryLogic.ProduceGetLatestDeviceAlertTime(historyItems); foreach (LocationJerkModel locationModel in locationJerkListModel.LocationJerkList) { if ((locationModel == null) || (locationModel.Latitude == null) || (locationModel.Longitude == null) || (locationModel.DeviceList == null)) { continue; } double maxJerk = GetMaximumJerkAtLocation(locationModel); var alertHistoryLocationModel = new AlertHistoryLocationModel() { Latitude = (double)locationModel.Latitude, Longitude = (double)locationModel.Longitude, TotalDeviceCount = locationModel.DeviceList.Count(), Status = locationModel.Status, MaxJerk = maxJerk, HighestJerks = GetHighestJerks(locationModel) }; //DateTime? lastStatusTime = getStatusTime(locationModel.DeviceId); //if (lastStatusTime.HasValue) //{ // TimeSpan deltaTime = currentTime - lastStatusTime.Value; // if (deltaTime < CriticalAlertMaxDelta) // { // deviceModel.Status = AlertHistoryDeviceStatus.Critical; // } // else if (deltaTime < CautionAlertMaxDelta) // { // deviceModel.Status = AlertHistoryDeviceStatus.Caution; // } //} locationModels.Add(alertHistoryLocationModel); } } } } } resultsModel.Data = historyItems.Take(DISPLAYED_HISTORY_ITEMS).ToList(); resultsModel.JerkLocations = locationModels; resultsModel.TotalAlertCount = historyItems.Count; resultsModel.TotalFilteredCount = historyItems.Count; return(resultsModel); }; return(await GetServiceResponseAsync <AlertHistoryResultsModel>(loadHistoryItems, false)); }
public async Task <LocationJerkListModel> GetLocationJerkListModel() { var result = new LocationJerkListModel(); // Initialize defaults to opposite extremes to ensure mins and maxes are beyond any actual values double minLat = double.MaxValue; double maxLat = double.MinValue; double minLong = double.MaxValue; double maxLong = double.MinValue; var locationList = new List <LocationJerkModel>(); IEnumerable <LocationJerkModel> fetchedData = await LoadLatestLocationJerkInfoAsync(); if (fetchedData != null) { locationList.AddRange(fetchedData); } if (locationList != null && locationList.Count > 0) { foreach (LocationJerkModel location in locationList) { if (location.DeviceList != null && location.Latitude != null && location.Longitude != null) { double latitude = (double)location.Latitude; double longitude = (double)location.Longitude; if (longitude < minLong) { minLong = longitude; } if (longitude > maxLong) { maxLong = longitude; } if (latitude < minLat) { minLat = latitude; } if (latitude > maxLat) { maxLat = latitude; } } } } if (locationList.Count == 0) { // reinitialize bounds to center on Seattle area if no devices minLat = 47.6; maxLat = 47.6; minLong = -122.3; maxLong = -122.3; } double offset = 0.05; result.LocationJerkList = locationList; result.MinimumLatitude = minLat - offset; result.MaximumLatitude = maxLat + offset; result.MinimumLongitude = minLong - offset; result.MaximumLongitude = maxLong + offset; return(result); }