|
1 | | -from typing import Any, Optional |
| 1 | +from collections import defaultdict |
| 2 | +from typing import TYPE_CHECKING, Any, Optional |
2 | 3 |
|
| 4 | +from prefect.client.orchestration import get_client |
| 5 | +from prefect.client.schemas.filters import ( |
| 6 | + FlowRunFilter, |
| 7 | + FlowRunFilterTags, |
| 8 | + LogFilter, |
| 9 | + LogFilterFlowRunId, |
| 10 | +) |
| 11 | +from prefect.client.schemas.sorting import ( |
| 12 | + FlowRunSort, |
| 13 | +) |
3 | 14 | from pydantic import ConfigDict, Field |
4 | 15 |
|
5 | 16 | from infrahub.core.constants import TaskConclusion |
|
10 | 21 | from infrahub.core.timestamp import current_timestamp |
11 | 22 | from infrahub.database import InfrahubDatabase |
12 | 23 | from infrahub.utils import get_nested_dict |
| 24 | +from infrahub.workflows.constants import TAG_NAMESPACE, WorkflowTag |
13 | 25 |
|
14 | 26 | from .task_log import TaskLog |
15 | 27 |
|
| 28 | +if TYPE_CHECKING: |
| 29 | + from prefect.client.schemas.objects import Log as PrefectLog |
| 30 | + |
| 31 | +LOG_LEVEL_MAPPING = {10: "debug", 20: "info", 30: "warning", 40: "error", 50: "critical"} |
| 32 | + |
16 | 33 |
|
17 | 34 | class Task(StandardNode): |
18 | 35 | model_config = ConfigDict(arbitrary_types_allowed=True) |
@@ -91,3 +108,83 @@ async def query( |
91 | 108 | ) |
92 | 109 |
|
93 | 110 | return {"count": count, "edges": nodes} |
| 111 | + |
| 112 | + |
| 113 | +class NewTask: |
| 114 | + @classmethod |
| 115 | + async def query( |
| 116 | + cls, |
| 117 | + fields: dict[str, Any], |
| 118 | + related_nodes: list[str], |
| 119 | + branch: str | None = None, |
| 120 | + limit: int | None = None, |
| 121 | + offset: int = 0, |
| 122 | + ) -> dict[str, Any]: |
| 123 | + nodes: list[dict] = [] |
| 124 | + count = None |
| 125 | + |
| 126 | + log_fields = get_nested_dict(nested_dict=fields, keys=["edges", "node", "logs", "edges", "node"]) |
| 127 | + logs_flow: dict[str, list[PrefectLog]] = defaultdict(list) |
| 128 | + |
| 129 | + async with get_client(sync_client=False) as client: |
| 130 | + tags = [TAG_NAMESPACE] |
| 131 | + |
| 132 | + if branch: |
| 133 | + tags.append(WorkflowTag.BRANCH.render(identifier=branch)) |
| 134 | + |
| 135 | + # We only support one related node for now, need to investigate HOW (and IF) we can support more |
| 136 | + if related_nodes: |
| 137 | + tags.append(WorkflowTag.RELATED_NODE.render(identifier=related_nodes[0])) |
| 138 | + |
| 139 | + flow_run_filters = FlowRunFilter( |
| 140 | + tags=FlowRunFilterTags(all_=tags), |
| 141 | + ) |
| 142 | + |
| 143 | + flows = await client.read_flow_runs( |
| 144 | + flow_run_filter=flow_run_filters, |
| 145 | + limit=limit, |
| 146 | + offset=offset, |
| 147 | + sort=FlowRunSort.START_TIME_DESC, |
| 148 | + ) |
| 149 | + |
| 150 | + # For now count will just return the number of objects in the response |
| 151 | + # it won't work well with pagination but it doesn't look like Prefect provide a good option to count all flows |
| 152 | + if "count" in fields: |
| 153 | + count = len(flows) |
| 154 | + |
| 155 | + if log_fields: |
| 156 | + flow_ids = [flow.id for flow in flows] |
| 157 | + all_logs = await client.read_logs(log_filter=LogFilter(flow_run_id=LogFilterFlowRunId(any_=flow_ids))) |
| 158 | + for log in all_logs: |
| 159 | + logs_flow[log.flow_run_id].append(log) |
| 160 | + |
| 161 | + for flow in flows: |
| 162 | + logs = [] |
| 163 | + if log_fields: |
| 164 | + logs = [ |
| 165 | + { |
| 166 | + "node": { |
| 167 | + "message": log.message, |
| 168 | + "severity": LOG_LEVEL_MAPPING.get(log.level, "error"), |
| 169 | + "timestamp": log.timestamp.to_iso8601_string(), |
| 170 | + } |
| 171 | + } |
| 172 | + for log in logs_flow[flow.id] |
| 173 | + ] |
| 174 | + |
| 175 | + nodes.append( |
| 176 | + { |
| 177 | + "node": { |
| 178 | + "title": flow.name, |
| 179 | + "conclusion": flow.state_name, |
| 180 | + "related_node": "", |
| 181 | + "related_node_kind": "", |
| 182 | + "created_at": flow.created.to_iso8601_string(), |
| 183 | + "updated_at": flow.updated.to_iso8601_string(), |
| 184 | + "id": flow.id, |
| 185 | + "logs": {"edges": logs}, |
| 186 | + } |
| 187 | + } |
| 188 | + ) |
| 189 | + |
| 190 | + return {"count": count or 0, "edges": nodes} |
0 commit comments