Add onlinetime_threshold-arg to render_template

and filter_threshold-function to tsstats.utils.
Only display clients in onlinetime-section with a onlinetime
greater than onlinetime_threshold seconds
This commit is contained in:
Thor77 2016-11-11 18:37:33 +01:00
parent 17392494ed
commit 4ac27143a6
2 changed files with 22 additions and 2 deletions

View File

@ -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_

View File

@ -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))