From 44a20c87cfc1f08f45320f7f7dace5de7dc51dbd Mon Sep 17 00:00:00 2001 From: renaud gaudin Date: Wed, 1 May 2024 09:19:47 +0000 Subject: [PATCH] Issue #35: prevent crash if missing started_on --- CHANGELOG.md | 6 ++++++ src/image_creator/__init__.py | 2 +- src/image_creator/utils/aria2.py | 12 +++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4386248..bba5004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.1.2] - 2024-05-01 + +### Fixed + +- Download crash if started_on information is missing (#35) + ## [1.1.1] - 2024-04-24 ### Added diff --git a/src/image_creator/__init__.py b/src/image_creator/__init__.py index a82b376..72f26f5 100644 --- a/src/image_creator/__init__.py +++ b/src/image_creator/__init__.py @@ -1 +1 @@ -__version__ = "1.1.1" +__version__ = "1.1.2" diff --git a/src/image_creator/utils/aria2.py b/src/image_creator/utils/aria2.py index 6c88dfd..f910e07 100644 --- a/src/image_creator/utils/aria2.py +++ b/src/image_creator/utils/aria2.py @@ -906,13 +906,19 @@ def get_feedback(self, only_for: list[str] | None = None): started_ons.append(dl.started_on) completed_ons.append(dl.completed_on) feedbacks.append(feedback) - started_on = min(started_ons) + completed_on = ( max(completed_ons) if all(isinstance(item, datetime.datetime) for item in completed_ons) else datetime.datetime.now(tz=datetime.UTC) ) - duration = completed_on - started_on + if started_ons: + started_on = min(started_ons) + duration = completed_on - started_on + general_speed = int(downloaded_bytes / duration.total_seconds()) + else: + duration = datetime.timedelta(seconds=0) + general_speed = 0 return GeneralFeedback( count=Progress(downloaded=downloaded_nb, total=total_nb, speed=0), @@ -920,7 +926,7 @@ def get_feedback(self, only_for: list[str] | None = None): downloaded=downloaded_bytes, total=total_bytes, speed=speed ), duration=duration, - speed=int(downloaded_bytes / duration.total_seconds()), + speed=general_speed, downloads=feedbacks, )