def Device_data_across_days(inputDeviceId, inputStartDate, inputEndDate): """ Retrieve data across days for a device per user input """ #split the date to retrieve days, months, years splitStartDate = inputStartDate.split("-") splitEndDate = inputEndDate.split("-") #create date object for start and end date startDate = date(int(splitStartDate[0]), int(splitStartDate[1]), int(splitStartDate[2])) endDate = date(int(splitEndDate[0]), int(splitEndDate[1]), int(splitEndDate[2]) + 1) #connect to cloudant database cloudantClient.connect() #initalize dataArray dataArray = [] #loop through dates for singleDate in daterange(startDate, endDate): #update database name with date databaseDate = singleDate.strftime("%Y-%m-%d") databaseName = dataset.Get_database() + databaseDate #connect to Cloudant database and retrieve data for the database endPoint = '{0}/{1}'.format(serviceURL, databaseName + "/_all_docs") params = {'include_docs': 'true'} response = cloudantClient.r_session.get(endPoint, params=params) data = response.json() #get length of rows rowsLength = len(data["rows"]) #loop through data for x in range(0, rowsLength): #if device id exists if("deviceId" in data["rows"][x]["doc"]): deviceID = data["rows"][x]["doc"]["deviceId"] #if deivceId matches user provided device ID, append data to dataArray if deviceID == inputDeviceId: timeStamp = data["rows"][x]["doc"]["timestamp"] activeClients = data["rows"][x]["doc"]["data"]["activeClients"] deviceCount = data["rows"][x]["doc"]["data"]["deviceCount"] connections = data["rows"][x]["doc"]["data"]["connections"] jsonData = {"deviceID": deviceID, "timeStamp": timeStamp, "activeClients": activeClients, "deviceCount": deviceCount , "connections": connections} dataArray.append(jsonData) #disconnect from cloudant db cloudantClient.disconnect() #return dataArray return dataArray
def Device_data_per_day(inputDeviceId, inputDate): """ Retrieve data for a day for a device per user input """ print("enter dev_data_per_day", file=sys.stderr) #update database name with the dataname databaseName = dataset.Get_database() + inputDate print(databaseName, file=sys.stderr) #connect to Cloudant database and retrieve data for the database cloudantClient.connect() endPoint = '{0}/{1}'.format(serviceURL, databaseName + "/_all_docs") params = {'include_docs': 'true'} response = cloudantClient.r_session.get(endPoint, params=params) data = response.json() print(data, file=sys.stderr) #initalize dataArray dataArray = [] #get length of rows rowsLength = len(data["rows"]) print("\n rowslength : "+str(rowsLength), file=sys.stderr) #loop through data for x in range(0, rowsLength): print("\n row : "+str(x),file=sys.stderr) #if device id exists if("deviceId" in data["rows"][x]["doc"]): deviceID = data["rows"][x]["doc"]["deviceId"] print("\n devID : "+str(deviceID), file=sys.stderr) #if deivceId matches user provided device ID, append data to dataArray if deviceID == inputDeviceId: timeStamp = data["rows"][x]["doc"]["timestamp"] print("\n tS : "+str(timeStamp), file=sys.stderr) #activeClients = data["rows"][x]["doc"]["data"]["activeClients"] activeClients = 4 print("\n activeClients : "+str(activeClients), file=sys.stderr) #deviceCount = data["rows"][x]["doc"]["data"]["deviceCount"] deviceCount = 2 print("\n devCount : "+str(deviceCount), file=sys.stderr) #connections = data["rows"][x]["doc"]["data"]["connections"] connections = rowsLength jsonData = {"deviceID": deviceID, "timeStamp": timeStamp, "activeClients": activeClients, "deviceCount": deviceCount , "connections": connections} dataArray.append(jsonData) #disconnect from cloudant db cloudantClient.disconnect() print(dataArray, file=sys.stderr) #return dataArray return dataArray
def Device_data_per_day(inputDeviceId, inputDate): """ Retrieve data for a day for a device per user input """ #update database name with the dataname databaseName = dataset.Get_database() + inputDate #connect to Cloudant database and retrieve data for the database cloudantClient.connect() endPoint = '{0}/{1}'.format(serviceURL, databaseName + "/_all_docs") params = {'include_docs': 'true'} response = cloudantClient.r_session.get(endPoint, params=params) data = response.json() #initalize dataArray dataArray = [] #get length of rows rowsLength = len(data["rows"]) #loop through data for x in range(0, rowsLength): #if device id exists if ("deviceId" in data["rows"][x]["doc"]): deviceID = data["rows"][x]["doc"]["deviceId"] #if deivceId matches user provided device ID, append data to dataArray if deviceID == inputDeviceId: timeStamp = data["rows"][x]["doc"]["timestamp"] bpm = data["rows"][x]["doc"]["data"]["BPM"] jsonData = { "deviceID": deviceID, "timeStamp": timeStamp, "BPM": bpm } dataArray.append(jsonData) #disconnect from cloudant db cloudantClient.disconnect() #return dataArray return dataArray