Added test functions to look into using indexes with mongodb.
authorPat Thoyts <patthoyts@users.sourceforge.net>
Mon, 16 May 2016 11:58:41 +0000 (12:58 +0100)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Mon, 16 May 2016 11:58:41 +0000 (12:58 +0100)
sensordata.py

index 813bf5a2f28a27c2531ada20841640f94b9d37cf..42d84d592ef84c92b7e0357f75dc48e95fd369a3 100644 (file)
@@ -8,6 +8,8 @@ Reads data from the ASCII log file and presents a collection
 from __future__ import print_function, absolute_import, division
 import sys, re, json, pymongo, unittest
 from pymongo import MongoClient
+from configparser import ConfigParser
+from timeit import timeit
 
 __all__ = ['SensorData','SensorDataIterator']
 __version__ = '1.0.0'
@@ -134,6 +136,26 @@ def tojson(filename, granularity = None):
         print(item)
     return 0
 
+def test_recent(*args, **kwargs):
+    from timeit import timeit
+    print(timeit('recent()', setup='from __main__ import recent', number=100))
+    print(recent())
+
+def recent(*args, **kwargs):
+    client = opendb()
+    db = client.sensorhub.sensorlog
+    db.create_index([("timestamp", pymongo.ASCENDING)]) # creates if not already present
+    cursor = db.find().sort([("timestamp", pymongo.DESCENDING)]).limit(1)
+    item = next(cursor)
+    return item
+    
+def opendb():
+    config = ConfigParser()
+    config.read('sensor-hub.config')
+    uri = config['database']['uri'].strip("\'")
+    client = MongoClient(uri)
+    return client
+
 def testing(*args, **kwargs):
     '''
     {"timestamp": "1460971800",
@@ -173,6 +195,8 @@ if __name__ == '__main__':
             tojson(*sys.argv[2:])
         elif sys.argv[1] == 'test':
             testing(*sys.argv[2:])
+        elif sys.argv[1] == 'recent':
+            test_recent(*sys.argv[2:])
         else:
             print("usage: SensorData test")
             1