Browse Source

adds usage stats for sessions

bergquist 7 years ago
parent
commit
2be60887ca

+ 4 - 4
pkg/infra/usagestats/service.go

@@ -5,7 +5,7 @@ import (
 	"time"
 	"time"
 
 
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/bus"
-	"github.com/grafana/grafana/pkg/services/auth"
+	"github.com/grafana/grafana/pkg/services/sqlstore"
 	"github.com/grafana/grafana/pkg/social"
 	"github.com/grafana/grafana/pkg/social"
 
 
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/log"
@@ -20,9 +20,9 @@ func init() {
 }
 }
 
 
 type UsageStatsService struct {
 type UsageStatsService struct {
-	Cfg          *setting.Cfg               `inject:""`
-	TokenService *auth.UserAuthTokenService `inject:""`
-	Bus          bus.Bus                    `inject:""`
+	Cfg      *setting.Cfg       `inject:""`
+	Bus      bus.Bus            `inject:""`
+	SQLStore *sqlstore.SqlStore `inject:""`
 
 
 	oauthProviders map[string]bool
 	oauthProviders map[string]bool
 }
 }

+ 9 - 0
pkg/infra/usagestats/usage_stats.go

@@ -59,6 +59,15 @@ func (uss *UsageStatsService) sendUsageStats(oauthProviders map[string]bool) {
 	metrics["stats.provisioned_dashboards.count"] = statsQuery.Result.ProvisionedDashboards
 	metrics["stats.provisioned_dashboards.count"] = statsQuery.Result.ProvisionedDashboards
 	metrics["stats.snapshots.count"] = statsQuery.Result.Snapshots
 	metrics["stats.snapshots.count"] = statsQuery.Result.Snapshots
 	metrics["stats.teams.count"] = statsQuery.Result.Teams
 	metrics["stats.teams.count"] = statsQuery.Result.Teams
+	metrics["stats.total_sessions.count"] = statsQuery.Result.Sessions
+
+	userCount := statsQuery.Result.Users
+	avgSessionsPerUser := statsQuery.Result.Sessions
+	if userCount != 0 {
+		avgSessionsPerUser = avgSessionsPerUser / userCount
+	}
+
+	metrics["stats.avg_sessions_per_user.count"] = avgSessionsPerUser
 
 
 	dsStats := models.GetDataSourceStatsQuery{}
 	dsStats := models.GetDataSourceStatsQuery{}
 	if err := uss.Bus.Dispatch(&dsStats); err != nil {
 	if err := uss.Bus.Dispatch(&dsStats); err != nil {

+ 7 - 1
pkg/infra/usagestats/usage_stats_test.go

@@ -15,6 +15,7 @@ import (
 	"github.com/grafana/grafana/pkg/components/simplejson"
 	"github.com/grafana/grafana/pkg/components/simplejson"
 	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/plugins"
 	"github.com/grafana/grafana/pkg/plugins"
+	"github.com/grafana/grafana/pkg/services/sqlstore"
 	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/setting"
 	. "github.com/smartystreets/goconvey/convey"
 	. "github.com/smartystreets/goconvey/convey"
 )
 )
@@ -22,7 +23,8 @@ import (
 func TestMetrics(t *testing.T) {
 func TestMetrics(t *testing.T) {
 	Convey("Test send usage stats", t, func() {
 	Convey("Test send usage stats", t, func() {
 		uss := &UsageStatsService{
 		uss := &UsageStatsService{
-			Bus: bus.New(),
+			Bus:      bus.New(),
+			SQLStore: sqlstore.InitTestDB(t),
 		}
 		}
 
 
 		var getSystemStatsQuery *models.GetSystemStatsQuery
 		var getSystemStatsQuery *models.GetSystemStatsQuery
@@ -43,6 +45,7 @@ func TestMetrics(t *testing.T) {
 				ProvisionedDashboards: 12,
 				ProvisionedDashboards: 12,
 				Snapshots:             13,
 				Snapshots:             13,
 				Teams:                 14,
 				Teams:                 14,
+				Sessions:              15,
 			}
 			}
 			getSystemStatsQuery = query
 			getSystemStatsQuery = query
 			return nil
 			return nil
@@ -226,6 +229,8 @@ func TestMetrics(t *testing.T) {
 				So(metrics.Get("stats.provisioned_dashboards.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.ProvisionedDashboards)
 				So(metrics.Get("stats.provisioned_dashboards.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.ProvisionedDashboards)
 				So(metrics.Get("stats.snapshots.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Snapshots)
 				So(metrics.Get("stats.snapshots.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Snapshots)
 				So(metrics.Get("stats.teams.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Teams)
 				So(metrics.Get("stats.teams.count").MustInt(), ShouldEqual, getSystemStatsQuery.Result.Teams)
+				So(metrics.Get("stats.total_sessions.count").MustInt64(), ShouldEqual, 15)
+				So(metrics.Get("stats.avg_sessions_per_user.count").MustInt64(), ShouldEqual, 5)
 
 
 				So(metrics.Get("stats.ds."+models.DS_ES+".count").MustInt(), ShouldEqual, 9)
 				So(metrics.Get("stats.ds."+models.DS_ES+".count").MustInt(), ShouldEqual, 9)
 				So(metrics.Get("stats.ds."+models.DS_PROMETHEUS+".count").MustInt(), ShouldEqual, 10)
 				So(metrics.Get("stats.ds."+models.DS_PROMETHEUS+".count").MustInt(), ShouldEqual, 10)
@@ -251,6 +256,7 @@ func TestMetrics(t *testing.T) {
 				So(metrics.Get("stats.auth_enabled.oauth_grafana_com.count").MustInt(), ShouldEqual, 1)
 				So(metrics.Get("stats.auth_enabled.oauth_grafana_com.count").MustInt(), ShouldEqual, 1)
 
 
 				So(metrics.Get("stats.packaging.deb.count").MustInt(), ShouldEqual, 1)
 				So(metrics.Get("stats.packaging.deb.count").MustInt(), ShouldEqual, 1)
+
 			})
 			})
 		})
 		})
 
 

+ 1 - 0
pkg/models/stats.go

@@ -15,6 +15,7 @@ type SystemStats struct {
 	FolderPermissions     int64
 	FolderPermissions     int64
 	Folders               int64
 	Folders               int64
 	ProvisionedDashboards int64
 	ProvisionedDashboards int64
+	Sessions              int64
 }
 }
 
 
 type DataSourceStats struct {
 type DataSourceStats struct {

+ 2 - 1
pkg/services/sqlstore/stats.go

@@ -74,7 +74,8 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
 
 
 	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_provisioning") + `) AS provisioned_dashboards,`)
 	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_provisioning") + `) AS provisioned_dashboards,`)
 	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_snapshot") + `) AS snapshots,`)
 	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_snapshot") + `) AS snapshots,`)
-	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("team") + `) AS teams`)
+	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("team") + `) AS teams,`)
+	sb.Write(`(SELECT COUNT(id) FROM ` + dialect.Quote("user_auth_token") + `) AS sessions`)
 
 
 	var stats m.SystemStats
 	var stats m.SystemStats
 	_, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats)
 	_, err := x.SQL(sb.GetSqlString(), sb.params...).Get(&stats)