def get_day_dev_info(dev_id): dev_data_list = [] connection = Connection() return_dict = {} db = connection.lws collection = db.devices dev_data = collection.find({'devid':dev_id}) dev_only_dict = json.loads(mongo_dumps(dev_data)) #getting this months worth of data collection = db.tempData this_year = datetime.now().year this_month = datetime.now().month this_day = datetime.now().day #print this_day month_data = collection.find({"phid":dev_id,"y":this_year,"month":this_month,"d":this_day,"s":0}) #month_data = collection.find({"phid":"402c8efa","y":this_year,"month":this_month}) for point in month_data: data_dict = json.loads(mongo_dumps(point)) for k in data_dict.keys(): if k =='_id': del data_dict[k] #print 'appending: %s'%data_dict dev_data_list.append(data_dict) return_dict['dev_info'] = dev_only_dict return_dict['data_list'] = dev_data_list #print dev_only_dict #dev_dict['month_info']=month_data #print month_data return return_dict
def get_data_timespan_db_date_query(dev_id,start_date,end_date,celc): return_list=[] connection = Connection() db = connection.lws collection = db.tempData start_date = parse_date(start_date) end_date = parse_date(end_date) #import time #start = time.time() #db.tempData.find({"h":{$gte:1,$lte:10},"min":{$gte:0,$lte:1},"s":{$gte:0,$lte:10}}).count() start_date = datetime.datetime(start_date['year'],start_date['month'],start_date['day'],start_date['hour'],start_date['min'],start_date['second']) end_date = datetime.datetime(end_date['year'],end_date['month'],end_date['day'],end_date['hour'],end_date['min'],end_date['second']) print start_date print end_date import lws_server_sensor_config db_data = collection.find({"phid":dev_id,"datetime":{"$gte":start_date,"$lt":end_date}}) return_list=[] #print time.time() - start if db_data.count()==0: print 'no data' return 0 else: for thing in db_data: thing = json.loads(mongo_dumps(thing)) thing['sensor_data'] = lws_server_sensor_config.transpose_to_strings(thing['sensor_data']) thing['sensor_data'] = lws_server_sensor_config.convert_to_real_values(thing['sensor_data'],celc,True) return_list.append(thing) #print thing print db_data.count() #return return json.dumps(return_list)
def get_last_checkin(devid): connection = Connection() db = connection.lws collection = db.deviceCheckIn checkin = {} for val in collection.find({'phid':devid}): checkin = json.loads(mongo_dumps(val)) return checkin
def get_response(devid): connection = Connection() db = connection.lws collection = db.response data = collection.find({'devid':devid}) if data.count() == 0: return 0 else: response = mongo_dumps(data) collection.remove({'devid':devid}) return response
def auth_user(username, password): auth = False collection = database.userPeople results = collection.find({"username":username}) for thing in results: results = json.loads(mongo_dumps(thing)) print results hash = results['password'] if bcrypt.hashpw(password, hash) == hash and results['active']: auth = True return auth
def get_current_stats(dev_id,min_offset): dev_data_list = [] connection = Connection() return_dict = {} db = connection.lws collection = db.devices dev_data = collection.find({'devid':dev_id}) for thing in dev_data: dev_only_dict = json.loads(mongo_dumps(thing)) #getting this months worth of data last_checkin = get_last_checkin(dev_id) db = connection.lws collection = db.tempData this_year = last_checkin['y'] this_month = last_checkin['month'] this_day = last_checkin['d'] this_hour = last_checkin['h'] this_minute = last_checkin['min'] this_second = last_checkin['s'] #print this_minute current_data = collection.find({"phid":dev_id,"y":this_year,"month":this_month,"d":this_day,"h":this_hour,"min":this_minute}).limit(1) #month_data = collection.find({"phid":"402c8efa","y":this_year,"month":this_month}) if current_data.count() == 0: print 'len is zero!' return get_current_stats(dev_id,1) else: for thing in current_data: data_dict = mongo_dumps(thing) data_dict = json.loads(data_dict) #print testa['sensor_data'] return_dict['dev_info'] = dev_only_dict return_dict['data_list'] = data_dict return return_dict
def dump_value_data(dev_id): connection = Connection() db = connection.lws collection = db.tempData data = collection.find() all_data = [] for point in data: data_dict = json.loads(mongo_dumps(point)) for k in data_dict.keys(): if k == '_id': del data_dict[k] all_data.append(data_dict) return all_data
def list_all_devices(): all_devices = [] connection = Connection() db = connection.lws collection = db.devices devices = collection.find() for device in devices: temp_dict = json.loads(mongo_dumps(device)) for k in temp_dict.keys(): if k == '_id': del temp_dict[k] all_devices.append(temp_dict) return all_devices
def export_to_s3_as_json(s3_client, mongo_client, named_table: str): """ Orchestrates export steps, encoding, and adds performance timer to logging """ export_time = time.time() # Serialize records as JSON bytestream table = mongo_client[named_table] records: bytes = mongo_dumps(table.find()).encode('utf-8') # Write JSON to S3 file_name: str = f'store_json/{to_snake_case(named_table)}.json' s3_client.put_object(Bucket=Clients.S3_BUCKET_NAME, Key=file_name, Body=records) print(f'Writing to S3: \t\t S3://{Clients.S3_BUCKET_NAME}/{file_name}') print(f'completed in \t\t {time.time() - export_time:.4f} seconds. \n')
connection = Connection() from_db = connection.lws to_db = connection.lws_date from_collection = from_db.tempData to_collection = to_db.tempData from_db_data = from_collection.find() print from_db_data.count() start = time.time() count = 0 for thing in from_db_data: if count == 1000: break else: #count +=1 new_data = json.loads(mongo_dumps(thing)) #date_obj = datetime.date(new_data['y'],new_data['month'],new_data['d']) try: print new_data date_time_obj = datetime.datetime(new_data['y'], new_data['month'], new_data['d'], new_data['h'], new_data['min'], new_data['s']) date_obj = datetime.datetime(new_data['y'], new_data['month'], new_data['d']) #print date_time_obj new_data['datetime'] = date_time_obj new_data['date'] = date_obj to_collection.insert(new_data) except: print 'exception'
def json(self, error=False) -> FlaskResponse: # https://stackoverflow.com/a/27024423/4694834 data = json.loads(mongo_dumps(self.data)) if isinstance( self.data, Cursor) else self.data data = {'message': data} if error else data return jsonify(data)
def json(self, error=False) -> FlaskResponse: data = json.loads(mongo_dumps(self.data)) if isinstance( self.data, Cursor) else self.data return jsonify(data)
def list_user_from_admin(user): response.content_type='application/json' db = get_database_connection() # conecta com a base de dados e armazena a conexao em db. users = db.users.find() return mongo_dumps(users)
def list_user_from_admin(user): response.content_type = 'application/json' db = get_database_connection() users = db.users.find() return mongo_dumps(users)
connection = Connection() from_db = connection.lws to_db = connection.lws_date from_collection = from_db.tempData to_collection = to_db.tempData from_db_data = from_collection.find() print from_db_data.count() start = time.time() count = 0 for thing in from_db_data: if count == 1000: break else: # count +=1 new_data = json.loads(mongo_dumps(thing)) # date_obj = datetime.date(new_data['y'],new_data['month'],new_data['d']) try: print new_data date_time_obj = datetime.datetime( new_data["y"], new_data["month"], new_data["d"], new_data["h"], new_data["min"], new_data["s"] ) date_obj = datetime.datetime(new_data["y"], new_data["month"], new_data["d"]) # print date_time_obj new_data["datetime"] = date_time_obj new_data["date"] = date_obj to_collection.insert(new_data) except: print "exception" # print date_obj