Skip to content

Commit 6d160f1

Browse files
author
Ruinong Tian
committed
improve download count logic
1 parent fe1755a commit 6d160f1

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/package_report.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,19 @@ def _generate_staleness_report_per_image(
7373
# Use previous month to get full month of data
7474
previous_month = (datetime.now() - relativedelta(months=1)).strftime("%Y-%m")
7575
pkg_list = list(package_versions_in_upstream.keys())
76-
# Suppress FutureWarning from pandas so it doesn't show in report
77-
with warnings.catch_warnings():
78-
warnings.filterwarnings("ignore", category=FutureWarning)
79-
conda_download_stats = overall(pkg_list, month=previous_month)
76+
77+
# Process packages individually to handle exceptions per package
78+
conda_download_stats = {}
79+
for pkg in pkg_list:
80+
try:
81+
# Suppress FutureWarning from pandas so it doesn't show in report
82+
with warnings.catch_warnings():
83+
warnings.filterwarnings("ignore", category=FutureWarning)
84+
# Get stats for single package
85+
pkg_stats = overall([pkg], month=previous_month)
86+
conda_download_stats[pkg] = pkg_stats.get(pkg, "N/A")
87+
except ValueError as e:
88+
conda_download_stats[pkg] = "N/A"
8089

8190
for package in package_versions_in_upstream:
8291
version_in_sagemaker_distribution = str(target_packages_match_spec_out[package].get("version")).removeprefix(
@@ -96,12 +105,23 @@ def _generate_staleness_report_per_image(
96105
}
97106
)
98107

99-
staleness_report_rows.sort(
100-
key=lambda x: (
101-
not x["package"].startswith("${\\color"), # Stale packages at top of list
102-
-x["downloads"], # Sorted by downloads
103-
)
104-
)
108+
def sort_key(x):
109+
# First key: False for stale packages (they start with "${\\color")
110+
is_current = not x["package"].startswith("${\\color")
111+
112+
# Second key: downloads count, with "N/A" treated as lowest priority
113+
downloads = x["downloads"]
114+
if downloads == "N/A":
115+
download_value = float('-inf') # Put N/A at the bottom
116+
else:
117+
try:
118+
download_value = float(downloads)
119+
except (ValueError, TypeError):
120+
download_value = float('-inf') # Handle any other non-numeric values
121+
122+
return (is_current, download_value)
123+
124+
staleness_report_rows.sort(key=sort_key, reverse=True)
105125
print(
106126
create_markdown_table(
107127
[

0 commit comments

Comments
 (0)