2019-08-16 16:46:09 +00:00
|
|
|
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())
|
2019-08-16 17:26:39 +00:00
|
|
|
players_clause = " WHERE ({}) ".format(" OR ".join(["player LIKE '%%{}%%'".format(p) for p in players]))
|
2019-08-16 16:46:09 +00:00
|
|
|
query = '''SELECT
|
|
|
|
0 AS id, cs.time AS unix, cu.user AS player, cs.action
|
|
|
|
FROM co_session cs
|
2019-08-16 17:15:30 +00:00
|
|
|
JOIN co_user cu ON cs.user = cu.rowid
|
2019-08-16 16:46:09 +00:00
|
|
|
{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 []
|