mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-11 08:10:10 +00:00
Move stats stuff into its own class. Closes #1
This commit is contained in:
parent
cd0af5321c
commit
a130e2215a
4 changed files with 76 additions and 59 deletions
|
|
@ -1,57 +1,69 @@
|
|||
from aioinflux import InfluxDBClient
|
||||
|
||||
client = None
|
||||
async def connect():
|
||||
global client
|
||||
client = InfluxDBClient(host="influx", db="pluralkit")
|
||||
await client.create_database(db="pluralkit")
|
||||
|
||||
async def report_db_query(query_name, time, success):
|
||||
if not client:
|
||||
return
|
||||
class StatCollector:
|
||||
async def report_db_query(self, query_name, time, success):
|
||||
pass
|
||||
|
||||
await client.write({
|
||||
"measurement": "database_query",
|
||||
"tags": {"query": query_name},
|
||||
"fields": {"response_time": time, "success": int(success)}
|
||||
})
|
||||
async def report_command(self, command_name, execution_time, response_time):
|
||||
pass
|
||||
|
||||
async def report_command(command_name, execution_time, response_time):
|
||||
if not client:
|
||||
return
|
||||
async def report_webhook(self, time, success):
|
||||
pass
|
||||
|
||||
await client.write({
|
||||
"measurement": "command",
|
||||
"tags": {"command": command_name},
|
||||
"fields": {"execution_time": execution_time, "response_time": response_time}
|
||||
})
|
||||
async def report_periodical_stats(self, conn):
|
||||
pass
|
||||
|
||||
async def report_webhook(time, success):
|
||||
if not client:
|
||||
return
|
||||
|
||||
await client.write({
|
||||
"measurement": "webhook",
|
||||
"fields": {"response_time": time, "success": int(success)}
|
||||
})
|
||||
class NullStatCollector(StatCollector):
|
||||
pass
|
||||
|
||||
async def report_periodical_stats(conn):
|
||||
if not client:
|
||||
return
|
||||
|
||||
from pluralkit import db
|
||||
class InfluxStatCollector(StatCollector):
|
||||
@staticmethod
|
||||
async def connect():
|
||||
client = InfluxDBClient(host="influx", db="pluralkit")
|
||||
await client.create_database(db="pluralkit")
|
||||
|
||||
systems = await db.system_count(conn)
|
||||
members = await db.member_count(conn)
|
||||
messages = await db.message_count(conn)
|
||||
accounts = await db.account_count(conn)
|
||||
return InfluxStatCollector(client)
|
||||
|
||||
await client.write({
|
||||
"measurement": "stats",
|
||||
"fields": {
|
||||
"systems": systems,
|
||||
"members": members,
|
||||
"messages": messages,
|
||||
"accounts": accounts
|
||||
}
|
||||
})
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
async def report_db_query(self, query_name, time, success):
|
||||
await self.client.write({
|
||||
"measurement": "database_query",
|
||||
"tags": {"query": query_name},
|
||||
"fields": {"response_time": time, "success": int(success)}
|
||||
})
|
||||
|
||||
async def report_command(self, command_name, execution_time, response_time):
|
||||
await self.client.write({
|
||||
"measurement": "command",
|
||||
"tags": {"command": command_name},
|
||||
"fields": {"execution_time": execution_time, "response_time": response_time}
|
||||
})
|
||||
|
||||
async def report_webhook(self, time, success):
|
||||
await self.client.write({
|
||||
"measurement": "webhook",
|
||||
"fields": {"response_time": time, "success": int(success)}
|
||||
})
|
||||
|
||||
async def report_periodical_stats(self, 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 self.client.write({
|
||||
"measurement": "stats",
|
||||
"fields": {
|
||||
"systems": systems,
|
||||
"members": members,
|
||||
"messages": messages,
|
||||
"accounts": accounts
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue