-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
259 lines (201 loc) · 7.35 KB
/
api.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
from flask import Flask, jsonify, json, request
import MySQLdb
import datetime
#from flask_restful import Resource, Api
#from flask_sqlalchemy import SQLAlchemy
#from sqlalchemy import create_engine
#declare that our app is a WSGI (Web Server Gateway Interface) app
app = Flask(__name__)
#Connect to db
db = MySQLdb.connect(#host="localhost",
user="root",
#port=3306,
passwd="",
db="testDB",
unix_socket="/opt/lampp/var/mysql/mysql.sock")
cursor = db.cursor() #initialize cursor
def construct_json(tbl,result):
'''Prepare data to be displayed in JSON format
This module prepares data to be formated in JSON
according to the table name passed as argument.
Matches dictionary keys, representing database
column names, to their appropriate values passed
in the result array parameter.
:type tbl: string
:param tbl: the name of the database table
:type result: 2D array
:param result: the array returned from cursor.fetchall()
:returns: a list with the constructed dictionary
'''
data = []
if tbl=="LivingRoomTemp":
for i in range(0,len(result)):
temp_data={
'id' : result[i][0],
'room' : result[i][1],
'temperature' : result[i][2],
'scale' : result[i][3],
'date' : result[i][4]
}
data.append(temp_data)
elif tbl=="VRMS":
for i in range(0,len(result)):
temp_data={
'id' : result[i][0],
'topic' : result[i][1],
'value' : result[i][2],
'date' : result[i][3]
}
data.append(temp_data)
return data
@app.route('/all/<table>')
def get_all(table):
'''Construct "localhost:5000/all/<table>" url functionality.
Executes appropriate sql query to return all entries in the
corresponding table of the database in JSON format.
Valid table names: "LivingRoomTemp", "VRMS"
:type table: string
:param table: the table name given in the URL
:returns: the query result set in JSON format
'''
try:
cursor.execute("SELECT * FROM "+table)
except Exception as e:
return "Exception type: "+ str(e)
result = cursor.fetchall()
data = construct_json(table,result)
return jsonify({'data' : data})
@app.route('/today/<table>')
def get_today(table):
'''Construct "localhost:5000/today/<table>" URL functionality.
Retrieves current date from Datetime module.
Executes appropriate sql query to return entries in the
corresponding table of the database with today's timestamp,
in JSON format. Valid table names: "LivingRoomTemp", "VRMS".
:type table: string
:param table: the table name given in the URL
:returns: the query result set in JSON format
'''
now = datetime.datetime.now()
rightnow = now.strftime("%Y-%m-%d")
rnow = rightnow+'%' #attach a % behind date for query purposes
select_stmt = "SELECT * FROM " +table+ " WHERE timestamp LIKE %(test)s"
try:
cursor.execute(select_stmt, {'test': rnow})
except Exception as e:
return "Exception type: "+ str(e)
result = cursor.fetchall()
data = construct_json(table,result)
return jsonify({'data' : data})
@app.route('/query/<table>')
def query(table):
'''Construct "localhost:5000/query/<table>?startDate=value1&endDate=value2" URL
functionality
Retrieves dates given in the URL as parameters. Executes appropriate sql query
to return entries in the corresponding table of the database, between the given
dates. Input values in YYYY-MM-DD format. Valid table names: "LivingRoomTemp",
"VRMS". Entries corresponding to second date are not returned.
Uncomment commented parts to implement pagination.
:type table: string
:param table: the table name given in the URL
:returns: the query result set in JSON format
'''
date1 = request.args.get('startDate')
date2 = request.args.get('endDate')
#page = request.args.get('page', default=1, type=int)
#limit = request.args.get('limit',default = 5, type=int)
#my_int = ((page-1)*limit)+1
queryy="SELECT * FROM "+table+" WHERE timestamp BETWEEN %(d1)s AND %(d2)s" #AND id>="+str(my_int)+" LIMIT "+str(limit)
try:
cursor.execute(queryy,{'d1': date1, 'd2': date2})
except Exception as e:
return "Exception type: "+ str(e)
result = cursor.fetchall()
data = construct_json(table,result)
return jsonify({'data' : data})
@app.route('/paginate/<table>')
def return_all(table):
'''Implements pagination. Construct "localhost:5000/paginate/<table> URL functionality
Executes appropriate sql query to return all entries in the corresponding
table of the database, paginated. Pass "limit=.." & "page=.." parameters
in URL to change the defaults and navigate through pages.
Default values: limit=5, page=1
:type table: string
:param table: the table name given in the URL
:returns: the query result set in JSON format
'''
page = request.args.get('page', default=1, type=int)
limit = request.args.get('limit',default = 5, type=int)
my_int = ((page-1)*limit)+1 #appropriate id number to appear first in each page
try:
cursor.execute("SELECT * FROM "+table+" WHERE id>="+str(my_int)+" LIMIT "+str(limit))
except Exception as e:
return "Exception type: "+ str(e)
result = cursor.fetchall()
data = construct_json(table,result)
return jsonify({'data': data})
@app.route('/avg/<table>')
def avg_between_dates(table):
'''Construct "localhost:5000/avg/<table>?date1=value1&date2=value2" URL functionality
Retrieves dates given in the URL as parameters. Executes appropriate sql query
to return the average value of in the corresponding table of the database,
between the given dates. Input values in YYYY-MM-DD format.
Valid table names: "LivingRoomTemp", "VRMS".
:type table: string
:param table: the table name given in the URL
:returns: the average value in JSON format. Value is either Temperature or VRMS
measurement, depending on the table name provided.
'''
date1 = request.args.get('date1')
date2 = request.args.get('date2')
queryy="SELECT AVG(payload) FROM "+table+" WHERE timestamp BETWEEN %(d1)s AND %(d2)s"
try:
cursor.execute(queryy,{'d1': date1, 'd2': date2})
except Exception as e:
return "Exception type: "+ str(e)
result = cursor.fetchall()
data = []
if table=="LivingRoomTemp":
temp_data={
'Temperature':result[0][0]
}
elif table=="VRMS":
temp_data={
'VRMS': result[0][0]
}
data.append(temp_data)
return jsonify({'Average': data})
@app.route('/avg/today/<table>')
def avg_today(table):
'''Construct 'localhost:5000/avg/today/<table>' URL functionality.
Retrieves current date from Datetime module.
Executes appropriate sql query to return the average value of
the entries in the corresponding table of the database with
today's timestamp. Valid table names: "LivingRoomTemp", "VRMS".
:type table: string
:param table: the table name given in the URL
:returns: the average value in JSON format. Value is either Temperature or VRMS
measurement, depending on the table name provided.
'''
now = datetime.datetime.now()
rightnow = now.strftime("%Y-%m-%d")
rnow = rightnow+'%' #attach a % behind date for query purposes
select_stmt = "SELECT AVG(payload) FROM " +table+ " WHERE timestamp LIKE %(test)s"
try:
cursor.execute(select_stmt, {'test': rnow})
except Exception as e:
return "Exception type: "+ str(e)
result = cursor.fetchall()
data = []
if table=="LivingRoomTemp":
temp_data={
'Temperature':result[0][0]
}
elif table=="VRMS":
temp_data={
'VRMS': result[0][0]
}
data.append(temp_data)
return jsonify({'Average_today': data})
if __name__ == '__main__':
app.run()