2015-03-22 14:14:00 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2017-08-09 03:36:41 -05:00
|
|
|
"time"
|
|
|
|
|
2015-03-22 14:14:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("sql", GetSystemStats)
|
2015-09-29 06:47:56 -05:00
|
|
|
bus.AddHandler("sql", GetDataSourceStats)
|
2018-05-25 09:00:15 -05:00
|
|
|
bus.AddHandler("sql", GetDataSourceAccessStats)
|
2016-01-24 23:18:17 -06:00
|
|
|
bus.AddHandler("sql", GetAdminStats)
|
2018-05-25 07:33:37 -05:00
|
|
|
bus.AddHandler("sql", GetSystemUserCountStats)
|
2015-09-29 06:47:56 -05:00
|
|
|
}
|
|
|
|
|
2018-04-27 15:14:36 -05:00
|
|
|
var activeUserTimeLimit = time.Hour * 24 * 30
|
2017-08-09 03:36:41 -05:00
|
|
|
|
2015-09-29 06:47:56 -05:00
|
|
|
func GetDataSourceStats(query *m.GetDataSourceStatsQuery) error {
|
|
|
|
var rawSql = `SELECT COUNT(*) as count, type FROM data_source GROUP BY type`
|
|
|
|
query.Result = make([]*m.DataSourceStats, 0)
|
2018-01-23 15:30:45 -06:00
|
|
|
err := x.SQL(rawSql).Find(&query.Result)
|
2015-09-29 06:47:56 -05:00
|
|
|
return err
|
2015-03-22 14:14:00 -05:00
|
|
|
}
|
|
|
|
|
2018-05-25 09:00:15 -05:00
|
|
|
func GetDataSourceAccessStats(query *m.GetDataSourceAccessStatsQuery) error {
|
|
|
|
var rawSql = `SELECT COUNT(*) as count, type, access FROM data_source GROUP BY type, access`
|
|
|
|
query.Result = make([]*m.DataSourceAccessStats, 0)
|
|
|
|
err := x.SQL(rawSql).Find(&query.Result)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:14:00 -05:00
|
|
|
func GetSystemStats(query *m.GetSystemStatsQuery) error {
|
|
|
|
var rawSql = `SELECT
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
2018-01-23 15:51:05 -06:00
|
|
|
FROM ` + dialect.Quote("user") + `
|
|
|
|
) AS users,
|
2015-03-22 14:14:00 -05:00
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
2018-01-23 15:51:05 -06:00
|
|
|
FROM ` + dialect.Quote("org") + `
|
|
|
|
) AS orgs,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("dashboard") + `
|
|
|
|
) AS dashboards,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("data_source") + `
|
|
|
|
) AS datasources,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*) FROM ` + dialect.Quote("star") + `
|
|
|
|
) AS stars,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("playlist") + `
|
|
|
|
) AS playlists,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("alert") + `
|
|
|
|
) AS alerts,
|
2018-05-25 07:33:37 -05:00
|
|
|
(
|
|
|
|
SELECT COUNT(*) FROM ` + dialect.Quote("user") + ` where last_seen_at > ?
|
|
|
|
) as active_users,
|
|
|
|
(
|
|
|
|
SELECT COUNT(id) FROM ` + dialect.Quote("dashboard") + ` where is_folder = ?
|
|
|
|
) as folders,
|
|
|
|
(
|
|
|
|
SELECT COUNT(acl.id) FROM ` + dialect.Quote("dashboard_acl") + ` as acl inner join ` + dialect.Quote("dashboard") + ` as d on d.id = acl.dashboard_id where d.is_folder = ?
|
|
|
|
) as dashboard_permissions,
|
|
|
|
(
|
|
|
|
SELECT COUNT(acl.id) FROM ` + dialect.Quote("dashboard_acl") + ` as acl inner join ` + dialect.Quote("dashboard") + ` as d on d.id = acl.dashboard_id where d.is_folder = ?
|
|
|
|
) as folder_permissions,
|
|
|
|
(
|
|
|
|
SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_provisioning") + `
|
|
|
|
) as provisioned_dashboards,
|
|
|
|
(
|
|
|
|
SELECT COUNT(id) FROM ` + dialect.Quote("dashboard_snapshot") + `
|
|
|
|
) as snapshots,
|
|
|
|
(
|
|
|
|
SELECT COUNT(id) FROM ` + dialect.Quote("team") + `
|
|
|
|
) as teams
|
2015-03-22 14:14:00 -05:00
|
|
|
`
|
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
|
2015-03-22 14:14:00 -05:00
|
|
|
var stats m.SystemStats
|
2018-05-25 07:33:37 -05:00
|
|
|
_, err := x.SQL(rawSql, activeUserDeadlineDate, dialect.BooleanStr(true), dialect.BooleanStr(false), dialect.BooleanStr(true)).Get(&stats)
|
2015-03-22 14:14:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &stats
|
2018-04-17 03:20:01 -05:00
|
|
|
|
2015-03-22 14:14:00 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-01-24 13:01:33 -06:00
|
|
|
|
|
|
|
func GetAdminStats(query *m.GetAdminStatsQuery) error {
|
2016-01-24 23:18:17 -06:00
|
|
|
var rawSql = `SELECT
|
2018-01-23 15:51:05 -06:00
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("user") + `
|
|
|
|
) AS users,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("org") + `
|
|
|
|
) AS orgs,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("dashboard") + `
|
|
|
|
) AS dashboards,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("dashboard_snapshot") + `
|
|
|
|
) AS snapshots,
|
|
|
|
(
|
|
|
|
SELECT COUNT( DISTINCT ( ` + dialect.Quote("term") + ` ))
|
|
|
|
FROM ` + dialect.Quote("dashboard_tag") + `
|
|
|
|
) AS tags,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("data_source") + `
|
|
|
|
) AS datasources,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("playlist") + `
|
|
|
|
) AS playlists,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*) FROM ` + dialect.Quote("star") + `
|
|
|
|
) AS stars,
|
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM ` + dialect.Quote("alert") + `
|
|
|
|
) AS alerts,
|
2017-08-09 03:36:41 -05:00
|
|
|
(
|
|
|
|
SELECT COUNT(*)
|
2018-01-23 15:51:05 -06:00
|
|
|
from ` + dialect.Quote("user") + ` where last_seen_at > ?
|
2017-08-09 03:36:41 -05:00
|
|
|
) as active_users
|
2018-01-23 15:51:05 -06:00
|
|
|
`
|
2016-01-24 13:01:33 -06:00
|
|
|
|
2017-08-09 03:36:41 -05:00
|
|
|
activeUserDeadlineDate := time.Now().Add(-activeUserTimeLimit)
|
|
|
|
|
2016-01-24 23:18:17 -06:00
|
|
|
var stats m.AdminStats
|
2018-01-23 15:30:45 -06:00
|
|
|
_, err := x.SQL(rawSql, activeUserDeadlineDate).Get(&stats)
|
2016-01-24 23:18:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-24 13:01:33 -06:00
|
|
|
|
2016-01-24 23:18:17 -06:00
|
|
|
query.Result = &stats
|
|
|
|
return err
|
2016-01-24 13:01:33 -06:00
|
|
|
}
|
2018-05-25 07:33:37 -05:00
|
|
|
|
|
|
|
func GetSystemUserCountStats(query *m.GetSystemUserCountStatsQuery) error {
|
|
|
|
var rawSql = `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user")
|
|
|
|
var stats m.SystemUserCountStats
|
|
|
|
_, err := x.SQL(rawSql).Get(&stats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &stats
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|