Пример #1
0
 public async Task CollectAsync(CounterKey key, CounterMap counters, CancellationToken ct = default)
 {
     if (key.AppId != null)
     {
         await collector.AddAsync(key.AppId, counters);
     }
 }
Пример #2
0
        public static CounterMap ForChannel(string channel, ProcessStatus status, int count = 1)
        {
            var result = new CounterMap();

            switch (status)
            {
            case ProcessStatus.Attempt when SupportPending:
                result.Increment(ChannelAttmept(channel), count);
                break;

            case ProcessStatus.Skipped:
                result.Increment(ChannelSkipped(channel), count);
                break;

            case ProcessStatus.Handled:
                result.Increment(ChannelHandled(channel), count);
                break;

            case ProcessStatus.Failed:
                result.Increment(ChannelFailed(channel), count);
                break;
            }

            return(result);
        }
Пример #3
0
        public Task TrackFailedAsync(UserEventMessage userEvent, CancellationToken ct)
        {
            Guard.NotNull(userEvent, nameof(userEvent));

            var counterMap = CounterMap.ForNotification(ProcessStatus.Failed);
            var counterKey = CounterKey.ForUserEvent(userEvent);

            return(StoreCountersAsync(counterKey, counterMap));
        }
Пример #4
0
        private async Task DeliverAsync(App?app, bool invalidate = false)
        {
            CounterMap.Cleanup(app?.Counters);

            if (app != null)
            {
                await cache.AddAsync(app.Id, app, CacheDuration, invalidate);
            }
        }
Пример #5
0
        public Task CollectAsync(CounterKey key, CounterMap counters,
                                 CancellationToken ct = default)
        {
            if (counters.Count == 0)
            {
                return(Task.CompletedTask);
            }

            return(Task.WhenAll(targets.Select(x => x.CollectAsync(key, counters, ct))));
        }
Пример #6
0
        public Task CollectAsync(IUserNotification notification, string channel, ProcessStatus status)
        {
            Guard.NotNull(notification, nameof(notification));
            Guard.NotNullOrEmpty(channel, nameof(channel));

            var counterMap = CounterMap.ForChannel(channel, status);
            var counterKey = CounterKey.ForNotification(notification);

            return(StoreCountersAsync(counterKey, counterMap));
        }
Пример #7
0
        public Task TrackAttemptAsync(UserEventMessage userEvent,
                                      CancellationToken ct = default)
        {
            Guard.NotNull(userEvent);

            var counterMap = CounterMap.ForNotification(ProcessStatus.Attempt);
            var counterKey = CounterKey.ForUserEvent(userEvent);

            return(StoreCountersAsync(counterKey, counterMap, ct));
        }
Пример #8
0
        public Task InsertAsync(UserNotification notification, CancellationToken ct)
        {
            Guard.NotNull(notification, nameof(notification));

            var counterMap = CounterMap.ForNotification(ProcessStatus.Handled, 1);
            var counterKey = CounterKey.ForNotification(notification);

            return(Task.WhenAll(
                       StoreCountersAsync(counterKey, counterMap),
                       StoreInternalAsync(notification, ct)));
        }
Пример #9
0
        public Task CollectAsync(IUserNotification notification, string channel, ProcessStatus status,
                                 CancellationToken ct = default)
        {
            Guard.NotNull(notification);
            Guard.NotNullOrEmpty(channel);

            var counterMap = CounterMap.ForChannel(channel, status);
            var counterKey = CounterKey.ForNotification(notification);

            return(StoreCountersAsync(counterKey, counterMap, ct));
        }
Пример #10
0
        public Task CollectAndUpdateAsync(IUserNotification notification, string channel, ProcessStatus status, string?detail)
        {
            Guard.NotNull(notification, nameof(notification));
            Guard.NotNullOrEmpty(channel, nameof(channel));

            var counterMap = CounterMap.ForChannel(channel, status);
            var counterKey = CounterKey.ForNotification(notification);

            return(Task.WhenAll(
                       StoreCountersAsync(counterKey, counterMap),
                       StoreInternalAsync(notification.Id, channel, status, detail)));
        }
Пример #11
0
        public Task CollectAndUpdateAsync(IUserNotification notification, string channel, string configuration, ProcessStatus status, string?detail = null,
                                          CancellationToken ct = default)
        {
            Guard.NotNull(notification);
            Guard.NotNullOrEmpty(channel);

            var counterMap = CounterMap.ForChannel(channel, status);
            var counterKey = CounterKey.ForNotification(notification);

            return(Task.WhenAll(
                       StoreCountersAsync(counterKey, counterMap, ct),
                       StoreInternalAsync(notification.Id, channel, configuration, status, detail)));
        }
Пример #12
0
        public static CounterMap ForNotification(ProcessStatus status, int count = 1)
        {
            var result = new CounterMap();

            switch (status)
            {
            case ProcessStatus.Attempt when SupportPending:
                result.Increment(NotificationsAttempt, count);
                break;

            case ProcessStatus.Handled:
                result.Increment(NotificationsHandled, count);
                break;

            case ProcessStatus.Failed:
                result.Increment(NotificationsFailed, count);
                break;
            }

            return(result);
        }
Пример #13
0
        public async Task PublishAsync(EventMessage message, CancellationToken ct = default)
        {
            if (string.IsNullOrWhiteSpace(message.AppId))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(message.Topic))
            {
                await logStore.LogAsync(message.AppId, Texts.Events_NoTopic, ct);

                return;
            }

            if (string.IsNullOrWhiteSpace(message.TemplateCode) && message.Formatting?.HasSubject() != true)
            {
                await logStore.LogAsync(message.AppId, Texts.Events_NoSubjectOrTemplateCode, ct);

                return;
            }

            var count = 0;

            await foreach (var subscription in GetSubscriptions(message).WithCancellation(ct))
            {
                if (count == 0)
                {
                    if (!string.IsNullOrWhiteSpace(message.TemplateCode))
                    {
                        var template = await templateStore.GetAsync(message.AppId, message.TemplateCode, ct);

                        if (template != null && !template.IsAutoCreated)
                        {
                            message.Formatting = template.Formatting;

                            if (template.Settings != null && template.Settings.Count > 0)
                            {
                                var settings = new NotificationSettings();

                                settings.OverrideBy(template.Settings);
                                settings.OverrideBy(message.Settings);

                                message.Settings = settings;
                            }
                        }
                    }

                    if (message.Formatting?.HasSubject() != true)
                    {
                        await logStore.LogAsync(message.AppId, string.Format(Texts.Template_NoSubject, message.TemplateCode), ct);

                        return;
                    }

                    message.Formatting = message.Formatting.Format(message.Properties);

                    try
                    {
                        await eventStore.InsertAsync(message, ct);
                    }
                    catch (UniqueConstraintException)
                    {
                        await logStore.LogAsync(message.AppId, Texts.Events_AlreadyProcessed, ct);

                        break;
                    }
                }

                var userEventMessage = CreateUserEventMessage(message, subscription);

                await userEventProducer.ProduceAsync(subscription.UserId, userEventMessage);

                count++;
            }

            if (count > 0)
            {
                var counterMap = CounterMap.ForNotification(ProcessStatus.Attempt, count);
                var counterKey = CounterKey.ForEvent(message);

                await counters.CollectAsync(counterKey, counterMap, ct);
            }
            else
            {
                await logStore.LogAsync(message.AppId, Texts.Events_NoSubscriber, ct);
            }
        }
Пример #14
0
 private Task StoreCountersAsync(CounterKey key, CounterMap counterValues,
                                 CancellationToken ct)
 {
     return(counters.CollectAsync(key, counterValues, ct));
 }
Пример #15
0
 public CounterMap(CounterMap source)
     : base(source)
 {
 }
Пример #16
0
        public async Task PublishAsync(EventMessage @event,
                                       CancellationToken ct)
        {
            using (var activity = Telemetry.Activities.StartActivity("HandleUserEvent"))
            {
                log.LogInformation("Received event for app {appId} with ID {id} to topic {topic}.",
                                   @event.AppId,
                                   @event.Id,
                                   @event.Topic);

                if (string.IsNullOrWhiteSpace(@event.AppId))
                {
                    log.LogInformation("Received invalid event with ID {id} to topic {topic}: No app id found.",
                                       @event.Id,
                                       @event.Topic);
                    return;
                }

                if (string.IsNullOrWhiteSpace(@event.Topic))
                {
                    await logStore.LogAsync(@event.AppId, Texts.Events_NoTopic);

                    return;
                }

                if (string.IsNullOrWhiteSpace(@event.TemplateCode) && @event.Formatting?.HasSubject() != true)
                {
                    await logStore.LogAsync(@event.AppId, Texts.Events_NoSubjectOrTemplateCode);

                    return;
                }

                var count = 0;

                await foreach (var subscription in GetSubscriptions(@event, ct))
                {
                    ct.ThrowIfCancellationRequested();

                    if (count == 0)
                    {
                        var templateCode = (string?)null;

                        if (@event.TemplateVariants?.Count > 0)
                        {
                            var random = randomizer.NextDouble();

                            var propability = 0d;

                            foreach (var(key, value) in @event.TemplateVariants)
                            {
                                propability += value;

                                if (random <= propability)
                                {
                                    templateCode = key;
                                    break;
                                }
                            }
                        }

                        if (string.IsNullOrWhiteSpace(templateCode))
                        {
                            templateCode = @event.TemplateCode;
                        }

                        if (!string.IsNullOrWhiteSpace(templateCode))
                        {
                            var template = await templateStore.GetAsync(@event.AppId, templateCode, ct);

                            if (template?.IsAutoCreated == false)
                            {
                                if (@event.Formatting != null)
                                {
                                    @event.Formatting = template.Formatting.MergedWith(@event.Formatting);
                                }
                                else
                                {
                                    @event.Formatting = template.Formatting;
                                }

                                @event.Settings = ChannelSettings.Merged(template.Settings, @event.Settings);
                            }
                        }

                        if (@event.Formatting?.HasSubject() != true)
                        {
                            await logStore.LogAsync(@event.AppId, string.Format(CultureInfo.InvariantCulture, Texts.Template_NoSubject, templateCode));

                            return;
                        }

                        if (@event.Properties != null)
                        {
                            @event.Formatting = @event.Formatting.Format(@event.Properties);
                        }

                        try
                        {
                            await eventStore.InsertAsync(@event, ct);
                        }
                        catch (UniqueConstraintException)
                        {
                            await logStore.LogAsync(@event.AppId, Texts.Events_AlreadyProcessed);

                            break;
                        }
                    }

                    var userEventMessage = CreateUserEventMessage(@event, subscription);

                    if (activity != null)
                    {
                        userEventMessage.UserEventActivity = activity.Context;
                    }

                    await ProduceAsync(subscription, userEventMessage);

                    count++;
                }

                if (count > 0)
                {
                    var counterMap = CounterMap.ForNotification(ProcessStatus.Attempt, count);
                    var counterKey = CounterKey.ForEvent(@event);

                    await counters.CollectAsync(counterKey, counterMap, ct);
                }
                else
                {
                    await logStore.LogAsync(@event.AppId, Texts.Events_NoSubscriber);
                }

                log.LogInformation("Processed event for app {appId} with ID {id} to topic {topic}.",
                                   @event.AppId,
                                   @event.Id,
                                   @event.Topic);
            }
        }
Пример #17
0
 private Task StoreCountersAsync(CounterKey key, CounterMap counterValues)
 {
     return(counters.CollectAsync(key, counterValues));
 }