Make InfluxDB connection options configurable

This commit is contained in:
Ske 2018-09-01 19:16:42 +02:00
parent a130e2215a
commit 99e2fad2b2
3 changed files with 19 additions and 11 deletions

View File

@ -16,6 +16,9 @@ services:
- "DATABASE_NAME=postgres" - "DATABASE_NAME=postgres"
- "DATABASE_HOST=db" - "DATABASE_HOST=db"
- "DATABASE_PORT=5432" - "DATABASE_PORT=5432"
- "INFLUX_HOST=influx"
- "INFLUX_PORT=8086"
- "INFLUX_DB=pluralkit"
restart: always restart: always
api: api:
build: src/ build: src/

View File

@ -43,13 +43,13 @@ class PluralKitBot:
# Ignore bot messages # Ignore bot messages
if message.author.bot: if message.author.bot:
return return
if await self.handle_command_dispatch(message): if await self.handle_command_dispatch(message):
return return
if await self.handle_proxy_dispatch(message): if await self.handle_proxy_dispatch(message):
return return
async def on_socket_raw_receive(self, msg): async def on_socket_raw_receive(self, msg):
# Since on_reaction_add is buggy (only works for messages the bot's already cached, ie. no old messages) # Since on_reaction_add is buggy (only works for messages the bot's already cached, ie. no old messages)
# we parse socket data manually for the reaction add event # we parse socket data manually for the reaction add event
@ -77,7 +77,7 @@ class PluralKitBot:
async def handle_command_dispatch(self, message): async def handle_command_dispatch(self, message):
command_items = commands.command_list.items() command_items = commands.command_list.items()
command_items = sorted(command_items, key=lambda x: len(x[0]), reverse=True) command_items = sorted(command_items, key=lambda x: len(x[0]), reverse=True)
prefix = "pk;" prefix = "pk;"
for command_name, command in command_items: for command_name, command in command_items:
if message.content.lower().startswith(prefix + command_name): if message.content.lower().startswith(prefix + command_name):
@ -99,7 +99,7 @@ class PluralKitBot:
await self.stats.report_command(command_name, execution_time, response_time) await self.stats.report_command(command_name, execution_time, response_time)
return True return True
async def handle_proxy_dispatch(self, message): async def handle_proxy_dispatch(self, message):
# Try doing proxy parsing # Try doing proxy parsing
async with self.pool.acquire() as conn: async with self.pool.acquire() as conn:
@ -126,11 +126,16 @@ class PluralKitBot:
async with self.pool.acquire() as conn: async with self.pool.acquire() as conn:
await db.create_tables(conn) await db.create_tables(conn)
self.logger.info("Connecting to InfluxDB...") if "INFLUX_HOST" in os.environ:
self.stats = await InfluxStatCollector.connect() self.logger.info("Connecting to InfluxDB...")
self.stats = await InfluxStatCollector.connect(
os.environ["INFLUX_HOST"],
os.environ["INFLUX_PORT"],
os.environ["INFLUX_DB"]
)
self.logger.info("Starting periodical stat reporting...") self.logger.info("Starting periodical stat reporting...")
asyncio.get_event_loop().create_task(self.periodical_stat_timer(self.pool)) asyncio.get_event_loop().create_task(self.periodical_stat_timer(self.pool))
self.logger.info("Connecting to Discord...") self.logger.info("Connecting to Discord...")
await self.client.start(self.token) await self.client.start(self.token)

View File

@ -21,9 +21,9 @@ class NullStatCollector(StatCollector):
class InfluxStatCollector(StatCollector): class InfluxStatCollector(StatCollector):
@staticmethod @staticmethod
async def connect(): async def connect(host: str, port: int, db: str):
client = InfluxDBClient(host="influx", db="pluralkit") client = InfluxDBClient(host=host, port=port, db=db)
await client.create_database(db="pluralkit") await client.create_database(db=db)
return InfluxStatCollector(client) return InfluxStatCollector(client)