1
0
Fork 0
mirror of synced 2024-05-19 20:12:30 +12:00

Update to change cache lookup from o(n) to o(1)

This commit is contained in:
phxntxm 2018-09-23 13:33:46 -05:00
parent e7c2cdceb7
commit e5b5d30553
5 changed files with 35 additions and 37 deletions

View file

@ -47,7 +47,11 @@ class Birthday:
member_ids = [str(m.id) for m in server.members] member_ids = [str(m.id) for m in server.members]
# Now create a list comparing to the server's list of member IDs # Now create a list comparing to the server's list of member IDs
bds = [x for x in bds if x['member_id'] in member_ids] bds = [
bd
for member_id, bd in bds.items()
if str(member_id) in member_ids
]
_entries = [] _entries = []

View file

@ -60,13 +60,13 @@ class StatsUpdate:
log.info('discordbots.com statistics retruned {} for {}'.format(resp.status, payload)) log.info('discordbots.com statistics retruned {} for {}'.format(resp.status, payload))
async def on_guild_join(self, _): async def on_guild_join(self, _):
self.bot.loop.create_task(self.update()) await self.update()
async def on_guild_leave(self, _): async def on_guild_leave(self, _):
self.bot.loop.create_task(self.update()) await self.update()
async def on_ready(self): async def on_ready(self):
self.bot.loop.create_task(self.update()) await self.update()
async def on_member_join(self, member): async def on_member_join(self, member):
guild = member.guild guild = member.guild

View file

@ -34,8 +34,8 @@ class Raffle:
if raffles is None: if raffles is None:
return return
for raffle in raffles: for server_id, raffle in raffles.items():
server = self.bot.get_guild(int(raffle['server_id'])) server = self.bot.get_guild(int(server_id))
# Check to see if this cog can find the server in question # Check to see if this cog can find the server in question
if server is None: if server is None:

View file

@ -193,8 +193,12 @@ class Stats:
command_stats = self.bot.db.load('command_usage') command_stats = self.bot.db.load('command_usage')
# Now use a dictionary comprehension to get just the command name, and usage # Now use a dictionary comprehension to get just the command name, and usage
# Based on the author's usage of the command # Based on the author's usage of the command
stats = {data['command']: data['member_usage'].get(str(author.id)) for data in command_stats
if data['member_usage'].get(str(author.id), 0) > 0} stats = {
command: data["member_usage"].get(str(author.id))
for command, data in command_stats.items()
if data["member_usage"].get(str(author.id), 0) > 0
}
# Now sort it by the amount of times used # Now sort it by the amount of times used
sorted_stats = sorted(stats.items(), key=lambda x: x[1], reverse=True) sorted_stats = sorted(stats.items(), key=lambda x: x[1], reverse=True)
@ -213,8 +217,11 @@ class Stats:
# This is exactly the same as above, except server usage instead of member usage # This is exactly the same as above, except server usage instead of member usage
server = ctx.message.guild server = ctx.message.guild
command_stats = self.bot.db.load('command_usage') command_stats = self.bot.db.load('command_usage')
stats = {data['command']: data['server_usage'].get(str(server.id)) for data in command_stats stats = {
if data['server_usage'].get(str(server.id), 0) > 0} command: data['server_usage'].get(str(server.id))
for command, data in command_stats.items()
if data['server_usage'].get(str(server.id), 0) > 0
}
sorted_stats = sorted(stats.items(), key=lambda x: x[1], reverse=True) sorted_stats = sorted(stats.items(), key=lambda x: x[1], reverse=True)
try: try:
top_5 = [(data[0], data[1]) for data in sorted_stats[:5]] top_5 = [(data[0], data[1]) for data in sorted_stats[:5]]
@ -314,7 +321,11 @@ class Stats:
if battles is None or len(battles) == 0: if battles is None or len(battles) == 0:
await ctx.send("No one has battled on this server!") await ctx.send("No one has battled on this server!")
battles = [battle for battle in battles if int(battle['member_id']) in server_member_ids] battles = [
battle
for member_id, battle in battles.items()
if int(member_id) in server_member_ids
]
# Sort the members based on their rating # Sort the members based on their rating
sorted_members = sorted(battles, key=lambda k: k['rating'], reverse=True) sorted_members = sorted(battles, key=lambda k: k['rating'], reverse=True)

View file

@ -27,12 +27,12 @@ class Cache:
self.key = key # The name of primary key self.key = key # The name of primary key
self.db = db # The database class connections are made through self.db = db # The database class connections are made through
self.loop = loop self.loop = loop
self.values = [] # The values returned from the database self.values = {} # The values returned from the database
self.refreshed_time = None self.refreshed_time = None
self.loop.create_task(self.refresh_task()) self.loop.create_task(self.refresh_task())
async def refresh(self): async def refresh(self):
self.values = await self.db.actual_load(self.table) self.values = await self.db.query(r.table(self.table).group(self.key)[0])
self.refreshed_time = datetime.now() self.refreshed_time = datetime.now()
async def refresh_task(self): async def refresh_task(self):
@ -47,30 +47,15 @@ class Cache:
if difference.total_seconds() > 300: if difference.total_seconds() > 300:
await self.refresh() await self.refresh()
def get(self, key=None, table_filter=None, pluck=None): def get(self, key=None, pluck=None):
"""This simulates the database call, to make it easier to get the data""" """This simulates the database call, to make it easier to get the data"""
if key is None and table_filter is None: value = self.values
return self.values if key:
elif key: value = value.get(str(key))
key = str(key) if pluck:
for value in self.values: value = value.get(pluck)
if value[self.key] == key:
if pluck:
return value.get(pluck)
else:
return value
elif table_filter:
req_key = list(table_filter.keys())[0]
req_val = list(table_filter.values())[0]
matched_values = []
for value in self.values:
if value[req_key] == req_val:
if pluck:
return value.get(pluck)
else:
matched_values.append(value)
return matched_values return value
class DB: class DB:
@ -121,8 +106,6 @@ class DB:
await self.cache.get(table).refresh() await self.cache.get(table).refresh()
def load(self, table, **kwargs): def load(self, table, **kwargs):
if kwargs.get('key'):
kwargs['key'] = str(kwargs.get('key'))
return self.cache.get(table).get(**kwargs) return self.cache.get(table).get(**kwargs)
async def actual_load(self, table, key=None, table_filter=None, pluck=None): async def actual_load(self, table, key=None, table_filter=None, pluck=None):