grafana/pkg/services/serviceaccounts/database/stats.go
Kristin Laemmert 05709ce411
chore: remove sqlstore & mockstore dependencies from (most) packages (#57087)
* chore: add alias for InitTestDB and Session

Adds an alias for the sqlstore InitTestDB and Session, and updates tests using these to reduce dependencies on the sqlstore.Store.

* next pass of removing sqlstore imports
* last little bit
* remove mockstore where possible
2022-10-19 09:02:15 -04:00

30 lines
875 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
}
return &sqlStats, nil
}