Skip to content

Commit b13780f

Browse files
XD-DENGChris Fei
authored andcommitted
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. (cherry-picked from bed1995)
1 parent b261c8c commit b13780f

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

airflow/www/templates/airflow/dags.html

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

218218
const DAGS_INDEX = "{{ url_for('admin.index') }}";
219219
const ENTER_KEY_CODE = 13;
220+
const STATE_COLOR = {{ state_color|tojson }};
220221

221222
$('#dag_query').on('keypress', function (e) {
222223
// check for key press on ENTER (key code 13) to trigger the search
@@ -349,7 +350,7 @@ <h2>DAGs</h2>
349350
})
350351
.attr('stroke', function(d) {
351352
if (d.count > 0)
352-
return d.color;
353+
return STATE_COLOR[d.state];
353354
else {
354355
return 'gainsboro';
355356
}
@@ -428,7 +429,7 @@ <h2>DAGs</h2>
428429
})
429430
.attr('stroke', function(d) {
430431
if (d.count > 0)
431-
return d.color;
432+
return STATE_COLOR[d.state];
432433
else {
433434
return 'gainsboro';
434435
}

airflow/www/views.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,7 @@ def dag_stats(self, session=None):
603603
count = d.get(state, 0)
604604
payload[dag_id].append({
605605
'state': state,
606-
'count': count,
607-
'color': State.color(state)
606+
'count': count
608607
})
609608
return wwwutils.json_response(payload)
610609

@@ -686,8 +685,7 @@ def task_stats(self, session=None):
686685
count = data.get(dag_id, {}).get(state, 0)
687686
payload[dag_id].append({
688687
'state': state,
689-
'count': count,
690-
'color': State.color(state)
688+
'count': count
691689
})
692690
return wwwutils.json_response(payload)
693691

@@ -2330,11 +2328,15 @@ def get_int_arg(value, default=0):
23302328
auto_complete_data.add(row.dag_id)
23312329
auto_complete_data.add(row.owners)
23322330

2331+
state_color_mapping = State.state_color.copy()
2332+
state_color_mapping["null"] = state_color_mapping.pop(None)
2333+
23332334
return self.render(
23342335
'airflow/dags.html',
23352336
dags=dags,
23362337
hide_paused=hide_paused,
23372338
current_page=current_page,
2339+
state_color=state_color_mapping,
23382340
search_query=arg_search_query if arg_search_query else '',
23392341
page_size=dags_per_page,
23402342
num_of_pages=num_of_pages,

airflow/www_rbac/templates/airflow/dags.html

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

245245
const DAGS_INDEX = "{{ url_for('Airflow.index') }}";
246246
const ENTER_KEY_CODE = 13;
247+
const STATE_COLOR = {{ state_color|tojson }};
247248

248249
$('#tags_filter').select2({
249250
placeholder: "Filter dags",
@@ -412,7 +413,7 @@ <h2>DAGs</h2>
412413
})
413414
.attr('stroke', function(d) {
414415
if (d.count > 0)
415-
return d.color;
416+
return STATE_COLOR[d.state];
416417
else {
417418
return 'gainsboro';
418419
}
@@ -489,7 +490,7 @@ <h2>DAGs</h2>
489490
})
490491
.attr('stroke', function(d) {
491492
if (d.count > 0)
492-
return d.color;
493+
return STATE_COLOR[d.state];
493494
else {
494495
return 'gainsboro';
495496
}

airflow/www_rbac/views.py

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

334334
num_of_pages = int(math.ceil(num_of_all_dags / float(dags_per_page)))
335335

336+
state_color_mapping = State.state_color.copy()
337+
state_color_mapping["null"] = state_color_mapping.pop(None)
338+
336339
return self.render_template(
337340
'airflow/dags.html',
338341
dags=dags,
@@ -349,6 +352,7 @@ def get_int_arg(value, default=0):
349352
status=arg_status_filter if arg_status_filter else None),
350353
num_runs=num_runs,
351354
tags=tags,
355+
state_color=state_color_mapping,
352356
status_filter=arg_status_filter,
353357
status_count_all=all_dags_count,
354358
status_count_active=status_count_active,
@@ -396,8 +400,7 @@ def dag_stats(self, session=None):
396400
count = data.get(dag_id, {}).get(state, 0)
397401
payload[dag_id].append({
398402
'state': state,
399-
'count': count,
400-
'color': State.color(state)
403+
'count': count
401404
})
402405

403406
return wwwutils.json_response(payload)
@@ -490,8 +493,7 @@ def task_stats(self, session=None):
490493
count = data.get(dag_id, {}).get(state, 0)
491494
payload[dag_id].append({
492495
'state': state,
493-
'count': count,
494-
'color': State.color(state)
496+
'count': count
495497
})
496498
return wwwutils.json_response(payload)
497499

tests/www/test_views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ def test_all_dags(self):
13081308
self.assertIn('example_bash_operator', stats)
13091309
self.assertIn('example_xcom', stats)
13101310
self.assertEqual(set(stats['example_bash_operator'][0].keys()),
1311-
{'state', 'count', 'color'})
1311+
{'state', 'count'})
13121312

13131313
def test_selected_dags(self):
13141314
resp = self.app.get(
@@ -1336,7 +1336,7 @@ def test_dag_stats(self):
13361336
self.assertEqual(resp.status_code, 200)
13371337
stats = json.loads(resp.data.decode('utf-8'))
13381338
self.assertEqual(set(list(stats.items())[0][1][0].keys()),
1339-
{'state', 'count', 'color'})
1339+
{'state', 'count'})
13401340

13411341

13421342
if __name__ == '__main__':

tests/www_rbac/test_views.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ def test_health(self):
487487
def test_home(self):
488488
resp = self.client.get('home', follow_redirects=True)
489489
self.check_content_in_response('DAGs', resp)
490+
val_state_color_mapping = 'const STATE_COLOR = {"failed": "red", ' \
491+
'"null": "lightblue", "queued": "gray", ' \
492+
'"removed": "lightgrey", "running": "lime", ' \
493+
'"scheduled": "tan", "shutdown": "blue", ' \
494+
'"skipped": "pink", "success": "green", ' \
495+
'"up_for_reschedule": "turquoise", ' \
496+
'"up_for_retry": "gold", "upstream_failed": "orange"};'
497+
self.check_content_in_response(val_state_color_mapping, resp)
490498

491499
def test_home_filter_tags(self):
492500
from airflow.www_rbac.views import FILTER_TAGS_COOKIE
@@ -548,7 +556,7 @@ def test_task_stats(self):
548556
resp = self.client.post('task_stats', follow_redirects=True)
549557
self.assertEqual(resp.status_code, 200)
550558
self.assertEqual(set(list(resp.json.items())[0][1][0].keys()),
551-
{'state', 'count', 'color'})
559+
{'state', 'count'})
552560

553561
def test_task_stats_only_noncompleted(self):
554562
conf.set("webserver", "show_recent_stats_for_completed_runs", "False")
@@ -1538,7 +1546,7 @@ def test_dag_stats_success(self):
15381546
resp = self.client.post('dag_stats', follow_redirects=True)
15391547
self.check_content_in_response('example_bash_operator', resp)
15401548
self.assertEqual(set(list(resp.json.items())[0][1][0].keys()),
1541-
{'state', 'count', 'color'})
1549+
{'state', 'count'})
15421550

15431551
def test_dag_stats_failure(self):
15441552
self.logout()

0 commit comments

Comments
 (0)