def _update_timeseries(self): """Insert data into the database "iset_data" table. Args: mapping: Map of DIDs to database row index values Returns: success: True if successful """ # Initialize key variables success = False data = self.agent_data['timeseries'] datapoints = self.datapoints data_list = [] timestamp_tracker = {} # Update data for item in data: # Process datapoint values id_datapoint = item['id_datapoint'] value = item['value'] timestamp = item['timestamp'] # Get data on datapoints idx_datapoint = datapoints[id_datapoint]['idx_datapoint'] last_timestamp = datapoints[id_datapoint]['last_timestamp'] # Only update with data collected after # the most recent DID update. Don't do anything more if timestamp > last_timestamp: data_list.append( Data( idx_datapoint=idx_datapoint, value=value, timestamp=timestamp ) ) # Update DID's last updated timestamp if idx_datapoint in timestamp_tracker: timestamp_tracker[idx_datapoint] = max( timestamp, timestamp_tracker[idx_datapoint]) else: timestamp_tracker[idx_datapoint] = timestamp # Update if there is data if bool(data_list) is True: # Do performance data update database = db.Database() success = database.add_all(data_list, 1056, die=False) success = True # Return return success
def __init__(self): """Method initializing the class.""" # Initialize key variables self.data = {} self.data['idx_datapoint'] = 1 self.data['idx_agentname'] = 1 self.data['idx_billcode'] = 1 self.data['idx_device'] = 1 self.data['idx_deviceagent'] = 1 self.data['idx_department'] = 1 self.data['timestamp'] = general.normalized_timestamp() self.data['last_timestamp'] = general.normalized_timestamp() self.data['devicename'] = general.hashstring(general.randomstring()) self.data['id_agent'] = general.hashstring(general.randomstring()) self.data['id_datapoint'] = general.hashstring(general.randomstring()) self.data['devicename'] = general.hashstring(general.randomstring()) self.data['device_description'] = general.hashstring( general.randomstring()) self.data['agent'] = general.hashstring(general.randomstring()) self.data['agent_source'] = general.hashstring(general.randomstring()) self.data['agent_label'] = general.hashstring(general.randomstring()) self.data['department_code'] = general.hashstring( general.randomstring()) self.data['department_name'] = general.hashstring( general.randomstring()) self.data['billcode_code'] = general.hashstring( general.randomstring()) self.data['billcode_name'] = general.hashstring( general.randomstring()) # Define data to Insert self.data['values'] = [] for timestamp in _timestamps(): value_dict = { 'idx_datapoint': self.data['idx_datapoint'], 'value': timestamp * (1 + random.uniform(0, 1)), 'timestamp': timestamp} self.data['values'].append(value_dict) # Drop the database and create tables initialize_db() # Initialize agent variables agent_data = {} agent_data['devicename'] = self.data['devicename'] agent_data['device_description'] = self.data['device_description'] agent_data['id_agent'] = self.data['id_agent'] agent_data['agent'] = self.data['agent'] agent_data['timestamp'] = self.data['timestamp'] ( self.data['idx_device'], self.data['idx_agent']) = _setup_db_deviceagent(agent_data) # Get DeviceAgent index value deviceagent = hagent.GetDeviceAgent( self.data['idx_device'], self.data['idx_agent']) self.data['idx_deviceagent'] = deviceagent.idx_deviceagent() # Insert Department data into database dept_data = Department( name=self.data['department_name'].encode(), code=self.data['department_code'].encode() ) database = db.Database() database.add_all([dept_data], 1035) # Insert Billcode data into database bill_data = Billcode( name=self.data['billcode_name'].encode(), code=self.data['billcode_code'].encode() ) database = db.Database() database.add_all([bill_data], 1039) # Insert Datapoint data into database new_data = Datapoint( agent_source=self.data['agent_source'].encode(), agent_label=self.data['agent_label'].encode(), last_timestamp=self.data['last_timestamp'], idx_deviceagent=self.data['idx_deviceagent'], id_datapoint=self.data['id_datapoint'].encode()) database = db.Database() database.add_all([new_data], 1144) # Insert timeseries data into database new_data_list = [] for item in self.data['values']: new_data_list.append( Data( idx_datapoint=item['idx_datapoint'], timestamp=item['timestamp'], value=item['value'])) database = db.Database() database.add_all(new_data_list, 1072)