diff --git a/pkg/rest/stats.go b/pkg/rest/stats.go index 057ef89..ae6ccd0 100644 --- a/pkg/rest/stats.go +++ b/pkg/rest/stats.go @@ -129,11 +129,11 @@ func (c *Context) userStatsHandler(w http.ResponseWriter, r *http.Request) { } handshake, err := time.Parse(wireguard.TIMESTAMP_FORMAT, inputSplit[5]) if err == nil { - if _, ok := handshakeData[userID]; !ok { - handshakeData[userID] = []UserStatsDataPoint{} - } handshake = handshake.Add(time.Duration(offset) * time.Minute) if dateEqual(handshake, date) && !handshake.Equal(handshakeLast[userID]) { + if _, ok := handshakeData[userID]; !ok { + handshakeData[userID] = []UserStatsDataPoint{} + } handshakeData[userID] = append(handshakeData[userID], UserStatsDataPoint{X: handshake.Format(wireguard.TIMESTAMP_FORMAT), Y: 1}) } } diff --git a/pkg/rest/stats_test.go b/pkg/rest/stats_test.go index 1778b33..5ec9712 100644 --- a/pkg/rest/stats_test.go +++ b/pkg/rest/stats_test.go @@ -2,7 +2,6 @@ package rest import ( "encoding/json" - "fmt" "net/http/httptest" "path" "strings" @@ -31,7 +30,7 @@ func TestUserStatsHandler(t *testing.T) { 2024-08-23T20:04:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,26574324,83886164,2024-08-23T20:02:53 2024-08-23T20:09:03,3df97301-5f73-407a-a26b-91829f1e7f48,1,39928520,85171728,2024-08-23T20:08:54` - statsFile := c.Storage.Client.ConfigPath(path.Join(wireguard.VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02")) + ".log") + statsFile := path.Join(wireguard.VPN_STATS_DIR, "user-"+time.Now().Format("2006-01-02")) + ".log" err = c.Storage.Client.WriteFile(statsFile, []byte(strings.ReplaceAll(testData, "2024-08-23", time.Now().Format("2006-01-02")))) if err != nil { t.Fatalf("Cannot write test file") @@ -63,5 +62,8 @@ func TestUserStatsHandler(t *testing.T) { if userStatsResponse.TransmitBytes.Datasets[0].Data[1].Y != 813588 { t.Fatalf("unexpected data: %f", userStatsResponse.TransmitBytes.Datasets[0].Data[1].Y) } - fmt.Printf("%+v\n", userStatsResponse.Handshakes) + if userStatsResponse.Handshakes.Datasets[0].Data[1].X != "2024-08-25T18:30:42" { + t.Fatalf("unexpected data: %s", userStatsResponse.Handshakes.Datasets[0].Data[1].X) + } + }