nomagick commited on
Commit
f171e54
·
unverified ·
1 Parent(s): 04eb6b3

fix: log charge amount

Browse files
backend/functions/src/cloud-functions/crawler.ts CHANGED
@@ -364,14 +364,13 @@ ${authMixin}`,
364
  { contentType: 'text/plain', envelope: null }
365
  );
366
  }
367
-
368
  if (uid) {
369
  const user = await auth.assertUser();
370
  if (!(user.wallet.total_balance > 0)) {
371
  throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
372
  }
373
 
374
- await this.rateLimitControl.simpleRPCUidBasedLimit(rpcReflect, uid, ['CRAWL'],
375
  [
376
  // 1000 requests per minute
377
  new Date(Date.now() - 60 * 1000), 1000
@@ -383,15 +382,26 @@ ${authMixin}`,
383
  auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => {
384
  this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
385
  });
 
 
 
386
  }
387
  });
388
  } else if (ctx.req.ip) {
389
- await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, ['CRAWL'],
390
  [
391
  // 100 requests per minute
392
  new Date(Date.now() - 60 * 1000), 100
393
  ]
394
  );
 
 
 
 
 
 
 
 
395
  }
396
 
397
  let urlToCrawl;
@@ -653,6 +663,10 @@ ${authMixin}`,
653
  }
654
 
655
  getChargeAmount(formatted: { [k: string]: any; }) {
 
 
 
 
656
  const textContent = formatted?.content || formatted?.text || formatted?.html;
657
 
658
  if (typeof textContent === 'string') {
 
364
  { contentType: 'text/plain', envelope: null }
365
  );
366
  }
 
367
  if (uid) {
368
  const user = await auth.assertUser();
369
  if (!(user.wallet.total_balance > 0)) {
370
  throw new InsufficientBalanceError(`Account balance not enough to run this query, please recharge.`);
371
  }
372
 
373
+ const apiRoll = await this.rateLimitControl.simpleRPCUidBasedLimit(rpcReflect, uid, ['CRAWL'],
374
  [
375
  // 1000 requests per minute
376
  new Date(Date.now() - 60 * 1000), 1000
 
382
  auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => {
383
  this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
384
  });
385
+ apiRoll._ref?.set({
386
+ chargeAmount,
387
+ }, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
388
  }
389
  });
390
  } else if (ctx.req.ip) {
391
+ const apiRoll = await this.rateLimitControl.simpleRpcIPBasedLimit(rpcReflect, ctx.req.ip, ['CRAWL'],
392
  [
393
  // 100 requests per minute
394
  new Date(Date.now() - 60 * 1000), 100
395
  ]
396
  );
397
+
398
+ rpcReflect.finally(() => {
399
+ if (chargeAmount) {
400
+ apiRoll._ref?.set({
401
+ chargeAmount,
402
+ }, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
403
+ }
404
+ });
405
  }
406
 
407
  let urlToCrawl;
 
663
  }
664
 
665
  getChargeAmount(formatted: { [k: string]: any; }) {
666
+ if (!formatted) {
667
+ return undefined;
668
+ }
669
+
670
  const textContent = formatted?.content || formatted?.text || formatted?.html;
671
 
672
  if (typeof textContent === 'string') {