Skip to content

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
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cathteng
Copy link
Member

The workflow engine equivalent of ProjectRuleGroupHistory endpoint (the logic is basically the same too) -- which fetches and serialized the Groups that have been triggered for a Workflow, along with when they were last triggered, how many times they were triggered in the queried period, and the last event_id they triggered with.

@cathteng cathteng requested a review from a team as a code owner April 18, 2025 20:51
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 18, 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,
}
Copy link
Contributor

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),
)
Copy link
Contributor

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
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants