From 4c4d6fd425b86f6d11e72cdf1957345c347814ac Mon Sep 17 00:00:00 2001 From: Ezequiel Victorero Date: Thu, 23 Jun 2022 12:02:57 -0300 Subject: [PATCH] PublicDashboards: collect stats for public dashboards (#50553) * PublicDashboards: collect stats for public dashboards --- pkg/infra/metrics/metrics.go | 10 ++++++++++ pkg/infra/usagestats/statscollector/service.go | 3 +++ pkg/infra/usagestats/statscollector/service_test.go | 2 ++ pkg/models/stats.go | 1 + pkg/services/sqlstore/stats.go | 3 +++ 5 files changed, 19 insertions(+) diff --git a/pkg/infra/metrics/metrics.go b/pkg/infra/metrics/metrics.go index 85d4e628297..788e9702c59 100644 --- a/pkg/infra/metrics/metrics.go +++ b/pkg/infra/metrics/metrics.go @@ -187,6 +187,9 @@ var ( // StatsTotalDataKeys is a metric of total number of data keys stored in Grafana. StatsTotalDataKeys *prometheus.GaugeVec + + // MStatTotalPublicDashboards is a metric total amount of public dashboards + MStatTotalPublicDashboards prometheus.Gauge ) func init() { @@ -550,6 +553,12 @@ func init() { Help: "total amount of data keys in the database", Namespace: ExporterName, }, []string{"active"}) + + MStatTotalPublicDashboards = prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "stat_totals_public_dashboard", + Help: "total amount of public dashboards", + Namespace: ExporterName, + }) } // SetBuildInformation sets the build information for this binary @@ -644,5 +653,6 @@ func initMetricVars() { StatsTotalLibraryPanels, StatsTotalLibraryVariables, StatsTotalDataKeys, + MStatTotalPublicDashboards, ) } diff --git a/pkg/infra/usagestats/statscollector/service.go b/pkg/infra/usagestats/statscollector/service.go index c5326b1491a..61e9e2c55a0 100644 --- a/pkg/infra/usagestats/statscollector/service.go +++ b/pkg/infra/usagestats/statscollector/service.go @@ -154,6 +154,7 @@ func (s *Service) collectSystemStats(ctx context.Context) (map[string]interface{ m["stats.api_keys.count"] = statsQuery.Result.APIKeys m["stats.data_keys.count"] = statsQuery.Result.DataKeys m["stats.active_data_keys.count"] = statsQuery.Result.ActiveDataKeys + m["stats.public_dashboards.count"] = statsQuery.Result.PublicDashboards ossEditionCount := 1 enterpriseEditionCount := 0 @@ -344,6 +345,8 @@ func (s *Service) updateTotalStats(ctx context.Context) bool { inactiveDataKeys := statsQuery.Result.DataKeys - statsQuery.Result.ActiveDataKeys metrics.StatsTotalDataKeys.With(prometheus.Labels{"active": "false"}).Set(float64(inactiveDataKeys)) + metrics.MStatTotalPublicDashboards.Set(float64(statsQuery.Result.PublicDashboards)) + dsStats := models.GetDataSourceStatsQuery{} if err := s.sqlstore.GetDataSourceStats(ctx, &dsStats); err != nil { s.log.Error("Failed to get datasource stats", "error", err) diff --git a/pkg/infra/usagestats/statscollector/service_test.go b/pkg/infra/usagestats/statscollector/service_test.go index 1a3d2c3f8e8..7779474c06b 100644 --- a/pkg/infra/usagestats/statscollector/service_test.go +++ b/pkg/infra/usagestats/statscollector/service_test.go @@ -177,6 +177,7 @@ func TestCollectingUsageStats(t *testing.T) { assert.EqualValues(t, 11, metrics["stats.data_keys.count"]) assert.EqualValues(t, 3, metrics["stats.active_data_keys.count"]) + assert.EqualValues(t, 5, metrics["stats.public_dashboards.count"]) assert.InDelta(t, int64(65), metrics["stats.uptime"], 6) } @@ -351,6 +352,7 @@ func mockSystemStats(sqlStore *mockstore.SQLStoreMock) { APIKeys: 2, DataKeys: 11, ActiveDataKeys: 3, + PublicDashboards: 5, } } diff --git a/pkg/models/stats.go b/pkg/models/stats.go index d25014fcc2b..f7d82218a13 100644 --- a/pkg/models/stats.go +++ b/pkg/models/stats.go @@ -41,6 +41,7 @@ type SystemStats struct { DailyActiveSessions int64 DataKeys int64 ActiveDataKeys int64 + PublicDashboards int64 } type DataSourceStats struct { diff --git a/pkg/services/sqlstore/stats.go b/pkg/services/sqlstore/stats.go index b2f0e8c396b..3eeab812a9c 100644 --- a/pkg/services/sqlstore/stats.go +++ b/pkg/services/sqlstore/stats.go @@ -105,6 +105,9 @@ func (ss *SQLStore) GetSystemStats(ctx context.Context, query *models.GetSystemS sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `) AS data_keys,`) sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("data_keys") + `WHERE active = true) AS active_data_keys,`) + // TODO: table name will change and filter should check only for is_enabled = true + sb.Write(`(SELECT COUNT(*) FROM ` + dialect.Quote("dashboard_public") + `WHERE is_enabled = true) AS public_dashboards,`) + sb.Write(ss.roleCounterSQL(ctx)) var stats models.SystemStats