def getTimeData(dev, MAC): """ Returns current day metrics for a given MAC address by querying MongoDB """ # Creating a MongoDB client and switching to the relevant database database = parseDatabase('MongoDB') mongo_client = pymongo.MongoClient(database['host'], int(database['port'])) db = mongo_client[dev] time_data = [] date=dt.now().strftime("%Y-%m-%d") # Switching to the current day collection in MongoDB and getting the relevant document col = db[date] results = col.find({'MAC':MAC}) data = dict() for i in results: data = i if data=={}: return {'data':'Not found'} else: data.pop('_id', None) return data
def getRepeatMACS(dev, offset=5, date=datetime.datetime.today().strftime("%Y-%m-%d")): """ Returns the number of repeated MACS detected on a given date upto a specified offset. Example - if date = 2019-07-08 and offset = 5, the function will return the number of MAC addresses that were detected on 2019-07-08 as well as on any day between 2019-07-08 and 2019-07-03 """ database = parseDatabase('InfluxDB') influx_client = InfluxDBClient(host=database['host'], port=database['port']) influx_client.switch_database('datadump') # Generate Query string date = datetime.datetime.strptime(date, "%Y-%m-%d") offset_date = date - datetime.timedelta(days=offset) date = date.strftime("%Y-%m-%d") offset_date = offset_date.strftime("%Y-%m-%d") # Iterate over all unique MACS MACS = getUniqueMACS(dev, date) repeats = 0 new = 0 for MAC in MACS: results = influx_client.query( "select count(*) from {} where time >= '{}' and time < '{}' and MAC='{}'" .format(dev, offset_date, date, MAC['MAC'])) points = results.get_points() for point in points: if point['count_strength'] > 0: repeats += 1 else: new += 1 return {'date': date, 'count': repeats}
def mongoUpdate(date=datetime.now().strftime("%Y-%m-%d")): """ Updates MongoDB after processing the data captured on a given day """ # Creating client for MongoDB mongo = parseDatabase('MongoDB') mongo_client = pymongo.MongoClient(mongo['host'], int(mongo['port'])) for device in devices: MACS = getMACSFromInflux(device, date) local_db = mongo_client[device] collection = local_db[date] if MACS == []: continue for MAC in MACS: if MAC == 'nown)' or MAC.startswith('da:a1:19'): continue time_data = getTimeData(device, MAC, date) post = { 'MAC': MAC, 'First detection': time_data['first'], 'Last detection': time_data['last'], 'Duration': time_data['duration'], 'Last updated': datetime.now().strftime("%H:%M:%S") } if not collection.count_documents({'MAC': MAC}): post_id = collection.insert_one(post) else: post_id = collection.update_one({'MAC': MAC}, {"$set": post})
def getHistoricalData(dev, MAC): """ Returns a dict containing the duration for a given MAC address for the last 5 days """ # Creating a MongoDB client and switching to the relevant database database = parseDatabase('MongoDB') mongo_client = pymongo.MongoClient(database['host'], int(database['port'])) db = mongo_client[dev] # Creating an array consisting of the dates for which data has to be retrieved base = datetime.datetime.today() - datetime.timedelta(days=1) date_list = [base - datetime.timedelta(days=x) for x in range(0, 5)] data = [] # Loop to get data for the last five days for date in date_list: date_string = date.strftime("%Y-%m-%d") col = db[date_string] temp = dict() results = col.find({'MAC':MAC}) for i in results: temp = i if not temp=={}: temp['date'] = date_string temp.pop('_id', None) to_pop = ['First detection', 'Last detection', 'Number', 'MAC', 'Last updated'] for item in to_pop: temp.pop(item, None) data.append(temp) else: temp['date'] = date_string temp['Duration'] = 0 data.append(temp) continue return data
from influxdb import InfluxDBClient import pymongo from datetime import datetime, timedelta from dateutil import tz from timeloop import Timeloop from functions import parseDatabase, parseDevices tl = Timeloop() devices = parseDevices() # Creating an InfluxDB client and switching to the relevant database influx = parseDatabase('InfluxDB') influx_client = InfluxDBClient(host=influx['host'], port=influx['port']) influx_client.switch_database('datadump') def getMACSFromInflux(dev, date=datetime.today().strftime("%Y-%m-%d")): """ Returns a list of unique MACS detected on a given day by a given device """ MACS = [] date = datetime.strptime(date, '%Y-%m-%d') start = date.strftime("%Y-%m-%d") end = (date + timedelta(days=1)).strftime("%Y-%m-%d") query = "select * from {} where time>='{}' and time<'{}'".format( dev, start, end) results = influx_client.query(query) points = results.get_points() for point in points: