-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·255 lines (201 loc) · 7.26 KB
/
app.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
#!/usr/bin/env python
from flask import Flask, request, render_template, make_response, \
send_from_directory, url_for
import pymysql
import csv
import StringIO
import time
import uuid
from ibQuery import ibQuery
with open('/var/www/isoblueDb/isoblueDb/passwd') as f:
_db_passwd = f.readline().strip('\n')
# connect to the database
conn = pymysql.connect(host='localhost', \
user='isoblue', \
password=_db_passwd,
db='isoblueData')
app = Flask(__name__)
@app.route('/pgns')
def pgnDownloads():
return send_from_directory('static', 'pgns')
@app.route('/csv/<filename>')
def csvDownloads(filename):
return send_from_directory('csv', filename)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
return '%s is not implemented. Please stand by.' % path
@app.route('/getlasthour/<string:dataType>')
def getLastHour(dataType):
'''
Get specified message within the last hour.
Output as an csv.
'''
cursor = conn.cursor()
# Get the query parameters and the csv first row
isoblue_id = None
q = ibQuery(dataType, None)
if q.query_param is None:
return 'Sorry, we cannot find this data type.'
app.logger.debug('%s, %s', q.query_param, q.first_row)
sql = "SELECT " + q.query_param + " FROM `" + dataType + \
"` WHERE `ts` > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 HOUR)"
app.logger.info('Query is: %s', sql)
# Get start time
start = time.time()
# Execute the query
cursor.execute(sql)
conn.commit();
results = cursor.fetchall()
# Get end time
end = time.time()
if results:
app.logger.info('Query returned data!')
filename = '%s_last_hr_%s.csv' %(dataType, uuid.uuid4())
p = {'filename': filename, \
'count': len(results), \
'duration': round((end - start), 3),
'sql': sql}
# Write the fetched data into a csv
with open('/var/www/isoblueDb/isoblueDb/csv/' + filename, 'w') as f:
csv_out = csv.writer(f)
csv_out.writerow(q.first_row)
for row in results:
csv_out.writerow(row)
else:
app.logger.info('Query returned nothing!')
p = {'sql': sql}
app.logger.debug('Template parameters: %s', p)
return render_template('index.html', p=p)
# make output response as a attachment
# output = make_response(si.getvalue())
# output.headers['Content-Disposition'] = 'attachment; \
# filename=%s_last_hr.csv' %dataType
# output.headers['Content-Type'] = 'text/csv'
# return output
@app.route('/getlastday/<string:dataType>')
def getLastDay(dataType):
'''
Get specified message within the last day
Output as an csv.
'''
cursor = conn.cursor()
# Get the query parameters and the csv first row
isoblue_id = None
q = ibQuery(dataType, None)
if q.query_param is None:
return 'Sorry, we cannot find this data type.'
app.logger.debug('%s, %s', q.query_param, q.first_row)
sql = "SELECT " + q.query_param + " FROM `" + dataType + \
"` WHERE `ts` > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)"
app.logger.info('Query is: %s', sql)
# Get start time
start = time.time()
# Execute the query
cursor.execute(sql)
conn.commit();
results = cursor.fetchall()
# Get end time
end = time.time()
if results:
app.logger.info('Query returned data!')
filename = '%s_last_day_%s.csv' %(dataType, uuid.uuid4())
p = {'filename': filename, \
'count': len(results), \
'duration': round((end - start), 3),
'sql': sql}
# Write the fetched data into a csv
with open('/var/www/isoblueDb/isoblueDb/csv/' + filename, 'w') as f:
csv_out = csv.writer(f)
csv_out.writerow(q.first_row)
for row in results:
csv_out.writerow(row)
else:
app.logger.info('Query returned nothing!')
p = {'sql': sql}
return render_template('index.html', p=p)
@app.route('/getlasthourbyid/<string:dataType>/<string:isoblueId>')
def getLastHourById(dataType, isoblueId):
'''
Get specified message within the last hour by isoblue id.
Output as an csv.
'''
cursor = conn.cursor()
# Get the query parameters and the csv first row
q = ibQuery(dataType, isoblueId)
if q.query_param is None:
return 'Sorry, we cannot find this data type.'
app.logger.debug('%s, %s', q.query_param, q.first_row)
sql = "SELECT " + q.query_param + " FROM `" + dataType + \
"` WHERE `isoblue_id` LIKE '%" + isoblueId + "%' AND" + \
"`ts` > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 HOUR)"
app.logger.info('Query is: %s', sql)
# Get start time
start = time.time()
# Execute the query
cursor.execute(sql)
conn.commit();
results = cursor.fetchall()
# Get end time
end = time.time()
if results:
app.logger.info('Query returned data!')
filename = '%s_last_hour_%s_%s.csv' %(dataType, isoblueId, uuid.uuid4())
p = {'filename': filename, \
'count': len(results), \
'duration': round((end - start), 3),
'sql': sql}
# Write the fetched data into a csv
with open('/var/www/isoblueDb/isoblueDb/csv/%s_last_hr_%s.csv' \
%(dataType, isoblueId), 'w') as f:
csv_out = csv.writer(f)
csv_out.writerow(q.first_row)
for row in results:
csv_out.writerow(row)
else:
app.logger.info('Query returned nothing!')
p = {'sql': sql}
return render_template('index.html', p=p)
@app.route('/getlastdaybyid/<string:dataType>/<string:isoblueId>')
def getLastDayById(dataType, isoblueId):
'''
Get specified message within the last day by isoblue id.
Output as an csv.
'''
cursor = conn.cursor()
# Get the query parameters and the csv first row
q = ibQuery(dataType, isoblueId)
if q.query_param is None:
return 'Sorry, we cannot find this data type.'
app.logger.debug('%s, %s', q.query_param, q.first_row)
sql = "SELECT " + q.query_param + " FROM `" + dataType + \
"` WHERE `isoblue_id` LIKE '%" + isoblueId + "%' AND" + \
"`ts` > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY)"
app.logger.info('Query is: %s', sql)
# Get start time
start = time.time()
# Execute the query
cursor.execute(sql)
conn.commit();
results = cursor.fetchall()
# Get end time
end = time.time()
if results:
app.logger.info('Query returned data!')
filename = '%s_last_day_%s_%s.csv' %(dataType, isoblueId, uuid.uuid4())
p = {'filename': filename, \
'count': len(results), \
'duration': round((end - start), 3),
'sql': sql}
# Write the fetched data into a csv
with open('/var/www/isoblueDb/isoblueDb/csv/' + filename, 'w') as f:
csv_out = csv.writer(f)
csv_out.writerow(q.first_row)
for row in results:
csv_out.writerow(row)
else:
app.logger.info('Query returned nothing!')
p = {'sql': sql}
return render_template('index.html', p=p)
if __name__ == '__main__':
app.run('0.0.0.0', debug=True)