Skip to content

Commit 7b57145

Browse files
committed
feat(api): time-series + single-object summary for /specs/:specId/charts
The endpoint previously ignored from/to and always returned a one-element array of the chain's alltime totals. The chain page's ChainChart fed that into a daily plotter and got one point (nothing visible), and its StatCards expected the summary response to be a single object not an array. Align with /index/charts: - no date params → { data: { chainId, cu, relays } } alltime - with from/to → { data: [{ date, cu, relays, qosSync, ... }] } Per-date rollup combines the (date, chain, provider) MV rows into one point per day with weighted QoS.
1 parent c877418 commit 7b57145

1 file changed

Lines changed: 92 additions & 15 deletions

File tree

‎apps/api/src/routes/specs.ts‎

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FastifyInstance } from "fastify";
22
import { CACHE_TTL } from "../config.js";
3+
import { weightedQos } from "@info/shared/utils";
34
import { gqlSafe } from "../graphql/client.js";
45
import { fetchAllSpecs, fetchProvidersForSpec } from "../rpc/lava.js";
56
import { readHealthSummaryForSpec, readHealthByProviderForSpec } from "../services/health-store.js";
@@ -144,34 +145,110 @@ export async function specRoutes(app: FastifyInstance) {
144145
});
145146

146147
// GET /specs/:specId/charts — indexer GraphQL (materialized view)
148+
// No date params → single alltime summary { chainId, cu, relays }.
149+
// With from/to → daily time-series for the chain, with weighted QoS.
147150
app.get<{ Params: { specId: string } }>("/:specId/charts", {
148-
schema: { ...specIdSchema, tags: ["Specs"], summary: "Alltime CU/relays for a chain" },
151+
schema: {
152+
...specIdSchema,
153+
tags: ["Specs"],
154+
summary: "Chain relay charts — alltime summary or daily time-series with QoS",
155+
querystring: {
156+
type: "object" as const,
157+
properties: {
158+
from: { type: "string" as const, description: "Start date (YYYY-MM-DD). Default: 90 days ago" },
159+
to: { type: "string" as const, description: "End date (YYYY-MM-DD). Default: today" },
160+
},
161+
},
162+
},
149163
config: { cacheTTL: CACHE_TTL.LIST },
150164
}, async (request) => {
151165
const { specId } = request.params;
166+
const q = request.query as Record<string, string>;
167+
168+
if (!q.from && !q.to) {
169+
const data = await gqlSafe<{
170+
allMvRelayDailies: {
171+
aggregates: { sum: { cu: string; relays: string } };
172+
};
173+
} | null>(`query($chainId: String!) {
174+
allMvRelayDailies(filter: { chainId: { equalTo: $chainId } }) {
175+
aggregates { sum { cu relays } }
176+
}
177+
}`, { chainId: specId }, null);
178+
179+
if (!data) return { data: { chainId: specId, cu: null, relays: null } };
180+
181+
return {
182+
data: {
183+
chainId: specId,
184+
cu: data.allMvRelayDailies.aggregates.sum.cu,
185+
relays: data.allMvRelayDailies.aggregates.sum.relays,
186+
},
187+
};
188+
}
189+
190+
const to = q.to ? q.to : new Date().toISOString().slice(0, 10);
191+
const from = q.from
192+
? q.from
193+
: new Date(Date.now() - 90 * 24 * 60 * 60 * 1000).toISOString().slice(0, 10);
152194

153195
const data = await gqlSafe<{
154196
allMvRelayDailies: {
155-
groupedAggregates: Array<{ keys: string[]; sum: { cu: string; relays: string } }>;
197+
nodes: Array<{
198+
date: string; cu: string; relays: string;
199+
qosSyncW: number | null; qosAvailW: number | null; qosLatencyW: number | null; qosWeight: string;
200+
}>;
156201
};
157-
} | null>(`query($chainId: String!) {
158-
allMvRelayDailies(filter: { chainId: { equalTo: $chainId } }) {
159-
groupedAggregates(groupBy: CHAIN_ID) {
160-
keys
161-
sum { cu relays }
202+
} | null>(`query($chainId: String!, $from: Date!, $to: Date!) {
203+
allMvRelayDailies(
204+
filter: {
205+
chainId: { equalTo: $chainId }
206+
date: { greaterThanOrEqualTo: $from, lessThanOrEqualTo: $to }
162207
}
208+
orderBy: DATE_ASC
209+
) {
210+
nodes { date cu relays qosSyncW qosAvailW qosLatencyW qosWeight }
163211
}
164-
}`, { chainId: specId }, null);
212+
}`, { chainId: specId, from, to }, null);
165213

166214
if (!data) return { data: [] };
167215

168-
return {
169-
data: data.allMvRelayDailies.groupedAggregates.map((g) => ({
170-
chainId: g.keys[0],
171-
cu: g.sum.cu,
172-
relays: g.sum.relays,
173-
})),
174-
};
216+
// Multiple providers produce one MV row per (date, chain, provider); roll up to per-date.
217+
const byDate = new Map<string, {
218+
cu: bigint; relays: bigint;
219+
qosSyncW: number; qosAvailW: number; qosLatW: number; qosWeight: number;
220+
}>();
221+
222+
for (const n of data.allMvRelayDailies.nodes) {
223+
const existing = byDate.get(n.date);
224+
if (existing) {
225+
existing.cu += BigInt(n.cu);
226+
existing.relays += BigInt(n.relays);
227+
existing.qosSyncW += n.qosSyncW ?? 0;
228+
existing.qosAvailW += n.qosAvailW ?? 0;
229+
existing.qosLatW += n.qosLatencyW ?? 0;
230+
existing.qosWeight += Number(n.qosWeight);
231+
} else {
232+
byDate.set(n.date, {
233+
cu: BigInt(n.cu),
234+
relays: BigInt(n.relays),
235+
qosSyncW: n.qosSyncW ?? 0,
236+
qosAvailW: n.qosAvailW ?? 0,
237+
qosLatW: n.qosLatencyW ?? 0,
238+
qosWeight: Number(n.qosWeight),
239+
});
240+
}
241+
}
242+
243+
const result = Array.from(byDate.entries()).map(([date, v]) => ({
244+
date,
245+
cu: v.cu.toString(),
246+
relays: v.relays.toString(),
247+
...weightedQos(v.qosSyncW, v.qosAvailW, v.qosLatW, v.qosWeight),
248+
}));
249+
250+
result.sort((a, b) => a.date.localeCompare(b.date));
251+
return { data: result };
175252
});
176253

177254
}

0 commit comments

Comments
 (0)