This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
49 lines (40 loc) · 1.61 KB
/
data.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
from sqlalchemy.sql import text
from models import db
def statewide_by_day():
sql = """
SELECT * FROM cumulative_stats_by_day
WHERE days_before < 45 AND days_before >= (select '2021-01-05' - max(date(file_update_time - interval '18 hours')) from updated_times);
"""
results = {}
for result in db.engine.execute(text(sql)):
results[result["days_before"]] = {
"total_general": int(result["total_returned_general"]),
"total_special": int(result["total_returned_special"]),
}
return results
def statewide_by_party_day():
sql = """
SELECT * FROM cumulative_stats_by_party_day
WHERE days_before < 45 AND days_before >= (select '2021-01-05' - max(date(file_update_time - interval '18 hours')) from updated_times);
"""
results = {}
results["combined"] = {}
for result in db.engine.execute(text(sql)):
if result["party"] in ("D", "R"):
party = result["party"]
else:
party = "U"
results.setdefault(party, {})[result["days_before"]] = {
"total_general": int(result["total_returned_general"]),
"total_special": int(result["total_returned_special"]),
}
results["combined"].setdefault(
result["days_before"], {"total_general": 0, "total_special": 0}
)
results["combined"][result["days_before"]]["total_general"] += int(
result["total_returned_general"]
)
results["combined"][result["days_before"]]["total_special"] += int(
result["total_returned_special"]
)
return results