コード例 #1
0
        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));
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public TemperatureValueViewModel AverageTemperature(List <WeatherDataSample> data, DateIntervalInputModel timeInterval = default)
        {
            var intervalData = GetDataWithinInterval(data, timeInterval);

            return(new TemperatureValueViewModel
            {
                Value = intervalData.Average(d => d.Value)
            });
        }
コード例 #4
0
        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
            });
        }