def api_query(): iql = request.args.get('q') if iql == None or iql == '': return json400({"error": "Empty query!"}) try: iql = json.loads(iql) except: return json400({"error": "Not valid JSON!"}) try: sql = iqlc.convert(iql, get_iql_config()) except ValueError as error: return json400({"error": str(error)}) dr = get_db().query(sql).dictresult() result_json = [] i = 0 for e in dr: remove_nulls(e) result_json.append(e) i += 1 if (i > 128): break return json200({"results": result_json})
def api_translate(): iql = request.args.get('q') if iql == None or iql == '': return json400({"error" : "Empty query!"}) try: iql = json.loads(iql) except: return json400({"error" : "Not valid JSON!"}) try: sql = iqlc.convert(iql) except ValueError as error: return json400({"error" : str(error)}) pp = pprint.PrettyPrinter(indent = 2) piql = pp.pformat(iql) lines = piql.splitlines() piql = '\n'.join(map(lambda a: '-- ' + a, lines)) dr = get_db().query(sql).dictresult() result_json = "" i = 0 for e in dr: for key in e: if key.lower() in ['time_to' ,'time_from']: e[key] = e[key].timestamp() if ('val_i' in e) and ('val_s' in e): if e['val_i'] != None: e['value'] = e['val_i'] elif e['val_s'] != None: e['value'] = e['val_s'] else: e['value'] = None del e['val_i'] del e['val_s'] elif 'val_i' in e: e['value'] = e['val_i'] del e['val_i'] elif 'val_s' in e: e['value'] = e['val_s'] del e['val_s'] result_json += json.dumps(e) + "\n" i += 1 if(i > 128): break return text200(piql + "\n\n" + sql + "\n\n" + result_json)
def api_index(): """ Dummy method answering with `{"status":"running"}` when the API is available. """ for e in get_db().query("SELECT 1+1;").dictresult(): print(e) return json200({'status':'running'})
def api_aquery(): iql = request.args.get('q') if iql == None or iql == '': return json400({"error": "Empty query!"}) try: iql = json.loads(iql) iqls = json.dumps(iql, sort_keys=True) except: return json400({"error": "Not valid JSON!"}) try: iqlc.convert(iql, get_iql_config()) except ValueError as error: return json400({"iql": iql, "error": str(error)}) query_hash = sha1_hash(iqls) sql = "SELECT * FROM query_queue WHERE id = '%s';" % ( escape_string(query_hash)) try: dr = get_db().query(sql).dictresult() except error: return json500({"error": "Internal Server Error"}) if len(dr) > 0: first = dr[0] return json200({"query_id": first["id"]}) sql = "INSERT INTO query_queue(id, iql, result, state) VALUES('%s', '%s'::JSON, NULL, 'new');" % ( escape_string(query_hash), escape_string(iqls)) try: get_db().query(sql) except: return json500({"error": "Internal Server Error"}) return json200({"query_id": query_hash})
def api_query(): iql = request.args.get('q') if iql == None or iql == '': return json400({"error" : "Empty query!"}) try: iql = json.loads(iql) except: return json400({"error" : "Not valid JSON!"}) try: sql = iqlc.convert(iql) except ValueError as error: return json400({"error" : str(error)}) dr = get_db().query(sql).dictresult() result_json = [] i = 0 for e in dr: for key in e: if key.lower() in ['time_to' ,'time_from']: e[key] = e[key].timestamp() if ('val_i' in e) and ('val_s' in e): if e['val_i'] != None: e['value'] = e['val_i'] elif e['val_s'] != None: e['value'] = e['val_s'] else: e['value'] = None del e['val_i'] del e['val_s'] elif 'val_i' in e: e['value'] = e['val_i'] del e['val_i'] elif 'val_s' in e: e['value'] = e['val_s'] del e['val_s'] result_json.append(e) i += 1 if(i > 128): break return json200({"results" : result_json})
def api_result(): query_id = request.args.get('id') sql = "SELECT * FROM query_queue WHERE id = '%s';" % ( escape_string(query_id)) try: dr = get_db().query(sql).dictresult() except: return json500({"error": "Internal Server Error"}) if len(dr) <= 0: return json404({"error": "Not found!"}) return json200(dr[0])
def api_old_single(): oid = to_int(request.args.get('oid')) iql_query = {"query": {"all": [{"simple": {"eq": ["@oid", oid]}}]}} try: sql = iqlc.convert(iql_query, get_iql_config()) except ValueError as error: return json400({"error": str(error)}) dr = get_db().query(sql).dictresult() for e in dr: remove_nulls(e) convert_row(e) return json200({"iql": iql_query, "result": e}) return json404({"iql": iql_query, "error": "Not found"})
def api_translate(): iql = request.args.get('q') if iql == None or iql == '': return json400({"error": "Empty query!"}) try: iql = json.loads(iql) except: return json400({"error": "Not valid JSON!"}) try: sql = iqlc.convert(iql, get_iql_config()) except ValueError as error: return json400({"error": str(error)}) pp = pprint.PrettyPrinter(indent=2) piql = pp.pformat(iql) lines = piql.splitlines() piql = '\n'.join(map(lambda a: '-- ' + a, lines)) dr = get_db().query(sql).dictresult() result_json = "" i = 0 for e in dr: remove_nulls(e) result_json += json.dumps(e, cls=CustomEncoder) + "\n" i += 1 if (i > 128): break return text200(piql + "\n\n" + sql + "\n\n" + result_json)
def api_old(): sip = request.args.get('sip') dip = request.args.get('dip') on_path = request.args.get('on_path') if on_path: on_path = on_path.split(',') time_from = int(to_int(request.args.get('from')) / 1000.0) time_to = int(to_int(request.args.get('to')) / 1000.0) conditions = request.args.get('conditions') dnf = [] if conditions: and_terms = conditions.split(',') for and_term in and_terms: and_term = and_term.split(':') dnf.append(and_term) iql_ands = [] for and_term in dnf: ands = [] for condition in and_term: ands.append(convert_condition_to_eq(condition)) iql_ands.append({"and": ands}) iql_dnf = {"or": iql_ands} iql_query_parts = [iql_dnf] if sip: iql_query_parts.append({"eq": ["@sip", sip]}) if dip: iql_query_parts.append({"eq": ["@dip", dip]}) iql_query_parts.append({"ge": ["@time_from", {"time": [time_from]}]}) iql_query_parts.append({"le": ["@time_to", {"time": [time_to]}]}) iql_query = ({"query": {"all": [{"simple": [{"and": iql_query_parts}]}]}}) try: sql = iqlc.convert(iql_query, get_iql_config()) except ValueError as error: return json400({"iql": iql_query, "error": str(error)}) dr = get_db().query(sql).dictresult() result_json = [] i = 0 for e in dr: remove_nulls(e) convert_row(e) result_json.append(e) i += 1 if (i > 128): break return json200({ "iql": iql_query, "count": len(result_json), "results": result_json })
def api_old_grouped(): sip = request.args.get('sip') dip = request.args.get('dip') on_path = request.args.get('on_path') if on_path: on_path = on_path.split(',') time_from = int(to_int(request.args.get('from')) / 1000.0) time_to = int(to_int(request.args.get('to')) / 1000.0) conditions = request.args.get('conditions') dnf = [] if conditions: and_terms = conditions.split(',') for and_term in and_terms: and_term = and_term.split(':') dnf.append(and_term) iql_unions = [] for and_term in dnf: ands = [] stages = [] for condition in and_term: ands.append(convert_condition_to_eq(condition)) for and_ in ands: stage = [ and_, { "ge": ["@time_from", { "time": [time_from] }] }, { "le": ["@time_to", { "time": [time_to] }] } ] if sip: stage.append({"eq": ["@sip", sip]}) if dip: stage.append({"eq": ["@dip", dip]}) stages.append({"and": stage}) iql_unions.append({"sieve-ex": ["", "@path_id"] + stages}) iql_query = ({"query": {"all": [{"union-ls": iql_unions}]}}) try: sql = iqlc.convert(iql_query, get_iql_config()) except ValueError as error: return json400({"iql": iql_query, "error": str(error)}) dr = get_db().query(sql).dictresult() rows = [] i = 0 for e in dr: remove_nulls(e) convert_row(e) rows.append(e) i += 1 if (i > 128): break groups = {} for row in rows: key = '\t'.join(row['path']) if key in groups: groups[key].append(row) else: groups[key] = [row] results = [] for group in groups: results.append({ "id": group.split('\t'), "path": group.split("\t"), "observations": groups[group] }) return json200({ "iql": iql_query, "results": results, "count": len(results) })