Commit bb7d79e6 by Torkel Ödegaard

Refactoring search to support more than just db dashboards

parent fb35f721
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
**Breaking changes** **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 - [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`) - 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) # 2.0.3 (unreleased - 2.0.x branch)
......
...@@ -102,6 +102,7 @@ func Register(r *macaron.Macaron) { ...@@ -102,6 +102,7 @@ func Register(r *macaron.Macaron) {
r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard) r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
r.Get("/file/:file", GetDashboardFromJsonFile) r.Get("/file/:file", GetDashboardFromJsonFile)
r.Get("/home", GetHomeDashboard) r.Get("/home", GetHomeDashboard)
r.Get("/tags", GetDashboardTags)
}) })
// Search // Search
......
...@@ -140,3 +140,14 @@ func GetDashboardFromJsonFile(c *middleware.Context) { ...@@ -140,3 +140,14 @@ func GetDashboardFromJsonFile(c *middleware.Context) {
c.JSON(200, &dash) 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)
}
...@@ -3,14 +3,12 @@ package api ...@@ -3,14 +3,12 @@ package api
import ( import (
"github.com/grafana/grafana/pkg/bus" "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/services/search" "github.com/grafana/grafana/pkg/services/search"
) )
func Search(c *middleware.Context) { func Search(c *middleware.Context) {
query := c.Query("query") query := c.Query("query")
tag := c.Query("tag") tag := c.Query("tag")
tagcloud := c.Query("tagcloud")
starred := c.Query("starred") starred := c.Query("starred")
limit := c.QueryInt("limit") limit := c.QueryInt("limit")
...@@ -18,41 +16,20 @@ func Search(c *middleware.Context) { ...@@ -18,41 +16,20 @@ func Search(c *middleware.Context) {
limit = 200 limit = 200
} }
result := m.SearchResult{ searchQuery := search.Query{
Dashboards: []*m.DashboardSearchHit{}, Title: query,
Tags: []*m.DashboardTagCloudItem{}, Tag: tag,
UserId: c.UserId,
Limit: limit,
IsStarred: starred == "true",
OrgId: c.OrgId,
} }
if tagcloud == "true" { err := bus.Dispatch(&searchQuery)
if err != nil {
query := m.GetDashboardTagsQuery{OrgId: c.OrgId} c.JsonApiErr(500, "Search failed", err)
err := bus.Dispatch(&query) return
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
} }
c.JSON(200, result) c.JSON(200, searchQuery.Result)
} }
...@@ -24,11 +24,10 @@ type JsonDashIndexItem struct { ...@@ -24,11 +24,10 @@ type JsonDashIndexItem struct {
} }
func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex { func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex {
log.Info("Creating json dashboard index for path: ", path)
index := JsonDashIndex{} index := JsonDashIndex{}
index.path = path index.path = path
// if orgIds != "" || orgIds != "*" {
// }
index.updateIndex() index.updateIndex()
return &index return &index
} }
...@@ -37,8 +36,21 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error ...@@ -37,8 +36,21 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error
results := make([]*m.DashboardSearchHit, 0) results := make([]*m.DashboardSearchHit, 0)
for _, item := range index.items { 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) { if strings.Contains(item.TitleLower, query.Title) {
results = append(results, &m.DashboardSearchHit{ results = append(results, &m.DashboardSearchHit{
Type: m.DashTypeJson,
Title: item.Dashboard.Title, Title: item.Dashboard.Title,
Tags: item.Dashboard.GetTags(), Tags: item.Dashboard.GetTags(),
Uri: "file/" + item.Path, Uri: "file/" + item.Path,
...@@ -60,7 +72,6 @@ func (index *JsonDashIndex) GetDashboard(path string) *m.Dashboard { ...@@ -60,7 +72,6 @@ func (index *JsonDashIndex) GetDashboard(path string) *m.Dashboard {
} }
func (index *JsonDashIndex) updateIndex() error { func (index *JsonDashIndex) updateIndex() error {
log.Info("Updating JSON dashboard index, path: %v", index.path)
index.items = make([]*JsonDashIndexItem, 0) index.items = make([]*JsonDashIndexItem, 0)
......
...@@ -176,6 +176,7 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error { ...@@ -176,6 +176,7 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error {
Id: item.Id, Id: item.Id,
Title: item.Title, Title: item.Title,
Uri: "db/" + item.Slug, Uri: "db/" + item.Slug,
Type: m.DashTypeDB,
Tags: []string{}, Tags: []string{},
} }
query.Result = append(query.Result, hit) query.Result = append(query.Result, hit)
......
...@@ -61,21 +61,21 @@ function (angular, _, config) { ...@@ -61,21 +61,21 @@ function (angular, _, config) {
}; };
$scope.searchDashboards = function() { $scope.searchDashboards = function() {
$scope.tagsMode = false;
$scope.currentSearchId = $scope.currentSearchId + 1; $scope.currentSearchId = $scope.currentSearchId + 1;
var localSearchId = $scope.currentSearchId; var localSearchId = $scope.currentSearchId;
return backendSrv.search($scope.query).then(function(results) { return backendSrv.search($scope.query).then(function(results) {
if (localSearchId < $scope.currentSearchId) { return; } if (localSearchId < $scope.currentSearchId) { return; }
$scope.resultCount = results.tagsOnly ? results.tags.length : results.dashboards.length; $scope.resultCount = results.length;
$scope.results.tags = results.tags; $scope.results = _.map(results, function(dash) {
$scope.results.dashboards = _.map(results.dashboards, function(dash) {
dash.url = 'dashboard/' + dash.uri; dash.url = 'dashboard/' + dash.uri;
return dash; return dash;
}); });
if ($scope.queryHasNoFilters()) { 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) { ...@@ -96,10 +96,12 @@ function (angular, _, config) {
} }
}; };
$scope.showTags = function() { $scope.getTags = function() {
$scope.query.tagcloud = !$scope.query.tagcloud; $scope.tagsMode = true;
$scope.giveSearchFocus = $scope.giveSearchFocus + 1; return backendSrv.get('/api/dashboards/tags').then(function(results) {
$scope.search(); $scope.resultCount = results.length;
$scope.results = results;
});
}; };
$scope.showStarred = function() { $scope.showStarred = function() {
......
...@@ -70,7 +70,7 @@ function (angular, $, config) { ...@@ -70,7 +70,7 @@ function (angular, $, config) {
}; };
$scope.updateTopNavPartial = function() { $scope.updateTopNavPartial = function() {
if ($scope.dashboard.meta.type === 'snapshot') { if ($scope.dashboard.meta.isSnapshot) {
$scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html'; $scope.topNavPartial = 'app/features/dashboard/partials/snapshotTopNav.html';
} }
}; };
......
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
<i class="fa fa-remove" ng-show="query.starred"></i> <i class="fa fa-remove" ng-show="query.starred"></i>
starred starred
</a> | </a> |
<a class="pointer" href="javascript:void 0;" ng-click="showTags()" tabindex="3"> <a class="pointer" href="javascript:void 0;" ng-click="getTags()" tabindex="3">
<i class="fa fa-remove" ng-show="query.tagcloud"></i> <i class="fa fa-remove" ng-show="tagsMode"></i>
tags tags
</a> </a>
<span ng-show="query.tag"> <span ng-show="query.tag">
...@@ -25,10 +25,10 @@ ...@@ -25,10 +25,10 @@
</div> </div>
<div ng-if="!showImport"> <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="row">
<div class="span6 offset1"> <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-class="{'selected': $index === selectedIndex }"
ng-click="filterByTag(tag.term, $event)"> ng-click="filterByTag(tag.term, $event)">
<a class="search-result-tag label label-tag" tag-color-from-name tag="tag.term"> <a class="search-result-tag label label-tag" tag-color-from-name tag="tag.term">
...@@ -40,11 +40,11 @@ ...@@ -40,11 +40,11 @@
</div> </div>
</div> </div>
<div class="search-results-container" ng-if="!query.tagcloud"> <div class="search-results-container" ng-if="!tagsMode">
<h6 ng-hide="results.dashboards.length">No dashboards matching your query were found.</h6> <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" <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}}"> ng-class="{'selected': $index == selectedIndex}" ng-href="{{row.url}}">
<span class="search-result-tags"> <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"> <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 @@ ...@@ -54,8 +54,7 @@
</span> </span>
<span class="search-result-link"> <span class="search-result-link">
<i class="fa fa-th-large" ng-show="!row.isHome"></i> <i class="search-result-icon"></i>
<i class="fa fa-home" ng-show="row.isHome"></i>
<span bo-text="row.title"></span> <span bo-text="row.title"></span>
</span> </span>
</a> </a>
......
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