Skip to content

Commit 75ea3d2

Browse files
committed
perf: only sort newly added dates rather than whole historic usage
1 parent d5579a0 commit 75ea3d2

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/main.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,24 @@ def get_and_update_historic_usage(
9191

9292
# Append the new usage data to the existing historic usage data
9393
dates_added = []
94+
new_usage_data = []
9495

9596
for day in usage_data:
9697
if not any(d["day"] == day["day"] for d in historic_usage):
97-
historic_usage.append(day)
98+
new_usage_data.append(day)
9899
dates_added.append(day["day"])
99100
logger.info("Added data for day %s", day["day"])
100101

101-
sorted_historic_usage = sorted(historic_usage, key=lambda x: x["day"])
102+
historic_usage.extend(sorted(new_usage_data, key=lambda x: x["day"]))
102103

103104
if not write_data_locally:
104105
# Write the updated historic_usage to organisation_history.json
105-
update_s3_object(s3, BUCKET_NAME, OBJECT_NAME, sorted_historic_usage)
106+
update_s3_object(s3, BUCKET_NAME, OBJECT_NAME, historic_usage)
106107
else:
107108
local_path = f"output/{OBJECT_NAME}"
108109
os.makedirs("output", exist_ok=True)
109110
with open(local_path, "w", encoding="utf-8") as f:
110-
json.dump(sorted_historic_usage, f, indent=4)
111+
json.dump(historic_usage, f, indent=4)
111112
logger.info("Historic usage data written locally to %s (S3 skipped)", local_path)
112113

113114
logger.info(
@@ -117,7 +118,7 @@ def get_and_update_historic_usage(
117118
dates_added,
118119
)
119120

120-
return sorted_historic_usage, dates_added
121+
return historic_usage, dates_added
121122

122123

123124
def update_s3_object(
@@ -277,5 +278,5 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument, to
277278

278279
# Dev Only
279280
# Uncomment the following line to run the script locally
280-
# if __name__ == "__main__":
281-
# handler(None, None)
281+
if __name__ == "__main__":
282+
handler(None, None)

0 commit comments

Comments
 (0)