Skip to content

Commit

Permalink
complte website
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Sep 4, 2023
1 parent 83daf2b commit 5e2f256
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 93 deletions.
37 changes: 32 additions & 5 deletions services/ansible_openvpn/docker/dashboard/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,48 @@
client = Elasticsearch(
configuration.get("url", "https://es01:9200"),
api_key=(configuration["id"], configuration["api_key"]),
verify_certs=configuration.get("verify_certs", True), timeout=60
verify_certs=configuration.get("verify_certs", True), request_timeout=60
)


@app.get("/api/sensor_position")
async def get_sensor_position(request: Request):
@app.get("/api/sensor_record_count/{start_epoch_millis}/{end_epoch_millis}")
async def get_sensor_record_count(request: Request, start_epoch_millis: int,
end_epoch_millis: int):
post_data = json.loads(
templates.get_template("query_sensor_list.json").render())
templates.get_template("query_sensor_record_count.json").render(
start_time=start_epoch_millis, end_time=end_epoch_millis))
resp = client.search(**post_data)
# reformat elastic search result
return [{"hwa": bucket["key"], "count": bucket["doc_count"]}
for bucket in resp["aggregations"]["group"]["buckets"]]


@app.get("/get-last-record/{sensor_id}")
async def get_sensor_6_hours_count(request: Request):
post_data = json.loads(
templates.get_template("query_sensor_uptime.json").render())
resp = client.search(**post_data)
# reformat elastic search result
return [{"hwa": hit["fields"]["hwa"][0], "date": hit["fields"]["date"][0],
"lat": hit["fields"]["TPV.lat"][0],
"lon": hit["fields"]["TPV.lon"][0]}
for bucket in resp["aggregations"]["group"]["buckets"] for hit in
bucket["group_docs"]["hits"]["hits"]]
bucket["group_docs"]["hits"]["hits"] if "TPV.lat" in hit["fields"].keys()]


@app.get("/api/sensor_position")
async def get_sensor_position(request: Request):
post_data = json.loads(
templates.get_template("query_sensor_list.json").render())
resp = client.search(**post_data)
# reformat elastic search result
return [
{"hwa": sub_hit["fields"]["hwa"][0],
"lat": sub_hit["fields"]["TPV.lat"][0],
"lon": sub_hit["fields"]["TPV.lon"][0],
"date": sub_hit["fields"]["date"][0]}
for hit in resp["hits"]["hits"] for
sub_hit in hit["inner_hits"]["most_recent"]["hits"]["hits"]]


@app.get('/', response_class=HTMLResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ h1 {
float: left;
margin-right: 8px;
opacity: 0.7;
}
}
.uptime {
text-align: center;
float: center;
}
100 changes: 56 additions & 44 deletions services/ansible_openvpn/docker/dashboard/app/static/status.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,72 @@

function onMarkerClick(e) {
selectedSensor = e.target.options.data["hwa"];
// Fetch the last sensor record
$.getJSON( "/get-last-record/" + selectedSensor, function( data ) {
if ( $("#uptimectrl").length == 0 ) {
upTimeControl.addTo(lmap);
loadDateTime();
buildUpTime();
}
var pickerCtrl = $('input[name="datetimes"]').data('daterangepicker');
var start = pickerCtrl.startDate.clone();
var end = pickerCtrl.endDate.clone();
start = start.add(start.utcOffset(), 'm').valueOf();
end = end.add(end.utcOffset(), 'm').valueOf();
var sensorEpoch = data["hits"]["hits"][0]["_source"]["timestamp"];
// Always display the last month of selected sensor data
start = moment(sensorEpoch).utc().startOf('month').valueOf();
end = moment(sensorEpoch).utc().endOf('month').valueOf();
pickerCtrl.setStartDate(moment(start));
pickerCtrl.setEndDate(moment(end));
uptimeChart(selectedSensor, start, end);
});
}


function getStations(lmap, sensorsLayer) {
$.getJSON( "static/network.json", function( data ) {
$.getJSON( "/api/sensor_position", function( data ) {
var minLat = 90;
var maxLat = -90;
var minLong = 180;
var maxLong = -180;
$.each( data, function( key, val ) {
var lat = val.lat;
var lon = val.long;
var style = {data: val, title:"id: "+val.esid+"\nlocal ip: 192.168.1."+val.box_id_sensor+"\n4g id: "+val["4g_router_id"], icon: greyIcon};
sensorsLayer.addLayer(L.marker([lat, lon], style));
var lon = val.lon;
var style = {data: val, title:"id: "+val.hwa, icon: greyIcon};
var marker = L.marker([lat, lon], style);
marker.on('click', onMarkerClick);
sensorsLayer.addLayer(marker);
minLat = Math.min(minLat, lat);
minLong = Math.min(minLong, lon);
maxLat = Math.max(maxLat, lat);
maxLong = Math.max(maxLong, lon);
});
// Set extent to sensors
if(minLat < maxLat && minLong < maxLong) {
if(minLat < 90 && minLong < 180) {
lmap.fitBounds([ [minLat, minLong], [maxLat, maxLong] ]);
}
// Update sensors status from database
getStationsRecordCount(lmap, sensorsLayer);
});
}

function getRouters(lmap, routersLayer) {
var params = {cache: false, url: "generated/routers.json",dataType: "json", success: function( data ) {
$.each( data, function( key, val ) {
var lat = val.lat;
var lon = val.long;
var cabinetIcon = redCabinetIcon;
var title = "id: "+val.id+"\n4g id: "+val["4g_router_id"]+"\nmac: "+val["mac"];
if("online" in val) {
title+= "\nonline: "+new Date(val.online * 1000).toISOString()
if(moment().subtract(25, 'hours').valueOf() / 1000 < val.online) {
cabinetIcon = greenCabinetIcon;
}
}
if("clients" in val) {
title+="\nclients: \n"+val["clients"].join("\n");
}
var style = {data: val, title:title, icon: cabinetIcon};
routersLayer.addLayer(L.marker([lat, lon], style));
});
}};
$.ajax(params);
}

function getStationsRecordCount(lmap, sensorsLayer) {
var start_time = moment().subtract(14, 'minutes').valueOf();
var end_time = moment().subtract(2, 'minutes').valueOf();
var start_time = moment().subtract(30, 'minutes').valueOf();
var end_time = moment().valueOf();
var expected_records = Math.trunc((end_time - start_time) / 1000 / 10);
var sensors = new Map();
$.getJSON( "sensors-record-count/" + start_time + "/" + end_time, function( data ) {
$.each( data.aggregations.group.buckets, function( key, val ) {
$.getJSON( "/api/sensor_record_count/" + start_time + "/" + end_time, function( data ) {
$.each( data, function( key, val ) {
// Set sensor status
sensors.set(val.key, val.doc_count);
sensors.set(val.hwa, val.count);
});
// Loop over leaflet markers
sensorsLayer.eachLayer(function(val) {
var icon = val.options.icon;
if(!sensors.has(val.options.data.esid)) {
if(!sensors.has(val.options.data.hwa)) {
val.setIcon(redIcon);
} else if(sensors.get(val.options.data.esid) < expected_records - 1) {
} else if(sensors.get(val.options.data.hwa) < expected_records - 1) {
val.setIcon(yellowIcon);
} else {
val.setIcon(greenIcon);
Expand All @@ -77,18 +81,15 @@ var lmap = L.map('mapid').setView([47.7456, -3.3687], 16);
var sensors = L.layerGroup();
var routers = L.layerGroup();

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: 'streets-v11'
}).addTo(lmap);
var osm = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
maxZoom: 18, attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>, &copy; <a href="https://carto.com/attribution">CARTO</a>'});

osm.addTo(lmap);

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

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

var legend = L.control({position: 'bottomright'});
Expand All @@ -113,4 +114,15 @@ legend.onAdd = function (lmap) {

legend.addTo(lmap);

L.control.locate().addTo(lmap);
L.control.locate().addTo(lmap);


var upTimeControl = L.control({position: 'bottomcenter', });


upTimeControl.onAdd = function (lmap) {
var div = L.DomUtil.create('div', 'info uptime');
div.id = "uptimectrl";
div.innerHTML += "<div class=\"form-style-7\"> <input type=\"text\" name=\"datetimes\"/> </div> <div id=\"chart\"></div>";
return div;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"index": "sensor_indicators_*",
"_source": "date_start",
"size": 1,
"sort": [
{
"date_start": {
"order": "desc"
}
}
],
"query": {
"match": {
"hwa": "{{ sensor_id }}"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
{
"index": "sensor_location_*",
"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"
}
"query": {
"bool": {
"must": [
{
"exists": {
"field": "TPV.lat"
}
}
]
}
},
"_source": false,
"collapse": {
"field": "hwa.keyword",
"inner_hits": {
"name": "most_recent",
"fields": [
"TPV.lat",
"TPV.lon",
"hwa",
"date"
],
"size": 1,
"sort": [
{
"date": "desc"
},
"group_docs": {
"top_hits": {
"size": 1,
"_source": false,
"sort": [
{
"date": {
"order": "desc"
}
}
]
}
{
"TPV.lat": "desc"
}
],
"_source": false
},
"max_concurrent_group_searches": 4
},
"sort": [
{
"date": {
"order": "desc"
}
}
}
],
"from": 0
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"index": "sensor_indicators_*",
"size": 0,
"query": {
"bool": {
Expand All @@ -8,10 +9,10 @@
},
{
"range": {
"timestamp": {
"gte": {{ start_time }},
"lte": {{ end_time }},
"format": "epoch_millis"
"date_start": {
"gte": {{ start_time }},
"lte": {{ end_time }},
"format": "epoch_millis"
}
}
}
Expand All @@ -24,18 +25,18 @@
"aggs": {
"group": {
"terms": {
"field": "producerID",
"field": "hwa.keyword",
"size" : 200
},
"aggs": {
"types_count" : { "value_count" : { "field" : "producerID" } },
"types_count" : { "value_count" : { "field" : "hwa.keyword" } },
"group_docs": {
"top_hits": {
"_source": ["timestamp"],
"_source": ["date_start"],
"size": 1,
"sort": [
{
"timestamp": {
"date_start": {
"order": "desc"
}
}
Expand All @@ -45,4 +46,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"index": "sensor_indicators_*",
"aggs": {
"daily": {
"date_histogram": {
"field": "date_start",
"interval": "6h",
"format": "yyyy-MM-dd HH"
}
}
},
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"date_start": {
"gte": {{ start_time }},
"lte": {{ end_time }}
}
}
},
{
"match": {
"hwa.keyword": "{{ sensor_id }}"
}
}
]
}
}
}
Loading

0 comments on commit 5e2f256

Please sign in to comment.