aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/frontend/html/pages/page.js
blob: 89a86a0448eebcda02edd6e9a6729ee560d966cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright Epic Games, Inc. All Rights Reserved.

"use strict";

import { WidgetHost } from "../util/widgets.js"
import { Fetcher } from "../util/fetcher.js"

////////////////////////////////////////////////////////////////////////////////
export class PageBase extends WidgetHost
{
	constructor(parent, params)
	{
		super(parent)
		this._params = params;
	}

	set_title(name)
	{
		var value = document.title;
		if (name.length && value.length)
			name = value + " - " + name;
		document.title = name;
	}

	get_param(name, fallback=undefined)
	{
		var ret = this._params.get(name);
		if (ret != undefined)
			return ret;

		if (fallback != undefined)
			this.set_param(name, fallback);

		return fallback;
	}

	set_param(name, value, update=true)
	{
		this._params.set(name, value);
		if (!update)
			return value;

		const url = new URL(window.location);
		for (var [key, xfer] of this._params)
			url.searchParams.set(key, xfer);
		history.replaceState(null, "", url);

		return value;
	}

	reload()
	{
		window.location.reload();
	}
}



////////////////////////////////////////////////////////////////////////////////
export class ZenPage extends PageBase
{
	constructor(parent, ...args)
	{
		super(parent, ...args);
		super.set_title("zen");
		this.add_branding(parent);
		this.add_service_nav(parent);
		this.generate_crumbs();
	}

	add_branding(parent)
	{
		var banner = parent.tag("zen-banner");
		banner.attr("subtitle", "Server");
		banner.attr("tagline", "Local Storage Service");
		banner.attr("logo-src", "favicon.ico");
		banner.attr("load", "0");

		this._banner = banner;
		this._poll_status();
	}

	static _mode_taglines = {
		"Server":  "Local Storage Service",
		"Proxy":   "Proxy Service",
		"Compute": "Compute Service",
		"Hub":     "Hub Service",
	};

	async _poll_status()
	{
		try
		{
			var cbo = await new Fetcher().resource("/status/status").cbo();
			if (cbo)
			{
				var obj = cbo.as_object();

				var mode = obj.find("serverMode");
				if (mode)
				{
					var modeStr = mode.as_value();
					this._banner.attr("subtitle", modeStr);
					var tagline = ZenPage._mode_taglines[modeStr] || modeStr;

					var hostname = obj.find("hostname");
					if (hostname)
						tagline += " \u2014 " + hostname.as_value();

					this._banner.attr("tagline", tagline);
				}

				var cpu = obj.find("cpuUsagePercent");
				if (cpu)
				{
					this._banner.attr("load", cpu.as_value().toFixed(1));
				}
			}
		}
		catch (e) { console.warn("status poll:", e); }

		setTimeout(() => this._poll_status(), 2000);
	}

	add_service_nav(parent)
	{
		const nav = parent.tag().id("service_nav");

		// Map service base URIs to dashboard links, this table is also used to detemine
		// which links to show based on the services that are currently registered.

		const service_dashboards = [
			{ base_uri: "/sessions/", label: "Sessions",		href: "/dashboard/?page=sessions" },
			{ base_uri: "/z$/",       label: "Cache",			href: "/dashboard/?page=cache" },
			{ base_uri: "/prj/",      label: "Projects",		href: "/dashboard/?page=projects" },
			{ base_uri: "/compute/",  label: "Compute",		href: "/dashboard/?page=compute" },
			{ base_uri: "/orch/",     label: "Orchestrator",	href: "/dashboard/?page=orchestrator" },
			{ base_uri: "/hub/",      label: "Hub",			href: "/dashboard/?page=hub" },
			{ base_uri: "/proxy/",    label: "Proxy",			href: "/dashboard/?page=proxy" },
		];

		nav.tag("a").text("Home").attr("href", "/dashboard/");

		this._info_link = nav.tag("a").text("Info").attr("href", "/dashboard/?page=info");

		new Fetcher().resource("/api/").json().then((data) => {
			const services = data.services || [];
			const uris = new Set(services.map(s => s.base_uri));

			const links = service_dashboards.filter(d => uris.has(d.base_uri));

			// Insert service links before the Info link
			const info_elem = this._info_link.inner();
			for (const link of links)
			{
				const a = document.createElement("a");
				a.textContent = link.label;
				a.href = link.href;
				info_elem.parentNode.insertBefore(a, info_elem);
			}
		}).catch(() => {});
	}

	set_title(...args)
	{
		super.set_title(...args);
	}

	generate_crumbs()
	{
		var auto_name = this.get_param("page") || "start";
		if (auto_name == "start")
			return;

		const crumbs = this.tag().id("crumbs");
		const new_crumb = function(name, search=undefined) {
			crumbs.tag();
			var crumb = crumbs.tag().text(name);
			if (search != undefined)
				crumb.on_click((x) => window.location.search = x, search);
		};

		new_crumb("home", "");

		var project = this.get_param("project");
		if (project != undefined)
		{
			auto_name = project;
			var oplog = this.get_param("oplog");
			if (oplog != undefined)
			{
				new_crumb(auto_name, `?page=project&project=${project}`);
				auto_name = oplog;
				var opkey = this.get_param("opkey")
				if (opkey != undefined)
				{
					new_crumb(auto_name, `?page=oplog&project=${project}&oplog=${oplog}`);
					auto_name = opkey.split("/").pop().split("\\").pop();

					// Check if we're viewing cook artifacts
					var page = this.get_param("page");
					var hash = this.get_param("hash");
					if (hash != undefined && page == "cookartifacts")
					{
						new_crumb(auto_name, `?page=entry&project=${project}&oplog=${oplog}&opkey=${opkey}`);
						auto_name = "cook artifacts";
					}
				}
			}
		}

		new_crumb(auto_name);
	}
}