aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/frontend/html/pages/start.js
blob: 2cf12bf127ca4e1d89a35d5690232f6a11634721 (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
// Copyright Epic Games, Inc. All Rights Reserved.

"use strict";

import { ZenPage } from "./page.js"
import { Fetcher } from "../util/fetcher.js"
import { Friendly } from "../util/friendly.js"
import { Modal } from "../util/modal.js"
import { Table, Toolbar } from "../util/widgets.js"

////////////////////////////////////////////////////////////////////////////////
export class Page extends ZenPage
{
	async main()
	{
		// project list
		var section = this.add_section("projects");

		section.tag().classify("dropall").text("drop-all").on_click(() => this.drop_all("projects"));

		var columns = [
			"name",
			"project_dir",
			"engine_dir",
			"actions",
		];
		var project_table = section.add_widget(Table, columns);

		for (const project of await new Fetcher().resource("/prj/list").json())
		{
			var row = project_table.add_row(
				"",
				project.ProjectRootDir,
				project.EngineRootDir,
			);

			var cell = row.get_cell(0);
			cell.tag().text(project.Id).on_click((x) => this.view_project(x), project.Id);

			var cell = row.get_cell(-1);
			var action_tb = new Toolbar(cell, true);
			action_tb.left().add("view").on_click((x) => this.view_project(x), project.Id);
			action_tb.left().add("drop").on_click((x) => this.drop_project(x), project.Id);

			row.attr("zs_name", project.Id);
		}

		// cache
		var section = this.add_section("cache");

		section.tag().classify("dropall").text("drop-all").on_click(() => this.drop_all("z$"));

		columns = [
			"namespace",
			"dir",
			"buckets",
			"entries",
			"size disk",
			"size mem",
			"actions",
		]
		var zcache_info = new Fetcher().resource("/z$/").json();
		const cache_table = section.add_widget(Table, columns, Table.Flag_FitLeft|Table.Flag_PackRight);
		for (const namespace of (await zcache_info)["Namespaces"])
		{
			new Fetcher().resource(`/z$/${namespace}/`).json().then((data) => {
				const row = cache_table.add_row(
					"",
					data["Configuration"]["RootDir"],
					data["Buckets"].length,
					data["EntryCount"],
					Friendly.kib(data["StorageSize"].DiskSize),
					Friendly.kib(data["StorageSize"].MemorySize)
				);
				var cell = row.get_cell(0);
				cell.tag().text(namespace).on_click(() => this.view_zcache(namespace));
				row.get_cell(1).tag().text(namespace);

				cell = row.get_cell(-1);
				const action_tb = new Toolbar(cell, true);
				action_tb.left().add("view").on_click(() => this.view_zcache(namespace));
				action_tb.left().add("drop").on_click(() => this.drop_zcache(namespace));

				row.attr("zs_name", namespace);
			});
		}

		// stats
		const safe_lookup = (obj, path, pretty=undefined) => {
			const ret = path.split(".").reduce((a,b) => a && a[b], obj);
			if (ret === undefined) return "-";
			return pretty ? pretty(ret) : ret;
		};

		section = this.add_section("stats");
		columns = [
			"name",
			"req count",
			"size disk",
			"size mem",
			"cid total",
		];
		const stats_table = section.add_widget(Table, columns, Table.Flag_PackRight);
		var providers = new Fetcher().resource("stats").json();
		for (var provider of (await providers)["providers"])
		{
			var stats = await new Fetcher().resource("stats", provider).json();
			var size_stat = (stats.store || stats.cache);
			var values = [
				"",
				safe_lookup(stats, "requests.count"),
				safe_lookup(size_stat, "size.disk", Friendly.kib),
				safe_lookup(size_stat, "size.memory", Friendly.kib),
				safe_lookup(stats, "cid.size.total"),
			];
			row = stats_table.add_row(...values);
			row.get_cell(0).tag().text(provider).on_click((x) => this.view_stat(x), provider);
		}

		// version
		var ver_tag = this.tag().id("version");
		var version = new Fetcher().resource("health", "version");
		version.param("detailed", "true");
		version.text().then((data) => ver_tag.text(data));

		this._project_table = project_table;
		this._cache_table = cache_table;
	}

	view_stat(provider)
	{
		window.location = "?page=stat&provider=" + provider;
	}

	view_project(project_id)
	{
		window.location = "?page=project&project=" + project_id;
	}

	drop_project(project_id)
	{
		const drop = async () => {
			await new Fetcher().resource("prj", project_id).delete();
			this.reload();
		};

		new Modal()
			.title("Confirmation")
			.message(`Drop project '${project_id}'?`)
			.option("Yes", () => drop())
			.option("No");
	}

	view_zcache(namespace)
	{
		window.location = "?page=zcache&namespace=" + namespace;
	}

	drop_zcache(namespace)
	{
		const drop = async () => {
			await new Fetcher().resource("z$", namespace).delete();
			this.reload();
		};

		new Modal()
			.title("Confirmation")
			.message(`Drop zcache '${namespace}'?`)
			.option("Yes", () => drop())
			.option("No");
	}

	async drop_all_projects()
	{
		for (const row of this._project_table)
		{
			const project_id = row.attr("zs_name");
			await new Fetcher().resource("prj", project_id).delete();
		}
		this.reload();
	}

	async drop_all_zcache()
	{
		for (const row of this._cache_table)
		{
			const namespace = row.attr("zs_name");
			await new Fetcher().resource("z$", namespace).delete();
		}
		this.reload();
	}

	drop_all(what)
	{
		const drop = async () => {
			if (what == "projects") return this.drop_all_projects();
			if (what == "z$")		return this.drop_all_zcache();
		};

		new Modal()
			.title("Confirmation")
			.message(`Drop every item from '${what}'?`)
			.option("Yes", () => drop())
			.option("No");
	}
}