/annotations

Annotations are used to mark, or note different points on charts. They query format is \:\. Like /query a time range is specified and is converted to milliseconds. To avoid marking every point on the graph, only the first annotation in a series is added and future annotations won't be added unless there is a gap of at least sixty seconds.

@app.route('/annotations', methods=methods)
@cross_origin(max_age=600)
def query_annotations():
    req = request.get_json()

    response = []
    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)

    container, query = req['annotation']['query'].split(':', 1)

    ts = gridstore.get_container(container)

    tql = "select * where timestamp > TO_TIMESTAMP_MS("+str(start)+") and timestamp < TO_TIMESTAMP_MS("+str(end)+") and ( "+query+" )"
    query = ts.query(tql) 
    rs = query.fetch(False) 
    last = None

    while rs.has_next():
        data = rs.next()
        if not last or data[0].timestamp() - last[0].timestamp() > 60:
            response.append({
                "annotation": container, # The original annotation sent from Grafana.
                "time": int(data[0].timestamp()*1000), # Time since UNIX Epoch in milliseconds. (required)
                "title": container+" "+query, # The title for the annotation tooltip. (required)
            })
        last = data

    return jsonify(response)