Skip to content

Commit

Permalink
display of sensors on map
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Sep 1, 2023
1 parent 9812b3f commit a140921
Show file tree
Hide file tree
Showing 74 changed files with 110 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ examples/*.ogg
.env
# openvpn server configuration
services/ansible_openvpn/docker/openvpn-data
services/ansible_openvpn/docker/dashboard/config.json
23 changes: 18 additions & 5 deletions services/ansible_openvpn/docker/dashboard/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
Derived from this work
https://github.com/docker/awesome-compose/tree/c2f8036fd353dae457eba7b9b436bf3a1c85d937/flask
# Dashboard website

To test locally
This frontend website is displaying the state and data of the sensor network.

It is linked to an elastic-search instance that store and index the files
generated by zero_*.py services.

This website and the backend is easily deployed if you are using the docker
services described in [ansible_openvpn](../ansible_openvpn) folder.

# Build and run image to container

```shell
sudo docker build ./ -t dashboard && sudo docker run -p "8080:80" -v ./app/:/home/dashboard/app/ dashboard
```

Then open localhost:8080 in browser

Without having to rebuild the image or restarting the container, all changes made in files will be automatically applied

docker build -t flasktest .
docker run --name flasktest -p 8000:8000 flasktest
8 changes: 8 additions & 0 deletions services/ansible_openvpn/docker/dashboard/app/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "hKUAUIoBOM9IQ1Kp18_2",
"name": "python2",
"api_key": "Qk77qq1AR3WxkwbuQw_Jcw",
"encoded": "aEtVQVVJb0JPTTlJUTFLcDE4XzI6UWs3N3FxMUFSM1d4a3didVF3X0pjdw==",
"url": "https://172.20.0.3:9200",
"verify_certs": false
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
47 changes: 34 additions & 13 deletions services/ansible_openvpn/docker/dashboard/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,44 @@
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from fastapi import FastAPI
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import json
import os
from elasticsearch import Elasticsearch
from elasticsearch.helpers import streaming_bulk
import elasticsearch.helpers

app = FastAPI()

app.mount("/static", StaticFiles(directory="app/static"), name="static")

app.mount("/fonts", StaticFiles(directory="app/fonts"), name="fonts")

templates = Jinja2Templates(directory="app/templates")

if not os.path.exists("app/config.json"):
raise Exception("Configuration file not found " +
os.path.abspath("app/config.json"))
configuration = json.load(open("app/config.json", "r"))

client = Elasticsearch(
configuration.get("url", "https://es01:9200"),
api_key=(configuration["id"], configuration["api_key"]),
verify_certs=configuration.get("verify_certs", True), timeout=60
)


@app.get("/api/sensor_position")
async def get_sensor_position(request: Request):
post_data = templates.get_template("query_sensor_list.json").render()
resp = client.search(index="sensor_location_*", query=post_data)
return resp

@app.get('/', response_class=HTMLResponse)
async def home():
return """
<!doctype html>
<html>
<head>
<title>NoiseSensor dashboard default page</title>
</head>
<body>
<p>Please run the Ansible playbook named deploy_elastic_web_dashboard.yml</p>
</body>
</html>"""
async def home(request: Request):
return templates.TemplateResponse("status.html",
context={"request": request})

Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ var lmap = L.map('mapid').setView([47.7456, -3.3687], 16);
var sensors = L.layerGroup();
var routers = L.layerGroup();

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1Ijoibmljb2xhcy1mIiwiYSI6ImNsbTBhMTkxMTE3ODkzZHA2ZGM0NWVhb3EifQ.XLAxFMiFQD1VomPnHjcFAA', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox.streets'
id: 'streets-v11'
}).addTo(lmap);

sensors.addTo(lmap);
routers.addTo(lmap);

getStations(lmap, sensors);
getRouters(lmap, routers);
//getStations(lmap, sensors);
//getRouters(lmap, routers);

var legend = L.control({position: 'bottomright'});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"size": 0,
"fields": [
"TPV.lat",
"TPV.lon",
"hwa",
"date"
],
"aggs": {
"group": {
"terms": {
"field": "hwa.keyword",
"size": 200
},
"aggs": {
"results_without_mb_id": {
"filter": {
"exists": {
"field": "TPV.lat"
}
}
},
"group_docs": {
"top_hits": {
"size": 1,
"_source": false,
"sort": [
{
"date": {
"order": "desc"
}
}
]
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<meta charset="utf-8">
<html>
<head>
<title>Live sensors map</title>
<link rel="stylesheet" type="text/css" href="static/status.css"/>
<script type="text/javascript" src="static/jquery.min.js"></script>
<script type="text/javascript" src="static/moment.min.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions services/ansible_openvpn/docker/dashboard/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
fastapi>=0.103.0,<0.104.0
uvicorn>=0.23.2,<0.24
Jinja2>=3.1.2,<3.2.0
elasticsearch>=8.9.0,<8.10.0
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
tasks:
- name: Create configuration file for website
copy:
content: "[ELASTIC_SEARCH]\nURL = es01\nAPI_ID = {{ api_key_id }}\nAPI_KEY = {{ api_key }}\n"
content: "{\"id\":\"{{ api_key_id }}\",\"name\":\"python\",\"api_key\":\"Qk77qq1AR3WxkwbuQw_Jcw\",\"es_url\":\"https://es01\",\"verify_certs\":false}"
dest: '{{ destination }}appconfig.ini'
- name: Website synchronization
ansible.posix.synchronize:
mode: 'pull'
src: '{{ playbook_dir }}../../website_dashboard'
src: '{{ playbook_dir }}../docker/dashboard'
dest: '{{ destination }}'
Original file line number Diff line number Diff line change
@@ -1 +1 @@
elasticsearch==8.9.0
elasticsearch>=8.9.0,<8.10.0
9 changes: 0 additions & 9 deletions services/website_dashboard/README.md

This file was deleted.

Empty file.
45 changes: 0 additions & 45 deletions services/website_dashboard/app/main.py

This file was deleted.

25 changes: 0 additions & 25 deletions services/website_dashboard/app/templates/query_sensor_list.json

This file was deleted.

2 changes: 0 additions & 2 deletions services/website_dashboard/requirements.txt

This file was deleted.

0 comments on commit a140921

Please sign in to comment.