From 5a95094943df09048d4359c2cc0e7aed530c7bd3 Mon Sep 17 00:00:00 2001 From: Pat Thoyts Date: Mon, 16 May 2016 12:58:41 +0100 Subject: [PATCH] Added test functions to look into using indexes with mongodb. --- sensordata.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sensordata.py b/sensordata.py index 813bf5a..42d84d5 100644 --- a/sensordata.py +++ b/sensordata.py @@ -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 -- 2.23.0