diff options
author | Lukas Metzger | 2020-07-02 12:30:15 +0200 |
---|---|---|
committer | Lukas Metzger | 2020-07-02 12:30:15 +0200 |
commit | def8d90e3b7ca51e2a14f0f631b84e4842f04c49 (patch) | |
tree | b8ddc32c784d729a7bf6ec9597cc50a3d8bccad6 | |
parent | Remove unneccessary debug output (diff) | |
download | bwlp-statistics-def8d90e3b7ca51e2a14f0f631b84e4842f04c49.tar.gz bwlp-statistics-def8d90e3b7ca51e2a14f0f631b84e4842f04c49.tar.xz bwlp-statistics-def8d90e3b7ca51e2a14f0f631b84e4842f04c49.zip |
Added prettytime function and use it
-rw-r--r-- | dash/pages/locations.py | 3 | ||||
-rw-r--r-- | dash/pages/satellites.py | 6 | ||||
-rw-r--r-- | dash/pages/total.py | 9 | ||||
-rw-r--r-- | dash/prettytime.py | 17 |
4 files changed, 27 insertions, 8 deletions
diff --git a/dash/pages/locations.py b/dash/pages/locations.py index 9125ba0..13aa752 100644 --- a/dash/pages/locations.py +++ b/dash/pages/locations.py @@ -10,6 +10,7 @@ import plotly.graph_objects as go import datetime as dt from natsort import natsorted +from prettytime import prettytime import db from app import app @@ -89,7 +90,7 @@ def make_content_all(days, date): figure.add_trace(go.Pie( labels=[item['locationname'][0:9] for item in data if item['ip'] == sat], values=[item['totalSessionTime'] for item in data if item['ip'] == sat], - text=[str(int(item['totalSessionTime'] / 3600)) + ' h' for item in data if item['ip'] == sat], + text=[prettytime(item['totalSessionTime']) for item in data if item['ip'] == sat], hole=0.3, textinfo='none', hoverinfo='label+text+percent', diff --git a/dash/pages/satellites.py b/dash/pages/satellites.py index ba607ba..37a1f61 100644 --- a/dash/pages/satellites.py +++ b/dash/pages/satellites.py @@ -6,8 +6,8 @@ import dash_table as dtbl import dash_bootstrap_components as dbc from dash.dependencies import Input, Output -import humanize import datetime as dt +from prettytime import prettytime import db from app import app @@ -78,8 +78,8 @@ def load_table(sort): data = cursor.fetchall() for record in data: - record['uptime'] = humanize.naturaldelta(dt.timedelta(seconds=record['uptime'])) - record['date'] = humanize.naturaldelta(dt.date.today() - record['date']) + record['uptime'] = prettytime(record['uptime']) + record['date'] = prettytime((dt.date.today() - record['date']).total_seconds()) db.closeConnection(dbcon) return data diff --git a/dash/pages/total.py b/dash/pages/total.py index 9655c51..e21c4b7 100644 --- a/dash/pages/total.py +++ b/dash/pages/total.py @@ -8,6 +8,7 @@ from dash.dependencies import Input, Output import plotly.graph_objects as go import datetime as dt +from prettytime import prettytime import db from app import app @@ -280,10 +281,10 @@ def make_sums(rangeStart, rangeEnd, satellites): db.closeConnection(dbcon) try: - strTimes = 'Total Time: {}h Total Session Time: {}h Total Off Time: {}h'.format( - int(data['totalTime'] / 3600), - int(data['totalSessionTime'] / 3600), - int(data['totalOffTime'] / 3600) + strTimes = 'Total Time: {} Total Session Time: {} Total Off Time: {}'.format( + prettytime(data['totalTime']), + prettytime(data['totalSessionTime']), + prettytime(data['totalOffTime']) ) except: strTimes = "No Data available!" diff --git a/dash/prettytime.py b/dash/prettytime.py new file mode 100644 index 0000000..6e9e562 --- /dev/null +++ b/dash/prettytime.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +def prettytime(seconds, prec = 2): + seconds = int(seconds) + output = [] + + for div, unit in [(365*86400, 'year'), (30*86400, 'month'), (86400, 'day'), (3600, 'hour'), (60, 'minute'), (1, 'second')]: + time = seconds // div + seconds = seconds % div + if time > 0: + if time > 1: + output.append('{} {}s'.format(time, unit)) + else: + output.append('{} {}'.format(time, unit)) + + return ', '.join(output[:prec]) + |