-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
621 lines (588 loc) Β· 22.3 KB
/
Copy pathindex.html
File metadata and controls
621 lines (588 loc) Β· 22.3 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>π° UsagiDB Demo</title>
<style>
:root {
color-scheme: light dark;
}
body {
font-family: system-ui, sans-serif;
padding: 20px;
max-width: 1200px;
margin: 0 auto;
}
h1 {
margin-bottom: 4px;
}
h2 {
font-size: 16px;
margin-bottom: 6px;
}
.tag {
opacity: 0.6;
font-size: 13px;
margin-top: 0;
}
.panels {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 14px;
margin-top: 16px;
}
.panel {
border: 1px solid #ccc4;
border-radius: 8px;
padding: 12px;
background: #f7f7f900;
}
button {
margin: 2px 3px 2px 0;
padding: 5px 9px;
cursor: pointer;
font-size: 12px;
}
pre {
background: #11151c;
color: #d6d8de;
padding: 10px;
border-radius: 5px;
white-space: pre-wrap;
font-family: ui-monospace, monospace;
font-size: 11px;
min-height: 240px;
margin-top: 8px;
line-height: 1.45;
max-height: 320px;
overflow: auto;
}
.pill {
display: inline-block;
padding: 1px 6px;
background: #3b82f6;
color: white;
border-radius: 8px;
font-size: 10px;
margin-left: 4px;
}
.pill.off {
background: #6b7280;
}
input[type="text"] {
width: 100px;
padding: 4px 6px;
font-size: 12px;
}
</style>
</head>
<body>
<h1>π° UsagiDB</h1>
<p class="tag">
Multi-modal: <strong>ts</strong>=HTAP TS-DB,
<strong>stream</strong>=jellyfish_moon, <strong>log</strong>=Kafka-like.
| <a href="./web/">π realtime canvas chart β</a>
</p>
<div class="panels">
<div class="panel">
<h2>π¦ ts mode</h2>
<button onclick="ts_create()">Create</button>
<button onclick="ts_insert()">Insert</button>
<button onclick="ts_scan()">Scan</button>
<button onclick="ts_stats()">Stats</button>
<button onclick="ts_serialize()">Serialize</button>
<button onclick="clear_ts()">Clear</button>
<pre id="ts_out"></pre>
</div>
<div class="panel">
<h2>π stream mode</h2>
<button onclick="stream_setup()">Downsample 1s OHLC</button>
<button onclick="stream_setup_keep_last(10)">KeepLastN(10)</button>
<button onclick="stream_setup_dead_band(0.5)">DeadBand(0.5)</button>
<button onclick="stream_push_random()">Push 30</button>
<button onclick="stream_show()">Query</button>
<button onclick="clear_stream()">Clear</button>
<pre id="stream_out"></pre>
</div>
<div class="panel" style="grid-column: 1 / span 3;">
<h2>π sync transport (ts mode)
<span id="ws_pill" class="pill off">ws: off</span>
<span id="bgsync_pill" class="pill off">bg-sync: off</span>
<span id="p2p_pill" class="pill off">p2p: off</span>
</h2>
<input type="text" id="ws_url" value="ws://localhost:9000/sync" style="width:240px"/>
<button onclick="ws_connect()">Connect</button>
<button onclick="ws_disconnect()">Disconnect</button>
<button onclick="ws_pull()">Pull</button>
<button onclick="ws_push()">Push</button>
<button onclick="ws_push_range()">Push (last 10s shape)</button>
<button onclick="enable_bg_sync()">β° Background Sync</button>
<button onclick="toggle_p2p()">β P2P (intra-tab)</button>
<button onclick="clear_sync()">Clear</button>
<pre id="sync_out" style="min-height:120px"></pre>
</div>
<div class="panel">
<h2>π log mode <span id="bcast_pill" class="pill off">bcast: off</span><span id="opfs_pill" class="pill off">opfs: off</span></h2>
<button onclick="log_setup()">Subscribe</button>
<button onclick="log_produce_one()">Produce</button>
<button onclick="log_poll_some()">Poll(5)</button>
<button onclick="log_topics()">Topics</button>
<br/>
<button onclick="toggle_bcast()">β BroadcastChannel</button>
<button onclick="toggle_opfs()">πΎ OPFS auto-save</button>
<button onclick="opfs_load()">π Load OPFS</button>
<button onclick="clear_log()">Clear</button>
<pre id="log_out"></pre>
</div>
</div>
<script type="module">
import {
create,
to_int64,
value_float,
value_string,
insert,
scan,
stats,
serialize,
deserialize,
stream_keep_last_n,
stream_downsample_ohlc,
stream_dead_band,
stream_put,
stream_query,
stream_serialize,
stream_restore,
log_enable,
log_produce_string,
log_subscribe,
log_poll,
log_list_topics,
log_serialize,
log_deserialize,
sync_pull,
sync_apply,
sync_local_seq,
sync_pull_range,
} from "./target/js/release/build/cmd/main/main.js";
const ts_out = document.getElementById("ts_out");
const stream_out = document.getElementById("stream_out");
const log_out = document.getElementById("log_out");
const sync_out = document.getElementById("sync_out");
let db = null;
const log_ts = (m) => (ts_out.textContent += m + "\n");
const log_stream = (m) => (stream_out.textContent += m + "\n");
const log_log = (m) => (log_out.textContent += m + "\n");
const log_sync = (m) => (sync_out.textContent += m + "\n");
window.clear_ts = () => (ts_out.textContent = "");
window.clear_stream = () => (stream_out.textContent = "");
window.clear_log = () => (log_out.textContent = "");
window.clear_sync = () => (sync_out.textContent = "");
function ensure_db() {
if (!db) {
db = create(50);
}
}
// ts panel ---------------------------------------------------------------
window.ts_create = () => {
ensure_db();
log_ts("β
db = create(50) // ts mode enabled");
};
window.ts_insert = () => {
ensure_db();
const now = Date.now();
const ts = to_int64(now % 2147483647);
const temp = 20 + Math.random() * 10;
const sensor = Math.random() > 0.5 ? "sensor-1" : "sensor-2";
insert(db, ts, [value_float(temp), value_string(sensor)]);
log_ts(`π ts insert temp=${temp.toFixed(2)} sensor=${sensor}`);
};
window.ts_scan = () => {
ensure_db();
const rows = scan(db, to_int64(0), to_int64(2147483647));
log_ts(`π ${rows.length} rows`);
for (let i = 0; i < Math.min(rows.length, 10); i++) {
const r = rows[i];
let s = "";
for (const v of r.values) {
if (v.$tag === 2) s += v._0.toFixed(2) + "Β°C, ";
else if (v.$tag === 3) s += v._0 + ", ";
}
log_ts(` [${i}] ts=${r.ts.lo >>> 0} ${s.slice(0, -2)}`);
}
};
window.ts_stats = () => {
ensure_db();
log_ts("π " + stats(db));
};
window.ts_serialize = () => {
ensure_db();
const json = serialize(db);
log_ts(`πΎ ${json.length} bytes`);
log_ts(` ${json.slice(0, 80)}...`);
};
// stream panel -----------------------------------------------------------
window.stream_setup = () => {
ensure_db();
stream_downsample_ohlc(db, "price", to_int64(1000));
log_stream("β
price β Downsample(1s, OHLC)");
};
window.stream_setup_keep_last = (n) => {
ensure_db();
stream_keep_last_n(db, "price", n);
log_stream(`β
price β KeepLastN(${n})`);
};
window.stream_setup_dead_band = (th) => {
ensure_db();
stream_dead_band(db, "price", th);
log_stream(`β
price β DeadBand(${th})`);
};
let sim_t = 0;
let sim_v = 100;
window.stream_push_random = () => {
ensure_db();
for (let i = 0; i < 30; i++) {
sim_v += (Math.random() - 0.5) * 2;
sim_t += 100;
stream_put(db, "price", sim_v, to_int64(sim_t));
}
log_stream(`π pushed 30 samples (t now = ${sim_t})`);
};
window.stream_show = () => {
ensure_db();
const rows = stream_query(db, "price");
log_stream(`π ${rows.length} stream rows`);
for (let i = 0; i < Math.min(rows.length, 12); i++) {
const r = rows[i];
if (r.$tag === 0) {
log_stream(` [${i}] Point t=${r._0.lo} v=${r._1.toFixed(2)}`);
} else {
const b = r._0;
log_stream(
` [${i}] Candle start=${b.start.lo} O=${b.open.toFixed(2)} H=${b.high.toFixed(2)} L=${b.low.toFixed(2)} C=${b.close.toFixed(2)} n=${b.count}`,
);
}
}
};
// log panel --------------------------------------------------------------
const TOPIC = "events";
const GROUP = "panel";
window.log_setup = () => {
ensure_db();
log_enable(db);
log_subscribe(db, TOPIC, GROUP);
log_log(`β
subscribed group="${GROUP}" β topic="${TOPIC}"`);
};
window.log_produce_one = () => {
ensure_db();
log_enable(db);
const now = Date.now();
const id = "id_" + (now % 100000);
const message = "hello @ " + new Date().toLocaleTimeString();
const offset = log_produce_string(db, TOPIC, id, message, to_int64(now % 2147483647));
log_log(`π produce key="${id}" β offset=${offset.lo}`);
if (bcast_on && bc) bc.postMessage({ kind: "produce", id, message, ts: now });
if (opfs_auto) schedule_opfs_save();
};
window.log_poll_some = () => {
ensure_db();
log_enable(db);
const records = log_poll(db, TOPIC, GROUP, 5);
log_log(`π polled ${records.length} record(s)`);
for (const r of records) {
const k = r.key === null ? "(no key)" : r.key._0; // Option<String> β {_0}
let v_str = "?";
const v = r.value;
if (v.$tag === 3) v_str = `"${v._0}"`;
else if (v.$tag === 1) v_str = String(v._0.lo);
else if (v.$tag === 2) v_str = v._0.toFixed(2);
log_log(` offset=${r.offset.lo} key=${k} v=${v_str}`);
}
};
window.log_topics = () => {
ensure_db();
const topics = log_list_topics(db);
log_log(`π¦ topics: [${topics.join(", ")}]`);
};
// BroadcastChannel cross-tab sync ---------------------------------------
let bc = null;
let bcast_on = false;
window.toggle_bcast = () => {
bcast_on = !bcast_on;
const pill = document.getElementById("bcast_pill");
if (bcast_on) {
bc = new BroadcastChannel("usagidb-log");
bc.onmessage = (e) => {
if (e.data.kind === "produce") {
ensure_db();
log_enable(db);
log_produce_string(db, TOPIC, e.data.id, e.data.message, to_int64(e.data.ts % 2147483647));
log_log(`β remote produce: id=${e.data.id} β mirrored locally`);
}
};
pill.textContent = "bcast: on";
pill.classList.remove("off");
log_log("β
BroadcastChannel('usagidb-log') opened β open another tab to sync");
} else {
if (bc) bc.close();
bc = null;
pill.textContent = "bcast: off";
pill.classList.add("off");
log_log("βΈ BroadcastChannel closed");
}
};
// OPFS auto-save (switchable) β covers ts + stream + log in one file.
const OPFS_FILE = "usagidb-all.json";
let opfs_auto = false;
let opfs_save_pending = false;
async function opfs_write(name, content) {
const root = await navigator.storage.getDirectory();
const handle = await root.getFileHandle(name, { create: true });
const writable = await handle.createWritable();
await writable.write(content);
await writable.close();
}
async function opfs_read(name) {
const root = await navigator.storage.getDirectory();
const handle = await root.getFileHandle(name);
const file = await handle.getFile();
return await file.text();
}
function snapshot_all() {
// Each mode is optional; omit when disabled.
const blob = {
ts: db && db.ts.$tag !== undefined ? null : (db && db.ts ? serialize(db) : null),
stream: db && db.stream ? stream_serialize(db) : null,
log: db && db.log ? log_serialize(db) : null,
};
// MoonBit Option in JS: `Some(x)` is `{$tag:0,_0:x}`, `None` is `null`.
// Our umbrella exposes ts/stream/log as plain `?` β JS sees the value
// (or `null` for None). The check above tries both shapes.
return JSON.stringify(blob);
}
function schedule_opfs_save() {
if (opfs_save_pending) return;
opfs_save_pending = true;
requestAnimationFrame(async () => {
opfs_save_pending = false;
try {
const text = snapshot_all();
await opfs_write(OPFS_FILE, text);
} catch (err) {
log_log("β οΈ opfs save failed: " + err.message);
}
});
}
window.toggle_opfs = () => {
opfs_auto = !opfs_auto;
const pill = document.getElementById("opfs_pill");
if (opfs_auto) {
pill.textContent = "opfs: on";
pill.classList.remove("off");
log_log("β
OPFS auto-save on (ts+stream+log) β " + OPFS_FILE);
if (db) schedule_opfs_save();
} else {
pill.textContent = "opfs: off";
pill.classList.add("off");
log_log("βΈ OPFS auto-save off");
}
};
window.opfs_load = async () => {
try {
const text = await opfs_read(OPFS_FILE);
const blob = JSON.parse(text);
// Restore ts (rebuilds the db from scratch; ts becomes the source).
if (blob.ts) {
const restored_ts = deserialize(blob.ts);
if (restored_ts) {
db = restored_ts;
log_log("π ts restored");
}
} else {
ensure_db();
}
// Restore log
if (blob.log) {
log_enable(db);
if (log_deserialize(db, blob.log)) {
log_log(`π log restored, topics: [${log_list_topics(db).join(", ")}]`);
}
}
// Restore stream (caller must have configured rules manually)
if (blob.stream) {
// Ensure stream mode exists with empty config.
if (!db.stream) {
// re-create db with stream enabled? For simplicity, panic if not enabled.
log_stream("β οΈ stream not enabled β set up rules first then load");
} else if (stream_restore(db, blob.stream)) {
log_stream("π stream state restored");
}
}
} catch (err) {
log_log("β οΈ opfs load failed: " + err.message);
}
};
log_ts("π° ts panel");
log_stream("π stream panel");
log_log("π log panel β Subscribe β Produce β Poll. Try BroadcastChannel + OPFS!");
log_sync("π sync β connect to a WebSocket relay to push/pull SyncPackets");
// sync transport ---------------------------------------------------------
let ws = null;
let last_pulled_seq = 0n; // BigInt to mirror Int64 wire shape
window.ws_connect = () => {
ensure_db();
const url = document.getElementById("ws_url").value;
try {
ws = new WebSocket(url);
} catch (err) {
log_sync("β οΈ connect failed: " + err.message);
return;
}
ws.onopen = () => {
document.getElementById("ws_pill").textContent = "ws: on";
document.getElementById("ws_pill").classList.remove("off");
log_sync("β
connected to " + url);
};
ws.onmessage = (ev) => {
// Wire format: a JSON SyncPacket. Apply it locally.
try {
const result = sync_apply(db, ev.data);
// SyncResult is enum { Success(n), Conflict([ts]), Error(s) }
if (result.$tag === 0) {
log_sync(`π₯ applied ${result._0} change(s)`);
} else if (result.$tag === 1) {
log_sync(`β οΈ ${result._0.length} conflict(s)`);
} else {
log_sync(`β οΈ error: ${result._0}`);
}
} catch (err) {
log_sync("β οΈ apply failed: " + err.message);
}
};
ws.onerror = (err) => log_sync("β οΈ ws error");
ws.onclose = () => {
document.getElementById("ws_pill").textContent = "ws: off";
document.getElementById("ws_pill").classList.add("off");
log_sync("βΈ disconnected");
};
};
window.ws_disconnect = () => {
if (ws) {
ws.close();
ws = null;
}
};
window.ws_pull = () => {
if (!ws || ws.readyState !== 1) return log_sync("β οΈ not connected");
// Send a small "pull" request β relay can interpret however.
const local = sync_local_seq(db);
ws.send(JSON.stringify({ kind: "pull", since: local }));
log_sync(`π€ pull request since seq=${local.lo || 0}`);
};
window.ws_push = () => {
if (!ws || ws.readyState !== 1) return enqueue_offline("push", sync_pull(db, to_int64(0)));
const json = sync_pull(db, to_int64(0));
ws.send(json);
log_sync(`π€ pushed packet (${json.length} bytes)`);
};
window.ws_push_range = () => {
ensure_db();
// Demo shape: last 10 seconds of data only.
const now = Date.now() % 2147483647;
const ts_min = to_int64(Math.max(0, now - 10000));
const ts_max = to_int64(now);
const json = sync_pull_range(db, to_int64(0), ts_min, ts_max);
if (!ws || ws.readyState !== 1) return enqueue_offline("push", json);
ws.send(json);
log_sync(`π€ pushed shape packet (${json.length} bytes, last 10s)`);
};
// Background Sync βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const OUTBOX_KEY = "usagidb_outbox";
const get_outbox = () => JSON.parse(localStorage.getItem(OUTBOX_KEY) || "[]");
const set_outbox = (arr) => localStorage.setItem(OUTBOX_KEY, JSON.stringify(arr));
function enqueue_offline(kind, body) {
const outbox = get_outbox();
outbox.push({
url: document.getElementById("ws_url").value.replace(/^ws/, "http"),
body,
});
set_outbox(outbox);
log_sync(`πΎ offline: queued (${outbox.length} pending)`);
// Try to wake the SW so it can drain when online.
if (navigator.serviceWorker && navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({ kind: "drain" });
}
}
// ts P2P sync via BroadcastChannel βββββββββββββββββββββββββββββββββββββ
// Intra-tab/cross-tab same-origin only. Plug in a WebRTC RTCDataChannel
// (with an external signaling server) to extend across origins.
let p2p_bc = null;
let p2p_on = false;
window.toggle_p2p = () => {
const pill = document.getElementById("p2p_pill");
p2p_on = !p2p_on;
if (p2p_on) {
ensure_db();
p2p_bc = new BroadcastChannel("usagidb-ts-p2p");
p2p_bc.onmessage = (e) => {
if (e.data.kind === "sync-packet") {
const r = sync_apply(db, e.data.json);
if (r.$tag === 0) {
log_sync(`β peer applied ${r._0} change(s)`);
}
}
};
// Auto-broadcast on every successful local push.
const orig_push = window.ws_push;
window.ws_push = () => {
const json = sync_pull(db, to_int64(0));
if (ws && ws.readyState === 1) ws.send(json);
if (p2p_bc) p2p_bc.postMessage({ kind: "sync-packet", json });
log_sync(`π€ push (ws=${ws ? "on" : "off"}, p2p=on)`);
};
pill.textContent = "p2p: on";
pill.classList.remove("off");
log_sync("β
P2P intra-tab on. Open another tab + toggle to sync ts via BroadcastChannel.");
} else {
if (p2p_bc) p2p_bc.close();
p2p_bc = null;
pill.textContent = "p2p: off";
pill.classList.add("off");
log_sync("βΈ P2P off");
}
};
window.enable_bg_sync = async () => {
try {
const reg = await navigator.serviceWorker.register("./sw.js");
await navigator.serviceWorker.ready;
// SW asks the page for the outbox on each drain attempt.
navigator.serviceWorker.addEventListener("message", (e) => {
if (e.data.kind === "request-outbox" && e.ports.length) {
const outbox = get_outbox();
e.ports[0].postMessage({ outbox });
set_outbox([]);
} else if (e.data.kind === "outbox-drained") {
log_sync(`π€ background drain: sent ${e.data.sent}`);
}
});
// Register the periodic background sync tag (Chromium only).
if (reg.sync) {
await reg.sync.register("usagidb");
}
// Fall back: drain on regular online events.
window.addEventListener("online", () => {
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({ kind: "drain" });
}
});
document.getElementById("bgsync_pill").textContent = "bg-sync: on";
document.getElementById("bgsync_pill").classList.remove("off");
log_sync("β
Service Worker registered. Offline pushes queue β flush on reconnect.");
} catch (err) {
log_sync("β οΈ SW register failed: " + err.message);
}
};
</script>
</body>
</html>