Skip to content

Commit 69222a3

Browse files
authored
Add a flag controlling if we should show download stats in staleness report (#651)
1 parent b5c4641 commit 69222a3

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

src/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,11 @@ def get_arg_parser():
424424
required=True,
425425
help="Specify the base patch version for which the package staleness report needs to be " "generated.",
426426
)
427+
package_staleness_parser.add_argument(
428+
"--download-stats",
429+
action="store_true",
430+
help="Specify if download count stats should be included in the package staleness report.",
431+
)
427432
package_size_parser = subparsers.add_parser(
428433
"generate-size-report",
429434
help="Generates toatl image size and package size report for each of the packages in the given "

src/package_report.py

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,20 @@ def _get_package_versions_in_upstream(target_packages_match_spec_out, target_ver
6464

6565

6666
def _generate_staleness_report_per_image(
67-
package_versions_in_upstream, target_packages_match_spec_out, image_config, version
67+
package_versions_in_upstream, target_packages_match_spec_out, image_config, version, download_stats
6868
):
6969
print("\n# Staleness Report: " + str(version) + "(" + image_config["image_type"] + ")\n")
7070
staleness_report_rows = []
7171

72-
# Get conda download statistics for all installed packages
73-
# Use the month before last to get full month of data
74-
previous_month = (datetime.now() - relativedelta(months=2)).strftime("%Y-%m")
75-
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)
72+
if download_stats:
73+
# Get conda download statistics for all installed packages
74+
# Use the month before last to get full month of data
75+
previous_month = (datetime.now() - relativedelta(months=2)).strftime("%Y-%m")
76+
pkg_list = list(package_versions_in_upstream.keys())
77+
# Suppress FutureWarning from pandas so it doesn't show in report
78+
with warnings.catch_warnings():
79+
warnings.filterwarnings("ignore", category=FutureWarning)
80+
conda_download_stats = overall(pkg_list, month=previous_month)
8081

8182
for package in package_versions_in_upstream:
8283
version_in_sagemaker_distribution = str(target_packages_match_spec_out[package].get("version")).removeprefix(
@@ -88,38 +89,44 @@ def _generate_staleness_report_per_image(
8889
else "${\color{red}" + package + "}$"
8990
)
9091

91-
# Get download count with error handling
92-
try:
93-
download_count = conda_download_stats[package]
94-
except (KeyError, TypeError):
95-
download_count = 0
92+
if download_stats:
93+
# Get download count with error handling
94+
try:
95+
download_count = conda_download_stats[package]
96+
except (KeyError, TypeError):
97+
download_count = 0
9698

97-
staleness_report_rows.append(
98-
{
99-
"package": package_string,
100-
"version_in_sagemaker_distribution": version_in_sagemaker_distribution,
101-
"latest_relavant_version": package_versions_in_upstream[package],
102-
"downloads": download_count,
103-
}
104-
)
99+
staleness_report_rows.append(
100+
{
101+
"package": package_string,
102+
"version_in_sagemaker_distribution": version_in_sagemaker_distribution,
103+
"latest_relavant_version": package_versions_in_upstream[package],
104+
"downloads": download_count,
105+
}
106+
)
107+
else:
108+
staleness_report_rows.append(
109+
{
110+
"package": package_string,
111+
"version_in_sagemaker_distribution": version_in_sagemaker_distribution,
112+
"latest_relavant_version": package_versions_in_upstream[package],
113+
}
114+
)
105115

106-
staleness_report_rows.sort(
107-
key=lambda x: (
108-
not x["package"].startswith("${\\color"), # Stale packages at top of list
109-
-x["downloads"], # Sorted by downloads
110-
)
111-
)
112-
print(
113-
create_markdown_table(
114-
[
115-
"Package",
116-
"Current Version in the Distribution image",
117-
"Latest Relevant Version in " "Upstream",
118-
"Downloads (Conda, previous month)",
119-
],
120-
staleness_report_rows,
116+
markdown_table_columns = [
117+
"Package",
118+
"Current Version in the Distribution image",
119+
"Latest Relevant Version in " "Upstream",
120+
]
121+
if download_stats:
122+
markdown_table_columns.append("Downloads (Conda, previous month)")
123+
staleness_report_rows.sort(
124+
key=lambda x: (
125+
not x["package"].startswith("${\\color"), # Stale packages at top of list
126+
-x["downloads"], # Sorted by downloads
127+
)
121128
)
122-
)
129+
print(create_markdown_table(markdown_table_columns, staleness_report_rows))
123130

124131

125132
def _get_installed_package_versions_and_conda_versions(
@@ -288,7 +295,11 @@ def generate_package_staleness_report(args):
288295
latest_package_versions_in_upstream,
289296
) = _get_installed_package_versions_and_conda_versions(image_config, target_version_dir, target_version)
290297
_generate_staleness_report_per_image(
291-
latest_package_versions_in_upstream, target_packages_match_spec_out, image_config, target_version
298+
latest_package_versions_in_upstream,
299+
target_packages_match_spec_out,
300+
image_config,
301+
target_version,
302+
args.download_stats,
292303
)
293304

294305

0 commit comments

Comments
 (0)