Add occasional basic stat reporting to InfluxDB
This commit is contained in:
		| @@ -1,3 +1,4 @@ | ||||
| import asyncio | ||||
| from datetime import datetime | ||||
| import logging | ||||
| import json | ||||
| @@ -90,6 +91,13 @@ async def on_socket_raw_receive(msg): | ||||
|         except ValueError: | ||||
|             pass | ||||
|  | ||||
| async def periodical_stat_timer(pool): | ||||
|     async with pool.acquire() as conn: | ||||
|         while True: | ||||
|             from pluralkit import stats | ||||
|             await stats.report_periodical_stats(conn) | ||||
|             await asyncio.sleep(30) | ||||
|  | ||||
| async def run(): | ||||
|     from pluralkit import db, stats | ||||
|     try: | ||||
| @@ -102,6 +110,9 @@ async def run(): | ||||
|  | ||||
|         logger.info("Connecting to InfluxDB...") | ||||
|         await stats.connect() | ||||
|          | ||||
|         logger.info("Starting periodical stat reporting...") | ||||
|         asyncio.get_event_loop().create_task(periodical_stat_timer(pool)) | ||||
|  | ||||
|         client.pool = pool | ||||
|         logger.info("Connecting to Discord...") | ||||
|   | ||||
| @@ -242,6 +242,14 @@ async def member_count(conn): | ||||
| async def system_count(conn): | ||||
|     return await conn.fetchval("select count(*) from systems") | ||||
|  | ||||
| @db_wrap | ||||
| async def message_count(conn): | ||||
|     return await conn.fetchval("select count(*) from messages") | ||||
|  | ||||
| @db_wrap | ||||
| async def account_count(conn): | ||||
|     return await conn.fetchval("select count(*) from accounts") | ||||
|  | ||||
| async def create_tables(conn): | ||||
|     await conn.execute("""create table if not exists systems ( | ||||
|         id          serial primary key, | ||||
|   | ||||
| @@ -26,4 +26,22 @@ async def report_webhook(time, success): | ||||
|     await client.write({ | ||||
|         "measurement": "webhook", | ||||
|         "fields": {"response_time": time, "success": int(success)} | ||||
|     }) | ||||
|  | ||||
| async def report_periodical_stats(conn): | ||||
|     from pluralkit import db | ||||
|  | ||||
|     systems = await db.system_count(conn) | ||||
|     members = await db.member_count(conn) | ||||
|     messages = await db.message_count(conn) | ||||
|     accounts = await db.account_count(conn) | ||||
|  | ||||
|     await client.write({ | ||||
|         "measurement": "stats", | ||||
|         "fields": { | ||||
|             "systems": systems, | ||||
|             "members": members, | ||||
|             "messages": messages, | ||||
|             "accounts": accounts | ||||
|         } | ||||
|     }) | ||||
		Reference in New Issue
	
	Block a user