From 75730c8a7c73bf9ddc4382f0550075d10929a546 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Fri, 16 Aug 2019 12:38:10 -0500 Subject: [PATCH] Convert to HAVING Signed-off-by: Etzelia --- activity.py | 27 +++++++++++++++++++++------ gui.py | 10 +++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/activity.py b/activity.py index 9ffbe65..b0345b2 100644 --- a/activity.py +++ b/activity.py @@ -1,5 +1,6 @@ from django_coreprotect.models import SessionResult, ActivityResult from django_coreprotect.utils import safe_int +from datetime import datetime class ActivityForm: @@ -26,17 +27,31 @@ def activity_data(request): def activity_results(form): query = "" - players = [] + + players_clause = "" if form.players: + 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])) + + 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: 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) + 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) if query: print(query) diff --git a/gui.py b/gui.py index 584f547..20e2e13 100644 --- a/gui.py +++ b/gui.py @@ -94,7 +94,7 @@ def gui_data(request): def gui_results(form): queries = [] - ignore_environment = " AND player NOT LIKE '#%%' " if form.ignore_environment else "" + ignore_environment = " AND cu.user NOT LIKE '#%%' " if form.ignore_environment else "" oldest_first = " ASC " if form.oldest_first else " DESC " coords = [] @@ -112,12 +112,12 @@ def gui_results(form): if form.players: for player in form.players.split(","): players.append(player.strip()) - players_clause = " AND ({})".format(" OR ".join(["player LIKE '%%{}%%'".format(p) for p in players])) + players_clause = " AND ({})".format(" OR ".join(["cu.user LIKE '%%{}%%'".format(p) for p in players])) worlds_clause = "" worlds = [world["id"] for world in form.worlds if world["checked"]] if len(worlds): - worlds_clause = " AND cw.id IN ({})".format(",".join(worlds)) + worlds_clause = " AND cw.rowid IN ({})".format(",".join(worlds)) time_clause = "" if form.date_from or form.date_to: @@ -126,7 +126,7 @@ def gui_results(form): dt = datetime.now().timestamp() if form.date_to and not form.date_from: df = datetime.now().timestamp() - time_clause = " AND unix BETWEEN {} AND {} ".format(df, dt) + time_clause = " HAVING unix BETWEEN {} AND {} ".format(df, dt) # Block Break, Block Place, and Interact block_actions = [] @@ -147,7 +147,7 @@ def gui_results(form): {ignore_environment} {players} {coords} - {worlds} + {worlds} {time} '''.format(action=",".join(block_actions), ignore_environment=ignore_environment, players=players_clause, coords=coords_clause, worlds=worlds_clause, time=time_clause))