2016-06-07 11:42:53 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
import logging
|
2016-11-19 16:50:28 -05:00
|
|
|
from collections import namedtuple
|
2016-07-04 15:14:29 -04:00
|
|
|
from datetime import datetime
|
2016-11-18 15:42:27 -05:00
|
|
|
from os.path import dirname, join
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-06-07 11:47:47 -04:00
|
|
|
from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-11-11 12:37:33 -05:00
|
|
|
from tsstats.utils import filter_threshold, seconds_to_text, sort_clients
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
logger = logging.getLogger('tsstats')
|
|
|
|
|
2016-11-19 16:50:28 -05:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-08-10 16:47:19 -04:00
|
|
|
def render_template(clients, output, title='TeamspeakStats',
|
2016-11-18 15:51:47 -05:00
|
|
|
template='stats.jinja2', datetime_fmt='%x %X %Z',
|
2016-11-11 12:37:33 -05:00
|
|
|
onlinetime_threshold=-1):
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
|
|
|
render template with `clients`
|
|
|
|
|
|
|
|
:param clients: clients to fill template with
|
|
|
|
:param output: path to output-file
|
|
|
|
:param template_name: path to template-file
|
|
|
|
:param title: title of the resulting html-document
|
2016-08-10 16:47:19 -04:00
|
|
|
:param template_path: path to template-file
|
2016-09-17 16:49:05 -04:00
|
|
|
:param datetime_fmt: custom datetime-format
|
2016-11-11 12:37:33 -05:00
|
|
|
:param onlinetime_threshold: threshold for clients onlinetime
|
2016-05-30 14:23:03 -04:00
|
|
|
|
|
|
|
:type clients: tsstats.client.Clients
|
|
|
|
:type output: str
|
|
|
|
:type template_name: str
|
|
|
|
:type title: str
|
2016-08-10 16:47:19 -04:00
|
|
|
:type template_path: str
|
2016-09-17 16:49:05 -04:00
|
|
|
:type datetime_fmt: str
|
2016-11-11 12:37:33 -05:00
|
|
|
:type onlinetime_threshold: int
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
2016-11-19 16:50:28 -05:00
|
|
|
prepared_clients = prepare_clients(clients)
|
|
|
|
objs = [
|
|
|
|
('Onlinetime', prepared_clients.onlinetime),
|
|
|
|
('Kicks', prepared_clients.kicks),
|
|
|
|
('passive Kicks', prepared_clients.pkicks),
|
|
|
|
('Bans', prepared_clients.bans),
|
|
|
|
('passive Bans', prepared_clients.pbans)
|
2016-05-08 15:32:37 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
# render
|
2016-06-07 11:47:47 -04:00
|
|
|
template_loader = ChoiceLoader([
|
2016-11-18 15:42:27 -05:00
|
|
|
PackageLoader(__package__, 'templates'),
|
|
|
|
FileSystemLoader(join(dirname(__file__), 'templates'))
|
2016-06-07 11:47:47 -04:00
|
|
|
])
|
2016-05-08 15:32:37 -04:00
|
|
|
template_env = Environment(loader=template_loader)
|
|
|
|
|
2016-06-12 11:55:03 -04:00
|
|
|
def frmttime(timestamp):
|
2016-06-12 12:01:47 -04:00
|
|
|
if not timestamp:
|
|
|
|
return ''
|
2016-09-18 15:51:24 -04:00
|
|
|
formatted = timestamp.strftime(datetime_fmt)
|
|
|
|
logger.debug('Formatting timestamp %s -> %s', timestamp, formatted)
|
|
|
|
return formatted
|
2016-06-12 11:55:03 -04:00
|
|
|
template_env.filters['frmttime'] = frmttime
|
2016-11-18 15:46:38 -05:00
|
|
|
template = template_env.get_template(template)
|
2016-09-18 15:51:24 -04:00
|
|
|
logger.debug('Rendering template %s', template)
|
2016-11-18 16:11:14 -05:00
|
|
|
template.stream(title=title, objs=objs,
|
|
|
|
debug=logger.level <= logging.DEBUG,
|
|
|
|
creation_time=datetime.utcnow())\
|
|
|
|
.dump(output, encoding='utf-8')
|
|
|
|
logger.debug('Wrote rendered template to %s', output)
|