Пример #1
0
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(ReportAddedToIncident e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            var settings = await _notificationsRepository.GetAllAsync(e.Incident.ApplicationId);

            foreach (var setting in settings)
            {
                if (setting.NewIncident != NotificationState.Disabled && e.Incident.ReportCount == 1)
                {
                    await CreateNotification(e, setting.AccountId, setting.NewIncident);
                }
                else if (setting.NewReport != NotificationState.Disabled)
                {
                    await CreateNotification(e, setting.AccountId, setting.NewReport);
                }
                else if (setting.ReopenedIncident != NotificationState.Disabled && e.IsReOpened)
                {
                    await CreateNotification(e, setting.AccountId, setting.ReopenedIncident);
                }
            }
        }
        /// <inheritdoc/>
        public async Task HandleAsync(FeedbackAttachedToIncident e)
        {
            var settings = await _notificationsRepository.GetAllAsync(-1);

            var incident = await _queryBus.QueryAsync(new GetIncident(e.IncidentId));

            foreach (var setting in settings)
            {
                if (setting.UserFeedback == NotificationState.Disabled)
                {
                    continue;
                }

                var notificationEmail = await _queryBus.QueryAsync(new GetAccountEmailById(setting.AccountId));

                var config = ConfigurationStore.Instance.Load <BaseConfiguration>();

                var shortName = incident.Description.Length > 40
                    ? incident.Description.Substring(0, 40) + "..."
                    : incident.Description;

                if (string.IsNullOrEmpty(e.UserEmailAddress))
                {
                    e.UserEmailAddress = "unknown";
                }

                var incidentUrl = string.Format("{0}/#/application/{1}/incident/{2}",
                                                config.BaseUrl.ToString().TrimEnd('/'),
                                                incident.ApplicationId,
                                                incident.Id);

                //TODO: Add more information
                var msg = new EmailMessage(notificationEmail);
                msg.Subject  = "New feedback: " + shortName;
                msg.TextBody = string.Format(@"Incident: {0}
Feedback: {0}/feedback
From: {1}

{2}
", incidentUrl, e.UserEmailAddress, e.Message);


                var emailCmd = new SendEmail(msg);
                await _commandBus.ExecuteAsync(emailCmd);
            }
        }
Пример #3
0
        /// <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 config   = _configStore.Load <BaseConfiguration>();
            var url      = config.BaseUrl;
            var settings = await _repository.GetAllAsync(e.Report.ApplicationId);

            if (!settings.Any(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 settings)
            {
                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 = string.Format("Spike detected for {0} ({1} reports)",
                                            e.Incident.ApplicationName,
                                            todaysCount),
                    TextBody =
                        string.Format(
                            "We've detected a spike in incoming reports for application <a href=\"{0}/#/application/{1}\">{2}</a>\r\n" +
                            "\r\n" +
                            "We've received {3} reports so far. Day average is {4}\r\n" +
                            "\r\n" +
                            "No further spike emails will be sent today for that application.",
                            url,
                            e.Incident.ApplicationId,
                            e.Incident.ApplicationName, todaysCount.SpikeCount, todaysCount.DayAverage)
                };

                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);
            }
        }