-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.html
More file actions
451 lines (413 loc) · 16.3 KB
/
widget.html
File metadata and controls
451 lines (413 loc) · 16.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
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>TVM-Based networks — current load</title>
<style>
@font-face {
font-family: 'JetBrains Mono';
src: url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff2/JetBrainsMono-Regular.woff2') format('woff2'),
url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff/JetBrainsMono-Regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: swap;
}
body {
font-family: 'JetBrains Mono', monospace;
margin: 0;
padding: 0;
}
h3 {
margin-bottom: 0;
}
h4 {
margin-top: 0;
margin-bottom: 0;
}
</style>
<script>
const u8 = (value) => BigInt.asUintN(8, BigInt(value)).toString(16)
function metricUpdate(elem) {
let startTime
let totalTransactionCount = 0
let totalBlockCount = 0
const wcCount = {}
return (block) => {
const transactionCount = block.tr_count
const workchainId = u8(block.workchain_id)
wcCount[workchainId] = (wcCount[workchainId] || 0) + transactionCount
const nowTime = Date.now()
if (!startTime) {
startTime = Date.now()
}
totalTransactionCount += transactionCount
totalBlockCount += 1
const timeDiff = nowTime - startTime
const BPS = Number(totalBlockCount / timeDiff * 1000)
const TPS = Number(totalTransactionCount / timeDiff * 1000)
const TPSMC = Number(wcCount['ff'] / timeDiff * 1000)
const TPSMWC0 = Number(wcCount['0'] / timeDiff * 1000)
if (timeDiff > 10000) {
elem.updateElem('bps', `${BPS.toFixed(1)}`)
elem.updateElem('tps', `${TPS.toFixed(1)}`)
if (TPSMC) {
elem.updateElem('tpsMC', `${TPSMC.toFixed(2)}`)
elem.updateElem('tpsWC0', `${TPSMWC0 >= 0 ? TPSMWC0.toFixed(2) : '...'}`)
}
}
}
}
function createMetric(root, network) {
const out = document.createElement('div')
out.className = 'metric'
let extra = ''
if (!network.includes('ackinacki')) {
extra += '<span>(MC: <span data-behavior="tpsMC">...</span></span>'
extra += '<span> WC: <span data-behavior="tpsWC0">...</span>)</span>'
}
out.innerHTML = `<h3>${name(network)}</h3>
<h4>
<span> BPS: <span data-behavior='bps'>...</span><span>
<span> TPS: <span data-behavior='tps'>...</span><span>
${extra}
</h4>
<div data-behavior='output'></div>`
out.updateElem = (name, value) => {
out.querySelector(`[data-behavior=${name}]`).innerHTML = value
}
root.append(out)
return {
elem: out,
up: metricUpdate(out),
log: (message) => out.updateElem('output', message),
}
}
function startLongPolling({ intervalMs = 1500, fetchOnce, normalize, onBatch, onError, onLog }) {
let stopped = false
async function tick() {
if (stopped) return
try {
const raw = await fetchOnce()
const normalized = normalize(raw)
await onBatch(normalized, raw)
} catch (e) {
if (onError) onError(e)
} finally {
if (!stopped) setTimeout(tick, intervalMs)
}
}
if (onLog) onLog('Starting long-poll...')
tick()
return { stop: () => { stopped = true } }
}
function createPollingMetricsAggregator(elem, options = {}) {
const retentionMs = options.retentionMs || 5 * 60 * 1000 // keep ids for 5 minutes
let firstBlockTime
let lastBlockTime
let totalTransactionCount = 0
let totalBlockCount = 0
const wcTransactionCount = {}
const seenBlocks = new Map()
function prune(now) {
for (const [id, meta] of seenBlocks) {
if (now - meta.gen_utime * 1000 > retentionMs) seenBlocks.delete(id)
}
}
function ingest({ blocks = [], transactions = [] }) {
const now = Date.now()
let newBlocks = 0
for (const b of blocks) {
if (!b || !b.id || typeof b.gen_utime !== 'number') continue
if (!seenBlocks.has(b.id)) {
seenBlocks.set(b.id, { gen_utime: b.gen_utime, workchain_id: b.workchain_id })
newBlocks += 1
// Track time range
if (!firstBlockTime || b.gen_utime < firstBlockTime) {
firstBlockTime = b.gen_utime
}
if (!lastBlockTime || b.gen_utime > lastBlockTime) {
lastBlockTime = b.gen_utime
}
if (typeof b.tr_count === 'number') {
totalTransactionCount += b.tr_count
if (b.workchain_id !== undefined) {
const wc = u8(b.workchain_id)
wcTransactionCount[wc] = (wcTransactionCount[wc] || 0) + b.tr_count
}
}
}
}
totalBlockCount += newBlocks
prune(now)
// Calculate metrics based on block time range, not client time
if (firstBlockTime && lastBlockTime && lastBlockTime > firstBlockTime) {
const timeDiffSeconds = lastBlockTime - firstBlockTime
if (timeDiffSeconds > 1) {
const BPS = Number(totalBlockCount / timeDiffSeconds)
const TPS = Number(totalTransactionCount / timeDiffSeconds)
const TPSMC = wcTransactionCount['ff'] ? Number(wcTransactionCount['ff'] / timeDiffSeconds) : undefined
const TPSWC0 = wcTransactionCount['0'] !== undefined ? Number(wcTransactionCount['0'] / timeDiffSeconds) : undefined
elem.updateElem('bps', `${BPS.toFixed(1)}`)
elem.updateElem('tps', `${TPS.toFixed(1)}`)
if (TPSMC !== undefined) {
elem.updateElem('tpsMC', `${TPSMC.toFixed(2)}`)
elem.updateElem('tpsWC0', `${TPSWC0 >= 0 ? TPSWC0.toFixed(2) : '...'}`)
}
}
}
return { newBlocks, newTxs: 0 }
}
return { ingest }
}
function notImplemented(root, network) {
const { log } = createMetric(root, network)
log(`<span style='color: red'>ERROR:</span> not impliment ${network}`)
}
function blockPolling(root, network) {
const { elem, log } = createMetric(root, network)
const aggregator = createPollingMetricsAggregator(elem)
const host = graphqlHost(network)
const query = `query { blockchain { blocks( last: 500 ) { edges { node { hash seq_no workchain_id shard gen_utime tr_count gen_software_version } cursor } pageInfo{ endCursor } } } }`
const makeUrl = () => `https://${host}/graphql?_=_&isBlockChainApiQuery=true&query=${encodeURIComponent(query)}`
const fetchOnce = async () => {
const res = await fetch(makeUrl(), { method: 'GET', cache: 'no-store' })
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return res.json()
}
const normalize = (response) => {
const edges = (((response || {}).data || {}).blockchain || {}).blocks || {}
const list = Array.isArray(edges.edges) ? edges.edges : []
const blocks = list.map(e => e && e.node).filter(Boolean).map(n => ({
id: n.hash,
tr_count: n.tr_count,
workchain_id: n.workchain_id,
seq_no: n.seq_no,
shard: n.shard,
gen_utime: n.gen_utime,
gen_software_version: n.gen_software_version,
}))
return { blocks, transactions: [] }
}
const onBatch = (normalized, raw) => {
aggregator.ingest(normalized)
// Always display latest block link using logTemplate
let latest = null
// Prefer normalized blocks list
if (normalized && Array.isArray(normalized.blocks) && normalized.blocks.length > 0) {
latest = normalized.blocks.reduce((acc, b) => {
if (!acc) return b
if (typeof b.seq_no === 'number' && typeof acc.seq_no === 'number') {
return b.seq_no > acc.seq_no ? b : acc
}
return b || acc
}, null)
}
// Fallback to raw edges if needed
if (!latest) {
const blocksNode = ((((raw || {}).data || {}).blockchain || {}).blocks) || {}
const edges = Array.isArray(blocksNode.edges) ? blocksNode.edges : []
if (edges.length > 0) {
const latestNode = edges.reduce((acc, e) => {
const n = e && e.node
if (!n) return acc
if (!acc || (typeof n.seq_no === 'number' && n.seq_no > acc.seq_no)) return n
return acc
}, null)
if (latestNode) {
latest = {
id: latestNode.hash,
workchain_id: latestNode.workchain_id,
shard: latestNode.shard,
seq_no: latestNode.seq_no,
gen_software_version: latestNode.gen_software_version,
}
}
}
}
if (latest) {
log(logTemplate(network, latest))
}
}
const onError = (e) => {
log(`<span style='color: red'>ERROR:</span> ${e.message || e}`)
}
startLongPolling({ intervalMs: 1500, fetchOnce, normalize, onBatch, onError, onLog: log })
}
function blockPollingTonCore(root, network) {
// TODO implement
console.log(`${network} not implement`)
}
function blockSubscriptionEverX(root, network) {
const query = 'subscription {blocks {id tr_count workchain_id seq_no shard gen_software_version}}'
const {up, log} = createMetric(root, network)
const connect = (url) => {
const start = Date.now()
let dataCount = 0
let dataEmptyCount = 0
const websocket = new WebSocket(url, [
'graphql-ws',
])
websocket.onopen = function() {
log('Trying to connect...')
websocket.send('{"type":"connection_init","payload":{}}')
websocket.send(`{"id":"1","type":"start","payload":{"variables":{},"extensions":{},"operationName":null,"query":"${query}"}}`)
}
websocket.onclose = function() {
log('Disconnected, reconnect will be attempted in 5 second...')
setTimeout(function() {
connect(url)
}, 5000)
}
websocket.onmessage = function(event) {
const count = 1 / dataEmptyCount * dataCount
const data = JSON.parse(event.data)
if (data.type === 'data') {
dataCount += 1
const block = data.payload.data.blocks
log(logTemplate(network, block))
up(block)
} else {
dataEmptyCount += 1
}
if (count < 1 && Date.now() - start > 10000) {
websocket.close()
}
}
websocket.onerror = function(event) {
log(`<span style='color: red'>ERROR:</span> ${event.data}`)
websocket.close()
}
}
connect(`wss://${graphqlHost(network)}/graphql`)
}
function logTemplate(network, block) {
const url = explorerBlock(network, block)
let content = `${block.workchain_id}:${block.shard.slice(0, 2)}:${block.seq_no}`
if (block.gen_software_version) {
content += `|v${block.gen_software_version}`
}
if (network.includes('ackinacki')) {
content = `${block.seq_no}`
}
if (url) {
return `<span><a target="_blank" href="${url}">${content}</a></span>`
}
return `<span>${content}</span>`
}
function explorerBlock(network, block) {
switch (network) {
case 'ackinacki-mainnet':
return `https://acki.live/blocks/${block.id}`
case 'ackinacki-shellnet':
return `https://shellnet.acki.live/blocks/${block.id}`
case 'ever-main':
return `https://everscan.io/blocks/${block.workchain_id}/${block.shard}/${block.seq_no}`
case 'ever-test':
return `https://testnet.everscan.io/blocks/${block.workchain_id}/${block.shard}/${block.seq_no}`
case 'venom-main':
return `https://venomscan.com/blocks/${block.workchain_id}/${block.shard}/${block.seq_no}`
case 'venom-test':
return `https://testnet.venomscan.com/blocks/${block.workchain_id}/${block.shard}/${block.seq_no}`
case 'gosh-main':
return `https://gosh.live/blocks/blockDetails?id=${block.id}`
case 'gosh-test':
return `https://ackinacki-testnet.tvmlabs.dev/blocks/blockDetails?id=${block.id}`
case 'ever-beta':
return `https://rfld.ever.live/blocks/blockDetails?id=${block.id}`
case 'ever-smft':
return `https://smft.ever.live/blocks/blockDetails?id=${block.id}`
case 'ton-main':
return `https://tonscan.org/block/${block.workchain_id}:${block.shard}:${block.seq_no}`
case 'ton-test':
return `https://testnet.tonscan.org/block/${block.workchain_id}:${block.shard}:${block.seq_no}`
default:
return null
}
}
function graphqlHost(network) {
const projectID = 'a3446bfb5e4e4f2a84d68400a28d990e'
switch (network) {
case 'ackinacki-shellnet':
return 'shellnet.ackinacki.org'
case 'ackinacki-mainnet':
return 'mainnet.ackinacki.org'
case 'gosh-main':
return 'gra01.network.gosh.sh'
case 'venom-main':
return 'venom-mainnet.tvmlabs.dev'
case 'venom-test':
return 'venom-testnet.tvmlabs.dev'
case 'ever-main':
return `mainnet.evercloud.dev/${projectID}`
case 'ever-test':
return `devnet.evercloud.dev/${projectID}`
case 'ton-main':
return 'ton-mainnet.tvmlabs.dev'
case 'ton-test':
return 'ton-testnet.tvmlabs.dev'
default:
return `${network}.evercloud.dev/${projectID}`
}
}
function name(network) {
switch (network) {
case 'ackinacki-shellnet':
return 'Acki Nacki Testnet'
case 'ackinacki-mainnet':
return 'Acki Nacki Mainnet'
case 'gosh-main':
return 'Gosh Mainnet'
case 'venom-main':
return 'Venom Mainnet'
case 'venom-test':
return 'Venom Testnet'
case 'ever-main':
return 'Everscale Mainnet'
case 'ever-test':
return 'Everscale Testnet'
case 'ton-main':
return 'TON Mainnet'
case 'ton-test':
return 'TON Testnet'
default:
return network
}
}
function loadWidget(elem, network) {
switch (network) {
case 'ackinacki-shellnet':
case 'ackinacki-mainnet':
blockPolling(elem, network)
break
case 'ton-main':
case 'ton-test':
blockPollingTonCore(elem, network)
break
case 'ever-main':
case 'ever-test':
case 'venom-main':
case 'venom-test':
blockSubscriptionEverX(elem, network)
break
default:
notImplemented(elem, network)
}
}
window.addEventListener('load', function () {
const root = document.querySelector('[data-behavior=content]')
loadWidget(root, 'ever-main')
// loadWidget(root, 'ever-test')
loadWidget(root, 'venom-main')
// loadWidget(root, 'venom-test')
loadWidget(root, 'ackinacki-mainnet')
loadWidget(root, 'ackinacki-shellnet')
// loadWidget(root, 'ton-main')
// loadWidget(root, 'ton-test')
})
</script>
</head>
<body>
<h2>Networks current load</h2>
<main data-behavior='content'></main>
</body>
</html>