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-11-18 15:42:27 -05:00
|
|
|
from os.path import dirname, join
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2017-05-19 18:31:26 -04:00
|
|
|
import pendulum
|
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-22 17:02:26 -05:00
|
|
|
from tsstats.log import Server
|
2017-05-19 18:31:26 -04: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
|
|
|
|
|
2017-02-11 16:14:34 -05:00
|
|
|
sort them, clean their nick-history and convert onlinetime to string
|
2016-11-24 16:57:56 -05:00
|
|
|
|
|
|
|
:param clients: List of clients to prepare
|
|
|
|
:param onlinetime_threshold: threshold for clients onlinetime
|
|
|
|
|
|
|
|
:type clients: tsstats.client.Clients
|
|
|
|
:type onlinetime_treshold: int
|
|
|
|
|
|
|
|
:return: `clients` sorted by onlinetime, kics, pkicks, bans and pbans
|
|
|
|
:rtype: tsstats.template.SortedClients
|
2016-11-19 16:50:28 -05:00
|
|
|
'''
|
|
|
|
# sort by onlinetime
|
|
|
|
onlinetime_ = sort_clients(
|
|
|
|
clients, lambda c: c.onlinetime.total_seconds()
|
|
|
|
)
|
|
|
|
# filter clients not matching threshold
|
2017-05-19 18:31:26 -04:00
|
|
|
onlinetime_ = filter_threshold(onlinetime_, onlinetime_threshold)
|
2016-11-19 16:50:28 -05:00
|
|
|
# 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-11-22 17:02:26 -05:00
|
|
|
def render_servers(servers, output, title='TeamspeakStats',
|
|
|
|
template='index.jinja2', datetime_fmt='%x %X %Z',
|
2017-05-19 19:00:12 -04:00
|
|
|
onlinetime_threshold=-1, lastseen_relative=True):
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
2016-11-22 17:02:26 -05:00
|
|
|
Render `servers`
|
2016-05-30 14:23:03 -04:00
|
|
|
|
2016-11-22 17:02:26 -05:00
|
|
|
:param servers: list of servers to render
|
2016-05-30 14:23:03 -04:00
|
|
|
: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
|
2017-05-19 19:00:12 -04:00
|
|
|
:param lastseen_relative: render last seen timestamp relative
|
2016-05-30 14:23:03 -04:00
|
|
|
|
2016-11-22 17:02:26 -05:00
|
|
|
|
|
|
|
:type servers: [tsstats.log.Server]
|
2016-05-30 14:23:03 -04:00
|
|
|
: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
|
2017-05-19 19:00:12 -04:00
|
|
|
:type lastseen_relative: bool
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
2016-11-22 17:02:26 -05:00
|
|
|
# preparse servers
|
|
|
|
prepared_servers = [
|
2016-11-23 15:23:20 -05:00
|
|
|
Server(sid, prepare_clients(clients, onlinetime_threshold))
|
2016-11-22 17:02:26 -05:00
|
|
|
for sid, clients in servers
|
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
|
2017-05-19 19:00:12 -04:00
|
|
|
|
|
|
|
def lastseen(timestamp):
|
|
|
|
if lastseen_relative:
|
|
|
|
return timestamp.diff_for_humans()
|
|
|
|
else:
|
|
|
|
return frmttime(timestamp)
|
2016-06-12 11:55:03 -04:00
|
|
|
template_env.filters['frmttime'] = frmttime
|
2017-05-19 19:00:12 -04:00
|
|
|
template_env.filters['lastseen'] = lastseen
|
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-22 17:02:26 -05:00
|
|
|
template.stream(title=title, servers=prepared_servers,
|
2016-11-18 16:11:14 -05:00
|
|
|
debug=logger.level <= logging.DEBUG,
|
2023-01-14 13:02:53 -05:00
|
|
|
creation_time=pendulum.now('UTC'))\
|
2016-11-18 16:11:14 -05:00
|
|
|
.dump(output, encoding='utf-8')
|
|
|
|
logger.debug('Wrote rendered template to %s', output)
|