Skip to content

Commit e9d1066

Browse files
Add name fields to SDK deadline alerts (#64926)
* Add deadlines support with name and description fields in alerts and UI * Add 'viewAll' label to deadlineStatus in dag.json * Refactor deadlineAlerts referenceType structure * Refine deadline alert translations in dag.json Updated deadline alert messages for clarity and consistency. * Add completion rule text for deadline alerts in UI * Remove duplicate completionRule entry in deadlineAlerts * Remove alert description from DeadlineAlert and related models * Enhance deadline handling: return name updates alongside UUID mapping in SerializedDagModel * Add alert_id field to DeadlineResponse and update tests for alert handling * Remove DEADLINES option from MenuItem enum * Update airflow-core/src/airflow/serialization/encoders.py Co-authored-by: D. Ferruzzi <[email protected]> --------- Co-authored-by: D. Ferruzzi <[email protected]>
1 parent e36d10a commit e9d1066

12 files changed

Lines changed: 128 additions & 56 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/datamodels/ui/deadline.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ class DeadlineResponse(BaseModel):
3535
created_at: datetime
3636
dag_id: str = Field(validation_alias=AliasPath("dagrun", "dag_id"))
3737
dag_run_id: str = Field(validation_alias=AliasPath("dagrun", "run_id"))
38+
alert_id: UUID | None = Field(validation_alias="deadline_alert_id", default=None)
3839
alert_name: str | None = Field(validation_alias=AliasPath("deadline_alert", "name"), default=None)
39-
alert_description: str | None = Field(
40-
validation_alias=AliasPath("deadline_alert", "description"), default=None
41-
)
4240

4341

4442
class DeadlineCollectionResponse(BaseModel):
@@ -53,7 +51,6 @@ class DeadlineAlertResponse(BaseModel):
5351

5452
id: UUID
5553
name: str | None = None
56-
description: str | None = None
5754
reference_type: str = Field(validation_alias=AliasPath("reference", "reference_type"))
5855
interval: float = Field(description="Interval in seconds between deadline evaluations.")
5956
created_at: datetime

‎airflow-core/src/airflow/api_fastapi/core_api/openapi/_private_ui.yaml‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,11 +2319,6 @@ components:
23192319
- type: string
23202320
- type: 'null'
23212321
title: Name
2322-
description:
2323-
anyOf:
2324-
- type: string
2325-
- type: 'null'
2326-
title: Description
23272322
reference_type:
23282323
type: string
23292324
title: Reference Type
@@ -2382,16 +2377,17 @@ components:
23822377
dag_run_id:
23832378
type: string
23842379
title: Dag Run Id
2385-
alert_name:
2380+
alert_id:
23862381
anyOf:
23872382
- type: string
2383+
format: uuid
23882384
- type: 'null'
2389-
title: Alert Name
2390-
alert_description:
2385+
title: Alert Id
2386+
alert_name:
23912387
anyOf:
23922388
- type: string
23932389
- type: 'null'
2394-
title: Alert Description
2390+
title: Alert Name
23952391
type: object
23962392
required:
23972393
- id

‎airflow-core/src/airflow/models/serialized_dag.py‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def _try_reuse_deadline_uuids(
431431
existing_deadline_uuids: list[str],
432432
new_deadline_data: list[dict],
433433
session: Session,
434-
) -> dict[str, dict] | None:
434+
) -> tuple[dict[str, dict], dict[str, str | None]] | None:
435435
"""
436436
Try to reuse existing deadline UUIDs if the deadline definitions haven't changed.
437437
@@ -440,7 +440,11 @@ def _try_reuse_deadline_uuids(
440440
:param existing_deadline_uuids: List of UUID strings from existing serialized Dag
441441
:param new_deadline_data: List of new deadline alert data dicts from the Dag
442442
:param session: Database session
443-
:return: UUID mapping dict if all match, None if any mismatch detected
443+
:return: Tuple of (uuid_mapping, name_updates) if all definitions match, None if any
444+
mismatch detected. ``uuid_mapping`` maps UUID string → new deadline data dict.
445+
``name_updates`` maps UUID string → new name **only** for entries whose name
446+
changed relative to the existing DB row, so callers can issue targeted UPDATEs
447+
and reliably detect whether any DB write occurred.
444448
"""
445449
# defensive check for old 3.1.x format
446450
if existing_deadline_uuids and not isinstance(existing_deadline_uuids[0], str):
@@ -468,6 +472,7 @@ def _definitions_match(deadline_data: dict, existing: DeadlineAlertModel) -> boo
468472

469473
matched_uuids: set[UUID] = set()
470474
uuid_mapping: dict[str, dict] = {}
475+
name_updates: dict[str, str | None] = {}
471476

472477
for deadline_alert in new_deadline_data:
473478
deadline_data = deadline_alert.get(Encoding.VAR, deadline_alert)
@@ -479,9 +484,13 @@ def _definitions_match(deadline_data: dict, existing: DeadlineAlertModel) -> boo
479484

480485
if _definitions_match(deadline_data, existing_alert):
481486
# Found a match, reuse this UUID
482-
uuid_mapping[str(existing_alert.id)] = deadline_data
487+
uuid_str = str(existing_alert.id)
488+
uuid_mapping[uuid_str] = deadline_data
483489
matched_uuids.add(existing_alert.id)
484490
found_match = True
491+
new_name = deadline_data.get(DeadlineAlertFields.NAME)
492+
if new_name != existing_alert.name:
493+
name_updates[uuid_str] = new_name
485494
break
486495

487496
if not found_match:
@@ -490,7 +499,7 @@ def _definitions_match(deadline_data: dict, existing: DeadlineAlertModel) -> boo
490499
# to another deadline), so partial reuse would risk stale cross-references.
491500
return None
492501

493-
return uuid_mapping
502+
return uuid_mapping, name_updates
494503

495504
@classmethod
496505
def _create_deadline_alert_records(
@@ -510,6 +519,7 @@ def _create_deadline_alert_records(
510519
for uuid_str, deadline_data in uuid_mapping.items():
511520
alert = DeadlineAlertModel(
512521
id=UUID(uuid_str),
522+
name=deadline_data.get(DeadlineAlertFields.NAME),
513523
reference=deadline_data[DeadlineAlertFields.REFERENCE],
514524
interval=deadline_data[DeadlineAlertFields.INTERVAL],
515525
callback_def=deadline_data[DeadlineAlertFields.CALLBACK],
@@ -625,6 +635,7 @@ def write_dag(
625635
serialized_dag_hash = _prefetched.dag_hash
626636
dag_version = _prefetched.dag_version
627637

638+
name_updated = False
628639
if dag.data.get("dag", {}).get("deadline"):
629640
# Try to reuse existing deadline UUIDs if the deadline definitions haven't changed.
630641
# This preserves the hash and avoids unnecessary SerializedDagModel recreations.
@@ -637,16 +648,24 @@ def write_dag(
637648
and existing_serialized_dag.data
638649
and (existing_deadline_uuids := existing_serialized_dag.data.get("dag", {}).get("deadline"))
639650
):
640-
deadline_uuid_mapping = cls._try_reuse_deadline_uuids(
651+
reuse_result = cls._try_reuse_deadline_uuids(
641652
existing_deadline_uuids,
642653
dag.data["dag"]["deadline"],
643654
session,
644655
)
645656

646-
if deadline_uuid_mapping is not None:
657+
if reuse_result is not None:
658+
deadline_uuid_mapping, name_updates = reuse_result
647659
# All deadlines matched — reuse the UUIDs to preserve hash.
648-
# Clear the mapping since the alert rows already exist in the DB;
649-
# no need to delete and recreate identical records.
660+
# Only issue UPDATE statements for rows whose name actually changed to
661+
# avoid unnecessary writes and to make the return value accurate.
662+
for uuid_str, new_name in name_updates.items():
663+
session.execute(
664+
update(DeadlineAlertModel)
665+
.where(DeadlineAlertModel.id == UUID(uuid_str))
666+
.values(name=new_name)
667+
)
668+
name_updated = bool(name_updates)
650669
dag.data["dag"]["deadline"] = existing_deadline_uuids
651670
deadline_uuid_mapping = {}
652671
else:
@@ -665,6 +684,10 @@ def write_dag(
665684
and dag_version
666685
and dag_version.bundle_name == bundle_name
667686
):
687+
if name_updated:
688+
# The serialized DAG itself is unchanged, but deadline alert name(s) were
689+
# updated in the DB, so report True so callers know a write did occur.
690+
return True
668691
log.debug("Serialized DAG (%s) is unchanged. Skipping writing to DB", dag.dag_id)
669692
return False
670693

‎airflow-core/src/airflow/serialization/decoders.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def decode_deadline_alert(encoded_data: dict):
166166
reference=reference,
167167
interval=datetime.timedelta(seconds=data[DeadlineAlertFields.INTERVAL]),
168168
callback=deserialize(data[DeadlineAlertFields.CALLBACK]),
169+
name=data.get(DeadlineAlertFields.NAME),
169170
)
170171

171172

‎airflow-core/src/airflow/serialization/definitions/deadline.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class DeadlineAlertFields:
4949
serializing DeadlineAlert instances to and from their dictionary representation.
5050
"""
5151

52+
NAME = "name"
5253
REFERENCE = "reference"
5354
INTERVAL = "interval"
5455
CALLBACK = "callback"
@@ -367,3 +368,4 @@ class SerializedDeadlineAlert:
367368
reference: SerializedReferenceModels.SerializedBaseDeadlineReference
368369
interval: timedelta
369370
callback: Any
371+
name: str | None = None

‎airflow-core/src/airflow/serialization/encoders.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def encode_deadline_alert(d: DeadlineAlert | SerializedDeadlineAlert) -> dict[st
211211
from airflow.sdk.serde import serialize
212212

213213
return {
214+
"name": d.name,
214215
"reference": encode_deadline_reference(d.reference),
215216
"interval": d.interval.total_seconds(),
216217
"callback": serialize(d.callback),

‎airflow-core/src/airflow/ui/openapi-gen/requests/schemas.gen.ts‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8065,17 +8065,6 @@ export const $DeadlineAlertResponse = {
80658065
],
80668066
title: 'Name'
80678067
},
8068-
description: {
8069-
anyOf: [
8070-
{
8071-
type: 'string'
8072-
},
8073-
{
8074-
type: 'null'
8075-
}
8076-
],
8077-
title: 'Description'
8078-
},
80798068
reference_type: {
80808069
type: 'string',
80818070
title: 'Reference Type'
@@ -8146,18 +8135,19 @@ export const $DeadlineResponse = {
81468135
type: 'string',
81478136
title: 'Dag Run Id'
81488137
},
8149-
alert_name: {
8138+
alert_id: {
81508139
anyOf: [
81518140
{
8152-
type: 'string'
8141+
type: 'string',
8142+
format: 'uuid'
81538143
},
81548144
{
81558145
type: 'null'
81568146
}
81578147
],
8158-
title: 'Alert Name'
8148+
title: 'Alert Id'
81598149
},
8160-
alert_description: {
8150+
alert_name: {
81618151
anyOf: [
81628152
{
81638153
type: 'string'
@@ -8166,7 +8156,7 @@ export const $DeadlineResponse = {
81668156
type: 'null'
81678157
}
81688158
],
8169-
title: 'Alert Description'
8159+
title: 'Alert Name'
81708160
}
81718161
},
81728162
type: 'object',

‎airflow-core/src/airflow/ui/openapi-gen/requests/types.gen.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,6 @@ export type DeadlineAlertCollectionResponse = {
19841984
export type DeadlineAlertResponse = {
19851985
id: string;
19861986
name?: string | null;
1987-
description?: string | null;
19881987
reference_type: string;
19891988
/**
19901989
* Interval in seconds between deadline evaluations.
@@ -2011,8 +2010,8 @@ export type DeadlineResponse = {
20112010
created_at: string;
20122011
dag_id: string;
20132012
dag_run_id: string;
2013+
alert_id?: string | null;
20142014
alert_name?: string | null;
2015-
alert_description?: string | null;
20162015
};
20172016

20182017
/**

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
RUN_DAG2 = "run_dag2" # belongs to DAG_ID_2, which is used for cross-dag filter tests
5555

5656
ALERT_NAME = "SLA Breach Alert"
57-
ALERT_DESCRIPTION = "Fires when SLA is breached"
5857

5958
_CALLBACK_PATH = "tests.unit.api_fastapi.core_api.routes.ui.test_deadlines._noop_callback"
6059

@@ -156,7 +155,6 @@ def setup(dag_maker, session):
156155
alert = DeadlineAlert(
157156
serialized_dag_id=serialized_dag.id,
158157
name=ALERT_NAME,
159-
description=ALERT_DESCRIPTION,
160158
reference=DeadlineReference.DAGRUN_QUEUED_AT.serialize_reference(),
161159
interval=3600.0,
162160
callback_def={"path": _CALLBACK_PATH},
@@ -246,7 +244,7 @@ def test_single_deadline_without_alert(self, test_client):
246244
assert deadline1["deadline_time"] == "2025-01-01T12:00:00Z"
247245
assert deadline1["missed"] is False
248246
assert deadline1["alert_name"] is None
249-
assert deadline1["alert_description"] is None
247+
assert deadline1["alert_id"] is None
250248
assert deadline1["dag_id"] == DAG_ID
251249
assert deadline1["dag_run_id"] == RUN_SINGLE
252250
assert "id" in deadline1
@@ -259,14 +257,16 @@ def test_missed_deadline_is_reflected(self, test_client):
259257
assert data["total_entries"] == 1
260258
assert data["deadlines"][0]["missed"] is True
261259

262-
def test_deadline_with_alert_name_and_description(self, test_client):
260+
def test_deadline_with_alert_name(self, test_client, session):
261+
alert = session.scalar(select(DeadlineAlert).where(DeadlineAlert.name == ALERT_NAME))
263262
with assert_queries_count(4):
264263
response = test_client.get(f"/dags/{DAG_ID}/dagRuns/{RUN_ALERT}/deadlines")
265264
assert response.status_code == 200
266265
data = response.json()
267266
assert data["total_entries"] == 1
268-
assert data["deadlines"][0]["alert_name"] == ALERT_NAME
269-
assert data["deadlines"][0]["alert_description"] == ALERT_DESCRIPTION
267+
dl = data["deadlines"][0]
268+
assert dl["alert_name"] == ALERT_NAME
269+
assert dl["alert_id"] == str(alert.id)
270270

271271
def test_deadlines_ordered_by_deadline_time_ascending(self, test_client):
272272
with assert_queries_count(4):
@@ -436,15 +436,19 @@ def test_pagination(self, test_client):
436436
assert [dl["id"] for dl in page1] == all_ids[:3]
437437
assert [dl["id"] for dl in page2] == all_ids[3:6]
438438

439-
def test_alert_name_present_when_linked(self, test_client):
440-
"""Deadlines linked to a DeadlineAlert include alert_name and alert_description."""
439+
def test_alert_name_present_when_linked(self, test_client, session):
440+
"""Deadlines linked to a DeadlineAlert include both alert_name and alert_id."""
441+
alert = session.scalar(select(DeadlineAlert).where(DeadlineAlert.name == ALERT_NAME))
441442
response = test_client.get(f"/dags/{DAG_ID}/dagRuns/~/deadlines")
442443
assert response.status_code == 200
443444
deadlines = response.json()["deadlines"]
444-
alerts = [dl for dl in deadlines if dl["alert_name"] is not None]
445-
assert len(alerts) == 1
446-
assert alerts[0]["alert_name"] == ALERT_NAME
447-
assert alerts[0]["alert_description"] == ALERT_DESCRIPTION
445+
linked = [dl for dl in deadlines if dl["alert_name"] is not None]
446+
assert len(linked) == 1
447+
assert linked[0]["alert_name"] == ALERT_NAME
448+
assert linked[0]["alert_id"] == str(alert.id)
449+
# Unlinked deadlines must have null alert_id
450+
unlinked = [dl for dl in deadlines if dl["alert_name"] is None]
451+
assert all(dl["alert_id"] is None for dl in unlinked)
448452

449453
def test_filter_nonexistent_dag_returns_empty(self, test_client):
450454
"""Filtering by a dag_id that doesn't exist returns an empty list."""
@@ -483,7 +487,6 @@ def test_alert_response_fields(self, test_client):
483487
assert response.status_code == 200
484488
alert = response.json()["deadline_alerts"][0]
485489
assert alert["name"] == ALERT_NAME
486-
assert alert["description"] == ALERT_DESCRIPTION
487490
assert alert["interval"] == 3600.0
488491
assert alert["reference_type"] == "DagRunQueuedAtDeadline"
489492
assert "id" in alert

‎airflow-core/tests/unit/models/test_deadline_alert.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
DAG_ID = "test_deadline_alert_dag"
3636
DEADLINE_NAME = "Test Alert"
37-
DEADLINE_DESCRIPTION = "This is a test alert description"
3837
DEADLINE_INTERVAL = 60
3938
DEADLINE_CALLBACK = {"path": "test.callback"}
4039
SERIALIZED_DAG_ID = "serialized_dag_uuid"
@@ -62,7 +61,6 @@ def deadline_alert_orm(dag_maker, session, deadline_reference):
6261
alert = DeadlineAlert(
6362
serialized_dag_id=serialized_dag.id,
6463
name=DEADLINE_NAME,
65-
description=DEADLINE_DESCRIPTION,
6664
reference=deadline_reference,
6765
interval=DEADLINE_INTERVAL,
6866
callback_def=DEADLINE_CALLBACK,
@@ -86,7 +84,6 @@ def test_deadline_alert_creation(self, deadline_alert_orm):
8684
assert deadline_alert_orm.id is not None
8785
assert deadline_alert_orm.created_at == DEFAULT_DATE
8886
assert deadline_alert_orm.name == DEADLINE_NAME
89-
assert deadline_alert_orm.description == DEADLINE_DESCRIPTION
9087

9188
def test_minimal_deadline_alert_creation(self, dag_maker, session, deadline_reference):
9289
with dag_maker(DAG_ID, session=session):
@@ -109,7 +106,6 @@ def test_minimal_deadline_alert_creation(self, dag_maker, session, deadline_refe
109106
assert deadline_alert.id is not None
110107
assert deadline_alert.created_at == DEFAULT_DATE
111108
assert deadline_alert.name is None
112-
assert deadline_alert.description is None
113109

114110
def test_deadline_alert_repr(self, deadline_alert_orm, deadline_reference):
115111
repr_str = repr(deadline_alert_orm)

0 commit comments

Comments
 (0)