Refactor clients preperation into prepare_clients

This commit is contained in:
Thor77 2016-11-19 22:50:28 +01:00
parent cbbedfa669
commit 51225175ce
1 changed files with 38 additions and 19 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
from collections import namedtuple
from datetime import datetime from datetime import datetime
from os.path import dirname, join from os.path import dirname, join
@ -10,6 +11,36 @@ from tsstats.utils import filter_threshold, seconds_to_text, sort_clients
logger = logging.getLogger('tsstats') logger = logging.getLogger('tsstats')
SortedClients = namedtuple('SortedClients', [
'onlinetime', 'kicks', 'pkicks', 'bans', 'pbans'])
def prepare_clients(clients, onlinetime_threshold=-1):
'''
Prepare `clients` for rendering
sort them and convert onlinetime to string
'''
# sort by onlinetime
onlinetime_ = sort_clients(
clients, lambda c: c.onlinetime.total_seconds()
)
# filter clients not matching threshold
onlinetime_ = filter_threshold(onlinetime_,
onlinetime_threshold)
# convert timespans to text
onlinetime = [
(client, seconds_to_text(int(onlinetime)))
for client, onlinetime in onlinetime_
]
return SortedClients(
onlinetime=onlinetime,
kicks=sort_clients(clients, lambda c: c.kicks),
pkicks=sort_clients(clients, lambda c: c.pkicks),
bans=sort_clients(clients, lambda c: c.bans),
pbans=sort_clients(clients, lambda c: c.pbans)
)
def render_template(clients, output, title='TeamspeakStats', def render_template(clients, output, title='TeamspeakStats',
template='stats.jinja2', datetime_fmt='%x %X %Z', template='stats.jinja2', datetime_fmt='%x %X %Z',
@ -33,27 +64,15 @@ def render_template(clients, output, title='TeamspeakStats',
:type datetime_fmt: str :type datetime_fmt: str
:type onlinetime_threshold: int :type onlinetime_threshold: int
''' '''
# prepare clients prepared_clients = prepare_clients(clients)
clients_onlinetime_ = sort_clients( objs = [
clients, lambda c: c.onlinetime.total_seconds() ('Onlinetime', prepared_clients.onlinetime),
) ('Kicks', prepared_clients.kicks),
# filter clients for onlinetime threshold ('passive Kicks', prepared_clients.pkicks),
clients_onlinetime_ = filter_threshold(clients_onlinetime_, ('Bans', prepared_clients.bans),
onlinetime_threshold) ('passive Bans', prepared_clients.pbans)
clients_onlinetime = [
(client, seconds_to_text(int(onlinetime)))
for client, onlinetime in clients_onlinetime_
] ]
clients_kicks = sort_clients(clients, lambda c: c.kicks)
clients_pkicks = sort_clients(clients, lambda c: c.pkicks)
clients_bans = sort_clients(clients, lambda c: c.bans)
clients_pbans = sort_clients(clients, lambda c: c.pbans)
objs = [('Onlinetime', clients_onlinetime), ('Kicks', clients_kicks),
('passive Kicks', clients_pkicks),
('Bans', clients_bans), ('passive Bans', clients_pbans)]
# render # render
template_loader = ChoiceLoader([ template_loader = ChoiceLoader([
PackageLoader(__package__, 'templates'), PackageLoader(__package__, 'templates'),