Search in sources :

Example 1 with MultipleConditionWindow

use of io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow in project kestra by kestra-io.

the class MultipleCondition method test.

/**
 * This conditions will only validate previously calculated value on
 * {@link FlowService#multipleFlowTrigger(Stream, Flow, Execution, MultipleConditionStorageInterface)}} and save on {@link MultipleConditionStorageInterface}
 * by the executor.
 * The real validation is done here.
 */
@Override
public boolean test(ConditionContext conditionContext) throws InternalException {
    Logger logger = conditionContext.getRunContext().logger();
    MultipleConditionStorageInterface multipleConditionStorage = conditionContext.getMultipleConditionStorage();
    Objects.requireNonNull(multipleConditionStorage);
    Optional<MultipleConditionWindow> triggerExecutionWindow = multipleConditionStorage.get(conditionContext.getFlow(), this.getId());
    Map<String, Boolean> results = conditions.keySet().stream().map(condition -> new AbstractMap.SimpleEntry<>(condition, (triggerExecutionWindow.isPresent() && triggerExecutionWindow.get().getResults() != null && triggerExecutionWindow.get().getResults().containsKey(condition) && triggerExecutionWindow.get().getResults().get(condition)))).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    long validatedCount = results.entrySet().stream().filter(Map.Entry::getValue).count();
    boolean result = conditions.size() == validatedCount;
    if (result && logger.isDebugEnabled()) {
        logger.debug("[namespace: {}] [flow: {}] Multiple conditions validated !", conditionContext.getFlow().getNamespace(), conditionContext.getFlow().getId());
    } else if (logger.isTraceEnabled()) {
        logger.trace("[namespace: {}] [flow: {}] Multiple conditions failed ({}/{}) with '{}'", conditionContext.getFlow().getNamespace(), conditionContext.getFlow().getId(), validatedCount, conditions.size(), results);
    }
    return result;
}
Also used : NotBlank(javax.validation.constraints.NotBlank) Getter(lombok.Getter) Plugin(io.kestra.core.models.annotations.Plugin) MultipleConditionStorageInterface(io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface) ConditionContext(io.kestra.core.models.conditions.ConditionContext) MultipleConditionWindow(io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow) NotEmpty(javax.validation.constraints.NotEmpty) FlowService(io.kestra.core.services.FlowService) Duration(java.time.Duration) Map(java.util.Map) ToString(lombok.ToString) Schema(io.swagger.v3.oas.annotations.media.Schema) Logger(org.slf4j.Logger) SuperBuilder(lombok.experimental.SuperBuilder) Condition(io.kestra.core.models.conditions.Condition) EqualsAndHashCode(lombok.EqualsAndHashCode) NotNull(javax.validation.constraints.NotNull) Execution(io.kestra.core.models.executions.Execution) Collectors(java.util.stream.Collectors) InternalException(io.kestra.core.exceptions.InternalException) PluginProperty(io.kestra.core.models.annotations.PluginProperty) Objects(java.util.Objects) AbstractMap(java.util.AbstractMap) Stream(java.util.stream.Stream) Pattern(javax.validation.constraints.Pattern) Example(io.kestra.core.models.annotations.Example) Optional(java.util.Optional) Flow(io.kestra.core.models.flows.Flow) NoArgsConstructor(lombok.NoArgsConstructor) MultipleConditionStorageInterface(io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface) MultipleConditionWindow(io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow) ToString(lombok.ToString) Logger(org.slf4j.Logger) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Aggregations

InternalException (io.kestra.core.exceptions.InternalException)1 Example (io.kestra.core.models.annotations.Example)1 Plugin (io.kestra.core.models.annotations.Plugin)1 PluginProperty (io.kestra.core.models.annotations.PluginProperty)1 Condition (io.kestra.core.models.conditions.Condition)1 ConditionContext (io.kestra.core.models.conditions.ConditionContext)1 Execution (io.kestra.core.models.executions.Execution)1 Flow (io.kestra.core.models.flows.Flow)1 MultipleConditionStorageInterface (io.kestra.core.models.triggers.multipleflows.MultipleConditionStorageInterface)1 MultipleConditionWindow (io.kestra.core.models.triggers.multipleflows.MultipleConditionWindow)1 FlowService (io.kestra.core.services.FlowService)1 Schema (io.swagger.v3.oas.annotations.media.Schema)1 Duration (java.time.Duration)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 NotBlank (javax.validation.constraints.NotBlank)1