def query_datapoints(config): lines = config.splitlines() es_hosts = lines[0] sql = ''.join(lines[1:]) datapoints = [] ts = int(time.time()) try: result_map = es_query.execute_sql(es_hosts, sql) except urllib2.HTTPError as e: LOGGER.exception(e.read()) sys.exit(1) except: LOGGER.exception('read datapoint failed') sys.exit(1) for metric_name, rows in result_map.iteritems(): for row in rows or []: datapoint = {} datapoint['timestamp'] = ts datapoint['name'] = row.pop('_metric_name', None) or metric_name datapoint['value'] = row.pop('value', 0) if row: tags = {} for k, v in row.iteritems(): k = to_str(k) if not k.startswith('_'): tags[k] = to_str(v) datapoint['tags'] = tags datapoints.append(datapoint) LOGGER.info('read datapoints: %s' % len(datapoints)) return datapoints
def test_save_as(self): result_map = es_query.execute_sql( None, """ SELECT * FROM abc; SAVE RESULT AS result1; """) self.assertEqual(['result1'], result_map.keys())
def test_complex_python_code(self): result_map = es_query.execute_sql( None, """ result_map['result1'] = [] for i in range(100): result_map['result1'].append(i) """) self.assertEqual(['result1'], result_map.keys())
def test_remove(self): result_map = es_query.execute_sql( None, """ SELECT * FROM abc; SAVE RESULT AS result1; REMOVE RESULT result1; """) self.assertEqual([], result_map.keys())
def test_python_code(self): result_map = es_query.execute_sql( None, """ result_map['result1'] = [] """, ) self.assertEqual(["result1"], result_map.keys())
def test_no_save_as(self): result_map = es_query.execute_sql( None, """ SELECT * FROM abc """, ) self.assertEqual(["result"], result_map.keys())
def test_save_as(self): result_map = es_query.execute_sql( None, """ SELECT * FROM abc; SAVE RESULT AS result1; """, ) self.assertEqual(["result1"], result_map.keys())
def test_complex_python_code(self): result_map = es_query.execute_sql( None, """ result_map['result1'] = [] for i in range(100): result_map['result1'].append(i) """, ) self.assertEqual(["result1"], result_map.keys())
def test_remove(self): result_map = es_query.execute_sql( None, """ SELECT * FROM abc; SAVE RESULT AS result1; REMOVE RESULT result1; """, ) self.assertEqual([], result_map.keys())
def search_with_arguments(): req = json.loads(flask.request.get_data(parse_form_data=False)) try: resp = { 'error': None, 'data': es_query.execute_sql(req['elasticsearch'], req['sql'], req['arguments']) } return json.dumps(resp, indent=2) except: etype, value, tb = sys.exc_info() resp = { 'request_args': flask.request.args, 'request_body': flask.request.get_data(parse_form_data=False), 'traceback': traceback.format_exception(etype, value, tb), 'error': str(value), 'data': None } return json.dumps(resp, indent=2)
def search(): if flask.request.method == 'GET': sql = flask.request.args.get('q') else: sql = flask.request.get_data(parse_form_data=False) es_hosts = flask.request.args.get('elasticsearch') try: resp = {'error': None, 'data': es_query.execute_sql(es_hosts, sql)} return json.dumps(resp, indent=2) except: etype, value, tb = sys.exc_info() resp = { 'request_args': flask.request.args, 'request_body': flask.request.get_data(parse_form_data=False), 'traceback': traceback.format_exception(etype, value, tb), 'error': str(value), 'data': None } return json.dumps(resp, indent=2)
def search(): if flask.request.method == 'GET': sql = flask.request.args.get('q') else: sql = flask.request.get_data(parse_form_data=False) es_hosts = flask.request.args.get('elasticsearch') try: resp = { 'error': None, 'data': es_query.execute_sql(es_hosts, sql) } return json.dumps(resp, indent=2) except: etype, value, tb = sys.exc_info() resp = { 'request_args': flask.request.args, 'request_body': flask.request.get_data(parse_form_data=False), 'traceback': traceback.format_exception(etype, value, tb), 'error': str(value), 'data': None } return json.dumps(resp, indent=2)
def test_python_code(self): result_map = es_query.execute_sql( None, """ result_map['result1'] = [] """) self.assertEqual(['result1'], result_map.keys())
def test_no_save_as(self): result_map = es_query.execute_sql( None, """ SELECT * FROM abc """) self.assertEqual(['result'], result_map.keys())