Repository navigation
Expand file tree
/
Copy pathtest-analytics.js
More file actions
258 lines (219 loc) Β· 6.54 KB
/
Copy pathtest-analytics.js
File metadata and controls
258 lines (219 loc) Β· 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env node
/**
* API Analytics Endpoint Test Script
*
* This script demonstrates how to use the API analytics endpoints
* without requiring a running database.
*/
console.log('π§ͺ API Usage Analytics - Endpoint Testing Guide\n');
console.log('='.repeat(60));
// Test 1: Make regular API requests
console.log('\nπ STEP 1: Generate API Usage Data');
console.log('-'.repeat(60));
console.log(`
When the application is running, make requests to any endpoints:
curl http://localhost:3000/api/transactions
curl http://localhost:3000/api/webhooks
curl http://localhost:3000/api/enrichment
Each request will be automatically logged to the analytics database.
`);
// Test 2: Check summary analytics
console.log('\nπ STEP 2: Get Analytics Summary');
console.log('-'.repeat(60));
console.log(`
Retrieve aggregated metrics for the last 24 hours:
curl -H "x-admin: true" \\
http://localhost:3000/admin/api-usage/summary
Expected response:
{
"success": true,
"data": {
"totalRequests": 1523,
"averageResponseTime": 145.32,
"requestsByRoute": [
{
"route": "/api/transactions",
"method": "GET",
"count": 487,
"avgDuration": 234
},
{
"route": "/api/transactions/search",
"method": "GET",
"count": 412,
"avgDuration": 567
}
],
"requestsByStatusCode": [
{
"statusCode": 200,
"count": 1489
},
{
"statusCode": 400,
"count": 34
}
],
"topUsers": [
{
"userId": "user-123",
"requestCount": 89
},
{
"userId": "user-456",
"requestCount": 67
}
]
}
}
`);
// Test 3: Get detailed logs
console.log('\nπ STEP 3: Get Detailed Request Logs');
console.log('-'.repeat(60));
console.log(`
Retrieve individual request logs with optional filtering:
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/raw?limit=10&offset=0"
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/raw?route=/api/transactions&statusCode=200"
Expected response:
{
"success": true,
"data": [
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"route": "/api/transactions",
"method": "GET",
"userId": "user-123",
"durationMs": 123,
"statusCode": 200,
"userAgent": "curl/7.68.0",
"ipAddress": "127.0.0.1",
"createdAt": "2026-01-25T15:30:00.000Z"
},
{
"id": "550e8400-e29b-41d4-a716-446655440001",
"route": "/api/transactions/search",
"method": "POST",
"userId": "user-456",
"durationMs": 567,
"statusCode": 200,
"userAgent": "Mozilla/5.0...",
"ipAddress": "192.168.1.1",
"createdAt": "2026-01-25T15:29:45.000Z"
}
],
"meta": {
"total": 1523,
"limit": 10,
"offset": 0
}
}
`);
// Test 4: Advanced filtering
console.log('\nπ STEP 4: Advanced Log Filtering');
console.log('-'.repeat(60));
console.log(`
Query parameters for detailed logs endpoint:
?limit=100 - Number of records (default: 100)
?offset=0 - Pagination offset (default: 0)
?route=/api/... - Filter by specific endpoint
?method=GET - Filter by HTTP method (GET, POST, PUT, DELETE, PATCH)
?statusCode=500 - Filter by status code (e.g., 500 for errors)
?hoursBack=48 - Time window in hours (default: 24)
Examples:
# Get only failed requests (500 errors)
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/raw?statusCode=500"
# Get slow transactions endpoint requests
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/raw?route=/api/transactions&hoursBack=48"
# Get POST requests from past 7 days
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/raw?method=POST&hoursBack=168"
# Paginate through 50 records at a time
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/raw?limit=50&offset=0"
`);
// Test 5: Summary with different time windows
console.log('\nβ° STEP 5: Summary Analytics - Time Windows');
console.log('-'.repeat(60));
console.log(`
You can specify different time windows for summary analytics:
# Last 1 hour
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/summary?hoursBack=1"
# Last 24 hours (default)
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/summary?hoursBack=24"
# Last 7 days
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/summary?hoursBack=168"
# Last 30 days
curl -H "x-admin: true" \\
"http://localhost:3000/admin/api-usage/summary?hoursBack=720"
`);
// Test 6: Authentication note
console.log('\nπ STEP 6: Authentication');
console.log('-'.repeat(60));
console.log(`
All analytics endpoints require admin access.
Current test implementation: "x-admin: true" header
In production, replace with:
- JWT token with admin claims
- API key with admin permissions
- Custom authentication scheme
All requests are protected by AdminGuard.
`);
// Test 7: Real-world use cases
console.log('\nπ‘ STEP 7: Real-World Use Cases');
console.log('-'.repeat(60));
console.log(`
1. Identify Slow Endpoints
β Check 'requestsByRoute' in summary
β Look for routes with high 'avgDuration'
2. Monitor Error Rates
β Check 'requestsByStatusCode' in summary
β Query raw logs filtering by statusCode >= 400
3. Track User Activity
β Check 'topUsers' in summary
β Query raw logs filtering by specific userId
4. Debug Performance Issues
β Filter by route and statusCode
β Look at durationMs and timestamps
β Correlate with infrastructure metrics
5. Capacity Planning
β Monitor 'totalRequests' trend over days/weeks
β Identify peak usage times
β Plan scaling based on patterns
6. Feature Usage Analysis
β Track requests to specific feature endpoints
β Understand adoption rates
β Identify unused endpoints
`);
// Test 8: Database information
console.log('\nπΎ STEP 8: Database Schema');
console.log('-'.repeat(60));
console.log(`
The api_usage_logs table stores:
- id (UUID)
- route (VARCHAR 255)
- method (VARCHAR 10)
- userId (UUID, nullable)
- durationMs (INT)
- statusCode (INT)
- userAgent (TEXT)
- ipAddress (VARCHAR 45)
- createdAt (TIMESTAMP)
Indices are created on:
- route
- method
- userId
- createdAt
- (route, method) - composite
Automatic cleanup: Logs older than 30 days are removed daily at 2 AM.
`);
console.log('\n' + '='.repeat(60));
console.log('β
Analytics system is ready to use!');
console.log('='.repeat(60) + '\n');
process.exit(0);