Skip to content

Commit

Permalink
Add stream latency calculation from neighbour RTTs
Browse files Browse the repository at this point in the history
  • Loading branch information
tumppi committed Feb 4, 2025
1 parent d0ea60f commit 0dcaf15
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/components/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Interval } from './Graphs/Graphs'
import { Intervals } from './Graphs/Intervals'
import { TimeSeries } from './Graphs/TimeSeries'
import { useStore } from '../Store'
import { getNeighbors } from '../getters'

type StatProps = {
id: string
Expand Down Expand Up @@ -264,8 +265,24 @@ function useStreamStatsQuery(chainId: number, streamId: string) {

const { messagesPerSecond, peerCount } = stream

const neighbors = await getNeighbors({
streamId: stream.id,
chainId,
})

const validRTTs = neighbors
.map((n) => n.rtt)
.filter((rtt): rtt is number => typeof rtt === 'number' && rtt > 0)

// Calculate average one-way latency from neighbors with valid RTT.
// Latency is the average RTT of neighbors in the stream, divided by 2.
const latency =
validRTTs.length > 0
? validRTTs.reduce((sum, rtt) => sum + rtt, 0) / validRTTs.length / 2
: undefined

return {
latency: undefined as undefined | number,
latency,
messagesPerSecond,
peerCount,
}
Expand Down Expand Up @@ -297,7 +314,7 @@ export function StreamStats({ streamId }: StreamStatsProps) {
<Stat
id="latency"
label="Latency ms"
value={latency == null ? undefined : latency.toFixed(2)}
value={latency == null ? undefined : latency.toFixed(0)}
/>
</Stats>
)
Expand Down
4 changes: 3 additions & 1 deletion src/generated/gql/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type Neighbor = {
__typename?: 'Neighbor';
nodeId1: Scalars['String']['output'];
nodeId2: Scalars['String']['output'];
rtt?: Maybe<Scalars['Int']['output']>;
streamPartId: Scalars['String']['output'];
};

Expand Down Expand Up @@ -171,7 +172,7 @@ export type GetNeighborsQueryVariables = Exact<{
}>;


export type GetNeighborsQuery = { __typename?: 'Query', neighbors: { __typename?: 'Neighbors', cursor?: string | null, items: Array<{ __typename?: 'Neighbor', streamPartId: string, nodeId1: string, nodeId2: string }> } };
export type GetNeighborsQuery = { __typename?: 'Query', neighbors: { __typename?: 'Neighbors', cursor?: string | null, items: Array<{ __typename?: 'Neighbor', streamPartId: string, nodeId1: string, nodeId2: string, rtt?: number | null }> } };


export const GetNodesDocument = gql`
Expand Down Expand Up @@ -229,6 +230,7 @@ export const GetNeighborsDocument = gql`
streamPartId
nodeId1
nodeId2
rtt
}
cursor
}
Expand Down
5 changes: 4 additions & 1 deletion src/getters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ interface GetNeighborsParams {
export async function getNeighbors(params: GetNeighborsParams): Promise<Neighbour[]> {
const pageSize = 1000

const { node, streamPartitionId, chainId } = params
const { node, streamId, streamPartitionId, chainId } = params

const items: Neighbour[] = []

Expand All @@ -95,6 +95,7 @@ export async function getNeighbors(params: GetNeighborsParams): Promise<Neighbou
cursor,
node,
pageSize,
streamId,
streamPart: streamPartitionId,
},
})
Expand All @@ -103,6 +104,7 @@ export async function getNeighbors(params: GetNeighborsParams): Promise<Neighbou
nodeId1: a,
nodeId2: b,
streamPartId: finalStreamPartitionId,
rtt,
} of neighbors.items) {
const pair = [a, b].sort() as [string, string]

Expand All @@ -120,6 +122,7 @@ export async function getNeighbors(params: GetNeighborsParams): Promise<Neighbou
nodeId0,
nodeId1,
streamPartitionId: finalStreamPartitionId,
rtt: rtt ?? undefined,
})
}

Expand Down
1 change: 1 addition & 0 deletions src/queries/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ gql`
streamPartId
nodeId1
nodeId2
rtt
}
cursor
}
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface Neighbour {
nodeId0: string
nodeId1: string
streamPartitionId: string
rtt?: number
}

interface Stream {
Expand Down

0 comments on commit 0dcaf15

Please sign in to comment.