def chart_timestamp_args(idx_datapoint, secondsago=None): """Create URI arguments for charts. Args: idx_datapoint: DataPoint index value secondsago: Number of seconds in the past to calculate start and stop times for charts Returns: result: Starting time """ # Calculate stop. This takes into account the ingester cycle and subtracts # a few extra seconds to prevent zero values at the end. datapoint = DataPoint(idx_datapoint) polling_interval = datapoint.polling_interval() now = normalized_timestamp(polling_interval, int(time.time() * 1000)) # Calculate start if bool(secondsago) is True and isinstance(secondsago, int) is True: result = now - (abs(secondsago) * 1000) else: # result = ts_stop - (604800 * 1000) result = now - (3600 * 24 * 7) * 1000 # Return return result
def test_data(self): """Testing method / function data.""" # Initialize key variables _data = [] expected = [] checksum = data.hashstring(str(random())) pattoo_key = data.hashstring(str(random())) agent_id = data.hashstring(str(random())) polling_interval = 300 * 1000 data_type = DATA_FLOAT _pattoo_value = 27 _timestamp = int(time.time() * 1000) ts_start = _timestamp for count in range(0, 10): timestamp = _timestamp + (polling_interval * count) ts_stop = timestamp pattoo_value = _pattoo_value * count insert = PattooDBrecord( pattoo_checksum=checksum, pattoo_key=pattoo_key, pattoo_agent_id=agent_id, pattoo_agent_polling_interval=polling_interval, pattoo_timestamp=timestamp, pattoo_data_type=data_type, pattoo_value=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=[]) # 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=polling_interval, timestamp=timestamp, value=pattoo_value)) # Append to expected results expected.append({ 'timestamp': times.normalized_timestamp(polling_interval, timestamp), 'value': pattoo_value }) # Insert rows of new data lib_data.insert_rows(_data) # Test obj = DataPoint(idx_datapoint) result = obj.data(ts_start, ts_stop) self.assertEqual(result, expected)
def test_polling_interval(self): """Testing method / function polling_interval.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20106) as session: result = session.query(_DataPoint.polling_interval).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.polling_interval, obj.polling_interval())
def test_last_timestamp(self): """Testing method / function last_timestamp.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20101) as session: result = session.query(_DataPoint.last_timestamp).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.last_timestamp, obj.last_timestamp())
def test_data_type(self): """Testing method / function data_type.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20102) as session: result = session.query(_DataPoint.data_type).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.data_type, obj.data_type())
def test_checksum(self): """Testing method / function checksum.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20103) as session: result = session.query(_DataPoint.checksum).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(result.checksum.decode(), obj.checksum())
def test_enabled(self): """Testing method / function enabled.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) # Get the result with db.db_query(20105) as session: result = session.query(_DataPoint.enabled).filter( _DataPoint.idx_datapoint == idx_datapoint).one() self.assertEqual(bool(result.enabled), obj.enabled())
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_exists(self): """Testing method / function exists.""" # Create a new row in the database and test idx_datapoint = _idx_datapoint() obj = DataPoint(idx_datapoint) self.assertTrue(obj.exists())
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