|
| 1 | +import { getRedshiftPool } from "../src/app/services/analytics/redshift-client"; |
| 2 | + |
| 3 | +async function testAPIExplorerRedshift() { |
| 4 | + const domain = process.argv[2] || "buildwithfern.com"; |
| 5 | + const pool = getRedshiftPool(); |
| 6 | + |
| 7 | + const endDate = new Date(); |
| 8 | + const startDate = new Date(); |
| 9 | + startDate.setDate(startDate.getDate() - 7); |
| 10 | + |
| 11 | + console.log("Testing API Explorer Redshift queries for:", domain); |
| 12 | + console.log("Date range:", startDate.toISOString(), "to", endDate.toISOString()); |
| 13 | + console.log("\n"); |
| 14 | + |
| 15 | + try { |
| 16 | + // Check event counts |
| 17 | + console.log("=== Event Counts ==="); |
| 18 | + |
| 19 | + const sentCountQuery = ` |
| 20 | + SELECT COUNT(*) as count |
| 21 | + FROM posthog.events |
| 22 | + WHERE |
| 23 | + event = 'api_playground_request_sent' |
| 24 | + AND ( |
| 25 | + properties."$host"::VARCHAR = $1 |
| 26 | + OR properties."$host"::VARCHAR = $2 |
| 27 | + ) |
| 28 | + AND timestamp >= $3 |
| 29 | + AND timestamp < $4 |
| 30 | + `; |
| 31 | + |
| 32 | + const receivedCountQuery = ` |
| 33 | + SELECT COUNT(*) as count |
| 34 | + FROM posthog.events |
| 35 | + WHERE |
| 36 | + event = 'api_playground_request_received' |
| 37 | + AND ( |
| 38 | + properties."$host"::VARCHAR = $1 |
| 39 | + OR properties."$host"::VARCHAR = $2 |
| 40 | + ) |
| 41 | + AND timestamp >= $3 |
| 42 | + AND timestamp < $4 |
| 43 | + `; |
| 44 | + |
| 45 | + const [sentCountResult, receivedCountResult] = await Promise.all([ |
| 46 | + pool.query(sentCountQuery, [domain, `www.${domain}`, startDate.toISOString(), endDate.toISOString()]), |
| 47 | + pool.query(receivedCountQuery, [domain, `www.${domain}`, startDate.toISOString(), endDate.toISOString()]) |
| 48 | + ]); |
| 49 | + |
| 50 | + const sentCount = parseInt(sentCountResult.rows[0].count); |
| 51 | + const receivedCount = parseInt(receivedCountResult.rows[0].count); |
| 52 | + console.log(`api_playground_request_sent: ${sentCount} events`); |
| 53 | + console.log(`api_playground_request_received: ${receivedCount} events`); |
| 54 | + console.log( |
| 55 | + `Missing received events: ${sentCount - receivedCount} (${(((sentCount - receivedCount) / sentCount) * 100).toFixed(1)}%)` |
| 56 | + ); |
| 57 | + |
| 58 | + // Check response status distribution in received events |
| 59 | + console.log("\n=== Response Status Distribution ==="); |
| 60 | + const statusQuery = ` |
| 61 | + SELECT |
| 62 | + JSON_SERIALIZE(properties) as props_json |
| 63 | + FROM posthog.events |
| 64 | + WHERE |
| 65 | + event = 'api_playground_request_received' |
| 66 | + AND ( |
| 67 | + properties."$host"::VARCHAR = $1 |
| 68 | + OR properties."$host"::VARCHAR = $2 |
| 69 | + ) |
| 70 | + AND timestamp >= $3 |
| 71 | + AND timestamp < $4 |
| 72 | + `; |
| 73 | + |
| 74 | + const statusResult = await pool.query(statusQuery, [ |
| 75 | + domain, |
| 76 | + `www.${domain}`, |
| 77 | + startDate.toISOString(), |
| 78 | + endDate.toISOString() |
| 79 | + ]); |
| 80 | + |
| 81 | + const statusCounts: Record<string, number> = {}; |
| 82 | + for (const row of statusResult.rows) { |
| 83 | + const props = JSON.parse(row.props_json); |
| 84 | + const status = props.responseStatus; |
| 85 | + const statusRange = |
| 86 | + status >= 500 ? "5xx" : status >= 400 ? "4xx" : status >= 300 ? "3xx" : status >= 200 ? "2xx" : "other"; |
| 87 | + statusCounts[statusRange] = (statusCounts[statusRange] || 0) + 1; |
| 88 | + } |
| 89 | + |
| 90 | + console.log("Status code distribution:"); |
| 91 | + Object.entries(statusCounts).forEach(([range, count]) => { |
| 92 | + console.log(` ${range}: ${count} (${((count / receivedCount) * 100).toFixed(1)}%)`); |
| 93 | + }); |
| 94 | + |
| 95 | + // Run actual aggregation |
| 96 | + console.log("\n=== Running Aggregation ==="); |
| 97 | + |
| 98 | + // Common WHERE clause (identical for both queries) |
| 99 | + const commonWhereClause = ` |
| 100 | + ( |
| 101 | + properties."$host"::VARCHAR = $1 |
| 102 | + OR properties."$host"::VARCHAR = $2 |
| 103 | + ) |
| 104 | + AND timestamp >= $3 |
| 105 | + AND timestamp < $4 |
| 106 | + `; |
| 107 | + |
| 108 | + const sentQuery = ` |
| 109 | + SELECT JSON_SERIALIZE(properties) as props_json |
| 110 | + FROM posthog.events |
| 111 | + WHERE |
| 112 | + event = 'api_playground_request_sent' |
| 113 | + AND ${commonWhereClause} |
| 114 | + `; |
| 115 | + |
| 116 | + const receivedQuery = ` |
| 117 | + SELECT JSON_SERIALIZE(properties) as props_json |
| 118 | + FROM posthog.events |
| 119 | + WHERE |
| 120 | + event = 'api_playground_request_received' |
| 121 | + AND ${commonWhereClause} |
| 122 | + `; |
| 123 | + |
| 124 | + const [sentResult, receivedResult] = await Promise.all([ |
| 125 | + pool.query(sentQuery, [domain, `www.${domain}`, startDate.toISOString(), endDate.toISOString()]), |
| 126 | + pool.query(receivedQuery, [domain, `www.${domain}`, startDate.toISOString(), endDate.toISOString()]) |
| 127 | + ]); |
| 128 | + |
| 129 | + // Build docsRoute mapping |
| 130 | + const docsRouteToEndpoint = new Map<string, { endpointRoute: string; endpointName: string; method: string }>(); |
| 131 | + for (const row of sentResult.rows) { |
| 132 | + const props = JSON.parse(row.props_json); |
| 133 | + const docsRoute = props.docsRoute || ""; |
| 134 | + const endpointRoute = props.endpointRoute || ""; |
| 135 | + const endpointName = props.endpointName || ""; |
| 136 | + const method = props.method || ""; |
| 137 | + if (docsRoute) { |
| 138 | + docsRouteToEndpoint.set(docsRoute, { endpointRoute, endpointName, method }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Aggregate by endpointRoute |
| 143 | + const counts = new Map< |
| 144 | + string, |
| 145 | + { method: string; endpoint: string; name: string; count: number; numSuccesses: number; numFailures: number } |
| 146 | + >(); |
| 147 | + |
| 148 | + for (const row of sentResult.rows) { |
| 149 | + const props = JSON.parse(row.props_json); |
| 150 | + const method = props.method || ""; |
| 151 | + const endpointRoute = props.endpointRoute || ""; |
| 152 | + const endpointName = props.endpointName || ""; |
| 153 | + |
| 154 | + const key = `${method}|${endpointRoute}|${endpointName}`; |
| 155 | + const existing = counts.get(key) || { |
| 156 | + method, |
| 157 | + endpoint: endpointRoute, |
| 158 | + name: endpointName, |
| 159 | + count: 0, |
| 160 | + numSuccesses: 0, |
| 161 | + numFailures: 0 |
| 162 | + }; |
| 163 | + existing.count++; |
| 164 | + counts.set(key, existing); |
| 165 | + } |
| 166 | + |
| 167 | + // Add status codes from received events |
| 168 | + for (const row of receivedResult.rows) { |
| 169 | + const props = JSON.parse(row.props_json); |
| 170 | + const docsRoute = props.docsRoute || ""; |
| 171 | + const responseStatus = props.responseStatus; |
| 172 | + |
| 173 | + const mappedEndpoint = docsRouteToEndpoint.get(docsRoute); |
| 174 | + if (!mappedEndpoint) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + |
| 178 | + const key = `${mappedEndpoint.method}|${mappedEndpoint.endpointRoute}|${mappedEndpoint.endpointName}`; |
| 179 | + const existing = counts.get(key); |
| 180 | + |
| 181 | + if (existing) { |
| 182 | + if (responseStatus >= 200 && responseStatus < 300) { |
| 183 | + existing.numSuccesses++; |
| 184 | + } else if (responseStatus >= 400) { |
| 185 | + existing.numFailures++; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + // Show top endpoints |
| 191 | + console.log("\n=== Top 10 Endpoints ==="); |
| 192 | + const sorted = Array.from(counts.values()) |
| 193 | + .sort((a, b) => b.count - a.count) |
| 194 | + .slice(0, 10); |
| 195 | + |
| 196 | + sorted.forEach((data, idx) => { |
| 197 | + const unaccounted = data.count - data.numSuccesses - data.numFailures; |
| 198 | + const unaccountedPct = ((unaccounted / data.count) * 100).toFixed(1); |
| 199 | + console.log(`\n${idx + 1}. ${data.method} ${data.endpoint || data.name}`); |
| 200 | + console.log(` Total: ${data.count}`); |
| 201 | + console.log(` Successes (2xx): ${data.numSuccesses}`); |
| 202 | + console.log(` Failures (4xx/5xx): ${data.numFailures}`); |
| 203 | + console.log(` Unaccounted (missing/3xx): ${unaccounted} (${unaccountedPct}%)`); |
| 204 | + }); |
| 205 | + |
| 206 | + // Summary |
| 207 | + console.log("\n=== SUMMARY ==="); |
| 208 | + const totalRequests = Array.from(counts.values()).reduce((sum, d) => sum + d.count, 0); |
| 209 | + const totalSuccesses = Array.from(counts.values()).reduce((sum, d) => sum + d.numSuccesses, 0); |
| 210 | + const totalFailures = Array.from(counts.values()).reduce((sum, d) => sum + d.numFailures, 0); |
| 211 | + const totalUnaccounted = totalRequests - totalSuccesses - totalFailures; |
| 212 | + |
| 213 | + console.log(`Total requests (sent): ${totalRequests}`); |
| 214 | + console.log( |
| 215 | + `Total successes (2xx): ${totalSuccesses} (${((totalSuccesses / totalRequests) * 100).toFixed(1)}%)` |
| 216 | + ); |
| 217 | + console.log( |
| 218 | + `Total failures (4xx/5xx): ${totalFailures} (${((totalFailures / totalRequests) * 100).toFixed(1)}%)` |
| 219 | + ); |
| 220 | + console.log( |
| 221 | + `Total unaccounted: ${totalUnaccounted} (${((totalUnaccounted / totalRequests) * 100).toFixed(1)}%)` |
| 222 | + ); |
| 223 | + console.log(`\nPossible reasons for unaccounted requests:`); |
| 224 | + console.log(` - Request timeout (no response received)`); |
| 225 | + console.log(` - Network error (no response received)`); |
| 226 | + console.log(` - 3xx redirects (not counted as success or failure)`); |
| 227 | + console.log(` - 1xx informational responses`); |
| 228 | + console.log(` - Response not logged to PostHog`); |
| 229 | + |
| 230 | + await pool.end(); |
| 231 | + console.log("\n✓ Test completed!"); |
| 232 | + process.exit(0); |
| 233 | + } catch (error) { |
| 234 | + console.error("❌ Error:", error); |
| 235 | + await pool.end(); |
| 236 | + process.exit(1); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +testAPIExplorerRedshift(); |
0 commit comments