Instrumentation: add context.Context to the dashboard get flow. (#34955)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
Carl Bergquist
2021-06-15 16:08:27 +02:00
committed by GitHub
parent 93860a90d8
commit b40e78a943
7 changed files with 63 additions and 48 deletions
+21 -14
View File
@@ -28,6 +28,7 @@ var shadowSearchCounter = prometheus.NewCounterVec(
func init() {
bus.AddHandler("sql", GetDashboard)
bus.AddHandler("sql", GetDashboards)
bus.AddHandlerCtx("sql", GetDashboardCtx)
bus.AddHandler("sql", DeleteDashboard)
bus.AddHandler("sql", SearchDashboards)
bus.AddHandler("sql", GetDashboardTags)
@@ -230,23 +231,29 @@ func (ss *SQLStore) GetFolderByTitle(orgID int64, title string) (*models.Dashboa
// TODO: Remove me
func GetDashboard(query *models.GetDashboardQuery) error {
if query.Id == 0 && len(query.Slug) == 0 && len(query.Uid) == 0 {
return models.ErrDashboardIdentifierNotSet
}
return GetDashboardCtx(context.Background(), query)
}
dashboard := models.Dashboard{Slug: query.Slug, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
has, err := x.Get(&dashboard)
func GetDashboardCtx(ctx context.Context, query *models.GetDashboardQuery) error {
return withDbSession(ctx, x, func(dbSession *DBSession) error {
if query.Id == 0 && len(query.Slug) == 0 && len(query.Uid) == 0 {
return models.ErrDashboardIdentifierNotSet
}
if err != nil {
return err
} else if !has {
return models.ErrDashboardNotFound
}
dashboard := models.Dashboard{Slug: query.Slug, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
has, err := dbSession.Get(&dashboard)
dashboard.SetId(dashboard.Id)
dashboard.SetUid(dashboard.Uid)
query.Result = &dashboard
return nil
if err != nil {
return err
} else if !has {
return models.ErrDashboardNotFound
}
dashboard.SetId(dashboard.Id)
dashboard.SetUid(dashboard.Uid)
query.Result = &dashboard
return nil
})
}
type DashboardSearchProjection struct {
+17 -13
View File
@@ -1,6 +1,8 @@
package sqlstore
import (
"context"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
)
@@ -9,24 +11,26 @@ func init() {
bus.AddHandler("sql", StarDashboard)
bus.AddHandler("sql", UnstarDashboard)
bus.AddHandler("sql", GetUserStars)
bus.AddHandler("sql", IsStarredByUser)
bus.AddHandlerCtx("sql", IsStarredByUserCtx)
}
func IsStarredByUser(query *models.IsStarredByUserQuery) error {
rawSQL := "SELECT 1 from star where user_id=? and dashboard_id=?"
results, err := x.Query(rawSQL, query.UserId, query.DashboardId)
func IsStarredByUserCtx(ctx context.Context, query *models.IsStarredByUserQuery) error {
return withDbSession(ctx, x, func(dbSession *DBSession) error {
rawSQL := "SELECT 1 from star where user_id=? and dashboard_id=?"
results, err := dbSession.Query(rawSQL, query.UserId, query.DashboardId)
if err != nil {
return err
}
if err != nil {
return err
}
if len(results) == 0 {
return nil
}
query.Result = true
if len(results) == 0 {
return nil
}
query.Result = true
return nil
})
}
func StarDashboard(cmd *models.StarDashboardCommand) error {
+3 -2
View File
@@ -3,6 +3,7 @@
package sqlstore
import (
"context"
"testing"
"github.com/grafana/grafana/pkg/models"
@@ -24,7 +25,7 @@ func TestUserStarsDataAccess(t *testing.T) {
Convey("IsStarredByUser should return true when starred", func() {
query := models.IsStarredByUserQuery{UserId: 12, DashboardId: 10}
err := IsStarredByUser(&query)
err := IsStarredByUserCtx(context.Background(), &query)
So(err, ShouldBeNil)
So(query.Result, ShouldBeTrue)
@@ -32,7 +33,7 @@ func TestUserStarsDataAccess(t *testing.T) {
Convey("IsStarredByUser should return false when not starred", func() {
query := models.IsStarredByUserQuery{UserId: 12, DashboardId: 12}
err := IsStarredByUser(&query)
err := IsStarredByUserCtx(context.Background(), &query)
So(err, ShouldBeNil)
So(query.Result, ShouldBeFalse)