waba-devtools 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +859 -2
- package/bin/waba.js +4 -0
- package/lib/budget.js +275 -0
- package/lib/cli.js +503 -0
- package/lib/commands.js +936 -0
- package/lib/compare.js +400 -0
- package/lib/config.js +280 -0
- package/lib/csv.js +778 -0
- package/lib/errors.js +68 -0
- package/lib/files.js +112 -0
- package/lib/graph.js +264 -0
- package/lib/index.js +29 -0
- package/lib/normalize.js +278 -0
- package/lib/plan.js +110 -0
- package/lib/report.js +328 -0
- package/package.json +64 -5
package/bin/waba.js
ADDED
package/lib/budget.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { UsageError } from './errors.js';
|
|
2
|
+
import { templateKey } from './normalize.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Meta lets a WABA create at most 100 templates per hour.
|
|
6
|
+
*/
|
|
7
|
+
export const META_CREATES_PER_HOUR = 100;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Share (percent) of Meta's limits `deploy` uses by default: of the template creates per hour and of
|
|
11
|
+
* the API allowance in `X-Business-Use-Case-Usage`. 80 stays 20 % below Meta's limits.
|
|
12
|
+
*/
|
|
13
|
+
export const DEFAULT_LIMIT_PERCENT = 80;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Graph error code when Meta throttles the app for a WABA (business use case rate limit).
|
|
17
|
+
*/
|
|
18
|
+
export const THROTTLED_CODE = 80_008;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Wait after error 80008 when Meta gives no estimate: its rate-limit window is one hour.
|
|
22
|
+
*/
|
|
23
|
+
export const DEFAULT_THROTTLE_MINUTES = 60;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Reports warn about Meta's API usage from this percentage on (or from the limit, when lower).
|
|
27
|
+
*/
|
|
28
|
+
export const USAGE_WARN_PERCENT = 50;
|
|
29
|
+
|
|
30
|
+
const HOUR = 3_600_000;
|
|
31
|
+
|
|
32
|
+
// Writes sent in one go free their slots one right after the other: slots less than a minute apart
|
|
33
|
+
// (the schedule shows minutes) are one batch, sent when the last of them is free.
|
|
34
|
+
const SAME_BATCH_GAP = 60_000;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Template creates per WABA and hour for a limit percentage: `floor(100 × percent / 100)`.
|
|
38
|
+
*
|
|
39
|
+
* @param {number} limitPercent - 1 to 100.
|
|
40
|
+
* @returns {number}
|
|
41
|
+
*/
|
|
42
|
+
export function writesPerHour(limitPercent) {
|
|
43
|
+
return Math.floor((META_CREATES_PER_HOUR * validateLimitPercent(limitPercent)) / 100);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Why writing to a WABA must stop, from the highest usage Meta reported, or `undefined`: any
|
|
48
|
+
* percentage at or above the limit, or a time to regain access.
|
|
49
|
+
*
|
|
50
|
+
* @param {import('./graph.js').UsageInfo|undefined} usage
|
|
51
|
+
* @param {number} [limitPercent]
|
|
52
|
+
* @returns {string|undefined}
|
|
53
|
+
*/
|
|
54
|
+
export function usageStopReason(usage, limitPercent = DEFAULT_LIMIT_PERCENT) {
|
|
55
|
+
if (!usage) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (usage.regainAccessMinutes > 0) {
|
|
59
|
+
return `Meta throttles this WABA for about ${usage.regainAccessMinutes} min (X-Business-Use-Case-Usage)`;
|
|
60
|
+
}
|
|
61
|
+
const peak = Math.max(usage.callCount, usage.totalCputime, usage.totalTime);
|
|
62
|
+
return peak >= limitPercent
|
|
63
|
+
? `Meta reports ${peak} % of the hourly API allowance used (stop at ${limitPercent} %)`
|
|
64
|
+
: undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @typedef {object} RecentWrite
|
|
69
|
+
* @property {string} wabaId - WABA the template was created or edited in.
|
|
70
|
+
* @property {string} name
|
|
71
|
+
* @property {string} language
|
|
72
|
+
* @property {'create'|'update'} [action] - Default `create`. Only creates count against Meta's
|
|
73
|
+
* hourly limit; a logged `update` tells the budget that a recent `last_updated_time` was an edit.
|
|
74
|
+
* @property {string|number|Date} at - When it was sent: ISO text, `Date`, or Unix time in seconds
|
|
75
|
+
* or milliseconds.
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @typedef {object} WriteBudget
|
|
80
|
+
* @property {number} limit - Creates allowed per hour.
|
|
81
|
+
* @property {number} usedLastHour - Creates counted in the last hour.
|
|
82
|
+
* @property {number} left - What may still be sent now (never below 0).
|
|
83
|
+
* @property {number[]} times - Time (ms) of every counted write, oldest first.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Time in milliseconds from Meta's `last_updated_time` (`2026-11-27T12:55:37+0000` or Unix seconds)
|
|
88
|
+
* or a logged write time.
|
|
89
|
+
*
|
|
90
|
+
* @param {unknown} value
|
|
91
|
+
* @returns {number} `NaN` when it is not a time.
|
|
92
|
+
*/
|
|
93
|
+
export function parseTime(value) {
|
|
94
|
+
if (value instanceof Date) {
|
|
95
|
+
return value.getTime();
|
|
96
|
+
}
|
|
97
|
+
if (typeof value === 'number' || (typeof value === 'string' && /^\d+$/.test(value))) {
|
|
98
|
+
const number = Number(value);
|
|
99
|
+
// Unix seconds until the year 33658; anything larger is milliseconds.
|
|
100
|
+
return number < 1e12 ? number * 1000 : number;
|
|
101
|
+
}
|
|
102
|
+
return typeof value === 'string' ? Date.parse(value) : NaN;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Checks the `recentWrites` option of `deploy`.
|
|
107
|
+
*
|
|
108
|
+
* @param {unknown} recentWrites
|
|
109
|
+
* @returns {RecentWrite[]}
|
|
110
|
+
*/
|
|
111
|
+
export function validateRecentWrites(recentWrites) {
|
|
112
|
+
if (!Array.isArray(recentWrites)) {
|
|
113
|
+
throw new UsageError('recentWrites must be a list of {wabaId, name, language, at}.');
|
|
114
|
+
}
|
|
115
|
+
for (const [index, write] of recentWrites.entries()) {
|
|
116
|
+
const isValid =
|
|
117
|
+
typeof write?.wabaId === 'string' &&
|
|
118
|
+
typeof write.name === 'string' &&
|
|
119
|
+
typeof write.language === 'string' &&
|
|
120
|
+
Number.isFinite(parseTime(write.at)) &&
|
|
121
|
+
[undefined, 'create', 'update'].includes(write.action);
|
|
122
|
+
if (!isValid) {
|
|
123
|
+
throw new UsageError(
|
|
124
|
+
`recentWrites[${index}] needs wabaId, name, language, a valid time "at" and action "create" or "update" (optional).`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return recentWrites;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Checks the `limitPercent` option of `deploy`.
|
|
133
|
+
*
|
|
134
|
+
* @param {unknown} value
|
|
135
|
+
* @returns {number}
|
|
136
|
+
*/
|
|
137
|
+
export function validateLimitPercent(value) {
|
|
138
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > 100) {
|
|
139
|
+
throw new UsageError('The limit must be a whole percentage from 1 to 100.');
|
|
140
|
+
}
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* How many creates a target WABA had in the last hour, and how many are left. Meta's list cannot tell
|
|
146
|
+
* a create from an edit, so every live template whose `last_updated_time` is within the hour counts,
|
|
147
|
+
* unless the caller logged an update of it in that hour (and no create). Logged creates within the
|
|
148
|
+
* hour whose name + language is not in the list count too (e.g. created and deleted again).
|
|
149
|
+
*
|
|
150
|
+
* @param {object} input
|
|
151
|
+
* @param {number} input.limit
|
|
152
|
+
* @param {object[]} input.live - Live templates of the target (with `last_updated_time`).
|
|
153
|
+
* @param {RecentWrite[]} [input.recentWrites] - Logged writes of this target WABA.
|
|
154
|
+
* @param {number} input.now - Milliseconds.
|
|
155
|
+
* @returns {WriteBudget}
|
|
156
|
+
*/
|
|
157
|
+
export function writeBudget({ limit, live, recentWrites = [], now }) {
|
|
158
|
+
const isRecent = (time) => Number.isFinite(time) && time > now - HOUR && time <= now;
|
|
159
|
+
const server = live
|
|
160
|
+
.map((template) => ({
|
|
161
|
+
key: templateKey(template),
|
|
162
|
+
time: parseTime(template.last_updated_time),
|
|
163
|
+
}))
|
|
164
|
+
.filter(({ time }) => isRecent(time));
|
|
165
|
+
const writes = recentWrites
|
|
166
|
+
.map((write) => ({
|
|
167
|
+
key: templateKey(write),
|
|
168
|
+
time: parseTime(write.at),
|
|
169
|
+
isCreate: write.action !== 'update',
|
|
170
|
+
}))
|
|
171
|
+
.filter(({ time }) => isRecent(time));
|
|
172
|
+
const createdKeys = new Set(writes.filter(({ isCreate }) => isCreate).map(({ key }) => key));
|
|
173
|
+
const editedKeys = new Set(
|
|
174
|
+
writes
|
|
175
|
+
.filter(({ key, isCreate }) => !isCreate && !createdKeys.has(key))
|
|
176
|
+
.map(({ key }) => key),
|
|
177
|
+
);
|
|
178
|
+
const counted = server.filter(({ key }) => !editedKeys.has(key));
|
|
179
|
+
const serverKeys = new Set(server.map(({ key }) => key));
|
|
180
|
+
const logged = writes.filter(({ key, isCreate }) => isCreate && !serverKeys.has(key));
|
|
181
|
+
const times = [...counted, ...logged].map(({ time }) => time).toSorted((a, b) => a - b);
|
|
182
|
+
return {
|
|
183
|
+
limit,
|
|
184
|
+
usedLastHour: times.length,
|
|
185
|
+
left: Math.max(0, limit - times.length),
|
|
186
|
+
times,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* When the next batch can go: the last of the first slots that free up less than a minute apart, or
|
|
192
|
+
* the one that frees enough.
|
|
193
|
+
*
|
|
194
|
+
* @param {[number, number][]} window - Counted writes as [time, count], oldest first, not empty.
|
|
195
|
+
* @param {number} needed - Slots that must free up for everything still to send.
|
|
196
|
+
* @returns {number} Milliseconds.
|
|
197
|
+
*/
|
|
198
|
+
function nextBatchTime(window, needed) {
|
|
199
|
+
let [last, freed] = window[0];
|
|
200
|
+
for (const [at, count] of window.slice(1)) {
|
|
201
|
+
if (freed >= needed || at - last >= SAME_BATCH_GAP) {
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
freed += count;
|
|
205
|
+
last = at;
|
|
206
|
+
}
|
|
207
|
+
return last + HOUR;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Batch schedule for `wanted` creates under an hourly limit: send what fits now, then each
|
|
212
|
+
* time counted writes are one hour old, send as many as that frees. Slots that free up less than a
|
|
213
|
+
* minute apart are one batch, sent when the last one it needs is free. Pure; feed it a deploy
|
|
214
|
+
* report's `budget.limit` and `budget.countedAt`.
|
|
215
|
+
*
|
|
216
|
+
* @param {object} input
|
|
217
|
+
* @param {number} input.wanted - Writes still to send (whole number, 0 or more).
|
|
218
|
+
* @param {number} input.limit - Writes per hour (whole number, 1 or more).
|
|
219
|
+
* @param {(string|number|Date)[]} [input.countedAt] - Writes already counted in the last hour.
|
|
220
|
+
* @param {number|string|Date} [input.now] - Default: now.
|
|
221
|
+
* @returns {{at: string, count: number}[]} ISO time and number of writes per batch, earliest first.
|
|
222
|
+
*/
|
|
223
|
+
export function planBatches({ wanted, limit, countedAt = [], now = Date.now() }) {
|
|
224
|
+
if (!Number.isSafeInteger(wanted) || wanted < 0) {
|
|
225
|
+
throw new UsageError('planBatches: wanted must be a whole number of 0 or more.');
|
|
226
|
+
}
|
|
227
|
+
if (!Number.isSafeInteger(limit) || limit < 1) {
|
|
228
|
+
throw new UsageError('planBatches: limit must be a whole number of 1 or more.');
|
|
229
|
+
}
|
|
230
|
+
let time = parseTime(now);
|
|
231
|
+
if (!Number.isFinite(time)) {
|
|
232
|
+
throw new UsageError('planBatches: now is not a valid time.');
|
|
233
|
+
}
|
|
234
|
+
// Counted writes as [time, count], oldest first.
|
|
235
|
+
let window = countedAt
|
|
236
|
+
.map((value) => parseTime(value))
|
|
237
|
+
.filter((value) => Number.isFinite(value) && value > time - HOUR)
|
|
238
|
+
.toSorted((a, b) => a - b)
|
|
239
|
+
.map((value) => [value, 1]);
|
|
240
|
+
const batches = [];
|
|
241
|
+
let remaining = wanted;
|
|
242
|
+
while (remaining > 0) {
|
|
243
|
+
window = window.filter(([at]) => at + HOUR > time);
|
|
244
|
+
const used = window.reduce((sum, [, count]) => sum + count, 0);
|
|
245
|
+
const count = Math.min(limit - used, remaining);
|
|
246
|
+
if (count > 0) {
|
|
247
|
+
batches.push({ at: new Date(time).toISOString(), count });
|
|
248
|
+
window.push([time, count]);
|
|
249
|
+
remaining -= count;
|
|
250
|
+
}
|
|
251
|
+
// never empty here: either this batch is in it or the limit is used up
|
|
252
|
+
const free = limit - used - Math.max(0, count);
|
|
253
|
+
time = nextBatchTime(window, remaining - free);
|
|
254
|
+
}
|
|
255
|
+
return batches;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Time of the batch that sends write number `index` (0-based) of a `planBatches` schedule; the last
|
|
260
|
+
* batch's time when the index is beyond the schedule.
|
|
261
|
+
*
|
|
262
|
+
* @param {{at: string, count: number}[]} schedule
|
|
263
|
+
* @param {number} index
|
|
264
|
+
* @returns {string|undefined} ISO time, or `undefined` for an empty schedule.
|
|
265
|
+
*/
|
|
266
|
+
export function batchTime(schedule, index) {
|
|
267
|
+
let end = 0;
|
|
268
|
+
for (const batch of schedule) {
|
|
269
|
+
end += batch.count;
|
|
270
|
+
if (index < end) {
|
|
271
|
+
return batch.at;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return schedule.at(-1)?.at;
|
|
275
|
+
}
|