Commit c1148f73 authored by Tristan Cavelier's avatar Tristan Cavelier

monitor: use hal+json

parent 6b911e36
......@@ -98,7 +98,7 @@ strip-top-level-dir = true
recipe = hexagonit.recipe.download
url = ${:_profile_base_location_}/${:filename}
download-only = true
md5sum = f64caed50df8add6a38ce1eac7a61585
md5sum = 8a5af1a03086788006345e27647c2a1d
filename = monitor.py.in
mode = 0644
......
......@@ -26,6 +26,38 @@
});
}
///////////////////
// tools for HAL //
function getProperty(object, path) {
if (Array.isArray(path)) {
while (path.length) {
object = object[path.shift()];
}
} else {
return object[path];
}
return object;
}
function softGetProperty(object, path) {
try {
return getProperty(object, path);
} catch (ignored) {
return undefined;
}
}
function forceList(value) {
if (Array.isArray(value)) {
return value;
}
return [value];
}
///////////////////
var monitor_json_list = [];
function htmlToElementList(html) {
......@@ -35,24 +67,12 @@
return div.querySelectorAll("*");
}
function absoluteUrl(url) {
/*global URL, location */
try {
return new URL(url, location.href).href;
} catch (e) {
return url;
}
}
function resolveUrl(firstUrl) {
/*global URL */
var l = arguments.length, i = 1, url = "";
url = absoluteUrl(firstUrl);
while (i < l) {
url = new URL(arguments[i], url).href;
i += 1;
}
return url;
/*jslint plusplus: true */
/*global URL, location */
var l = arguments.length, i = 1, url = new URL(firstUrl, location.href);
while (i < l) { url = new URL(arguments[i++], url); }
return url.href;
}
function escapeHtml(html) {
......@@ -60,22 +80,25 @@
}
function loadAndRenderMonitorSection(root, monitor_dict, monitor_url) {
if (!monitor_dict.service_list) {
var table, service_list = forceList(softGetProperty(monitor_dict, ["_embedded", "service"]));
if (!service_list) {
root.textContent = "";
return;
}
var table = document.createElement("table");
table = document.createElement("table");
root.appendChild(table);
return Promise.all(monitor_dict.service_list.map(function (service_dict) {
var href_html_part = (service_dict.interface_url ? " href=\"" + escapeHtml(service_dict.interface_url) + "\"" : ""),
return Promise.all(service_list.map(function (service_dict) {
var interface_url = softGetProperty(service_dict, ["_links", "interface", "href"]),
status_url = softGetProperty(service_dict, ["_links", "status", "href"]),
href_html_part = (interface_url ? " href=\"" + escapeHtml(interface_url) + "\"" : ""),
title_html_part = (service_dict.title ? escapeHtml(service_dict.title) : "Untitled"),
row = htmlToElementList("<table><tbody><tr><td><a" + href_html_part + ">" + title_html_part + "</a></td><td>Loading status...</td><td><a" + href_html_part + "><div style=\"height: 10mm; width: 10mm; background-color: gray;\"></div></a></td></tr></tbody></table>");
table.appendChild(row[2]);
if (!service_dict.status_url) {
if (!status_url) {
row[5].textContent = "No status";
return;
}
return loadJson(resolveUrl(monitor_url, service_dict.status_url)).then(function (status_dict) {
return loadJson(resolveUrl(monitor_url, status_url)).then(function (status_dict) {
if (status_dict.description) {
row[2].title = status_dict.description;
}
......@@ -92,24 +115,24 @@
function loadAndRenderMonitorJson(root) {
root.textContent = "Loading monitor section...";
return loadJson("monitor.json").then(function (monitor_dict) {
return loadJson("monitor.haljson").then(function (monitor_dict) {
monitor_json_list.push(monitor_dict);
root.innerHTML = "";
var loading = loadAndRenderMonitorSection(root, monitor_dict);
if (!monitor_dict.monitor_url_list) { return loading; }
return Promise.all([loading, Promise.all(monitor_dict.monitor_url_list.map(function (url) {
var loading = loadAndRenderMonitorSection(root, monitor_dict), related_monitor_list = forceList(softGetProperty(monitor_dict, ["_links", "related_monitor"]));
if (!related_monitor_list) { return loading; }
return Promise.all([loading, Promise.all(related_monitor_list.map(function (link) {
var div = htmlToElementList("<div>Loading monitor section...</div>")[0];
root.appendChild(div);
if (url[url.length - 1] !== "/") {
url += "/";
if (link.href[link.href.length - 1] !== "/") {
link.href += "/";
}
url = resolveUrl(url, "monitor.json");
return loadJson(url).catch(function (reason) {
link.href = resolveUrl(link.href, "monitor.haljson");
return loadJson(link.href).catch(function (reason) {
div.textContent = (reason && (reason.name + ": " + reason.message));
}).then(function (monitor_dict) {
monitor_json_list.push(monitor_dict);
div.remove();
return loadAndRenderMonitorSection(root, monitor_dict, url);
return loadAndRenderMonitorSection(root, monitor_dict, link.href);
});
}))]);
});
......
......@@ -26,17 +26,26 @@ def main():
# XXX see old monitor.py.in
# generate monitor.json
monitor_dict = {}
tmp = softConfigGet(config, "monitor", "title")
if tmp:
monitor_dict["title"] = tmp
tmp = softConfigGet(config, "monitor", "monitor-url-list")
if tmp:
monitor_dict["monitor_url_list"] = tmp.split()
monitor_dict["_links"] = {"related_monitor": [{"href": url} for url in tmp.split()]}
if service_config_list:
monitor_dict["service_list"] = []
service_list = []
monitor_dict["_embedded"] = {"service": service_list}
for service_config in service_config_list:
service_dict = {}
monitor_dict["service_list"].append(service_dict)
setConfigIfNotEmptyToDict(service_config, "service", "title", service_dict, "title")
service_name = service_dict["service_name"] = service_config.get("service", "name")
service_dict["status_url"] = "/public/%s/status.json" % service_name
service_list.append(service_dict)
service_name = service_dict["id"] = service_config.get("service", "name")
service_dict["_links"] = {"status": {"href": "/public/%s/status.json" % service_name}}
tmp = softConfigGet(service_config, "service", "title")
if tmp:
service_dict["title"] = tmp
tmp = softConfigGet(service_config, "service", "interface-url")
if tmp:
service_dict["_links"]["interface"] = {"href": tmp}
with open(config.get("monitor", "monitor-json"), "w") as fp:
json.dump(monitor_dict, fp)
# create symlinks from service configurations
......@@ -59,11 +68,6 @@ def softConfigGet(config, *args, **kwargs):
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
return None
def setConfigIfNotEmptyToDict(config, section, option, dyct, key):
tmp = softConfigGet(config, section, option)
if tmp:
dyct[key] = tmp
def createSymlinksFromConfig(destination_folder_config_tuple, source_list_config_tuple):
destination_folder = softConfigGet(*destination_folder_config_tuple)
if destination_folder:
......
Markdown is supported
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