From 1a8da301028b2f2a4ba4995c76354706e924cd8a Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 29 Mar 2024 14:27:52 -0700 Subject: [PATCH] Add plc-activity.py --- plc-activity.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 plc-activity.py diff --git a/plc-activity.py b/plc-activity.py new file mode 100755 index 0000000..c8009fd --- /dev/null +++ b/plc-activity.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +from datetime import datetime, timezone +import sys +import json +import redis +import requests +import time + +PLC_EXPORT_URL = 'https://plc.directory/export' + +cids = [] +redis_conn = redis.Redis() +redis_pipe = redis_conn.pipeline() + +while True: + plc_latest = redis_conn.get('dev.edavis.muninsky.plc_latest') + assert plc_latest is not None, 'manually set the `plc_latest` redis key first' + ts = datetime.fromisoformat(plc_latest.decode()) + ts = ts.isoformat('T', 'milliseconds').replace('+00:00', 'Z') + + qs = '?after={ts}'.format(ts=ts) + + print(f'Requesting {PLC_EXPORT_URL}{qs}', end=' ') + resp = requests.get(PLC_EXPORT_URL + qs) + resp.raise_for_status() + + ops = 0 + after = datetime.now(timezone.utc) + for line in resp.iter_lines(): + doc = json.loads(line) + after = datetime.strptime(doc['createdAt'], '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc) + if doc['cid'] in cids: + continue + cids.insert(0, doc['cid']) + redis_pipe.incr('dev.edavis.muninsky.plc_ops') + ops += 1 + + print(f'Fetched {ops} operations') + sys.stdout.flush() + cids = cids[:25] + + redis_pipe.set('dev.edavis.muninsky.plc_latest', after.isoformat()) + redis_pipe.execute() + + time.sleep(60) -- 2.51.2