quota/tokenCount: Implement the "request tokens as half" case

This commit is contained in:
Wroclaw 2023-09-21 20:42:06 +02:00
parent 4abdaebf70
commit 16fb74ec4b

View file

@ -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<Array<Usage & {usage: number}>>`
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<Array<Usage & {usage: bigint}>>`
SELECT t1.*, (
SELECT