Repository navigation
Expand file tree
/
Copy pathquery.js
More file actions
173 lines (159 loc) · 6.34 KB
/
Copy pathquery.js
File metadata and controls
173 lines (159 loc) · 6.34 KB
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
// ============================================================
// Xerj Console — query gateway
//
// All dashboard data flows through this one entry point. It picks
// the active backend (default: xerj), tries a live call, and
// falls back to the in-memory mock when the live call fails or
// returns null (no adapter for this dashboard yet).
//
// This file is the only place in the playground that touches HTTP.
// Dashboards stay backend-agnostic — they receive a shaped `data`
// object and never see fetch().
// ============================================================
import { mock as mockData } from './mock.js';
import {
activeBackend,
activeBackendId,
backendBaseUrl,
BACKENDS,
} from './backends/index.js';
/**
* Views that show the user's OWN DOCUMENTS and therefore never fall back to
* the in-memory mock, whatever backend is selected and however the live call
* fails. A fabricated hit on Discover, or an invented dataset on the Corpus
* home, is indistinguishable from a real one to the person reading it — so
* these get the live adapter or an explicit error, nothing else.
*/
export const NEVER_MOCK = new Set(['corpus', 'reader', 'search-discover']);
/** Views that fetch for themselves (ux/reader-view.js); `query()` has nothing
* to load for them and must not invent a payload. */
const SELF_FETCHING = new Set(['reader']);
/** Last-known status for the nav pill. Updated on every query. */
let _lastSourceKind = 'pending';
let _lastSourceLabel = 'STARTING UP';
/** Function form (preferred). Returns the live status string. */
export function dataSourceStatus() {
return _lastSourceLabel;
}
export function dataSourceKind() {
return _lastSourceKind;
}
/**
* Fetch shaped data for a dashboard.
*
* ctx.dashId: dashboard id from the registry
* ctx.range: '1H' | '24H' | '7D' | '30D' | '90D' | 'CUSTOM'
* ctx.cluster: cluster id (defaults to active cluster)
* ctx.filters: { [field]: value | [values] }
* ctx.search: per-dashboard freeform state (q, type, index, …)
* ctx.signal: AbortSignal — backends that honour it cancel in flight
*
* Returns `{ data, meta }` where meta carries timing, the active
* backend, and `fetchedAt` for the nav status line.
*/
export async function query(ctx = {}) {
const {
dashId,
range = '24H',
customRange = null,
cluster = '',
filters = {},
search = {},
signal,
} = typeof ctx === 'string' ? { dashId: ctx } : ctx;
const t0 = perfNow();
const neverMock = NEVER_MOCK.has(dashId);
// A NEVER_MOCK view always talks to the engine, even when the operator has
// picked the demo "mock" backend for the telemetry dashboards.
const backendId = neverMock ? 'xerj' : activeBackendId();
const backend = neverMock ? BACKENDS.xerj : activeBackend();
const baseUrl = backendBaseUrl(backendId);
// Try the live backend. A return of `null` means the backend
// recognises the dashId but has no live adapter yet — we fall
// through to mock without flipping the status pill to red.
let data = null;
let liveError = null;
if (SELF_FETCHING.has(dashId)) {
data = { _selfFetching: true };
} else if (backend && typeof backend.search === 'function') {
try {
data = await backend.search(baseUrl, dashId, { range, customRange, cluster, filters, search }, signal);
} catch (e) {
liveError = String(e);
}
}
if (data == null && neverMock) {
data = { error: liveError || 'no live data for this view', status: 'error' };
}
if (data == null) {
// Fallback to mock.
data = mockData(dashId, range, { cluster, filters, customRange });
_lastSourceKind = backendId === 'mock' ? 'mock' : 'fallback';
_lastSourceLabel = backendId === 'mock'
? 'MOCK DATA'
: `${BACKENDS[backendId]?.meta?.label || backendId}: MOCK FALLBACK`;
} else if (data.error) {
// The backend ran but returned an error envelope. Surface it to
// the dashboard but keep the live source label so the user can
// see "the backend is up but this query failed."
_lastSourceKind = 'live-error';
_lastSourceLabel = `${BACKENDS[backendId]?.meta?.label || backendId}: ${String(data.error).slice(0, 80)}`;
} else if (data._selfFetching) {
// The view fetches for itself and reports its own status once it has
// (ux/reader-view.js#onStatus). Nothing has been loaded here, so nothing
// is claimed.
_lastSourceKind = 'pending';
_lastSourceLabel = 'LOADING…';
} else if (data._sample) {
// The shape carries fabricated/sample panels the engine can't produce
// (e.g. system host-metrics — no metrics agent). Never claim LIVE for it;
// label it SAMPLE so a demo panel is never mistaken for measured data.
_lastSourceKind = 'sample';
_lastSourceLabel = `${BACKENDS[backendId]?.meta?.label || backendId}: SAMPLE DATA`;
} else {
_lastSourceKind = 'live';
// The bundled console is served by the engine it shows; naming this
// page's own origin adds nothing (and pushed the pill off a 1440px nav).
const own = typeof window !== 'undefined' && window.location && baseUrl === window.location.origin;
_lastSourceLabel = `LIVE · ${BACKENDS[backendId]?.meta?.label || backendId}${baseUrl && !own ? ' · ' + baseUrl : ''}`;
}
const t1 = perfNow();
return {
data,
meta: {
dashId,
range,
customRange,
cluster,
filters,
search,
fetchedAt: new Date().toISOString(),
durationMs: Math.round((t1 - t0) * 10) / 10,
backend: backendId,
backendLabel: BACKENDS[backendId]?.meta?.label || backendId,
baseUrl,
sourceKind: _lastSourceKind,
sourceLabel: _lastSourceLabel,
liveError,
},
};
}
/** Probe the active backend. Used by the nav to flip the status
* pill from amber → green when the engine becomes reachable. */
export async function probeActiveBackend(signal) {
const backend = activeBackend();
const backendId = activeBackendId();
const baseUrl = backendBaseUrl(backendId);
if (!backend || typeof backend.probe !== 'function') return false;
try {
return await backend.probe(baseUrl, signal);
} catch (_e) {
return false;
}
}
function perfNow() {
return typeof performance !== 'undefined' ? performance.now() : Date.now();
}
/** Initial value (overwritten at runtime). Kept as a default export
* for any old `import { DATA_SOURCE_KIND }` call sites. */
export const DATA_SOURCE_KIND = 'pending';