-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completely redo stats system as there was an issue where it loses all…
… data on restart
- Loading branch information
1 parent
35530c7
commit e90e752
Showing
7 changed files
with
196 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package middleware | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/jasonlovesdoggo/abacus/utils" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func formatPath(path string) string { | ||
path = path[1:] | ||
path = strings.Split(path, "/")[0] | ||
return path | ||
} | ||
|
||
//func shouldSkip(path string) bool { | ||
// switch path { | ||
// | ||
// case "/favicon.ico", "/docs", "/", "favicon.svg": | ||
// return true | ||
// } | ||
// return false | ||
//} | ||
|
||
func Stats() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
route := formatPath(c.Request.URL.Path) | ||
utils.Total++ | ||
utils.CommonStats[route]++ | ||
c.Next() | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
var Total int64 = 0 | ||
|
||
var CommonStats = map[string]int64{} | ||
|
||
var ServerClose = make(chan struct{}) | ||
|
||
func saveStats(client *redis.Client) { | ||
newTotal := Total | ||
Total = 0 // reset the total | ||
|
||
newStats := CommonStats | ||
CommonStats = map[string]int64{} // reset the map | ||
|
||
client.IncrBy(context.Background(), "stats:Total", newTotal) // Capitalized to avoid conflict with a potential key named "total" | ||
for key, value := range newStats { | ||
client.IncrBy(context.Background(), "stats:"+key, value) | ||
} | ||
} | ||
|
||
func InitializeStats(client *redis.Client) { | ||
ticker := time.NewTicker(30 * time.Second) | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
saveStats(client) | ||
|
||
case <-ServerClose: | ||
ticker.Stop() | ||
log.Println("Saving stats... Closing stats goroutine. Goodbye!") | ||
saveStats(client) // save the stats one last time before closing | ||
|
||
return | ||
} | ||
} | ||
}() | ||
} |