← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~stevenk/launchpad/auditorclient-timeline into lp:launchpad

 

Steve Kowalik has proposed merging lp:~stevenk/launchpad/auditorclient-timeline into lp:launchpad.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~stevenk/launchpad/auditorclient-timeline/+merge/171942

Sprinkle auditor requests into the timeline, like memcache and the librarian do.
-- 
https://code.launchpad.net/~stevenk/launchpad/auditorclient-timeline/+merge/171942
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~stevenk/launchpad/auditorclient-timeline into lp:launchpad.
=== modified file 'lib/lp/services/auditor/client.py'
--- lib/lp/services/auditor/client.py	2013-06-27 04:12:11 +0000
+++ lib/lp/services/auditor/client.py	2013-06-28 00:56:29 +0000
@@ -9,12 +9,14 @@
     ]
 
 from auditorclient.client import Client
+from lazr.restful.utils import get_current_browser_request
 
 from lp.services.config import config
 from lp.services.enterpriseid import (
     enterpriseids_to_objects,
     object_to_enterpriseid,
     )
+from lp.services.timeline.requesttimeline import get_request_timeline
 
 
 class AuditorClient(Client):
@@ -23,10 +25,21 @@
         super(AuditorClient, self).__init__(
             config.auditor.host, config.auditor.port)
 
+    def __get_timeline_action(self, suffix, obj, operation, actorobj):
+        data = "Object: %s; Operation: %s, Actor: %s" % (
+            obj, operation, actorobj)
+        timeline = get_request_timeline(get_current_browser_request())
+        return timeline.start("auditor-%s" % suffix, data)
+
     def send(self, obj, operation, actorobj, comment=None, details=None):
-        return super(AuditorClient, self).send(
-            object_to_enterpriseid(obj), operation,
-            object_to_enterpriseid(actorobj), comment, details)
+        obj = object_to_enterpriseid(obj)
+        actorobj = object_to_enterpriseid(actorobj)
+        action = self.__get_timeline_action("send", obj, operation, actorobj)
+        try:
+            return super(AuditorClient, self).send(
+                obj, operation, actorobj, comment, details)
+        finally:
+            action.finish()
 
     def _convert_to_enterpriseid(self, obj):
         if isinstance(obj, (list, tuple)):
@@ -39,14 +52,19 @@
             obj = self._convert_to_enterpriseid(obj)
         if actorobj:
             actorobj = self._convert_to_enterpriseid(actorobj)
-        logs = super(AuditorClient, self).receive(
-            obj, operation, actorobj, limit)
-        # Process the actors and objects back from enterprise ids.
-        eids = set()
-        for entry in logs['log-entries']:
-            eids |= set([entry['actor'], entry['object']])
-        map_eids_to_obj = enterpriseids_to_objects(eids)
-        for entry in logs['log-entries']:
-            entry['actor'] = map_eids_to_obj.get(entry['actor'], None)
-            entry['object'] = map_eids_to_obj.get(entry['object'], None)
-        return logs['log-entries']
+        action = self.__get_timeline_action(
+            "receive", obj, operation, actorobj)
+        try:
+            logs = super(AuditorClient, self).receive(
+                obj, operation, actorobj, limit)
+            # Process the actors and objects back from enterprise ids.
+            eids = set()
+            for entry in logs['log-entries']:
+                eids |= set([entry['actor'], entry['object']])
+            map_eids_to_obj = enterpriseids_to_objects(eids)
+            for entry in logs['log-entries']:
+                entry['actor'] = map_eids_to_obj.get(entry['actor'], None)
+                entry['object'] = map_eids_to_obj.get(entry['object'], None)
+            return logs['log-entries']
+        finally:
+            action.finish()

=== modified file 'lib/lp/services/auditor/tests/test_client.py'
--- lib/lp/services/auditor/tests/test_client.py	2013-06-18 03:57:26 +0000
+++ lib/lp/services/auditor/tests/test_client.py	2013-06-28 00:56:29 +0000
@@ -3,7 +3,11 @@
 
 __metaclass__ = type
 
+from lazr.restful.utils import get_current_browser_request
+
 from lp.services.auditor.client import AuditorClient
+from lp.services.enterpriseid import object_to_enterpriseid
+from lp.services.timeline.requesttimeline import get_request_timeline
 from lp.testing import TestCaseWithFactory
 from lp.testing.layers import AuditorLayer
 
@@ -12,6 +16,12 @@
 
     layer = AuditorLayer
 
+    def assertAction(self, category, data):
+        timeline = get_request_timeline(get_current_browser_request())
+        action = timeline.actions[-1]
+        self.assertEqual(category, action.category)
+        self.assertEqual(data, action.detail)
+
     def test_send_and_receive(self):
         # We can use .send() and .receive() on AuditorClient to log.
         actor = self.factory.makePerson()
@@ -19,12 +29,18 @@
         client = AuditorClient()
         result = client.send(pu, 'packageupload-accepted', actor)
         self.assertEqual('Operation recorded.', result)
+        data = "Object: %s; Operation: packageupload-accepted, Actor: %s" % (
+                object_to_enterpriseid(pu), object_to_enterpriseid(actor))
+        self.assertAction('auditor-send', data)
         result = client.receive(obj=pu)
         del result[0]['date'] # Ignore the date.
         expected = [{
             u'comment': u'', u'details': u'', u'actor': actor,
             u'operation': u'packageupload-accepted', u'object': pu}]
         self.assertContentEqual(expected, result)
+        self.assertAction(
+            'auditor-receive', "Object: %s; Operation: None, Actor: None" % (
+                object_to_enterpriseid(pu)))
 
     def test_multiple_receive(self):
         # We can ask AuditorClient for a number of operations.