[Secure-testing-commits] r39406 - bin

Sebastien Delafond seb at moszumanska.debian.org
Mon Feb 1 22:33:23 UTC 2016


Author: seb
Date: 2016-02-01 22:33:23 +0000 (Mon, 01 Feb 2016)
New Revision: 39406

Modified:
   bin/tracker_service.py
Log:
[tracker_service] Crude caching mechanism for JSON API

  * once computed, keep the JSON data cached in memory for 5
    minutes (default value) before regenerating it

  * at the very minimum we'll need to send along a Last-Modified header
    exposing when the data was last generated

Modified: bin/tracker_service.py
===================================================================
--- bin/tracker_service.py	2016-02-01 22:20:54 UTC (rev 39405)
+++ bin/tracker_service.py	2016-02-01 22:33:23 UTC (rev 39406)
@@ -7,6 +7,7 @@
 import security_db
 from web_support import *
 import json
+from datetime import datetime
 
 if __name__ == "__main__":
     if len(sys.argv) not in (3, 5):
@@ -123,9 +124,13 @@
         determining the values of these factors, but the rating itself comes
         from a fully automated formula.''')
 
+    json_generation_interval = 5 * 60 # in seconds
+
     def __init__(self, socket_name, db_name):
         webservice_base_class.__init__(self, socket_name)
         self.db = security_db.DB(db_name)
+        self.json_data = None # the JSON dump itself
+        self.json_timestamp = None # timestamp of JSON generation
         self.register('', self.page_home)
         self.register('*', self.page_object)
         self.register('redirect/*', self.page_redirect)
@@ -161,7 +166,7 @@
         self.register('data/fake-names', self.page_data_fake_names)
         self.register('data/pts/1', self.page_data_pts)
         # temorarly disabled on 2016-02-01 due to load on security-tracker
-        #self.register('data/json', self.page_json)
+        self.register('data/json', self.page_json)
         self.register('debsecan/**', self.page_debsecan)
         self.register('data/report', self.page_report)
         self.register('style.css', self.page_style_css)
@@ -1256,8 +1261,19 @@
             data.append('\n')
         return BinaryResult(''.join(data),'application/octet-stream')
 
-    def page_json(self, path, params, url):
+    def _get_json(self):
+        """Helper method handling basic caching of the JSON data, to avoid
+           overloading security-tracker.d.o. It'll return the cached
+           version of this data unless it's been generated more than
+           self.json_generation_interval seconds ago """
 
+        if self.json_timestamp: # we've generated the JSON at least once
+            delta = datetime.now() - self.json_timestamp
+            if delta.total_seconds() <= self.json_generation_interval:
+                # not expired yet, serve the cached data
+                return self.json_data
+
+        # if we reached here, the data has expired; let's regenerate it
         from collections import defaultdict
         packages = []
         issues = defaultdict(list)
@@ -1392,9 +1408,18 @@
 
                 data[pkg][issue]=pkg_issue
 
-        json_data = json.dumps(data, separators=(',', ':'))
-        return BinaryResult(json_data,'application/octet-stream')
+        # store the JSON dump in memory, and update the generation
+        # timestamp before returning
+        self.json_data = json.dumps(data, separators=(',', ':'))
+        self.json_timestamp = datetime.now()
+        return self.json_data
 
+    def page_json(self, path, params, url):
+        # FIXME: we want to send along a Last-Modified header, whose
+        # value will be:
+        #   self.json_timestamp.strftime("%a, %d %b %Y %H:%M:%S ") + TZ
+        return BinaryResult(self._get_json(),'application/octet-stream')
+
     def page_debsecan(self, path, params, url):
         obj = '/'.join(path)
         data = self.db.getDebsecan(obj)




More information about the Secure-testing-commits mailing list