Skip to content

Commit 867f341

Browse files
committed
FIx Addressed comments
1 parent 0b73d97 commit 867f341

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

src/app.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,36 @@ def get_secret_manager_client() -> boto3.client:
4242
secret_manager = session.client("secretsmanager", secret_reigon)
4343
return secret_manager
4444

45-
def get_last_modified() -> dict:
46-
47-
client = get_s3_client()
45+
def get_last_modified(client: boto3.client, file_names: list[str]) -> dict:
46+
"""
47+
This function retrieves and returns the `LastModified` value for the specified set of files
48+
in a given S3 bucket.
4849
49-
file_names = ["repositories.json", "secret_scanning.json", "dependabot.json"]
50-
formatted_lm = {}
50+
Args:
51+
client (boto3.client): An initialized boto3 S3 client.
52+
file_names: A list fo string values.
5153
52-
for x in file_names:
53-
response = client.head_object(Bucket=bucket_name, Key=x)
54-
last_modified = response["LastModified"]
55-
file_key =x.split(".")[0]
56-
formatted_lm[file_key]=last_modified.strftime('%Y-%m-%d %H:%M:%S')
54+
Returns:
55+
dict: A dictionary containing formatted datetime values of the last modification date for each file.
56+
The keys are the base file names (without extension) and the values are the formatted datetime strings.
57+
58+
Raises:
59+
Exception: Propagates any exceptions not caught by the individual file processing.
60+
"""
5761

62+
last_modified_timestamps = {}
5863

59-
return formatted_lm
64+
for file in file_names:
65+
try:
66+
response = client.head_object(Bucket=bucket_name, Key=file)
67+
last_modified = response["LastModified"]
68+
file_key = file.split(".")[0]
69+
last_modified_timestamps[file_key] = last_modified.strftime('%Y-%m-%d %H:%M:%S')
70+
except Exception as e:
71+
# Handle the error for a specific file here.
72+
print(f"Error processing file {file}: {e}")
6073

74+
return last_modified_timestamps
6175

6276
@st.cache_data
6377
def load_repositories(_s3, load_date: datetime.date) -> pd.DataFrame | str:
@@ -231,7 +245,9 @@ def load_file(filename: str) -> dict:
231245
loading_date = loading_date[:-1] + "0"
232246

233247
s3 = get_s3_client()
234-
last_modified_values = get_last_modified()
248+
249+
file_names = ["repositories.json", "secret_scanning.json", "dependabot.json"]
250+
last_modified_values = get_last_modified(s3, file_names)
235251

236252
df_repositories = load_repositories(s3, loading_date)
237253
total_secret_alerts, oldest_secret_alert, df_secret_scanning = load_secret_scanning(s3, loading_date)
@@ -276,12 +292,12 @@ def load_file(filename: str) -> dict:
276292

277293
with repository_tab:
278294
rep_last_modified = last_modified_values["repositories"]
279-
colh, colt =st.columns([3,1])
295+
col1, col2 =st.columns([3,1])
280296

281-
with colh:
297+
with col1:
282298
st.header(":blue-background[Repository Analysis]")
283299

284-
with colt:
300+
with col2:
285301
st.write(f"#### Last Updated: {rep_last_modified}")
286302

287303

@@ -571,13 +587,13 @@ def load_file(filename: str) -> dict:
571587
# Secret Scanning Analysis Section
572588

573589
with secret_tab:
574-
colh, colt =st.columns([3,1])
590+
col1, col2 =st.columns([3,1])
575591
secrets_tab = last_modified_values["secret_scanning"]
576592

577-
with colh:
593+
with col1:
578594
st.header(":blue-background[Secret Scanning Analysis]")
579595

580-
with colt:
596+
with col2:
581597
st.write(f"#### Last Updated: {secrets_tab}")
582598

583599
st.write("Alerts open for more than 5 days.")
@@ -617,13 +633,13 @@ def load_file(filename: str) -> dict:
617633
# Dependabot Analysis Section
618634

619635
with dependabot_tab:
620-
colh, colt =st.columns([3,1])
636+
col1, col2 =st.columns([3,1])
621637
dependabot = last_modified_values["dependabot"]
622-
623-
with colh:
638+
639+
with col1:
624640
st.header(":blue-background[Dependabot Analysis]")
625641

626-
with colt:
642+
with col2:
627643
st.write(f"#### Last Updated: {dependabot}")
628644

629645
st.write("Alerts open for more than 5 days (Critical), 15 days (High), 60 days (Medium), 90 days (Low).")

0 commit comments

Comments
 (0)