Exemplo n.º 1
0
        public async Task<LightsResponse> HandleRequestAsync(LightsRequest request)
        {
            var now = _dateTimeProvider.UtcNow();

            var metric = new Metric
            {
                TimestampUtc = now,
                RequestText = request.Text,
                Source = request.Source,
                From = request.From
            };

            var lightDisplay = await _lightDisplayParser.ParseAsync(request.Text);

            if (lightDisplay.Colours.Count == 0)
            {
                await _metricWriter.SendAsync(metric);

                return LightsResponse.Failure();
            }

            var scheduledEnqueueTimeUtc = _lastRequestUtc.AddMinutes(1);
            
            if (now > scheduledEnqueueTimeUtc || (request.From == _lastRequestUser && !_isScheduleActive))
            {
                _lastRequestUtc = now;
                _isScheduleActive = false;
                
                await _busPublisher.PublishAsync(lightDisplay);
            }
            else
            {
                _isScheduleActive = true;
                _lastRequestUtc = scheduledEnqueueTimeUtc;

                await _busPublisher.PublishAsync(lightDisplay, scheduledEnqueueTimeUtc);
            }

            _lastRequestUser = request.From;

            metric.IsValidCommand = true;
            metric.DisplayType = lightDisplay.GetType().Name;
            metric.Command = lightDisplay;
            metric.IsScheduled = _isScheduleActive;
            metric.ScheduledAtUtc = _isScheduleActive ? (DateTime?) scheduledEnqueueTimeUtc : null;
            metric.ScheduledDelaySecs = _isScheduleActive ? (double?)(scheduledEnqueueTimeUtc - now).TotalSeconds : null;

            await _metricWriter.SendAsync(metric);

            return _isScheduleActive ? LightsResponse.Scheduled(scheduledEnqueueTimeUtc) : LightsResponse.Success();
        }
Exemplo n.º 2
0
 public async Task SendAsync(Metric metric)
 {
     await _elasticClient.IndexAsync(metric);
 }