File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed
Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,30 @@ def write_measurements(device_id):
178178 return None
179179
180180
181+ def get_measurements (device_id ):
182+ influxdb_client = InfluxDBClient (url = config .get ('APP' , 'INFLUX_URL' ),
183+ token = config .get ('APP' , 'INFLUX_TOKEN' ),
184+ org = config .get ('APP' , 'INFLUX_ORG' ))
185+
186+ # Queries must be formatted with single and double quotes correctly
187+ query_api = QueryApi (influxdb_client )
188+ device_id = str (device_id )
189+ device_filter = f'r.device == "{ device_id } "'
190+ flux_query = f'from(bucket: "{ config .get ("APP" , "INFLUX_BUCKET" )} ") ' \
191+ f'|> range(start: 0) ' \
192+ f'|> filter(fn: (r) => r._measurement == "environment" and { device_filter } ) ' \
193+ f'|> last()'
194+
195+ response = query_api .query (flux_query )
196+
197+ # iterate through the result(s)
198+ results = []
199+ for table in response :
200+ results .append (table .records [0 ].values )
201+
202+ return results
203+
204+
181205# Creates an authorization for a deviceId and writes it to a bucket
182206def create_device (device_id ) -> Authorization :
183207 device = get_device (device_id )
Original file line number Diff line number Diff line change @@ -85,3 +85,13 @@ def write():
8585 device_id = request .form .get ('device_id_input' , None )
8686 device_id = devices .write_measurements (device_id )
8787 return render_template ('write.html' , device_id = device_id )
88+
89+
90+ @app .route ('/data' , methods = ['GET' , 'POST' ])
91+ def data ():
92+ if request .method == 'GET' :
93+ return render_template ('data.html' , data = None )
94+ else :
95+ device_id = request .form .get ('device_id_input' , None )
96+ results = devices .get_measurements (device_id )
97+ return render_template ('data.html' , data = results )
Original file line number Diff line number Diff line change 3333 < li >
3434 < a class ="nav-link " href ="{{url_for('write')}} "> Write</ a >
3535 </ li >
36+ < li >
37+ < a class ="nav-link " href ="{{url_for('data')}} "> Data</ a >
38+ </ li >
3639 < li >
3740 < a class ="nav-link " href ="{{url_for('help_page')}} "> Help</ a >
3841 </ li >
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Data</ title >
6+ </ head >
7+ < body >
8+ {% extends 'base.html' %}
9+ {% block content %}
10+
11+
12+
13+ {% if data is none %}
14+ < div >
15+ < form method ="post " action ="/data ">
16+ < div class ="form-group ">
17+ < label for ="deviceIdInput "> IoT Device Name</ label >
18+ < input type ="text " class ="form-control " id ="deviceIdInput " name ="device_id_input ">
19+ </ div >
20+ < div class ="form-group ">
21+ < input type ="submit " class ="btn btn-primary " name ="set " value ="Query Data ">
22+ </ div >
23+ </ form >
24+ </ div >
25+ {% else %}
26+ < table id ="data " class ="table table-striped ">
27+ < thead >
28+ < tr >
29+ < th > Time</ th >
30+ < th > Device</ th >
31+ < th > Field</ th >
32+ < th > Value</ th >
33+ </ tr >
34+ </ thead >
35+ < tbody >
36+ {% for result in data %}
37+ < tr >
38+ < td > {{ result._time }}</ td >
39+ < td > {{ result.device }}</ td >
40+ < td > {{ result._field }}</ td >
41+ < td > {{ result._value }}</ td >
42+ </ tr >
43+ {% endfor %}
44+ </ tbody >
45+ </ table >
46+ {% endif %}
47+
48+ {% endblock %}
49+ </ body >
50+ </ html >
You can’t perform that action at this time.
0 commit comments