public async Task <ActionResult <VoterTurnout> > GetVoterTurnout([FromQuery] string electionId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(electionId))
                {
                    electionId = Consts.FirstElectionRound;
                }
                var key    = Consts.VoteTurnoutKey + electionId;
                var result = await _appCache.GetOrAddAsync(
                    key, () => _resultsAggregator.GetVoterTurnout(electionId),
                    DateTimeOffset.Now.AddSeconds(_config.Value.TurnoutCacheIntervalInSeconds));

                if (result.IsFailure)
                {
                    _appCache.Remove(key);
                    Log.LogWarning(result.Error);
                    return(BadRequest(result.Error));
                }
                return(result.Value);
            }
            catch (Exception e)
            {
                Log.LogError(e, "Exception encountered while retrieving voter turnout stats");
                return(StatusCode(500, e));
            }
        }
Пример #2
0
        private async Task SendUpdatedVoterTurnout()
        {
            _logger.LogInformation($"Sending voter turnout updates");
            var voterTurnoutResult = await _resultsAggregator.GetVoterTurnout();

            if (voterTurnoutResult.IsSuccess)
            {
                await _hubContext.Clients.All.SendAsync("turnout-updated", voterTurnoutResult.Value);
            }
        }
Пример #3
0
        public async Task <ActionResult <VoterTurnout> > GetVoterTurnout()
        {
            try
            {
                var result = await _appCache.GetOrAddAsync(
                    Consts.VOTE_TURNOUT_KEY, () => _resultsAggregator.GetVoterTurnout(), DateTimeOffset.Now.AddMinutes(5));

                if (result.IsFailure)
                {
                    _appCache.Remove(Consts.VOTE_TURNOUT_KEY);
                    _logger.LogError(result.Error);
                    return(BadRequest(result.Error));
                }
                return(result.Value);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Exception encountered while retrieving voter turnout stats");
                return(StatusCode(500, e));
            }
        }