summaryrefslogtreecommitdiff
path: root/f1elo/interface.py
blob: a492bba2fb93bde64f87567701c749728226a042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from __future__ import print_function

import datetime
import sys

import dateutil.relativedelta
from f1elo.db import Session
from f1elo.elo import Elo
from f1elo.model import *
from sqlalchemy import MetaData


class Interface:

    def __init__(self, date=None):
        self.session = Session()
        self.date = date

    def init_db(self, force=False):
        from f1elo.model import Base
        if force:
            Base.metadata.drop_all(self.session.get_bind())
        Base.metadata.create_all(self.session.get_bind())

    def reset(self, date=None, _debug=False):
        if date is None:
            date = self.date

        query = self.session.query(Race)
        if date is not None:
            query = query.filter(Race.date > date)
        for race in query.all():
            race.ranked = False
            if _debug:
                print(race, file=sys.stderr)

        query = self.session.query(Ranking)
        if date is not None:
            query = query.filter(Ranking.rank_date > date)
        query.delete()

        self.session.commit()

        if date is not None:
            date += datetime.timedelta(1)

        self.date = date

        return

    def rate(self, date=None, _debug=False):
        if date is None:
            date = self.date
        if date is None:
            date = datetime.date.today()

        elo = Elo(self.session)
        race_query = self.session.query(Race).filter(Race.ranked == False)
        if date is not None:
            race_query = race_query.filter(Race.date <= date)
        races = race_query.order_by(Race.date, Race.id).all()

        for race in races:
            if _debug:
                print(race, file=sys.stderr)
                print('', file=sys.stderr)

            ranks = elo.rank_race(race)
            driver_ranks = {}
            for entry, rank in ranks.iteritems():
                correction = rank / len(entry.drivers)
                for driver in entry.drivers:
                    if driver not in driver_ranks:
                        driver_ranks[driver] = 0
                    driver_ranks[driver] += correction
            for driver, rank in driver_ranks.iteritems():
                ranking = Ranking()
                ranking.rank_date = race.date
                ranking.ranking = round(elo.get_ranking(driver, race.date) + rank, 2)
                self.session.add(ranking)
                driver.rankings.append(ranking)

            if _debug:
                for entry in race.entries:
                    print(
                        entry,
                        elo.get_entry_ranking(entry,
                                              race.date - dateutil.relativedelta.relativedelta(days=1)),
                        elo.get_entry_ranking(entry),
                        file=sys.stderr)
                print('', file=sys.stderr)

            race.ranked = True
            date = race.date

        self.session.commit()

        self.date = date

        if self.date is not None:
            self.date += datetime.timedelta(1)

        return

    def fetch(self, date=None):
        if date is None:
            date = self.date
        if date is None:
            date = datetime.date.today()
            date += datetime.timedelta(1)

        one_year = dateutil.relativedelta.relativedelta(years=1)
        rankings = self.session.query(
            Ranking
        ).filter(
            Ranking.rank_date > (date - one_year)
        ).filter(
            Ranking.rank_date <= date
        ).all()

        drivers = {}
        for ranking in rankings:
            if ranking.driver not in drivers:
                drivers[ranking.driver] = ranking.driver.get_ranking(date)

        self.date = date

        return sorted(drivers.values(), key=lambda rank: rank.ranking, reverse=True)