Beispiel #1
0
        public async Task SaveActionLog(ActionLogDto actionLogDto)
        {
            if (actionLogDto?.Id == Guid.Empty)
            {
                actionLogDto.Id = Guid.NewGuid();
            }

            var actionLog = _mapper.Map <ActionLogDto, ActionLog>(actionLogDto);

            if (actionLog.LogLevel != LogLevel.Info)
            {
                var message = new InstanceNotificationMessage()
                {
                    InstanceId = actionLog.ClientId,
                    Text       = actionLog.Message,
                    CreatedAt  = actionLog.Timestamp,
                    Type       = (InstanceNotifyType)actionLog.LogLevel
                };

                // Send log to backend like notification
                await _serviceBusProvider.SendNotificationMessage(message);
            }

            await _repository.AddEntity(actionLog);
        }
Beispiel #2
0
        private async Task <MessageProcessResponse> OnNotifyProcessAsync(InstanceNotificationMessage arg, CancellationToken stoppingToken)
        {
            if (stoppingToken.IsCancellationRequested)
            {
                using (LogContext.PushProperty("ClassName", this.GetType().FullName))
                    using (LogContext.PushProperty("Source", this.GetType().Name))
                    {
                        _logger.LogError("Cancellation was requested, stopping token.");
                    }

                return(MessageProcessResponse.Abandon);
            }

            using (var scope = _scopeFactory.CreateScope())
            {
                var notificationService = scope.ServiceProvider.GetRequiredService <INotificationService>();

                var notificationRequest = new NotificationRequest
                {
                    Text       = arg.Text,
                    CreatedAt  = arg.CreatedAt,
                    InstanceId = arg.InstanceId
                };

                switch (arg.Type)
                {
                case InstanceNotifyType.Critical:
                    notificationRequest.Type = NotificationType.Error;
                    break;

                case InstanceNotifyType.Error:
                    notificationRequest.Type = NotificationType.Warning;
                    break;

                default:
                    notificationRequest.Type = NotificationType.Info;
                    break;
                }

                var result = await notificationService.CreateEntityAsync(notificationRequest);

                if (result == null)
                {
                    return(MessageProcessResponse.Abandon);
                }
            }

            _logger.LogInformation("Instance Notification Message was created.");

            return(MessageProcessResponse.Complete);
        }
Beispiel #3
0
 public Task SendNotificationMessage(InstanceNotificationMessage message)
 {
     return(_azureQueueSender.SendAsync(_instanceNotifyQueueClient, message));
 }
        public async Task Validate(CollectedDataDto collectedDataDto)
        {
            try
            {
                var instanceValidatorDto = await _instanceValidatorService
                                           .GetLastEntityByInstanceIdAsync(collectedDataDto.ClientId);

                var validator = new CollectedDataThresholdsValidator(instanceValidatorDto);

                var validatorParams = new List <string>();
                if (instanceValidatorDto.RamValidator)
                {
                    validatorParams.Add("RamUsage");
                }
                if (instanceValidatorDto.LocalDiskVallidator)
                {
                    validatorParams.Add("LocalDiskUsage");
                }
                if (instanceValidatorDto.CpuValidator)
                {
                    validatorParams.Add("CpuUsage");
                }

                var context = new ValidationContext <CollectedDataDto>(collectedDataDto,
                                                                       new PropertyChain(), new RulesetValidatorSelector(validatorParams.ToArray()));

                var validationResult = await validator.ValidateAsync(context);

                if (!validationResult.IsValid)
                {
                    StringBuilder textMessage = new StringBuilder();

                    foreach (var item in validationResult.Errors)
                    {
                        var name = item.FormattedMessagePlaceholderValues["PropertyName"]
                                   .ToString();

                        int index = name.IndexOf("Percentage");
                        name = (index < 0)
                            ? name
                            : name.Remove(index, "Percentage".Length);

                        index = name.IndexOf("Local");
                        name  = (index < 0)
                           ? name
                           : name.Remove(index, "Local".Length);

                        name = name.ToLower();

                        textMessage.Append(" " + name + " has reached " + item.AttemptedValue.ToString().Substring(0, 4) + "% ");
                    }
                    var message = new InstanceNotificationMessage()
                    {
                        InstanceId = instanceValidatorDto.ClientId,
                        CreatedAt  = DateTime.Now,
                        Type       = InstanceNotifyType.Error,
                        Text       = textMessage.ToString()
                    };
                    await _serviceBusProvider.SendNotificationMessage(message);
                }
            }
            catch (NotFoundException e)
            {
                Console.WriteLine(e);
            }
        }