Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -7174,7 +7174,18 @@ func sendAITokenLimitAlert(ctx context.Context, execution WorkflowExecution, ful

cacheKey := generateAlertCacheKey(billingOrgId, "agent_token_limit_exceeded", admins)
if !checkAndSetAlertCache(ctx, cacheKey) {
log.Printf("[DEBUG] Skipping duplicate AI token limit alert for org %s - already sent recently", billingOrgId)
log.Printf("[DEBUG] Skipping duplicate AI token limit alert for org %s - already sent recently (1)", billingOrgId)
return
}

orgStats, err := GetOrgStatistics(ctx, billingOrgId)
if err != nil {
log.Printf("[ERROR] Failed to get org stats for AI token limit alert for org %s: %s", billingOrgId, err)
return
}

if orgStats.MonthlyAIUsageAlertSent {
log.Printf("[DEBUG] Skipping duplicate AI token limit alert for org %s - already sent recently (2)", billingOrgId)
return
}

Expand All @@ -7196,6 +7207,11 @@ func sendAITokenLimitAlert(ctx context.Context, execution WorkflowExecution, ful
log.Printf("[ERROR] Failed sending AI token limit alert email to %v for org %s: %s", admins, billingOrgId, errMail)
} else {
log.Printf("[INFO] Sent AI token limit alert email to %v of org %s", admins, billingOrgId)
orgStats.MonthlyAIUsageAlertSent = true
errStats := SetOrgStatistics(ctx, *orgStats, billingOrgId)
if errStats != nil {
log.Printf("[ERROR] Failed to update org stats after sending AI token limit alert for org %s: %s", billingOrgId, errStats)
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions db-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ func SetOrgStatistics(ctx context.Context, stats ExecutionInfo, id string) error
log.Printf("[ERROR] Failed adding stats with ID %s: %s", id, putErr)

if strings.Contains(fmt.Sprintf("%s", putErr), "entity is too big") {
log.Printf("[WARNING] SetOrgStatistics: entity too big for org %s – archiving to GCS and trimming", id)
log.Printf("[WARNING] SetOrgStatistics: entity too big for org %s – attempting to archive to GCS", id)

if archiveErr := archiveOldStatsToGCSBucket(ctx, id, &stats); archiveErr != nil {
log.Printf("[WARNING] SetOrgStatistics: GCS archive failed for org %s: %s – trimming anyway", id, archiveErr)
log.Printf("[ERROR] SetOrgStatistics: GCS archive failed for org %s: %s – cannot trim stats without backup, returning original error", id, archiveErr)
return putErr
}

if len(stats.DailyStatistics) > 60 {
Expand Down
3 changes: 2 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ func HandleGetStatistics(resp http.ResponseWriter, request *http.Request) {

// Get a max of the last 365 days
if len(info.DailyStatistics) > 365 {
info.DailyStatistics = info.DailyStatistics[len(info.DailyStatistics)-60:]
info.DailyStatistics = info.DailyStatistics[len(info.DailyStatistics)-365:]
}
}

Expand Down Expand Up @@ -1406,6 +1406,7 @@ func handleDailyCacheUpdate(executionInfo *ExecutionInfo) *ExecutionInfo {
executionInfo.MonthlyAgentOutputTokens = 0
executionInfo.LastMonthlyResetMonth = currentMonth
executionInfo.LastUsageAlertThreshold = 0
executionInfo.MonthlyAIUsageAlertSent = false

// Reset all usage alerts to unsent
for index := range executionInfo.UsageAlerts {
Expand Down
1 change: 1 addition & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ type ExecutionInfo struct {
LastMonthlyResetMonth int `json:"last_monthly_reset_month" datastore:"last_monthly_reset_month"`
LastUsageAlertThreshold int64 `json:"last_usage_alert_threshold" datastore:"last_usage_alert_threshold"`
UsageAlerts []AlertThreshold `json:"usage_alerts" datastore:"usage_alerts"`
MonthlyAIUsageAlertSent bool `json:"monthly_ai_usage_alert_sent" datastore:"monthly_ai_usage_alert_sent"`
}

type AdditionalUseConfig struct {
Expand Down