@@ -88,7 +88,12 @@ import {
8888 fetchReplicaUtilizationHistory ,
8989 ReplicaUtilizationHistoryParameters ,
9090} from "~/api/materialize/cluster/replicaUtilizationHistory" ;
91- import { fetchObjectLagHistory } from "~/api/materialize/freshness/lagHistory" ;
91+ import {
92+ calculateBucketSizeFromLookback ,
93+ fetchLatestLag ,
94+ fetchObjectLagHistory ,
95+ LATEST_READING_INTERVAL_MS ,
96+ } from "~/api/materialize/freshness/lagHistory" ;
9297import { assertNoMoreThanOneRow } from "~/api/materialize/MoreThanOneRowError" ;
9398import { fetchOwners } from "~/api/materialize/owners" ;
9499import { useSubscribe } from "~/api/materialize/useSubscribe" ;
@@ -185,16 +190,28 @@ export const clusterQueryKeys = {
185190 ...clusterQueryKeys . all ( ) ,
186191 buildQueryKeyPart ( "replicaOfflineEvents" , params ) ,
187192 ] as const ,
188- clusterFreshness : ( params : { lookbackMs : number ; objectIds : string [ ] } ) =>
193+ // Sorted so these keys track set membership, not the order the objects
194+ // arrived in.
195+ clusterFreshnessSeries : ( params : {
196+ lookbackMs : number ;
197+ objectIds : string [ ] ;
198+ } ) =>
189199 [
190200 ...clusterQueryKeys . all ( ) ,
191- // Sorted so the key tracks set membership, not the order the objects
192- // arrived in.
193- buildQueryKeyPart ( "clusterFreshness" , {
201+ buildQueryKeyPart ( "clusterFreshnessSeries" , {
194202 lookbackMs : params . lookbackMs ,
195203 objectIds : [ ...params . objectIds ] . sort ( ) . join ( "," ) ,
196204 } ) ,
197205 ] as const ,
206+ // No lookback: the newest reading is the newest reading whatever range is
207+ // being graphed, so changing the range must not refetch it.
208+ clusterFreshnessLatest : ( params : { objectIds : string [ ] } ) =>
209+ [
210+ ...clusterQueryKeys . all ( ) ,
211+ buildQueryKeyPart ( "clusterFreshnessLatest" , {
212+ objectIds : [ ...params . objectIds ] . sort ( ) . join ( "," ) ,
213+ } ) ,
214+ ] as const ,
198215 replicaUtilization : ( ) =>
199216 [
200217 ...clusterQueryKeys . all ( ) ,
@@ -980,9 +997,10 @@ export interface FreshnessObject {
980997 objectType : string ;
981998}
982999
983- type LagQueryResults = Awaited < ReturnType < typeof fetchObjectLagHistory > > ;
984- type LagReading = LagQueryResults [ 0 ] [ "rows" ] [ number ] ;
985- type LatestReading = LagQueryResults [ 1 ] [ "rows" ] [ number ] ;
1000+ type LagReading = Awaited <
1001+ ReturnType < typeof fetchObjectLagHistory >
1002+ > [ "rows" ] [ number ] ;
1003+ type LatestReading = Awaited < ReturnType < typeof fetchLatestLag > > [ "rows" ] [ number ] ;
9861004
9871005export interface LagQueryRows {
9881006 /** One binned point per object per bin, each the worst reading in its span. */
@@ -1094,14 +1112,6 @@ export function buildFreshnessData(
10941112 } ;
10951113}
10961114
1097- /**
1098- * Lag readings for a known set of objects, shaped for the freshness graph.
1099- *
1100- * The caller supplies the objects rather than naming a cluster, because the
1101- * `useAllObjects` subscribe already holds every name, schema, database and
1102- * type. Resolving those in SQL cost three joins and three full scans per
1103- * request; here they are a map lookup.
1104- */
11051115/**
11061116 * Lag readings for a known set of objects, shaped for the freshness graph.
11071117 *
@@ -1119,32 +1129,61 @@ export function useClusterFreshness({
11191129 [ objects ] ,
11201130 ) ;
11211131
1122- const query = useSuspenseQuery ( {
1123- queryKey : clusterQueryKeys . clusterFreshness ( { lookbackMs, objectIds } ) ,
1124- queryFn : async ( { queryKey, signal } ) : Promise < LagQueryRows > => {
1125- if ( objectIds . length === 0 ) return { readings : [ ] , latest : [ ] } ;
1132+ // The binned series and the latest readings go in separate requests because
1133+ // they go stale at different rates. A bin cannot change faster than its own
1134+ // width, which at a 24 hour range is 24 minutes, so refetching the series on
1135+ // the one minute clock would re-read the whole window to change at most one
1136+ // point.
1137+ const binSizeMs = calculateBucketSizeFromLookback ( lookbackMs ) ;
1138+
1139+ const series = useSuspenseQuery ( {
1140+ queryKey : clusterQueryKeys . clusterFreshnessSeries ( {
1141+ lookbackMs,
1142+ objectIds,
1143+ } ) ,
1144+ queryFn : async ( { queryKey, signal } ) : Promise < LagReading [ ] > => {
1145+ if ( objectIds . length === 0 ) return [ ] ;
11261146
1127- const [ series , latest ] = await fetchObjectLagHistory ( {
1147+ const { rows } = await fetchObjectLagHistory ( {
11281148 objectIds,
11291149 lookbackMs,
11301150 requestOptions : { signal } ,
11311151 queryKey,
11321152 } ) ;
1133- return { readings : series . rows , latest : latest . rows } ;
1153+ return rows ;
11341154 } ,
1135- // A reading lands once a minute, so a refetch inside that window re-asks a
1136- // question whose answer cannot have changed, and an interval any longer
1137- // than it leaves an open page behind the data it is describing.
1138- staleTime : 60_000 ,
1139- refetchInterval : 60_000 ,
1155+ staleTime : binSizeMs ,
1156+ refetchInterval : binSizeMs ,
1157+ } ) ;
1158+
1159+ const latest = useSuspenseQuery ( {
1160+ queryKey : clusterQueryKeys . clusterFreshnessLatest ( { objectIds } ) ,
1161+ queryFn : async ( { queryKey, signal } ) : Promise < LatestReading [ ] > => {
1162+ if ( objectIds . length === 0 ) return [ ] ;
1163+
1164+ const { rows } = await fetchLatestLag ( {
1165+ objectIds,
1166+ requestOptions : { signal } ,
1167+ queryKey,
1168+ } ) ;
1169+ return rows ;
1170+ } ,
1171+ // A reading lands once a minute, so anything shorter re-asks a question
1172+ // whose answer cannot have changed.
1173+ staleTime : LATEST_READING_INTERVAL_MS ,
1174+ refetchInterval : LATEST_READING_INTERVAL_MS ,
11401175 } ) ;
11411176
1142- // Only `data` is returned. Spreading the query object would make every
1177+ // Only `data` is returned. Spreading a query object would make every
11431178 // consumer observe all of its state, and suspense already covers loading and
11441179 // errors for these callers.
11451180 const data = useMemo (
1146- ( ) => buildFreshnessData ( query . data , objects ) ,
1147- [ query . data , objects ] ,
1181+ ( ) =>
1182+ buildFreshnessData (
1183+ { readings : series . data , latest : latest . data } ,
1184+ objects ,
1185+ ) ,
1186+ [ series . data , latest . data , objects ] ,
11481187 ) ;
11491188
11501189 return { data } ;
0 commit comments