Skip to content

Commit

Permalink
Query library: requiresDevMode dummy backend (#56466)
Browse files Browse the repository at this point in the history
* query library - dummy backend

* fix tests

* dont explicitly marshall backend dataresponse

* skip integration tests

* null check for tests

* added query library to codeowners

* null check for tests

* lint
  • Loading branch information
ArturWierzbicki committed Oct 7, 2022
1 parent 23e04c0 commit bf264d2
Show file tree
Hide file tree
Showing 24 changed files with 1,292 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ go.sum @grafana/backend-platform
/pkg/services/live/ @grafana/grafana-edge-squad
/pkg/services/searchV2/ @grafana/grafana-edge-squad
/pkg/services/store/ @grafana/grafana-edge-squad
/pkg/services/querylibrary/ @grafana/grafana-edge-squad
/pkg/services/export/ @grafana/grafana-edge-squad
/pkg/infra/filestore/ @grafana/grafana-edge-squad
/pkg/tsdb/testdatasource/sims/ @grafana/grafana-edge-squad
Expand Down
1 change: 1 addition & 0 deletions packages/grafana-data/src/types/featureToggles.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export interface FeatureToggles {
redshiftAsyncQueryDataSupport?: boolean;
athenaAsyncQueryDataSupport?: boolean;
increaseInMemDatabaseQueryCache?: boolean;
queryLibrary?: boolean;
}
4 changes: 4 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ func (hs *HTTPServer) registerRoutes() {
apiRoute.Group("/search-v2", hs.SearchV2HTTPService.RegisterHTTPRoutes)
}

if hs.QueryLibraryHTTPService != nil && !hs.QueryLibraryHTTPService.IsDisabled() {
apiRoute.Group("/query-library", hs.QueryLibraryHTTPService.RegisterHTTPRoutes)
}

// current org
apiRoute.Group("/org", func(orgRoute routing.RouteRegister) {
userIDScope := ac.Scope("users", "id", ac.Parameter(":userId"))
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ func (hs *HTTPServer) GetDashboard(c *models.ReqContext) response.Response {
return response.Error(500, "Error while loading library panels", err)
}

if hs.QueryLibraryService != nil && !hs.QueryLibraryService.IsDisabled() {
if err := hs.QueryLibraryService.UpdateDashboardQueries(c.Req.Context(), c.SignedInUser, dash); err != nil {
return response.Error(500, "Error while loading saved queries", err)
}
}

dto := dtos.DashboardFullWithMeta{
Dashboard: dash.Data,
Meta: meta,
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware/csrf"
"github.com/grafana/grafana/pkg/services/querylibrary"
"github.com/grafana/grafana/pkg/services/searchV2"
"github.com/grafana/grafana/pkg/services/store/object"
"github.com/grafana/grafana/pkg/services/userauth"
Expand Down Expand Up @@ -143,6 +144,8 @@ type HTTPServer struct {
StorageService store.StorageService
httpObjectStore object.HTTPObjectStore
SearchV2HTTPService searchV2.SearchHTTPService
QueryLibraryHTTPService querylibrary.HTTPService
QueryLibraryService querylibrary.Service
ContextHandler *contexthandler.ContextHandler
SQLStore sqlstore.Store
AlertEngine *alerting.AlertEngine
Expand Down Expand Up @@ -243,7 +246,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
loginAttemptService loginAttempt.Service, orgService org.Service, teamService team.Service,
accesscontrolService accesscontrol.Service, dashboardThumbsService thumbs.DashboardThumbService, navTreeService navtree.Service,
annotationRepo annotations.Repository, tagService tag.Service, searchv2HTTPService searchV2.SearchHTTPService,
userAuthService userauth.Service,
userAuthService userauth.Service, queryLibraryHTTPService querylibrary.HTTPService, queryLibraryService querylibrary.Service,
) (*HTTPServer, error) {
web.Env = cfg.Env
m := web.New()
Expand Down Expand Up @@ -346,6 +349,8 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
annotationsRepo: annotationRepo,
tagService: tagService,
userAuthService: userAuthService,
QueryLibraryHTTPService: queryLibraryHTTPService,
QueryLibraryService: queryLibraryService,
}
if hs.Listener != nil {
hs.log.Debug("Using provided listener")
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/manager/manager_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestIntegrationPluginManager(t *testing.T) {
pg := postgres.ProvideService(cfg)
my := mysql.ProvideService(cfg, hcp)
ms := mssql.ProvideService(cfg)
sv2 := searchV2.ProvideService(cfg, sqlstore.InitTestDB(t), nil, nil, tracer, features, nil, nil)
sv2 := searchV2.ProvideService(cfg, sqlstore.InitTestDB(t), nil, nil, tracer, features, nil, nil, nil)
graf := grafanads.ProvideService(sv2, nil)

coreRegistry := coreplugin.ProvideCoreRegistry(am, cw, cm, es, grap, idb, lk, otsdb, pr, tmpo, td, pg, my, ms, graf)
Expand Down
3 changes: 3 additions & 0 deletions pkg/server/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import (
publicdashboardsService "github.com/grafana/grafana/pkg/services/publicdashboards/service"
"github.com/grafana/grafana/pkg/services/query"
"github.com/grafana/grafana/pkg/services/queryhistory"
"github.com/grafana/grafana/pkg/services/querylibrary/querylibraryimpl"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/rendering"
"github.com/grafana/grafana/pkg/services/search"
Expand Down Expand Up @@ -278,6 +279,8 @@ var wireBasicSet = wire.NewSet(
secretsManager.ProvideSecretsService,
wire.Bind(new(secrets.Service), new(*secretsManager.SecretsService)),
secretsDatabase.ProvideSecretsStore,
querylibraryimpl.ProvideService,
querylibraryimpl.ProvideHTTPService,
wire.Bind(new(secrets.Store), new(*secretsDatabase.SecretsStoreImpl)),
secretsMigrator.ProvideSecretsMigrator,
wire.Bind(new(secrets.Migrator), new(*secretsMigrator.SecretsMigrator)),
Expand Down
6 changes: 6 additions & 0 deletions pkg/services/featuremgmt/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,11 @@ var (
Name: "increaseInMemDatabaseQueryCache",
Description: "Enable more in memory caching for database queries",
},
{
Name: "queryLibrary",
Description: "Reusable query library",
State: FeatureStateAlpha,
RequiresDevMode: true,
},
}
)
4 changes: 4 additions & 0 deletions pkg/services/featuremgmt/toggles_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,8 @@ const (
// FlagIncreaseInMemDatabaseQueryCache
// Enable more in memory caching for database queries
FlagIncreaseInMemDatabaseQueryCache = "increaseInMemDatabaseQueryCache"

// FlagQueryLibrary
// Reusable query library
FlagQueryLibrary = "queryLibrary"
)
1 change: 1 addition & 0 deletions pkg/services/navtree/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
WeightSavedItems
WeightCreate
WeightDashboard
WeightQueryLibrary
WeightExplore
WeightAlerting
WeightDataConnections
Expand Down
17 changes: 16 additions & 1 deletion pkg/services/navtree/navtreeimpl/navtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/pluginsettings"
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/querylibrary"
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/setting"
)
Expand All @@ -33,6 +34,7 @@ type ServiceImpl struct {
accesscontrolService ac.Service
kvStore kvstore.KVStore
apiKeyService apikey.Service
queryLibraryService querylibrary.HTTPService

// Navigation
navigationAppConfig map[string]NavigationAppConfig
Expand All @@ -44,7 +46,7 @@ type NavigationAppConfig struct {
SortWeight int64
}

func ProvideService(cfg *setting.Cfg, accessControl ac.AccessControl, pluginStore plugins.Store, pluginSettings pluginsettings.Service, starService star.Service, features *featuremgmt.FeatureManager, dashboardService dashboards.DashboardService, accesscontrolService ac.Service, kvStore kvstore.KVStore, apiKeyService apikey.Service) navtree.Service {
func ProvideService(cfg *setting.Cfg, accessControl ac.AccessControl, pluginStore plugins.Store, pluginSettings pluginsettings.Service, starService star.Service, features *featuremgmt.FeatureManager, dashboardService dashboards.DashboardService, accesscontrolService ac.Service, kvStore kvstore.KVStore, apiKeyService apikey.Service, queryLibraryService querylibrary.HTTPService) navtree.Service {
service := &ServiceImpl{
cfg: cfg,
log: log.New("navtree service"),
Expand All @@ -57,6 +59,7 @@ func ProvideService(cfg *setting.Cfg, accessControl ac.AccessControl, pluginStor
accesscontrolService: accesscontrolService,
kvStore: kvStore,
apiKeyService: apiKeyService,
queryLibraryService: queryLibraryService,
}

service.readNavigationSettings()
Expand Down Expand Up @@ -121,6 +124,18 @@ func (s *ServiceImpl) GetNavTree(c *models.ReqContext, hasEditPerm bool, prefs *
})
}

if !s.queryLibraryService.IsDisabled() {
treeRoot.AddSection(&navtree.NavLink{
Text: "Query Library",
Id: "query",
SubTitle: "Store, import, export and manage your team queries in an easy way.",
Icon: "file-search-alt",
SortWeight: navtree.WeightQueryLibrary,
Section: navtree.NavSectionCore,
Url: s.cfg.AppSubURL + "/query-library",
})
}

if setting.ProfileEnabled && c.IsSignedIn {
treeRoot.AddSection(s.getProfileNode(c))
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/services/querylibrary/querylibraryimpl/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package querylibraryimpl

import (
"encoding/json"
"fmt"
"io"
"strings"

"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/querylibrary"
)

type queriesServiceHTTPHandler struct {
service querylibrary.Service
}

func (s *queriesServiceHTTPHandler) IsDisabled() bool {
return s.service.IsDisabled()
}

func (s *queriesServiceHTTPHandler) delete(c *models.ReqContext) response.Response {
uid := c.Query("uid")
err := s.service.Delete(c.Req.Context(), c.SignedInUser, uid)
if err != nil {
return response.Error(500, fmt.Sprintf("error deleting query with id %s", uid), err)
}

return response.JSON(200, map[string]interface{}{
"success": true,
})
}

func (s *queriesServiceHTTPHandler) RegisterHTTPRoutes(routes routing.RouteRegister) {
reqSignedIn := middleware.ReqSignedIn
routes.Get("/", reqSignedIn, routing.Wrap(s.getBatch))
routes.Post("/", reqSignedIn, routing.Wrap(s.update))
routes.Delete("/", reqSignedIn, routing.Wrap(s.delete))
}

func (s *queriesServiceHTTPHandler) getBatch(c *models.ReqContext) response.Response {
uids := c.QueryStrings("uid")

queries, err := s.service.GetBatch(c.Req.Context(), c.SignedInUser, uids)
if err != nil {
return response.Error(500, fmt.Sprintf("error retrieving queries: [%s]", strings.Join(uids, ",")), err)
}

return response.JSON(200, queries)
}

func (s *queriesServiceHTTPHandler) update(c *models.ReqContext) response.Response {
body, err := io.ReadAll(c.Req.Body)
if err != nil {
return response.Error(500, "error reading bytes", err)
}

query := &querylibrary.Query{}
err = json.Unmarshal(body, query)
if err != nil {
return response.Error(400, "error parsing body", err)
}

if err := s.service.Update(c.Req.Context(), c.SignedInUser, query); err != nil {
var msg string
if len(query.UID) > 0 {
msg = fmt.Sprintf("error updating query with UID %s: %s", query.UID, err.Error())
} else {
msg = fmt.Sprintf("error updating query with: %s", err.Error())
}
return response.Error(500, msg, err)
}

return response.JSON(200, map[string]interface{}{
"success": true,
})
}

func ProvideHTTPService(
queriesService querylibrary.Service,
) querylibrary.HTTPService {
return &queriesServiceHTTPHandler{
service: queriesService,
}
}
Loading

0 comments on commit bf264d2

Please sign in to comment.