public SearchResultItem(IList<string> keywords, string category, string counter, string instance)
        {
            Key = new CounterKey(category, counter, instance);

            var searchText = $"{category} > {counter} > {instance}";
            var keywordItems = keywords.Select(keyword => new KeywordItem(keyword, searchText)).ToArray();
            IsMatch = keywordItems.All(keywordItem => keywordItem.Index >= 0);
            IsValid = false;

            if (IsMatch)
            {
                string instanceName;
                if (instance != null)
                {
                    IsValid = true;
                    instanceName = instance;
                }
                else
                {
                    instanceName = "[i]No instance[/i]";
                }

                var displayTextBeforeHighlight = $"{category} > {counter} > {instanceName}";
                DisplayText = BuildDisplayText(displayTextBeforeHighlight, keywordItems.OrderBy(item => item.Index));
            }
            else
            {
                DisplayText = null;
            }
        }
Esempio n. 2
0
        public SearchResultItem(IList <string> keywords, string category, string counter, string instance)
        {
            Key = new CounterKey(category, counter, instance);

            var searchText   = $"{category} > {counter} > {instance}";
            var keywordItems = keywords.Select(keyword => new KeywordItem(keyword, searchText)).ToArray();

            IsMatch = keywordItems.All(keywordItem => keywordItem.Index >= 0);
            IsValid = false;

            if (IsMatch)
            {
                string instanceName;
                if (instance != null)
                {
                    IsValid      = true;
                    instanceName = instance;
                }
                else
                {
                    instanceName = "[i]No instance[/i]";
                }

                var displayTextBeforeHighlight = $"{category} > {counter} > {instanceName}";
                DisplayText = BuildDisplayText(displayTextBeforeHighlight, keywordItems.OrderBy(item => item.Index));
            }
            else
            {
                DisplayText = null;
            }
        }
Esempio n. 3
0
 public async Task CollectAsync(CounterKey key, CounterMap counters, CancellationToken ct = default)
 {
     if (key.AppId != null)
     {
         await collector.AddAsync(key.AppId, counters);
     }
 }
Esempio n. 4
0
        public CounterData(CounterKey key, Color color)
        {
            Key = key;
            Color = color;
            _previousSample = CounterSample.Empty;

            Id = Interlocked.Increment(ref _globalId);
        }
Esempio n. 5
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));
        }
Esempio n. 6
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));
        }
Esempio n. 7
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))));
        }
Esempio n. 8
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));
        }
Esempio n. 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));
        }
Esempio n. 10
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)));
        }
Esempio n. 11
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)));
        }
Esempio n. 12
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)));
        }
        public void Add(CounterKey key)
        {
            CounterData item;

            if (!_counters.TryGetItem(key, out item))
            {
                if (++_lastColorIndex >= VividColors.All.Count)
                {
                    _lastColorIndex = 0;
                }
                var counterData = new CounterData(key, VividColors.All[_lastColorIndex]);
                _counters.Add(counterData);
                List <CounterData> category;
                if (!_categories.TryGetValue(key.Category, out category))
                {
                    category = new List <CounterData>();
                    _categories.Add(key.Category, category);
                }
                category.Add(counterData);
                OnCounterAdded(counterData);
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Gets a performance counter handle for the specified type and instance.
        /// </summary>
        /// <param name="counterType">Counter definition type</param>
        /// <param name="instanceName">Name of the performance counter instance</param>
        /// <returns>Counter instance</returns>
        public static PerformanceCounterHandle GetCounter(Type counterType, string instanceName = null)
        {
            // --- Check arguments
            if (counterType == null)
            {
                throw new ArgumentNullException("counterType");
            }
            if (!counterType.IsSubclassOf(typeof(PmcDefinitionBase)))
            {
                throw new InvalidOperationException(
                          String.Format("{0} type is not derived from PmcDefinitionBase.", counterType));
            }

            // --- Try to obtain the instance from the cache
            var key = new CounterKey(counterType, instanceName);
            PerformanceCounterHandle handle;

            if (!s_CounterCache.TryGetValue(key, out handle))
            {
                handle = new PerformanceCounterHandle(counterType, instanceName);
                s_CounterCache[key] = handle;
            }
            return(handle);
        }
Esempio n. 15
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);
            }
        }
Esempio n. 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);
            }
        }
Esempio n. 17
0
 private Task StoreCountersAsync(CounterKey key, CounterMap counterValues)
 {
     return(counters.CollectAsync(key, counterValues));
 }
 public void Add(CounterKey key)
 {
     CounterData item;
     if (!_counters.TryGetItem(key, out item))
     {
         if (++_lastColorIndex >= VividColors.All.Count)
         {
             _lastColorIndex = 0;
         }
         var counterData = new CounterData(key, VividColors.All[_lastColorIndex]);
         _counters.Add(counterData);
         List<CounterData> category;
         if (!_categories.TryGetValue(key.Category, out category))
         {
             category = new List<CounterData>();
             _categories.Add(key.Category, category);
         }
         category.Add(counterData);
         OnCounterAdded(counterData);
     }
 }
Esempio n. 19
0
        public void PublishAdd(CounterKey key)
        {
            var handler = Added;

            handler?.Invoke(key);
        }
Esempio n. 20
0
 private Task StoreCountersAsync(CounterKey key, CounterMap counterValues,
                                 CancellationToken ct)
 {
     return(counters.CollectAsync(key, counterValues, ct));
 }
 public void PublishAdd(CounterKey key)
 {
     var handler = Added;
     handler?.Invoke(key);
 }