only calculate users when we actually need them

remotes/1693176582716663532/tmp_refs/heads/watchparty
Aevann1 2022-10-15 09:31:24 +02:00
parent bcf4450cbd
commit c24851dbb3
3 changed files with 29 additions and 24 deletions

View File

@ -5,6 +5,7 @@ from os import listdir, environ
from .const import *
import time
from files.helpers.assetcache import assetcache_path
from files.helpers.wrappers import calc_users
@app.template_filter("post_embed")
def post_embed(id, v):
@ -74,5 +75,5 @@ def inject_constants():
"KOFI_TOKEN":KOFI_TOKEN, "KOFI_LINK":KOFI_LINK,
"approved_embed_hosts":approved_embed_hosts,
"site_settings":app.config['SETTINGS'],
"EMAIL":EMAIL,
"EMAIL":EMAIL, "calc_users": calc_users
}

View File

@ -8,6 +8,32 @@ import functools
import user_agents
import time
def calc_users(v):
loggedin = cache.get(f'{SITE}_loggedin') or {}
loggedout = cache.get(f'{SITE}_loggedout') or {}
timestamp = int(time.time())
if v:
if session["session_id"] in loggedout: del loggedout[session["session_id"]]
loggedin[v.id] = timestamp
# Check against last_active + ACTIVE_TIME to reduce frequency of
# UPDATEs in exchange for a ±ACTIVE_TIME margin of error.
if (v.last_active + LOGGEDIN_ACTIVE_TIME) < timestamp:
v.last_active = timestamp
g.db.add(v)
else:
ua = str(user_agents.parse(g.agent))
if 'spider' not in ua.lower() and 'bot' not in ua.lower():
loggedout[session["session_id"]] = (timestamp, ua)
loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME}
loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME}
cache.set(f'{SITE}_loggedin', loggedin)
cache.set(f'{SITE}_loggedout', loggedout)
g.loggedin_counter = len(loggedin)
g.loggedout_counter = len(loggedout)
def get_logged_in_user():
if hasattr(g, 'v'): return g.v
@ -49,30 +75,7 @@ def get_logged_in_user():
if not session.get("session_id"):
session.permanent = True
session["session_id"] = secrets.token_hex(49)
loggedin = cache.get(f'{SITE}_loggedin') or {}
loggedout = cache.get(f'{SITE}_loggedout') or {}
timestamp = int(time.time())
if v:
if session["session_id"] in loggedout: del loggedout[session["session_id"]]
loggedin[v.id] = timestamp
# Check against last_active + ACTIVE_TIME to reduce frequency of
# UPDATEs in exchange for a ±ACTIVE_TIME margin of error.
if (v.last_active + LOGGEDIN_ACTIVE_TIME) < timestamp:
v.last_active = timestamp
g.db.add(v)
else:
ua = str(user_agents.parse(g.agent))
if 'spider' not in ua.lower() and 'bot' not in ua.lower():
loggedout[session["session_id"]] = (timestamp, ua)
loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME}
loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME}
cache.set(f'{SITE}_loggedin', loggedin)
cache.set(f'{SITE}_loggedout', loggedout)
g.loggedin_counter = len(loggedin)
g.loggedout_counter = len(loggedout)
g.v = v

View File

@ -13,6 +13,7 @@
}
</style>
{{calc_users(v)}}
{% if g.loggedin_counter and not err and (SITE_NAME in ('PCM','rDrama') or g.loggedin_counter+g.loggedout_counter > 100) %}
<div class="srd">
{% if SITE_NAME == 'rDrama' %}