Skip to content

Commit

Permalink
Fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed Jan 14, 2024
1 parent 6f68e13 commit b5d75f6
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 57 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ __pycache__/
.vscode/

sausage_bot/data/json
sausage_bot/data/db
sausage_bot/data/logs
sausage_bot/data/.env
sausage_bot/testing
sausage_bot/test/test_parse/*

out
sausage_bot/data/db
sausage_bot/test/test_parse/*
out
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ RUN pipenv install --system --deploy --ignore-pipfile
VOLUME [ "/data" ]

# Run bot
ENTRYPOINT ["python", "-m", "sausage_bot", "-l", "-lm", "-lp", "-d", "--data-dir", "/data"]
CMD ["python", "-m", "sausage_bot", "--log", "--verbose", "--log-print", "--log-database", "--debug", "--data-dir", "/data"]
85 changes: 57 additions & 28 deletions sausage_bot/cogs/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
from sausage_bot.util.log import log


def get_role_numbers():
def get_role_numbers(hide_bots: bool = None):
'Get roles and number of members'
guild = discord_commands.get_guild()
member_count = guild.member_count
log.debug(f'`hide_bots` is {hide_bots}')
roles_info = discord_commands.get_roles(
filter_zeroes=True, filter_bots=hide_bots
)
return {
'member_count': member_count,
'roles': discord_commands.get_roles(filter_zeroes=True)
'member_count': len(roles_info),
'roles': roles_info
}


Expand Down Expand Up @@ -44,37 +46,46 @@ def __init__(self, bot):
self.bot = bot

# Tasks
@tasks.loop(minutes=5)
@tasks.loop(
minutes=config.env.int('STATS_LOOP', default=5)
)
async def update_stats():
'''
Update interesting stats in a channel post and write the info to
the log db.
The channel is defined in stats settings db.
'''
def tabify(
async def tabify(
dict_in: dict,
headers: list,
):
hide_roles = stats_settings['hide_roles']
hide_roles_lower = [x.lower() for x in hide_roles]
hide_roles = await db_helper.get_output(
template_info=envs.stats_db_schema,
select=('value'),
where=[('setting', 'hide_role')]
)
hide_roles_lower = [x[0].lower() for x in hide_roles]
# TODO var msg
log.debug(f'Using this for filter:\n{hide_roles_lower}')
text_out = ''
if isinstance(dict_in, dict):
log.debug(
'Checking `sort_abc` ({}) and `sort_321` ({})'.format(
stats_settings['sort_roles_abc'],
stats_settings['sort_roles_321']
eval(stats_settings['sort_roles_abc']),
eval(stats_settings['sort_roles_321'])
)
)
if not eval(stats_settings['sort_roles_abc']) and\
not eval(stats_settings['sort_roles_321']):
stats_settings['sort_roles_abc'] = True
if stats_settings['sort_roles_abc']:
dict_in = dict(sorted(
dict_in.items(), key=lambda x: x[1]['name']
))
log.debug(
f'Sorting roles alphabetically: {list(dict_in)[0:4]}'
)
if stats_settings['sort_roles_321']:
elif eval(stats_settings['sort_roles_321']):
dict_in = dict(sorted(
dict_in.items(), key=lambda x: x[1]['members'],
reverse=True
Expand All @@ -83,6 +94,7 @@ def tabify(
f'Sorting roles by number of members: '
f'{list(dict_in)[0:4]}'
)

# Tabulate the output
dict_out = {
'name': [],
Expand All @@ -92,6 +104,7 @@ def tabify(
if role.lower() not in hide_roles_lower:
if role != '@everyone':
# Add an if to check for filter bot roles

dict_out['name'].append(dict_in[role]['name'])
dict_out['members'].append(
dict_in[role]['members']
Expand All @@ -112,6 +125,7 @@ def tabify(
template_info=envs.stats_db_schema
)
)
log.debug(f'`stats_settings` is {stats_settings}')
if stats_settings['channel']:
stats_channel = stats_settings['channel']
else:
Expand All @@ -122,18 +136,29 @@ def tabify(
lines_in_codebase = _codebase['total_lines']
files_in_codebase = _codebase['total_files']
# Get server members
members = get_role_numbers()
# Update log database if not alredy this day
members = get_role_numbers(
hide_bots=eval(stats_settings['hide_bot_roles'])
)
# Update log database if not already this day
log.debug('Logging stats')
date_exist = await db_helper.get_output(
template_info=envs.stats_db_log_schema,
order_by=[('datetime', 'DESC')],
single=True
)
if datetime_handling.get_dt(
format='date'
) > datetime_handling.get_dt(
format='date', dt=date_exist[0]
):
log_stats = False
if date_exist:
if datetime_handling.get_dt(
format='date'
) > datetime_handling.get_dt(
format='date', dt=date_exist
):
log_stats = True
else:
log.verbose('Today has already been logged, skipping...')
elif date_exist is None:
log_stats = True
if log_stats:
stats_log_inserts.append(
(
str(datetime_handling.get_dt('ISO8601')),
Expand All @@ -146,22 +171,26 @@ def tabify(
template_info=envs.stats_db_log_schema,
inserts=stats_log_inserts
)
else:
log.verbose('Today has already been logged, skipping...')
# Update the stats-msg
if stats_settings['show_role_stats']:
if eval(stats_settings['show_role_stats']):
total_members = members['member_count']
roles_members = tabify(
roles_members = await tabify(
dict_in=members['roles'], headers=['Rolle', 'Brukere']
)
dt_log = datetime_handling.get_dt('datetimefull')
stats_msg = ''
if stats_settings['show_role_stats']:
stats_msg += f'> Medlemmer\n```'\
log.debug('`show_role_stats` is {})'.format(
stats_settings['show_role_stats']
))
if eval(stats_settings['show_role_stats']):
stats_msg += f'### Medlemmer\n```'\
f'Antall medlemmer: {total_members}\n\n'\
f'{roles_members}```\n'
if stats_settings['show_code_stats']:
stats_msg += f'> Kodebase\n```'\
log.debug('`show_code_stats` is {}'.format(
stats_settings['show_code_stats']
))
if eval(stats_settings['show_code_stats']):
stats_msg += f'### Kodebase\n```'\
f'Antall filer med kode: {files_in_codebase}\n'\
f'Antall linjer med kode: {lines_in_codebase}```\n'
stats_msg += f'```(Serverstats sist oppdatert: {dt_log})```\n'
Expand All @@ -186,7 +215,7 @@ async def setup(bot):
log.verbose('Checking db')
# Convert json to sqlite db-files if exists
stats_file_inserts = None
stats_settings_inserts = None
stats_settings_inserts = envs.stats_db_schema['inserts']
stats_log_inserts = None
if file_io.file_size(envs.stats_file):
log.verbose('Found old json file')
Expand Down
3 changes: 1 addition & 2 deletions sausage_bot/util/cogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ def get_cogs_list(cogs_file):
log.debug(f'Got this from `cogs_file`: {cogs_file}')
text_out = '```{}```'.format(
tabulate(
cogs_file, headers=['Cog', 'Status'],
tablefmt='presto'
cogs_file, headers=['Cog', 'Status']
)
)
log.debug(f'Returning:\n{text_out}')
Expand Down
1 change: 1 addition & 0 deletions sausage_bot/util/db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ async def insert_many_all(
input_singles = False
input_multiples = False
_cmd = f'INSERT INTO {table_name} VALUES('
print(inserts)
if isinstance(inserts[0], str):
_cmd += ', '.join('?'*len(inserts))
input_singles = True
Expand Down
10 changes: 8 additions & 2 deletions sausage_bot/util/discord_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,21 @@ def get_sorted_scheduled_events():
return out


def get_roles(filter_zeroes=None):
def get_roles(filter_zeroes=None, filter_bots=None):
'''
Get a dict of all roles on server and their ID's
#autodoc skip#
'''
log.debug(f'`filter_zeroes` is {filter_zeroes}')
log.debug(f'`filter_bots` is {filter_bots}')
roles_dict = {}
# Get all roles and their IDs
for role in get_guild().roles:
if filter_zeroes and len(role.members) == 0:
continue
if filter_bots:
if role.is_bot_managed():
continue
roles_dict[role.name.lower()] = {
'name': role.name,
'id': role.id,
Expand Down Expand Up @@ -217,11 +222,12 @@ async def update_stats_post(stats_info, stats_channel):
'''
server_channels = get_text_channel_list()
if stats_channel in server_channels:
log.debug(f'Found stats channel {stats_channel}')
channel_out = config.bot.get_channel(server_channels[stats_channel])
found_stats_msg = False
async for msg in channel_out.history(limit=10):
# TODO var msg
log.debug(f'Got msg: ({msg.author.id}) {msg.content[0:50]}')
log.debug(f'Got msg: ({msg.author.id}) {msg.content[0:50]}...')
if str(msg.author.id) == config.BOT_ID:
if 'Serverstats sist' in str(msg.content):
# TODO var msg
Expand Down
9 changes: 9 additions & 0 deletions sausage_bot/util/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@
'items': [
'setting TEXT NOT NULL',
'value TEXT NOT NULL'
],
'inserts': [
['channel', ''],
['hide_role', ''],
['hide_bot_roles', True],
['show_code_stats', False],
['show_role_stats', True],
['sort_roles_abc', False],
['sort_roles_321', False]
]
}

Expand Down
3 changes: 1 addition & 2 deletions sausage_bot/util/feeds_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,7 @@ def split_list(lst, chunk_size):
table_out = tabulate(
tabular_data=feeds_out,
headers=headers,
maxcolwidths=maxcolwidths,
tablefmt='presto'
maxcolwidths=maxcolwidths
)
return split_lengthy_list(table_out)

Expand Down
39 changes: 20 additions & 19 deletions sausage_bot/util/log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,28 @@ def log_function(
pretty Prettify specific output. Works on dict, list and tuple
sameline Reuse line for next output
'''
if color is None:
color = Fore.GREEN
else:
color = eval('Fore.{}'.format(color.upper()))
if extra_color is None:
extra_color = Fore.GREEN
else:
extra_color = eval('Fore.{}'.format(extra_color.upper()))
if args.log_print:
if color is None:
color = Fore.GREEN
else:
color = eval('Fore.{}'.format(color.upper()))
if extra_color is None:
extra_color = Fore.GREEN
else:
extra_color = eval('Fore.{}'.format(extra_color.upper()))
function_name = log_func_name()
if args.log_highlight is not None and str(args.log_highlight)\
in function_name['name']:
color = Fore.RED
if args.log_print:
if args.log_highlight is not None and str(args.log_highlight)\
in function_name['name']:
color = Fore.RED
dt = pendulum.now(config.TIMEZONE)
_dt_full = dt.format(f'DD.MM.YYYY HH.mm.ss')
log_out = '{color}{style}[ {dt} ] '.format(
color=color,
style=Style.BRIGHT,
dt=_dt_full
)
if args.log_print:
log_out = '{color}{style}[ {dt} ] '.format(
color=color,
style=Style.BRIGHT,
dt=_dt_full
)
if args.log_print:
if extra_info:
log_out += '[ {extra_info} ]'.format(
Expand All @@ -87,15 +90,14 @@ def log_function(
else:
log_out += str(log_in)
if sameline:
# TODO Denne fungerer nok kanskje ikke helt som forventa?
try:
max_cols, max_rows = os.get_terminal_size(0)
except (OSError):
max_cols = 0
msg_len = len(str(log_out))
rem_len = max_cols - msg_len - 2
print('{}{}'.format(
log_out, ' ' * rem_len
log_out, ' '*rem_len
), end='\r')
else:
print(log_out)
Expand All @@ -105,7 +107,6 @@ def log_function(
log_out += '[ {} ] '.format(function_name['name'])
log_out += str(log_in)
else:
log_out += '\n'
dt = pendulum.now(config.TIMEZONE)
_dt_rev = dt.format(f'YYYY-MM-DD HH.mm.ss')
_logfilename = envs.LOG_DIR / f'{_dt_rev}.log'
Expand Down

0 comments on commit b5d75f6

Please sign in to comment.