make get_sorted public and move seconds-formatting into own function
This commit is contained in:
parent
4ad9bf1b44
commit
98a4274981
49
tsstats.py
49
tsstats.py
|
@ -124,6 +124,20 @@ if len(path_split) > 0:
|
||||||
abspath += sep
|
abspath += sep
|
||||||
|
|
||||||
|
|
||||||
|
def _get_sorted(stor, key):
|
||||||
|
clients = stor.values()
|
||||||
|
return sorted([(client, client[key]) for client in clients if client[key] > 0], key=lambda data: data[1], reverse=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _format_seconds(seconds):
|
||||||
|
minutes, seconds = divmod(seconds, 60)
|
||||||
|
hours, minutes = divmod(minutes, 60)
|
||||||
|
hours = str(hours) + 'h ' if hours > 0 else ''
|
||||||
|
minutes = str(minutes) + 'm ' if minutes > 0 else ''
|
||||||
|
seconds = str(seconds) + 's' if seconds > 0 else ''
|
||||||
|
return hours + minutes + seconds
|
||||||
|
|
||||||
|
|
||||||
def parse_logs(logpath, file_log=False):
|
def parse_logs(logpath, file_log=False):
|
||||||
# setup logging
|
# setup logging
|
||||||
log = logging.getLogger()
|
log = logging.getLogger()
|
||||||
|
@ -180,33 +194,20 @@ def parse_logs(logpath, file_log=False):
|
||||||
def render_template(output, template_name='template.html', title='TeamspeakStats', debug=False):
|
def render_template(output, template_name='template.html', title='TeamspeakStats', debug=False):
|
||||||
# render template
|
# render template
|
||||||
template = Environment(loader=FileSystemLoader(abspath)).get_template('template.html')
|
template = Environment(loader=FileSystemLoader(abspath)).get_template('template.html')
|
||||||
cl_by_id = clients.clients_by_id
|
|
||||||
cl_by_uid = clients.clients_by_uid
|
|
||||||
|
|
||||||
def get_sorted(key, uid):
|
clients_onlinetime_ = _get_sorted(clients.clients_by_id, 'onlinetime')
|
||||||
clients = cl_by_uid.values() if uid else cl_by_id.values()
|
clients_onlinetime = [(client, _format_seconds(onlinetime)) for client, onlinetime in clients_onlinetime_]
|
||||||
return sorted([(client, client[key]) for client in clients if client[key] > 0], key=lambda data: data[1], reverse=True)
|
|
||||||
|
|
||||||
clients_onlinetime_ = get_sorted('onlinetime', False)
|
clients_kicks = _get_sorted(clients.clients_by_uid, 'kicks')
|
||||||
clients_onlinetime = []
|
clients_pkicks = _get_sorted(clients.clients_by_id, 'pkicks')
|
||||||
for client, onlinetime in clients_onlinetime_:
|
clients_bans = _get_sorted(clients.clients_by_uid, 'bans')
|
||||||
minutes, seconds = divmod(client.onlinetime, 60)
|
clients_pbans = _get_sorted(clients.clients_by_id, 'pbans')
|
||||||
hours, minutes = divmod(minutes, 60)
|
objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks),
|
||||||
hours = str(hours) + 'h ' if hours > 0 else ''
|
('passive Kicks', clients_pkicks),
|
||||||
minutes = str(minutes) + 'm ' if minutes > 0 else ''
|
('Bans', clients_bans), ('passive Bans', clients_pbans)] # (headline, list)
|
||||||
seconds = str(seconds) + 's' if seconds > 0 else ''
|
|
||||||
clients_onlinetime.append((client, hours + minutes + seconds))
|
|
||||||
|
|
||||||
clients_kicks = get_sorted('kicks', True)
|
with open(output, 'w') as f:
|
||||||
clients_pkicks = get_sorted('pkicks', False)
|
f.write(template.render(title=title, objs=objs, debug=debug))
|
||||||
clients_bans = get_sorted('bans', True)
|
|
||||||
clients_pbans = get_sorted('pbans', False)
|
|
||||||
objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks),
|
|
||||||
('passive Kicks', clients_pkicks),
|
|
||||||
('Bans', clients_bans), ('passive Bans', clients_pbans)] # (headline, list)
|
|
||||||
|
|
||||||
with open(output_path, 'w') as f:
|
|
||||||
f.write(template.render(title=title, objs=objs, debug=debug))
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Reference in New Issue