Commit da67afa5 by utkarshcmu

Fixed api bugs, stats endpoint working

parent c7fae538
...@@ -3,10 +3,10 @@ package api ...@@ -3,10 +3,10 @@ package api
import ( import (
"strings" "strings"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
"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) {
...@@ -32,12 +32,12 @@ func AdminGetSettings(c *middleware.Context) { ...@@ -32,12 +32,12 @@ func AdminGetSettings(c *middleware.Context) {
func AdminGetStats(c *middleware.Context) { func AdminGetStats(c *middleware.Context) {
statsQuery := m.GetAdminStatsQuery{} 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) 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,7 +40,7 @@ func Register(r *macaron.Macaron) { ...@@ -40,7 +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("/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)
...@@ -211,7 +211,7 @@ func Register(r *macaron.Macaron) { ...@@ -211,7 +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) r.Get("/stats", AdminGetStats)
}, reqGrafanaAdmin) }, reqGrafanaAdmin)
// rendering // rendering
......
...@@ -20,16 +20,16 @@ type GetDataSourceStatsQuery struct { ...@@ -20,16 +20,16 @@ type GetDataSourceStatsQuery struct {
} }
type AdminStats struct { type AdminStats struct {
UserCount int UserCount int `json:"user_count"`
OrgCount int OrgCount int `json:"org_count"`
DashboardCount int DashboardCount int `json:"dashboard_count"`
DBSnapshotCount int DbSnapshotCount int `json:"db_snapshot_count"`
DBTagCount int DbTagCount int `json:"db_tag_count"`
DataSourceCount int DataSourceCount int `json:"data_source_count"`
PlaylistCount int PlaylistCount int `json:"playlist_count"`
StarredDBCount int StarredDbCount int `json:"starred_db_count"`
} }
type GetAdminStatsQuery struct { type GetAdminStatsQuery struct {
Result *AdminStats Result *AdminStats
} }
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +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) bus.AddHandler("sql", GetAdminStats)
} }
func GetDataSourceStats(query *m.GetDataSourceStatsQuery) error { func GetDataSourceStats(query *m.GetDataSourceStatsQuery) error {
...@@ -49,7 +49,7 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error { ...@@ -49,7 +49,7 @@ func GetSystemStats(query *m.GetSystemStatsQuery) error {
} }
func GetAdminStats(query *m.GetAdminStatsQuery) error { func GetAdminStats(query *m.GetAdminStatsQuery) error {
var rawSql = `SELECT var rawSql = `SELECT
( (
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("user") + ` FROM ` + dialect.Quote("user") + `
...@@ -73,23 +73,23 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error { ...@@ -73,23 +73,23 @@ func GetAdminStats(query *m.GetAdminStatsQuery) error {
( (
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("data_source") + ` FROM ` + dialect.Quote("data_source") + `
) AS datasource_count, ) AS data_source_count,
( (
SELECT COUNT(*) SELECT COUNT(*)
FROM ` + dialect.Quote("playlist") + ` FROM ` + dialect.Quote("playlist") + `
) AS playlist_count, ) AS playlist_count,
( (
SELECT DISTINCT(dashboard_id) SELECT COUNT (DISTINCT ` + dialect.Quote("dashboard_id") + ` )
FROM ` + dialect.Quote("star") + ` FROM ` + dialect.Quote("star") + `
) AS starred_db_count ) AS starred_db_count
` `
var stats m.AdminStats var stats m.AdminStats
_, err := x.Sql(rawSql).Get(&stats) _, err := x.Sql(rawSql).Get(&stats)
if err != nil { if err != nil {
return err return err
} }
query.Result = &stats query.Result = &stats
return err return err
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment