Maintain a single lossy timeseries (PoC)

This commit is contained in:
Peter J. Holzer 2022-08-20 17:39:12 +02:00 committed by Peter J. Holzer
commit 5630f43e62
1 changed files with 36 additions and 0 deletions

36
ltsdb-json Executable file
View File

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