querysub 0.632.0 → 0.633.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "querysub",
3
- "version": "0.632.0",
3
+ "version": "0.633.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -79,7 +79,7 @@
79
79
  "node-forge": "https://github.com/sliftist/forge#e618181b469b07bdc70b968b0391beb8ef5fecd6",
80
80
  "pako": "^2.1.0",
81
81
  "peggy": "^5.0.6",
82
- "sliftutils": "^1.7.102",
82
+ "sliftutils": "^1.7.103",
83
83
  "socket-function": "^1.2.30",
84
84
  "terser": "^5.31.0",
85
85
  "typenode": "^6.6.1",
@@ -10,6 +10,7 @@ import { MachineDetailPage } from "./components/MachineDetailPage";
10
10
  import { Anchor } from "../library-components/ATag";
11
11
  import { DeployPage } from "./components/DeployPage";
12
12
  import { StoragePage } from "./components/storage/StoragePage";
13
+ import { StorageLogsPage } from "./components/storage/StorageLogsPage";
13
14
  import { RoutingTablePage } from "../diagnostics/misc-pages/RoutingTablePage";
14
15
 
15
16
  export class MachinesPage extends qreact.Component {
@@ -23,6 +24,7 @@ export class MachinesPage extends qreact.Component {
23
24
  { key: "services", label: "Services", otherKeys: ["service-detail"] },
24
25
  { key: "deploy", label: "Deploy", otherKeys: [] },
25
26
  { key: "storage", label: "Storage", otherKeys: [] },
27
+ { key: "storage-logs", label: "Storage Logs", otherKeys: [] },
26
28
  { key: "routing", label: "PathValues / Functions Routing", otherKeys: [] },
27
29
  ].map(tab => {
28
30
  let isActive = currentViewParam.value === tab.key || tab.otherKeys.includes(currentViewParam.value);
@@ -52,6 +54,7 @@ export class MachinesPage extends qreact.Component {
52
54
  {currentViewParam.value === "machine-detail" && <MachineDetailPage />}
53
55
  {currentViewParam.value === "deploy" && <DeployPage />}
54
56
  {currentViewParam.value === "storage" && <StoragePage />}
57
+ {currentViewParam.value === "storage-logs" && <StorageLogsPage />}
55
58
  {currentViewParam.value === "routing" && <RoutingTablePage />}
56
59
  </div>
57
60
  </div>;
@@ -0,0 +1,251 @@
1
+ import preact from "preact";
2
+ import { SocketFunction } from "socket-function/SocketFunction";
3
+ import { qreact } from "../../../4-dom/qreact";
4
+ import { css } from "typesafecss";
5
+ import { Querysub } from "../../../4-querysub/Querysub";
6
+ import { isDefined } from "../../../misc";
7
+ import { formatNumber, formatTime, formatDateTime } from "socket-function/src/formatting/format";
8
+ import { timeInSecond } from "socket-function/src/misc";
9
+ import type { LogFileInfo } from "sliftutils/storage/StreamingLogs";
10
+ import { MachineServiceController } from "../../machineSchema";
11
+ import { getStorageServers } from "./storageServers";
12
+ import { Table } from "../../../5-diagnostics/Table";
13
+ import { URLParam } from "../../../library-components/URLParam";
14
+ import { InputLabelURL } from "../../../library-components/InputLabel";
15
+ import { Button } from "../../../library-components/Button";
16
+ import { TimeRangeSelector, getTimeRange } from "../../../diagnostics/logs/TimeRangeSelector";
17
+ import { StorageLogsSynced } from "./storageLogsController";
18
+ import { searchStorageLogs, invalidateStorageLogSearchCache } from "./storageLogSearch";
19
+
20
+ module.hotreload = true;
21
+
22
+ export const storageLogSearchParam = new URLParam("storageLogSearch", "");
23
+
24
+ // Fixed columns first so every search reads the same way; the entries' own fields follow in the order they appear.
25
+ const LEADING_COLUMNS = ["server", "file", "time", "kind"];
26
+ const ERROR_COLOR = { h: 0, s: 60, l: 40 };
27
+ const AGE_COLOR = { h: 0, s: 0, l: 40 };
28
+ const AGE_FONT_SIZE = 13;
29
+
30
+ type ServerLogListing = {
31
+ url: string;
32
+ loading?: boolean;
33
+ error?: string;
34
+ files?: LogFileInfo[];
35
+ fetchTime?: number;
36
+ };
37
+
38
+ type SearchState = {
39
+ searching: boolean;
40
+ doneFiles: number;
41
+ totalFiles: number;
42
+ currentFile: string;
43
+ /** How long the last search took */
44
+ durationTime: number;
45
+ /** The oldest fetch time among the results being displayed */
46
+ resultsFetchTime: number;
47
+ rows: Record<string, unknown>[];
48
+ columns: string[];
49
+ error: string;
50
+ };
51
+
52
+ /** Whether a log file could hold entries in the range - a live file (no endTime) runs to now. */
53
+ function fileOverlapsRange(file: LogFileInfo, startTime: number, endTime: number): boolean {
54
+ let fileEnd = file.endTime ?? Date.now();
55
+ return file.startTime <= endTime && fileEnd >= startTime;
56
+ }
57
+
58
+ export class StorageLogsPage extends qreact.Component {
59
+ state: SearchState = {
60
+ searching: false,
61
+ doneFiles: 0,
62
+ totalFiles: 0,
63
+ currentFile: "",
64
+ durationTime: 0,
65
+ resultsFetchTime: 0,
66
+ rows: [],
67
+ columns: [],
68
+ error: "",
69
+ };
70
+
71
+ private getListings(): ServerLogListing[] {
72
+ let machineController = MachineServiceController(SocketFunction.browserNodeId());
73
+ let serviceList = machineController.getServiceList();
74
+ let configs = (serviceList || []).map(serviceId => machineController.getServiceConfig(serviceId)).filter(isDefined);
75
+ return getStorageServers(configs).map(server => {
76
+ // Every other server still has something worth showing, so one that cannot be read becomes its own error line instead of an empty page
77
+ try {
78
+ let listing = StorageLogsSynced(SocketFunction.browserNodeId()).getServerLogFiles(server.url);
79
+ if (!listing) return { url: server.url, loading: true };
80
+ return { url: server.url, files: listing.files, fetchTime: listing.fetchTime };
81
+ } catch (e: any) {
82
+ return { url: server.url, error: e.stack ?? String(e) };
83
+ }
84
+ });
85
+ }
86
+
87
+ private getSelectedFiles(listings: ServerLogListing[]): { url: string; name: string; size: number }[] {
88
+ let { startTime, endTime } = getTimeRange();
89
+ let selected: { url: string; name: string; size: number }[] = [];
90
+ for (let listing of listings) {
91
+ for (let file of listing.files || []) {
92
+ if (!fileOverlapsRange(file, startTime, endTime)) continue;
93
+ selected.push({ url: listing.url, name: file.name, size: file.size });
94
+ }
95
+ }
96
+ return selected;
97
+ }
98
+
99
+ private search() {
100
+ if (this.state.searching) return;
101
+ // State and synced reads are captured HERE, in the tracked part, before the async work
102
+ let listings = this.getListings();
103
+ let files = this.getSelectedFiles(listings).map(file => ({ url: file.url, name: file.name }));
104
+ let query = storageLogSearchParam.value;
105
+ let { startTime, endTime } = getTimeRange();
106
+ this.state.searching = true;
107
+ this.state.error = "";
108
+ this.state.doneFiles = 0;
109
+ this.state.totalFiles = files.length;
110
+ this.state.currentFile = "";
111
+ Querysub.onCommitFinished(async () => {
112
+ try {
113
+ let result = await searchStorageLogs({
114
+ files,
115
+ query,
116
+ onProgress: progress => {
117
+ Querysub.commit(() => {
118
+ this.state.doneFiles = progress.doneFiles;
119
+ this.state.totalFiles = progress.totalFiles;
120
+ this.state.currentFile = progress.currentFile;
121
+ });
122
+ },
123
+ });
124
+ let rows: Record<string, unknown>[] = [];
125
+ let columns: string[] = [...LEADING_COLUMNS];
126
+ for (let file of result.files) {
127
+ for (let entry of file.entries) {
128
+ let fields = entry && typeof entry === "object" && entry as Record<string, unknown> || { value: String(entry) };
129
+ // The file selection is only as granular as whole files, so the entries themselves still need the range applied
130
+ let time = fields.time;
131
+ if (typeof time === "number" && (time < startTime || time > endTime)) continue;
132
+ for (let key of Object.keys(fields)) {
133
+ if (!columns.includes(key)) {
134
+ columns.push(key);
135
+ }
136
+ }
137
+ rows.push({ server: file.url, file: file.name, ...fields });
138
+ }
139
+ }
140
+ rows.sort((a, b) => (typeof a.time === "number" && a.time || 0) - (typeof b.time === "number" && b.time || 0));
141
+ Querysub.commit(() => {
142
+ this.state.rows = rows;
143
+ this.state.columns = columns;
144
+ this.state.durationTime = result.durationTime;
145
+ this.state.resultsFetchTime = result.oldestFetchTime;
146
+ });
147
+ } catch (e: any) {
148
+ Querysub.commit(() => {
149
+ this.state.error = e.stack ?? String(e);
150
+ });
151
+ } finally {
152
+ Querysub.commit(() => {
153
+ this.state.searching = false;
154
+ });
155
+ }
156
+ });
157
+ }
158
+
159
+ render() {
160
+ let listings = this.getListings();
161
+ let selectedFiles = this.getSelectedFiles(listings);
162
+ let selectedBytes = selectedFiles.reduce((sum, file) => sum + file.size, 0);
163
+ let loadedListings = listings.filter(listing => listing.fetchTime);
164
+ let oldestListingTime = loadedListings.length && Math.min(...loadedListings.map(listing => listing.fetchTime || 0)) || 0;
165
+ let now = Querysub.nowDelayed(timeInSecond);
166
+
167
+ let columnsConfig: { [column: string]: { title?: string; formatter?: (value: unknown) => preact.ComponentChild } } = {};
168
+ for (let column of this.state.columns) {
169
+ if (column === "time") {
170
+ columnsConfig[column] = {
171
+ title: "Time",
172
+ formatter: value => typeof value === "number" && <span className={css.whiteSpace("nowrap")}>{formatDateTime(value)}</span> || value as preact.ComponentChild,
173
+ };
174
+ } else {
175
+ columnsConfig[column] = {};
176
+ }
177
+ }
178
+
179
+ return <div className={css.vbox(16)}>
180
+ <div className={css.hbox(12).alignItems("center")}>
181
+ <h2 className={css.flexGrow(1)}>Storage logs</h2>
182
+ <button
183
+ className={css.pad2(12, 8).button.bord2(0, 0, 20).hsl(0, 0, 100)}
184
+ title="Drops both the cached log file listings and the cached search results"
185
+ onClick={() => {
186
+ StorageLogsSynced(SocketFunction.browserNodeId()).getServerLogFiles.resetAll();
187
+ invalidateStorageLogSearchCache();
188
+ this.state.rows = [];
189
+ this.state.columns = [];
190
+ this.state.resultsFetchTime = 0;
191
+ this.state.durationTime = 0;
192
+ }}
193
+ >
194
+ Invalidate
195
+ </button>
196
+ </div>
197
+ <TimeRangeSelector />
198
+ <div className={css.hbox(10).fillWidth}>
199
+ <InputLabelURL
200
+ flavor="large"
201
+ fillWidth
202
+ label="Search (a | b & c - or of ands, empty downloads everything in range)"
203
+ url={storageLogSearchParam}
204
+ onKeyDown={e => {
205
+ if (e.key === "Enter") {
206
+ storageLogSearchParam.value = e.currentTarget.value;
207
+ this.search();
208
+ }
209
+ }}
210
+ />
211
+ <Button
212
+ flavor="large"
213
+ hue={210}
214
+ onClick={() => this.search()}
215
+ className={this.state.searching && css.opacity(0.5) || undefined}
216
+ >
217
+ {this.state.searching && `Searching ${this.state.doneFiles}/${this.state.totalFiles}...` || "Search"}
218
+ </Button>
219
+ </div>
220
+ <div className={css.vbox(4)}>
221
+ {listings.map(listing => {
222
+ let files = listing.files || [];
223
+ let totalBytes = files.reduce((sum, file) => sum + file.size, 0);
224
+ return <div key={listing.url} className={css.hbox(8).wrap.alignItems("center")}>
225
+ <b>{listing.url}</b>
226
+ {listing.loading && <span>Loading log files...</span>}
227
+ {listing.error && <span className={css.colorhsl(ERROR_COLOR.h, ERROR_COLOR.s, ERROR_COLOR.l).ellipsis} title={listing.error}>{listing.error}</span>}
228
+ {listing.files && <span>{formatNumber(files.length)} log files, {formatNumber(totalBytes)}B</span>}
229
+ </div>;
230
+ })}
231
+ <div>
232
+ Selected range covers <b>{formatNumber(selectedFiles.length)}</b> files, <b>{formatNumber(selectedBytes)}B</b> (compressed) to search
233
+ </div>
234
+ <div className={css.hbox(12).wrap.fontSize(AGE_FONT_SIZE).colorhsl(AGE_COLOR.h, AGE_COLOR.s, AGE_COLOR.l)}>
235
+ {!!oldestListingTime && <span>File listing from {formatTime(now - oldestListingTime)} ago</span>}
236
+ {!!this.state.resultsFetchTime && <span>Results from {formatTime(now - this.state.resultsFetchTime)} ago</span>}
237
+ {!!this.state.durationTime && <span>Search took {formatTime(this.state.durationTime)}</span>}
238
+ {this.state.searching && !!this.state.currentFile && <span>Searching {this.state.currentFile}</span>}
239
+ </div>
240
+ </div>
241
+ {this.state.error && <div className={css.colorhsl(ERROR_COLOR.h, ERROR_COLOR.s, ERROR_COLOR.l).whiteSpace("pre-wrap")}>{this.state.error}</div>}
242
+ {!!this.state.rows.length && <div>
243
+ <Table
244
+ rows={this.state.rows}
245
+ columns={columnsConfig}
246
+ />
247
+ </div>}
248
+ {!this.state.rows.length && !this.state.searching && !!this.state.durationTime && <div>No matching log entries.</div>}
249
+ </div>;
250
+ }
251
+ }
@@ -0,0 +1,90 @@
1
+ import { SocketFunction } from "socket-function/SocketFunction";
2
+ import { runInParallel } from "socket-function/src/batching";
3
+ import { StorageLogsController } from "./storageLogsController";
4
+
5
+ module.hotreload = true;
6
+
7
+ // searchServerLogs only ANDs its search strings, so the `|` level of a query has to be decomposed on our side: one server-side search per or-clause, unioned. The union check uses the entry's own JSON (the same text the server searched), so it never collapses genuinely duplicate log lines the way a dedupe-by-content would.
8
+
9
+ const PARALLEL_FILE_SEARCHES = 4;
10
+
11
+ /** A query's or-clauses, each the list of terms that must all appear - the same `|` / `&` semantics matchFilter (misc.ts) applies locally, minus its lowercasing, as the server search is case-sensitive. */
12
+ export function parseSearchClauses(query: string): string[][] {
13
+ return query.split("|")
14
+ .map(clause => clause.split("&").map(x => x.trim()).filter(x => x))
15
+ .filter(clause => clause.length > 0);
16
+ }
17
+
18
+ export type SearchedLogFile = {
19
+ url: string;
20
+ name: string;
21
+ entries: unknown[];
22
+ /** When these entries were actually fetched - older than the search when they came from the cache. */
23
+ fetchTime: number;
24
+ };
25
+
26
+ const searchCache = new Map<string, { entries: unknown[]; fetchTime: number }>();
27
+ export function invalidateStorageLogSearchCache(): void {
28
+ searchCache.clear();
29
+ }
30
+
31
+ async function searchFile(url: string, name: string, query: string): Promise<{ entries: unknown[]; fetchTime: number }> {
32
+ let cacheKey = `${url}|${name}|${query}`;
33
+ let cached = searchCache.get(cacheKey);
34
+ if (cached) return cached;
35
+ let controller = StorageLogsController.nodes[SocketFunction.browserNodeId()];
36
+ let clauses = parseSearchClauses(query);
37
+ let entries: unknown[];
38
+ if (clauses.length <= 1) {
39
+ entries = await controller.searchLogFile(url, name, clauses[0] || []);
40
+ } else {
41
+ // Later clauses drop entries an earlier clause already returned, which unions the or-clauses without collapsing real duplicates
42
+ entries = [];
43
+ for (let [index, clause] of clauses.entries()) {
44
+ let clauseEntries = await controller.searchLogFile(url, name, clause);
45
+ for (let entry of clauseEntries) {
46
+ let line = JSON.stringify(entry);
47
+ if (clauses.slice(0, index).some(prev => prev.every(term => line.includes(term)))) continue;
48
+ entries.push(entry);
49
+ }
50
+ }
51
+ }
52
+ let result = { entries, fetchTime: Date.now() };
53
+ searchCache.set(cacheKey, result);
54
+ return result;
55
+ }
56
+
57
+ export type StorageLogSearchProgress = {
58
+ doneFiles: number;
59
+ totalFiles: number;
60
+ currentFile: string;
61
+ };
62
+
63
+ /** Searches the query across the given files (per-file, so progress is per-file and each file's result caches on its own). */
64
+ export async function searchStorageLogs(config: {
65
+ files: { url: string; name: string }[];
66
+ query: string;
67
+ onProgress: (progress: StorageLogSearchProgress) => void;
68
+ }): Promise<{
69
+ files: SearchedLogFile[];
70
+ durationTime: number;
71
+ /** The oldest fetch time among the results used, so the UI can say how stale the display is. */
72
+ oldestFetchTime: number;
73
+ }> {
74
+ let startTime = Date.now();
75
+ let doneFiles = 0;
76
+ let results: SearchedLogFile[] = [];
77
+ let runFile = runInParallel({ parallelCount: PARALLEL_FILE_SEARCHES }, async (file: { url: string; name: string }) => {
78
+ config.onProgress({ doneFiles, totalFiles: config.files.length, currentFile: file.name });
79
+ let result = await searchFile(file.url, file.name, config.query);
80
+ results.push({ url: file.url, name: file.name, entries: result.entries, fetchTime: result.fetchTime });
81
+ doneFiles++;
82
+ config.onProgress({ doneFiles, totalFiles: config.files.length, currentFile: file.name });
83
+ });
84
+ await Promise.all(config.files.map(runFile));
85
+ return {
86
+ files: results,
87
+ durationTime: Date.now() - startTime,
88
+ oldestFetchTime: results.length && Math.min(...results.map(x => x.fetchTime)) || Date.now(),
89
+ };
90
+ }
@@ -0,0 +1,54 @@
1
+ import { SocketFunction } from "socket-function/SocketFunction";
2
+ import { listServerLogFiles, getServerLogs, searchServerLogs } from "sliftutils/storage/remoteStorage/createArchives";
3
+ import type { LogFileInfo } from "sliftutils/storage/StreamingLogs";
4
+ import { getSyncedController } from "../../../library-components/SyncedController";
5
+ import { assertIsManagementUser } from "../../../diagnostics/managementPages";
6
+ import { STORAGE_ACCOUNT } from "../../../-a-archives/archives2";
7
+
8
+ // The registered controller and its synced wrapper have to keep a stable identity, so this file is a full-refresh boundary rather than a hot-reload one (matching SyncedController itself).
9
+ module.hotreload = false;
10
+
11
+ export type ServerLogFiles = {
12
+ files: LogFileInfo[];
13
+ /** When this listing was fetched, so the UI can say how stale a cached listing is. */
14
+ fetchTime: number;
15
+ };
16
+
17
+ /** Every storage call is proxied through here: they authenticate with this machine's identity CA, which the browser has no access to. */
18
+ class StorageLogsControllerBase {
19
+ /** One call per server, so each caches and refreshes on its own. */
20
+ public async getServerLogFiles(url: string): Promise<ServerLogFiles> {
21
+ return {
22
+ files: await listServerLogFiles({ url, account: STORAGE_ACCOUNT }),
23
+ fetchTime: Date.now(),
24
+ };
25
+ }
26
+ /** One file per call, so the client can report per-file progress. Empty searches downloads and decodes the whole file (searchServerLogs requires at least one search string). */
27
+ public async searchLogFile(url: string, name: string, searches: string[]): Promise<unknown[]> {
28
+ if (!searches.length) {
29
+ let results = await getServerLogs({ url, account: STORAGE_ACCOUNT, names: [name] });
30
+ return results[0]?.entries || [];
31
+ }
32
+ let results = await searchServerLogs({ url, account: STORAGE_ACCOUNT, names: [name], searches });
33
+ return results[0]?.entries || [];
34
+ }
35
+ }
36
+
37
+ export const StorageLogsController = SocketFunction.register(
38
+ "StorageLogsController-8b3f52c1-6a4e-4c0d-9d71-2f8e4a5b6c93",
39
+ new StorageLogsControllerBase(),
40
+ () => ({
41
+ getServerLogFiles: {},
42
+ searchLogFile: {},
43
+ }),
44
+ () => ({
45
+ hooks: [assertIsManagementUser],
46
+ }),
47
+ {
48
+ }
49
+ );
50
+
51
+ export const StorageLogsSynced = getSyncedController(StorageLogsController, {
52
+ reads: {},
53
+ writes: {},
54
+ });
@@ -1,6 +1,6 @@
1
1
  import { URLParam } from "../library-components/URLParam";
2
2
 
3
- export type ViewType = "services" | "machines" | "service-detail" | "machine-detail" | "deploy" | "storage" | "routing";
3
+ export type ViewType = "services" | "machines" | "service-detail" | "machine-detail" | "deploy" | "storage" | "storage-logs" | "routing";
4
4
 
5
5
  export const currentViewParam = new URLParam<ViewType>("machineview", "machines");
6
6
  export const selectedServiceIdParam = new URLParam("serviceId", "");