From 5630f43e62a06c80d3c5dafd1695d0a18be7ac8f Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sat, 20 Aug 2022 17:39:12 +0200 Subject: [PATCH] Maintain a single lossy timeseries (PoC) --- ltsdb-json | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 ltsdb-json 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))