@@ -628,32 +628,6 @@ func HandleGetStatistics(resp http.ResponseWriter, request *http.Request) {
628628 log .Printf ("[INFO] Should get stats for key %s" , statsKey )
629629 }
630630
631- currentMonth := time .Now ().Month ()
632- if int (currentMonth ) != info .LastMonthlyResetMonth {
633- handleDailyCacheUpdate (info )
634- err = SetOrgStatistics (ctx , * info , info .OrgId )
635- if err != nil {
636- log .Printf ("[WARNING] Failed setting org stats after monthly reset for org %s: %s" , orgId , err )
637- }
638- }
639-
640- if len (org .CreatorOrg ) > 0 {
641- // get stats for parent org as well
642- parentInfo , err := GetOrgStatistics (ctx , org .CreatorOrg )
643- if err != nil {
644- log .Printf ("[WARNING] Failed getting stats for parent org %s: %s" , org .CreatorOrg , err )
645- } else {
646- currentMonth := time .Now ().Month ()
647- if int (currentMonth ) != parentInfo .LastMonthlyResetMonth {
648- handleDailyCacheUpdate (parentInfo )
649- err = SetOrgStatistics (ctx , * parentInfo , parentInfo .OrgId )
650- if err != nil {
651- log .Printf ("[WARNING] Failed setting org stats after monthly reset for parent org %s: %s" , org .CreatorOrg , err )
652- }
653- }
654- }
655- }
656-
657631 if len (info .DailyStatistics ) > 0 {
658632 // Sort the array
659633 sort .Slice (info .DailyStatistics , func (i , j int ) bool {
@@ -1408,6 +1382,11 @@ func handleDailyCacheUpdate(executionInfo *ExecutionInfo) *ExecutionInfo {
14081382 executionInfo .MonthlyAIUsage = 0
14091383 executionInfo .LastMonthlyResetMonth = currentMonth
14101384 executionInfo .LastUsageAlertThreshold = 0
1385+
1386+ // Reset all usage alerts to unsent
1387+ for index := range executionInfo .UsageAlerts {
1388+ executionInfo .UsageAlerts [index ].Email_send = false
1389+ }
14111390 }
14121391
14131392 return executionInfo
@@ -1550,6 +1529,24 @@ func HandleIncrement(dataType string, orgStatistics *ExecutionInfo, increment ui
15501529 return orgStatistics
15511530 }
15521531
1532+ for _ , alert := range org .Billing .AlertThreshold {
1533+ found := false
1534+ for _ , statAlert := range orgStatistics .UsageAlerts {
1535+ if statAlert .Percentage == alert .Percentage || statAlert .Count == alert .Count {
1536+ found = true
1537+ break
1538+ }
1539+ }
1540+
1541+ if ! found {
1542+ orgStatistics .UsageAlerts = append (orgStatistics .UsageAlerts , AlertThreshold {
1543+ Percentage : alert .Percentage ,
1544+ Count : alert .Count ,
1545+ Email_send : alert .Email_send ,
1546+ })
1547+ }
1548+ }
1549+
15531550 for index , AlertThreshold := range org .Billing .AlertThreshold {
15541551
15551552 totalAppExecutions := orgStatistics .MonthlyAppExecutions + orgStatistics .MonthlyChildAppExecutions
@@ -1561,7 +1558,15 @@ func HandleIncrement(dataType string, orgStatistics *ExecutionInfo, increment ui
15611558 shouldSendAlert = true
15621559 }
15631560
1564- if int64 (AlertThreshold .Count ) < totalAppExecutions && AlertThreshold .Email_send == false && shouldSendAlert {
1561+ sendAlert := false
1562+ for _ , alerts := range orgStatistics .UsageAlerts {
1563+ if alerts .Percentage == AlertThreshold .Percentage && alerts .Count == AlertThreshold .Count {
1564+ sendAlert = alerts .Email_send
1565+ break
1566+ }
1567+ }
1568+
1569+ if int64 (AlertThreshold .Count ) < totalAppExecutions && ! sendAlert && shouldSendAlert {
15651570
15661571 allAdmins := []string {}
15671572 firstAdmin := ""
@@ -1623,6 +1628,15 @@ func HandleIncrement(dataType string, orgStatistics *ExecutionInfo, increment ui
16231628 log .Printf ("[ERROR] Failed setting org in increment: %s" , err )
16241629 return orgStatistics
16251630 }
1631+
1632+ // update the the alert send in the statistics
1633+ for index , alerts := range orgStatistics .UsageAlerts {
1634+ if alerts .Percentage == AlertThreshold .Percentage && alerts .Count == AlertThreshold .Count {
1635+ orgStatistics .UsageAlerts [index ].Email_send = true
1636+ break
1637+ }
1638+ }
1639+
16261640 log .Printf ("[DEBUG] Successfully sent alert mail for org %s" , orgId )
16271641 }
16281642 }
0 commit comments