Commit a8aee891 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

map view fully added

parent 8bae9d72
...@@ -9,3 +9,4 @@ ...@@ -9,3 +9,4 @@
[2020-10-01 15:11:55,695] INFO in views: superuser admin deleted for application 8cc6f4b1 device 2 [2020-10-01 15:11:55,695] INFO in views: superuser admin deleted for application 8cc6f4b1 device 2
[2020-10-07 17:18:50,561] INFO in views: Logged in superuser admin [2020-10-07 17:18:50,561] INFO in views: Logged in superuser admin
[2020-10-07 17:19:21,368] INFO in views: Logged in superuser admin [2020-10-07 17:19:21,368] INFO in views: Logged in superuser admin
[2020-10-07 19:11:04,501] INFO in views: superuser admin added new device testgeo for application 8cc6f4b1
...@@ -128,7 +128,13 @@ def get_list(cur, appkey): ...@@ -128,7 +128,13 @@ def get_list(cur, appkey):
""" """
cur.execute( cur.execute(
sql.SQL(query).format(sql.Identifier(tn))) sql.SQL(query).format(sql.Identifier(tn)))
return (True, cur.fetchall())
devlist = cur.fetchall()
for i in range(len(devlist)):
devlist[i] = [d for d in devlist[i]]
devlist[i][3] = json.loads(devlist[i][3].tobytes())
return (True, devlist)
@with_psql @with_psql
......
from psycopg2 import sql from psycopg2 import sql
import app.helpers.device_data_model as ddm
from app.helpers.misc import with_psql, utc_roundhour, utc_roundday, utc_local_diff from app.helpers.misc import with_psql, utc_roundhour, utc_roundday, utc_local_diff
import app.dao.application.application as ad import app.dao.application.application as ad
import app.dao.device.device as dd import app.dao.device.device as dd
import app.dao.data.data as data
import json
# appkeys is a list of tuples [(app1), (app2), ..., (appn)] # appkeys is a list of tuples [(app1), (app2), ..., (appn)]
# devids is a list of lists of tuples [[(dev1),...],[(dev1),...]] # devids is a list of lists of tuples [[(dev1),...],[(dev1),...]]
...@@ -174,13 +177,21 @@ def get_recent_activity(cur, username, n=5): ...@@ -174,13 +177,21 @@ def get_recent_activity(cur, username, n=5):
else: else:
return (True, []) return (True, [])
@with_psql def get_devices_locations(username):
def get_devices_locations(cur, username):
devlocs = {} devlocs = {}
apps = ad.get_list(username)[1] apps = ad.get_list(username)[1]
for a in apps: for a in apps:
devs = dd.get_list(a[1])[1] devs = dd.get_list(a[1])[1]
print(devs) devs = [d for d in devs if all (k in d[3]['format'] for k in ('lat', 'lon'))]
if devs != []:
for d in devs:
ddata = data.get_last_n(a[1], d[1], 1)
if ddata[0]:
devlocs[a[1]] = {int(d[1]) : {}}
ddata = ddm.decode_datum(ddata[1][0], d[3])
#decodify the data and put it into the dict
devlocs[a[1]][int(d[1])].update({'appname' : a[0], 'devname' : d[0], 'utc' : int(ddata[0]), 'lat' : ddata[2]['lat'], 'lon': ddata[2]['lon']})
return devlocs
...@@ -2,9 +2,6 @@ import msgpack ...@@ -2,9 +2,6 @@ import msgpack
import struct import struct
from collections import OrderedDict from collections import OrderedDict
import json import json
import app.helpers.misc as misc
from psycopg2 import Binary
from datetime import datetime
MODELS = { MODELS = {
'json' : 'JSON', 'json' : 'JSON',
...@@ -95,36 +92,6 @@ def test_done(): ...@@ -95,36 +92,6 @@ def test_done():
} }
print(read_data_ddm(rdata, ddm)) print(read_data_ddm(rdata, ddm))
@misc.with_psql
def mpack_test(cur):
import random
m = {
"temperature" : random.randint(0, 100),
"lever": random.randint(0,1)
}
m = msgpack.packb(m)
query = """
INSERT INTO dev_3b56f3d8_3 VALUES ({}, '{}', {})
""".format(misc.get_utc(), datetime.now().strftime('%H:%M:%S'), Binary(m))
cur.execute(query)
return (True,)
@misc.with_psql
def raw_test(cur):
import random
upstr = '<fQ20s?h'
m = struct.pack(upstr, random.random()*1000, random.randint(10000,10000000), 'hello'.encode('utf-8'), random.randint(0,2), random.randint(0, 30000))
query = """
INSERT INTO dev_3b56f3d8_2 VALUES ({}, '{}', {})
""".format(misc.get_utc(), datetime.now().strftime('%H:%M:%S'), Binary(m))
cur.execute(query)
return (True,)
def decode_datum(data, ddm): def decode_datum(data, ddm):
data = [d for d in data] data = [d for d in data]
......
...@@ -105,12 +105,26 @@ ...@@ -105,12 +105,26 @@
crossorigin=""> crossorigin="">
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
var devslocs = JSON.parse('{{ devslocs | safe }}');
var devmap = L.map('map').setView([50, 50], 2); var devmap = L.map('map').setView([50, 50], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
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>', 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>',
maxZoom: 18 maxZoom: 18
}).addTo(devmap); }).addTo(devmap);
Object.entries(devslocs).forEach(([appkey, devdata]) => {
applink = "/application/" + appkey;
Object.entries(devdata).forEach(([devid, data]) => {
devlink = applink + "/device/" + devid;
var devmarker = L.marker([data["lat"],data["lon"]]).addTo(devmap);
var popuptxt = "<b> Application: <a href='"+applink+"'>"+data["appname"] + "</a></b><br>";
popuptxt += "<b> Device: <a href='"+devlink+"'>"+data["devname"] + "</a></b><br>";
var dateutc = new Date(data["utc"]*1000);
popuptxt += "<b>Timestamp: </b>"+dateutc.toString();
devmarker.bindPopup(popuptxt).openPopup();
});
});
</script> </script>
{% endblock %} {% endblock %}
...@@ -659,6 +659,6 @@ def map(): ...@@ -659,6 +659,6 @@ def map():
last_activity = md.get_user_data_count_per_day(session['name'])[1][0] last_activity = md.get_user_data_count_per_day(session['name'])[1][0]
info = [created_apps, active_devices, total_activity, last_activity] info = [created_apps, active_devices, total_activity, last_activity]
devslocs = md.get_devices_locations(session['name'])
import json
return render_template('views/public/map.html', info=info) return render_template('views/public/map.html', info=info, devslocs=json.dumps(devslocs))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment