fix: log charge amount

This commit is contained in:
Yanlong Wang 2024-05-14 17:25:59 +08:00
parent 04eb6b3087
commit f171e54ac9
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37

View File

@ -364,14 +364,13 @@ ${authMixin}`,
{ contentType: 'text/plain', envelope: null } { contentType: 'text/plain', envelope: null }
); );
} }
if (uid) { if (uid) {
const user = await auth.assertUser(); const user = await auth.assertUser();
if (!(user.wallet.total_balance > 0)) { if (!(user.wallet.total_balance > 0)) {
throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`); throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
} }
await this.rateLimitControl.simpleRPCUidBasedLimit(rpcReflect, uid, ['CRAWL'], const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(rpcReflect, uid, ['CRAWL'],
[ [
// 1000 requests per minute // 1000 requests per minute
new Date(Date.now() - 60 * 1000), 1000 new Date(Date.now() - 60 * 1000), 1000
@ -383,15 +382,26 @@ ${authMixin}`,
auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => { auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) }); this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
}); });
apiRoll._ref?.set({
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
} }
}); });
} else if (ctx.req.ip) { } else if (ctx.req.ip) {
await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, ['CRAWL'], const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, ['CRAWL'],
[ [
// 100 requests per minute // 100 requests per minute
new Date(Date.now() - 60 * 1000), 100 new Date(Date.now() - 60 * 1000), 100
] ]
); );
rpcReflect.finally(() => {
if (chargeAmount) {
apiRoll._ref?.set({
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
}
});
} }
let urlToCrawl; let urlToCrawl;
@ -653,6 +663,10 @@ ${authMixin}`,
} }
getChargeAmount(formatted: { [k: string]: any; }) { getChargeAmount(formatted: { [k: string]: any; }) {
if (!formatted) {
return undefined;
}
const textContent = formatted?.content || formatted?.text || formatted?.html; const textContent = formatted?.content || formatted?.text || formatted?.html;
if (typeof textContent === 'string') { if (typeof textContent === 'string') {