forked from modrzew/pokeminer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.py
160 lines (129 loc) · 4.39 KB
/
db.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from datetime import datetime
import json
import time
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.sql import not_
with open('locales/pokemon.en.json') as f:
pokemon_names = json.load(f)
try:
import config
DB_ENGINE = config.DB_ENGINE
except ImportError, AttributeError:
DB_ENGINE = 'sqlite:///db.sqlite'
def get_engine():
return create_engine(DB_ENGINE)
Base = declarative_base()
class Sighting(Base):
__tablename__ = 'sightings'
id = Column(Integer, primary_key=True)
pokemon_id = Column(Integer)
spawn_id = Column(String(32))
expire_timestamp = Column(Integer)
normalized_timestamp = Column(Integer)
lat = Column(String(16))
lon = Column(String(16))
Session = sessionmaker(bind=get_engine())
def normalize_timestamp(timestamp):
return int(float(timestamp) / 120.0) * 120
def add_sighting(session, spawn_id, pokemon):
obj = Sighting(
pokemon_id=pokemon['id'],
spawn_id=spawn_id,
expire_timestamp=pokemon['disappear_time'],
normalized_timestamp=normalize_timestamp(pokemon['disappear_time']),
lat=pokemon['lat'],
lon=pokemon['lng'],
)
# Check if there isn't the same entry already
existing = session.query(Sighting) \
.filter(Sighting.pokemon_id == obj.pokemon_id) \
.filter(Sighting.spawn_id == obj.spawn_id) \
.filter(Sighting.expire_timestamp > obj.expire_timestamp - 10) \
.filter(Sighting.expire_timestamp < obj.expire_timestamp + 10) \
.filter(Sighting.lat == obj.lat) \
.filter(Sighting.lon == obj.lon) \
.first()
if existing:
return
session.add(obj)
def get_sightings(session):
query = session.query(Sighting) \
.filter(Sighting.expire_timestamp > time.time())
trash_list = getattr(config, 'TRASH_IDS', None)
if trash_list:
query = query.filter(not_(Sighting.pokemon_id.in_(config.TRASH_IDS)))
return query.all()
def get_session_stats(session):
min_max_query = session.execute('''
SELECT
MIN(expire_timestamp) ts_min,
MAX(expire_timestamp) ts_max,
COUNT(*)
FROM `sightings`;
''')
min_max_result = min_max_query.first()
length_hours = (min_max_result[1] - min_max_result[0]) // 3600
# Convert to datetime
return {
'start': datetime.fromtimestamp(min_max_result[0]),
'end': datetime.fromtimestamp(min_max_result[1]),
'count': min_max_result[2],
'length_hours': length_hours,
'per_hour': min_max_result[2] / length_hours,
}
def get_punch_card(session):
query = session.execute('''
SELECT
CAST((expire_timestamp / 300) AS SIGNED) ts_date,
COUNT(*) how_many
FROM `sightings`
GROUP BY ts_date ORDER BY ts_date
''')
results = query.fetchall()
results_dict = {r[0]: r[1] for r in results}
filled = []
for row_no, i in enumerate(range(int(results[0][0]), int(results[-1][0]))):
item = results_dict.get(i)
filled.append((row_no, item if item else 0))
return filled
def get_top_pokemon(session, count=30, order='DESC'):
query = session.execute('''
SELECT
pokemon_id,
COUNT(*) how_many
FROM sightings
GROUP BY pokemon_id
ORDER BY how_many {order}
LIMIT {count}
'''.format(order=order, count=count))
return query.fetchall()
def get_stage2_pokemon(session):
result = []
if not hasattr(config, 'STAGE2'):
return []
for pokemon_id in config.STAGE2:
count = session.query(Sighting) \
.filter(Sighting.pokemon_id == pokemon_id) \
.count()
if count > 0:
result.append((pokemon_id, count))
return result
def get_nonexistent_pokemon(session):
result = []
query = session.execute('''
SELECT DISTINCT pokemon_id FROM sightings
''')
db_ids = [r[0] for r in query.fetchall()]
for pokemon_id in range(1, 152):
if pokemon_id not in db_ids:
result.append(pokemon_id)
return result
def get_all_sightings(session, pokemon_ids):
# TODO: rename this and get_sightings
query = session.query(Sighting) \
.filter(Sighting.pokemon_id.in_(pokemon_ids)) \
.all()
return query