-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudchasers.py
More file actions
247 lines (205 loc) · 8.79 KB
/
cloudchasers.py
File metadata and controls
247 lines (205 loc) · 8.79 KB
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
from flask import Flask
from flask import render_template
from flask import send_from_directory
import datetime
import random
from couchbase.cluster import Cluster, ClusterOptions
from couchbase.cluster import PasswordAuthenticator
from couchbase.cluster import QueryOptions
from couchbase.exceptions import CouchbaseException
from couchbase.cluster import AnalyticsOptions
from couchbase.management.queries import CreatePrimaryQueryIndexOptions
endpoint = ''
username = ''
password = ''
bucketName = ''
# Initialize the Connection
cluster = Cluster('couchbases://' + endpoint + '?ssl=no_verify', ClusterOptions(PasswordAuthenticator(username, password)))
cb = cluster.bucket(bucketName)
cb_coll = cb.default_collection()
app = Flask(__name__)
#cties = ['Munich', 'Manchester', 'Stockholm', 'Paris', 'Oslo', 'London']
query = "select location from couchbasecloudbucket group by location;"
cties = []
for row in cluster.query(query):
cties.append(row.get('location', ''))
@app.route("/")
def index():
query = "select location from couchbasecloudbucket group by location;"
cties = []
for row in cluster.query(query):
cties.append(row.get('location', ''))
cities = []
for city in cties:
temperature = []
humidity = []
wind_speed = []
rainfall = []
sunshine = []
query = "select lw.temperature, lw.humidity, lw.wind_speed, lw.sunshine, lw.rainfall, lw.timestamp from couchbasecloudbucket lw where lw.location = '{city}' and lw.timestamp > {timestamp} and type is missing order by lw.timestamp asc;"
for row in cluster.query(query.format(**{'city': city, 'timestamp': (int(datetime.datetime.now().timestamp()) - 3600 * 4)})):
temperature.append([row.get('timestamp',0) * 1000, row.get('temperature', 0)])
humidity.append([row.get('timestamp',0) * 1000, row.get('humidity', 0)])
wind_speed.append([row.get('timestamp',0) * 1000, row.get('wind_speed', 0)])
sunshine.append([row.get('timestamp',0) * 1000, int(bool(row.get('sunshine', False)) == True)])
rainfall.append([row.get('timestamp',0) * 1000, row.get('rainfall', 0)])
cities.append({'name': city,
'temperature': temperature,
'humidity': humidity,
'wind_speed': wind_speed,
'rainfall': rainfall,
'sunshine': sunshine
})
return render_template('dashboard.html', cities=cities)
@app.route("/analytics")
def analytics():
analytics = {}
ct = 1
query = "select lw.location, sum(lw.rainfall) as rainfall_total from cloudchasers lw where lw.timestamp > %s group by lw.location order by rainfall_total desc;" % (int(datetime.datetime.now().timestamp()) - 604800)
rainfall_total = []
res = cluster.analytics_query(query)
for row in res:
row['counter'] = ct
row['rainfall_total'] = format(row.get('rainfall_total', 0) * 60, '.2f')
ct+=1
rainfall_total.append(row)
analytics['rainfall_total'] = rainfall_total
ct = 1
query = "select lw.location, sum(lw.sunshine) as sunshine_total from cloudchasers lw where lw.timestamp > %s group by lw.location order by sunshine_total desc;" % (int(datetime.datetime.now().timestamp()) - 604800)
sunshine_total = []
res = cluster.analytics_query(query)
for row in res:
row['counter'] = ct
row['sunshine_total'] = format(row.get('sunshine_total', 0) / 60, '.1f')
ct+=1
sunshine_total.append(row)
analytics['sunshine_total'] = sunshine_total
return render_template('analytics.html', analytics=analytics)
@app.route("/reports")
def reports():
reports = {}
query = "select temperature, location, timestamp from couchbasecloudbucket where type is missing order by temperature asc limit 10;"
res = cluster.query(query)
res_transf = []
ct = 1
for row in res:
row['counter'] = ct
ct+=1
row['timestamp'] = datetime.datetime.utcfromtimestamp(row.get('timestamp', 0)).isoformat()
row['temperature'] = format(row.get('temperature', 0), '.2f')
res_transf.append(row)
reports['temperature_low'] = res_transf
query = "select temperature, location, timestamp from couchbasecloudbucket where type is missing order by temperature desc limit 10;"
res = cluster.query(query)
res_transf = []
ct = 1
for row in res:
row['counter'] = ct
ct+=1
row['temperature'] = format(row.get('temperature', 0), '.2f')
row['timestamp'] = datetime.datetime.utcfromtimestamp(row.get('timestamp', 0)).isoformat()
res_transf.append(row)
reports['temperature_high'] = res_transf
query = "select humidity, location, timestamp from couchbasecloudbucket where type is missing order by humidity asc limit 10;"
res = cluster.query(query)
res_transf = []
ct = 1
for row in res:
row['counter'] = ct
ct+=1
row['humidity'] = format(row.get('humidity', 0), '.2f')
row['timestamp'] = datetime.datetime.utcfromtimestamp(row.get('timestamp', 0)).isoformat()
res_transf.append(row)
reports['humidity_low'] = res_transf
query = "select humidity, location, timestamp from couchbasecloudbucket where type is missing order by humidity desc limit 10;"
res = cluster.query(query)
res_transf = []
ct = 1
for row in res:
row['counter'] = ct
ct+=1
row['humidity'] = format(row.get('humidity', 0), '.2f')
row['timestamp'] = datetime.datetime.utcfromtimestamp(row.get('timestamp', 0)).isoformat()
res_transf.append(row)
reports['humidity_high'] = res_transf
return render_template('reports.html', reports=reports)
@app.route("/alarms")
def alarms():
query = "select location from couchbasecloudbucket group by location;"
cties = []
for row in cluster.query(query):
cties.append(row.get('location', ''))
# normal distributions values
# distribution_params = [mu, sigma]
day_temp_distr = [5, 4]
night_temp_distr = [0, 4]
wind_speed_distr = [4, 2]
humidity_distr = [80, 8]
rain_distr = [0.003, 0.0005]
sunshine_prob = 0.3
for city in cties:
humidity = -1
wind_speed = -1
timestamp = int(datetime.datetime.now().timestamp())
rnd = random.random()
if rnd > 0.6:
wind_speed = rnd * 50
else:
while wind_speed < 0:
wind_speed = random.normalvariate(wind_speed_distr[0], wind_speed_distr[1])
while (humidity > 100 or humidity < 0):
humidity = random.normalvariate(humidity_distr[0], humidity_distr[1])
if (random.random() <= sunshine_prob):
sunshine = 1
else:
sunshine = 0
hour = datetime.datetime.now().hour
if (hour > 7 or hour > 19):
temperature = random.normalvariate(day_temp_distr[0], day_temp_distr[1])
else:
temperature = random.normalvariate(night_temp_distr[0], night_temp_distr[1])
if (random.random() <= (14/30)):
rainfall_mm_sec = random.normalvariate(rain_distr[0], rain_distr[1])
else:
rainfall_mm_sec = 0
key = city + "::" + str(timestamp)
value = {
"location": city,
"timestamp": timestamp,
"temperature": temperature,
"humidity": humidity,
"wind_speed": wind_speed,
"sunshine": sunshine,
"rainfall": rainfall_mm_sec
}
cb_coll.upsert(key, value)
alarms = {}
query = "select location, status, wind_speed from couchbasecloudbucket where type = 'WindAlert';"
res = cluster.query(query)
res_transf = []
for row in res:
status = row.get('status', '')
if status == 'No storm':
status = '<i style="color:MediumSeaGreen;font-size:20px" class="bi bi-emoji-sunglasses"></i>'
elif status == 'Violent storm':
status = '<i style="color:Tomato;font-size:20px" class="bi bi-emoji-dizzy"></i>'
elif status == 'Storm':
status = '<i style="color:Orange;font-size:20px" class="bi bi-emoji-frown"></i>'
elif status == 'Hurricane':
status = '<i style="color:Violet;font-size:20px" class="bi bi-emoji-dizzy-fill"></i>'
row['status_icon'] = status
row['wind_speed'] = format(row.get('wind_speed', 0), '.2f')
res_transf.append(row)
alarms['alarms'] = res_transf
return render_template('alarms.html', alarms=alarms)
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('templates/js', path)
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('templates/css', path)
@app.route('/assets/<path:path>')
def send_assets(path):
return send_from_directory('templates/assets', path)
if __name__ == "__main__":
app.run(host='0.0.0.0')