From e681ea49ae3bf528cc779a289fe48964432dcdf6 Mon Sep 17 00:00:00 2001
From: Vincent Pelletier <vincent@nexedi.com>
Date: Fri, 11 Jul 2008 15:14:53 +0000
Subject: [PATCH] Avoid scanning the whole list of active processes related to
 an alarm when we just want the most recent one. When alarm is currently
 runing, skip latest active result to display the previous one: this way, we
 display the result of a complete run. This is more consistent for alarms
 checking big quantities of documents in multiple activities and only
 displaying an error when one is actualy encountered (an error at the end of a
 processing batch would be hidden when the next batch starts processing).

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@22456 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/Document/Alarm.py | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/product/ERP5/Document/Alarm.py b/product/ERP5/Document/Alarm.py
index 614bfaa0c9..4a7772f216 100644
--- a/product/ERP5/Document/Alarm.py
+++ b/product/ERP5/Document/Alarm.py
@@ -331,6 +331,7 @@ class Alarm(XMLObject, PeriodicityMixin):
 
     The process parameter can be used to retrive sense values for 
     past processes.
+    If it is None, it will return the status of last completed active result.
     """
     method_id = self.getSenseMethodId()
     if process is None:
@@ -479,16 +480,16 @@ Alarm URL: %s
     This returns the last active process finished. So it will
     not returns the current one
     """
-    active_process_list = self.getCausalityRelatedValueList(
-                                  portal_type='Active Process')
-
-    def sort_date(a, b):
-      return cmp(a.getStartDate(), b.getStartDate())
-
-    active_process_list.sort(sort_date)
-    if len(active_process_list) > 0:
-      return active_process_list[-1]
-    return None
+    limit = self.isActive() and 2 or 1
+    active_process_list = self.getPortalObject().portal_catalog(
+      portal_type='Active Process', limit=limit,
+      sort_on=(('creation_date', 'DESC'), ),
+      causality_uid=self.getUid())
+    if len(active_process_list) < limit:
+      process = None
+    else:
+      process = active_process_list[-1].getObject()
+    return process
 
   security.declareProtected(Permissions.ModifyPortalContent, 
                             'newActiveProcess')
-- 
2.30.9