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 396fae5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
21 changes: 15 additions & 6 deletions management/server/file_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,23 @@ func (s *FileStore) CalculateUsageStats(_ context.Context, accountID string, sta

activeUsers := make(map[string]bool)
for _, peer := range account.Peers {
lastSeen := peer.Status.LastSeen
if lastSeen.Compare(start) >= 0 && lastSeen.Compare(end) <= 0 {
if user, exists := account.Users[peer.UserID]; exists {
if user.IsServiceUser {
continue
}

if _, exists := account.Users[peer.UserID]; exists && !activeUsers[peer.UserID] {
activeUsers[peer.UserID] = true
stats.ActiveUsers++
if !activeUsers[peer.UserID] {
stats.TotalUsers++
}

lastSeen := peer.Status.LastSeen
if lastSeen.Compare(start) >= 0 && lastSeen.Compare(end) <= 0 {
if !activeUsers[peer.UserID] {
activeUsers[peer.UserID] = true
stats.ActiveUsers++
}
stats.ActivePeers++
}
stats.ActivePeers++
}
}

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
4 changes: 4 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

0 comments on commit 396fae5

Please sign in to comment.