project/analysis/analyzers/render/default.py

116 lines
3.1 KiB
Python

import json
import logging
from typing import List
import datetime
import matplotlib.pyplot as plt
from analysis.analyzers import LogEntryCountAnalyzer
from analysis.util.meta_temp import KML_PATTERN
from . import Render, Result
from analysis.analyzers import LocationAnalyzer
log = logging.getLogger(__name__)
class PrintRender(Render):
def render(self, results: List[Result], name=None):
print("\t" + "\n\t".join([str(r) for r in results]))
class JSONRender(Render):
def render(self, results: List[Result], name=None):
print(json.dumps([r.get() for r in self.filter(results)], indent=1))
class TrackRender(Render):
result_types = [LocationAnalyzer]
def render(self, results: List[Result], name=None):
data = []
log.debug(results)
for result in self.filter(results):
if len(result.get()) > 0:
data.append(
[[entry['location']['coordinates'][1], entry['location']['coordinates'][0]] for entry in
# TODO: configurable
result.get()])
dumps = json.dumps(data)
with open("track_data.js", "w") as out:
out.write("tracks=" + dumps + ";")
return dumps
def format_time(ts):
return datetime.datetime.fromtimestamp(ts/1000).strftime("%Y-%m-%dT%H:%M:%S.%f")
class KMLRender(Render):
result_types = [LocationAnalyzer]
def render(self, results: List[Result], name=None):
files = []
for result in self.filter(results):
times = ["<when>{time}</when>".format(time=format_time(entry["timestamp"])) for entry in result.get()]
coords = [
"<gx:coord>{long} {lat} 0.0</gx:coord>"
.format(
lat=entry['location']['coordinates'][1],
long=entry['location']['coordinates'][0])
for entry in result.get()
]
filename = str(result.name)+".kml"
print(filename)
with open(filename, "w") as out:
out.write(KML_PATTERN.format(name=str(result.name), coordinates="\n".join(coords), when="\n".join(times)))
files.append(filename)
return files
class HeatMapRender(TrackRender):
weight = 0.01
def render(self, results: List[Result], name=None):
raw = super(HeatMapRender, self).render(results)
data = []
for session in json.loads(raw):
data += [(entry[0], entry[1], self.weight) for entry in session]
dumps = json.dumps(data)
with open('heat_data.js', 'w') as out:
out.write("coords = " + dumps + ";")
return dumps
class LogEntryCountAnalyzerPlot(Render):
result_types = [LogEntryCountAnalyzer]
def render(self, results: List[Result], name=None):
raw_data = list(self.filter(results))[0].get()
print(raw_data)
labels = []
data = []
for x in sorted(raw_data.items()):
labels.append(str(x[0]).split(".")[-1])
data.append(x[1])
plt.bar(range(len(data)), list(data))
plt.xticks(range(len(data)), labels, rotation="vertical")
plt.tight_layout()
name = "plots/{}.png".format(name)
plt.savefig(name)
plt.cla()
plt.clf()
plt.close()
class LogEntryCountCSV(Render):
result_types = [LogEntryCountAnalyzer]
summary = None
def render(self, results: List[Result], name=None):
if self.summary is None:
return
for result in self.filter(results):
raw_data = result.get()
self.summary[name] = raw_data