|
1 | 1 | import type { FastifyInstance } from "fastify"; |
2 | 2 | import { CACHE_TTL } from "../config.js"; |
| 3 | +import { weightedQos } from "@info/shared/utils"; |
3 | 4 | import { gqlSafe } from "../graphql/client.js"; |
4 | 5 | import { fetchAllSpecs, fetchProvidersForSpec } from "../rpc/lava.js"; |
5 | 6 | import { readHealthSummaryForSpec, readHealthByProviderForSpec } from "../services/health-store.js"; |
@@ -144,34 +145,110 @@ export async function specRoutes(app: FastifyInstance) { |
144 | 145 | }); |
145 | 146 |
|
146 | 147 | // 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. |
147 | 150 | 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 | + }, |
149 | 163 | config: { cacheTTL: CACHE_TTL.LIST }, |
150 | 164 | }, async (request) => { |
151 | 165 | 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); |
152 | 194 |
|
153 | 195 | const data = await gqlSafe<{ |
154 | 196 | 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 | + }>; |
156 | 201 | }; |
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 } |
162 | 207 | } |
| 208 | + orderBy: DATE_ASC |
| 209 | + ) { |
| 210 | + nodes { date cu relays qosSyncW qosAvailW qosLatencyW qosWeight } |
163 | 211 | } |
164 | | - }`, { chainId: specId }, null); |
| 212 | + }`, { chainId: specId, from, to }, null); |
165 | 213 |
|
166 | 214 | if (!data) return { data: [] }; |
167 | 215 |
|
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 }; |
175 | 252 | }); |
176 | 253 |
|
177 | 254 | } |
0 commit comments