Skip to content

Commit

Permalink
DH-5447 added record usage analytics (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
DishenWang2023 committed May 7, 2024
1 parent 139f3e1 commit d5150e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions apps/ai/server/modules/organization/invoice/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)
from modules.organization.invoice.repository import InvoiceRepository
from modules.organization.repository import OrganizationRepository
from utils.analytics import Analytics, EventName, EventType
from utils.billing import Billing
from utils.exception import ErrorCode

Expand All @@ -35,6 +36,7 @@ def __init__(self):
self.billing = Billing()
self.repo = InvoiceRepository()
self.org_repo = OrganizationRepository()
self.analytics = Analytics()
self.cost_dict = {
UsageType.SQL_GENERATION: invoice_settings.sql_generation_cost,
UsageType.FINETUNING_GPT_35: invoice_settings.finetuning_gpt_35_cost,
Expand Down Expand Up @@ -264,6 +266,17 @@ def record_usage(
f"negative credit from usage {usage_id}",
)

self.analytics.track(
org_id,
EventName.usage_recorded,
EventType.usage_event(
id=usage_id,
organization_id=org_id,
type=type,
cost=round(self.cost_dict[type] * quantity / 100, 2),
),
)

def check_usage(
self,
org_id: str,
Expand Down
7 changes: 7 additions & 0 deletions apps/ai/server/utils/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class EventName(str, Enum):
finetuning_created = "finetuning_created"
sql_generation_created = "sql_generation_created"
sql_generation_updated = "sql_generation_updated"
usage_recorded = "usage_recorded"


class Event(BaseModel):
Expand Down Expand Up @@ -60,6 +61,11 @@ class UserEvent(Event):
name: str | None


class UsageEvent(Event):
type: str | None
cost: float | None


class EventType(BaseModel):
db_connection_event = DBConnectionEvent
finetuning_event = FinetuningEvent
Expand All @@ -68,6 +74,7 @@ class EventType(BaseModel):
sql_generation_event = SQLGenerationEvent
sql_generation_updated_event = SQLGenerationUpdatedEvent
user_event = UserEvent
usage_event = UsageEvent


class Properties(BaseModel):
Expand Down

0 comments on commit d5150e9

Please sign in to comment.