-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(aci): WorkflowGroupHistory endpoint #89948
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
Open
cathteng
wants to merge
4
commits into
master
Choose a base branch
from
cathy/aci/workflow-group-history-endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+291
−14
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
saponifi3d
reviewed
Apr 21, 2025
Comment on lines
+89
to
+115
class WorkflowFireHistoryResponse(TypedDict): | ||
group: BaseGroupSerializerResponse | ||
count: int | ||
lastTriggered: datetime | ||
eventId: str | ||
|
||
|
||
class WorkflowGroupHistorySerializer(Serializer): | ||
def get_attrs( | ||
self, item_list: Sequence[WorkflowFireHistory], user: Any, **kwargs: Any | ||
) -> MutableMapping[Any, Any]: | ||
serialized_groups = { | ||
g["id"]: g for g in serialize([item.group for item in item_list], user) | ||
} | ||
return { | ||
history: {"group": serialized_groups[str(history.group.id)]} for history in item_list | ||
} | ||
|
||
def serialize( | ||
self, obj: WorkflowGroupHistory, attrs: Mapping[Any, Any], user: Any, **kwargs: Any | ||
) -> WorkflowFireHistoryResponse: | ||
return { | ||
"group": attrs["group"], | ||
"count": obj.count, | ||
"lastTriggered": obj.last_triggered, | ||
"eventId": obj.event_id, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should all this code live in the workflow_engine/endpoints/serializers.py
file?
Comment on lines
+33
to
+86
@dataclass(frozen=True) | ||
class WorkflowGroupHistory: | ||
group: Group | ||
count: int | ||
last_triggered: datetime | ||
event_id: str | ||
|
||
|
||
class _Result(TypedDict): | ||
group: int | ||
count: int | ||
last_triggered: datetime | ||
event_id: str | ||
|
||
|
||
def convert_results(results: Sequence[_Result]) -> Sequence[WorkflowGroupHistory]: | ||
group_lookup = {g.id: g for g in Group.objects.filter(id__in=[r["group"] for r in results])} | ||
return [ | ||
WorkflowGroupHistory( | ||
group_lookup[r["group"]], r["count"], r["last_triggered"], r["event_id"] | ||
) | ||
for r in results | ||
] | ||
|
||
|
||
def fetch_workflow_groups_paginated( | ||
workflow: Workflow, | ||
start: datetime, | ||
end: datetime, | ||
cursor: Cursor | None = None, | ||
per_page: int = 25, | ||
) -> CursorResult[Group]: | ||
filtered_history = WorkflowFireHistory.objects.filter( | ||
workflow=workflow, | ||
date_added__gte=start, | ||
date_added__lt=end, | ||
) | ||
|
||
# subquery that retrieves row with the largest date in a group | ||
group_max_dates = filtered_history.filter(group=OuterRef("group")).order_by("-date_added")[:1] | ||
qs = ( | ||
filtered_history.select_related("group") | ||
.values("group") | ||
.annotate(count=Count("group")) | ||
.annotate(event_id=Subquery(group_max_dates.values("event_id"))) | ||
.annotate(last_triggered=Max("date_added")) | ||
) | ||
|
||
return cast( | ||
CursorResult[Group], | ||
OffsetPaginator( | ||
qs, order_by=("-count", "-last_triggered"), on_results=convert_results | ||
).get_result(per_page, cursor), | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this live in /processors/workflow_group_history.py
? #newfile
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The workflow engine equivalent of
ProjectRuleGroupHistory
endpoint (the logic is basically the same too) -- which fetches and serialized theGroups
that have been triggered for aWorkflow
, along with when they were last triggered, how many times they were triggered in the queried period, and the lastevent_id
they triggered with.