mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 20:24:18 -06:00
0e565a2e6c
collect usage stat for service account forced expiry
32 lines
945 B
Go
32 lines
945 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
|
)
|
|
|
|
func (s *ServiceAccountsStoreImpl) GetUsageMetrics(ctx context.Context) (*serviceaccounts.Stats, error) {
|
|
dialect := s.sqlStore.GetDialect()
|
|
|
|
sb := &db.SQLBuilder{}
|
|
sb.Write("SELECT ")
|
|
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`)
|
|
|
|
var sqlStats serviceaccounts.Stats
|
|
if err := s.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
|
_, err := sess.SQL(sb.GetSQLString(), sb.GetParams()...).Get(&sqlStats)
|
|
return err
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sqlStats.ForcedExpiryEnabled = s.cfg.SATokenExpirationDayLimit != 0
|
|
|
|
return &sqlStats, nil
|
|
}
|