2019-08-16 16:46:09 +00:00
|
|
|
from django_coreprotect.models import SessionResult, ActivityResult
|
|
|
|
from django_coreprotect.utils import safe_int
|
2019-08-16 17:38:10 +00:00
|
|
|
from datetime import datetime
|
2019-08-16 16:46:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
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 = ""
|
2019-08-16 17:38:10 +00:00
|
|
|
|
|
|
|
players_clause = ""
|
2019-08-16 16:46:09 +00:00
|
|
|
if form.players:
|
2019-08-16 17:38:10 +00:00
|
|
|
players = []
|
2019-08-16 16:46:09 +00:00
|
|
|
for player in form.players.split(","):
|
|
|
|
players.append(player.strip())
|
2019-08-16 17:39:25 +00:00
|
|
|
players_clause = " WHERE ({}) ".format(" OR ".join(["cu.user LIKE '%%{}%%'".format(p) for p in players]))
|
2019-08-16 17:38:10 +00:00
|
|
|
|
|
|
|
time_clause = ""
|
|
|
|
if form.date_from or form.date_to:
|
|
|
|
df, dt = form.date_from, form.date_to
|
|
|
|
if form.date_from and not form.date_to:
|
|
|
|
dt = datetime.now().timestamp()
|
|
|
|
if form.date_to and not form.date_from:
|
|
|
|
df = datetime.now().timestamp()
|
|
|
|
time_clause = " HAVING unix BETWEEN {} AND {} ".format(df, dt)
|
|
|
|
|
|
|
|
if players_clause or time_clause:
|
2019-08-16 16:46:09 +00:00
|
|
|
query = '''SELECT
|
2019-08-16 17:38:10 +00:00
|
|
|
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}
|
|
|
|
{time}
|
|
|
|
'''.format(players=players_clause, time=time_clause)
|
2019-08-16 16:46:09 +00:00
|
|
|
|
|
|
|
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 []
|