Add web API

This commit is contained in:
Peter J. Holzer 2022-09-02 14:06:47 +02:00
parent 8e26433a86
commit 24544cbdac
2 changed files with 45 additions and 3 deletions

33
app.py Normal file
View File

@ -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 })

View File

@ -4,6 +4,7 @@ import fcntl
import glob import glob
import hashlib import hashlib
import json import json
import random
import time import time
class LTS: class LTS:
@ -74,8 +75,16 @@ class LTS:
print("index rebuilt in", t1 - t0, "seconds") print("index rebuilt in", t1 - t0, "seconds")
@classmethod @classmethod
def find(self, match): def find(cls, match):
with open(self.base_dir + "/.index", "r") as fh: result = None
with open(cls.base_dir + "/.index", "r") as fh:
fcntl.flock(fh, fcntl.LOCK_SH) 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