From 57cda6ff60319f6a32ab988111eccac2f7a89367 Mon Sep 17 00:00:00 2001
From: Martijn Vermaat <martijn@vermaat.name>
Date: Thu, 21 Jul 2011 15:41:26 +0000
Subject: [PATCH] Creation timestamp for cache entries.

git-svn-id: https://humgenprojects.lumc.nl/svn/mutalyzer/branches/gbinfo-sync-branch@313 eb6bd6ab-9ccd-42b9-aceb-e2899b4a52f1
---
 extras/post-install.sh        |  4 +++-
 extras/soap-tools/getcache.py | 10 +++++++++-
 mutalyzer/Db.py               | 13 +++++++++----
 mutalyzer/models.py           |  4 +++-
 mutalyzer/sync.py             |  9 +++++++--
 mutalyzer/webservice.py       |  9 +++++----
 6 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/extras/post-install.sh b/extras/post-install.sh
index 123f3850..b24e2c8b 100644
--- a/extras/post-install.sh
+++ b/extras/post-install.sh
@@ -303,9 +303,11 @@ CREATE TABLE GBInfo (
   ChrStop int(12) DEFAULT NULL,
   orientation int(2) DEFAULT NULL,
   url char(255) DEFAULT NULL,
+  created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (AccNo),
   UNIQUE KEY hash (hash),
-  UNIQUE KEY alias (GI)
+  UNIQUE KEY alias (GI),
+  INDEX (created)
 );
 CREATE TABLE Link (
   mrnaAcc char(20) NOT NULL,
diff --git a/extras/soap-tools/getcache.py b/extras/soap-tools/getcache.py
index 9e44d31f..fc8cd298 100755
--- a/extras/soap-tools/getcache.py
+++ b/extras/soap-tools/getcache.py
@@ -13,6 +13,7 @@ def _import_open_patched(self, *args, **kwargs):
 Import.open = _import_open_patched
 
 import sys
+from datetime import datetime, timedelta
 from suds.client import Client
 
 URL = 'http://localhost/mutalyzer/services/?wsdl'
@@ -20,9 +21,15 @@ URL = 'http://localhost/mutalyzer/services/?wsdl'
 c = Client(URL, cache=None)
 o = c.service
 
+days = 1
+if len(sys.argv) > 1:
+    days = int(sys.argv[1])
+
+created_since = datetime.today() - timedelta(minutes=days)
+
 print 'Getting cache...'
 
-cache = o.getCache()
+cache = o.getCache(created_since)
 
 if cache:
     for r in cache.CacheEntry:
@@ -40,4 +47,5 @@ if cache:
             print r.chromosomeOrientation
         if 'url' in r:
             print r.url
+        print r.created
         print
diff --git a/mutalyzer/Db.py b/mutalyzer/Db.py
index 8bc326e9..ce7b8e32 100644
--- a/mutalyzer/Db.py
+++ b/mutalyzer/Db.py
@@ -1033,20 +1033,25 @@ class Cache(Db) :
         return None
     #getGBFromGI
 
-    def getGB(self):
+    def getGB(self, created_since):
         """
-        Get all accession numbers from the cache.
+        Get all accession number entries starting with creation date
+        {created_since}.
 
         SQL tables from internalDb:
             - GBInfo ; Information about cached and uploaded GenBank files.
 
+        @arg created_since: Only entries with later creation dates are returned.
+        @type created_since: datatime.datetime
+
         @return: The accession number
         @rtype: string
         """
         statement = """
             SELECT *
-              FROM GBInfo;
-        """, None
+            FROM GBInfo
+            WHERE created >= %s;
+        """, created_since
 
         return self.query(statement)
     #getGB
diff --git a/mutalyzer/models.py b/mutalyzer/models.py
index c6fba15c..d8b00138 100644
--- a/mutalyzer/models.py
+++ b/mutalyzer/models.py
@@ -17,7 +17,7 @@ Additional attributes values for the soaplib String model:
 """
 
 
-from soaplib.core.model.primitive import String, Integer, Boolean
+from soaplib.core.model.primitive import String, Integer, Boolean, DateTime
 from soaplib.core.model.clazz import ClassModel, Array
 
 from mutalyzer import SOAP_NAMESPACE
@@ -31,6 +31,7 @@ class Mandatory(object):
     String = String(min_occurs=1, nillable=False)
     Integer = Integer(min_occurs=1, nillable=False)
     Boolean = Boolean(min_occurs=1, nillable=False)
+    DateTime = DateTime(min_occurs=1, nillable=False)
 #Mandatory
 
 
@@ -231,4 +232,5 @@ class CacheEntry(ClassModel):
     chromosomeStop = Integer
     chromosomeOrientation = Integer
     url = String
+    created = Mandatory.DateTime
 #CacheEntry
diff --git a/mutalyzer/sync.py b/mutalyzer/sync.py
index 2ece6e61..b99cf116 100644
--- a/mutalyzer/sync.py
+++ b/mutalyzer/sync.py
@@ -3,6 +3,9 @@ Module for synchronizing the database with other Mutalyzer instances.
 """
 
 
+from datetime import datetime, timedelta
+
+
 class CacheSync(object):
     """
     Todo.
@@ -14,8 +17,10 @@ class CacheSync(object):
         self._config = config
         self._database = database
 
-    def local_cache(self):
+    def local_cache(self, created_since=None):
         """
         Todo.
         """
-        return self._database.getGB()
+        if not created_since:
+            created_since = datetime.today() - timedelta(days=7)
+        return self._database.getGB(created_since)
diff --git a/mutalyzer/webservice.py b/mutalyzer/webservice.py
index 6798ba02..720ba445 100644
--- a/mutalyzer/webservice.py
+++ b/mutalyzer/webservice.py
@@ -23,7 +23,7 @@ import logging; logging.basicConfig()
 from soaplib.core import Application
 from soaplib.core.service import soap
 from soaplib.core.service import DefinitionBase
-from soaplib.core.model.primitive import String, Integer
+from soaplib.core.model.primitive import String, Integer, DateTime
 from soaplib.core.model.clazz import Array
 from soaplib.core.model.exception import Fault
 from soaplib.core.server import wsgi
@@ -882,8 +882,8 @@ class MutalyzerService(DefinitionBase):
         return result
     #info
 
-    @soap(_returns = Array(CacheEntry))
-    def getCache(self):
+    @soap(DateTime, _returns = Array(CacheEntry))
+    def getCache(self, created_since=None):
         """
         Todo: documentation.
         """
@@ -895,7 +895,7 @@ class MutalyzerService(DefinitionBase):
         database = Db.Cache(self._config.Db)
         sync = CacheSync(self._config.Sync, database)
 
-        cache = sync.local_cache()
+        cache = sync.local_cache(created_since)
 
         def soap_cache_entry(entry):
             e = CacheEntry()
@@ -907,6 +907,7 @@ class MutalyzerService(DefinitionBase):
             e.chromosomeStop = entry[5]
             e.chromosomeOrientation = entry[6]
             e.url = entry[7]
+            e.created = entry[8]
             return e
 
         output.addMessage(__file__, -1, 'INFO',
-- 
GitLab