-
Notifications
You must be signed in to change notification settings - Fork 69
/
signal_dash_data_generator.py
284 lines (228 loc) · 9.78 KB
/
signal_dash_data_generator.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""Updates the signal dashboard data."""
# standard library
import argparse
import sys
import time
import datetime
import mysql.connector
import pandas as pd
from dataclasses import dataclass
from epiweeks import Week
from typing import List
# first party
import covidcast
import delphi.operations.secrets as secrets
from delphi.epidata.common.logger import get_structured_logger
LOOKBACK_DAYS_FOR_COVERAGE = 56
BASE_COVIDCAST = covidcast.covidcast.Epidata.BASE_URL[:-len("api.php")] + "covidcast"
COVERAGE_URL = f"{BASE_COVIDCAST}/coverage?format=csv&signal={{source}}:{{signal}}&days={LOOKBACK_DAYS_FOR_COVERAGE}"
@dataclass
class DashboardSignal:
"""Container class for information about dashboard signals."""
db_id: int
name: str
source: str
covidcast_signal: str
latest_coverage_update: datetime.date
latest_status_update: datetime.date
@dataclass
class DashboardSignalCoverage:
"""Container class for coverage of a dashboard signal"""
signal_id: int
date: datetime.date
geo_type: str
count: int
@dataclass
class DashboardSignalStatus:
"""Container class for status of a dashboard signal"""
signal_id: int
date: datetime.date
latest_issue: datetime.date
latest_time_value: datetime.date
class Database:
"""Storage for dashboard data."""
DATABASE_NAME = 'epidata'
SIGNAL_TABLE_NAME = 'dashboard_signal'
STATUS_TABLE_NAME = 'dashboard_signal_status'
COVERAGE_TABLE_NAME = 'dashboard_signal_coverage'
def __init__(self, connector_impl=mysql.connector):
"""Establish a connection to the database."""
u, p = secrets.db.epi
self._connection = connector_impl.connect(
host=secrets.db.host,
user=u,
password=p,
database=Database.DATABASE_NAME)
self._cursor = self._connection.cursor()
def rowcount(self) -> int:
"""Get the last modified row count"""
return self._cursor.rowcount
def write_status(self, status_list: List[DashboardSignalStatus]) -> None:
"""Write the provided status to the database."""
insert_statement = f'''INSERT INTO `{Database.STATUS_TABLE_NAME}`
(`signal_id`, `date`, `latest_issue`, `latest_time_value`)
VALUES
(%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
`latest_issue`=VALUES(`latest_issue`),
`latest_time_value`=VALUES(`latest_time_value`)
'''
status_as_tuples = [
(x.signal_id, x.date, x.latest_issue, x.latest_time_value)
for x in status_list]
self._cursor.executemany(insert_statement, status_as_tuples)
latest_status_dates = {}
for x in status_list:
latest_status_date = latest_status_dates.get(x.signal_id)
if not latest_status_date or x.date > latest_status_date:
latest_status_dates.update({x.signal_id: x.date})
latest_status_tuples = [(v, k) for k, v in latest_status_dates.items()]
update_statement = f'''UPDATE `{Database.SIGNAL_TABLE_NAME}`
SET `latest_status_update` = GREATEST(`latest_status_update`, %s)
WHERE `id` = %s
'''
self._cursor.executemany(update_statement, latest_status_tuples)
self._connection.commit()
def write_coverage(
self, coverage_list: List[DashboardSignalCoverage]) -> None:
"""Write the provided coverage to the database."""
insert_statement = f'''INSERT INTO `{Database.COVERAGE_TABLE_NAME}`
(`signal_id`, `date`, `geo_type`, `count`)
VALUES
(%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE `count` = VALUES(`count`)
'''
coverage_as_tuples = [
(x.signal_id, x.date, x.geo_type, x.count)
for x in coverage_list]
self._cursor.executemany(insert_statement, coverage_as_tuples)
latest_coverage_dates = {}
oldest_coverage_dates = {}
for x in coverage_list:
latest_coverage_date = latest_coverage_dates.get(x.signal_id)
oldest_coverage_date = oldest_coverage_dates.get(x.signal_id)
if not latest_coverage_date or x.date > latest_coverage_date:
latest_coverage_dates.update({x.signal_id: x.date})
if not oldest_coverage_date or x.date < oldest_coverage_date:
oldest_coverage_dates.update({x.signal_id: x.date})
latest_coverage_tuples = [(v, k) for k, v in latest_coverage_dates.items()]
oldest_coverage_tuples = [(v, k) for k, v in oldest_coverage_dates.items()]
update_statement = f'''UPDATE `{Database.SIGNAL_TABLE_NAME}`
SET `latest_coverage_update` = GREATEST(`latest_coverage_update`, %s)
WHERE `id` = %s
'''
self._cursor.executemany(update_statement, latest_coverage_tuples)
delete_statement = f'''DELETE FROM `{Database.COVERAGE_TABLE_NAME}`
WHERE `date` < %s
AND `signal_id` = %s
'''
self._cursor.executemany(delete_statement, oldest_coverage_tuples)
self._connection.commit()
def get_enabled_signals(self) -> List[DashboardSignal]:
"""Retrieve all enabled signals from the database"""
select_statement = f'''SELECT `id`,
`name`,
`source`,
`covidcast_signal`,
`latest_coverage_update`,
`latest_status_update`
FROM `{Database.SIGNAL_TABLE_NAME}`
WHERE `enabled`
'''
self._cursor.execute(select_statement)
enabled_signals = []
for result in self._cursor.fetchall():
enabled_signals.append(
DashboardSignal(
db_id=result[0],
name=result[1],
source=result[2],
covidcast_signal=result[3],
latest_coverage_update=result[4],
latest_status_update=result[5]))
return enabled_signals
def get_argument_parser():
"""Define command line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument("--log_file", help="filename for log output")
return parser
def get_latest_issue_from_metadata(dashboard_signal, metadata):
"""Get the most recent issue date for the signal."""
df_for_source = metadata[(metadata.data_source == dashboard_signal.source) & (
metadata.signal == dashboard_signal.covidcast_signal)]
max_issue = df_for_source["max_issue"].max()
return pd.to_datetime(str(max_issue), format="%Y%m%d").date()
def get_latest_time_value_from_metadata(dashboard_signal, metadata):
"""Get the most recent date with data for the signal."""
df_for_source = metadata[(metadata.data_source == dashboard_signal.source) & (
metadata.signal == dashboard_signal.covidcast_signal)]
return df_for_source["max_time"].max().date()
def get_coverage(dashboard_signal: DashboardSignal) -> List[DashboardSignalCoverage]:
"""Get the most recent coverage for the signal."""
count_by_geo_type_df = pd.read_csv(
COVERAGE_URL.format(source=dashboard_signal.source,
signal=dashboard_signal.covidcast_signal))
try:
count_by_geo_type_df["time_value"] = count_by_geo_type_df["time_value"].apply(
lambda x: pd.to_datetime(str(x), format="%Y%m%d"))
except:
count_by_geo_type_df["time_value"] = count_by_geo_type_df["time_value"].apply(
lambda x: pd.to_datetime(Week(x // 100, x % 100).startdate()))
signal_coverage_list = []
for _, row in count_by_geo_type_df.iterrows():
signal_coverage = DashboardSignalCoverage(
signal_id=dashboard_signal.db_id,
date=row['time_value'].date(),
geo_type='county',
count=row['count'])
signal_coverage_list.append(signal_coverage)
return signal_coverage_list
def main(args):
"""Generate data for the signal dashboard.
`args`: parsed command-line arguments
"""
log_file = None
if args:
log_file = args.log_file
logger = get_structured_logger(
"signal_dash_data_generator",
filename=log_file, log_exceptions=False)
start_time = time.time()
database = Database()
signals_to_generate = database.get_enabled_signals()
logger.info("Starting generating dashboard data.", enabled_signals=[
signal.name for signal in signals_to_generate])
metadata = covidcast.metadata()
signal_status_list: List[DashboardSignalStatus] = []
coverage_list: List[DashboardSignalCoverage] = []
for dashboard_signal in signals_to_generate:
latest_issue = get_latest_issue_from_metadata(
dashboard_signal,
metadata)
latest_time_value = get_latest_time_value_from_metadata(
dashboard_signal, metadata)
latest_coverage = get_coverage(dashboard_signal)
signal_status_list.append(
DashboardSignalStatus(
signal_id=dashboard_signal.db_id,
date=datetime.date.today(),
latest_issue=latest_issue,
latest_time_value=latest_time_value))
coverage_list.extend(latest_coverage)
try:
database.write_status(signal_status_list)
logger.info("Wrote status.", rowcount=database.rowcount())
except mysql.connector.Error as exception:
logger.exception(exception)
try:
database.write_coverage(coverage_list)
logger.info("Wrote coverage.", rowcount=database.rowcount())
except mysql.connector.Error as exception:
logger.exception(exception)
logger.info(
"Generated signal dashboard data",
total_runtime_in_seconds=round(time.time() - start_time, 2))
return True
if __name__ == '__main__':
if not main(get_argument_parser().parse_args()):
sys.exit(1)