ltsdb/ltsdb-json

37 lines
703 B
Plaintext
Raw Permalink Normal View History

#!/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))