@@ -433,7 +433,7 @@ func GetSpecificStats(resp http.ResponseWriter, request *http.Request) {
433433 statEntries = mergedEntries
434434
435435 // Check if entries exist for the last X statDays
436- // Backfill any missing ones
436+ // Backfill any missing ones so that the number is correct
437437 if len (statEntries ) < statDays {
438438 // Find the missing days
439439 missingDays := []time.Time {}
@@ -468,16 +468,43 @@ func GetSpecificStats(resp http.ResponseWriter, request *http.Request) {
468468 statEntries = append (statEntries , toAppend ... )
469469 }
470470
471+ // Append cache for right now as it may not be in the DB yet
472+ for statEntryIndex , statEntry := range statEntries {
473+ if statEntry .Date .Day () == time .Now ().Day () && statEntry .Date .Month () == time .Now ().Month () && statEntry .Date .Year () == time .Now ().Year () {
474+ for _ , addition := range info .Additions {
475+ if addition .Key != statsKey {
476+ continue
477+ }
478+
479+ key := fmt .Sprintf ("cache_%s_%s" , orgId , addition .Key )
480+ cacheItem , err := GetCache (ctx , key )
481+ if err == nil {
482+ parsedItem := []byte (cacheItem .([]uint8 ))
483+ increment , err := strconv .Atoi (string (parsedItem ))
484+ if err == nil {
485+ statEntries [statEntryIndex ].Value += int64 (increment )
486+ totalValue += int (increment )
487+ }
488+ }
489+
490+ break
491+ }
492+ }
493+ }
494+
471495 // Sort statentries by date
472496 sort .Slice (statEntries , func (i , j int ) bool {
473497 return statEntries [i ].Date .Before (statEntries [j ].Date )
474498 })
475499
500+ // For debugging stats that don't show up by injecting them
501+ /*
476502 if debug && totalValue == 0 {
477503 log.Printf("[DEBUG] Found %d entries for '%s' with 0 in data. Force-adding data to first entry.", len(statEntries), statsKey)
478504 chosenIndex := rand.Intn(len(statEntries))
479505 statEntries[chosenIndex].Value = int64(rand.Intn(10) + 1)
480506 }
507+ */
481508
482509 marshalledEntries , err := json .Marshal (statEntries )
483510 if err != nil {
@@ -631,6 +658,24 @@ func HandleGetStatistics(resp http.ResponseWriter, request *http.Request) {
631658 }
632659 }
633660
661+ for additionCnt , addition := range info .Additions {
662+
663+ key := fmt .Sprintf ("cache_%s_%s" , orgId , addition .Key )
664+ cacheItem , err = GetCache (ctx , key )
665+ if err == nil {
666+ parsedItem := []byte (cacheItem .([]uint8 ))
667+ increment , err := strconv .Atoi (string (parsedItem ))
668+ if err == nil {
669+ info .Additions [additionCnt ].Value += int64 (increment )
670+ }
671+ }
672+
673+ // In case a lot of use
674+ if additionCnt > 10 {
675+ break
676+ }
677+ }
678+
634679 if len (statsKey ) > 0 {
635680 log .Printf ("[INFO] Should get stats for key %s" , statsKey )
636681 }
@@ -1232,7 +1277,6 @@ func IncrementCache(ctx context.Context, orgId, dataType string, amount ...int)
12321277
12331278 } else {
12341279 // Get the cache, but use requestCache instead of memcache
1235- //log.Printf("[DEBUG] Incrementing cache for %s with amount %d", key, incrementAmount)
12361280 foundItem := 1
12371281 item , err := GetCache (ctx , key )
12381282 if err != nil {
@@ -1269,7 +1313,7 @@ func IncrementCache(ctx context.Context, orgId, dataType string, amount ...int)
12691313
12701314 if foundItem >= int (dbDumpInterval ) {
12711315 // Memcache dump first to keep the counter going for other executions
1272- go SetCache (ctx , key , []byte (fmt .Sprintf ("%x" , 0 )), 86400 )
1316+ go SetCache (context . Background () , key , []byte (fmt .Sprintf ("%x" , 0 )), 86400 )
12731317 IncrementCacheDump (ctx , orgId , dataType , foundItem )
12741318
12751319 //log.Printf("[DEBUG] Dumping cache for %s with amount %d", key, foundItem)
0 commit comments