mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 20:24:18 -06:00
1c7f89c41b
* Add tests for service accounts metrics usage * Add service account store implementation * Add service account service implementation * Add tests for org metrics usage * Add org implementation * Add service implementation
38 lines
1.3 KiB
Go
38 lines
1.3 KiB
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,`)
|
|
sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("org_user") + ` AS ou ` +
|
|
`JOIN ` + dialect.Quote("user") + ` AS u ON u.id = ou.user_id ` +
|
|
`WHERE u.is_disabled = ` + dialect.BooleanStr(false) + ` ` +
|
|
`AND u.is_service_account = ` + dialect.BooleanStr(true) + ` ` +
|
|
`AND ou.role=?) AS serviceaccounts_with_no_role`)
|
|
sb.AddParams("None")
|
|
|
|
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
|
|
}
|