aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/frontend/html/pages/oplog.js
blob: bef5bacce664128ab1adb32125a25ae6f59ea8d6 (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
// 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 { Table, Toolbar, ProgressBar } from "../util/widgets.js"
import { create_indexer } from "../indexer/indexer.js"

////////////////////////////////////////////////////////////////////////////////
export class Page extends ZenPage
{
	constructor(...args)
	{
		super(...args);

		this._index_start = Number(this.get_param("start", 0)) || 0;
		this._index_count = Number(this.get_param("count", 50)) || 0;
	}

	async main()
	{
		const project = this.get_param("project");
		const oplog = this.get_param("oplog");

		var oplog_info = new Fetcher()
			.resource("prj", project, "oplog", oplog)
			.json();

		this._indexer = this._load_indexer(project, oplog);

		this.set_title("oplog - " + oplog);

		var section = this.add_section(project + " - " + oplog);

		oplog_info = await oplog_info;
		this._index_max = oplog_info["opcount"];
		this._build_nav(section, oplog_info);

		this._entry_table = section.add_widget(Table, ["key"]);
		await this._build_table(this._index_start);
	}

	async _load_indexer(project, oplog)
	{
		const progress_bar = this.add_widget(ProgressBar);
		progress_bar.set_progress("indexing");
		var indexer = create_indexer(project, oplog, (...args) => {
			progress_bar.set_progress(...args);
		});
		indexer = await indexer;
		progress_bar.destroy();
		return indexer;
	}

	_build_nav(section, oplog_info)
	{
		const nav = section.add_widget(Toolbar);
		const left = nav.left();
		left.add("|<")   .on_click(() => this._on_next_prev(-10e10));
		left.add("<<").on_click(() => this._on_next_prev(-10));
		left.add("prev")    .on_click(() => this._on_next_prev( -1));
		left.add("next")    .on_click(() => this._on_next_prev(  1));
		left.add(">>").on_click(() => this._on_next_prev( 10));
		left.add(">|")   .on_click(() => this._on_next_prev( 10e10));

		left.sep();
		for (var count of [10, 25, 50, 100])
		{
			var handler = (n) => this._on_change_count(n);
			left.add(count).on_click(handler, count);
		}

		left.sep();
		left.add("tree").link("", {
			"page" : "tree",
			"project" : this.get_param("project"),
			"oplog" : this.get_param("oplog"),
		});

		const right = nav.right();
		right.add(Friendly.sep(oplog_info["opcount"]));
		right.add("(" + Friendly.kib(oplog_info["totalsize"]) + ")");
		right.sep();

		var search_input = right.add("search:", "label").tag("input")
		search_input.on("change", (x) => this._search(x.inner().value), search_input);
	}

	async _build_table(index)
	{
		this._index_count = Math.max(this._index_count, 1);
		index = Math.min(index, this._index_max - this._index_count);
		index = Math.max(index, 0);

		const project = this.get_param("project");
		const oplog = this.get_param("oplog");

		var entries = new Fetcher()
			.resource("prj", project, "oplog", oplog, "entries")
			.param("start", index)
			.param("count", this.set_param("count", this._index_count))
			.json();

		entries = (await entries)["entries"];
		if (entries == undefined)
			return;

		if (entries.length == 0)
			return;

		this._entry_table.clear(index);
		for (const entry of entries)
		{
			var row = this._entry_table.add_row(entry["key"]);

			row.get_cell(0).link("", {
				"page" : "entry",
				"project" : project,
				"oplog" : oplog,
				"opkey" : entry["key"],
			});
		}

		this.set_param("start", index);
		this._index_start = index;
	}

	_on_change_count(value)
	{
		this._index_count = parseInt(value);
		this._build_table(this._index_start);
	}

	_on_next_prev(direction)
	{
		var index = this._index_start + (this._index_count * direction);
		index = Math.max(0, index);
		this._build_table(index);
	}

	async _search(needle)
	{
		if (needle.length == 0)
		{
			this._build_table(this._index_start);
			return;
		}
		needle = needle.trim();

		this._entry_table.clear(this._index_start);

		const project = this.get_param("project");
		const oplog = this.get_param("oplog");

		const indexer = await this._indexer;

		var added = 0;
		const truncate_at = this.get_param("searchmax") || 250;
		for (var name of indexer.search(needle))
		{
			var row = this._entry_table.add_row(name);

			row.get_cell(0).link("", {
				"page" : "entry",
				"project" : project,
				"oplog" : oplog,
				"opkey" : name,
			});

			if (++added >= truncate_at)
			{
				this._entry_table.add_row("...truncated");
				break;
			}
		}
	}
}