From: Pat Thoyts Date: Mon, 16 May 2016 11:58:41 +0000 (+0100) Subject: Added test functions to look into using indexes with mongodb. X-Git-Url: https://privyetmir.co.uk/gitweb.cgi?a=commitdiff_plain;h=5a95094943df09048d4359c2cc0e7aed530c7bd3;p=spd%2Fsensor-hub.git Added test functions to look into using indexes with mongodb. --- 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