public async Task UpdateSpikeAsync(ErrorReportSpike spike)
 {
     await _unitOfWork.UpdateAsync(spike);
 }
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            var notificationSettings = (await _repository.GetAllAsync(e.Report.ApplicationId)).ToList();

            if (notificationSettings.All(x => x.ApplicationSpike == NotificationState.Disabled))
            {
                return;
            }

            var countToday = await CalculateSpike(e);

            if (countToday == null)
            {
                return;
            }

            var spike = await _spikeRepository.GetSpikeAsync(e.Incident.ApplicationId);

            spike?.IncreaseReportCount();

            var existed = spike != null;

            foreach (var setting in notificationSettings)
            {
                if (setting.ApplicationSpike == NotificationState.Disabled)
                {
                    continue;
                }

                if (spike != null && spike.HasAccount(setting.AccountId))
                {
                    continue;
                }

                if (spike == null)
                {
                    spike = new ErrorReportSpike(e.Incident.ApplicationId, 1);
                }

                spike.AddNotifiedAccount(setting.AccountId);

                if (setting.ApplicationSpike == NotificationState.Email)
                {
                    var msg = BuildEmail(e, setting, countToday);
                    var cmd = new SendEmail(msg);
                    await context.SendAsync(cmd);
                }
                else
                {
                    var notification = BuildBrowserNotification(e, countToday, setting);
                    try
                    {
                        await _notificationService.SendBrowserNotification(setting.AccountId, notification);
                    }
                    catch (Exception ex)
                    {
                        Err.Report(ex, new { setting, notification });
                    }
                }
            }

            if (existed)
            {
                await _spikeRepository.UpdateSpikeAsync(spike);
            }
            else
            {
                await _spikeRepository.CreateSpikeAsync(spike);
            }
        }
 public async Task CreateSpikeAsync(ErrorReportSpike spike)
 {
     await _unitOfWork.InsertAsync(spike);
 }
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            _log.Info("ReportId: " + e.Report.Id);

            var url = _baseConfiguration.BaseUrl;
            var notificationSettings = (await _repository.GetAllAsync(e.Report.ApplicationId)).ToList();

            if (notificationSettings.All(x => x.ApplicationSpike == NotificationState.Disabled))
            {
                return;
            }

            var todaysCount = await CalculateSpike(e);

            if (todaysCount == null)
            {
                return;
            }

            var spike = await _spikeRepository.GetSpikeAsync(e.Incident.ApplicationId);

            if (spike != null)
            {
                spike.IncreaseReportCount();
            }

            var existed  = spike != null;
            var messages = new List <EmailMessage>();

            foreach (var setting in notificationSettings)
            {
                if (setting.ApplicationSpike == NotificationState.Disabled)
                {
                    continue;
                }

                if (spike != null && spike.HasAccount(setting.AccountId))
                {
                    continue;
                }

                if (spike == null)
                {
                    spike = new ErrorReportSpike(e.Incident.ApplicationId, 1);
                }

                spike.AddNotifiedAccount(setting.AccountId);
                var msg = new EmailMessage(setting.AccountId.ToString())
                {
                    Subject  = $"Spike detected for {e.Incident.ApplicationName} ({todaysCount} reports)",
                    HtmlBody =
                        $"We've detected a spike in incoming reports for application <a href=\"{url}/discover/{e.Incident.ApplicationId}/\">{e.Incident.ApplicationName}</a>\r\n" +
                        "\r\n" +
                        $"We've received {todaysCount.SpikeCount} reports so far. Day average is {todaysCount.DayAverage}\r\n" +
                        "\r\n" + "No further spike emails will be sent today for this application."
                };

                messages.Add(msg);
            }

            if (existed)
            {
                await _spikeRepository.UpdateSpikeAsync(spike);
            }
            else
            {
                await _spikeRepository.CreateSpikeAsync(spike);
            }

            foreach (var message in messages)
            {
                var sendEmail = new SendEmail(message);
                await context.SendAsync(sendEmail);
            }
        }