Skip to content

Commit bed1995

Browse files
authored
Avoid color info in response of /dag_stats & /task_stats (apache#8742)
* Avoid color info in response of /dag_stats & /task_stats Currently the color for each state is repeatedly appearing in the response payload of endpoints /dag_stats and /task_stats. This can be avoided by having the mapping between state and color in the static file, and map each state into different color at client side after client receives the necessary info, instead of passing duplicated color information in the response payload. This significantly reduces the size of data to be transferred from server to client.
1 parent 493b685 commit bed1995

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

airflow/www/templates/airflow/dags.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ <h2>DAGs</h2>
246246

247247
const DAGS_INDEX = "{{ url_for('Airflow.index') }}";
248248
const ENTER_KEY_CODE = 13;
249+
const STATE_COLOR = {{ state_color|tojson }};
249250

250251
$('#tags_filter').select2({
251252
placeholder: "Filter dags",
@@ -414,7 +415,7 @@ <h2>DAGs</h2>
414415
})
415416
.attr('stroke', function(d) {
416417
if (d.count > 0)
417-
return d.color;
418+
return STATE_COLOR[d.state];
418419
else {
419420
return 'gainsboro';
420421
}
@@ -491,7 +492,7 @@ <h2>DAGs</h2>
491492
})
492493
.attr('stroke', function(d) {
493494
if (d.count > 0)
494-
return d.color;
495+
return STATE_COLOR[d.state];
495496
else {
496497
return 'gainsboro';
497498
}

airflow/www/views.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ def get_int_arg(value, default=0):
337337

338338
num_of_pages = int(math.ceil(num_of_all_dags / float(dags_per_page)))
339339

340+
state_color_mapping = State.state_color.copy()
341+
state_color_mapping["null"] = state_color_mapping.pop(None)
342+
340343
return self.render_template(
341344
'airflow/dags.html',
342345
dags=dags,
@@ -353,6 +356,7 @@ def get_int_arg(value, default=0):
353356
status=arg_status_filter if arg_status_filter else None),
354357
num_runs=num_runs,
355358
tags=tags,
359+
state_color=state_color_mapping,
356360
status_filter=arg_status_filter,
357361
status_count_all=all_dags_count,
358362
status_count_active=status_count_active,
@@ -399,8 +403,7 @@ def dag_stats(self, session=None):
399403
count = data.get(dag_id, {}).get(state, 0)
400404
payload[dag_id].append({
401405
'state': state,
402-
'count': count,
403-
'color': State.color(state)
406+
'count': count
404407
})
405408

406409
return wwwutils.json_response(payload)
@@ -499,8 +502,7 @@ def task_stats(self, session=None):
499502
count = data.get(dag_id, {}).get(state, 0)
500503
payload[dag_id].append({
501504
'state': state,
502-
'count': count,
503-
'color': State.color(state)
505+
'count': count
504506
})
505507
return wwwutils.json_response(payload)
506508

tests/www/test_views.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,23 @@ def test_health(self):
503503
self.assertIsNone(None, resp_json['scheduler']['latest_scheduler_heartbeat'])
504504

505505
def test_home(self):
506-
resp = self.client.get('home', follow_redirects=True)
507-
self.check_content_in_response('DAGs', resp)
506+
with self.capture_templates() as templates:
507+
resp = self.client.get('home', follow_redirects=True)
508+
self.check_content_in_response('DAGs', resp)
509+
val_state_color_mapping = 'const STATE_COLOR = {"failed": "red", ' \
510+
'"null": "lightblue", "queued": "gray", ' \
511+
'"removed": "lightgrey", "running": "lime", ' \
512+
'"scheduled": "tan", "shutdown": "blue", ' \
513+
'"skipped": "pink", "success": "green", ' \
514+
'"up_for_reschedule": "turquoise", ' \
515+
'"up_for_retry": "gold", "upstream_failed": "orange"};'
516+
self.check_content_in_response(val_state_color_mapping, resp)
517+
518+
self.assertEqual(len(templates), 1)
519+
self.assertEqual(templates[0].name, 'airflow/dags.html')
520+
state_color_mapping = State.state_color.copy()
521+
state_color_mapping["null"] = state_color_mapping.pop(None)
522+
self.assertEqual(templates[0].local_context['state_color'], state_color_mapping)
508523

509524
def test_users_list(self):
510525
resp = self.client.get('users/list', follow_redirects=True)
@@ -585,7 +600,7 @@ def test_task_stats(self):
585600
resp = self.client.post('task_stats', follow_redirects=True)
586601
self.assertEqual(resp.status_code, 200)
587602
self.assertEqual(set(list(resp.json.items())[0][1][0].keys()),
588-
{'state', 'count', 'color'})
603+
{'state', 'count'})
589604

590605
@conf_vars({("webserver", "show_recent_stats_for_completed_runs"): "False"})
591606
def test_task_stats_only_noncompleted(self):
@@ -1681,7 +1696,7 @@ def test_dag_stats_success(self):
16811696
resp = self.client.post('dag_stats', follow_redirects=True)
16821697
self.check_content_in_response('example_bash_operator', resp)
16831698
self.assertEqual(set(list(resp.json.items())[0][1][0].keys()),
1684-
{'state', 'count', 'color'})
1699+
{'state', 'count'})
16851700

16861701
def test_dag_stats_failure(self):
16871702
self.logout()

0 commit comments

Comments
 (0)