def _query(self, query, time_precision='s', chunked=False): if time_precision not in ['s', 'm', 'ms', 'u']: raise Exception( "Invalid time precision is given. (use 's', 'm', 'ms' or 'u')") if chunked is True: chunked_param = 'true' else: chunked_param = 'false' # Build the URL of the serie to query url = "db/{0}/series".format(self._database) params = { 'q': query, 'time_precision': time_precision, 'chunked': chunked_param } response = self.request( url=url, method='GET', params=params, expected_response_code=200 ) if chunked: try: decoded = chunked_json.loads(response.content.decode()) except UnicodeDecodeError: decoded = chunked_json.loads(response.content.decode('utf-8')) return list(decoded) return response.json()
def _query(self, query, time_precision='s', chunked=False): if time_precision not in ['s', 'm', 'ms', 'u']: raise Exception( "Invalid time precision is given. (use 's', 'm', 'ms' or 'u')") if chunked is True: chunked_param = 'true' else: chunked_param = 'false' # Build the URL of the serie to query url = "db/{0}/series".format(self._database) params = { 'q': query, 'time_precision': time_precision, 'chunked': chunked_param } response = self.request(url=url, method='GET', params=params, expected_response_code=200) if chunked: try: decoded = chunked_json.loads(response.content.decode()) except UnicodeDecodeError: decoded = chunked_json.loads(response.content.decode('utf-8')) return list(decoded) return response.json()
def test_load(self): """ Tests reading a sequence of JSON values from a string """ example_response = \ '{"results": [{"series": [{"measurement": "sdfsdfsdf", ' \ '"columns": ["time", "value"], "values": ' \ '[["2009-11-10T23:00:00Z", 0.64]]}]}, {"series": ' \ '[{"measurement": "cpu_load_short", "columns": ["time", "value"],'\ '"values": [["2009-11-10T23:00:00Z", 0.64]]}]}]}' res = list(chunked_json.loads(example_response)) # import ipdb; ipdb.set_trace() # self.assertTrue(res) self.assertListEqual( [ { 'results': [ {'series': [{ 'values': [['2009-11-10T23:00:00Z', 0.64]], 'measurement': 'sdfsdfsdf', 'columns': ['time', 'value']}]}, {'series': [{ 'values': [['2009-11-10T23:00:00Z', 0.64]], 'measurement': 'cpu_load_short', 'columns': ['time', 'value']}]} ] } ], res )
def test_load(self): """ Tests reading a sequence of JSON values from a string """ example_response = \ '{"results": [{"series": [{"name": "sdfsdfsdf", ' \ '"columns": ["time", "value"], "values": ' \ '[["2009-11-10T23:00:00Z", 0.64]]}]}, {"series": ' \ '[{"name": "cpu_load_short", "columns": ["time", "value"], ' \ '"values": [["2009-11-10T23:00:00Z", 0.64]]}]}]}' res = list(chunked_json.loads(example_response)) # import ipdb; ipdb.set_trace() # self.assertTrue(res) self.assertListEqual( [ { 'results': [ {'series': [{ 'values': [['2009-11-10T23:00:00Z', 0.64]], 'name': 'sdfsdfsdf', 'columns': ['time', 'value']}]}, {'series': [{ 'values': [['2009-11-10T23:00:00Z', 0.64]], 'name': 'cpu_load_short', 'columns': ['time', 'value']}]} ] } ], res )
def query(self, query, time_precision='s', chunked=False): """ Quering data :param time_precision: [Optional, default 's'] Either 's', 'm', 'ms' or 'u'. :param chunked: [Optional, default=False] True if the data shall be retrieved in chunks, False otherwise. """ if time_precision not in ['s', 'm', 'ms', 'u']: raise Exception( "Invalid time precision is given. (use 's', 'm', 'ms' or 'u')") if chunked is True: chunked_param = 'true' else: chunked_param = 'false' # Build the URL of the serie to query url = "db/{0}/series".format(self._database) params = { 'q': query, 'time_precision': time_precision, 'chunked': chunked_param } response = self.request( url=url, method='GET', params=params, expected_response_code=200 ) if chunked: return list(chunked_json.loads(response.content.decode())) else: return response.json()