Skip to content

Commit fad9b50

Browse files
authored
Merge pull request #256 from Monilprajapati/billing
stats data fix
2 parents 99172a8 + 52b0002 commit fad9b50

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

stats.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,71 @@ func GetSpecificStats(resp http.ResponseWriter, request *http.Request) {
389389
})
390390

391391
allStats := []string{}
392+
393+
getTypedValue := func(d DailyStatistics, key string) int64 {
394+
switch key {
395+
case "app_executions":
396+
return d.AppExecutions
397+
case "childorg_app_executions":
398+
return d.ChildAppExecutions
399+
case "app_executions_failed":
400+
return d.AppExecutionsFailed
401+
case "subflow_executions":
402+
return d.SubflowExecutions
403+
case "workflow_executions":
404+
return d.WorkflowExecutions
405+
case "workflow_executions_finished":
406+
return d.WorkflowExecutionsFinished
407+
case "workflow_executions_failed":
408+
return d.WorkflowExecutionsFailed
409+
case "org_sync_actions":
410+
return d.OrgSyncActions
411+
case "workflow_executions_cloud":
412+
return d.CloudExecutions
413+
case "workflow_executions_onprem":
414+
return d.OnpremExecutions
415+
case "api_usage":
416+
return d.ApiUsage
417+
case "ai_executions":
418+
return d.AIUsage
419+
default:
420+
return -1
421+
}
422+
}
423+
424+
isPredictable := ArrayContains(PredictableDataTypes, statsKey)
425+
392426
for _, daily := range info.DailyStatistics {
393427
// Check if the date is more than statDays ago
394428
shouldAppend := true
395429
if daily.Date.Before(time.Now().AddDate(0, 0, -statDays)) {
396430
shouldAppend = false
397431
}
398432

433+
if isPredictable {
434+
if shouldAppend {
435+
value := getTypedValue(daily, statsKey)
436+
if value >= 0 {
437+
totalEntires++
438+
totalValue += int(value)
439+
statEntries = append(statEntries, AdditionalUseConfig{
440+
Key: statsKey,
441+
Value: value,
442+
Date: daily.Date,
443+
})
444+
}
445+
}
446+
447+
// Track available keys too
448+
for _, k := range PredictableDataTypes {
449+
if !ArrayContains(allStats, k) {
450+
allStats = append(allStats, k)
451+
}
452+
}
453+
continue
454+
}
455+
456+
// Custom additions path (original behavior)
399457
for _, addition := range daily.Additions {
400458
newKey := strings.ToLower(strings.ReplaceAll(addition.Key, " ", "_"))
401459
if shouldAppend && newKey == statsKey {
@@ -413,6 +471,49 @@ func GetSpecificStats(resp http.ResponseWriter, request *http.Request) {
413471
}
414472
}
415473

474+
// If predictable key, also include today's in-memory daily counters (not yet rolled into DailyStatistics)
475+
if isPredictable {
476+
today := time.Now()
477+
var todayValue int64 = 0
478+
switch statsKey {
479+
case "app_executions":
480+
todayValue = info.DailyAppExecutions
481+
case "childorg_app_executions":
482+
todayValue = info.DailyChildAppExecutions
483+
case "app_executions_failed":
484+
todayValue = info.DailyAppExecutionsFailed
485+
case "subflow_executions":
486+
todayValue = info.DailySubflowExecutions
487+
case "workflow_executions":
488+
todayValue = info.DailyWorkflowExecutions
489+
case "workflow_executions_finished":
490+
todayValue = info.DailyWorkflowExecutionsFinished
491+
case "workflow_executions_failed":
492+
todayValue = info.DailyWorkflowExecutionsFailed
493+
case "org_sync_actions":
494+
todayValue = info.DailyOrgSyncActions
495+
case "workflow_executions_cloud":
496+
todayValue = info.DailyCloudExecutions
497+
case "workflow_executions_onprem":
498+
todayValue = info.DailyOnpremExecutions
499+
case "api_usage":
500+
todayValue = info.DailyApiUsage
501+
case "ai_executions":
502+
todayValue = info.DailyAIUsage
503+
}
504+
505+
// Only append if within window
506+
if !today.Before(time.Now().AddDate(0, 0, -statDays)) {
507+
statEntries = append(statEntries, AdditionalUseConfig{
508+
Key: statsKey,
509+
Value: todayValue,
510+
Date: today,
511+
})
512+
totalEntires++
513+
totalValue += int(todayValue)
514+
}
515+
}
516+
416517
// Deduplicate and merge same days
417518
mergedEntries := []AdditionalUseConfig{}
418519
for _, entry := range statEntries {

0 commit comments

Comments
 (0)