Skip to content

Commit 0e4d3a9

Browse files
committed
Display the # of *active* operators on the overview page
1 parent dffc063 commit 0e4d3a9

File tree

1 file changed

+108
-5
lines changed

1 file changed

+108
-5
lines changed

src/pages/NetworkOverviewPage.tsx

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ import { Separator } from '~/components/Separator'
1919
import { StatCell, StatGrid } from '~/components/StatGrid'
2020
import { OperatorIdCell } from '~/components/Table'
2121
import WalletPass from '~/components/WalletPass'
22+
import {
23+
GetStreamsDocument as GetIndexerStreamsDocument,
24+
GetStreamsQuery as GetIndexerStreamsQuery,
25+
GetStreamsQueryVariables as GetIndexerStreamsQueryVariables,
26+
StreamOrderBy as IndexerOrderBy,
27+
OrderDirection as IndexerOrderDirection,
28+
} from '~/generated/gql/indexer'
2229
import { OperatorDailyBucket } from '~/generated/gql/network'
2330
import {
2431
getNetworkStats,
2532
getOperatorDailyBuckets,
2633
getTimestampForChartPeriod,
2734
} from '~/getters'
2835
import { getDelegationStats } from '~/getters/getDelegationStats'
36+
import { getIndexerClient } from '~/getters/getGraphClient'
2937
import { getSponsorshipTokenInfo } from '~/getters/getSponsorshipTokenInfo'
3038
import {
3139
useDelegationsForWalletQuery,
@@ -71,30 +79,125 @@ export function NetworkOverviewPage() {
7179
function NetworkStats() {
7280
const currentChainId = useCurrentChainId()
7381

74-
const { data } = useQuery({
82+
const statsQuery = useQuery({
7583
queryKey: ['networkStats', currentChainId],
7684
async queryFn() {
7785
return getNetworkStats(currentChainId)
7886
},
7987
})
8088

89+
const operatorCountQuery = useActiveOperatorCountQuery()
90+
8191
return (
8292
<NetworkPageSegment title="Network stats">
8393
<Pad>
8494
<StatGrid>
8595
<StatCell label="Total stake">
86-
{data && (
87-
<SponsorshipDecimals abbr amount={data.totalStake} tooltip />
96+
{statsQuery.isLoading ? (
97+
<>&zwnj;</>
98+
) : (
99+
<>
100+
{statsQuery.data ? (
101+
<SponsorshipDecimals
102+
abbr
103+
amount={statsQuery.data.totalStake}
104+
tooltip
105+
/>
106+
) : (
107+
<>N/A</>
108+
)}
109+
</>
110+
)}
111+
</StatCell>
112+
<StatCell label="Sponsorships">
113+
{statsQuery.isLoading ? (
114+
<>&zwnj;</>
115+
) : (
116+
statsQuery.data?.sponsorshipsCount ?? <>N/A</>
117+
)}
118+
</StatCell>
119+
<StatCell label="Active Operators">
120+
{operatorCountQuery.isLoading ? (
121+
<>&zwnj;</>
122+
) : (
123+
operatorCountQuery.data ?? <>N/A</>
88124
)}
89125
</StatCell>
90-
<StatCell label="Sponsorships">{data?.sponsorshipsCount}</StatCell>
91-
<StatCell label="Operators">{data?.operatorsCount}</StatCell>
92126
</StatGrid>
93127
</Pad>
94128
</NetworkPageSegment>
95129
)
96130
}
97131

132+
function useActiveOperatorCountQuery() {
133+
const chainId = useCurrentChainId()
134+
135+
return useQuery({
136+
queryKey: ['useActiveOperatorCountQuery', chainId],
137+
queryFn: async () => {
138+
let cursor = '0'
139+
140+
const uniquenessGate: Record<string, true | undefined> = {}
141+
142+
let count = 0
143+
144+
const client = getIndexerClient(chainId)
145+
146+
if (!client) {
147+
return 0
148+
}
149+
150+
for (;;) {
151+
const {
152+
data: { streams },
153+
} = await client.query<
154+
GetIndexerStreamsQuery,
155+
GetIndexerStreamsQueryVariables
156+
>({
157+
fetchPolicy: 'network-only',
158+
query: GetIndexerStreamsDocument,
159+
variables: {
160+
first: 500,
161+
orderBy: IndexerOrderBy.MessagesPerSecond,
162+
orderDirection: IndexerOrderDirection.Desc,
163+
cursor,
164+
},
165+
})
166+
167+
const breakEarly = (() => {
168+
for (const item of streams.items) {
169+
if (!item.messagesPerSecond) {
170+
/**
171+
* Make the whole stream fetching skip streams that have no
172+
* data flowing through them.
173+
*/
174+
return true
175+
}
176+
177+
if (!uniquenessGate[item.id]) {
178+
if (/^[^/]+\/operator\/coordination$/.test(item.id)) {
179+
count += 1
180+
}
181+
}
182+
183+
uniquenessGate[item.id] = true
184+
}
185+
186+
return false
187+
})()
188+
189+
if (!streams.cursor || breakEarly) {
190+
break
191+
}
192+
193+
cursor = streams.cursor
194+
}
195+
196+
return count
197+
},
198+
})
199+
}
200+
98201
function MyOperatorSummary() {
99202
const wallet = useWalletAccount()
100203

0 commit comments

Comments
 (0)