-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4db6781
commit 9ec8b6a
Showing
4 changed files
with
116 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,8 @@ services: | |
volumes: | ||
- mongo-data:/data/db | ||
restart: unless-stopped | ||
logging: | ||
driver: none | ||
|
||
volumes: | ||
mongo-data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import mongoose from "mongoose"; | ||
|
||
// User schema | ||
const userSchema = new mongoose.Schema({ | ||
email: { type: String, required: true, unique: true }, | ||
password: { type: String, required: true }, | ||
usageStats: { | ||
gemini: { | ||
inputCharacters: { type: Number, default: 0 }, | ||
outputCharacters: { type: Number, default: 0 }, | ||
imagesGenerated: { type: Number, default: 0 }, | ||
moneyConsumed: { type: Number, default: 0 }, | ||
}, | ||
claude: { | ||
inputTokens: { type: Number, default: 0 }, | ||
outputTokens: { type: Number, default: 0 }, | ||
moneyConsumed: { type: Number, default: 0 }, | ||
}, | ||
}, | ||
}); | ||
|
||
// User model | ||
export const User = mongoose.model("User", userSchema); | ||
|
||
export function countCharacters(text) { | ||
return text.length; | ||
} | ||
|
||
export function countTokens(text) { | ||
let tokenCount = 0; | ||
for (let i = 0; i < text.length; i++) { | ||
const char = text[i]; | ||
if (/[a-zA-Z]/.test(char)) { | ||
tokenCount += 0.25; | ||
} else { | ||
tokenCount += 1; | ||
} | ||
} | ||
return Math.round(tokenCount); | ||
} | ||
|
||
export function storeUsageStats( | ||
userId, | ||
model, | ||
inputTokens, | ||
outputTokens, | ||
inputCharacters, | ||
outputCharacters, | ||
imagesGenerated | ||
) { | ||
let moneyConsumed = 0; | ||
|
||
if (model === "gemini") { | ||
const inputCharactersCost = (inputCharacters / 1000) * 0.000125; | ||
const outputCharactersCost = (outputCharacters / 1000) * 0.000375; | ||
const imagesGeneratedCost = imagesGenerated * 0.0025; | ||
moneyConsumed = inputCharactersCost + outputCharactersCost + imagesGeneratedCost; | ||
} else if (model === "claude") { | ||
const inputTokensCost = (inputTokens * 0.25) / 1000000; | ||
const outputTokensCost = (outputTokens * 1.25) / 1000000; | ||
moneyConsumed = inputTokensCost + outputTokensCost; | ||
} | ||
|
||
User.findByIdAndUpdate( | ||
userId, | ||
{ | ||
$inc: { | ||
[`usageStats.${model}.inputTokens`]: inputTokens, | ||
[`usageStats.${model}.outputTokens`]: outputTokens, | ||
[`usageStats.${model}.inputCharacters`]: inputCharacters, | ||
[`usageStats.${model}.outputCharacters`]: outputCharacters, | ||
[`usageStats.${model}.imagesGenerated`]: imagesGenerated, | ||
[`usageStats.${model}.moneyConsumed`]: moneyConsumed, | ||
}, | ||
}, | ||
{ new: true } | ||
) | ||
.then((updatedUser) => console.log(`Usage stats updated for user ${updatedUser._id}`)) | ||
.catch((err) => console.error("Error updating usage stats:", err)); | ||
} |