Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Allow key budgets to be reset #1567

Closed
krrishdholakia opened this issue Jan 23, 2024 · 5 comments
Closed

[Feature]: Allow key budgets to be reset #1567

krrishdholakia opened this issue Jan 23, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@krrishdholakia
Copy link
Contributor

The Feature

Budgets are set per key, will reset over a time period

Motivation, pitch

allow app owners (i.e. key) to have budgets that reset after a time period

Twitter / LinkedIn details

No response

@krrishdholakia krrishdholakia added the enhancement New feature or request label Jan 23, 2024
@krrishdholakia
Copy link
Contributor Author

krrishdholakia commented Jan 23, 2024

Tentative backend code:

Install APScheduler:

pip install apscheduler

Edit your main.py to add budget reset logic.

from apscheduler.schedulers.asyncio import AsyncIOScheduler

scheduler = AsyncIOScheduler()

@scheduler.scheduled_job('interval', days=30)
async def reset_budgets():
    now = datetime.datetime.utcnow()
    keys_to_reset = await prisma.apiKey.find_many(
        where={
            'resetAt': {
                'lt': now
            },
        }
    )

    for key in keys_to_reset:
        key.budget = initial_budget
        key.resetAt = now + datetime.timedelta(days=30)
        await prisma.apiKey.update({
            'where': {'id': key.id},
            'data': key
        })

# inside your startup event after prisma.connect()
scheduler.start()

@krrishdholakia
Copy link
Contributor Author

krrishdholakia commented Jan 23, 2024

We'll need to introduce 1 new field into /key/generate:

  • budget_duration: Specify the length of time the budget is valid for. If null, default is set to 1 hour. You can set duration as seconds ("30s"), minutes ("30m"), hours ("30h"), days ("30d").

2 new fields in prisma.schema:

  • budget_duration
  • budget_reset_at # <- internal value, used to identify if budget needs to be reset

@krrishdholakia
Copy link
Contributor Author

Thinking

Sample curl

curl 'http://0.0.0.0:8000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
  "team_id": "core-infra", # [OPTIONAL]
  "max_budget": 0.0005,
  "budget_duration": "10s" 
}'

@krrishdholakia
Copy link
Contributor Author

krrishdholakia commented Jan 23, 2024

PR created - #1570

@krrishdholakia
Copy link
Contributor Author

PR Merged and passing testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant