summaryrefslogtreecommitdiffstats
path: root/web/gui/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'web/gui/main.js')
-rw-r--r--web/gui/main.js807
1 files changed, 730 insertions, 77 deletions
diff --git a/web/gui/main.js b/web/gui/main.js
index efcd027b4b..3f149bbe5c 100644
--- a/web/gui/main.js
+++ b/web/gui/main.js
@@ -453,7 +453,7 @@ function saveObjectToClient(data, filename) {
saveTextToClient(JSON.stringify(data), filename);
}
-// --------------------------------------------------------------------
+// -----------------------------------------------------------------------------
// registry call back to render my-netdata menu
function toggleExpandIcon(svgEl) {
@@ -476,19 +476,6 @@ function toggleAgentItem(e, guid) {
}
}
-// TODO: consider renaming to `truncateString`
-
-/// Enforces a maximum string length while retaining the prefix and the postfix of
-/// the string.
-function clipString(str, maxLength) {
- if (str.length <= maxLength) {
- return str;
- }
-
- const spanLength = Math.floor((maxLength - 3) / 2);
- return `${str.substring(0, spanLength)}...${str.substring(str.length - spanLength)}`;
-}
-
// When you stream metrics from netdata to netdata, the recieving netdata now
// has multiple host databases. It's own, and multiple mirrored. Mirrored databases
// can be accessed with <http://localhost:19999/host/NAME/>
@@ -512,10 +499,20 @@ function renderStreamedHosts(options) {
return naturalSortCompare(a.hostname, b.hostname);
});
+ let displayedDatabases = false;
+
for (var s of sorted) {
let url, icon;
const hostname = s.hostname;
+ if (myNetdataMenuFilterValue !== "") {
+ if (!hostname.includes(myNetdataMenuFilterValue)) {
+ continue;
+ }
+ }
+
+ displayedDatabases = true;
+
if (hostname === master) {
url = `${base}/`;
icon = 'home';
@@ -537,11 +534,24 @@ function renderStreamedHosts(options) {
)
}
+ if (!displayedDatabases) {
+ html += (
+ `<div class="info-item">
+ <i class="fas fa-filter"></i>
+ <span style="margin-left: 8px">no databases match the filter criteria.<span>
+ </div>`
+ )
+ }
+
return html;
}
function renderMachines(machinesArray) {
- let html = `<div class="info-item">My netdata agents</div>`;
+ // let html = isSignedIn()
+ // ? `<div class="info-item">My nodes</div>`
+ // : `<div class="info-item">My nodes</div>`;
+
+ let html = `<div class="info-item">My nodes</div>`;
if (machinesArray === null) {
let ret = loadLocalStorage("registryCallback");
@@ -552,6 +562,9 @@ function renderMachines(machinesArray) {
}
let found = false;
+ let displayedAgents = false;
+
+ const maskedURL = NETDATA.registry.MASKED_DATA;
if (machinesArray) {
saveLocalStorage("registryCallback", JSON.stringify(machinesArray));
@@ -563,18 +576,31 @@ function renderMachines(machinesArray) {
for (var machine of machines) {
found = true;
+ if (myNetdataMenuFilterValue !== "") {
+ if (!machine.name.includes(myNetdataMenuFilterValue)) {
+ continue;
+ }
+ }
+
+ displayedAgents = true;
+
const alternateUrlItems = (
`<div class="agent-alternate-urls agent-${machine.guid} collapsed">
- ${machine.alternate_urls.reduce(
- (str, url) => str + (
- `<div class="agent-item agent-item--alternate">
- <div></div>
- <a href="${url}" title="${url}">${clipString(url, 64)}</a>
- <a href="#" onclick="deleteRegistryModalHandler('${machine.guid}', '${machine.name}', '${url}'); return false;">
- <i class="fas fa-trash" style="color: #777;"></i>
- </a>
- </div>`
- ),
+ ${machine.alternate_urls.reduce((str, url) => {
+ if (url === maskedURL) {
+ return str
+ }
+
+ return str + (
+ `<div class="agent-item agent-item--alternate">
+ <div></div>
+ <a href="${url}" title="${url}">${truncateString(url, 64)}</a>
+ <a href="#" onclick="deleteRegistryModalHandler('${machine.guid}', '${machine.name}', '${url}'); return false;">
+ <i class="fas fa-trash" style="color: #777;"></i>
+ </a>
+ </div>`
+ )
+ },
''
)}
</div>`
@@ -593,13 +619,22 @@ function renderMachines(machinesArray) {
${alternateUrlItems}`
)
}
+
+ if (found && (!displayedAgents)) {
+ html += (
+ `<div class="info-item">
+ <i class="fas fa-filter"></i>
+ <span style="margin-left: 8px">zero nodes are matching the filter value.<span>
+ </div>`
+ )
+ }
}
if (!found) {
if (machines) {
html += (
`<div class="info-item">
- <a href="https://github.com/netdata/netdata/tree/master/registry#netdata-registry" target="_blank">Your netdata server list is empty</a>
+ <a href="https://github.com/netdata/netdata/tree/master/registry#netdata-registry" target="_blank">Your nodes list is empty</a>
</div>`
)
} else {
@@ -611,7 +646,7 @@ function renderMachines(machinesArray) {
}
html += `<hr />`;
- html += `<div class="info-item">Demo netdata agents</div>`;
+ html += `<div class="info-item">Demo netdata nodes</div>`;
const demoServers = [
{url: "//london.netdata.rocks/default.html", title: "UK - London (DigitalOcean.com)"},
@@ -628,7 +663,7 @@ function renderMachines(machinesArray) {
for (var server of demoServers) {
html += (
`<div class="agent-item">
- <i class="fas fa-chart-bar" color: #fff"></i>
+ <i class="fas fa-chart-bar" style="color: #fff"></i>
<a href="${server.url}">${server.title}</a>
<div></div>
</div>
@@ -640,36 +675,110 @@ function renderMachines(machinesArray) {
return html;
}
-// Populates the my-netdata menu.
-function netdataRegistryCallback(machinesArray) {
+function setMyNetdataMenu(html) {
+ const el = document.getElementById('my-netdata-dropdown-content')
+ el.innerHTML = html;
+}
+
+function clearMyNetdataMenu() {
+ setMyNetdataMenu(`<div class="agent-item" style="white-space: nowrap">
+ <i class="fas fa-hourglass-half"></i>
+ Loading, please wait...
+ <div></div>
+ </div>`);
+}
+
+function errorMyNetdataMenu() {
+ setMyNetdataMenu(`<div class="agent-item" style="white-space: nowrap">
+ <i class="fas fa-exclamation-triangle" style="color: red"></i>
+ Cannot load known netdata agents from netdata.cloud!
+ <div></div>
+ </div>`);
+}
+
+function restrictMyNetdataMenu() {
+ setMyNetdataMenu(`<div class="info-item" style="white-space: nowrap">
+ <span>Please <a href="#" onclick="signInDidClick(event); return false">sign in to netdata.cloud</a> to view your nodes!</span>
+ <div></div>
+ </div>`);
+}
+
+function renderMyNetdataMenu(machinesArray) {
+ const el = document.getElementById('my-netdata-dropdown-content');
+ el.classList.add(`theme-${netdataTheme}`);
+
+ if (!isSignedIn()) {
+ if (!NETDATA.registry.isRegistryEnabled()) {
+ restrictMyNetdataMenu();
+ return;
+ }
+ }
+
+ if (machinesArray == registryAgents) {
+ console.log("Rendering my-netdata menu from registry");
+ } else {
+ console.log("Rendering my-netdata menu from netdata.cloud", machinesArray);
+ }
+
let html = '';
+ if (isSignedIn()) {
+ html += (
+ `<div class="filter-control">
+ <input
+ id="my-netdata-menu-filter-input"
+ type="text"
+ placeholder="filter nodes..."
+ autocomplete="off"
+ value="${myNetdataMenuFilterValue}"
+ onkeydown="myNetdataFilterDidChange(event)"
+ />
+ <span class="filter-control__clear" onclick="myNetdataFilterClearDidClick(event)"><i class="fas fa-times"></i><span>
+ </div>
+ <hr />`
+ );
+ }
+
if (options.hosts.length > 1) {
- html += renderStreamedHosts(options) + `<hr />`;
+ html += `<div id="my-netdata-menu-streamed">${renderStreamedHosts(options)}</div><hr />`;
}
- html += renderMachines(machinesArray);
-
- html += (
- `<hr />
- <div class="agent-item">
- <i class="fas fa-cog""></i>
- <a href="#" onclick="switchRegistryModalHandler(); return false;">Switch Identity</a>
- <div></div>
- </div>
- <div class="agent-item">
- <i class="fas fa-question-circle""></i>
- <a href="https://github.com/netdata/netdata/tree/master/registry#netdata-registry" target="_blank">What is this?</a>
- <div></div>
- </div>`
- )
+ html += `<div id="my-netdata-menu-machines">${renderMachines(machinesArray)}</div>`;
+
+ if (!isSignedIn()) {
+ html += (
+ `<hr />
+ <div class="agent-item">
+ <i class="fas fa-cog""></i>
+ <a href="#" onclick="switchRegistryModalHandler(); return false;">Switch Identity</a>
+ <div></div>
+ </div>
+ <div class="agent-item">
+ <i class="fas fa-question-circle""></i>
+ <a href="https://github.com/netdata/netdata/tree/master/registry#netdata-registry" target="_blank">What is this?</a>
+ <div></div>
+ </div>`
+ )
+ } else {
+ html += (
+ `<hr />
+ <div class="agent-item">
+ <i class="fas fa-sync"></i>
+ <a href="#" onclick="showSyncModal(); return false">Synchronize with netdata.cloud</a>
+ <div></div>
+ </div>
+ <div class="agent-item">
+ <i class="fas fa-question-circle""></i>
+ <a href="https://netdata.cloud/about" target="_blank">What is this?</a>
+ <div></div>
+ </div>`
+ )
+ }
- const el = document.getElementById('my-netdata-dropdown-content')
- el.classList.add(`theme-${netdataTheme}`);
el.innerHTML = html;
gotoServerInit();
-};
+}
function isdemo() {
if (this_is_demo !== null) {
@@ -764,6 +873,7 @@ function gotoServerValidateUrl(id, guid, url) {
} else {
document.getElementById('gotoServerResponse').innerHTML += 'found it! It is at:<br/><small>' + escapeUserInputHTML(url) + '</small>';
document.location = verifyURL(finalURL);
+ $('#gotoServerModal').modal('hide');
}
}
} else {
@@ -802,24 +912,29 @@ function gotoServerModalHandler(guid) {
gotoServerValidateUrl(count++, guid, url);
}
- setTimeout(function () {
- if (gotoServerStop === false) {
- document.getElementById('gotoServerResponse').innerHTML = '<b>Added all the known URLs for this machine.</b>';
- NETDATA.registry.search(guid, function (data) {
- // console.log(data);
- len = data.urls.length;
- while (len--) {
- var url = data.urls[len][1];
- // console.log(url);
- if (typeof checked[url] === 'undefined') {
- gotoServerValidateRemaining++;
- checked[url] = true;
- gotoServerValidateUrl(count++, guid, url);
+ if (!isSignedIn()) {
+ // When the registry is enabled, if the user's known URLs are not working
+ // we consult the registry to get additional URLs.
+ setTimeout(function () {
+ if (gotoServerStop === false) {
+ document.getElementById('gotoServerResponse').innerHTML = '<b>Added all the known URLs for this machine.</b>';
+ NETDATA.registry.search(guid, function (data) {
+ // console.log(data);
+ len = data.urls.length;
+ while (len--) {
+ var url = data.urls[len][1];
+ // console.log(url);
+ if (typeof checked[url] === 'undefined') {
+ gotoServerValidateRemaining++;
+ checked[url] = true;
+ gotoServerValidateUrl(count++, guid, url);
+ }
}
- }
- });
- }
- }, 2000);
+ });
+ }
+ }, 2000);
+ }
+
return false;
}
@@ -860,30 +975,55 @@ function notifyForSwitchRegistry() {
}
}
+var deleteRegistryGuid = null;
var deleteRegistryUrl = null;
function deleteRegistryModalHandler(guid, name, url) {
- void (guid);
+ // void (guid);
+ deleteRegistryGuid = guid;
deleteRegistryUrl = url;
+
document.getElementById('deleteRegistryServerName').innerHTML = name;
document.getElementById('deleteRegistryServerName2').innerHTML = name;
document.getElementById('deleteRegistryServerURL').innerHTML = url;
document.getElementById('deleteRegistryResponse').innerHTML = '';
+
$('#deleteRegistryModal').modal('show');
}
function notifyForDeleteRegistry() {
+ const responseEl = document.getElementById('deleteRegistryResponse');
+
if (deleteRegistryUrl) {
- NETDATA.registry.delete(deleteRegistryUrl, function (result) {
- if (result !== null) {
- deleteRegistryUrl = null;
- $('#deleteRegistryModal').modal('hide');
- NETDATA.registry.init();
- } else {
- document.getElementById('deleteRegistryResponse').innerHTML = "<b>Sorry! this command was rejected by the registry server.</b>";
- }
- });
+ if (isSignedIn()) {
+ deleteCloudAgentURL(deleteRegistryGuid, deleteRegistryUrl)
+ .then((count) => {
+ if (!count) {
+ responseEl.innerHTML = "<b>Sorry, this command was rejected by netdata.cloud!</b>";
+ return;
+ }
+ NETDATA.registry.delete(deleteRegistryUrl, function (result) {
+ if (result === null) {
+ console.log("Received error from registry", result);
+ }
+
+ deleteRegistryUrl = null;
+ $('#deleteRegistryModal').modal('hide');
+ NETDATA.registry.init();
+ });
+ });
+ } else {
+ NETDATA.registry.delete(deleteRegistryUrl, function (result) {
+ if (result !== null) {
+ deleteRegistryUrl = null;
+ $('#deleteRegistryModal').modal('hide');
+ NETDATA.registry.init();
+ } else {
+ responseEl.innerHTML = "<b>Sorry, this command was rejected by the registry server!</b>";
+ }
+ });
+ }
}
}
@@ -2563,7 +2703,7 @@ function initializeDynamicDashboardWithData(data) {
}
// update the dashboard hostname
- document.getElementById('hostname').innerHTML = options.hostname + ((netdataSnapshotData !== null) ? ' (snap)' : '').toString();
+ document.getElementById('hostname').innerHTML = options.hostname + ((netdataSnapshotData !== null) ? ' (snap)' : '').toString() + '&nbsp;&nbsp;<strong class="caret">';
document.getElementById('hostname').href = NETDATA.serverDefault;
document.getElementById('netdataVersion').innerHTML = options.version;
@@ -4344,3 +4484,516 @@ var selected_server_timezone = function (timezone, status) {
// var netdataStarted = performance.now();
var netdataCallback = initializeDynamicDashboard;
+
+// =================================================================================================
+// netdata.cloud
+
+let registryAgents = [];
+
+let cloudAgents = [];
+
+let myNetdataMenuFilterValue = "";
+
+let cloudAccountID = null;
+
+let cloudAccountName = null;
+
+let cloudToken = null;
+
+/// Enforces a maximum string length while retaining the prefix and the postfix of
+/// the string.
+function truncateString(str, maxLength) {
+ if (str.length <= maxLength) {
+ return str;
+ }
+
+ const spanLength = Math.floor((maxLength - 3) / 2);
+ return `${str.substring(0, spanLength)}...${str.substring(str.length - spanLength)}`;
+}
+
+// -------------------------------------------------------------------------------------------------
+// netdata.cloud API Client
+// -------------------------------------------------------------------------------------------------
+
+function isValidAgent(a) {
+ return a.urls != null && a.urls.length > 0;
+}
+
+// https://github.com/netdata/hub/issues/146
+function getCloudAccountAgents() {
+ if (!isSignedIn()) {
+ return [];
+ }
+
+ return fetch(
+ `${NETDATA.registry.cloudBaseURL}/api/v1/accounts/${cloudAccountID}/agents`,
+ {
+ method: "GET",
+ mode: "cors",
+ headers: {
+ "Authorization": `Bearer ${cloudToken}`
+ }
+ }
+ ).then((response) => {
+ if (!response.ok) {
+ throw Error("Cannot fetch known accounts");
+ }
+ return response.json();
+ }).then((payload) => {
+ const agents = payload.result ? payload.result.agents : null;
+
+ if (!agents) {
+ return [];
+ }
+
+ return agents.filter((a) => isValidAgent(a)).map((a) => {
+ return {
+ "guid": a.id,
+ "name": a.name,
+ "url": a.urls[0],
+ "alternate_urls": a.urls
+ }
+ })
+ }).catch(function (error) {
+ console.log(error);
+ return null;
+ });
+}
+
+// https://github.com/netdata/hub/issues/128
+function postCloudAccountAgents(agentsToSync) {
+ if (!isSignedIn()) {
+ return [];
+ }
+
+ const maskedURL = NETDATA.registry.MASKED_DATA;
+
+ const agents = agentsToSync.map((a) => {
+ const urls = a.alternate_urls.filter((url) => url != maskedURL);
+
+ return {
+ "id": a.guid,
+ "name": a.name,
+ "urls": urls
+ }
+ }).filter((a) => isValidAgent(a))
+
+ const payload = {
+ "accountID": cloudAccountID,
+ "agents": agents,
+ "merge": false,
+ };
+
+ return fetch(
+ `${NETDATA.registry.cloudBaseURL}/api/v1/accounts/${cloudAccountID}/agents`,
+ {
+ method: "POST",
+ mode: "cors",
+ headers: {
+ "Content-Type": "application/json; charset=utf-8",
+ "Authorization": `Bearer ${cloudToken}`
+ },
+ body: JSON.stringify(payload)
+ }
+ ).then((response) => {
+ return response.json();
+ }).then((payload) => {
+ const agents = payload.result ? payload.result.agents : null;
+
+ if (!agents) {
+ return [];
+ }
+
+ return agents.filter((a) => isValidAgent(a)).map((a) => {
+ return {
+ "guid": a.id,
+ "name": a.name,
+ "url": a.urls[0],
+ "alternate_urls": a.urls
+ }
+ })
+ });
+}
+
+function deleteCloudAgentURL(agentID, url) {
+ if (!isSignedIn()) {
+ return [];
+ }
+
+ return fetch(
+ `${NETDATA.registry.cloudBaseURL}/api/v1/accounts/${cloudAccountID}/agents/${agentID}/url?value=${encodeURIComponent(url)}`,
+ {
+ method: "DELETE",
+ mode: "cors",
+ headers: {
+ "Content-Type": "application/json; charset=utf-8",
+ "Authorization": `Bearer ${cloudToken}`
+ },
+ }
+ ).then((response) => {
+ return response.json();
+ }).then((payload) => {
+ const count = payload.result ? payload.result.count : 0;
+ return count;
+ });
+}
+
+// -------------------------------------------------------------------------------------------------
+
+function signInDidClick(e) {
+ e.preventDefault();
+ e.stopPropagation();
+
+ if (!NETDATA.registry.isUsingGlobalRegistry()) {
+ // If user is using a private registry, request his consent for
+ // synchronizing with cloud.
+ showSignInModal();
+ return;
+ }
+
+ signIn();
+}
+
+function signOutDidClick(e) {
+ e.preventDefault();
+ e.stopPropagation();
+ signOut();
+}
+
+// -------------------------------------------------------------------------------------------------
+
+function updateMyNetdataAfterFilterChange() {
+ const machinesEl = document.getElementById("my-netdata-menu-machines")
+ machinesEl.innerHTML = renderMachines(cloudAgents);
+
+ if (options.hosts.length > 1) {
+ const streamedEl = document.getElementById("my-netdata-menu-streamed")
+ streamedEl.innerHTML = renderStreamedHosts(options);
+ }
+}
+
+function myNetdataFilterDidChange(e) {
+ const inputEl = e.target;
+ setTimeout(() => {
+ myNetdataMenuFilterValue = inputEl.value;
+ updateMyNetdataAfterFilterChange();
+ }, 1);
+}
+
+function myNetdataFilterClearDidClick(e) {
+ e.preventDefault();
+ e.stopPropagation();
+
+ const inputEl = document.getElementById("my-netdata-menu-filter-input");
+ inputEl.value = "";
+ myNetdataMenuFilterValue = "";
+
+ updateMyNetdataAfterFilterChange();
+
+ inputEl.focus();
+}
+
+// -------------------------------------------------------------------------------------------------
+
+function clearCloudVariables() {
+ cloudAccountID = null;
+ cloudAccountName = null;
+ cloudToken = null;
+}
+
+function clearCloudLocalStorageItems() {
+ localStorage.removeItem("cloud.baseURL");
+ localStorage.removeItem("cloud.agentID");
+ localStorage.removeItem("cloud.sync");
+}
+
+function signIn() {
+ const url = `${NETDATA.registry.cloudBaseURL}/account/sign-in-agent?origin=${encodeURIComponent(window.location.origin + "/")}`;
+ window.open(url);
+}
+
+function signOut() {
+ cloudSSOSignOut();
+}
+
+function renderAccountUI() {
+ if (!NETDATA.registry.isCloudEnabled) {
+ return
+ }
+
+ const container = document.getElementById("account-menu-container");
+ if (isSignedIn()) {
+ container.removeAttribute("title");
+ container.removeAttribute("data-original-title");
+ container.removeAttribute("data-placement");
+ container.innerHTML = (
+ `<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span id="amc-account-name"></span> <strong class="caret"></strong></a>
+ <ul id="cloud-menu" class="dropdown-menu scrollable-menu inpagemenu" role="menu">
+ <li>
+ <a href="#" class="btn" onclick="signOutDidClick(event); return false">
+ <i class="fas fa-sign-out-alt"></i>&nbsp;&nbsp;<span class="hidden-sm hidden-md">Sign Out</span>
+ </a>
+ </li>
+ </ul>`
+ )
+ document.getElementById("amc-account-name").textContent = cloudAccountName; // Anti-XSS
+ } else {
+ container.setAttribute("data-original-title", "sign in");
+ container.setAttribute("data-placement", "bottom");
+ container.innerHTML = (
+ `<a href="#" class="btn" onclick="signInDidClick(event); return false">
+ <i class="fas fa-sign-in-alt"></i>&nbsp;<span class="hidden-sm hidden-md">Sign In</span>
+ </a>`
+ )
+ }
+}
+
+function handleMessage(e) {
+ switch (e.data.type) {
+ case "sign-in":
+ handleSignInMessage(e);
+ break;
+
+ case "sign-out":
+ handleSignOutMessage(e);
+ break;
+
+ default:
+ console.log("Unknown message", e);
+ }
+}
+
+function handleSignInMessage(e) {
+ localStorage.setItem("cloud.baseURL", NETDATA.registry.cloudBaseURL);
+
+ cloudAccountID = e.data.accountID;
+ cloudAccountName = e.data.accountName;
+ cloudToken = e.data.token;
+
+ netdataRegistryCallback(registryAgents);
+}
+
+function handleSignOutMessage(e) {
+ clearCloudVariables();
+ renderAccountUI();
+ renderMyNetdataMenu(registryAgents);
+}
+
+function isSignedIn() {
+ return cloudToken != null && cloudAccountID != null;
+}
+
+function sortedArraysEqual(a, b) {
+ if (a.length != b.length) return false;
+
+ for (var i = 0; i < a.length; ++i) {
+ if (a[i] !== b[i]) return false;
+ }
+
+ return true;
+}
+
+// If merging is needed returns the merged agents set, otherwise returns null.
+function mergeAgents(cloud, local) {
+ let dirty = false;
+
+ const union = new Map();
+
+ for (const cagent of cloud) {
+ union.set(cagent.guid, cagent);
+ }
+
+ for (const lagent of local) {
+ const cagent = union.get(lagent.guid);
+ if (cagent) {
+ for (const u of lagent.alternate_urls) {
+ if (u === NETDATA.registry.MASKED_DATA) { // TODO: temp until registry is updated.
+ continue;
+ }
+
+ if (!cagent.alternate_urls.includes(u)) {
+ dirty = true;
+ cagent.alternate_urls.push(u);
+ }
+ }
+ } else {
+ dirty = true;
+ union.set(lagent.guid, lagent);
+ }
+ }
+
+ if (dirty) {
+ return Array.from(union.values());
+ }
+
+ return null;
+}
+
+function showSignInModal() {
+ document.getElementById("sim-registry").innerHTML = NETDATA.registry.server;
+ $("#signInModal").modal("show");
+}
+
+function explicitlySignIn() {
+ $("#signInModal").modal("hide");
+ signIn();
+}
+
+function showSyncModal() {
+ document.getElementById("sync-registry-modal-registry").innerHTML = NETDATA.registry.server;
+ $("#syncRegistryModal").modal("show");
+}
+
+function explicitlySyncAgents() {
+ $("#syncRegistryModal").modal("hide");
+
+ const json = localStorage.getItem("cloud.sync");
+ const sync = json ? JSON.parse(json): {};
+ delete sync[cloudAccountID];
+ localStorage.setItem("cloud.sync", JSON.stringify(sync));
+
+ NETDATA.registry.init();
+}
+
+function syncAgents(callback) {
+ const json = localStorage.getItem("cloud.sync");
+ const sync = json ? JSON.parse(json): {};
+
+ const currentAgent = {
+ guid: NETDATA.registry.machine_guid,
+ name: NETDATA.registry.hostname,
+ url: NETDATA.serverDefault,
+ alternate_urls: [NETDATA.serverDefault],
+ }
+
+ const localAgents = sync[cloudAccountID]
+ ? [currentAgent]
+ : registryAgents.concat([currentAgent]);
+
+ console.log("Checking if sync is needed.", localAgents);
+
+ const agentsToSync = mergeAgents(cloudAgents, localAgents);
+
+ if ((!sync[cloudAccountID]) || agentsToSync) {
+ sync[cloudAccountID] = new Date().getTime();
+ localStorage.setItem("cloud.sync", JSON.stringify(sync));
+ }
+
+ if (agentsToSync) {
+ console.log("Synchronizing with netdata.cloud.");
+
+ postCloudAccountAgents(agentsToSync).then((agents) => {
+ // TODO: clear syncTime on error!
+ cloudAgents = agents;
+ callback(cloudAgents);
+ });
+
+ return
+ }
+
+ callback(cloudAgents);
+}
+
+let isCloudSSOInitialized = false;
+
+function cloudSSOInit() {
+ const iframeEl = document.getElementById("ssoifrm");
+ const url = `${NETDATA.registry.cloudBaseURL}/account/sso-agent?id=${NETDATA.registry.machine_guid}`;
+ iframeEl.src = url;
+ isCloudSSOInitialized = true;
+}
+
+function cloudSSOSignOut() {
+ const iframe = document.getElementById("ssoifrm");
+ const url = `${NETDATA.registry.cloudBaseURL}/account/sign-out-agent`;
+ iframe.src = url;
+}
+
+function initCloud() {
+ if (!NETDATA.registry.isCloudEnabled) {
+ clearCloudVariables();
+ clearCloudLocalStorageItems();
+ return;
+ }
+
+ if (NETDATA.registry.cloudBaseURL != localStorage.getItem("cloud.baseURL")) {
+ clearCloudVariables();
+ clearCloudLocalStorageItems();
+ if (NETDATA.registry.cloudBaseURL) {
+ localStorage.setItem("cloud.baseURL", NETDATA.registry.cloudBaseURL);
+ }
+ }
+
+ if (!isCloudSSOInitialized) {
+ cloudSSOInit();
+ }
+
+ renderAccountUI();
+}
+
+// This callback is called after NETDATA.registry is initialized.
+function netdataRegistryCallback(machinesArray) {
+ localStorage.setItem("cloud.agentID", NETDATA.registry.machine_guid);
+
+ initCloud();
+
+ registryAgents = machinesArray;
+
+ if (isSignedIn()) {
+ // We call getCloudAccountAgents() here because it requires that
+ // NETDATA.registry is initialized.
+ clearMyNetdataMenu();
+ getCloudAccountAgents().then((agents) => {
+ if (!agents) {
+ errorMyNetdataMenu();
+ return;
+ }
+ cloudAgents = agents;
+ syncAgents((agents) => {
+ const agentsMap = {}
+ for (const agent of agents) {
+ agentsMap[agent.guid] = agent;
+ }
+
+ NETDATA.registry.machines = agentsMap;
+ NETDATA.registry.machines_array = agents;
+
+ renderMyNetdataMenu(agents);
+ });
+ });
+ } else {
+ renderMyNetdataMenu(machinesArray)
+ }
+};
+
+// If we know the cloudBaseURL and agentID from local storage render (eagerly)
+// the account ui before receiving the definitive response from the web server.
+// This improves the perceived performance.
+function tryFastInitCloud() {
+ const baseURL = localStorage.getItem("cloud.baseURL");
+ const agentID = localStorage.getItem("cloud.agentID");
+
+ if (baseURL && agentID) {
+ NETDATA.registry.cloudBaseURL = baseURL;
+ NETDATA.registry.machine_guid = agentID;
+ NETDATA.registry.isCloudEnabled = true;
+
+ initCloud();
+ }
+}
+
+function initializeApp() {
+ window.addEventListener("message", handleMessage, false);
+
+// tryFastInitCloud();
+}
+
+if (document.readyState === "complete") {
+ initializeApp();
+} else {
+ document.addEventListener("readystatechange", () => {
+ if (document.readyState === "complete") {
+ initializeApp();
+ }
+ })
+}