diff --git a/tsstats/template.py b/tsstats/template.py index 90b279e..da45d95 100644 --- a/tsstats/template.py +++ b/tsstats/template.py @@ -7,13 +7,14 @@ from os.path import dirname from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader -from tsstats.utils import seconds_to_text, sort_clients +from tsstats.utils import filter_threshold, seconds_to_text, sort_clients logger = logging.getLogger('tsstats') def render_template(clients, output, title='TeamspeakStats', - template_path='template.html', datetime_fmt='%x %X %Z'): + template_path='template.html', datetime_fmt='%x %X %Z', + onlinetime_threshold=-1): ''' render template with `clients` @@ -23,6 +24,7 @@ def render_template(clients, output, title='TeamspeakStats', :param title: title of the resulting html-document :param template_path: path to template-file :param datetime_fmt: custom datetime-format + :param onlinetime_threshold: threshold for clients onlinetime :type clients: tsstats.client.Clients :type output: str @@ -30,11 +32,16 @@ def render_template(clients, output, title='TeamspeakStats', :type title: str :type template_path: str :type datetime_fmt: str + :type onlinetime_threshold: int ''' # prepare clients clients_onlinetime_ = sort_clients( clients, lambda c: c.onlinetime.total_seconds() ) + # filter clients for onlinetime threshold + clients_onlinetime_ = filter_threshold(clients_onlinetime_, + onlinetime_threshold) + clients_onlinetime = [ (client, seconds_to_text(int(onlinetime))) for client, onlinetime in clients_onlinetime_ diff --git a/tsstats/utils.py b/tsstats/utils.py index c2bde20..e935624 100644 --- a/tsstats/utils.py +++ b/tsstats/utils.py @@ -36,3 +36,16 @@ def seconds_to_text(seconds): minutes = str(minutes) + 'm ' if minutes > 0 else '' seconds = str(seconds) + 's' if seconds > 0 else '' return hours + minutes + seconds + + +def filter_threshold(clients, threshold): + ''' + Filter clients by threshold + + :param clients: List of clients as returned by tsstats.utils.sort_clients + :type clients: list + + :return: Clients matching given threshold + :rtype: list + ''' + return list(filter(lambda c: c[1] > threshold, clients))