Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '--rating-history-db' to save ratings in a DB #65

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions analysis/analyze_glicko2_one_game_at_a_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def process_game(self, game: GameRecord) -> Glicko2Analytics:

self._storage.set(game.black_id, updated_black)
self._storage.set(game.white_id, updated_white)
#self._storage.add_rating_history(game.black_id, game.ended, updated_black)
#self._storage.add_rating_history(game.white_id, game.ended, updated_white)
self._storage.add_rating_history(game.black_id, game.ended, updated_black)
self._storage.add_rating_history(game.white_id, game.ended, updated_white)

return Glicko2Analytics(
skipped=False,
Expand Down Expand Up @@ -99,6 +99,8 @@ def process_game(self, game: GameRecord) -> Glicko2Analytics:
analytics = engine.process_game(game)
tally.add_glicko2_analytics(analytics)

storage.save_rating_history("overall")

tally.print()

self_reported_ratings = tally.get_self_reported_rating()
Expand Down
44 changes: 44 additions & 0 deletions analysis/util/InMemoryStorage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import sqlite3
from collections import defaultdict
from typing import Any, DefaultDict, Dict, List, Tuple

from analysis.util import cli
from goratings.interfaces import Storage

from .Config import config

__all__ = ["InMemoryStorage"]

cli.add_argument(
"--rating-history-db", dest="rating_history_db", type=str,
help="Path to DB for ratings history (not saved by default)",
)

class InMemoryStorage(Storage):
_data: Dict[int, Any]
Expand Down Expand Up @@ -87,3 +95,39 @@ def get_matches_newer_or_equal_to(self, player_id: int, timestamp: int) -> Any:
else:
break
return [e[1] for e in self._match_history[player_id][-ct:]]

def save_rating_history(self, category: str) -> None:
if config.args.rating_history_db is None:
return
connection = sqlite3.connect(config.args.rating_history_db)
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS rating_history(
category TEXT,
player_id INTEGER,
timestamp INTEGER,
rating REAL,
deviation REAL,
volatility REAL
)""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS index_ratings_by_player
ON rating_history
(player_id,timestamp)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS index_ratings_by_category
ON rating_history
(category,player_id,timestamp)
""")
cursor.executemany("""
INSERT INTO rating_history(category,player_id,timestamp,rating,deviation,volatility)
VALUES (?,?,?,?,?,?)
""",
((category, player, timestamp, r.rating, r.deviation, r.volatility)
for (player,history) in self._rating_history.items()
for (timestamp, r) in history
),
)
connection.commit()
connection.close()