From f50a08b50bd7c4dacea5de8062f75f417c3bbaed Mon Sep 17 00:00:00 2001 From: Ske Date: Sat, 21 Jul 2018 01:22:07 +0200 Subject: [PATCH] Add occasional basic stat reporting to InfluxDB --- bot/pluralkit/bot.py | 11 +++++++++++ bot/pluralkit/db.py | 8 ++++++++ bot/pluralkit/stats.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/bot/pluralkit/bot.py b/bot/pluralkit/bot.py index f3ca66a6..f0968839 100644 --- a/bot/pluralkit/bot.py +++ b/bot/pluralkit/bot.py @@ -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...") diff --git a/bot/pluralkit/db.py b/bot/pluralkit/db.py index 9c52c790..d5772406 100644 --- a/bot/pluralkit/db.py +++ b/bot/pluralkit/db.py @@ -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, diff --git a/bot/pluralkit/stats.py b/bot/pluralkit/stats.py index 3077f935..61db8c05 100644 --- a/bot/pluralkit/stats.py +++ b/bot/pluralkit/stats.py @@ -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 + } }) \ No newline at end of file