Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use total_seconds instead of seconds in the datetime #46

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/onc/modules/_MultiPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from time import time

import dateutil.parser
import humanize

from ._util import _formatDuration

Expand Down Expand Up @@ -38,11 +39,17 @@ def getAllPages(self, service: str, url: str, filters: dict):
)

pageCount = 1
pageEstimate = self._estimatePages(response, service, responseTime)
pageEstimate = self._estimatePages(response, service)
if pageEstimate > 0:
timeEstimate = _formatDuration(pageEstimate * responseTime)
print(f"Estimated approx. {pageEstimate} pages")
print(f"Estimated approx. {timeEstimate} to complete")
# Exclude the first page when calculating the time estimation
timeEstimate = _formatDuration((pageEstimate - 1) * responseTime)
print(
f"Downloading time for the first page: {humanize.naturaldelta(responseTime)}" # noqa: E501
)
print(f"Estimated approx. {pageEstimate} pages in total.")
print(
f"Estimated approx. {timeEstimate} to complete for the rest of the pages." # noqa: E501
)

# keep downloading pages until next is None
print("")
Expand Down Expand Up @@ -111,7 +118,7 @@ def _catenateData(self, response: object, nextResponse: object, service: str):
elif service == "archivefiles":
response["files"] += nextResponse["files"]

def _estimatePages(self, response: object, service: str, responseTime: float):
def _estimatePages(self, response: object, service: str):
"""
Estimate the number of pages the request will require.

Expand All @@ -127,16 +134,17 @@ def _estimatePages(self, response: object, service: str, responseTime: float):
if pageTimespan == 0:
return 0

# total timespan to cover
# total timespan to cover in the next parameter excluding the first page
totalBegin = dateutil.parser.parse(response["next"]["parameters"]["dateFrom"])
totalEnd = dateutil.parser.parse(response["next"]["parameters"]["dateTo"])
totalTimespan = totalEnd - totalBegin

# handle cases of very small timeframes
pageSeconds = max(pageTimespan.seconds, 1)
totalSeconds = totalTimespan.seconds
pageSeconds = max(pageTimespan.total_seconds(), 1)
totalSeconds = totalTimespan.total_seconds()

return math.ceil(totalSeconds / pageSeconds)
# plus one for the first page
return math.ceil(totalSeconds / pageSeconds) + 1

def _rowCount(self, response, service: str):
"""
Expand Down
Loading