commit 5630f43e62a06c80d3c5dafd1695d0a18be7ac8f Author: Peter J. Holzer Date: Sat Aug 20 17:39:12 2022 +0200 Maintain a single lossy timeseries (PoC) diff --git a/ltsdb-json b/ltsdb-json new file mode 100755 index 0000000..3b1b8b2 --- /dev/null +++ b/ltsdb-json @@ -0,0 +1,36 @@ +#!/usr/bin/python3 + +# PoC + +# add a new entry and remove a random old entry with a probability +# depending on the target size + +import argparse +import json +import random +import time + +ap = argparse.ArgumentParser() + +ap.add_argument("--targetsize", type=int, default=1000) +ap.add_argument("file") +ap.add_argument("value", type=float) + +args = ap.parse_args() + +ts = [] +try: + with open(args.file) as f: + ts = json.load(f) +except FileNotFoundError as e: + pass # Ok, will create + +now = time.time() +ts.append((now, args.value)) +if random.random() > 1 - len(ts) / args.targetsize: + i = random.randrange(0, len(ts)) + ts.pop(i) + +with open(args.file, "w") as f: + json.dump(ts, f) +print(len(ts))