Add occasional basic stat reporting to InfluxDB
This commit is contained in:
parent
ec5a023348
commit
f50a08b50b
@ -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:
|
||||
@ -103,6 +111,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...")
|
||||
await client.start(os.environ["TOKEN"])
|
||||
|
@ -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,
|
||||
|
@ -27,3 +27,21 @@ async def report_webhook(time, success):
|
||||
"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
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user