@@ -13,6 +13,7 @@ import (
1313 "github.com/gin-gonic/gin"
1414 "github.com/go-resty/resty/v2"
1515 "github.com/google/uuid"
16+ jsoniter "github.com/json-iterator/go"
1617 "github.com/sashabaranov/go-openai"
1718 "io"
1819 "log"
@@ -38,15 +39,6 @@ const (
3839
3940var AuthCount = make (map [string ]map [StatisticsType ]int )
4041
41- func IncAuthCount (authType StatisticsType ) {
42- day := time .Now ().Format ("2006-01-02" )
43- if _ , ok := AuthCount [day ]; ! ok {
44- AuthCount [day ] = make (map [StatisticsType ]int )
45- AuthCount [day ][authType ] = 0
46- }
47- AuthCount [day ][authType ]++
48- }
49-
5042func init () {
5143 var err error
5244 TokenCache , err = bigcache .New (context .Background (), bigcache .DefaultConfig (23 * time .Minute ))
@@ -55,6 +47,7 @@ func init() {
5547 panic (err )
5648 }
5749 log .Println ("init CopilotTokenCache success" )
50+ go ClearAuthCount ()
5851}
5952
6053type CopilotApi struct {
@@ -320,6 +313,11 @@ func GetCopilotToken(key string, isCo bool) (token string, data map[string]inter
320313 SetResult (& data ).
321314 SetError (& errDataMap ).
322315 Get (tokenUrl )
316+
317+ go func () {
318+ jsonData , _ := jsoniter .MarshalIndent (AuthCount , "" , " " )
319+ global .SugarLog .Infow ("request statistics" , "statistics" , string (jsonData ))
320+ }()
323321 if err != nil {
324322 global .SugarLog .Errorw ("GetCopilotToken request http error" , "err" , err , "url" , global .Config .Copilot .CoTokenURL , "key" , key )
325323 return
@@ -385,3 +383,37 @@ func GetCompletionsHeader(token string) map[string]string {
385383 }
386384 return headersMap
387385}
386+
387+ func IncAuthCount (authType StatisticsType ) {
388+ day := time .Now ().Format ("2006-01-02" )
389+ if _ , ok := AuthCount [day ]; ! ok {
390+ AuthCount [day ] = make (map [StatisticsType ]int )
391+ AuthCount [day ][authType ] = 0
392+ }
393+ AuthCount [day ][authType ]++
394+ jsonData , _ := jsoniter .MarshalIndent (AuthCount , "" , " " )
395+ global .SugarLog .Infof ("request statistics \n %s" , string (jsonData ))
396+ }
397+
398+ // ClearAuthCount 每天 0 点清空7天前统计数据
399+ func ClearAuthCount () {
400+ t := time .Now ()
401+ next := time .Date (t .Year (), t .Month (), t .Day ()+ 1 , 0 , 0 , 0 , 0 , t .Location ())
402+ d := next .Sub (t )
403+
404+ ticker := time .NewTicker (d )
405+ defer ticker .Stop ()
406+
407+ for {
408+ select {
409+ case <- ticker .C :
410+ t := time .Now ()
411+ date := t .AddDate (0 , 0 , - 7 ).Format ("2006-01-02" )
412+ if _ , ok := AuthCount [date ]; ok {
413+ delete (AuthCount , date )
414+ }
415+ global .SugarLog .Infow ("ClearAuthCount success" , "clearDay" , date )
416+ ticker .Reset (24 * time .Hour )
417+ }
418+ }
419+ }
0 commit comments