You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ka...@apache.org on 2020/06/26 20:52:34 UTC

[airflow] branch v1-10-test updated: Avoid color info in response of /dag_stats & /task_stats (#8742)

This is an automated email from the ASF dual-hosted git repository.

kaxilnaik pushed a commit to branch v1-10-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v1-10-test by this push:
     new 18ff4c2  Avoid color info in response of /dag_stats & /task_stats (#8742)
18ff4c2 is described below

commit 18ff4c24dc1e5f66ced8f20087de736ef7eda5fe
Author: Xiaodong DENG <xd...@hotmail.com>
AuthorDate: Mon May 11 08:35:31 2020 +0200

    Avoid color info in response of /dag_stats & /task_stats (#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)
---
 airflow/www_rbac/templates/airflow/dags.html |  5 +++--
 airflow/www_rbac/views.py                    | 10 ++++++----
 tests/www_rbac/test_views.py                 | 23 +++++++++++++++++++----
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/airflow/www_rbac/templates/airflow/dags.html b/airflow/www_rbac/templates/airflow/dags.html
index f2f131c..095c213 100644
--- a/airflow/www_rbac/templates/airflow/dags.html
+++ b/airflow/www_rbac/templates/airflow/dags.html
@@ -244,6 +244,7 @@
 
     const DAGS_INDEX = "{{ url_for('Airflow.index') }}";
     const ENTER_KEY_CODE = 13;
+    const STATE_COLOR = {{ state_color|tojson }};
 
     $('#tags_filter').select2({
       placeholder: "Filter dags",
@@ -412,7 +413,7 @@
           })
           .attr('stroke', function(d) {
             if (d.count > 0)
-              return d.color;
+              return STATE_COLOR[d.state];
             else {
               return 'gainsboro';
             }
@@ -489,7 +490,7 @@
           })
           .attr('stroke', function(d) {
             if (d.count > 0)
-              return d.color;
+              return STATE_COLOR[d.state];
             else {
               return 'gainsboro';
             }
diff --git a/airflow/www_rbac/views.py b/airflow/www_rbac/views.py
index c7831c4..a626d87 100644
--- a/airflow/www_rbac/views.py
+++ b/airflow/www_rbac/views.py
@@ -332,6 +332,9 @@ class Airflow(AirflowBaseView):
 
         num_of_pages = int(math.ceil(num_of_all_dags / float(dags_per_page)))
 
+        state_color_mapping = State.state_color.copy()
+        state_color_mapping["null"] = state_color_mapping.pop(None)
+
         return self.render_template(
             'airflow/dags.html',
             dags=dags,
@@ -348,6 +351,7 @@ class Airflow(AirflowBaseView):
                                            status=arg_status_filter if arg_status_filter else None),
             num_runs=num_runs,
             tags=tags,
+            state_color=state_color_mapping,
             status_filter=arg_status_filter,
             status_count_all=all_dags_count,
             status_count_active=status_count_active,
@@ -395,8 +399,7 @@ class Airflow(AirflowBaseView):
                 count = data.get(dag_id, {}).get(state, 0)
                 payload[dag_id].append({
                     'state': state,
-                    'count': count,
-                    'color': State.color(state)
+                    'count': count
                 })
 
         return wwwutils.json_response(payload)
@@ -489,8 +492,7 @@ class Airflow(AirflowBaseView):
                 count = data.get(dag_id, {}).get(state, 0)
                 payload[dag_id].append({
                     'state': state,
-                    'count': count,
-                    'color': State.color(state)
+                    'count': count
                 })
         return wwwutils.json_response(payload)
 
diff --git a/tests/www_rbac/test_views.py b/tests/www_rbac/test_views.py
index 35dab60..894e92f 100644
--- a/tests/www_rbac/test_views.py
+++ b/tests/www_rbac/test_views.py
@@ -485,8 +485,23 @@ class TestAirflowBaseViews(TestBase):
         self.assertIsNone(None, resp_json['scheduler']['latest_scheduler_heartbeat'])
 
     def test_home(self):
-        resp = self.client.get('home', follow_redirects=True)
-        self.check_content_in_response('DAGs', resp)
+        with self.capture_templates() as templates:
+            resp = self.client.get('home', follow_redirects=True)
+            self.check_content_in_response('DAGs', resp)
+            val_state_color_mapping = 'const STATE_COLOR = {"failed": "red", ' \
+                                      '"null": "lightblue", "queued": "gray", ' \
+                                      '"removed": "lightgrey", "running": "lime", ' \
+                                      '"scheduled": "tan", "shutdown": "blue", ' \
+                                      '"skipped": "pink", "success": "green", ' \
+                                      '"up_for_reschedule": "turquoise", ' \
+                                      '"up_for_retry": "gold", "upstream_failed": "orange"};'
+            self.check_content_in_response(val_state_color_mapping, resp)
+
+        self.assertEqual(len(templates), 1)
+        self.assertEqual(templates[0].name, 'airflow/dags.html')
+        state_color_mapping = State.state_color.copy()
+        state_color_mapping["null"] = state_color_mapping.pop(None)
+        self.assertEqual(templates[0].local_context['state_color'], state_color_mapping)
 
     def test_home_filter_tags(self):
         from airflow.www_rbac.views import FILTER_TAGS_COOKIE
@@ -548,7 +563,7 @@ class TestAirflowBaseViews(TestBase):
         resp = self.client.post('task_stats', follow_redirects=True)
         self.assertEqual(resp.status_code, 200)
         self.assertEqual(set(list(resp.json.items())[0][1][0].keys()),
-                         {'state', 'count', 'color'})
+                         {'state', 'count'})
 
     def test_task_stats_only_noncompleted(self):
         conf.set("webserver", "show_recent_stats_for_completed_runs", "False")
@@ -1538,7 +1553,7 @@ class TestDagACLView(TestBase):
         resp = self.client.post('dag_stats', follow_redirects=True)
         self.check_content_in_response('example_bash_operator', resp)
         self.assertEqual(set(list(resp.json.items())[0][1][0].keys()),
-                         {'state', 'count', 'color'})
+                         {'state', 'count'})
 
     def test_dag_stats_failure(self):
         self.logout()