diff mbox series

[auh,v2,2/3] modules/statistics.py: rework and include first-seen statistics in status reports

Message ID 20261203212050.799982-2-alex@linutronix.de
State New
Headers show
Series [auh,v2,1/3] modules/state: rework state to include both original and updated version | expand

Commit Message

Alexander Kanavin Dec. 3, 2026, 9:20 p.m. UTC
This module's code quality wasn't great (non-pythonic code, excessive amount of unneeded class data,
producing a not particularly useful per-maintainer report), so I took the opportunity to clean it all
up. Per-maintainer section of the report is dropped altogether.

The status report now includes information about how long ago the current
and new versions were first seen. This helps identify recipes which are
falling behind upstream development: if either the current or new version
was first seen many days ago, then updating that recipe should be prioritized:

INFO: Recipe upgrade statistics:

    * Succeeded: 1
        gpgme, 2.2.0 (first seen 117 day(s) ago) -> 2.3.1 (first seen 117 day(s) ago), Unassigned

AI-Generated: Claude Sonnet 5.5
Signed-off-by: Alexander Kanavin <alex@linutronix.de>
---
 modules/state.py      |  14 +++++
 modules/statistics.py | 116 +++++++++++++++++++++---------------------
 upgrade-helper.py     |   2 +-
 3 files changed, 72 insertions(+), 60 deletions(-)
diff mbox series

Patch

diff --git a/modules/state.py b/modules/state.py
index 9a10d32..1f3aebf 100644
--- a/modules/state.py
+++ b/modules/state.py
@@ -131,3 +131,17 @@  class State:
         return {'skip': True,
                 'reason': "%slast attempt %s result=%s; will retry in %d day(s)"
                           % (prefix, when, result, retry_in)}
+
+    def _firstseen_days(self, pn, field, version):
+        """Days since 'version' was first seen in 'field' (oldversion or
+        newversion); 0 if there is no matching state entry."""
+        entry = self.data.get(pn)
+        if not entry or entry.get(field) != version:
+            return 0
+        return int((time.time() - entry[field + '_firstseen']) / SECONDS_PER_DAY)
+
+    def get_oldver_firstseen_days(self, pn, version):
+        return self._firstseen_days(pn, 'oldversion', version)
+
+    def get_newver_firstseen_days(self, pn, version):
+        return self._firstseen_days(pn, 'newversion', version)
diff --git a/modules/statistics.py b/modules/statistics.py
index 5126a39..6174cec 100644
--- a/modules/statistics.py
+++ b/modules/statistics.py
@@ -25,16 +25,10 @@ 
 
 class Statistics(object):
     def __init__(self):
-        self.succeeded = dict()
-        self.failed = dict()
-        self.succeeded["total"] = 0
-        self.failed["total"] = 0
         self.upgrade_stats = dict()
-        self.maintainers = set()
-        self.total_attempted = 0
         self.skipped = []
 
-    def _update(self, pn, new_ver, maintainer, error):
+    def _update(self, p, error):
         if error is None:
             status = "Succeeded"
         else:
@@ -43,85 +37,89 @@  class Statistics(object):
         if not status in self.upgrade_stats:
             self.upgrade_stats[status] = []
 
-        self.upgrade_stats[status].append((pn, new_ver, maintainer))
-
-        # add maintainer to the set of unique maintainers
-        self.maintainers.add(maintainer)
-
-        if not maintainer in self.succeeded:
-            self.succeeded[maintainer] = 0
-        if not maintainer in self.failed:
-            self.failed[maintainer] = 0
-
-        if status == "Succeeded":
-            self.succeeded["total"] += 1
-            self.succeeded[maintainer] += 1
-        else:
-            self.failed["total"] += 1
-            self.failed[maintainer] += 1
-
-        self.total_attempted += 1
+        self.upgrade_stats[status].append(p)
 
     def update(self, group):
         for p in group['pkgs']:
-            self._update(p['PN'],p['NPV'],p['MAINTAINER'],group['error'])
+            self._update(p, group['error'])
 
     def skip(self, group, reason):
         for p in group['pkgs']:
-            self.skipped.append((p['PN'],p['NPV'],p['MAINTAINER'],reason))
-
-    def _pkg_stats(self):
+            self.skipped.append((p, reason))
+
+    @property
+    def total_attempted(self):
+        return sum(len(v) for v in self.upgrade_stats.values())
+
+    @staticmethod
+    def _firstseen_days(state, p):
+        """(old version, new version) days since first seen; (0, 0) without state."""
+        if state is None:
+            return (0, 0)
+        return (state.get_oldver_firstseen_days(p['PN'], p['PV']),
+                state.get_newver_firstseen_days(p['PN'], p['NPV']))
+
+    @staticmethod
+    def _pkg_line(p, old_days, new_days, state, reason=None):
+        """One report line; first-seen ages are only shown when state is in use."""
+        if state is None:
+            line = "{}, {} -> {}".format(p['PN'], p['PV'], p['NPV'])
+        else:
+            line = "{}, {} (first seen {} day(s) ago) -> {} (first seen {} day(s) ago)".format(
+                p['PN'], p['PV'], old_days, p['NPV'], new_days)
+        if reason:
+            line += ", " + reason
+        if p['MAINTAINER']:
+            line += ", " + p['MAINTAINER']
+        return "        " + line + "\n"
+
+    def _pkg_stats(self, state):
         stat_msg = "Recipe upgrade statistics:\n\n"
         for status in self.upgrade_stats:
             list_len = len(self.upgrade_stats[status])
             if list_len > 0:
-                stat_msg += "    * " + status + ": " + str(list_len) + "\n"
+                stat_msg += "    * {}: {}\n".format(status, list_len)
 
-                for pkg, new_ver, maintainer in self.upgrade_stats[status]:
-                    stat_msg += "        " + pkg + ", " + new_ver
-                    if maintainer:
-                        stat_msg += ", " + maintainer
-                    stat_msg += "\n"
+                for p in self.upgrade_stats[status]:
+                    old_days, new_days = self._firstseen_days(state, p)
+                    stat_msg += self._pkg_line(p, old_days, new_days, state)
 
         if self.skipped:
             stat_msg += "    * Skipped: {}\n".format(len(self.skipped))
-            for (pkg, new_ver, maintainer, reason) in self.skipped:
-                stat_msg += "        {}, {}, {}, {}\n".format(pkg, new_ver, reason, maintainer)
 
-        if self.total_attempted == 0:
-            percent_succeded = 0
+            skipped = [(p, reason) + self._firstseen_days(state, p)
+                       for (p, reason) in self.skipped]
+            # Longest outstanding first
+            skipped.sort(key=lambda item: max(item[2], item[3]), reverse=True)
+
+            for (p, reason, old_days, new_days) in skipped:
+                stat_msg += self._pkg_line(p, old_days, new_days, state, reason)
+
+        attempted = self.total_attempted
+        succeeded = len(self.upgrade_stats.get('Succeeded', []))
+        failed = attempted - succeeded
+
+        if attempted == 0:
+            percent_succeeded = 0
             percent_failed = 0
         else:
-            percent_succeded = self.succeeded["total"] * 100.0 / self.total_attempted
-            percent_failed = self.failed["total"] * 100.0 / self.total_attempted
+            percent_succeeded = succeeded * 100.0 / attempted
+            percent_failed = failed * 100.0 / attempted
         stat_msg += "\n    TOTAL: attempted=%d succeeded=%d(%.2f%%) failed=%d(%.2f%%)\n\n" % \
-                    (self.total_attempted, self.succeeded["total"],
-                    percent_succeded,
-                    self.failed["total"],
+                    (attempted, succeeded,
+                    percent_succeeded,
+                    failed,
                     percent_failed)
 
         return stat_msg
 
-    def _maintainer_stats(self):
-        stat_msg = "Recipe upgrade statistics per Maintainer:\n\n"
-        for m in self.maintainers:
-            attempted = self.succeeded[m] + self.failed[m]
-            stat_msg += "    %s: attempted=%d succeeded=%d(%.2f%%) failed=%d(%.2f%%)\n" % \
-                        (m.split("@")[0] if m else "Maintainerless", attempted, self.succeeded[m],
-                        self.succeeded[m] * 100.0 / attempted,
-                        self.failed[m],
-                        self.failed[m] * 100.0 / attempted)
-
-        return stat_msg
-
-    def get_summary(self, publish_work_url, workdir):
+    def get_summary(self, publish_work_url, workdir, state):
         msg = ''
 
         if publish_work_url:
             msg += "AUH finished upgrade batch the result patches/logs can be found at:\n" \
                    "%s/%s, next are the statistics:\n\n" % (publish_work_url, workdir)
 
-        msg += self._pkg_stats()
-        msg += self._maintainer_stats()
+        msg += self._pkg_stats(state)
 
         return msg
diff --git a/upgrade-helper.py b/upgrade-helper.py
index 8e47e40..6997e89 100755
--- a/upgrade-helper.py
+++ b/upgrade-helper.py
@@ -622,7 +622,7 @@  class Updater(object):
                     attach_tarball = False
 
             statistics_summary = self.statistics.get_summary(
-                    publish_work_url, os.path.basename(self.uh_work_dir))
+                    publish_work_url, os.path.basename(self.uh_work_dir), self.state)
 
             statistics_file = os.path.join(self.uh_work_dir,
                     "statistics_summary")