From ddd3f64fa35153f9c9b015c749b10953ce76d70d Mon Sep 17 00:00:00 2001 From: Timothy Allen Date: Tue, 14 Aug 2018 11:04:42 +0200 Subject: [PATCH] Further cleanups. --- aacstats.py | 62 +++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/aacstats.py b/aacstats.py index 0913e34..39517a2 100644 --- a/aacstats.py +++ b/aacstats.py @@ -86,6 +86,7 @@ def read_db(listing=None, event=None, person=None, licence=None, search=dict(), if show == -1: limit = '' + ''' Build standard query (list of results) ''' if event: if isinstance(event, str): event = db.escape_string(event).decode() @@ -98,19 +99,8 @@ def read_db(listing=None, event=None, person=None, licence=None, search=dict(), if isinstance(licence, str): licence = db.escape_string(licence).decode() where += ' AND licence LIKE "{}"'.format(licence.lower()) - if year: - if not isinstance(year, (int, float)): - year = int(db.escape_string(year).decode()) - firstdate = dt.datetime.min - lastdate = dt.datetime.max - firstdate = firstdate.replace(year=int(year)) - lastdate = lastdate.replace(year=int(year)) - where += ' AND date > "{}" AND date < "{}"'.format(firstdate, lastdate) - ''' This statement is expensive but doesn't increase the count, so don't change the count statement ''' - if finishers: - select = 'total.finishers, query.* FROM( SELECT *' - close = ') AS query INNER JOIN (SELECT event, date, distance, COUNT(event) as finishers FROM `results` GROUP BY event, distance, date) AS total ON total.event=query.event AND total.date=query.date AND total.distance=query.distance' + ''' Build search query ''' for column in search.keys(): if isinstance(column, str): column = db.escape_string(column).decode() @@ -143,32 +133,44 @@ def read_db(listing=None, event=None, person=None, licence=None, search=dict(), else: pass + ''' Build list query (list of races, rankings, licences, or runners by distance) ''' if listing: + where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no%return%"' + where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no%card%"' + where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%blank%card%"' + where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%disqualified%"' if listing == 'races': - select = 'event, date' + select = 'TRIM(event), date' group = 'GROUP BY event, date' - order = 'date DESC, event' + order = 'date DESC, TRIM(event)' elif listing == 'runners': - select = 'CONCAT_WS(" ", name, surname) person, FORMAT(SUM(distance),0) total' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no return%"' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no card%"' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%blank card%"' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%disqualified%"' - group = 'GROUP BY CONCAT_WS(" ", name, surname)' - order = 'SUM(distance) DESC, CONCAT_WS(" ", name, surname)' + select = 'TRIM(CONCAT_WS(" ", name, surname)) AS person, FORMAT(SUM(distance),0) AS total' + group = 'GROUP BY TRIM(CONCAT_WS(" ", name, surname))' + order = 'SUM(distance) DESC, TRIM(CONCAT_WS(" ", name, surname))' elif listing == 'rankings': - select = 'CONCAT_WS(" ", name, surname) person, SUM(position) positions, COUNT(event) races, SUM(position)/COUNT(event) podiums, FORMAT(SUM(position)/COUNT(event),1) score' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no return%"' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%no card%"' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%blank card%"' - where += ' AND CONCAT_WS(" ", name, surname) NOT LIKE "%disqualified%"' - group = 'GROUP BY CONCAT_WS(" ", name, surname)' + # SELECT query.person, query.positions, query.races, query.podiums, query.score, sex.positions AS sexpositions, sex.races AS sexraces, cat.positions AS catpositions, cat.races catraces FROM (SELECT *, CONCAT_WS(" ", name, surname) person, SUM(position) positions, COUNT(event) races, SUM(position)/COUNT(event) podiums, FORMAT(SUM(position)/COUNT(event),1) score FROM `results` WHERE club LIKE "AAC" GROUP BY CONCAT_WS(" ", name, surname) ) AS query INNER JOIN (SELECT *, CONCAT_WS(" ", name, surname) person, SUM(sexposition) as positions, COUNT(event) races FROM `results` WHERE club LIKE "AAC" AND sexposition > 0 GROUP BY CONCAT_WS(" ", name, surname) ) sex ON query.person=sex.person INNER JOIN (SELECT *, CONCAT_WS(" ", name, surname) person, SUM(catposition) as positions, COUNT(event) races FROM `results` WHERE club LIKE "AAC" AND catposition > 0 GROUP BY CONCAT_WS(" ", name, surname) ) cat ON query.person=cat.person WHERE query.person NOT LIKE "%no return%" AND query.person NOT LIKE "%no card%" AND query.person NOT LIKE "%blank card%" AND query.person NOT LIKE "%disqualified%" GROUP BY query.person ORDER BY podiums, races DESC; + select = 'TRIM(CONCAT_WS(" ", name, surname)) AS person, SUM(position) AS positions, COUNT(event) AS races, SUM(position)/COUNT(event) AS podiums, FORMAT(SUM(position)/COUNT(event), 1) AS score' + group = 'GROUP BY TRIM(CONCAT_WS(" ", name, surname))' order = 'podiums, races DESC' elif listing == 'licence': - select = 'licence, date, CONCAT_WS(" ", name, surname) person' - group = 'GROUP BY licence, CONCAT_WS(" ", name, surname)' - order = 'CONCAT_WS(" ", name, surname), date DESC' + select = 'licence, date, TRIM(CONCAT_WS(" ", name, surname)) AS person' + group = 'GROUP BY licence, name, surname' + order = 'TRIM(CONCAT_WS(" ", name, surname)) ASC' + ''' Add elements common to multiple types of queries ''' + if year: + if not isinstance(year, (int, float)): + year = int(db.escape_string(year).decode()) + firstdate = dt.datetime.min + lastdate = dt.datetime.max + firstdate = firstdate.replace(year=int(year)) + lastdate = lastdate.replace(year=int(year)) + where += ' AND date > "{}" AND date < "{}"'.format(firstdate, lastdate) + ''' This statement is expensive but doesn't increase the count, so don't change the count statement ''' + if finishers: + select = 'total.finishers, query.* FROM( SELECT *' + close = ') AS query INNER JOIN (SELECT event, date, distance, COUNT(event) as finishers FROM `results` GROUP BY event, distance, date) AS total ON total.event=query.event AND total.date=query.date AND total.distance=query.distance' + sql = 'SELECT {} FROM `results` {} {} ORDER BY {} {} {};'.format(select, where, group, order, limit, close) app.logger.debug(sql)