Files
kefu/wechat_rpa/tmp/archive-remote-verify.py
T
2026-09-21 10:34:06 +08:00

38 lines
2.3 KiB
Python

from pathlib import Path
import json, sqlite3, time
from admin_backend import Database
from archive_store import ArchiveStore
class ReadDatabase(Database):
def connect(self):
db=super().connect()
db.execute("PRAGMA query_only=ON")
deadline=time.monotonic()+10
db.set_progress_handler(lambda: time.monotonic()>deadline,1000)
return db
database=ReadDatabase(Path('/data/backend.db'))
store=ArchiveStore(database)
with database.connect() as db:
tenants=[row[0] for row in db.execute('SELECT tenant_id FROM desktop_accounts ORDER BY id')]
total=db.execute('SELECT COUNT(*) FROM archive_message').fetchone()[0]
indexes=[row[0] for row in db.execute("SELECT name FROM sqlite_master WHERE type='index' AND name IN ('idx_archive_conv_page','idx_archive_person_recent','idx_archive_msg_recent','idx_archive_media_recent','idx_archive_export_files')")]
assert len(indexes)==5
assert total>=412361
results=[]
for tenant in ['default',*tenants]:
timings={}
for name,fn in [('stats',lambda:store.stats(tenant)),('conversations',lambda:store.conversations(50,'',tenant)),('people',lambda:store.people(200,'',tenant)),('exports',lambda:store.export_jobs(100,tenant)),('media',lambda:store.media_items(100,tenant))]:
start=time.monotonic();value=fn();timings[name]=round((time.monotonic()-start)*1000,2)
if name=='conversations' and value['items']:
conversation=value['items'][0]
start=time.monotonic();page=store.messages(conversation['id'],100,'',tenant);timings['messages']=round((time.monotonic()-start)*1000,2)
if page['has_more']:
following=store.messages(conversation['id'],100,page['next_cursor'],tenant)
assert not {x['id'] for x in page['items']}&{x['id'] for x in following['items']}
results.append({'tenant':tenant,'milliseconds':timings})
start=time.monotonic();config=store.storage_config();storage_ms=round((time.monotonic()-start)*1000,2)
report={'messages':total,'tenants_checked':len(results),'indexes':indexes,'storage_config_ms':storage_ms,'storage_enabled':bool(config['enabled']),'results':results}
Path('/data/backups/archive-performance-20260914-170249/after-verification.json').write_text(json.dumps(report,indent=2))
print(json.dumps(report),flush=True)