Development
This commit is contained in:
parent
f5722f3c7b
commit
d60bf15ce4
15 changed files with 367 additions and 285 deletions
|
|
@ -1,25 +1,33 @@
|
|||
import json
|
||||
import sqlite3
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime
|
||||
import config_utils
|
||||
import factory
|
||||
|
||||
|
||||
def _fmt_ts(ts):
|
||||
def _fmt_ts(ts, now):
|
||||
try:
|
||||
dt = datetime.fromtimestamp(int(ts), tz=timezone.utc)
|
||||
return dt.strftime('%Y-%m-%d %H:%M UTC')
|
||||
dt = datetime.fromtimestamp(int(ts))
|
||||
ago = config_utils.relative_time(int(ts), now)
|
||||
return f'{dt.strftime("%Y-%m-%d %H:%M")} ({ago} ago)'
|
||||
except Exception:
|
||||
return '-'
|
||||
|
||||
|
||||
_LEVEL_INT_TO_STR = {0: 'nothing', 1: 'viewer', 2: 'administrator', 3: 'manager'}
|
||||
|
||||
|
||||
def _active_sessions_table():
|
||||
try:
|
||||
con = sqlite3.connect(config_utils.SESSIONS_DB, timeout=5)
|
||||
con = sqlite3.connect(config_utils.ACCOUNTS_DB, timeout=5)
|
||||
con.row_factory = sqlite3.Row
|
||||
rows = con.execute(
|
||||
'SELECT session_id, email, access_level, created_at, last_seen'
|
||||
' FROM sessions ORDER BY last_seen DESC'
|
||||
'''SELECT s.session_id, a.email, a.access_level,
|
||||
s.session_started_ts, s.last_seen_ts
|
||||
FROM sessions s
|
||||
JOIN accounts a ON a.account_id = s.account_id
|
||||
ORDER BY s.last_seen_ts DESC'''
|
||||
).fetchall()
|
||||
con.close()
|
||||
except Exception:
|
||||
|
|
@ -30,12 +38,21 @@ def _active_sessions_table():
|
|||
|
||||
now = int(time.time())
|
||||
trs = ''
|
||||
for sid, email, access_level, created_at, last_seen in rows:
|
||||
online = (now - int(last_seen)) < 300
|
||||
badge = (
|
||||
'<span class="badge badge-enabled">Online</span>'
|
||||
if online else
|
||||
'<span class="badge badge-disabled">Offline</span>'
|
||||
for row in rows:
|
||||
sid = row['session_id']
|
||||
email = row['email']
|
||||
access_level = _LEVEL_INT_TO_STR.get(row['access_level'], 'viewer')
|
||||
started_ts = row['session_started_ts']
|
||||
last_seen = row['last_seen_ts']
|
||||
online = (now - int(last_seen)) < 300
|
||||
ago = config_utils.relative_time(int(last_seen), now)
|
||||
tip = factory.e(f'Last seen {ago} ago')
|
||||
badge_cls = 'badge-enabled' if online else 'badge-disabled'
|
||||
badge_lbl = 'Online' if online else 'Offline'
|
||||
badge = (
|
||||
f'<span class="tooltip-wrap" data-tooltip="{tip}">'
|
||||
f'<span class="badge {badge_cls}">{badge_lbl}</span>'
|
||||
f'</span>'
|
||||
)
|
||||
btn = (
|
||||
f'<form method="post" action="/action/accountmanage/session_invalidate"'
|
||||
|
|
@ -49,8 +66,7 @@ def _active_sessions_table():
|
|||
f'<td class="table-cell">{factory.e(email)}</td>'
|
||||
f'<td class="table-cell">{factory.e(access_level)}</td>'
|
||||
f'<td class="table-cell">{badge}</td>'
|
||||
f'<td class="table-cell">{_fmt_ts(created_at)}</td>'
|
||||
f'<td class="table-cell">{_fmt_ts(last_seen)}</td>'
|
||||
f'<td class="table-cell">{_fmt_ts(started_ts, now)}</td>'
|
||||
f'<td class="table-cell">{btn}</td>'
|
||||
f'</tr>'
|
||||
)
|
||||
|
|
@ -60,7 +76,6 @@ def _active_sessions_table():
|
|||
'<th class="table-header">Access Level</th>'
|
||||
'<th class="table-header">Status</th>'
|
||||
'<th class="table-header">Logged In</th>'
|
||||
'<th class="table-header">Last Seen</th>'
|
||||
'<th class="table-header"></th>'
|
||||
'</tr></thead><tbody>' + trs + '</tbody></table>'
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue