Skip to content

Commit

Permalink
bugfix: 修复节点执行时间超过一天后耗时计算不正确的问题 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
homholueng authored and pagezz-canway committed Apr 9, 2019
1 parent 310ff58 commit ecf7ad4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pipeline/engine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def calculate_elapsed_time(started_time, archived_time):
"""
if archived_time and started_time:
# when status_tree['archived_time'] == status_tree['started_time'], set elapsed_time to 1s
elapsed_time = (archived_time - started_time).seconds or 1
elapsed_time = (archived_time - started_time).total_seconds() or 1
elif started_time:
elapsed_time = (timezone.now() - started_time).seconds
elapsed_time = (timezone.now() - started_time).total_seconds()
else:
elapsed_time = 0
return elapsed_time
Expand Down
55 changes: 55 additions & 0 deletions pipeline/tests/engine/utils/test_utils_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""

import datetime

from django.test import TestCase
from django.utils import timezone

from pipeline.engine.utils import calculate_elapsed_time


class EngineUtilsFuncTestCase(TestCase):

def test_calculate_elapsed_time(self):
self.assertEqual(calculate_elapsed_time(None, None), 0)

self.assertEqual(calculate_elapsed_time(started_time=None, archived_time=timezone.now()), 0)

self.assertNotEqual(calculate_elapsed_time(started_time=timezone.now() - datetime.timedelta(seconds=1),
archived_time=None),
0)

# seconds
start = timezone.now()
archive = start + datetime.timedelta(seconds=59)

self.assertEqual(calculate_elapsed_time(started_time=start, archived_time=archive), 59)

# minutes
start = timezone.now()
archive = start + datetime.timedelta(minutes=3)

self.assertEqual(calculate_elapsed_time(started_time=start, archived_time=archive), 3 * 60)

# hours
start = timezone.now()
archive = start + datetime.timedelta(hours=3)

self.assertEqual(calculate_elapsed_time(started_time=start, archived_time=archive), 3 * 60 * 60)

# days
start = timezone.now()
archive = start + datetime.timedelta(days=3)

self.assertEqual(calculate_elapsed_time(started_time=start, archived_time=archive), 3 * 24 * 60 * 60)

0 comments on commit ecf7ad4

Please sign in to comment.