From 4abdaebf70201f2264e32ebb73f2bd71653552a5 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Thu, 21 Sep 2023 20:38:50 +0200 Subject: [PATCH 1/2] quota/tokenCount: I always forget about debug logs... --- src/quota/tokenCount.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/quota/tokenCount.ts b/src/quota/tokenCount.ts index f1ce0a3..03dfc2f 100644 --- a/src/quota/tokenCount.ts +++ b/src/quota/tokenCount.ts @@ -100,7 +100,6 @@ export default class tokenCount implements IQuota { this.findNthUsage(userId, request.createdTimestamp, unitCount) ]); - console.log(renameMebecause); return { ...userQuota, recoveryTimestamp: (renameMebecause.at(0)?.timestamp.valueOf() ?? Infinity) + this.lookback, From 16fb74ec4b9fc61a222b600935eb70e3ea988938 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Thu, 21 Sep 2023 20:42:06 +0200 Subject: [PATCH 2/2] quota/tokenCount: Implement the "request tokens as half" case --- src/quota/tokenCount.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/quota/tokenCount.ts b/src/quota/tokenCount.ts index 03dfc2f..263ba13 100644 --- a/src/quota/tokenCount.ts +++ b/src/quota/tokenCount.ts @@ -65,9 +65,34 @@ export default class tokenCount implements IQuota { return this.createUserQuotaData(this.defaultQuota, usedUnits); } + /** + * Helper function to use proper query based on considerInputTokensAsHalf setting + * @param user The user to find nth usage + * @param requestTimestamp the timestamp of the request + * @param unitCount the unit count to check + * @returns promise of giving out the record + */ findNthUsage(user: string, requestTimestamp: number, unitCount: number) { if (this.considerInputTokensAsHalf) - throw("Not implemented"); + return database.$queryRaw>` + SELECT t1.*, ( + SELECT + SUM(usageResponse + usageRequest/2) AS usage + FROM \`usage\` + WHERE + user = ${user} AND + timestamp >= ${requestTimestamp - this.lookback} AND + timestamp <= t1.timestamp + ) as usage + FROM + \`usage\` AS t1 + WHERE + user = ${user} AND + timestamp >= ${requestTimestamp - this.lookback} AND + usage >= ${unitCount} + ORDER BY timestamp ASC + LIMIT 1 + `; return database.$queryRaw>` SELECT t1.*, ( SELECT