2012-05-11 18:39:26 +02:00
|
|
|
import json
|
2013-03-12 20:09:48 +01:00
|
|
|
import datetime
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
class D3MapBuilder:
|
|
|
|
def __init__(self, db):
|
2012-05-12 14:57:25 +02:00
|
|
|
self._db = db
|
2012-05-11 18:39:26 +02:00
|
|
|
|
|
|
|
def build(self):
|
|
|
|
output = dict()
|
|
|
|
|
2013-03-12 20:54:14 +01:00
|
|
|
now = datetime.datetime.utcnow().replace(microsecond=0)
|
2013-03-12 20:52:20 +01:00
|
|
|
|
2012-06-06 03:34:52 +02:00
|
|
|
nodes = self._db.get_nodes()
|
|
|
|
|
2012-06-07 22:21:50 +02:00
|
|
|
output['nodes'] = [{'name': x.name, 'id': x.id,
|
2012-06-07 00:02:45 +02:00
|
|
|
'macs': ', '.join(x.macs),
|
2013-01-17 22:13:20 +01:00
|
|
|
'geo': [float(x) for x in x.gps.split(" ")] if x.gps else None,
|
2014-02-02 23:28:09 +01:00
|
|
|
'firmware': x.firmware,
|
2014-04-10 17:17:11 +02:00
|
|
|
'flags': x.flags,
|
|
|
|
'clientcount': x.clientcount
|
2012-06-12 00:37:07 +02:00
|
|
|
} for x in nodes]
|
2012-06-11 23:53:45 +02:00
|
|
|
|
|
|
|
links = self._db.get_links()
|
|
|
|
|
|
|
|
output['links'] = [{'source': x.source.id, 'target': x.target.id,
|
2012-06-06 03:34:52 +02:00
|
|
|
'quality': x.quality,
|
2012-06-07 22:58:45 +02:00
|
|
|
'type': x.type,
|
2012-06-11 23:53:45 +02:00
|
|
|
'id': x.id
|
|
|
|
} for x in links]
|
2012-05-11 18:39:26 +02:00
|
|
|
|
2013-03-12 20:09:48 +01:00
|
|
|
output['meta'] = {
|
2013-03-12 20:52:20 +01:00
|
|
|
'timestamp': now.isoformat()
|
2013-03-12 20:09:48 +01:00
|
|
|
}
|
|
|
|
|
2012-05-11 18:39:26 +02:00
|
|
|
return json.dumps(output)
|
|
|
|
|