|
| 1 | +import { get } from 'data/fetchers' |
| 2 | +import { generateRegexpWhere } from './Reports.constants' |
| 3 | +import { ReportFilterItem } from './Reports.types' |
| 4 | +import { useQueries } from '@tanstack/react-query' |
| 5 | +import * as Sentry from '@sentry/nextjs' |
| 6 | + |
| 7 | +export const SHARED_API_REPORT_SQL = { |
| 8 | + totalRequests: { |
| 9 | + queryType: 'logs', |
| 10 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 11 | + --reports-api-total-requests |
| 12 | + select |
| 13 | + cast(timestamp_trunc(t.timestamp, hour) as datetime) as timestamp, |
| 14 | + count(t.id) as count |
| 15 | + FROM ${src} t |
| 16 | + cross join unnest(metadata) as m |
| 17 | + cross join unnest(m.response) as response |
| 18 | + cross join unnest(m.request) as request |
| 19 | + cross join unnest(request.headers) as headers |
| 20 | + ${generateRegexpWhere(filters)} |
| 21 | + GROUP BY |
| 22 | + timestamp |
| 23 | + ORDER BY |
| 24 | + timestamp ASC`, |
| 25 | + }, |
| 26 | + topRoutes: { |
| 27 | + queryType: 'logs', |
| 28 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 29 | + -- reports-api-top-routes |
| 30 | + select |
| 31 | + request.path as path, |
| 32 | + request.method as method, |
| 33 | + request.search as search, |
| 34 | + response.status_code as status_code, |
| 35 | + count(t.id) as count |
| 36 | + from ${src} t |
| 37 | + cross join unnest(metadata) as m |
| 38 | + cross join unnest(m.response) as response |
| 39 | + cross join unnest(m.request) as request |
| 40 | + cross join unnest(request.headers) as headers |
| 41 | + ${generateRegexpWhere(filters)} |
| 42 | + group by |
| 43 | + request.path, request.method, request.search, response.status_code |
| 44 | + order by |
| 45 | + count desc |
| 46 | + limit 10 |
| 47 | + `, |
| 48 | + }, |
| 49 | + errorCounts: { |
| 50 | + queryType: 'logs', |
| 51 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 52 | + -- reports-api-error-counts |
| 53 | + select |
| 54 | + cast(timestamp_trunc(t.timestamp, hour) as datetime) as timestamp, |
| 55 | + count(t.id) as count |
| 56 | + FROM ${src} t |
| 57 | + cross join unnest(metadata) as m |
| 58 | + cross join unnest(m.response) as response |
| 59 | + cross join unnest(m.request) as request |
| 60 | + cross join unnest(request.headers) as headers |
| 61 | + WHERE |
| 62 | + response.status_code >= 400 |
| 63 | + ${generateRegexpWhere(filters, false)} |
| 64 | + GROUP BY |
| 65 | + timestamp |
| 66 | + ORDER BY |
| 67 | + timestamp ASC |
| 68 | + `, |
| 69 | + }, |
| 70 | + topErrorRoutes: { |
| 71 | + queryType: 'logs', |
| 72 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 73 | + -- reports-api-top-error-routes |
| 74 | + select |
| 75 | + request.path as path, |
| 76 | + request.method as method, |
| 77 | + request.search as search, |
| 78 | + response.status_code as status_code, |
| 79 | + count(t.id) as count |
| 80 | + from ${src} t |
| 81 | + cross join unnest(metadata) as m |
| 82 | + cross join unnest(m.response) as response |
| 83 | + cross join unnest(m.request) as request |
| 84 | + cross join unnest(request.headers) as headers |
| 85 | + where |
| 86 | + response.status_code >= 400 |
| 87 | + ${generateRegexpWhere(filters, false)} |
| 88 | + group by |
| 89 | + request.path, request.method, request.search, response.status_code |
| 90 | + order by |
| 91 | + count desc |
| 92 | + limit 10 |
| 93 | + `, |
| 94 | + }, |
| 95 | + responseSpeed: { |
| 96 | + queryType: 'logs', |
| 97 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 98 | + -- reports-api-response-speed |
| 99 | + select |
| 100 | + cast(timestamp_trunc(t.timestamp, hour) as datetime) as timestamp, |
| 101 | + avg(response.origin_time) as avg |
| 102 | + FROM |
| 103 | + ${src} t |
| 104 | + cross join unnest(metadata) as m |
| 105 | + cross join unnest(m.response) as response |
| 106 | + cross join unnest(m.request) as request |
| 107 | + cross join unnest(request.headers) as headers |
| 108 | + ${generateRegexpWhere(filters)} |
| 109 | + GROUP BY |
| 110 | + timestamp |
| 111 | + ORDER BY |
| 112 | + timestamp ASC |
| 113 | + `, |
| 114 | + }, |
| 115 | + topSlowRoutes: { |
| 116 | + queryType: 'logs', |
| 117 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 118 | + -- reports-api-top-slow-routes |
| 119 | + select |
| 120 | + request.path as path, |
| 121 | + request.method as method, |
| 122 | + request.search as search, |
| 123 | + response.status_code as status_code, |
| 124 | + count(t.id) as count, |
| 125 | + avg(response.origin_time) as avg |
| 126 | + from ${src} t |
| 127 | + cross join unnest(metadata) as m |
| 128 | + cross join unnest(m.response) as response |
| 129 | + cross join unnest(m.request) as request |
| 130 | + cross join unnest(request.headers) as headers |
| 131 | + ${generateRegexpWhere(filters)} |
| 132 | + group by |
| 133 | + request.path, request.method, request.search, response.status_code |
| 134 | + order by |
| 135 | + avg desc |
| 136 | + limit 10 |
| 137 | + `, |
| 138 | + }, |
| 139 | + networkTraffic: { |
| 140 | + queryType: 'logs', |
| 141 | + sql: (filters: ReportFilterItem[], src = 'edge_logs') => ` |
| 142 | + -- reports-api-network-traffic |
| 143 | + select |
| 144 | + cast(timestamp_trunc(t.timestamp, hour) as datetime) as timestamp, |
| 145 | + coalesce( |
| 146 | + safe_divide( |
| 147 | + sum( |
| 148 | + cast(coalesce(headers.content_length, "0") as int64) |
| 149 | + ), |
| 150 | + 1000000 |
| 151 | + ), |
| 152 | + 0 |
| 153 | + ) as ingress_mb, |
| 154 | + coalesce( |
| 155 | + safe_divide( |
| 156 | + sum( |
| 157 | + cast(coalesce(resp_headers.content_length, "0") as int64) |
| 158 | + ), |
| 159 | + 1000000 |
| 160 | + ), |
| 161 | + 0 |
| 162 | + ) as egress_mb, |
| 163 | + FROM |
| 164 | + ${src} t |
| 165 | + cross join unnest(metadata) as m |
| 166 | + cross join unnest(m.response) as response |
| 167 | + cross join unnest(m.request) as request |
| 168 | + cross join unnest(request.headers) as headers |
| 169 | + cross join unnest(response.headers) as resp_headers |
| 170 | + ${generateRegexpWhere(filters)} |
| 171 | + GROUP BY |
| 172 | + timestamp |
| 173 | + ORDER BY |
| 174 | + timestamp ASC |
| 175 | + `, |
| 176 | + }, |
| 177 | +} |
| 178 | + |
| 179 | +export type SharedAPIReportKey = keyof typeof SHARED_API_REPORT_SQL |
| 180 | + |
| 181 | +const fetchLogs = async ({ |
| 182 | + projectRef, |
| 183 | + sql, |
| 184 | + start, |
| 185 | + end, |
| 186 | +}: { |
| 187 | + projectRef: string |
| 188 | + sql: string |
| 189 | + start: string |
| 190 | + end: string |
| 191 | +}) => { |
| 192 | + const { data, error } = await get(`/platform/projects/{ref}/analytics/endpoints/logs.all`, { |
| 193 | + params: { |
| 194 | + path: { ref: projectRef }, |
| 195 | + query: { |
| 196 | + sql, |
| 197 | + iso_timestamp_start: start, |
| 198 | + iso_timestamp_end: end, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }) |
| 202 | + |
| 203 | + if (error || data?.error) { |
| 204 | + Sentry.captureException({ |
| 205 | + message: 'Shared API Report Error', |
| 206 | + data: { |
| 207 | + error, |
| 208 | + data, |
| 209 | + }, |
| 210 | + }) |
| 211 | + throw error || data?.error |
| 212 | + } |
| 213 | + |
| 214 | + return data |
| 215 | +} |
| 216 | + |
| 217 | +type SharedAPIReportParams = { |
| 218 | + src: string |
| 219 | + filters: ReportFilterItem[] |
| 220 | + start: string |
| 221 | + end: string |
| 222 | + projectRef: string |
| 223 | + enabled?: boolean |
| 224 | +} |
| 225 | +export const useSharedAPIReport = ({ |
| 226 | + src = 'edge_logs', |
| 227 | + filters, |
| 228 | + start, |
| 229 | + end, |
| 230 | + projectRef, |
| 231 | + enabled = true, |
| 232 | +}: SharedAPIReportParams) => { |
| 233 | + const queries = useQueries({ |
| 234 | + queries: Object.entries(SHARED_API_REPORT_SQL).map(([key, value]) => ({ |
| 235 | + queryKey: ['shared-api-report', key, src, filters, start, end, projectRef], |
| 236 | + enabled, |
| 237 | + queryFn: () => |
| 238 | + fetchLogs({ |
| 239 | + projectRef, |
| 240 | + sql: value.sql(filters, src), |
| 241 | + start, |
| 242 | + end, |
| 243 | + }), |
| 244 | + })), |
| 245 | + }) |
| 246 | + |
| 247 | + const keys = Object.keys(SHARED_API_REPORT_SQL) as Array<keyof typeof SHARED_API_REPORT_SQL> |
| 248 | + |
| 249 | + const data = keys.reduce( |
| 250 | + (acc, key, i) => { |
| 251 | + acc[key] = queries[i].data?.result || [] |
| 252 | + return acc |
| 253 | + }, |
| 254 | + {} as { [K in keyof typeof SHARED_API_REPORT_SQL]: unknown[] } |
| 255 | + ) |
| 256 | + |
| 257 | + const error = keys.reduce( |
| 258 | + (acc, key, i) => { |
| 259 | + acc[key] = queries[i].error as string |
| 260 | + return acc |
| 261 | + }, |
| 262 | + {} as { [K in keyof typeof SHARED_API_REPORT_SQL]: string } |
| 263 | + ) |
| 264 | + |
| 265 | + const isLoading = keys.reduce( |
| 266 | + (acc, key, i) => { |
| 267 | + acc[key] = queries[i].isLoading |
| 268 | + return acc |
| 269 | + }, |
| 270 | + {} as { [K in keyof typeof SHARED_API_REPORT_SQL]: boolean } |
| 271 | + ) |
| 272 | + |
| 273 | + return { |
| 274 | + data, |
| 275 | + error, |
| 276 | + isLoading, |
| 277 | + } |
| 278 | +} |
0 commit comments