Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display DKG best authorities in authorities table #1815

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
uptime: number;
reputation: number;
authorityId: string;
isBest: boolean;
};

/**
Expand Down Expand Up @@ -301,41 +302,49 @@

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 @@
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 @@
})
.subscribe(setAuthorities);
return () => subscription.unsubscribe();
}, [query, authoritiesUptimes, authorityReputations]);
}, [query, authoritiesUptimes, highestReputationScore]);

return authorities;
}
Expand All @@ -409,11 +417,21 @@
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 @@
});
}, [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 @@ -516,7 +531,7 @@
const sessionValidators = res.data.sessionValidators;
const counter = sessionValidators?.aggregates?.distinctCount
?.id as number;
const session = sessionValidator.session!;

Check warning on line 534 in apps/stats-dapp/src/provider/hooks/useAuthorities.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Forbidden non-null assertion
const thresholds = thresholdMap(session.thresholds);
const keyGen = thresholds.KEY_GEN;

Expand All @@ -538,9 +553,7 @@
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 @@
})
.subscribe(setStats);
return () => subscription.unsubscribe();
}, [queryValidatorOfSession, authorityReputations]);
}, [queryValidatorOfSession, highestReputationScore]);
return useMemo(() => ({ stats, keyGens }), [stats, keyGens]);
}

Expand Down Expand Up @@ -598,7 +611,7 @@
if (res.data.sessions) {
const items: SessionThresholdEntry[] = res.data.sessions.nodes.map(
(node) => {
const thresholds = thresholdMap(node!.thresholds);

Check warning on line 614 in apps/stats-dapp/src/provider/hooks/useAuthorities.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Forbidden non-null assertion
const keyGen = thresholds.KEY_GEN;
const signature = thresholds.SIGNATURE;

Expand Down
Loading