@@ -215,16 +215,21 @@ def _count_by_node(csv_path: Path) -> dict[str, int]:
215215 return counts
216216
217217
218- def generate_metrics_from_csvs (csv_files : dict [str , Path ]) -> str :
219- """Generate metrics summary markdown from CSV files."""
220- mb = _count_by_node (csv_files ["missing_bundles" ])
221- mb_latest = _count_by_node (csv_files ["missing_bundles_latest" ])
222- mb_superseded = _count_by_node (csv_files ["missing_bundles_superseded" ])
223- mc = _count_by_node (csv_files ["missing_collections" ])
224- mc_latest = _count_by_node (csv_files ["missing_collections_latest" ])
225- mc_superseded = _count_by_node (csv_files ["missing_collections_superseded" ])
226- sb = _count_by_node (csv_files ["staged_bundles" ])
227- sc = _count_by_node (csv_files ["staged_collections" ])
218+ def _load_all_counts (csv_files : dict [str , Path ]) -> dict [str , dict [str , int ]]:
219+ """Load node→count mappings for all CSV files at once, reading each file only once."""
220+ return {key : _count_by_node (path ) for key , path in csv_files .items ()}
221+
222+
223+ def generate_metrics_from_csvs (counts : dict [str , dict [str , int ]]) -> str :
224+ """Generate metrics summary markdown from pre-computed node→count mappings."""
225+ mb = counts ["missing_bundles" ]
226+ mb_latest = counts ["missing_bundles_latest" ]
227+ mb_superseded = counts ["missing_bundles_superseded" ]
228+ mc = counts ["missing_collections" ]
229+ mc_latest = counts ["missing_collections_latest" ]
230+ mc_superseded = counts ["missing_collections_superseded" ]
231+ sb = counts ["staged_bundles" ]
232+ sc = counts ["staged_collections" ]
228233
229234 all_nodes = sorted (
230235 set (mb ) | set (mc ) | set (sb ) | set (sc )
@@ -301,25 +306,25 @@ def generate_metrics_from_csvs(csv_files: dict[str, Path]) -> str:
301306)
302307
303308
304- def append_history_row (history_file : Path , csv_files : dict [str , Path ]) -> None :
309+ def append_history_row (history_file : Path , counts : dict [str , dict [ str , int ] ]) -> None :
305310 """Append one dated row of aggregate counts to the history CSV.
306311
307312 If the file does not yet exist, a header row is written first. The file is
308313 never truncated — only appended to — so historical data accumulates over time.
309314 """
310- def total (path : Path ) -> int :
311- return sum (_count_by_node ( path ) .values ())
315+ def total (key : str ) -> int :
316+ return sum (counts [ key ] .values ())
312317
313318 row = "," .join ([
314319 datetime .now (timezone .utc ).strftime ("%Y-%m-%d" ),
315- str (total (csv_files [ "missing_bundles" ] )),
316- str (total (csv_files [ "missing_bundles_latest" ] )),
317- str (total (csv_files [ "missing_bundles_superseded" ] )),
318- str (total (csv_files [ "missing_collections" ] )),
319- str (total (csv_files [ "missing_collections_latest" ] )),
320- str (total (csv_files [ "missing_collections_superseded" ] )),
321- str (total (csv_files [ "staged_bundles" ] )),
322- str (total (csv_files [ "staged_collections" ] )),
320+ str (total ("missing_bundles" )),
321+ str (total ("missing_bundles_latest" )),
322+ str (total ("missing_bundles_superseded" )),
323+ str (total ("missing_collections" )),
324+ str (total ("missing_collections_latest" )),
325+ str (total ("missing_collections_superseded" )),
326+ str (total ("staged_bundles" )),
327+ str (total ("staged_collections" )),
323328 ])
324329
325330 write_header = not history_file .exists ()
@@ -585,8 +590,11 @@ def main() -> int:
585590 }
586591 readme_path = output_dir / "README.md"
587592
593+ # Load all node→count mappings once; reuse for both README metrics and history row.
594+ counts = _load_all_counts (csv_files )
595+
588596 try :
589- metrics_markdown = generate_metrics_from_csvs (csv_files )
597+ metrics_markdown = generate_metrics_from_csvs (counts )
590598 update_readme_metrics (readme_path , metrics_markdown )
591599 print_info (f"Successfully updated metrics in { readme_path } " )
592600 output_files .append (readme_path )
@@ -597,7 +605,7 @@ def main() -> int:
597605 # Append a snapshot row to the history file for burndown tracking
598606 history_file = output_dir / "counts_history.csv"
599607 try :
600- append_history_row (history_file , csv_files )
608+ append_history_row (history_file , counts )
601609 print_info (f"Appended counts snapshot to { history_file } " )
602610 output_files .append (history_file )
603611 except Exception as e :
0 commit comments