/query
The /query function is used to populate charts. In the reqest a time range is specified and is converted to milliseconds for use with querying GridDB. If intervalMs is set, GridDB's TIME_SAMPLING function is used, otherwise a normal query is ran where the time range is control via where clauses.
@app.route('/query', methods=methods) @cross_origin(max_age=600) def query_metrics(): req = request.get_json() results = [] start = int(datetime.strptime(req['range']['from'], "%Y-%m-%dT%H:%M:%S.%fZ").timestamp()*1000) end = int(datetime.strptime(req['range']['to'], "%Y-%m-%dT%H:%M:%S.%fZ").timestamp()*1000) if 'intervalMs' in req: freq = "TIME_SAMPLING(*, TO_TIMESTAMP_MS("+ str(start) +"), TO_TIMESTAMP_MS("+ str(end) +"), "+ str(req.get('intervalMs'))+", MILLISECOND)" else: freq = "*" for target in req['targets']: datapoints= [] if ':' not in target.get('target', ''): abort(404, Exception('Target must be of type:: got instead: ' + target['target'])) req_type = target.get('type', 'timeserie') container, column = target['target'].split(':', 1) ts = gridstore.get_container(container) tql = "select "+ freq if freq == "*": tql = tql + " where timestamp > TO_TIMESTAMP_MS("+str(start)+") and timestamp < TO_TIMESTAMP_MS("+str(end)+")" query = ts.query(tql) rs = query.fetch(False) columns = rs.get_column_names() while rs.has_next(): data = rs.next() datapoints.append((data[int(columns.index(column))], int(data[0].timestamp()*1000))) column = {'target': '%s' % (container+" "+column), 'datapoints': datapoints} results.append(column) return jsonify(results)