Skip to content

Add usage model to schema.ts and add query to lib/db/queries #110

@sirily11

Description

@sirily11

Create a usage schema that records user's usage. One user should have multiple usages (One usage per chat) including token usage and usage in dollar. It also records the model it uses. The usage is coming from the model, however, it only includes the token usage, and you neeed to use the selected model (which has the pricing for 1k tokens), then you need to multiply those numbers to get usage in USD per request.

For usage schema you need

  1. id - autogenerated
  2. created_at
  3. updated_at
  4. model name
  5. model provider
  6. token usage - json
  7. usd usage - number
  8. isPaid - boolean
  9. user - fk

In the query, you need to write the following queries:

  1. record usage by user with selected model
  2. get total usage by month (as input parameter)
  3. get list of usages order by time and paginated
  4. isOverlimit (gather all unpaid usage and check if it over the user's usage limit by user comparing user's credit and web3Credit)
  5. get unPaidedUsage (show all unpaid usage paginated)
  6. payUsage will set the usage to be paid and deduct user's credit. If user's credit not enough to pay the credit, use web3's credit and deduct the web3Credit. This function takes two object, one is payInWeb2 and one is payInWeb3. Call these two functions for payment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions