-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (112 loc) · 4.18 KB
/
main.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
from flask import Flask, render_template
#from flask import jsonify
import google.auth
from google.cloud import bigquery
app = Flask(__name__)
@app.route('/')
def predict():
credentials, project_id = google.auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
project_id = 'covid19-302022'
bqclient = bigquery.Client(credentials= credentials,project=project_id)
#get predicted cases
query_string= """
SELECT
DATE(forecast_timestamp) as PredDate, round(m.forecast_value) as Cases
FROM
ML.FORECAST(MODEL Covid19NYT.cases_arima_model,
STRUCT(30 AS horizon, 0.8 AS confidence_level)) m
where m.forecast_timestamp = (select max(forecast_timestamp)
FROM
ML.FORECAST(MODEL Covid19NYT.cases_arima_model,
STRUCT(30 AS horizon, 0.8 AS confidence_level))
)
"""
query_job = bqclient.query(query_string)
results = query_job.result()
for row in results:
CasesPredictionDate = row.PredDate
PredCases = row.Cases
#get predicted deaths
query_string= """
SELECT
DATE(forecast_timestamp) as PredDate, round(m.forecast_value) as Deaths
FROM
ML.FORECAST(MODEL Covid19NYT.deaths_arima_model,
STRUCT(30 AS horizon, 0.8 AS confidence_level)) m
where m.forecast_timestamp = (select max(forecast_timestamp)
FROM
ML.FORECAST(MODEL Covid19NYT.deaths_arima_model,
STRUCT(30 AS horizon, 0.8 AS confidence_level))
)
"""
query_job = bqclient.query(query_string)
results = query_job.result()
for row in results:
DeathsPredictionDate = row.PredDate
PredDeaths = row.Deaths
#get most recent deaths
query_string= """
SELECT
cast(deaths_date_history as TIMESTAMP) AS deaths_date_history,
total_deaths_history as deaths_history_value,
FROM
(
SELECT
date as deaths_date_history,
SUM(daily_deaths) AS total_deaths_history
FROM
`covid19-302022.Covid19NYT.DailyCasesDeaths`
GROUP BY date
ORDER BY date ASC
) dcd
where dcd.deaths_date_history = (select max(date)
FROM `covid19-302022.Covid19NYT.DailyCasesDeaths`)
"""
query_job = bqclient.query(query_string)
results = query_job.result()
for row in results:
DeathsDate = row.deaths_date_history
QtyDeaths = row.deaths_history_value
#get most recent cases
query_string= """
SELECT
cast(cases_date_history as TIMESTAMP) AS cases_date_history,
total_cases_history as cases_history_value,
FROM
(
SELECT
date as cases_date_history,
SUM(daily_confirmed_cases) AS total_cases_history
FROM
`covid19-302022.Covid19NYT.DailyCasesDeaths`
GROUP BY date
ORDER BY date ASC
) dcd
where dcd.cases_date_history = (select max(date)
FROM `covid19-302022.Covid19NYT.DailyCasesDeaths`)
"""
query_job = bqclient.query(query_string)
results = query_job.result()
for row in results:
CasesDate = row.cases_date_history
QtyCases = row.cases_history_value
# this would not present the data, want to debug later
#val2 = jsonify(CasesPredictionDate=CasesPredictionDate.strftime('%m/%d/%Y'),
# PredictedCases=PredCases,
# DeathsPredictionDate=DeathsPredictionDate.strftime('%m/%d/%Y'),
# PredictedDeaths=PredDeaths,
# DeathsDate=DeathsDate.strftime('%m/%d/%Y'),
# MostRecentDeathCount=QtyDeaths,
# CasesDate=CasesDate.strftime('%m/%d/%Y'),
# MostRecentCasesCount=QtyCases)
val = {"CasesPredictionDate":CasesPredictionDate.strftime('%m/%d/%Y'), "PredictedCases":PredCases,
"DeathsPredictionDate":DeathsPredictionDate.strftime('%m/%d/%Y'),"PredictedDeaths":PredDeaths,
"DeathsDate":DeathsDate.strftime('%m/%d/%Y'),"MostRecentDeathCount":QtyDeaths,
"CasesDate":CasesDate.strftime('%m/%d/%Y'),"MostRecentCasesCount":QtyCases}
return render_template("page.html", title="COVID19: Current Stats and Predictions",jsonfile = val )
if __name__ == '__main__':
#predict()
#app.run(host='127.0.0.1', port=8080, debug = True)
app.run(host='0.0.0.0')