public async Task <List <WindParametersVm> > Handle(GetWindParametersQuery request, CancellationToken cancellationToken) { var currentDate = _dateTimeProvider.GetUtcNow(); request.WithDefaultValues(currentDate.AddDays(-2), currentDate); int granulation = (int)(request.Granulation ?? default); Debug.Assert(request.From != null, "request.From != null"); var fromDate = request.From.Value; var result = await _applicationDbContext.WeatherStationWindParameters .AsNoTracking() .Where(x => x.MeasurementStartTime >= request.From && x.MeasurementEndTime <= request.To) .Select(x => new { TimestampGroup = granulation == (int)DateRangeGranulation.Year ? EF.Functions.DateDiffYear(fromDate, x.MeasurementEndTime) : granulation == (int)DateRangeGranulation.Month ? EF.Functions.DateDiffMonth(fromDate, x.MeasurementEndTime) : EF.Functions.DateDiffSecond(fromDate, x.MeasurementEndTime) / granulation, x.AverageWindSpeed, x.MaxWindSpeed, x.MinWindSpeed, x.MostFrequentWindDirection }) .ToListAsync(cancellationToken); //EF Core 3.1 cannot do nested grouping on DB side so we need do this in-memory ... return(result.GroupBy(x => x.TimestampGroup) .OrderBy(x => x.Key) .Select(g => new WindParametersVm { Timestamp = DateTime.SpecifyKind( granulation == (int)DateRangeGranulation.Year ? fromDate.AddYears(g.Key) : granulation == (int)DateRangeGranulation.Month ? fromDate.AddMonths(g.Key) : fromDate.AddSeconds(g.Key * granulation), DateTimeKind.Utc), AverageWindSpeed = Math.Round(g.Average(x => x.AverageWindSpeed), 2), MaxWindSpeed = Math.Round(g.Max(x => x.MaxWindSpeed), 2), MinWindSpeed = Math.Round(g.Min(x => x.MinWindSpeed), 2), MostFrequentWindDirection = g .GroupBy(x => x.MostFrequentWindDirection) .Select(f => new { Direction = f.Key, Count = f.Count() }) .OrderByDescending(f => f.Count).First().Direction }) .ToList()); }
public Task <IEnumerable <WindParametersVm> > GetWindParameters(GetWindParametersQuery query) { return(_apiClient.Get <GetWindParametersQuery, IEnumerable <WindParametersVm> >("WeatherData/wind", query)); }
public async Task <IActionResult> GetWindParameters([FromQuery] GetWindParametersQuery query) { return(Ok(await _mediator.Send(query))); }