mirror of
https://github.com/Thor77/TeamspeakStats.git
synced 2025-03-14 02:24:33 -04:00
This adds support for a more expressive (albeit more verbose) IdentMap structure. It makes it easier to annotate the structure with additional data (such as names to associate with the IDs), to assist with maintaining the IdentMap.
99 lines
2.4 KiB
Python
99 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
import datetime
|
|
|
|
|
|
def sort_clients(clients, key_l):
|
|
'''
|
|
sort `clients` by `key`
|
|
|
|
:param clients: clients to sort
|
|
:param key_l: lambda/function returning the value of `key` for a client
|
|
|
|
:type clients: tsstats.client.Clients
|
|
:type key_l: function
|
|
|
|
:return: sorted `clients`
|
|
:rtype: list
|
|
'''
|
|
cl_data = [
|
|
(client, key_l(client)) for client in clients if key_l(client) > 0
|
|
]
|
|
return sorted(cl_data, key=lambda data: data[1], reverse=True)
|
|
|
|
|
|
def seconds_to_text(seconds):
|
|
'''
|
|
convert `seconds` to a text-representation
|
|
|
|
:param seconds: seconds to convert
|
|
:type seconds: int
|
|
|
|
:return: `seconds` as text-representation
|
|
:rtype: str
|
|
'''
|
|
minutes, seconds = divmod(seconds, 60)
|
|
hours, minutes = divmod(minutes, 60)
|
|
hours = str(hours) + 'h ' if hours > 0 else ''
|
|
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))
|
|
|
|
|
|
class UTC(datetime.tzinfo):
|
|
'''
|
|
Reimplementation of `timezone.utc` for Python2-Compatibility
|
|
'''
|
|
|
|
def utcoffset(self, dt):
|
|
return datetime.timedelta(0)
|
|
|
|
def dst(self, dt):
|
|
return datetime.timedelta(0)
|
|
|
|
def tzname(self, dt):
|
|
return 'UTC'
|
|
|
|
|
|
def tz_aware_datime(datetime, timezone=UTC()):
|
|
'''
|
|
Make `datetime` aware of it's timezone (UTC by default)
|
|
|
|
:param datetime: Target datetime
|
|
:param timezone: Target timezone
|
|
|
|
:type datetime: datetime.datetime
|
|
:type timezone: datetime.timezone
|
|
'''
|
|
return datetime.replace(tzinfo=timezone)
|
|
|
|
|
|
def transform_pretty_identmap(pretty_identmap):
|
|
'''
|
|
Transforms a list of client ID mappings from a more descriptive format
|
|
to the traditional format of alternative IDs to actual ID.
|
|
|
|
:param pretty_identmap: ID mapping in "nice" form
|
|
:type pretty_identmap: list
|
|
|
|
:return: ID mapping in simple key/value pairs
|
|
:rtype: dict
|
|
'''
|
|
|
|
final_identmap = {}
|
|
for mapping in pretty_identmap:
|
|
for alt_id in mapping['alternate_ids']:
|
|
final_identmap[alt_id] = mapping['primary_id']
|
|
return final_identmap
|