mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Remove specific stats from usage stats service * Create statscollector service * refactor * Update and move tests Mostly equivalent tests to before, but they've been divided over the two services and removed the behavior driven legacy from GoConvey to reduce the complexity of the tests. * Collect featuremgmr metrics (copied over from #47407) I removed the metrics registration from the feature manager in the merge and re-add them in this commit. Separated to make things easier to review.
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package statscollector
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
)
|
|
|
|
const concurrentUserStatsCacheLifetime = time.Hour
|
|
|
|
type concurrentUsersStats struct {
|
|
BucketLE3 int32 `xorm:"bucket_le_3"`
|
|
BucketLE6 int32 `xorm:"bucket_le_6"`
|
|
BucketLE9 int32 `xorm:"bucket_le_9"`
|
|
BucketLE12 int32 `xorm:"bucket_le_12"`
|
|
BucketLE15 int32 `xorm:"bucket_le_15"`
|
|
BucketLEInf int32 `xorm:"bucket_le_inf"`
|
|
}
|
|
|
|
type memoConcurrentUserStats struct {
|
|
stats *concurrentUsersStats
|
|
|
|
memoized time.Time
|
|
}
|
|
|
|
func (s *Service) concurrentUsers(ctx context.Context) (*concurrentUsersStats, error) {
|
|
memoizationPeriod := time.Now().Add(-concurrentUserStatsCacheLifetime)
|
|
if !s.concurrentUserStatsCache.memoized.Before(memoizationPeriod) {
|
|
return s.concurrentUserStatsCache.stats, nil
|
|
}
|
|
|
|
s.concurrentUserStatsCache.stats = &concurrentUsersStats{}
|
|
err := s.sqlstore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
// Retrieves concurrent users stats as a histogram. Buckets are accumulative and upper bound is inclusive.
|
|
rawSQL := `
|
|
SELECT
|
|
COUNT(CASE WHEN tokens <= 3 THEN 1 END) AS bucket_le_3,
|
|
COUNT(CASE WHEN tokens <= 6 THEN 1 END) AS bucket_le_6,
|
|
COUNT(CASE WHEN tokens <= 9 THEN 1 END) AS bucket_le_9,
|
|
COUNT(CASE WHEN tokens <= 12 THEN 1 END) AS bucket_le_12,
|
|
COUNT(CASE WHEN tokens <= 15 THEN 1 END) AS bucket_le_15,
|
|
COUNT(1) AS bucket_le_inf
|
|
FROM (select count(1) as tokens from user_auth_token group by user_id) uat;`
|
|
_, err := sess.SQL(rawSQL).Get(s.concurrentUserStatsCache.stats)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get concurrent users stats from database: %w", err)
|
|
}
|
|
|
|
s.concurrentUserStatsCache.memoized = time.Now()
|
|
return s.concurrentUserStatsCache.stats, nil
|
|
}
|