quota-keepalive 0.1.10 → 0.1.11
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/package.json +1 -1
- package/src/inventory.js +28 -2
- package/src/runner.js +5 -2
- package/src/snapshot.js +2 -0
package/package.json
CHANGED
package/src/inventory.js
CHANGED
|
@@ -261,12 +261,37 @@ function historyByAccount(history) {
|
|
|
261
261
|
return out
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
|
|
264
|
+
function refreshedQuotaCard(card) {
|
|
265
|
+
if (!object(card) || object(card.quota)) return null
|
|
266
|
+
const provider = sanitize(card.provider, 80).trim()
|
|
267
|
+
const user = sanitize(card.user, 240).trim()
|
|
268
|
+
if (!provider || !user || sanitize(card.error, 300).trim()) return null
|
|
269
|
+
const windows = (Array.isArray(card.windows) ? card.windows : []).map(publicWindow)
|
|
270
|
+
const plan = sanitize(card.plan, 80)
|
|
271
|
+
const balance = typeof card.balance === "number" && Number.isFinite(card.balance)
|
|
272
|
+
? String(card.balance)
|
|
273
|
+
: sanitize(card.balance, 80)
|
|
274
|
+
if (!windows.length && !plan && !balance) return null
|
|
275
|
+
const readAt = sanitize(card.readAt || card.asOf || card.at, 40)
|
|
276
|
+
return {
|
|
277
|
+
key: cardKey(provider, user).toLowerCase(),
|
|
278
|
+
entry: {
|
|
279
|
+
at: readAt,
|
|
280
|
+
quota: { plan, balance, windows, readAt },
|
|
281
|
+
},
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function quotaByAccount(quotas, quotaCards = []) {
|
|
265
286
|
const out = new Map()
|
|
266
287
|
for (const [key, entry] of Object.entries(object(quotas) ? quotas : {})) {
|
|
267
288
|
if (!object(entry?.quota)) continue
|
|
268
289
|
out.set(key.toLowerCase(), entry)
|
|
269
290
|
}
|
|
291
|
+
for (const card of Array.isArray(quotaCards) ? quotaCards : []) {
|
|
292
|
+
const refreshed = refreshedQuotaCard(card)
|
|
293
|
+
if (refreshed) out.set(refreshed.key, refreshed.entry)
|
|
294
|
+
}
|
|
270
295
|
return out
|
|
271
296
|
}
|
|
272
297
|
|
|
@@ -581,6 +606,7 @@ export async function readInventory({
|
|
|
581
606
|
logPath = "",
|
|
582
607
|
records = [],
|
|
583
608
|
accountsData = null,
|
|
609
|
+
quotaCards = [],
|
|
584
610
|
} = {}) {
|
|
585
611
|
const [providers, checkins, quotas, histories, auth] = await Promise.all([
|
|
586
612
|
readJson(join(configHome, "plugin-providers.json"), []),
|
|
@@ -589,7 +615,7 @@ export async function readInventory({
|
|
|
589
615
|
readJson(join(configHome, "quota-history.json"), {}),
|
|
590
616
|
readJson(join(configHome, "plugin-auth.json"), {}),
|
|
591
617
|
])
|
|
592
|
-
const cards = quotaByAccount(quotas)
|
|
618
|
+
const cards = quotaByAccount(quotas, quotaCards)
|
|
593
619
|
const history = historyByAccount(histories)
|
|
594
620
|
const activity = activityIndex(records)
|
|
595
621
|
const sourceProviders = Array.isArray(providers) ? providers : []
|
package/src/runner.js
CHANGED
|
@@ -290,6 +290,7 @@ export async function runRefresh({
|
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
const accounts = []
|
|
293
|
+
const quotaCards = []
|
|
293
294
|
for (const id of providers) {
|
|
294
295
|
const { result, body } = await runJsonCommand({
|
|
295
296
|
args: ["quota", "refresh", id, "--json"],
|
|
@@ -298,6 +299,7 @@ export async function runRefresh({
|
|
|
298
299
|
timeoutMs: Math.max(30_000, Math.min(50_000, Number(config.timeoutMs) || 50_000)),
|
|
299
300
|
})
|
|
300
301
|
const classified = classifyRefreshResult(result, body)
|
|
302
|
+
if (Array.isArray(classified.cards)) quotaCards.push(...classified.cards)
|
|
301
303
|
accounts.push({
|
|
302
304
|
provider: id,
|
|
303
305
|
account: "",
|
|
@@ -324,7 +326,7 @@ export async function runRefresh({
|
|
|
324
326
|
console.warn(`[quota-keepalive] cannot write quota refresh log: ${errorText(error)}`)
|
|
325
327
|
})
|
|
326
328
|
}
|
|
327
|
-
await refreshSnapshot(config, null)
|
|
329
|
+
await refreshSnapshot(config, null, quotaCards)
|
|
328
330
|
const success = accounts.filter((item) => item.status === "success").length
|
|
329
331
|
const failed = accounts.filter((item) => item.status === "failed").length
|
|
330
332
|
const skipped = accounts.filter((item) => item.status === "skipped").length
|
|
@@ -358,13 +360,14 @@ async function logCommand(config, entry, run = newRunId()) {
|
|
|
358
360
|
})
|
|
359
361
|
}
|
|
360
362
|
|
|
361
|
-
async function refreshSnapshot(config, accountsData) {
|
|
363
|
+
async function refreshSnapshot(config, accountsData, quotaCards = []) {
|
|
362
364
|
await writeDailySnapshot({
|
|
363
365
|
...(config.configHome ? { configHome: config.configHome } : {}),
|
|
364
366
|
logPath: config.logPath,
|
|
365
367
|
snapshotPath: config.snapshotPath,
|
|
366
368
|
commandPath: config.commandPath,
|
|
367
369
|
accountsData,
|
|
370
|
+
quotaCards,
|
|
368
371
|
}).catch((error) => {
|
|
369
372
|
console.warn(`[daily-keepalive] ${snapshotWarning(error)}`)
|
|
370
373
|
})
|
package/src/snapshot.js
CHANGED
|
@@ -278,6 +278,7 @@ export async function writeDailySnapshot(options = {}) {
|
|
|
278
278
|
snapshotPath = defaultSnapshotPath(),
|
|
279
279
|
configHome = undefined,
|
|
280
280
|
accountsData = null,
|
|
281
|
+
quotaCards = [],
|
|
281
282
|
commandPath = undefined,
|
|
282
283
|
} = options
|
|
283
284
|
const writeCompanion = options.writeCompanion ??
|
|
@@ -302,6 +303,7 @@ export async function writeDailySnapshot(options = {}) {
|
|
|
302
303
|
logPath,
|
|
303
304
|
records,
|
|
304
305
|
accountsData,
|
|
306
|
+
quotaCards,
|
|
305
307
|
})
|
|
306
308
|
if (!Array.isArray(accountsData)) {
|
|
307
309
|
inventory = carryForwardQuotas(inventory, await readPreviousSnapshot(snapshotPath))
|