mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Added backend API for stats
This commit is contained in:
parent
c260c319ee
commit
c7fae5386d
@ -5,6 +5,8 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/middleware"
|
"github.com/grafana/grafana/pkg/middleware"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AdminGetSettings(c *middleware.Context) {
|
func AdminGetSettings(c *middleware.Context) {
|
||||||
@ -27,3 +29,15 @@ func AdminGetSettings(c *middleware.Context) {
|
|||||||
|
|
||||||
c.JSON(200, settings)
|
c.JSON(200, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AdminGetStats(c *middleware.Context) {
|
||||||
|
|
||||||
|
statsQuery := m.GetAdminStatsQuery{}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&statsQuery); err != nil {
|
||||||
|
c.JsonApiErr(500, "Failed to get admin stats from database", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, statsQuery.Result)
|
||||||
|
}
|
@ -40,6 +40,7 @@ func Register(r *macaron.Macaron) {
|
|||||||
r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
|
r.Get("/admin/users/edit/:id", reqGrafanaAdmin, Index)
|
||||||
r.Get("/admin/orgs", reqGrafanaAdmin, Index)
|
r.Get("/admin/orgs", reqGrafanaAdmin, Index)
|
||||||
r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
|
r.Get("/admin/orgs/edit/:id", reqGrafanaAdmin, Index)
|
||||||
|
r.Get("/admin/stats", reqGrafanaAdmin, Index)
|
||||||
|
|
||||||
r.Get("/apps", reqSignedIn, Index)
|
r.Get("/apps", reqSignedIn, Index)
|
||||||
r.Get("/apps/edit/*", reqSignedIn, Index)
|
r.Get("/apps/edit/*", reqSignedIn, Index)
|
||||||
@ -210,6 +211,7 @@ func Register(r *macaron.Macaron) {
|
|||||||
r.Delete("/users/:id", AdminDeleteUser)
|
r.Delete("/users/:id", AdminDeleteUser)
|
||||||
r.Get("/users/:id/quotas", wrap(GetUserQuotas))
|
r.Get("/users/:id/quotas", wrap(GetUserQuotas))
|
||||||
r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
|
r.Put("/users/:id/quotas/:target", bind(m.UpdateUserQuotaCmd{}), wrap(UpdateUserQuota))
|
||||||
|
r.Get("/stats", AdminGetStats)
|
||||||
}, reqGrafanaAdmin)
|
}, reqGrafanaAdmin)
|
||||||
|
|
||||||
// rendering
|
// rendering
|
||||||
|
@ -18,3 +18,18 @@ type GetSystemStatsQuery struct {
|
|||||||
type GetDataSourceStatsQuery struct {
|
type GetDataSourceStatsQuery struct {
|
||||||
Result []*DataSourceStats
|
Result []*DataSourceStats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AdminStats struct {
|
||||||
|
UserCount int
|
||||||
|
OrgCount int
|
||||||
|
DashboardCount int
|
||||||
|
DBSnapshotCount int
|
||||||
|
DBTagCount int
|
||||||
|
DataSourceCount int
|
||||||
|
PlaylistCount int
|
||||||
|
StarredDBCount int
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAdminStatsQuery struct {
|
||||||
|
Result *AdminStats
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
bus.AddHandler("sql", GetSystemStats)
|
bus.AddHandler("sql", GetSystemStats)
|
||||||
bus.AddHandler("sql", GetDataSourceStats)
|
bus.AddHandler("sql", GetDataSourceStats)
|
||||||
|
bus.AddHandler("sql", GetAdminStats)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDataSourceStats(query *m.GetDataSourceStatsQuery) error {
|
func GetDataSourceStats(query *m.GetDataSourceStatsQuery) error {
|
||||||
@ -46,3 +47,49 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
|
|||||||
query.Result = &stats
|
query.Result = &stats
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAdminStats(query *m.GetAdminStatsQuery) error {
|
||||||
|
var rawSql = `SELECT
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("user") + `
|
||||||
|
) AS user_count,
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("org") + `
|
||||||
|
) AS org_count,
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("dashboard") + `
|
||||||
|
) AS dashboard_count,
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("dashboard_snapshot") + `
|
||||||
|
) AS db_snapshot_count,
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("dashboard_tag") + `
|
||||||
|
) AS db_tag_count,
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("data_source") + `
|
||||||
|
) AS datasource_count,
|
||||||
|
(
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM ` + dialect.Quote("playlist") + `
|
||||||
|
) AS playlist_count,
|
||||||
|
(
|
||||||
|
SELECT DISTINCT(dashboard_id)
|
||||||
|
FROM ` + dialect.Quote("star") + `
|
||||||
|
) AS starred_db_count
|
||||||
|
`
|
||||||
|
|
||||||
|
var stats m.AdminStats
|
||||||
|
_, err := x.Sql(rawSql).Get(&stats)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
query.Result = &stats
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user