Add web API
This commit is contained in:
parent
8e26433a86
commit
24544cbdac
|
@ -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 })
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue