Auto-reload page on new test

This commit is contained in:
Alexey Lavrenuke 2014-10-14 15:40:43 +04:00
parent b6f5228d35
commit 78155444c3
4 changed files with 45 additions and 15 deletions

View File

@ -1,6 +1,5 @@
''' local webserver with online graphs '''
from threading import Thread
import json
import logging
import os.path
import time
@ -66,6 +65,7 @@ class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener):
def end_test(self, retcode):
self.server.send({'reload': True})
raw_input('Press Enter to stop report server.')
del self.server
self.server = None
@ -77,16 +77,23 @@ class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener):
address = socket.gethostname()
self.log.info("Starting local HTTP server for online view at port: http://%s:%s/", address, self.port)
self.server.serve()
self.server.send({'reload': True})
def aggregate_second(self, data):
data = decode_aggregate(data)
self.cache.store(data)
if self.server is not None:
self.server.send(json.dumps(data))
message = {
'data': data,
}
self.server.send(message)
def monitoring_data(self, data):
data = decode_monitoring(data)
self.cache.store(data)
if self.server is not None and len(data):
self.server.send(json.dumps(data))
message = {
'data': data,
}
self.server.send(message)

View File

@ -5,6 +5,7 @@ import tornado.ioloop
import tornado.web
import os.path
import json
import uuid
from tornado import template
from pyjade.ext.tornado import patch_tornado
patch_tornado()
@ -30,24 +31,32 @@ class Client(SocketConnection):
class MainHandler(tornado.web.RequestHandler):
cacher = None
def initialize(self, template):
def initialize(self, template, reportUUID):
self.template = template
self.reportUUID = reportUUID
def get(self):
if MainHandler.cacher is not None:
cached_data = MainHandler.cacher.get_all_data()
cached_data = {
'data': MainHandler.cacher.get_all_data(),
'uuid': self.reportUUID,
}
else:
cached_data = {}
cached_data = {
'data':{},
'uuid': self.reportUUID,
}
self.render(self.template, cached_data=json.dumps(cached_data))
class ReportServer(object):
def __init__(self, cacher):
router = TornadioRouter(Client)
self.reportUUID = uuid.uuid4().hex
self.app = tornado.web.Application(
router.apply_routes([
(r"/", MainHandler, dict(template='index.jade')),
(r"/brief\.html$", MainHandler, dict(template='brief.jade')),
(r"/monitoring\.html$", MainHandler, dict(template='monitoring.jade')),
(r"/", MainHandler, dict(template='index.jade', reportUUID=self.reportUUID)),
(r"/brief\.html$", MainHandler, dict(template='brief.jade', reportUUID=self.reportUUID)),
(r"/monitoring\.html$", MainHandler, dict(template='monitoring.jade', reportUUID=self.reportUUID)),
]),
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
@ -63,7 +72,8 @@ class ReportServer(object):
def send(self, data):
if Client.CONNECTION is not None:
Client.CONNECTION.send(data)
data['uuid'] = self.reportUUID
Client.CONNECTION.send(json.dumps(data))
if __name__ == "__main__":

View File

@ -11,7 +11,8 @@ collect_subtree = (storage, subtree, ts) ->
app.controller "TankReport", ($scope, $element) ->
$scope.status = "Disconnected"
$scope.data = document.cached_data
$scope.data = document.cached_data.data
$scope.uuid = document.cached_data.uuid
$scope.updateData = (tankData) ->
for ts, storages of tankData
for storage, data of storages
@ -85,7 +86,6 @@ app.controller "TankReport", ($scope, $element) ->
console.log("Connection opened...")
$scope.status = "Connected"
$scope.$apply()
#console.log document.cached_data
conn.on 'disconnect', () =>
console.log("Connection closed...")
@ -93,4 +93,9 @@ app.controller "TankReport", ($scope, $element) ->
$scope.$apply()
conn.on 'message', (msg) =>
tankData = JSON.parse msg
$scope.updateData(tankData)
if tankData.uuid and $scope.uuid != tankData.uuid
location.reload(true)
if tankData.reload
location.reload(true)
else
$scope.updateData(tankData.data)

View File

@ -24,7 +24,8 @@
app.controller("TankReport", function($scope, $element) {
var conn;
$scope.status = "Disconnected";
$scope.data = document.cached_data;
$scope.data = document.cached_data.data;
$scope.uuid = document.cached_data.uuid;
$scope.updateData = function(tankData) {
var data, storage, storages, ts;
for (ts in tankData) {
@ -168,7 +169,14 @@
return function(msg) {
var tankData;
tankData = JSON.parse(msg);
return $scope.updateData(tankData);
if (tankData.uuid && $scope.uuid !== tankData.uuid) {
location.reload(true);
}
if (tankData.reload) {
return location.reload(true);
} else {
return $scope.updateData(tankData.data);
}
};
})(this));
});