this repo has no description
1#!/usr/bin/env python3 2 3import asyncio 4from datetime import datetime, timezone 5import json 6import os 7import sqlite3 8import sys 9 10import redis 11import websockets 12 13app_bsky_allowlist = set([ 14 'app.bsky.actor.profile', 15 'app.bsky.feed.generator', 16 'app.bsky.feed.like', 17 'app.bsky.feed.post', 18 'app.bsky.feed.postgate', 19 'app.bsky.feed.repost', 20 'app.bsky.feed.threadgate', 21 'app.bsky.graph.block', 22 'app.bsky.graph.follow', 23 'app.bsky.graph.list', 24 'app.bsky.graph.listblock', 25 'app.bsky.graph.listitem', 26 'app.bsky.graph.starterpack', 27 'app.bsky.labeler.service', 28 'chat.bsky.actor.declaration', 29]) 30 31other_allowlist = set([ 32 'social.psky.feed.post', 33 'social.psky.chat.message', 34 'blue.zio.atfile.upload', 35]) 36 37async def bsky_activity(): 38 relay_url = 'ws://localhost:6008/subscribe' 39 40 sys.stdout.write(f'opening websocket connection to {relay_url}\n') 41 sys.stdout.flush() 42 43 async with websockets.connect(relay_url, ping_timeout=60) as firehose: 44 while True: 45 yield json.loads(await firehose.recv()) 46 47async def main(): 48 redis_cnx = redis.Redis() 49 redis_pipe = redis_cnx.pipeline() 50 51 if os.path.exists('/opt/muninsky/users.db'): 52 db_fname = '/opt/muninsky/users.db' 53 else: 54 db_fname = 'users.db' 55 56 db_cnx = sqlite3.connect(db_fname) 57 with db_cnx: 58 db_cnx.executescript(""" 59 PRAGMA journal_mode = WAL; 60 PRAGMA synchronous = off; 61 CREATE TABLE IF NOT EXISTS users (did TEXT, ts TIMESTAMP); 62 CREATE UNIQUE INDEX IF NOT EXISTS did_idx on users(did); 63 CREATE INDEX IF NOT EXISTS ts_idx on users(ts); 64 """) 65 66 sys.stdout.write('starting up\n') 67 sys.stdout.flush() 68 69 op_count = 0 70 async for event in bsky_activity(): 71 if event['kind'] != 'commit': 72 continue 73 74 payload = event.get('commit') 75 if payload is None: 76 continue 77 78 if payload['operation'] != 'create': 79 continue 80 81 collection = payload['collection'] 82 if collection not in app_bsky_allowlist | other_allowlist: 83 continue 84 85 repo_did = event['did'] 86 repo_update_time = datetime.now(timezone.utc) 87 db_cnx.execute( 88 'insert into users values (:did, :ts) on conflict (did) do update set ts = :ts', 89 {'did': repo_did, 'ts': repo_update_time.timestamp()} 90 ) 91 92 if collection == 'app.bsky.feed.post': 93 embed = payload['record'].get('embed') 94 if embed is not None and embed.get('$type', ''): 95 embed_type = embed['$type'] 96 redis_pipe.incr(f'app.bsky.feed.post:embed:{embed_type}') 97 98 redis_pipe \ 99 .incr(collection) \ 100 .incr('dev.edavis.muninsky.ops') 101 102 op_count += 1 103 if op_count % 500 == 0: 104 current_time_ms = datetime.now(timezone.utc).timestamp() 105 event_time_ms = event['time_us'] / 1_000_000 106 current_lag = current_time_ms - event_time_ms 107 sys.stdout.write(f'lag: {current_lag:.2f}\n') 108 redis_pipe.execute() 109 db_cnx.commit() 110 sys.stdout.flush() 111 112if __name__ == '__main__': 113 asyncio.run(main())