-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added database server information to core engine + differentiated ran…
…kings calculation query based on database engine. (#1253) * Added database server information to core engine + differentiated rankings calculation query based on database engine. * Some code clean-up + removed SQLite references. * Small comment fix in database server info object
- Loading branch information
1 parent
5e2f343
commit 2c7c432
Showing
5 changed files
with
170 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class Queries: | ||
CALCULATE_WITH_PARTITION = """ | ||
-- Reset the current ranks to insert new ones later one. | ||
TRUNCATE TABLE rankings_rank; | ||
-- Limit on maximum ranked records. | ||
SET @ranked_record_limit = {}; | ||
-- Minimum amount of ranked records required to acquire a rank. | ||
SET @minimum_ranked_records = {}; | ||
-- Total amount of maps active on the server. | ||
SET @active_map_count = {}; | ||
-- Set the rank/current rank variables to ensure correct first calculation | ||
INSERT INTO rankings_rank (player_id, average, calculated_at) | ||
SELECT | ||
player_id, average, calculated_at | ||
FROM ( | ||
SELECT | ||
player_id, | ||
-- Calculation: the sum of the record ranks is combined with the ranked record limit times the amount of unranked maps. | ||
-- Divide this summed ranking by the amount of active maps on the server, and an average calculated rank will be returned. | ||
ROUND((SUM(player_rank) + (@active_map_count - COUNT(player_rank)) * @ranked_record_limit) / @active_map_count * 10000, 0) AS average, | ||
NOW() AS calculated_at, | ||
COUNT(player_rank) AS ranked_records_count | ||
FROM | ||
( | ||
SELECT | ||
id, | ||
map_id, | ||
player_id, | ||
score, | ||
RANK() OVER (PARTITION BY map_id ORDER BY score ASC) AS player_rank | ||
FROM localrecord | ||
WHERE map_id IN ({}) | ||
) AS ranked_records | ||
WHERE player_rank <= @ranked_record_limit | ||
GROUP BY player_id | ||
) grouped_ranks | ||
WHERE ranked_records_count >= @minimum_ranked_records | ||
""" | ||
|
||
CALCULATE_WITHOUT_PARTITION = """ | ||
-- Reset the current ranks to insert new ones later one. | ||
TRUNCATE TABLE rankings_rank; | ||
-- Limit on maximum ranked records. | ||
SET @ranked_record_limit = {}; | ||
-- Minimum amount of ranked records required to acquire a rank. | ||
SET @minimum_ranked_records = {}; | ||
-- Total amount of maps active on the server. | ||
SET @active_map_count = {}; | ||
-- Set the rank/current rank variables to ensure correct first calculation | ||
SET @player_rank = 0; | ||
SET @current_rank = 0; | ||
INSERT INTO rankings_rank (player_id, average, calculated_at) | ||
SELECT | ||
player_id, average, calculated_at | ||
FROM ( | ||
SELECT | ||
player_id, | ||
-- Calculation: the sum of the record ranks is combined with the ranked record limit times the amount of unranked maps. | ||
-- Divide this summed ranking by the amount of active maps on the server, and an average calculated rank will be returned. | ||
ROUND((SUM(player_rank) + (@active_map_count - COUNT(player_rank)) * @ranked_record_limit) / @active_map_count * 10000, 0) AS average, | ||
NOW() AS calculated_at, | ||
COUNT(player_rank) AS ranked_records_count | ||
FROM | ||
( | ||
SELECT | ||
id, | ||
map_id, | ||
player_id, | ||
score, | ||
@player_rank := IF(@current_rank = map_id, @player_rank + 1, 1) AS player_rank, | ||
@current_rank := map_id | ||
FROM localrecord | ||
WHERE map_id IN ({}) | ||
ORDER BY map_id, score ASC | ||
) AS ranked_records | ||
WHERE player_rank <= @ranked_record_limit | ||
GROUP BY player_id | ||
) grouped_ranks | ||
WHERE ranked_records_count >= @minimum_ranked_records | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
The server information object contains version information about the database server. | ||
""" | ||
import logging | ||
import peewee | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ServerInfo: | ||
def __init__(self, engine, db): | ||
""" | ||
Initiate database server information object. | ||
:param engine: Database engine instance. | ||
:param db: Database instance. | ||
""" | ||
self.engine = engine | ||
self.db = db | ||
self.type = None | ||
self.version = None | ||
self.version_text = None | ||
|
||
async def determine(self): | ||
if isinstance(self.db.engine, peewee.MySQLDatabase): | ||
self.type = "mysql" | ||
query = "SELECT @@version AS version, @@innodb_version AS innodb_version" | ||
elif isinstance(self.db.engine, peewee.PostgresqlDatabase): | ||
self.type = "postgresql" | ||
query = "SHOW SERVER_VERSION" | ||
else: | ||
logger.warning("Unable to determine database server version (unknown engine type: {})".format(type(self.db.engine))) | ||
return | ||
|
||
try: | ||
cursor = self.engine.execute_sql(query) | ||
result = cursor.fetchone() | ||
|
||
if result is None or len(result) == 0: | ||
return | ||
|
||
if self.type == "mysql": | ||
# Use the innodb_version (1) for a clean engine version, use the version (0) as version text. | ||
self.version = result[1] | ||
self.version_text = result[0] | ||
|
||
if "mariadb" in self.version_text.lower(): | ||
self.type = "mariadb" | ||
elif self.type == "postgresql": | ||
self.version_text = result[0] | ||
self.version = self.version_text.split(' ')[0] | ||
|
||
logger.info("Determined database server type: {}, version: {}".format(self.type, self.version)) | ||
except: | ||
# No database version could be established | ||
logger.warning("Unable to determine database server version (type: {})".format(self.type)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,5 +30,6 @@ bcrypt==3.2.0 | |
raven==6.10.0 | ||
xmltodict==0.12.0 | ||
PyYAML==5.4.1 | ||
packaging<=23.1 | ||
|
||
cryptography==36.0.1 |