public async Task <IActionResult> GetAverageTemperatureAsync([FromBody] DateIntervalInputModel intervalInputModel) { try { var data = await _dataFetcher.GetData(); return(Ok(_weatherAnalyzerService.AverageTemperature(data, intervalInputModel))); } catch (Exception e) { return(HandleException(e)); } }
private static List <WeatherDataSample> GetDataWithinInterval(List <WeatherDataSample> data, DateIntervalInputModel timeInterval) { if (timeInterval != default) { data = data.Where(d => d.Time > timeInterval.From && d.Time < timeInterval.To).ToList(); if (data.Count == 0) { throw new InvalidTimeIntervalException("The time interval is outside of data range"); } } return(data); }
public TemperatureValueViewModel AverageTemperature(List <WeatherDataSample> data, DateIntervalInputModel timeInterval = default) { var intervalData = GetDataWithinInterval(data, timeInterval); return(new TemperatureValueViewModel { Value = intervalData.Average(d => d.Value) }); }
public TemparetureDataPointViewModel LowestTemperature(List <WeatherDataSample> data, DateIntervalInputModel timeInterval = default) { var intervalData = GetDataWithinInterval(data, timeInterval); var min = intervalData.Min(sample => sample.Value); return(new TemparetureDataPointViewModel { Temperature = min, Time = intervalData.FirstOrDefault(sample => sample.Value == min).Time }); }