Skip to content

Commit 476841f

Browse files
authored
Merge pull request #46 from OceanNetworksCanada/issue-45-fix-time-estimation
fix: use total_seconds instead of seconds in the datetime
2 parents 6859a94 + 2e17ca3 commit 476841f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/onc/modules/_MultiPage.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from time import time
55

66
import dateutil.parser
7+
import humanize
78

89
from ._util import _formatDuration
910

@@ -38,11 +39,17 @@ def getAllPages(self, service: str, url: str, filters: dict):
3839
)
3940

4041
pageCount = 1
41-
pageEstimate = self._estimatePages(response, service, responseTime)
42+
pageEstimate = self._estimatePages(response, service)
4243
if pageEstimate > 0:
43-
timeEstimate = _formatDuration(pageEstimate * responseTime)
44-
print(f"Estimated approx. {pageEstimate} pages")
45-
print(f"Estimated approx. {timeEstimate} to complete")
44+
# Exclude the first page when calculating the time estimation
45+
timeEstimate = _formatDuration((pageEstimate - 1) * responseTime)
46+
print(
47+
f"Downloading time for the first page: {humanize.naturaldelta(responseTime)}" # noqa: E501
48+
)
49+
print(f"Estimated approx. {pageEstimate} pages in total.")
50+
print(
51+
f"Estimated approx. {timeEstimate} to complete for the rest of the pages." # noqa: E501
52+
)
4653

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

114-
def _estimatePages(self, response: object, service: str, responseTime: float):
121+
def _estimatePages(self, response: object, service: str):
115122
"""
116123
Estimate the number of pages the request will require.
117124
@@ -127,16 +134,17 @@ def _estimatePages(self, response: object, service: str, responseTime: float):
127134
if pageTimespan == 0:
128135
return 0
129136

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

135142
# handle cases of very small timeframes
136-
pageSeconds = max(pageTimespan.seconds, 1)
137-
totalSeconds = totalTimespan.seconds
143+
pageSeconds = max(pageTimespan.total_seconds(), 1)
144+
totalSeconds = totalTimespan.total_seconds()
138145

139-
return math.ceil(totalSeconds / pageSeconds)
146+
# plus one for the first page
147+
return math.ceil(totalSeconds / pageSeconds) + 1
140148

141149
def _rowCount(self, response, service: str):
142150
"""

0 commit comments

Comments
 (0)