Skip to content

Commit 4b4ee5f

Browse files
committed
Add local writes
1 parent 5ea8af3 commit 4b4ee5f

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/main.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,14 @@ def get_copilot_team_date(gh: github_api_toolkit.github_interface, page: int) ->
115115

116116

117117
def get_and_update_historic_usage(
118-
s3: boto3.client, gh: github_api_toolkit.github_interface
118+
s3: boto3.client, gh: github_api_toolkit.github_interface, write_data_locally: bool
119119
) -> tuple:
120120
"""Get and update historic usage data from GitHub Copilot.
121121
122122
Args:
123123
s3 (boto3.client): An S3 client.
124124
gh (github_api_toolkit.github_interface): An instance of the github_interface class.
125+
write_data_locally (bool): Whether to write data locally instead of to S3.
125126
126127
Returns:
127128
tuple: A tuple containing the updated historic usage data and a list of dates added.
@@ -155,18 +156,28 @@ def get_and_update_historic_usage(
155156
extra={"no_days_added": len(dates_added), "dates_added": dates_added},
156157
)
157158

158-
# Write the updated historic_usage to historic_usage_data.json
159-
update_s3_object(s3, BUCKET_NAME, OBJECT_NAME, historic_usage)
159+
if not write_data_locally:
160+
# Write the updated historic_usage to historic_usage_data.json
161+
update_s3_object(s3, BUCKET_NAME, OBJECT_NAME, historic_usage)
162+
else:
163+
local_path = f"local_data/{OBJECT_NAME}"
164+
os.makedirs("local_data", exist_ok=True)
165+
with open(local_path, "w", encoding="utf-8") as f:
166+
json.dump(historic_usage, f, indent=4)
167+
logger.info(
168+
"Historic usage data written locally to %s (S3 skipped)", local_path
169+
)
160170

161171
return historic_usage, dates_added
162172

163173

164-
def get_and_update_copilot_teams(s3: boto3.client, gh: github_api_toolkit.github_interface) -> list:
174+
def get_and_update_copilot_teams(s3: boto3.client, gh: github_api_toolkit.github_interface, write_data_locally: bool) -> list:
165175
"""Get and update GitHub Teams with Copilot Data.
166176
167177
Args:
168178
s3 (boto3.client): An S3 client.
169179
gh (github_api_toolkit.github_interface): An instance of the github_interface class.
180+
write_data_locally (bool): Whether to write data locally instead of to S3.
170181
171182
Returns:
172183
list: A list of GitHub Teams with Copilot Data.
@@ -193,7 +204,14 @@ def get_and_update_copilot_teams(s3: boto3.client, gh: github_api_toolkit.github
193204
extra={"no_teams": len(copilot_teams)},
194205
)
195206

196-
update_s3_object(s3, BUCKET_NAME, "copilot_teams.json", copilot_teams)
207+
if not write_data_locally:
208+
update_s3_object(s3, BUCKET_NAME, "copilot_teams.json", copilot_teams)
209+
else:
210+
local_path = "local_data/copilot_teams.json"
211+
os.makedirs("local_data", exist_ok=True)
212+
with open(local_path, "w", encoding="utf-8") as f:
213+
json.dump(copilot_teams, f, indent=4)
214+
logger.info("Copilot teams data written locally to %s (S3 skipped)", local_path)
197215

198216
return copilot_teams
199217

@@ -249,14 +267,15 @@ def create_dictionary(
249267
return list(existing_team_data_map.values())
250268

251269

270+
# TODO: refactor update_s3_object to accept write_data_locally to handle local writes logic
252271
def update_s3_object(
253272
s3_client: boto3.client,
254273
bucket_name: str,
255274
object_name: str,
256275
data: dict,
257-
write_data_locally: bool = False, # TODO write_data_locally
276+
write_data_locally: bool = False,
258277
) -> bool:
259-
"""Update an S3 object with new data.
278+
"""Update an S3 object with new data or write locally based on the flag.
260279
261280
Args:
262281
s3_client (boto3.client): The S3 client.
@@ -419,10 +438,10 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument
419438
logger.info("API Controller created")
420439

421440
# Copilot Usage Data (Historic)
422-
historic_usage, dates_added = get_and_update_historic_usage(s3, gh)
441+
historic_usage, dates_added = get_and_update_historic_usage(s3, gh, write_data_locally)
423442

424443
# GitHub Teams with Copilot Data
425-
copilot_teams = get_and_update_copilot_teams(s3, gh)
444+
copilot_teams = get_and_update_copilot_teams(s3, gh, write_data_locally)
426445

427446
logger.info("Getting history of each team identified previously")
428447

@@ -436,11 +455,19 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument
436455

437456
logger.info("Existing team history has %d entries", len(existing_team_history))
438457

439-
# Convert to dictionary for quick lookup
440-
updated_team_history = create_dictionary(gh, copilot_teams, existing_team_history)
458+
if not write_data_locally:
459+
# Convert to dictionary for quick lookup
460+
updated_team_history = create_dictionary(gh, copilot_teams, existing_team_history)
441461

442-
# Write updated team history to S3
443-
update_s3_object(s3, BUCKET_NAME, "teams_history.json", updated_team_history)
462+
# Write updated team history to S3
463+
update_s3_object(s3, BUCKET_NAME, "teams_history.json", updated_team_history)
464+
else:
465+
local_path = "local_data/teams_history.json"
466+
os.makedirs("local_data", exist_ok=True)
467+
updated_team_history = create_dictionary(gh, copilot_teams, existing_team_history)
468+
with open(local_path, "w", encoding="utf-8") as f:
469+
json.dump(updated_team_history, f, indent=4)
470+
logger.info("Team history written locally to %s (S3 skipped)", local_path)
444471

445472
logger.info(
446473
"Process complete",
@@ -459,5 +486,5 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument
459486

460487
# # Dev Only
461488
# # Uncomment the following line to run the script locally
462-
# if __name__ == "__main__":
463-
# handler(None, None)
489+
if __name__ == "__main__":
490+
handler(None, None)

0 commit comments

Comments
 (0)