mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-11-23 22:29:58 +00:00
feat: initial Pezkuwi Apps rebrand from polkadot-apps
Rebranded terminology: - Polkadot → Pezkuwi - Kusama → Dicle - Westend → Zagros - Rococo → PezkuwiChain - Substrate → Bizinikiwi - parachain → teyrchain Custom logos with Kurdistan brand colors (#e6007a → #86e62a): - bizinikiwi-hexagon.svg - sora-bizinikiwi.svg - hezscanner.svg - heztreasury.svg - pezkuwiscan.svg - pezkuwistats.svg - pezkuwiassembly.svg - pezkuwiholic.svg
This commit is contained in:
commit
d21bfb1320
5867 files changed
+329019
No files matched your search
@@ -0,0 +1,48 @@
|
||||
// Copyright 2017-2025 @pezkuwi/app-explorer authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ChartOptions } from 'chart.js';
|
||||
import type { ChartContents } from './types.js';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import { Chart, styled } from '@pezkuwi/react-components';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
colors: string[];
|
||||
legends: string[];
|
||||
options?: ChartOptions;
|
||||
title: string;
|
||||
value: ChartContents;
|
||||
}
|
||||
|
||||
const OPTIONS: ChartOptions = {
|
||||
aspectRatio: 6,
|
||||
maintainAspectRatio: true
|
||||
};
|
||||
|
||||
function ChartDisplay ({ className, colors, legends, options, title, value: { labels, values } }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<StyledDiv className={className}>
|
||||
<Chart.Line
|
||||
colors={colors}
|
||||
labels={labels}
|
||||
legends={legends}
|
||||
options={options || OPTIONS}
|
||||
title={title}
|
||||
values={values}
|
||||
/>
|
||||
</StyledDiv>
|
||||
);
|
||||
}
|
||||
|
||||
const StyledDiv = styled.div`
|
||||
background: var(--bg-table);
|
||||
border: 1px solid var(--border-table);
|
||||
border-radius: 0.25rem;
|
||||
margin-bottom: 1rem;
|
||||
padding: 1rem 1.5rem;
|
||||
`;
|
||||
|
||||
export default React.memo(ChartDisplay);
|
||||
@@ -0,0 +1,214 @@
|
||||
// Copyright 2017-2025 @pezkuwi/app-explorer authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ChartContents, Detail } from './types.js';
|
||||
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { CardSummary, NextTick, styled, SummaryBox } from '@pezkuwi/react-components';
|
||||
import { formatNumber, nextTick } from '@pezkuwi/util';
|
||||
|
||||
import { useTranslation } from '../translate.js';
|
||||
import Chart from './Chart.js';
|
||||
import useLatency from './useLatency.js';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
}
|
||||
|
||||
interface ChartInfo {
|
||||
blockLast: number;
|
||||
blocks: ChartContents;
|
||||
events: ChartContents;
|
||||
extrinsics: ChartContents;
|
||||
times: ChartContents;
|
||||
}
|
||||
|
||||
const ORDER = ['times', 'blocks', 'extrinsics', 'events'] as const;
|
||||
|
||||
const COLORS = {
|
||||
blocks: ['#008c8c', '#acacac'],
|
||||
events: ['#00448c', '#8c0044', '#acacac'],
|
||||
extrinsics: ['#448c00', '#acacac'],
|
||||
times: ['#8c8c00', '#acacac']
|
||||
};
|
||||
|
||||
function getPoints (details: Detail[], timeAvg: number): ChartInfo {
|
||||
const blocks: ChartContents = {
|
||||
labels: [],
|
||||
values: [[], []]
|
||||
};
|
||||
const events: ChartContents = {
|
||||
labels: [],
|
||||
values: [[], [], []]
|
||||
};
|
||||
const extrinsics: ChartContents = {
|
||||
labels: [],
|
||||
values: [[], []]
|
||||
};
|
||||
const times: ChartContents = {
|
||||
labels: [],
|
||||
values: [[], []]
|
||||
};
|
||||
|
||||
const eventTotal = details.reduce((a, { events: { count } }) => a + count, 0);
|
||||
const txTotal = details.reduce((a, { extrinsics: { count } }) => a + count, 0);
|
||||
const blockTotal = details.reduce((a, { block: { bytes } }) => a + bytes, 0);
|
||||
|
||||
for (let i = 0, count = details.length; i < count; i++) {
|
||||
const blockNumber = formatNumber(details[i].block.number);
|
||||
|
||||
blocks.labels.push(blockNumber);
|
||||
blocks.values[0].push(details[i].block.bytes);
|
||||
blocks.values[1].push(blockTotal / details.length);
|
||||
|
||||
events.labels.push(blockNumber);
|
||||
events.values[0].push(details[i].events.count);
|
||||
events.values[1].push(details[i].events.system);
|
||||
events.values[2].push(eventTotal / details.length);
|
||||
|
||||
extrinsics.labels.push(blockNumber);
|
||||
extrinsics.values[0].push(details[i].extrinsics.count);
|
||||
extrinsics.values[1].push(txTotal / details.length);
|
||||
}
|
||||
|
||||
const filtered = details.filter(({ delay }) => delay);
|
||||
const avgBase = timeAvg * filtered.length;
|
||||
|
||||
for (let i = 0, count = filtered.length; i < count; i++) {
|
||||
times.labels.push(formatNumber(filtered[i].block.number));
|
||||
times.values[0].push(filtered[i].delay / 1000);
|
||||
times.values[1].push(avgBase / filtered.length / 1000);
|
||||
}
|
||||
|
||||
return {
|
||||
blockLast: times.values[0][times.values[0].length - 1],
|
||||
blocks,
|
||||
events,
|
||||
extrinsics,
|
||||
times
|
||||
};
|
||||
}
|
||||
|
||||
function formatTime (time: number, divisor = 1000): React.ReactNode {
|
||||
return <span className='--digits'>{`${(time / divisor).toFixed(3)}`}<span className='postfix'> s</span></span>;
|
||||
}
|
||||
|
||||
function Latency ({ className }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { details, isLoaded, maxItems, stdDev, timeAvg, timeMax, timeMin } = useLatency();
|
||||
const [shouldRender, setShouldRender] = useState(() => new Array<boolean>(ORDER.length).fill(false));
|
||||
|
||||
useEffect((): void => {
|
||||
// HACK try and render the charts in order - this _may_ work around the
|
||||
// crosshair plugin init issues, but at best it is non-reproducible
|
||||
if (isLoaded) {
|
||||
const index = shouldRender.findIndex((v) => !v);
|
||||
|
||||
if (index !== -1) {
|
||||
nextTick(() =>
|
||||
setShouldRender(
|
||||
shouldRender.map((v, i) => (i === index) || v)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}, [isLoaded, shouldRender]);
|
||||
|
||||
const points = useMemo(
|
||||
() => getPoints(details, timeAvg),
|
||||
[details, timeAvg]
|
||||
);
|
||||
|
||||
const [legend, title] = useMemo(
|
||||
() => [
|
||||
{
|
||||
blocks: [t('bytes'), t('average')],
|
||||
events: [t('events'), t('system'), t('average')],
|
||||
extrinsics: [t('extrinsics'), t('average')],
|
||||
times: [t('blocktime'), t('average')]
|
||||
},
|
||||
{
|
||||
blocks: t('blocksize (last {{n}} blocks)', { replace: { n: maxItems } }),
|
||||
events: t('events (last {{n}} blocks)', { replace: { n: maxItems } }),
|
||||
extrinsics: t('extrinsics (last {{n}} blocks)', { replace: { n: maxItems } }),
|
||||
times: t('blocktimes (last {{n}} blocks)', { replace: { n: maxItems } })
|
||||
}
|
||||
],
|
||||
[maxItems, t]
|
||||
);
|
||||
|
||||
const EMPTY_TIME = <span className='--tmp --digits'>0.000 <span className='postfix'>s</span></span>;
|
||||
|
||||
return (
|
||||
<StyledDiv className={className}>
|
||||
<SummaryBox>
|
||||
<section>
|
||||
<CardSummary label={t('avg')}>
|
||||
{isLoaded
|
||||
? formatTime(timeAvg)
|
||||
: EMPTY_TIME}
|
||||
</CardSummary>
|
||||
<CardSummary
|
||||
className='media--1000'
|
||||
label={t('std dev')}
|
||||
>
|
||||
{isLoaded
|
||||
? formatTime(stdDev)
|
||||
: EMPTY_TIME}
|
||||
</CardSummary>
|
||||
</section>
|
||||
<section>
|
||||
<CardSummary label={t('min')}>
|
||||
{isLoaded
|
||||
? formatTime(timeMin)
|
||||
: EMPTY_TIME}
|
||||
</CardSummary>
|
||||
<CardSummary label={t('max')}>
|
||||
{isLoaded
|
||||
? formatTime(timeMax)
|
||||
: EMPTY_TIME
|
||||
}
|
||||
</CardSummary>
|
||||
</section>
|
||||
<CardSummary label={t('last')}>
|
||||
{isLoaded
|
||||
? formatTime(points.blockLast, 1)
|
||||
: EMPTY_TIME}
|
||||
</CardSummary>
|
||||
</SummaryBox>
|
||||
<NextTick isActive={isLoaded}>
|
||||
{ORDER.map((key) => (
|
||||
<Chart
|
||||
colors={COLORS[key]}
|
||||
key={key}
|
||||
legends={legend[key]}
|
||||
title={title[key]}
|
||||
value={points[key]}
|
||||
/>
|
||||
))}
|
||||
</NextTick>
|
||||
</StyledDiv>
|
||||
);
|
||||
}
|
||||
|
||||
const StyledDiv = styled.div`
|
||||
.container {
|
||||
background: var(--bg-table);
|
||||
border: 1px solid var(--border-table);
|
||||
border-radius: 0.25rem;
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
|
||||
.container+.container {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
span.--digits {
|
||||
.postfix {
|
||||
font-size: var(--font-percent-tiny);
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export default React.memo(Latency);
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2025 @pezkuwi/app-explorer authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Hash } from '@pezkuwi/types/interfaces';
|
||||
|
||||
export interface Detail {
|
||||
block: {
|
||||
bytes: number;
|
||||
number: number;
|
||||
};
|
||||
delay: number;
|
||||
events: {
|
||||
count: number;
|
||||
system: number;
|
||||
};
|
||||
extrinsics: {
|
||||
bytes: number;
|
||||
count: number;
|
||||
};
|
||||
now: number;
|
||||
parentHash: Hash;
|
||||
}
|
||||
|
||||
export interface Result {
|
||||
details: Detail[];
|
||||
isLoaded: boolean;
|
||||
maxItems: number;
|
||||
stdDev: number;
|
||||
timeAvg: number;
|
||||
timeMax: number;
|
||||
timeMin: number;
|
||||
}
|
||||
|
||||
export interface ChartContents {
|
||||
labels: string[];
|
||||
values: number[][];
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
// Copyright 2017-2025 @pezkuwi/app-explorer authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@pezkuwi/api';
|
||||
import type { SignedBlockExtended } from '@pezkuwi/api-derive/types';
|
||||
import type { GenericExtrinsic, u32 } from '@pezkuwi/types';
|
||||
import type { Block } from '@pezkuwi/types/interfaces';
|
||||
import type { Detail, Result } from './types.js';
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||
|
||||
import { createNamedHook, useApi, useCall } from '@pezkuwi/react-hooks';
|
||||
|
||||
const INITIAL_ITEMS = 50;
|
||||
const MAX_ITEMS = INITIAL_ITEMS;
|
||||
const EMPTY: Result = {
|
||||
details: [],
|
||||
isLoaded: false,
|
||||
maxItems: MAX_ITEMS,
|
||||
stdDev: 0,
|
||||
timeAvg: 0,
|
||||
timeMax: 0,
|
||||
timeMin: 0
|
||||
};
|
||||
|
||||
function getSetter ({ extrinsics }: Block): GenericExtrinsic | undefined {
|
||||
return extrinsics.find(({ method: { method, section } }) =>
|
||||
method === 'set' &&
|
||||
section === 'timestamp'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the delay for each block, correctly handling elastic scaling
|
||||
* by distributing the time difference across all blocks produced between two differing slots.
|
||||
*/
|
||||
function calcDelay (details: Detail[]): Detail[] {
|
||||
const filtered = details
|
||||
.sort((a, b) => a.block.number - b.block.number)
|
||||
.filter(({ block }, index) =>
|
||||
index === 0 ||
|
||||
block.number > details[index - 1].block.number
|
||||
);
|
||||
|
||||
for (let i = 0; i < filtered.length - 1; i++) {
|
||||
const a = filtered[i];
|
||||
const b = filtered[i + 1];
|
||||
|
||||
// Check if the current block 'a' and the next block 'b' share the same timestamp.
|
||||
// If they do, 'a' cannot be the slot anchor for 'b', so we skip 'a' and let the loop continue
|
||||
// to find the true anchor later when 'b' or a subsequent block is processed.
|
||||
if (a.now === b.now) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// At this point, 'a' is the last block of the previous slot
|
||||
// and 'b' is the first block of the new slot.
|
||||
|
||||
const delta = b.now - a.now;
|
||||
|
||||
if (delta < 0) {
|
||||
b.delay = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
let k = i + 1;
|
||||
|
||||
// Find the end of the current slot
|
||||
while (k < filtered.length && filtered[k].now === b.now) {
|
||||
k++;
|
||||
}
|
||||
|
||||
const lastBlockInSlot = filtered[k - 1];
|
||||
const blocksInWindow = lastBlockInSlot.block.number - a.block.number;
|
||||
|
||||
if (blocksInWindow <= 0) {
|
||||
b.delay = delta;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate the distributed delay (Delta t / N)
|
||||
const distributedDelay = delta / blocksInWindow;
|
||||
|
||||
// Apply the distributed delay to ALL blocks in the current slot/window
|
||||
for (let m = i + 1; m < k; m++) {
|
||||
filtered[m].delay = distributedDelay;
|
||||
}
|
||||
}
|
||||
|
||||
return filtered.slice(-MAX_ITEMS);
|
||||
}
|
||||
|
||||
function addBlock (prev: Detail[], { block, events }: SignedBlockExtended): Detail[] {
|
||||
const setter = getSetter(block);
|
||||
|
||||
if (!setter) {
|
||||
return prev;
|
||||
}
|
||||
|
||||
return [
|
||||
...prev,
|
||||
{
|
||||
block: {
|
||||
bytes: block.encodedLength,
|
||||
number: block.header.number.toNumber()
|
||||
},
|
||||
delay: 0,
|
||||
events: {
|
||||
count: events.length,
|
||||
system: events.filter(({ phase }) => !phase.isApplyExtrinsic).length
|
||||
},
|
||||
extrinsics: {
|
||||
bytes: block.extrinsics.reduce((a, x) => a + x.encodedLength, 0),
|
||||
count: block.extrinsics.length
|
||||
},
|
||||
now: (setter.args[0] as u32).toNumber(),
|
||||
parentHash: block.header.parentHash
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function addBlocks (prev: Detail[], blocks: SignedBlockExtended[]): Detail[] {
|
||||
return blocks.reduce((p, b) => addBlock(p, b), prev);
|
||||
}
|
||||
|
||||
async function getBlocks (api: ApiPromise, blockNumbers: number[]): Promise<SignedBlockExtended[]> {
|
||||
if (!blockNumbers.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const blocks = await Promise.all(
|
||||
blockNumbers.map((n) => api.derive.chain.getBlockByNumber(n))
|
||||
);
|
||||
|
||||
return blocks.filter((b): b is SignedBlockExtended => !!b);
|
||||
}
|
||||
|
||||
async function getPrev (api: ApiPromise, { block: { header } }: SignedBlockExtended): Promise<SignedBlockExtended[]> {
|
||||
const blockNumbers: number[] = [];
|
||||
let blockNumber = header.number.toNumber();
|
||||
|
||||
for (let i = 1; blockNumber > 0 && i <= INITIAL_ITEMS; i++) {
|
||||
blockNumbers.push(--blockNumber);
|
||||
}
|
||||
|
||||
return getBlocks(api, blockNumbers);
|
||||
}
|
||||
|
||||
async function getNext (api: ApiPromise, { block: { number: start } }: Detail, { block: { number: end } }: Detail): Promise<SignedBlockExtended[]> {
|
||||
const blockNumbers: number[] = [];
|
||||
|
||||
for (let n = start + 1; n < end; n++) {
|
||||
blockNumbers.push(n);
|
||||
}
|
||||
|
||||
return getBlocks(api, blockNumbers);
|
||||
}
|
||||
|
||||
function useLatencyImpl (): Result {
|
||||
const { api } = useApi();
|
||||
const [details, setDetails] = useState<Detail[]>([]);
|
||||
const signedBlock = useCall<SignedBlockExtended>(api.derive.chain.subscribeNewBlocks);
|
||||
const hasHistoric = useRef(false);
|
||||
|
||||
useEffect((): void => {
|
||||
if (!signedBlock) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDetails((prev) => calcDelay(addBlock(prev, signedBlock)));
|
||||
|
||||
if (hasHistoric.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasHistoric.current = true;
|
||||
|
||||
getPrev(api, signedBlock)
|
||||
.then((all) => setDetails((prev) => calcDelay(addBlocks(prev, all))))
|
||||
.catch(console.error);
|
||||
}, [api, signedBlock]);
|
||||
|
||||
useEffect((): void => {
|
||||
if (details.length <= 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastIndex = details.findIndex(({ block }, index) =>
|
||||
index !== (details.length - 1) &&
|
||||
(details[index + 1].block.number - block.number) > 1
|
||||
);
|
||||
|
||||
if (lastIndex === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
getNext(api, details[lastIndex], details[lastIndex + 1])
|
||||
.then((all) => setDetails((prev) => calcDelay(addBlocks(prev, all))))
|
||||
.catch(console.error);
|
||||
}, [api, details]);
|
||||
|
||||
return useMemo((): Result => {
|
||||
const delays = details
|
||||
.map(({ delay }) => delay)
|
||||
.filter((delay) => delay);
|
||||
|
||||
if (!delays.length) {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
const timeAvg = delays.reduce((avg, d) => avg + d, 0) / delays.length;
|
||||
const stdDev = Math.sqrt(delays.reduce((dev, d) => dev + Math.pow(timeAvg - d, 2), 0) / delays.length);
|
||||
|
||||
return {
|
||||
details,
|
||||
isLoaded: details.length === MAX_ITEMS,
|
||||
maxItems: MAX_ITEMS,
|
||||
stdDev,
|
||||
timeAvg,
|
||||
timeMax: Math.max(...delays),
|
||||
timeMin: Math.min(...delays)
|
||||
};
|
||||
}, [details]);
|
||||
}
|
||||
|
||||
export default createNamedHook('useLatency', useLatencyImpl);
|
||||
Reference in new issue
Block a user