Skip to content

Commit

Permalink
chore: show only dkg best authorities in table
Browse files Browse the repository at this point in the history
  • Loading branch information
devpavan04 committed Nov 2, 2023
1 parent 4b6435c commit 5d66de9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const AuthoritiesTable: FC<AuthoritiesTableProps> = ({
}) => {
const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
pageIndex: 0,
pageSize: 10,
pageSize: 20,
});

const pagination = useMemo(
Expand Down Expand Up @@ -166,18 +166,22 @@ export const AuthoritiesTable: FC<AuthoritiesTableProps> = ({
globalFilter,
]
);

const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);

const authorities = useAuthorities(query);
const totalItems = useMemo(
() => authorities.val?.pageInfo.count ?? 0,
[authorities]
);

const data = useMemo(() => {
return authorities.val?.items?.filter((item) => item.isBest === true) || [];
}, [authorities]);

const totalItems = useMemo(() => data.length, [data]);

const pageCount = useMemo(
() => Math.ceil(totalItems / pageSize),
[pageSize, totalItems]
);
const data = useMemo(() => authorities.val?.items ?? [], [authorities]);

const table = useReactTable<AuthorityListItem>({
data: data ?? ([] as AuthorityListItem[]),
columns,
Expand Down
113 changes: 63 additions & 50 deletions apps/stats-dapp/src/provider/hooks/useAuthorities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type AuthorityListItem = {
uptime: number;
reputation: number;
authorityId: string;
isBest: boolean;
};

/**
Expand Down Expand Up @@ -301,41 +302,49 @@ export function useAuthorities(

const authoritiesUptime = useAuthorityUptimesQuery({
pollInterval: 1000,
fetchPolicy: 'cache-and-network',
fetchPolicy: 'network-only',
});

const authoritiesUptimes = useMemo(() => {
return authoritiesUptime.data?.authorityUpTimes?.nodes;
}, [authoritiesUptime]);

const highestReputationScore = useMemo(() => {
if (authorityReputations.highestReputationScore !== -Infinity) {
return authorityReputations.highestReputationScore;
} else {
return 0;
}
}, [authorityReputations]);

const latestIndexedSessionId = useLatestSessionIdQuery({
pollInterval: 1000,
fetchPolicy: 'network-only',
});

const latestIndexedSessionIdNumber = useMemo(() => {
return Number(latestIndexedSessionId.data?.sessions?.totalCount ?? 0);
}, [latestIndexedSessionId]);

useEffect(() => {
if (
latestIndexedSessionId.data &&
latestIndexedSessionId.data.sessions?.totalCount
) {
call({
variables: {
offset: reqQuery.offset,
perPage: reqQuery.perPage,
sessionId:
String(latestIndexedSessionId.data?.sessions?.totalCount - 1) ??
'0',
},
}).catch((e) => {
setAuthorities({
val: null,
isFailed: true,
isLoading: false,
error: e.message,
});
call({
variables: {
offset: reqQuery.offset,
perPage: reqQuery.perPage,
sessionId:
latestIndexedSessionIdNumber === 0
? '0'
: String(latestIndexedSessionIdNumber - 4),
},
}).catch((e) => {
setAuthorities({
val: null,
isFailed: true,
isLoading: false,
error: e.message,
});
}
}, [reqQuery, call, latestIndexedSessionId]);
});
}, [reqQuery, call, latestIndexedSessionIdNumber]);

useEffect(() => {
const subscription = query.observable
Expand All @@ -356,11 +365,10 @@ export function useAuthorities(
location: auth.location ?? undefined,
uptime: uptime,
reputation: auth
? (auth.reputation /
authorityReputations.highestReputationScore) *
100
? (auth.reputation / highestReputationScore) * 100
: 0,
authorityId: auth.authorityId,
isBest: auth.isBest,
};
});
return {
Expand All @@ -385,7 +393,7 @@ export function useAuthorities(
})
.subscribe(setAuthorities);
return () => subscription.unsubscribe();
}, [query, authoritiesUptimes, authorityReputations]);
}, [query, authoritiesUptimes, highestReputationScore]);

return authorities;
}
Expand All @@ -409,11 +417,21 @@ export function useAuthority(pageQuery: AuthorityQuery): AuthorityDetails {
pollInterval: 1000,
fetchPolicy: 'network-only',
});
const latestIndexedSessionIdNumber = useMemo(() => {
return Number(latestIndexedSessionId.data?.sessions?.totalCount ?? 0);
}, [latestIndexedSessionId]);
const { authorityId } = pageQuery.filter;
const [callKeyGen, queryKeyGen] = useValidatorSessionsLazyQuery();
const [callValidatorOfSession, queryValidatorOfSession] =
useValidatorOfSessionLazyQuery();
const authorityReputations = useReputations();
const highestReputationScore = useMemo(() => {
if (authorityReputations.highestReputationScore !== -Infinity) {
return authorityReputations.highestReputationScore;
} else {
return 0;
}
}, [authorityReputations]);

useEffect(() => {
callKeyGen({
Expand All @@ -433,27 +451,24 @@ export function useAuthority(pageQuery: AuthorityQuery): AuthorityDetails {
});
}, [authorityId, callKeyGen, setKeyGens, pageQuery]);
useEffect(() => {
if (
latestIndexedSessionId.data &&
latestIndexedSessionId.data.sessions?.totalCount
) {
callValidatorOfSession({
variables: {
sessionValidatorId: `${
latestIndexedSessionId.data?.sessions?.totalCount - 1
}-${authorityId}`,
validatorId: authorityId,
},
}).catch((e) => {
setStats({
val: null,
isFailed: true,
isLoading: false,
error: e.message,
});
callValidatorOfSession({
variables: {
sessionValidatorId: `${
latestIndexedSessionIdNumber === 0
? 0
: latestIndexedSessionIdNumber - 4
}-${authorityId}`,
validatorId: authorityId,
},
}).catch((e) => {
setStats({
val: null,
isFailed: true,
isLoading: false,
error: e.message,
});
}
}, [latestIndexedSessionId, callValidatorOfSession, authorityId]);
});
}, [latestIndexedSessionIdNumber, callValidatorOfSession, authorityId]);
useEffect(() => {
const subscription = queryKeyGen.observable
.map((res): AuthorityDetails['keyGens'] => {
Expand Down Expand Up @@ -538,9 +553,7 @@ export function useAuthority(pageQuery: AuthorityQuery): AuthorityDetails {
inTheSet: auth.isBest,
},
reputation:
(Number(auth.reputation) /
authorityReputations.highestReputationScore) *
100,
(Number(auth.reputation) / highestReputationScore) * 100,
uptime: Number(auth?.uptime ?? 0) * Math.pow(10, -7),
};
return {
Expand All @@ -559,7 +572,7 @@ export function useAuthority(pageQuery: AuthorityQuery): AuthorityDetails {
})
.subscribe(setStats);
return () => subscription.unsubscribe();
}, [queryValidatorOfSession, authorityReputations]);
}, [queryValidatorOfSession, highestReputationScore]);
return useMemo(() => ({ stats, keyGens }), [stats, keyGens]);
}

Expand Down

0 comments on commit 5d66de9

Please sign in to comment.