diff --git a/app.py b/app.py new file mode 100644 index 0000000..18b64c2 --- /dev/null +++ b/app.py @@ -0,0 +1,33 @@ +import logging +import json + +from flask import (Flask, request, jsonify) + +from ltsdb_json import LTS + +logging.basicConfig(level=logging.DEBUG) + +app = Flask(__name__) + +log = logging.getLogger() + +@app.route("/") +def home(): + return jsonify({ "success": None }) + + +@app.route("/report", methods=["POST"]) +def report(): + data = request.get_json() + n_ts = 0 + n_dp = 0 + for d in data: + d["description"]["remote_addr"] = request.remote_addr + log.info("received %s", json.dumps(d)) + ts = LTS(d["description"]) + for dp in d["data"]: + ts.add(*dp) + ts.save() + n_dp += 1 + n_ts += 1 + return jsonify({ "success": True, "timeseries": n_ts, "datapoints": n_dp }) diff --git a/ltsdb_json.py b/ltsdb_json.py index 19e13ff..6eb2f3a 100644 --- a/ltsdb_json.py +++ b/ltsdb_json.py @@ -4,6 +4,7 @@ import fcntl import glob import hashlib import json +import random import time class LTS: @@ -74,8 +75,16 @@ class LTS: print("index rebuilt in", t1 - t0, "seconds") @classmethod - def find(self, match): - with open(self.base_dir + "/.index", "r") as fh: + def find(cls, match): + result = None + with open(cls.base_dir + "/.index", "r") as fh: fcntl.flock(fh, fcntl.LOCK_SH) - index = json.dump(fh) + index = json.load(fh) + for d, v in match.items(): + ts = set(index[d][v]) + if result is None: + result = ts + else: + result &= ts + return result