Skip to content

Commit

Permalink
WSTATS: use more temporary tables
Browse files Browse the repository at this point in the history
This simplifies queries and it's a 20% performance gain!
  • Loading branch information
mascaldotfr committed Jun 7, 2024
1 parent 8fbdfee commit e5f35cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion js/menu.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 26 additions & 25 deletions warstatus/stats/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ def statistics(events, db_file, force_rewrite = False):
now = datetime.now()
days = [7, 30, 90]

# Optimise queries by using only the needed subset of the table
# Optimise queries by using only the needed subsets of the table
report_mints = int(now.timestamp()) - (max(days) * 24 * 3600)
sql.execute(f"""create temporary table report as
select * from events where date >= {report_mints};""")
select * from events where date >= {report_mints} and type != "relic";""")
sql.execute(f"""create temporary table reportforts as
select * from report where type = "fort";""")
sql.execute(f"""create temporary table reportwalls as
select * from reportforts where name like "Great Wall of %";""")
sql.execute(f"""create temporary table reportgems as
select * from report where type = "gem" and location != owner;""")

full_report = [{"generated": int(now.timestamp())}]
for day in days:
Expand Down Expand Up @@ -120,8 +126,8 @@ def get_activity(self):
def query(condition, sign=""):
return f"""select owner, strftime("%H", time(date, 'unixepoch')) as time,
{sign} cast(count (rowid) as float) / {self.sample_days} as average
from report
where type = "fort" and owner {condition} location
from reportforts
where owner {condition} location
group by owner, time
order by time;"""

Expand All @@ -141,9 +147,8 @@ def get_invasions(self):
def query(condition, sign=""):
return f"""select owner, strftime("%H", time(date, 'unixepoch')) as time,
{sign} cast(count(rowid) as float) / {self.sample_days} as average
from report
where type = "fort" and owner {condition} location
and name like "Great Wall of %"
from reportwalls
where owner {condition} location
group by owner, time
order by time;"""

Expand All @@ -160,8 +165,7 @@ def get_gems(self):
gems = {"Alsius": [0] * 24, "Ignis": [0] * 24, "Syrtis": [0] * 24}
self.sql.execute(f"""select owner, strftime("%H", time(date, 'unixepoch')) as time,
count(rowid) as count
from report
where type="gem" and location != owner
from reportgems
group by owner, time;""")
for r in self.sql.fetchall():
gems[r["owner"]][int(r["time"])] = r["count"]
Expand Down Expand Up @@ -197,13 +201,13 @@ def get_fortsheld(self):
total_forts = {"Alsius": 0, "Ignis": 0, "Syrtis": 0}
average_forts = {"Alsius":[], "Ignis":[], "Syrtis":[]}
count_forts = {"Alsius":[], "Ignis":[], "Syrtis":[]}

for fort in forts_held:
for realm in total_forts: # Ensure 1st run is ok
forts_held[fort][realm] = {"time": 0, "count": 0}
self.sql.execute(f"""select date, owner, location
from report
where name like "%{fort}%"
and type = "fort"
from reportforts
where name = "Fort {fort}" or name = "{fort} Castle"
order by date asc;""")
events = self.sql.fetchall()
for i in range(0, len(events) - 1):
Expand Down Expand Up @@ -273,8 +277,8 @@ def generate_stats(self):

def query_fort_events(condition):
return f"""select owner as realm, count(rowid) as count
from report
where type = "fort" and owner {condition} location
from reportforts
where owner {condition} location
and date >= {self.startfrom}
group by owner;"""

Expand All @@ -291,8 +295,8 @@ def query_fort_events(condition):

# Get most captured forts by realm
self.sql.execute(f"""select owner as realm, name, count(name) as count
from report
where type="fort" and owner != location
from reportforts
where owner != location
and date >= {self.startfrom}
group by owner, name
order by count asc;""")
Expand All @@ -304,9 +308,8 @@ def query_fort_events(condition):

# Get count of invasions and last invasion by realm
self.sql.execute(f"""select owner as realm, location, max(date) as date, count(name) as count
from report
where type = "fort" and owner != location
and name like "Great Wall of %"
from reportwalls
where owner != location
and date >= {self.startfrom}
group by owner;""")
for r in self.sql.fetchall():
Expand All @@ -316,19 +319,17 @@ def query_fort_events(condition):

# Get the number of times a realm got invaded
self.sql.execute(f"""select owner as realm, count(name) as count
from report
where type = "fort" and owner = location
and name like "Great Wall of %"
from reportwalls
where owner = location
and date >= {self.startfrom}
group by location;""")
for r in self.sql.fetchall():
self.stats[r["realm"]]["invasions"]["invaded"]["count"] = r["count"]

# Get last gem stolen date and total count
self.sql.execute(f"""select owner as realm, max(date) as date, count(date) as count
from report
where type = "gem" and location != owner
and date >= {self.startfrom}
from reportgems
where date >= {self.startfrom}
group by owner;""")
for r in self.sql.fetchall():
self.stats[r["realm"]]["gems"]["stolen"]["last"] = r["date"]
Expand Down

0 comments on commit e5f35cf

Please sign in to comment.