Skip to content

Commit 8eaf560

Browse files
Fix Dashboard for queued DagRuns (#49961)
1 parent 0051ad6 commit 8eaf560

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

airflow-core/src/airflow/api_fastapi/core_api/routes/ui/dashboard.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def historical_metrics(
5858
dag_run_types = session.execute(
5959
select(DagRun.run_type, func.count(DagRun.run_id))
6060
.where(
61-
DagRun.start_date >= start_date,
61+
func.coalesce(DagRun.start_date, current_time) >= start_date,
6262
func.coalesce(DagRun.end_date, current_time) <= func.coalesce(end_date, current_time),
6363
)
6464
.group_by(DagRun.run_type)
@@ -67,7 +67,7 @@ def historical_metrics(
6767
dag_run_states = session.execute(
6868
select(DagRun.state, func.count(DagRun.run_id))
6969
.where(
70-
DagRun.start_date >= start_date,
70+
func.coalesce(DagRun.start_date, current_time) >= start_date,
7171
func.coalesce(DagRun.end_date, current_time) <= func.coalesce(end_date, current_time),
7272
)
7373
.group_by(DagRun.state)
@@ -78,7 +78,7 @@ def historical_metrics(
7878
select(TaskInstance.state, func.count(TaskInstance.run_id))
7979
.join(TaskInstance.dag_run)
8080
.where(
81-
DagRun.start_date >= start_date,
81+
func.coalesce(DagRun.start_date, current_time) >= start_date,
8282
func.coalesce(DagRun.end_date, current_time) <= func.coalesce(end_date, current_time),
8383
)
8484
.group_by(TaskInstance.state)

airflow-core/tests/unit/api_fastapi/core_api/routes/ui/test_dashboard.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,19 @@ def make_dag_runs(dag_maker, session, time_machine):
8484
run_id="run_3",
8585
state=DagRunState.RUNNING,
8686
run_type=DagRunType.SCHEDULED,
87-
logical_date=pendulum.DateTime(2023, 2, 3, 0, 0, 0, tzinfo=pendulum.UTC),
88-
start_date=pendulum.DateTime(2023, 2, 3, 0, 0, 0, tzinfo=pendulum.UTC),
87+
logical_date=date + timedelta(days=2),
88+
start_date=date + timedelta(days=2),
8989
)
90+
9091
run3.end_date = None
9192

93+
dag_maker.create_dagrun(
94+
run_id="run_4",
95+
state=DagRunState.QUEUED,
96+
run_type=DagRunType.SCHEDULED,
97+
logical_date=date + timedelta(days=3),
98+
)
99+
92100
for ti in run1.task_instances:
93101
ti.state = TaskInstanceState.SUCCESS
94102

@@ -208,12 +216,12 @@ class TestHistoricalMetricsDataEndpoint:
208216
(
209217
{"start_date": "2023-01-01T00:00", "end_date": "2023-08-02T00:00"},
210218
{
211-
"dag_run_states": {"failed": 1, "queued": 0, "running": 1, "success": 1},
212-
"dag_run_types": {"backfill": 0, "asset_triggered": 1, "manual": 0, "scheduled": 2},
219+
"dag_run_states": {"failed": 1, "queued": 1, "running": 1, "success": 1},
220+
"dag_run_types": {"backfill": 0, "asset_triggered": 1, "manual": 0, "scheduled": 3},
213221
"task_instance_states": {
214222
"deferred": 0,
215223
"failed": 2,
216-
"no_status": 2,
224+
"no_status": 4,
217225
"queued": 0,
218226
"removed": 0,
219227
"restarting": 0,
@@ -252,12 +260,12 @@ class TestHistoricalMetricsDataEndpoint:
252260
(
253261
{"start_date": "2023-02-02T00:00"},
254262
{
255-
"dag_run_states": {"failed": 1, "queued": 0, "running": 1, "success": 0},
256-
"dag_run_types": {"backfill": 0, "asset_triggered": 1, "manual": 0, "scheduled": 1},
263+
"dag_run_states": {"failed": 1, "queued": 1, "running": 1, "success": 0},
264+
"dag_run_types": {"backfill": 0, "asset_triggered": 1, "manual": 0, "scheduled": 2},
257265
"task_instance_states": {
258266
"deferred": 0,
259267
"failed": 2,
260-
"no_status": 2,
268+
"no_status": 4,
261269
"queued": 0,
262270
"removed": 0,
263271
"restarting": 0,

0 commit comments

Comments
 (0)