From 608e64e8b8ad61a76b5d7b9d74cc5b2af8aac194 Mon Sep 17 00:00:00 2001
From: Joe Gordon <joe.gordon0@gmail.com>
Date: Fri, 6 Sep 2013 19:08:40 -0700
Subject: [PATCH] Merge duplicate bugs in recheckwatch

When a bug is marked as a duplicate move all the hits to the
duplicate_of bug and delete the duplicate bug.

This change should make it easier to keep recheckwatch easier to read
by removing duplicate bugs.  The fewer bugs on recheck the easier it is
for a patch author to figure out which one fits his issue.

Note: unmarking a bug as a duplicate will not undo this

Change-Id: I19b25bb9595330393e664e9499204083b18ab5c9
---
 modules/recheckwatch/files/recheckwatch | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/modules/recheckwatch/files/recheckwatch b/modules/recheckwatch/files/recheckwatch
index 51836c20e1..0dc7e23fee 100755
--- a/modules/recheckwatch/files/recheckwatch
+++ b/modules/recheckwatch/files/recheckwatch
@@ -53,6 +53,7 @@ class Bug(object):
         self.changes = []
         self.last_seen = None
         self.first_seen = None
+        self.duplicate_of = None
         self.update()
 
     def update(self):
@@ -62,6 +63,8 @@ class Bug(object):
         self.title = lpitem.title
         self.status = map(lambda x: x.status,
                           lpitem.bug_tasks)
+        if lpitem.duplicate_of:
+            self.duplicate_of = lpitem.duplicate_of.id
 
     def is_closed(self):
         closed = True
@@ -80,6 +83,10 @@ class Bug(object):
             self.changes.append(hit.change)
         self.last_seen = hit.ts
 
+    def addHits(self, hits):
+        for hit in hits:
+            self.addHit(hit)
+
 class Scoreboard(threading.Thread):
     def __init__(self, config):
         threading.Thread.__init__(self)
@@ -122,11 +129,22 @@ class Scoreboard(threading.Thread):
         bug = self.scores.get(bugno)
         if not bug:
             bug = Bug(bugno)
+        else:
+            bug.update()
         bug.addHit(hit)
         self.scores[bugno] = bug
         self.update()
 
     def update(self):
+        # Check for duplicate bugs
+        dupes = []
+        for bugno, bug in self.scores.items():
+            if bug.duplicate_of:
+                dupes.append(bugno)
+        for bugno in dupes:
+            self.scores[self.scores[bugno].duplicate_of].addHits(self.scores[bugno].hits)
+            del self.scores[bugno]
+
         # Remove bugs that haven't been seen in ages
         to_remove = []
         now = datetime.datetime.utcnow()