django_coreprotect/activity.py

56 lines
1.7 KiB
Python

from django_coreprotect.models import SessionResult, ActivityResult
from django_coreprotect.utils import safe_int
class ActivityForm:
def __init__(self):
self.players = ""
self.date_from = ""
self.date_to = ""
def activity_data(request):
request_data = request.GET
form = ActivityForm()
# Players
form.players = request_data["players"] if "players" in request_data else ""
# Date and Time
form.date_from = request_data["date_from"] if "date_from" in request_data else ""
form.date_to = request_data["date_to"] if "date_to" in request_data else ""
return form
def activity_results(form):
query = ""
players = []
if form.players:
for player in form.players.split(","):
players.append(player.strip())
players_clause = " WHERE ({}) ".format(" OR ".join(["player LIKE '%{}%'".format(p) for p in players]))
query = '''SELECT
0 AS id, cs.time AS unix, cu.user AS player, cs.action
FROM co_session cs
JOIN co_user cu ON cs.user = cu.rowid
{players}
'''.format(players=players_clause)
if query:
print(query)
sessions = SessionResult.objects.raw(query)
activity = {}
for session in sessions:
time = -safe_int(session.unix) if session.action == 1 else safe_int(session.unix)
if session.player in activity:
activity[session.player] += time
else:
activity[session.player] = time
results = []
for player, time in activity.items():
results.append(ActivityResult(player, time))
return results
return []