Skip to content

Commit

Permalink
Make relative last seen time optional
Browse files Browse the repository at this point in the history
by introducing --lastseenabsolute flag and
tsstats.template.render_servers(lastseen_relative=True)
  • Loading branch information
Thor77 committed Jun 2, 2017
1 parent 6b02a4a commit 30eaee3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
11 changes: 10 additions & 1 deletion tsstats/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def cli():
'-otth', '--onlinetimethreshold',
type=int, help='threshold for displaying onlinetime (in seconds)'
)
parser.add_argument(
'-lsa', '--lastseenabsolute',
help='render last seen timestamp absolute (instead of relative)',
action='store_false', dest='lastseenrelative'
)
options = parser.parse_args()
if 'config' in options:
configuration = config.load(options.config)
Expand Down Expand Up @@ -115,7 +120,11 @@ def main(configuration):
template=configuration.get('General', 'template'),
datetime_fmt=configuration.get('General', 'datetimeformat'),
onlinetime_threshold=int(configuration.get(
'General', 'onlinetimethreshold'))
'General', 'onlinetimethreshold'
)),
lastseen_relative=configuration.getboolean(
'General', 'lastseenrelative'
)
)
logger.info('Finished after %s seconds', time() - start_time)

Expand Down
3 changes: 2 additions & 1 deletion tsstats/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'onlinedc': True,
'template': 'index.jinja2',
'datetimeformat': '%x %X %Z',
'onlinetimethreshold': -1
'onlinetimethreshold': -1,
'lastseenrelative': True
}
}

Expand Down
11 changes: 10 additions & 1 deletion tsstats/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def prepare_clients(clients, onlinetime_threshold=-1):

def render_servers(servers, output, title='TeamspeakStats',
template='index.jinja2', datetime_fmt='%x %X %Z',
onlinetime_threshold=-1):
onlinetime_threshold=-1, lastseen_relative=True):
'''
Render `servers`
Expand All @@ -70,6 +70,7 @@ def render_servers(servers, output, title='TeamspeakStats',
:param template_path: path to template-file
:param datetime_fmt: custom datetime-format
:param onlinetime_threshold: threshold for clients onlinetime
:param lastseen_relative: render last seen timestamp relative
:type servers: [tsstats.log.Server]
Expand All @@ -79,6 +80,7 @@ def render_servers(servers, output, title='TeamspeakStats',
:type template_path: str
:type datetime_fmt: str
:type onlinetime_threshold: int
:type lastseen_relative: bool
'''
# preparse servers
prepared_servers = [
Expand All @@ -98,7 +100,14 @@ def frmttime(timestamp):
formatted = timestamp.strftime(datetime_fmt)
logger.debug('Formatting timestamp %s -> %s', timestamp, formatted)
return formatted

def lastseen(timestamp):
if lastseen_relative:
return timestamp.diff_for_humans()
else:
return frmttime(timestamp)
template_env.filters['frmttime'] = frmttime
template_env.filters['lastseen'] = lastseen
template = template_env.get_template(template)
logger.debug('Rendering template %s', template)
template.stream(title=title, servers=prepared_servers,
Expand Down
2 changes: 1 addition & 1 deletion tsstats/templates/stats.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{% set id = [headline_id, client.nick|striptags]|join('.') %}
<li id="{{ id }}" class="list-group-item{{ ' list-group-item-success' if client.connected else loop.cycle('" style="background-color: #eee;', '') }}">
<span class="hint--right hint--medium--xs" data-hint="{{ client.nick_history|join(', ') }}"><a href="#{{ id }}">{{ client.nick }}{{ " (" + client.identifier + ")" if debug }}</a></span>
<span class="badge"><div{% if not client.connected and headline == 'Onlinetime' %} class="hint--left" data-hint="{{ client.last_seen.diff_for_humans() }}"{% endif %}>{{ value }}</div></span>
<span class="badge"><div{% if not client.connected and headline == 'Onlinetime' %} class="hint--left" data-hint="{{ client.last_seen|lastseen }}"{% endif %}>{{ value }}</div></span>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit 30eaee3

Please sign in to comment.