Align graph lengths (#1887)

This commit is contained in:
Jaco Greeff 2019-11-15 01:04:26 +01:00 committed by GitHub
parent 4241653182
commit 87dbcd2067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 14 deletions

View File

@ -14,6 +14,7 @@ import { ApiContext, withCalls } from '@polkadot/react-api';
import { getHistoric } from '@polkadot/react-api/util';
import { formatBalance, formatNumber } from '@polkadot/util';
import { MAX_SESSIONS } from '../constants';
import translate from '../translate';
interface Props extends I18nProps {
@ -35,9 +36,6 @@ interface SplitEntry {
type SplitData = SplitEntry[];
// assuming 4 hrs sessions, we grab results for 10 days
const SESSIONS = 10 * (24 / 4);
const COLORS_MINE = ['#ff8c00'];
const COLORS_OTHER = ['#acacac'];
const COLORS_BLOCKS = [undefined, '#acacac'];
@ -46,7 +44,7 @@ function getIndexRange (currentIndex: SessionIndex): BN[] {
const range: BN[] = [];
let thisIndex: BN = currentIndex;
while (thisIndex.gtn(0) && range.length < SESSIONS) {
while (thisIndex.gtn(0) && range.length < MAX_SESSIONS) {
range.push(thisIndex);
thisIndex = thisIndex.subn(1);
@ -102,7 +100,7 @@ function Validator ({ blockCounts, className, currentIndex, stakingRewards, star
api.isReady.then(async (): Promise<void> => {
const values = await getHistoric<Exposure>(api, 'staking.stakers', [validatorId], {
interval: (api.consts.babe?.epochDuration as BlockNumber || new BN(500)).muln(2).divn(3),
max: SESSIONS,
max: MAX_SESSIONS,
startNumber
});
const [stakeLabels, stakeChart] = extractStake(values, divisor);

View File

@ -3,3 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
export const STORE_FAVS_BASE = 'staking:favorites';
// assuming 4 hrs sessions, we grab results for 10 days
export const MAX_SESSIONS = 10 * (24 / 4);

View File

@ -24,6 +24,7 @@ import Accounts from './Actions/Accounts';
import basicMd from './md/basic.md';
import Overview from './Overview';
import Query from './Query';
import { MAX_SESSIONS } from './constants';
import translate from './translate';
interface Props extends AppProps, ApiProps, I18nProps {
@ -39,7 +40,7 @@ const EMPTY_ALL: [string[], string[]] = [EMPY_ACCOUNTS, EMPY_ACCOUNTS];
function App ({ allAccounts, allStashesAndControllers: [allStashes, allControllers] = EMPTY_ALL, basePath, bestNumber, className, recentlyOnline, stakingOverview, t }: Props): React.ReactElement<Props> {
const { api } = useContext(ApiContext);
const stakingRewards = useSessionSlashes();
const stakingRewards = useSessionSlashes(MAX_SESSIONS);
const routeMatch = useRouteMatch({ path: basePath, strict: true });
const _renderComponent = (Component: React.ComponentType<ComponentProps>, className?: string): () => React.ReactNode => {
// eslint-disable-next-line react/display-name

View File

@ -27,8 +27,8 @@ interface SessionResultSer {
slashes: SlashSer[];
}
// assuming 4 hrs sessions, we grab results for 10 days (+2 for at-start throw-away)
const MAX_SESSIONS = 10 * (24 / 4) + 2;
// assuming 4 hrs sessions, we grab results for 10 days
const MAX_SESSIONS = 10 * (24 / 4);
function getStorage (storageKey: string): SessionRewards[] {
const sessions: SessionResultSer[] = store.get(storageKey, []);
@ -46,7 +46,7 @@ function getStorage (storageKey: string): SessionRewards[] {
}));
}
function setStorage (storageKey: string, sessions: SessionRewards[]): SessionResultSer[] {
function setStorage (storageKey: string, sessions: SessionRewards[], maxSessions: number): SessionResultSer[] {
return store.set(
storageKey,
sessions
@ -61,7 +61,7 @@ function setStorage (storageKey: string, sessions: SessionRewards[]): SessionRes
amount: amount.toHex()
}))
}))
.slice(0, MAX_SESSIONS + 1)
.slice(-maxSessions)
);
}
@ -127,8 +127,9 @@ export default function useSessionSlashes (maxSessions = MAX_SESSIONS): SessionR
let workQueue = results;
api.isReady.then(async (): Promise<void> => {
const maxSessionsStore = maxSessions + 1; // assuming first is a bust
const sessionLength = (api.consts.babe?.epochDuration as BlockNumber || new BN(500));
const count = sessionLength.muln(maxSessions).divn(10).toNumber();
const count = Math.min(sessionLength.muln(maxSessionsStore).divn(10).toNumber(), 10000);
const bestHeader = await api.rpc.chain.getHeader();
let toHash = bestHeader.hash;
let toNumber = bestHeader.number.unwrap().toBn();
@ -145,10 +146,12 @@ export default function useSessionSlashes (maxSessions = MAX_SESSIONS): SessionR
toNumber = fromNumber;
fromNumber = bnMax(toNumber.subn(count), new BN(1));
setStorage(STORAGE_KEY, workQueue);
setStorage(STORAGE_KEY, workQueue, maxSessionsStore);
setResults(workQueue);
if (fromNumber.eqn(1) || (workQueue.length > maxSessions)) {
const lastNumber = workQueue[workQueue.length - 1]?.blockNumber;
if (!lastNumber || fromNumber.eqn(1) || ((workQueue.length >= maxSessionsStore) && fromNumber.lt(lastNumber))) {
break;
}
}
@ -156,7 +159,7 @@ export default function useSessionSlashes (maxSessions = MAX_SESSIONS): SessionR
}, []);
useEffect((): void => {
setFiltered(results.filter(({ isEventsEmpty }): boolean => !isEventsEmpty));
setFiltered(results.slice(-maxSessions));
}, [results]);
return filtered;