Skip to content

Commit 156c4c4

Browse files
save variable conditions
1 parent ce59697 commit 156c4c4

File tree

8 files changed

+103
-26
lines changed

8 files changed

+103
-26
lines changed

executor/playbook_result_conditional_evaluators/step_condition_evaluator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ def evaluate(self, condition: PlaybookStepResultCondition,
7272
all_evaluations.append(evaluation)
7373

7474
variable_rules: [PlaybookTaskResultRule] = rs.variable_rules
75+
print("WOOOOOOO variable_rules", variable_rules)
7576
for vr in variable_rules:
76-
variable_evaluator = self._variable_rule_map.get()
77+
print("WOOOOOOO", vr)
78+
variable_evaluator = self._variable_rule_map.get(vr.type)
7779
print("WOOOOOOO 0", variable_evaluator, vr)
7880
global_variable_set = next(
7981
(tr.execution_global_variable_set for tr in playbook_task_execution_log), None)

protos/playbooks/playbook_global_variable_evaluator.proto

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@ package protos.playbooks;
44
import "protos/base.proto";
55
import "google/protobuf/wrappers.proto";
66

7-
message GlobalVariableResultRule {
7+
message CompareGlobalVariable {
88
google.protobuf.StringValue variable_name = 1;
99
Operator operator = 2;
1010
google.protobuf.StringValue threshold = 3;
1111
}
12+
13+
message GlobalVariableResultRule {
14+
enum Type {
15+
UNKOWN = 0;
16+
COMPARE_GLOBAL_VARIABLE = 1;
17+
}
18+
19+
Type type = 1;
20+
oneof rule {
21+
CompareGlobalVariable compare_global_variable = 101;
22+
}
23+
}

protos/playbooks/playbook_global_variable_evaluator_pb2.py

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/playbooks/playbook_global_variable_evaluator_pb2.pyi

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ isort:skip_file
44
"""
55
import builtins
66
import google.protobuf.descriptor
7+
import google.protobuf.internal.enum_type_wrapper
78
import google.protobuf.message
89
import google.protobuf.wrappers_pb2
910
import protos.base_pb2
1011
import sys
12+
import typing
1113

12-
if sys.version_info >= (3, 8):
14+
if sys.version_info >= (3, 10):
1315
import typing as typing_extensions
1416
else:
1517
import typing_extensions
1618

1719
DESCRIPTOR: google.protobuf.descriptor.FileDescriptor
1820

1921
@typing_extensions.final
20-
class GlobalVariableResultRule(google.protobuf.message.Message):
22+
class CompareGlobalVariable(google.protobuf.message.Message):
2123
DESCRIPTOR: google.protobuf.descriptor.Descriptor
2224

2325
VARIABLE_NAME_FIELD_NUMBER: builtins.int
@@ -38,4 +40,38 @@ class GlobalVariableResultRule(google.protobuf.message.Message):
3840
def HasField(self, field_name: typing_extensions.Literal["threshold", b"threshold", "variable_name", b"variable_name"]) -> builtins.bool: ...
3941
def ClearField(self, field_name: typing_extensions.Literal["operator", b"operator", "threshold", b"threshold", "variable_name", b"variable_name"]) -> None: ...
4042

43+
global___CompareGlobalVariable = CompareGlobalVariable
44+
45+
@typing_extensions.final
46+
class GlobalVariableResultRule(google.protobuf.message.Message):
47+
DESCRIPTOR: google.protobuf.descriptor.Descriptor
48+
49+
class _Type:
50+
ValueType = typing.NewType("ValueType", builtins.int)
51+
V: typing_extensions.TypeAlias = ValueType
52+
53+
class _TypeEnumTypeWrapper(google.protobuf.internal.enum_type_wrapper._EnumTypeWrapper[GlobalVariableResultRule._Type.ValueType], builtins.type): # noqa: F821
54+
DESCRIPTOR: google.protobuf.descriptor.EnumDescriptor
55+
UNKOWN: GlobalVariableResultRule._Type.ValueType # 0
56+
COMPARE_GLOBAL_VARIABLE: GlobalVariableResultRule._Type.ValueType # 1
57+
58+
class Type(_Type, metaclass=_TypeEnumTypeWrapper): ...
59+
UNKOWN: GlobalVariableResultRule.Type.ValueType # 0
60+
COMPARE_GLOBAL_VARIABLE: GlobalVariableResultRule.Type.ValueType # 1
61+
62+
TYPE_FIELD_NUMBER: builtins.int
63+
COMPARE_GLOBAL_VARIABLE_FIELD_NUMBER: builtins.int
64+
type: global___GlobalVariableResultRule.Type.ValueType
65+
@property
66+
def compare_global_variable(self) -> global___CompareGlobalVariable: ...
67+
def __init__(
68+
self,
69+
*,
70+
type: global___GlobalVariableResultRule.Type.ValueType = ...,
71+
compare_global_variable: global___CompareGlobalVariable | None = ...,
72+
) -> None: ...
73+
def HasField(self, field_name: typing_extensions.Literal["compare_global_variable", b"compare_global_variable", "rule", b"rule"]) -> builtins.bool: ...
74+
def ClearField(self, field_name: typing_extensions.Literal["compare_global_variable", b"compare_global_variable", "rule", b"rule", "type", b"type"]) -> None: ...
75+
def WhichOneof(self, oneof_group: typing_extensions.Literal["rule", b"rule"]) -> typing_extensions.Literal["compare_global_variable"] | None: ...
76+
4177
global___GlobalVariableResultRule = GlobalVariableResultRule

web/src/components/AddVariableCondition/VariableCondition.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import CustomInput from "../Inputs/CustomInput";
2-
import { InputTypes, VariableConditionRule } from "../../types";
2+
import {
3+
InputTypes,
4+
VariableConditionRule,
5+
VariableRuleTypes,
6+
} from "../../types";
37
import useIsPrefetched from "../../hooks/playbooks/useIsPrefetched";
48
import { useSelector } from "react-redux";
59
import { currentPlaybookSelector } from "../../store/features/playbook/selectors";
@@ -24,6 +28,8 @@ function VariableCondition({
2428
const isPrefetched = useIsPrefetched();
2529
const currentPlaybook = useSelector(currentPlaybookSelector);
2630
const { handleRule } = useEdgeConditions(id);
31+
const ruleType = condition.type?.toLowerCase() as VariableRuleTypes;
32+
const ruleData = condition?.[ruleType] ?? {};
2733

2834
const handleChange = (val: string, type: string) => {
2935
handleRule(type, val, i, RuleType.VARIABLE_RULE);
@@ -45,9 +51,11 @@ function VariableCondition({
4551
id: key,
4652
label: key,
4753
}))}
48-
value={condition.variable_name}
54+
value={ruleData.variable_name}
4955
placeholder={`Variable Name`}
50-
handleChange={(id: string) => handleChange(id, "variable_name")}
56+
handleChange={(id: string) =>
57+
handleChange(id, `${ruleType}.variable_name`)
58+
}
5159
disabled={!!isPrefetched}
5260
/>
5361
</div>
@@ -56,21 +64,25 @@ function VariableCondition({
5664
inputType={InputTypes.DROPDOWN}
5765
disabled={!!isPrefetched}
5866
options={operationOptions}
59-
value={condition.operator}
67+
value={ruleData.operator}
6068
placeholder={`Select Operator`}
61-
handleChange={(id: string) => handleChange(id, `operator`)}
69+
handleChange={(id: string) =>
70+
handleChange(id, `${ruleType}.operator`)
71+
}
6272
/>
6373
<CustomInput
6474
inputType={InputTypes.TEXT}
6575
disabled={!!isPrefetched}
66-
value={condition.threshold}
76+
value={ruleData.threshold}
6777
placeholder={`Threshold/Value`}
68-
handleChange={(id: string) => handleChange(id, `threshold`)}
78+
handleChange={(id: string) =>
79+
handleChange(id, `${ruleType}.threshold`)
80+
}
6981
/>
7082
</div>
7183

7284
{showDelete && (
73-
<DeleteRuleButton ruleType={RuleType.RULE} ruleIndex={i} />
85+
<DeleteRuleButton ruleType={RuleType.VARIABLE_RULE} ruleIndex={i} />
7486
)}
7587
</div>
7688
</div>

web/src/store/features/playbook/slices/relations/addVariableRule.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PayloadAction } from "@reduxjs/toolkit";
2-
import { PlaybookUIState } from "../../../../../types";
2+
import { PlaybookUIState, VariableRuleTypes } from "../../../../../types";
33
import { OperatorOptions } from "../../../../../utils/conditionals/types/operatorOptionTypes";
44
import { playbookSlice } from "../../playbookSlice";
55

@@ -28,8 +28,11 @@ export const addVariableRuleAction = (
2828
const ruleSet = relation.condition.rule_sets[ruleSetIndex];
2929
if (!ruleSet.variable_rules) ruleSet.variable_rules = [];
3030
ruleSet.variable_rules.push({
31-
operator: OperatorOptions.EQUAL_O,
32-
variable_name: "",
33-
threshold: "",
31+
type: VariableRuleTypes.COMPARE_GLOBAL_VARIABLE,
32+
compare_global_variable: {
33+
operator: OperatorOptions.EQUAL_O,
34+
variable_name: "",
35+
threshold: "",
36+
},
3437
});
3538
};
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export type LowercaseString<T extends string> = T extends `${infer F}${infer R}`
2-
? `${Lowercase<F>}${R}`
3-
: T;
1+
export type LowercaseString<T extends string> = Lowercase<T>;

web/src/types/playbooks/stepRelations.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export enum StepRuleTypes {
1212
COMPARE_TIME_WITH_CRON = "COMPARE_TIME_WITH_CRON",
1313
}
1414

15+
export enum VariableRuleTypes {
16+
COMPARE_GLOBAL_VARIABLE = "COMPARE_GLOBAL_VARIABLE",
17+
}
18+
1519
enum RuleType {
1620
TIMESERIES = "timeseries",
1721
TABLE = "table",
@@ -37,12 +41,18 @@ export type ConditionRule = {
3741
[key in RuleType]?: any;
3842
};
3943

40-
export type VariableConditionRule = {
41-
variable_name: string;
42-
operator: OperatorOptionType;
43-
threshold: string;
44+
export type VariableConditionRuleType = {
45+
[key in VariableRuleTypes as LowercaseString<VariableRuleTypes>]: {
46+
variable_name: string;
47+
operator: OperatorOptionType;
48+
threshold: string;
49+
};
4450
};
4551

52+
export type VariableConditionRule = {
53+
type: VariableRuleTypes;
54+
} & VariableConditionRuleType;
55+
4656
export type StepRuleType = {
4757
[key in StepRuleTypes as LowercaseString<StepRuleTypes>]: {
4858
operator: OperatorOptionType;

0 commit comments

Comments
 (0)