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

ash pushed a commit to branch v2-0-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 71b44a2244521582d94a4298d514234738e1b1e1
Author: Yujia Yang <[email protected]>
AuthorDate: Fri Feb 19 00:14:44 2021 +0800

    fix lossing duration < 1 secs in tree (#13537)
    
    truncat duration to 3dp when duration < 10
    
    Update airflow/www/views.py
    
    Co-authored-by: Ash Berlin-Taylor <[email protected]>
    
    fix import, retrigger ci
    
    (cherry picked from commit 8f21fb1bf77fc67e37dc13613778ff1e6fa87cea)
---
 airflow/www/views.py    | 10 +++++++++-
 tests/www/test_views.py | 14 +++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/airflow/www/views.py b/airflow/www/views.py
index ab31607..4c272b5 100644
--- a/airflow/www/views.py
+++ b/airflow/www/views.py
@@ -111,6 +111,14 @@ FILTER_TAGS_COOKIE = 'tags_filter'
 FILTER_STATUS_COOKIE = 'dag_status_filter'
 
 
+def truncate_task_duration(task_duration):
+    """
+    Cast the task_duration to an int was for optimization for large/huge dags 
if task_duration > 10s
+    otherwise we keep it as a float with 3dp
+    """
+    return int(task_duration) if task_duration > 10.0 else 
round(task_duration, 3)
+
+
 def get_safe_url(url):
     """Given a user-supplied URL, ensure it points to our web server"""
     valid_schemes = ['http', 'https', '']
@@ -1921,7 +1929,7 @@ class Airflow(AirflowBaseView):  # noqa: D101  pylint: 
disable=too-many-public-m
                 # round to seconds to reduce payload size
                 task_instance_data[2] = 
int(task_instance.start_date.timestamp())
                 if task_instance.duration is not None:
-                    task_instance_data[3] = int(task_instance.duration)
+                    task_instance_data[3] = 
truncate_task_duration(task_instance.duration)
 
             return task_instance_data
 
diff --git a/tests/www/test_views.py b/tests/www/test_views.py
index b391e56..d284314 100644
--- a/tests/www/test_views.py
+++ b/tests/www/test_views.py
@@ -62,7 +62,7 @@ from airflow.utils.state import State
 from airflow.utils.timezone import datetime
 from airflow.utils.types import DagRunType
 from airflow.www import app as application
-from airflow.www.views import ConnectionModelView, get_safe_url
+from airflow.www.views import ConnectionModelView, get_safe_url, 
truncate_task_duration
 from tests.test_utils import fab_utils
 from tests.test_utils.asserts import assert_queries_count
 from tests.test_utils.config import conf_vars
@@ -3311,3 +3311,15 @@ class TestHelperFunctions(TestBase):
         mock_url_for.return_value = "/home"
         with self.app.test_request_context(base_url="http://localhost:8080";):
             assert get_safe_url(test_url) == expected_url
+
+    @parameterized.expand(
+        [
+            (0.12345, 0.123),
+            (0.12355, 0.124),
+            (3.12, 3.12),
+            (9.99999, 10.0),
+            (10.01232, 10),
+        ]
+    )
+    def test_truncate_task_duration(self, test_duration, expected_duration):
+        assert truncate_task_duration(test_duration) == expected_duration

Reply via email to