Service accounts: add usagestats for teams (#51585)

* remove enabled service account count

* add service account count in teams

* removed unnessecary comment
This commit is contained in:
Eric Leijonmarck 2022-06-30 11:05:49 +02:00 committed by GitHub
parent b45c71c9ca
commit c5089f1595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -7,7 +7,7 @@ import (
)
func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (map[string]interface{}, error) {
stats := map[string]interface{}{"stats.serviceaccounts.enabled.count": int64(1)}
stats := map[string]interface{}{}
sb := &sqlstore.SQLBuilder{}
dialect := s.sqlStore.Dialect
@ -15,11 +15,16 @@ func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (map[str
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("user") +
` WHERE is_service_account = ` + dialect.BooleanStr(true) + `) AS serviceaccounts,`)
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("api_key") +
` WHERE service_account_id IS NOT NULL ) AS serviceaccount_tokens`)
` WHERE service_account_id IS NOT NULL ) AS serviceaccount_tokens,`)
// Add count to how many service accounts are in teams
sb.Write(`(SELECT COUNT(*) FROM team_member
JOIN ` + dialect.Quote("user") + ` on team_member.user_id=` + dialect.Quote("user") + `.id
WHERE ` + dialect.Quote("user") + `.is_service_account=` + dialect.BooleanStr(true) + ` ) as serviceaccounts_in_teams`)
type saStats struct {
ServiceAccounts int64 `xorm:"serviceaccounts"`
Tokens int64 `xorm:"serviceaccount_tokens"`
InTeams int64 `xorm:"serviceaccounts_in_teams"`
}
var sqlStats saStats
@ -32,6 +37,7 @@ func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (map[str
stats["stats.serviceaccounts.count"] = sqlStats.ServiceAccounts
stats["stats.serviceaccounts.tokens.count"] = sqlStats.Tokens
stats["stats.serviceaccounts.in_teams.count"] = sqlStats.InTeams
return stats, nil
}

View File

@ -37,5 +37,5 @@ func TestStore_UsageStats(t *testing.T) {
assert.Equal(t, int64(1), stats["stats.serviceaccounts.count"].(int64))
assert.Equal(t, int64(1), stats["stats.serviceaccounts.tokens.count"].(int64))
assert.Equal(t, int64(1), stats["stats.serviceaccounts.enabled.count"].(int64))
assert.Equal(t, int64(0), stats["stats.serviceaccounts.in_teams.count"].(int64))
}