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

[FRONT-1544] Add stream stats badge to project listing tiles #1967

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 2 additions & 50 deletions src/components/Stats.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import React, { ReactNode } from 'react'
import styled from 'styled-components'
import { getIndexerClient } from '~/getters/getGraphClient'
import {
GetStreamsDocument,
GetStreamsQuery,
GetStreamsQueryVariables,
} from '../generated/gql/indexer'
import { defaultStreamStats } from '~/getters/getStreamStats'
import { useStreamStatsQuery } from '~/hooks/useStreamStats'

type StatProps = {
id: string
Expand Down Expand Up @@ -88,53 +83,10 @@ const ButtonGrid = styled.div`
}
`

function useStreamStatsQuery(streamId: string) {
return useQuery({
queryKey: ['useStreamStatsQuery', streamId],
queryFn: async () => {
const client = getIndexerClient(137)

if (!client) {
return defaultStreamStats
}

const {
data: { streams },
} = await client.query<GetStreamsQuery, GetStreamsQueryVariables>({
query: GetStreamsDocument,
variables: {
streamIds: [streamId],
first: 1,
},
})

const [stream = undefined] = streams.items

if (!stream) {
return null
}

const { messagesPerSecond, peerCount } = stream

return {
latency: undefined as undefined | number,
messagesPerSecond,
peerCount,
}
},
})
}

interface StreamStatsProps {
streamId: string
}

const defaultStreamStats = {
latency: undefined,
messagesPerSecond: undefined,
peerCount: undefined,
}

export function StreamStats({ streamId }: StreamStatsProps) {
const { data: stats } = useStreamStatsQuery(streamId)

Expand Down
44 changes: 44 additions & 0 deletions src/getters/getStreamStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getIndexerClient } from '~/getters/getGraphClient'
import {
GetStreamsDocument,
GetStreamsQuery,
GetStreamsQueryVariables,
} from '../generated/gql/indexer'

export const defaultStreamStats = {
latency: undefined,
messagesPerSecond: undefined,
peerCount: undefined,
}

export const getStreamStats = async (streamId: string) => {
const client = getIndexerClient(137)

if (!client) {
return defaultStreamStats
}

const {
data: { streams },
} = await client.query<GetStreamsQuery, GetStreamsQueryVariables>({
query: GetStreamsDocument,
variables: {
streamIds: [streamId],
first: 1,
},
})

const [stream = undefined] = streams.items

if (!stream) {
return null
}

const { messagesPerSecond, peerCount } = stream

return {
latency: undefined as undefined | number,
messagesPerSecond,
peerCount,
}
}
47 changes: 47 additions & 0 deletions src/hooks/useStreamStats.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useQuery } from '@tanstack/react-query'
import { getStreamStats } from '~/getters/getStreamStats'

type StreamStats = {
latency: number | undefined
messagesPerSecond: number
peerCount: number
}

export function useStreamStatsQuery(streamId: string) {
return useQuery({
queryKey: ['useStreamStatsQuery', streamId],
queryFn: async () => {
return getStreamStats(streamId)
},
})
}

export function useMultipleStreamStatsQuery(streamIds: string[]) {
return useQuery({
queryKey: ['useMultipleStreamStatsQuery', streamIds],
queryFn: async () => {
const stats = (await Promise.all(
streamIds.map(getStreamStats),
)) as StreamStats[]
return stats.reduce(
(acc: StreamStats, curr: StreamStats) => ({
// For latency, we can take the average of non-undefined values
latency:
acc.latency === undefined && curr.latency === undefined
? undefined
: ((acc.latency || 0) + (curr.latency || 0)) /
(acc.latency !== undefined && curr.latency !== undefined
? 2
: 1),
messagesPerSecond: acc.messagesPerSecond + curr.messagesPerSecond,
peerCount: acc.peerCount + curr.peerCount,
}),
{
latency: undefined,
messagesPerSecond: 0,
peerCount: 0,
},
)
},
})
}
59 changes: 58 additions & 1 deletion src/shared/components/Tile/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { ReactNode } from 'react'
import styled, { css } from 'styled-components'
import Link from '~/shared/components/Link'
import { useMultipleStreamStatsQuery } from '~/hooks/useStreamStats'

const SingleBadge = styled.div`
display: flex;
Expand All @@ -26,6 +27,7 @@ const SingleBadge = styled.div`
margin-left: 8px;
}
`

type BadgeContainerProps = {
children: ReactNode
top?: boolean
Expand Down Expand Up @@ -134,4 +136,59 @@ const DataUnionBadge = ({

const BadgeLink = ({ ...props }) => <Link {...props} />

export { DataUnionBadge }
const StatsBadge = styled.div`
display: flex;
align-items: center;
padding: 4px 8px;
gap: 10px;
background: rgba(245, 245, 247, 0.6);
backdrop-filter: blur(13.3871px);
border-radius: 8px;

font-weight: 500;
font-size: 16px;
line-height: 24px;
color: #323232;

a,
a:link,
a:active,
a:focus,
a:hover,
a:visited {
color: white !important;
}

> * + * {
margin-left: 8px;
}
`

interface StreamStatsBadgeProps extends Omit<BadgeContainerProps, 'children'> {
streamIds: string[]
}

const StreamStatsBadge = ({ streamIds, ...props }: StreamStatsBadgeProps) => {
const { data: stats, isLoading, error } = useMultipleStreamStatsQuery(streamIds)

if (error || isLoading) {
return null
}

const messagesPerSecond = stats?.messagesPerSecond
const formattedMsgRate =
typeof messagesPerSecond === 'number' ? messagesPerSecond.toFixed(1) : 'N/A'

return (
<BadgeContainer {...props}>
<StatsBadge>
<span>
{streamIds.length} {streamIds.length === 1 ? 'Stream' : 'Streams'}
</span>
<span>{formattedMsgRate} Msg/s</span>
</StatsBadge>
</BadgeContainer>
)
}

export { DataUnionBadge, StreamStatsBadge }
5 changes: 3 additions & 2 deletions src/shared/components/Tile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useCurrentChainId } from '~/utils/chains'
import { Route as R, routeOptions } from '~/utils/routes'
import { useCurrentChainSymbolicName } from '~/utils/chains'
import Summary from './Summary'
import { DataUnionBadge } from './Badge'
import { DataUnionBadge, StreamStatsBadge } from './Badge'

const Image = styled(Img)`
img& {
Expand Down Expand Up @@ -212,10 +212,11 @@ function MarketplaceProductTile({
/>
</TileImageContainer>
</Link>
<StreamStatsBadge top left streamIds={product.streams} />
{!!showDataUnionBadge && (
<DataUnionBadge
top
left
right
linkTo={R.projectOverview(
product.id,
routeOptions(chainName, undefined, 'stats'),
Expand Down
Loading