-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(workflow_engine): workflow and action serializers #83146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8d1a4e4
b71c3bc
e4b84b9
62e6620
a919ecd
387c3a9
f03f2fa
ef097d3
4d415e0
aa592e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,31 @@ | |
| from sentry.api.serializers import Serializer, register, serialize | ||
| from sentry.issues.grouptype import ErrorGroupType | ||
| from sentry.models.options.project_option import ProjectOption | ||
| from sentry.utils import json | ||
| from sentry.workflow_engine.models import ( | ||
| Action, | ||
| DataCondition, | ||
| DataConditionGroup, | ||
| DataSource, | ||
| DataSourceDetector, | ||
| Detector, | ||
| Workflow, | ||
| WorkflowDataConditionGroup, | ||
| ) | ||
| from sentry.workflow_engine.models.data_condition_group_action import DataConditionGroupAction | ||
| from sentry.workflow_engine.types import DataSourceTypeHandler | ||
|
|
||
|
|
||
| @register(Action) | ||
| class ActionSerializer(Serializer): | ||
| def serialize(self, obj: Action, *args, **kwargs): | ||
| return { | ||
| "id": str(obj.id), | ||
| "type": obj.type, | ||
| "data": json.dumps(obj.data), | ||
| } | ||
|
|
||
|
|
||
| @register(DataSource) | ||
| class DataSourceSerializer(Serializer): | ||
| def get_attrs( | ||
|
|
@@ -79,9 +94,20 @@ def get_attrs( | |
| for condition, serialized in zip(condition_list, serialize(condition_list, user=user)): | ||
| conditions[condition.condition_group_id].append(serialized) | ||
|
|
||
| dcga_list = list(DataConditionGroupAction.objects.filter(condition_group__in=item_list)) | ||
| actions = {dcga.action for dcga in dcga_list} | ||
|
|
||
| serialized_actions = { | ||
| action.id: serialized | ||
| for action, serialized in zip(actions, serialize(actions, user=user)) | ||
| } | ||
| action_map = defaultdict(list) | ||
| for dcga in dcga_list: | ||
| action_map[dcga.condition_group_id].append(serialized_actions[dcga.action_id]) | ||
|
|
||
| for item in item_list: | ||
| attrs[item]["conditions"] = conditions.get(item.id, []) | ||
|
|
||
| attrs[item]["actions"] = action_map.get(item.id, []) | ||
| return attrs | ||
|
|
||
| def serialize( | ||
|
|
@@ -92,6 +118,7 @@ def serialize( | |
| "organizationId": str(obj.organization_id), | ||
| "logicType": obj.logic_type, | ||
| "conditions": attrs.get("conditions"), | ||
| "actions": attrs.get("actions"), | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -167,3 +194,52 @@ def serialize(self, obj: Detector, attrs: Mapping[str, Any], user, **kwargs) -> | |
| "conditionGroup": attrs.get("condition_group"), | ||
| "config": attrs.get("config"), | ||
| } | ||
|
|
||
|
|
||
| @register(Workflow) | ||
| class WorkflowSerializer(Serializer): | ||
| def get_attrs(self, item_list, user, **kwargs): | ||
| attrs: MutableMapping[Workflow, dict[str, Any]] = defaultdict(dict) | ||
| trigger_conditions = list( | ||
| DataConditionGroup.objects.filter( | ||
| id__in=[w.when_condition_group_id for w in item_list if w.when_condition_group_id] | ||
| ) | ||
| ) | ||
| trigger_condition_map = { | ||
| group.id: serialized | ||
| for group, serialized in zip( | ||
| trigger_conditions, serialize(trigger_conditions, user=user) | ||
| ) | ||
| } | ||
|
|
||
| wdcg_list = list(WorkflowDataConditionGroup.objects.filter(workflow__in=item_list)) | ||
| condition_groups = {wdcg.condition_group for wdcg in wdcg_list} | ||
|
|
||
| serialized_condition_groups = { | ||
| dcg.id: serialized | ||
| for dcg, serialized in zip(condition_groups, serialize(condition_groups, user=user)) | ||
| } | ||
| dcg_map = defaultdict(list) | ||
| for wdcg in wdcg_list: | ||
| dcg_map[wdcg.workflow_id].append(serialized_condition_groups[wdcg.condition_group_id]) | ||
|
|
||
| for item in item_list: | ||
| attrs[item]["trigger_condition_group"] = trigger_condition_map.get( | ||
| item.when_condition_group_id | ||
| ) # when condition group | ||
| attrs[item]["data_condition_groups"] = dcg_map.get( | ||
| item.id, [] | ||
| ) # data condition groups associated with workflow via WorkflowDataConditionGroup lookup table | ||
| return attrs | ||
|
|
||
| def serialize(self, obj: Workflow, attrs: Mapping[str, Any], user, **kwargs) -> dict[str, Any]: | ||
| # WHAT TO DO ABOUT CONFIG? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good question 🤔 Cathy added enforcement of the schemas in https://github.com/getsentry/sentry/pull/81979/files and I was working on defining a schema for the detector config here that I need to get back to but we haven't made others yet afaik. Wanna bring it up in office hours tomorrow? |
||
| return { | ||
| "id": str(obj.id), | ||
| "organizationId": str(obj.organization_id), | ||
| "dateCreated": obj.date_added, | ||
| "dateUpdated": obj.date_updated, | ||
| "triggerConditionGroup": attrs.get("trigger_condition_group"), | ||
| "dataConditionGroups": attrs.get("data_condition_groups"), | ||
| "environment": obj.environment.name if obj.environment else None, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.