Просмотр исходного кода

Refactoring search to support more than just db dashboards

Torkel Ödegaard 10 лет назад
Родитель
Сommit
bb7d79e6d2

+ 1 - 0
CHANGELOG.md

@@ -17,6 +17,7 @@
 **Breaking changes**
 - [Issue #1928](https://github.com/grafana/grafana/issues/1928). HTTP API: GET /api/dashboards/db/:slug response changed property `model` to `dashboard` to match the POST request nameing
 - Backend render URL changed from `/render/dashboard/solo` `render/dashboard-solo/` (in order to have consistent dashboard url `/dashboard/:type/:slug`)
+- Search HTTP API response has changed (simplified), tags list moved to seperate HTTP resource URI
 
 # 2.0.3 (unreleased - 2.0.x branch)
 

+ 1 - 0
pkg/api/api.go

@@ -102,6 +102,7 @@ func Register(r *macaron.Macaron) {
 			r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
 			r.Get("/file/:file", GetDashboardFromJsonFile)
 			r.Get("/home", GetHomeDashboard)
+			r.Get("/tags", GetDashboardTags)
 		})
 
 		// Search

+ 11 - 0
pkg/api/dashboard.go

@@ -140,3 +140,14 @@ func GetDashboardFromJsonFile(c *middleware.Context) {
 
 	c.JSON(200, &dash)
 }
+
+func GetDashboardTags(c *middleware.Context) {
+	query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
+	err := bus.Dispatch(&query)
+	if err != nil {
+		c.JsonApiErr(500, "Failed to get tags from database", err)
+		return
+	}
+
+	c.JSON(200, query.Result)
+}

+ 12 - 35
pkg/api/search.go

@@ -3,14 +3,12 @@ package api
 import (
 	"github.com/grafana/grafana/pkg/bus"
 	"github.com/grafana/grafana/pkg/middleware"
-	m "github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/services/search"
 )
 
 func Search(c *middleware.Context) {
 	query := c.Query("query")
 	tag := c.Query("tag")
-	tagcloud := c.Query("tagcloud")
 	starred := c.Query("starred")
 	limit := c.QueryInt("limit")
 
@@ -18,41 +16,20 @@ func Search(c *middleware.Context) {
 		limit = 200
 	}
 
-	result := m.SearchResult{
-		Dashboards: []*m.DashboardSearchHit{},
-		Tags:       []*m.DashboardTagCloudItem{},
+	searchQuery := search.Query{
+		Title:     query,
+		Tag:       tag,
+		UserId:    c.UserId,
+		Limit:     limit,
+		IsStarred: starred == "true",
+		OrgId:     c.OrgId,
 	}
 
-	if tagcloud == "true" {
-
-		query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
-		err := bus.Dispatch(&query)
-		if err != nil {
-			c.JsonApiErr(500, "Failed to get tags from database", err)
-			return
-		}
-		result.Tags = query.Result
-		result.TagsOnly = true
-
-	} else {
-
-		query := search.Query{
-			Title:     query,
-			Tag:       tag,
-			UserId:    c.UserId,
-			Limit:     limit,
-			IsStarred: starred == "true",
-			OrgId:     c.OrgId,
-		}
-
-		err := bus.Dispatch(&query)
-		if err != nil {
-			c.JsonApiErr(500, "Search failed", err)
-			return
-		}
-
-		result.Dashboards = query.Result
+	err := bus.Dispatch(&searchQuery)
+	if err != nil {
+		c.JsonApiErr(500, "Search failed", err)
+		return
 	}
 
-	c.JSON(200, result)
+	c.JSON(200, searchQuery.Result)
 }

+ 15 - 4
pkg/services/search/json_index.go

@@ -24,11 +24,10 @@ type JsonDashIndexItem struct {
 }
 
 func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex {
+	log.Info("Creating json dashboard index for path: ", path)
+
 	index := JsonDashIndex{}
 	index.path = path
-	// if orgIds != ""  || orgIds != "*" {
-	// }
-
 	index.updateIndex()
 	return &index
 }
@@ -37,8 +36,21 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error
 	results := make([]*m.DashboardSearchHit, 0)
 
 	for _, item := range index.items {
+		if len(results) > query.Limit {
+			break
+		}
+
+		// filter out results with tag filter
+		if query.Tag != "" {
+			if !strings.Contains(item.TagsCsv, query.Tag) {
+				continue
+			}
+		}
+
+		// add results with matchig title filter
 		if strings.Contains(item.TitleLower, query.Title) {
 			results = append(results, &m.DashboardSearchHit{
+				Type:  m.DashTypeJson,
 				Title: item.Dashboard.Title,
 				Tags:  item.Dashboard.GetTags(),
 				Uri:   "file/" + item.Path,
@@ -60,7 +72,6 @@ func (index *JsonDashIndex) GetDashboard(path string) *m.Dashboard {
 }
 
 func (index *JsonDashIndex) updateIndex() error {
-	log.Info("Updating JSON dashboard index, path: %v", index.path)
 
 	index.items = make([]*JsonDashIndexItem, 0)
 

+ 1 - 0
pkg/services/sqlstore/dashboard.go

@@ -176,6 +176,7 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error {
 				Id:    item.Id,
 				Title: item.Title,
 				Uri:   "db/" + item.Slug,
+				Type:  m.DashTypeDB,
 				Tags:  []string{},
 			}
 			query.Result = append(query.Result, hit)

+ 10 - 8
public/app/controllers/search.js

@@ -61,21 +61,21 @@ function (angular, _, config) {
     };
 
     $scope.searchDashboards = function() {
+      $scope.tagsMode = false;
       $scope.currentSearchId = $scope.currentSearchId + 1;
       var localSearchId = $scope.currentSearchId;
 
       return backendSrv.search($scope.query).then(function(results) {
         if (localSearchId < $scope.currentSearchId) { return; }
 
-        $scope.resultCount = results.tagsOnly ? results.tags.length : results.dashboards.length;
-        $scope.results.tags = results.tags;
-        $scope.results.dashboards = _.map(results.dashboards, function(dash) {
+        $scope.resultCount = results.length;
+        $scope.results = _.map(results, function(dash) {
           dash.url = 'dashboard/' + dash.uri;
           return dash;
         });
 
         if ($scope.queryHasNoFilters()) {
-          $scope.results.dashboards.unshift({ title: 'Home', url: config.appSubUrl + '/', isHome: true });
+          $scope.results.unshift({ title: 'Home', url: config.appSubUrl + '/', isHome: true });
         }
       });
     };
@@ -96,10 +96,12 @@ function (angular, _, config) {
       }
     };
 
-    $scope.showTags = function() {
-      $scope.query.tagcloud = !$scope.query.tagcloud;
-      $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
-      $scope.search();
+    $scope.getTags = function() {
+      $scope.tagsMode = true;
+      return backendSrv.get('/api/dashboards/tags').then(function(results) {
+        $scope.resultCount = results.length;
+        $scope.results = results;
+      });
     };
 
     $scope.showStarred = function() {

+ 1 - 1
public/app/features/dashboard/dashboardCtrl.js

@@ -70,7 +70,7 @@ function (angular, $, config) {
     };
 
     $scope.updateTopNavPartial = function() {
-      if ($scope.dashboard.meta.type === 'snapshot') {
+      if ($scope.dashboard.meta.isSnapshot) {
         $scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html';
       }
     };

+ 9 - 10
public/app/partials/search.html

@@ -11,8 +11,8 @@
 				<i class="fa fa-remove" ng-show="query.starred"></i>
 				starred
 			</a> |
-			<a class="pointer" href="javascript:void 0;" ng-click="showTags()" tabindex="3">
-				<i class="fa fa-remove" ng-show="query.tagcloud"></i>
+			<a class="pointer" href="javascript:void 0;" ng-click="getTags()" tabindex="3">
+				<i class="fa fa-remove" ng-show="tagsMode"></i>
 				tags
 			</a>
 			<span ng-show="query.tag">
@@ -25,10 +25,10 @@
 	</div>
 
 	<div ng-if="!showImport">
-		<div class="search-results-container" ng-if="query.tagcloud">
+		<div class="search-results-container" ng-if="tagsMode">
 			<div class="row">
 				<div class="span6 offset1">
-					<div ng-repeat="tag in results.tags" class="pointer" style="width: 180px; float: left;"
+					<div ng-repeat="tag in results" class="pointer" style="width: 180px; float: left;"
 						ng-class="{'selected': $index === selectedIndex }"
 						ng-click="filterByTag(tag.term, $event)">
 						<a class="search-result-tag label label-tag" tag-color-from-name tag="tag.term">
@@ -40,11 +40,11 @@
 			</div>
 		</div>
 
-		<div class="search-results-container" ng-if="!query.tagcloud">
-			<h6 ng-hide="results.dashboards.length">No dashboards matching your query were found.</h6>
+		<div class="search-results-container" ng-if="!tagsMode">
+			<h6 ng-hide="results.length">No dashboards matching your query were found.</h6>
 
-			<a class="search-result-item pointer" bindonce ng-repeat="row in results.dashboards"
-				ng-class="{'selected': $index === selectedIndex }" ng-href="{{row.url}}">
+			<a class="search-result-item pointer search-result-item-{{row.type}}" bindonce ng-repeat="row in results"
+				ng-class="{'selected': $index == selectedIndex}" ng-href="{{row.url}}">
 
 				<span class="search-result-tags">
 					<span ng-click="filterByTag(tag, $event)" ng-repeat="tag in row.tags" tag-color-from-name tag="tag"  class="label label-tag">
@@ -54,8 +54,7 @@
 				</span>
 
 				<span class="search-result-link">
-					<i class="fa fa-th-large" ng-show="!row.isHome"></i>
-					<i class="fa fa-home" ng-show="row.isHome"></i>
+					<i class="search-result-icon"></i>
 					<span bo-text="row.title"></span>
 				</span>
 			</a>