예제 #1
0
        private async Task EnqueueFromSnapshotsAsync(IRuleEntity rule)
        {
            var errors = 0;

            await foreach (var(job, ex) in ruleService.CreateSnapshotJobsAsync(rule.RuleDef, rule.Id, rule.AppId.Id))
            {
                if (job != null)
                {
                    await ruleEventRepository.EnqueueAsync(job, ex);
                }
                else if (ex != null)
                {
                    errors++;

                    if (errors >= MaxErrors)
                    {
                        throw ex;
                    }

                    log.LogWarning(ex, w => w
                                   .WriteProperty("action", "runRule")
                                   .WriteProperty("status", "failedPartially"));
                }
            }
        }
예제 #2
0
        public async Task <IEnrichedRuleEntity> EnrichAsync(IRuleEntity rule, Context context)
        {
            Guard.NotNull(rule, nameof(rule));

            var enriched = await EnrichAsync(Enumerable.Repeat(rule, 1), context);

            return(enriched[0]);
        }
예제 #3
0
        public async Task <IEnrichedRuleEntity> EnrichAsync(IRuleEntity rule, Context context,
                                                            CancellationToken ct)
        {
            Guard.NotNull(rule);

            var enriched = await EnrichAsync(Enumerable.Repeat(rule, 1), context, ct);

            return(enriched[0]);
        }
 private static RuleContext GetContext(IRuleEntity rule)
 {
     return(new RuleContext
     {
         AppId = rule.AppId,
         Rule = rule.RuleDef,
         RuleId = rule.Id
     });
 }
        public static void UpdateModelStateWithViolations(this Controller current, IRuleEntity entity, ModelStateDictionary modelState)
        {
            List<RuleViolation> productIssues = entity.GetRuleViolations();

            foreach (var issue in productIssues)
            {
                modelState.AddModelError(issue.PropertyName,
                                          issue.ErrorMessage);
            }
        }
예제 #6
0
        public static void UpdateModelStateWithViolations(this Controller current, IRuleEntity entity, ModelStateDictionary modelState)
        {
            List <RuleViolation> productIssues = entity.GetRuleViolations();

            foreach (var issue in productIssues)
            {
                modelState.AddModelError(issue.PropertyName,
                                         issue.ErrorMessage);
            }
        }
예제 #7
0
        public static void UpdateModelStateWithRuleViolations(IRuleEntity entity,
            ModelStateDictionary modelState)
        {
            List<RuleViolation> issues = entity.GetRuleViolations();

            foreach (var issue in issues)
            {
                modelState.AddModelError(issue.PropertyName,

                    issue.ErrorMessage);
            }
        }
예제 #8
0
파일: RuleDto.cs 프로젝트: xixaoly/squidex
        public static RuleDto FromRule(IRuleEntity rule, ApiController controller, string app)
        {
            var result = new RuleDto();

            SimpleMapper.Map(rule, result);
            SimpleMapper.Map(rule.RuleDef, result);

            if (rule.RuleDef.Trigger != null)
            {
                result.Trigger = RuleTriggerDtoFactory.Create(rule.RuleDef.Trigger);
            }

            return(result.CreateLinks(controller, app));
        }
예제 #9
0
        public static RuleDto FromRule(IRuleEntity rule)
        {
            var response = new RuleDto();

            SimpleMapper.Map(rule, response);
            SimpleMapper.Map(rule.RuleDef, response);

            if (rule.RuleDef.Trigger != null)
            {
                response.Trigger = RuleTriggerDtoFactory.Create(rule.RuleDef.Trigger);
            }

            return(response);
        }
        public async Task <List <SimulatedRuleEvent> > SimulateAsync(IRuleEntity rule,
                                                                     CancellationToken ct = default)
        {
            Guard.NotNull(rule, nameof(rule));

            var context = new RuleContext
            {
                AppId          = rule.AppId,
                Rule           = rule.RuleDef,
                RuleId         = rule.Id,
                IncludeSkipped = true,
                IncludeStale   = true
            };

            var simulatedEvents = new List <SimulatedRuleEvent>(MaxSimulatedEvents);

            var fromNow = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromDays(7));

            await foreach (var storedEvent in eventStore.QueryAllReverseAsync($"^([a-zA-Z0-9]+)\\-{rule.AppId.Id}", fromNow, MaxSimulatedEvents, ct))
            {
                var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                if (@event?.Payload is AppEvent appEvent)
                {
                    // Also create jobs for rules with failing conditions because we want to show them in th table.
                    await foreach (var result in ruleService.CreateJobsAsync(@event, context, ct))
                    {
                        var eventName = result.Job?.EventName;

                        if (string.IsNullOrWhiteSpace(eventName))
                        {
                            eventName = ruleService.GetName(appEvent);
                        }

                        simulatedEvents.Add(new SimulatedRuleEvent
                        {
                            ActionData    = result.Job?.ActionData,
                            ActionName    = result.Job?.ActionName,
                            EnrichedEvent = result.EnrichedEvent,
                            Error         = result.EnrichmentError?.Message,
                            Event         = @event.Payload,
                            EventName     = eventName,
                            SkipReason    = result.SkipReason
                        });
                    }
                }
            }

            return(simulatedEvents);
        }
예제 #11
0
        public static RuleDto ToModel(this IRuleEntity entity)
        {
            var dto = new RuleDto();

            SimpleMapper.Map(entity, dto);
            SimpleMapper.Map(entity.RuleDef, dto);

            if (entity.RuleDef.Trigger != null)
            {
                dto.Trigger = RuleTriggerDtoFactory.Create(entity.RuleDef.Trigger);
            }

            if (entity.RuleDef.Action != null)
            {
                dto.Action = RuleActionDtoFactory.Create(entity.RuleDef.Action);
            }

            return(dto);
        }
예제 #12
0
        private async Task EnqueueFromEventsAsync(State currentState, IRuleEntity rule, CancellationToken ct)
        {
            var errors = 0;

            await eventStore.QueryAsync(async storedEvent =>
            {
                try
                {
                    var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                    if (@event != null)
                    {
                        var jobs = await ruleService.CreateJobsAsync(rule.RuleDef, rule.Id, @event, false);

                        foreach (var(job, ex) in jobs)
                        {
                            await ruleEventRepository.EnqueueAsync(job, ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    errors++;

                    if (errors >= MaxErrors)
                    {
                        throw;
                    }

                    log.LogWarning(ex, w => w
                                   .WriteProperty("action", "runRule")
                                   .WriteProperty("status", "failedPartially"));
                }
                finally
                {
                    currentState.Position = storedEvent.EventPosition;
                }

                await state.WriteAsync();
            }, $"^([a-z]+)\\-{Key}", currentState.Position, ct);
        }
예제 #13
0
        public static Task CanUpdate(UpdateRule command, IRuleEntity rule, IAppProvider appProvider)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(async e =>
            {
                if (command.Trigger != null)
                {
                    var errors = await RuleTriggerValidator.ValidateAsync(rule.AppId.Id, command.Trigger, appProvider);

                    errors.Foreach((x, _) => x.AddTo(e));
                }

                if (command.Action != null)
                {
                    var errors = command.Action.Validate();

                    errors.Foreach((x, _) => x.AddTo(e));
                }
            }));
        }
예제 #14
0
        public async Task <List <SimulatedRuleEvent> > SimulateAsync(IRuleEntity rule, CancellationToken ct)
        {
            Guard.NotNull(rule, nameof(rule));

            var context = GetContext(rule);

            var result = new List <SimulatedRuleEvent>(MaxSimulatedEvents);

            var fromNow = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromDays(7));

            await foreach (var storedEvent in eventStore.QueryAllReverseAsync($"^([a-z]+)\\-{rule.AppId.Id}", fromNow, MaxSimulatedEvents, ct))
            {
                var @event = eventDataFormatter.ParseIfKnown(storedEvent);

                if (@event?.Payload is AppEvent appEvent)
                {
                    await foreach (var(job, exception, skip) in ruleService.CreateJobsAsync(@event, context, ct))
                    {
                        var name = job?.EventName;

                        if (string.IsNullOrWhiteSpace(name))
                        {
                            name = ruleService.GetName(appEvent);
                        }

                        var simulationResult = new SimulatedRuleEvent(
                            name,
                            job?.ActionName,
                            job?.ActionData,
                            exception?.Message,
                            skip);

                        result.Add(simulationResult);
                    }
                }
            }

            return(result);
        }
        public bool CanRunFromSnapshots(IRuleEntity rule)
        {
            var context = GetContext(rule);

            return(CanRunRule(rule) && ruleService.CanCreateSnapshotEvents(context));
        }
        public bool CanRunRule(IRuleEntity rule)
        {
            var context = GetContext(rule);

            return(context.Rule.IsEnabled && context.Rule.Trigger is not ManualTrigger);
        }
예제 #17
0
 public bool CanRunFromSnapshots(IRuleEntity rule)
 {
     return(CanRunRule(rule) && ruleService.CanCreateSnapshotEvents(rule.RuleDef));
 }
예제 #18
0
 private static bool IsFound(IRuleEntity rule)
 {
     return(rule.Version > EtagVersion.Empty && !rule.IsDeleted);
 }
예제 #19
0
 public bool CanRunRule(IRuleEntity rule)
 {
     return(rule.RuleDef.IsEnabled && rule.RuleDef.Trigger is not ManualTrigger);
 }
예제 #20
0
 public Task <List <SimulatedRuleEvent> > SimulateAsync(IRuleEntity rule,
                                                        CancellationToken ct = default)
 {
     return(SimulateAsync(rule.AppId, rule.Id, rule.RuleDef, ct));
 }