def test_chart_timestamp_args(self): """Testing function chart_timestamp_args.""" # Create a new Agent entry _pi = 1000 agent_id = data.hashstring(str(random())) agent_target = data.hashstring(str(random())) agent_program = data.hashstring(str(random())) agent.insert_row(agent_id, agent_target, agent_program) idx_agent = agent.exists(agent_id, agent_target) # Create entry and check checksum = data.hashstring(str(random())) result = datapoint.checksum_exists(checksum) self.assertFalse(result) datapoint.insert_row(checksum, DATA_FLOAT, _pi, idx_agent) idx_datapoint = datapoint.checksum_exists(checksum) # Test values = [False, None] for value in values: now = normalized_timestamp(_pi, int(time.time() * 1000)) result = uri.chart_timestamp_args(idx_datapoint, value) self.assertEqual(result + 604800000, now) values = [-1, -6011, 1, 6011] for value in values: now = normalized_timestamp(_pi, int(time.time() * 1000)) result = uri.chart_timestamp_args(idx_datapoint, value) self.assertEqual(result + (abs(value) * 1000), now) values = ['foo', [None]] for value in values: now = normalized_timestamp(_pi, int(time.time() * 1000)) result = uri.chart_timestamp_args(idx_datapoint, value) self.assertEqual(result + 604800000, now)
def route_data(idx_datapoint): """Provide data from the Data table. Args: idx_datapoint: DataPoint.idx_datapoint key Returns: _result: JSONify list of dicts {timestamp: value} from the Data table. """ # Initialize key variables _result = {} secondsago = data.integerize(request.args.get('secondsago')) ts_start = uri.chart_timestamp_args(idx_datapoint, secondsago) # Get data _datapoint = DataPoint(idx_datapoint) ts_stop = _datapoint.last_timestamp() _result = _datapoint.data(ts_start, ts_stop) # Return result = jsonify(_result) return result
def test_route_data(self): """Testing method / function route_data.""" # Initialize key variables secondsago = 3600 ts_start = uri.chart_timestamp_args(secondsago) # Initialize key variables _data = [] checksum = data.hashstring(str(random())) pattoo_key = data.hashstring(str(random())) agent_id = data.hashstring(str(random())) _pi = 300 * 1000 data_type = DATA_FLOAT now = int(time.time()) * 1000 count = 0 for timestamp in range(ts_start, now, _pi): insert = PattooDBrecord( pattoo_checksum=checksum, pattoo_key=pattoo_key, pattoo_agent_id=agent_id, pattoo_agent_polling_interval=_pi, pattoo_timestamp=timestamp, pattoo_data_type=data_type, pattoo_value=count, pattoo_agent_polled_target='pattoo_agent_polled_target', pattoo_agent_program='pattoo_agent_program', pattoo_agent_hostname='pattoo_agent_hostname', pattoo_metadata=[]) count += 1 # Create checksum entry in the DB, then update the data table idx_datapoint = datapoint.idx_datapoint(insert) _data.append( IDXTimestampValue(idx_datapoint=idx_datapoint, polling_interval=_pi, timestamp=timestamp, value=count)) # Insert rows of new data lib_data.insert_rows(_data) # Test obj = DataPoint(idx_datapoint) ts_stop = obj.last_timestamp() expected = obj.data(ts_start, ts_stop) # Create URL config = WebConfig() url = ('{}/{}'.format(config.web_api_server_url(graphql=False), idx_datapoint)) # Check response with requests.get(url) as response: result = response.json() count = 0 for item in result: ts_norm = times.normalized_timestamp(_pi, ts_start) if item['timestamp'] < ts_norm: self.assertIsNone(item['value']) else: self.assertEqual(item, expected[count]) count += 1