public GetStatsResponse GetStats(GetStatsRequest request) { try { return(DB.GetStats()); } catch (Exception) { return(null); } }
/// <nodoc /> public async Task <GetStatsResponse> GetStatsAsync(GetStatsRequest request, CancellationToken token) { var cacheContext = new Context(Guid.NewGuid(), _logger); var counters = await _sessionHandler.GetStatsAsync(new OperationContext(cacheContext, token)); if (!counters) { return(GetStatsResponse.Failure()); } return(GetStatsResponse.Create(counters.Value.ToDictionaryIntegral())); }
public async Task <GetStatsResponse> GetStatsAsync(GetStatsRequest request, CancellationToken token) { var cacheContext = new Context(Guid.NewGuid(), Logger); var counters = await ContentSessionHandler.GetStatsAsync(new OperationContext(cacheContext, token)); if (!counters) { return(GetStatsResponse.Failure()); } var result = counters.Value !; result.Merge(Counters.ToCounterSet(), "GrpcContentServer"); return(GetStatsResponse.Create(result.ToDictionaryIntegral())); }
/// <summary> /// Convert the statistics query into a statistics request object. /// Validate the parts of the query. /// </summary> /// <param name="query"></param> /// <returns>A valid statistics request object</returns> public static GetStatsRequest GetStatsRequestFromQuery(GetStatsQuery query) { GetStatsRequest result = null; var currencyLength = query.CurrencyTypes.Trim().Length; if (query != null && (currencyLength == 8) && (query.CurrencyTypes.Contains("->"))) { if (query.Dates != null && query.Dates.Length >= 8) { var allValidDates = ValidateDates(query.Dates.Split(',').ToList()); result = new GetStatsRequest(); result.CurrencyConversion = query.CurrencyTypes.ToUpperInvariant(); result.Dates = allValidDates; } } return(result); }
/// <nodoc /> protected abstract AsyncUnaryCall <GetStatsResponse> GetStatsAsync(GetStatsRequest getStatsRequest);
/// <inheritdoc /> protected override AsyncUnaryCall <GetStatsResponse> GetStatsAsync(GetStatsRequest getStatsRequest) { return(_client.GetStatsAsync(getStatsRequest)); }
/// <summary> /// Check if the dates are available in the cache. /// Y: serve from cache, N: make a new request to external api, then cache them as well. /// Filter the data. /// From the cached results calculate the Min,Max and Avg statistics for the dates. /// </summary> /// <param name="statsRequest"></param> /// <returns>Statistics for provided dates</returns> /// public async Task <Statistics> GetStatsAsync(GetStatsRequest statsRequest) { List <ExchangeRates> poolOfExchangeRatesFromCache = new List <ExchangeRates>(); var cachedRespStr = await _responseCacheService.GetCachedResponseAsync(_cacheKey); if (!String.IsNullOrEmpty(cachedRespStr)) { poolOfExchangeRatesFromCache = JsonConvert.DeserializeObject <List <ExchangeRates> >(cachedRespStr); poolOfExchangeRatesFromCache = poolOfExchangeRatesFromCache.Where(d => statsRequest.Dates.Contains(d.CurrenyOnDate)).ToList(); } // Get only the non-cached dates statsRequest.Dates = statsRequest.Dates.Except(poolOfExchangeRatesFromCache.Select(d => d.CurrenyOnDate)); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken token = cancellationTokenSource.Token; // Get new exchange rates from external source foreach (var queryDate in statsRequest.Dates) { string apiUrl = Utility.Utility.GetExchangeRatesApiUrlWithParameters(_exchangeRatesApiUrl, queryDate, statsRequest.CurrencyConversion); var currencyRatesForDate = await GetExchangeRateDataFromStreamAsync(token, apiUrl); // Remove the entries that don't have exchange rates for the queried date currencyRatesForDate = currencyRatesForDate.Where(c => c.CurrenyOnDate == queryDate).ToList(); if (currencyRatesForDate.Any()) { // Store this value in the cache await _responseCacheService.CacheResponseAsync(_cacheKey, currencyRatesForDate, new TimeSpan(0, 1, 0)); // cache it for 1 minute } // Merge the newly cached results with the existing results poolOfExchangeRatesFromCache.AddRange(currencyRatesForDate); } if (!poolOfExchangeRatesFromCache.Any()) { return(new Statistics()); } var leastValue = poolOfExchangeRatesFromCache .OrderBy(o => o.CurrenyOnDate) .Min(m => m.ToCurrencyValue); var leastValueDate = poolOfExchangeRatesFromCache .OrderBy(o => o.CurrenyOnDate) .Where(m => m.ToCurrencyValue == leastValue) .Select(m => m.CurrenyOnDate) .FirstOrDefault(); var maxValue = poolOfExchangeRatesFromCache .OrderBy(o => o.CurrenyOnDate) .Max(m => m.ToCurrencyValue); var maxValueDate = poolOfExchangeRatesFromCache .OrderBy(o => o.CurrenyOnDate) .Where(m => m.ToCurrencyValue == maxValue) .Select(m => m.CurrenyOnDate) .FirstOrDefault(); var avgValue = poolOfExchangeRatesFromCache .Average(m => m.ToCurrencyValue); var stats = new Statistics() { Min = leastValue, MinDate = leastValueDate, Max = maxValue, MaxDate = maxValueDate, Avg = avgValue }; return(stats); }
/// <inheritdoc /> public override Task <GetStatsResponse> GetStats(GetStatsRequest request, ServerCallContext context) => _contentServer.GetStatsAsync(request, context.CancellationToken);
public override async Task <GetStatsResponse> GetStats(GetStatsRequest request, ServerCallContext context) { throw new NotImplementedException(); // return await _db.GetStats(request, context.CancellationToken); }