Skip to content

Commit cca1ac3

Browse files
committed
refactor: improve logging and use requests
1 parent 4e02c38 commit cca1ac3

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/main.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
for an organization. Data is retrieved from the GitHub API and stored in S3.
66
"""
77

8-
from itertools import count
98
import json
109
import logging
1110
import os
12-
from typing import Any, Optional
11+
from typing import Any
1312

1413
import boto3
1514
import github_api_toolkit
15+
import requests
1616
from botocore.exceptions import ClientError
17-
from requests import Response
18-
import urllib.request
1917

2018
# GitHub Organisation
2119
org = os.getenv("GITHUB_ORG")
@@ -74,8 +72,8 @@ def get_and_update_historic_usage(
7472
"""
7573
# Get the usage data
7674
api_response = gh.get(f"/orgs/{org}/copilot/metrics/reports/organization-28-day/latest").json()
77-
usage_data = json.loads(urllib.request.urlopen(api_response["download_links"][0]).read())["day_totals"]
78-
75+
usage_data = requests.get(api_response["download_links"][0], timeout=30).json()["day_totals"]
76+
7977
logger.info("Usage data retrieved")
8078

8179
try:
@@ -86,25 +84,15 @@ def get_and_update_historic_usage(
8684
historic_usage = []
8785

8886
dates_added = []
89-
count = 0
9087

9188
for day in usage_data:
9289
if not any(d["day"] == day["day"] for d in historic_usage):
9390
historic_usage.append(day)
9491
dates_added.append(day["day"])
9592
logger.info("Added data for day %s", day["day"])
96-
count += 1
97-
98-
logger.info("Total new days added: %d", count)
9993

10094
sorted_historic_usage = sorted(historic_usage, key=lambda x: x["day"])
10195

102-
logger.info(
103-
"New usage data added to %s",
104-
OBJECT_NAME,
105-
extra={"no_days_added": len(dates_added), "dates_added": dates_added},
106-
)
107-
10896
if not write_data_locally:
10997
# Write the updated historic_usage to historic_usage_data.json
11098
update_s3_object(s3, BUCKET_NAME, OBJECT_NAME, sorted_historic_usage)
@@ -115,6 +103,13 @@ def get_and_update_historic_usage(
115103
json.dump(sorted_historic_usage, f, indent=4)
116104
logger.info("Historic usage data written locally to %s (S3 skipped)", local_path)
117105

106+
logger.info(
107+
"Usage data written to %s: %d days added (%s)",
108+
OBJECT_NAME,
109+
len(dates_added),
110+
dates_added,
111+
)
112+
118113
return sorted_historic_usage, dates_added
119114

120115

@@ -232,7 +227,6 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument, to
232227
filename="debug.log",
233228
filemode="w",
234229
format="%(asctime)s %(levelname)s %(message)s",
235-
236230
)
237231
else:
238232
# Ensure INFO logs show in the terminal when not logging to a file
@@ -286,5 +280,5 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument, to
286280

287281
# Dev Only
288282
# Uncomment the following line to run the script locally
289-
# if __name__ == "__main__":
290-
# handler(None, None)
283+
if __name__ == "__main__":
284+
handler(None, None)

0 commit comments

Comments
 (0)