public async Task <AggregateDataModel> GetAggregatedDataAsync(RestaurantAction query) { var actions = _mongoDbClient.MongoDatabase.GetCollection <RestaurantAction>(_settings.ActionCollectionName); var filters = new List <FilterDefinition <RestaurantAction> >(); if (!string.IsNullOrWhiteSpace(query?.UserAction)) { var filter = Builders <RestaurantAction> .Filter.Eq(c => c.UserAction, query.UserAction.Trim()); filters.Add(filter); } if (!string.IsNullOrWhiteSpace(query?.Dish)) { var filter = Builders <RestaurantAction> .Filter.Eq(c => c.Dish, query.Dish.Trim()); filters.Add(filter); } if (!string.IsNullOrWhiteSpace(query?.Station)) { var filter = Builders <RestaurantAction> .Filter.Eq(c => c.Station, query.Station.Trim()); filters.Add(filter); } if (!filters.Any()) { filters.Add(Builders <RestaurantAction> .Filter.Empty); } var aggregateFilters = Builders <RestaurantAction> .Filter.And(filters); var result = await actions.Find(aggregateFilters) .ToListAsync().ConfigureAwait(false); return(new AggregateDataModel { TotalDuration = Math.Round(result.Sum(x => x.Duration), 3), AverageDuration = Math.Round(result.Sum(x => x.Duration) / result.Count, 3), TotalItems = result.Count, Actions = result.GroupBy(x => x.UserAction).Select(x => new Pair { Value = x.Count(), Key = x.Key }), Dishes = result.GroupBy(x => x.Dish).Select(x => new Pair { Value = x.Count(), Key = x.Key }), Stations = result.GroupBy(x => x.Station).Select(x => new Pair { Value = x.Count(), Key = x.Key }) }); }
public async Task <AggregateDataModel> GetAggregatedDataAsync(RestaurantAction query) { return(await _restaurantRepository.GetAggregatedDataAsync(query).ConfigureAwait(false)); }