Skip to content

Commit

Permalink
Exclude service users from total user count
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Feb 21, 2024
1 parent 0740aff commit ac7dff4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions management/server/file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,20 @@ func (s *FileStore) CalculateUsageStats(_ context.Context, accountID string, sta
}

stats := &AccountUsageStats{
TotalUsers: int64(len(account.Users)),
TotalUsers: 0,
TotalPeers: int64(len(account.Peers)),
}

for _, user := range account.Users {
if !user.IsServiceUser {
stats.TotalUsers++
}
}

activeUsers := make(map[string]bool)
for _, peer := range account.Peers {
lastSeen := peer.Status.LastSeen
if lastSeen.Compare(start) >= 0 && lastSeen.Compare(end) <= 0 {

if _, exists := account.Users[peer.UserID]; exists && !activeUsers[peer.UserID] {
activeUsers[peer.UserID] = true
stats.ActiveUsers++
Expand Down
2 changes: 1 addition & 1 deletion management/server/file_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func newStore(t *testing.T) *FileStore {
return store
}

func TestFileStore_CalculateStats(t *testing.T) {
func TestFileStore_CalculateUsageStats(t *testing.T) {
storeDir := t.TempDir()

err := util.CopyFileContents("testdata/store_stats.json", filepath.Join(storeDir, "store.json"))
Expand Down
2 changes: 1 addition & 1 deletion management/server/sqlite_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func (s *SqliteStore) CalculateUsageStats(ctx context.Context, accountID string,
}

err = tx.Model(&User{}).
Where("account_id = ?", accountID).
Where("account_id = ? AND is_service_user = ?", accountID, false).
Count(&stats.TotalUsers).Error
if err != nil {
return fmt.Errorf("get total users: %w", err)
Expand Down
8 changes: 8 additions & 0 deletions management/server/testdata/store_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
},
"user-4-account-1": {
"Id": "user-4-account-1"
},
"user-5-account-1": {
"Id": "user-5-account-1",
"IsServiceUser": true
}
},
"Peers": {
Expand Down Expand Up @@ -122,6 +126,10 @@
},
"user-2-account-2": {
"Id": "user-1-account-2"
},
"user-3-account-1": {
"Id": "user-3-account-1",
"IsServiceUser": true
}
},
"Peers": {
Expand Down

0 comments on commit ac7dff4

Please sign in to comment.