-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
236 lines (220 loc) · 7.19 KB
/
index.js
File metadata and controls
236 lines (220 loc) · 7.19 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
#!/usr/bin/env node
// Multi-app Moesif aggregation script.
// Usage example:
// node index.js \
// --tokens TOKEN1,TOKEN2 \
// --from 2025-10-28T21:35:47.000Z \
// --to 2025-11-28T22:35:47.000Z \
// --interval 1d \
// --timeZone America/Los_Angeles \
// --cumulative true
// Outputs JSON mapping app -> time series buckets.
const { URL } = require('url');
const https = require('https');
function parseArgs(argv) {
const args = {};
for (let i = 2; i < argv.length; i++) {
const part = argv[i];
if (part.startsWith('--')) {
const [key, value] = part.includes('=') ? part.split('=') : [part, argv[i + 1]];
const cleanKey = key.replace(/^--/, '');
if (!part.includes('=')) i++; // consumed next token as value
args[cleanKey] = value;
}
}
return args;
}
function decodeJwtApp(token) {
try {
const segments = token.split('.');
if (segments.length < 2) return 'unknown';
const payloadSeg = segments[1];
const normalized = payloadSeg.replace(/-/g, '+').replace(/_/g, '/');
const padded = normalized + '='.repeat((4 - (normalized.length % 4)) % 4);
const json = Buffer.from(padded, 'base64').toString('utf8');
const payload = JSON.parse(json);
return payload.app || payload.app_id || payload.application || 'unknown';
} catch (e) {
return 'unknown';
}
}
function buildRequestBody(interval, timeZone, cumulative) {
// Since each token is already scoped to a single app, remove terms aggregation on app_id.
const dateHistogram = {
date_histogram: {
field: 'request.time',
interval: interval,
time_zone: timeZone
},
aggs: {
'_id|cardinality': {
cardinality: { field: '_id' }
}
}
};
if (cumulative) {
dateHistogram.aggs['_id|cardinality|accumulate'] = {
cumulative_sum: { buckets_path: '_id|cardinality' }
};
}
return {
aggs: {
entTmSrs: {
filter: { match_all: {} },
aggs: {
'request.time': dateHistogram,
'_id|cardinality': { cardinality: { field: '_id' } }
}
}
},
size: 0
};
}
function httpPostJson(urlString, token, body) {
return new Promise((resolve, reject) => {
const url = new URL(urlString);
const data = JSON.stringify(body);
const options = {
method: 'POST',
hostname: url.hostname,
path: url.pathname + url.search,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
const req = https.request(options, (res) => {
let chunks = '';
res.on('data', (d) => (chunks += d));
res.on('end', () => {
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
try {
resolve(JSON.parse(chunks));
} catch (e) {
reject(new Error('Invalid JSON response'));
}
} else {
reject(new Error(`HTTP ${res.statusCode}: ${chunks}`));
}
});
});
req.on('error', reject);
req.write(data);
req.end();
});
}
async function fetchForToken(baseUrl, token, from, to, interval, timeZone, cumulative) {
const app = decodeJwtApp(token);
const queryUrl = `${baseUrl}?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}`;
const body = buildRequestBody(interval, timeZone, cumulative);
try {
const json = await httpPostJson(queryUrl, token, body);
return { app, json };
} catch (err) {
return { app, error: err.message };
}
}
function transformResponse(app, json, cumulative, debug) {
if (!json) {
return { app, points: [], status: 'empty-response', raw: debug ? json : undefined };
}
// Graceful no-data: if no aggregations but hits exist, consider it no-data.
if (!json.aggregations && json.hits) {
let totalHits = null;
try {
if (typeof json.hits.total === 'number') totalHits = json.hits.total;
else if (json.hits.total && typeof json.hits.total.value === 'number') totalHits = json.hits.total.value;
} catch (_) {}
return { app, points: [], status: 'no-data', total_hits: totalHits };
}
const agg = json.aggregations;
let totalCard = null;
let timeBuckets = [];
let pathUsed = null;
// Path 1: current simplified structure entTmSrs -> request.time
if (agg && agg.entTmSrs && agg.entTmSrs['request.time']) {
const timeAgg = agg.entTmSrs['request.time'];
pathUsed = 'aggregations.entTmSrs["request.time"]';
if (agg.entTmSrs['_id|cardinality']) {
totalCard = agg.entTmSrs['_id|cardinality'].value;
}
timeBuckets = timeAgg.buckets || [];
}
// Path 2: legacy structure with app_id terms
else if (agg && agg.entTmSrs && agg.entTmSrs.app_id && Array.isArray(agg.entTmSrs.app_id.buckets)) {
pathUsed = 'aggregations.entTmSrs.app_id.buckets[*]["request.time"]';
for (const b of agg.entTmSrs.app_id.buckets) {
if (b['request.time']) {
if (b['_id|cardinality'] && totalCard === null) totalCard = b['_id|cardinality'].value;
timeBuckets.push(...(b['request.time'].buckets || []));
}
}
}
// Path 3: direct histogram without entTmSrs wrapper
else if (agg && agg['request.time']) {
pathUsed = 'aggregations["request.time"]';
const timeAgg = agg['request.time'];
timeBuckets = timeAgg.buckets || [];
if (agg['_id|cardinality']) totalCard = agg['_id|cardinality'].value;
}
if (!timeBuckets.length && pathUsed === null) {
const topKeys = Object.keys(json);
return {
app,
points: [],
status: 'unrecognized-format',
availableKeys: topKeys,
aggregationsKeys: agg ? Object.keys(agg) : undefined,
rawSample: debug ? JSON.stringify(json).slice(0, 1000) : undefined
};
}
const points = timeBuckets.map(t => ({
key: t.key_as_string || t.key,
cardinality: t['_id|cardinality'] ? t['_id|cardinality'].value : null,
cumulative: cumulative && t['_id|cardinality|accumulate'] ? t['_id|cardinality|accumulate'].value : null
}));
return { app, total_cardinality: totalCard, points, path: debug ? pathUsed : undefined, status: 'ok' };
}
async function main() {
const args = parseArgs(process.argv);
const tokensRaw = args.tokens;
if (!tokensRaw) {
console.error('Missing --tokens');
process.exit(1);
}
const from = args.from;
const to = args.to;
const interval = args.interval || '1d';
const timeZone = args.timeZone || 'UTC';
const cumulative = (args.cumulative || 'false').toLowerCase() === 'true';
const baseUrl = args.baseUrl || 'https://api.moesif.com/v1/search/~/count/events';
const debug = (args.debug || 'false').toLowerCase() === 'true';
if (!from || !to) {
console.error('Missing --from or --to');
process.exit(1);
}
const tokens = tokensRaw.split(',').map(s => s.trim()).filter(Boolean);
const results = [];
for (const token of tokens) {
// eslint-disable-next-line no-await-in-loop
const fetched = await fetchForToken(baseUrl, token, from, to, interval, timeZone, cumulative);
if (fetched.error) {
results.push({ app: fetched.app, error: fetched.error });
} else {
const transformed = transformResponse(fetched.app, fetched.json, cumulative, debug);
results.push(transformed);
if (debug && transformed.error) {
console.error(`Debug (${fetched.app}): keys=${Object.keys(fetched.json)} pathUsed=${transformed.path}`);
}
}
}
const output = { from, to, interval, timeZone, cumulative, debug, apps: results };
console.log(JSON.stringify(output, null, 2));
}
if (require.main === module) {
main().catch(err => {
console.error('Fatal error:', err.message);
process.exit(1);
});
}