Skip to content

Commit dceda6e

Browse files
committed
Closes grafana#86, dashboard tags and tag search feature is now done! turned out pretty good!
1 parent ef26492 commit dceda6e

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

src/app/controllers/search.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function (angular, _, config, $) {
1414
$scope.init = function() {
1515
$scope.giveSearchFocus = 0;
1616
$scope.selectedIndex = -1;
17+
$scope.results = {dashboards: [], tags: [], metrics: []};
1718
$scope.query = { query: 'title:' };
1819
$rootScope.$on('open-search', $scope.openSearch);
1920
};
@@ -29,7 +30,15 @@ function (angular, _, config, $) {
2930
$scope.selectedIndex--;
3031
}
3132
if (evt.keyCode === 13) {
32-
var selectedDash = $scope.search_results.dashboards[$scope.selectedIndex];
33+
if ($scope.tagsOnly) {
34+
var tag = $scope.results.tags[$scope.selectedIndex];
35+
if (tag) {
36+
$scope.filterByTag(tag.term);
37+
}
38+
return;
39+
}
40+
41+
var selectedDash = $scope.results.dashboards[$scope.selectedIndex];
3342
if (selectedDash) {
3443
$location.path("/dashboard/elasticsearch/" + encodeURIComponent(selectedDash._id));
3544
setTimeout(function(){
@@ -39,15 +48,21 @@ function (angular, _, config, $) {
3948
}
4049
};
4150

42-
$scope.elasticsearch_dashboards = function(query) {
51+
$scope.searchDasboards = function(query) {
4352
var request = $scope.ejs.Request().indices(config.grafana_index).types('dashboard');
44-
45-
if (query.length === 0) {
46-
query = 'title:';
53+
var tagsOnly = query.indexOf('tags!:') === 0;
54+
if (tagsOnly) {
55+
var tagsQuery = query.substring(6, query.length);
56+
query = 'tags:' + tagsQuery + '*';
4757
}
58+
else {
59+
if (query.length === 0) {
60+
query = 'title:';
61+
}
4862

49-
if (query[query.length - 1] !== '*') {
50-
query += '*';
63+
if (query[query.length - 1] !== '*') {
64+
query += '*';
65+
}
5166
}
5267

5368
return request
@@ -58,28 +73,31 @@ function (angular, _, config, $) {
5873
.then(function(results) {
5974

6075
if(_.isUndefined(results.hits)) {
61-
$scope.search_results = { dashboards: [] };
76+
$scope.results.dashboards = [];
77+
$scope.results.tags = [];
6278
return;
6379
}
6480

65-
$scope.search_results = { dashboards: results.hits.hits };
66-
$scope.tags = results.facets.tags.terms;
81+
$scope.tagsOnly = tagsOnly;
82+
$scope.results.dashboards = results.hits.hits;
83+
$scope.results.tags = results.facets.tags.terms;
6784
});
6885
};
6986

7087
$scope.filterByTag = function(tag, evt) {
7188
$scope.query.query = "tags:" + tag + " AND title:";
7289
$scope.search();
73-
$scope.tagsOnly = false;
7490
$scope.giveSearchFocus = $scope.giveSearchFocus + 1;
75-
evt.stopPropagation();
76-
evt.preventDefault();
91+
if (evt) {
92+
evt.stopPropagation();
93+
evt.preventDefault();
94+
}
7795
};
7896

7997
$scope.showTags = function(evt) {
8098
evt.stopPropagation();
8199
$scope.tagsOnly = !$scope.tagsOnly;
82-
$scope.query.query = $scope.tagsOnly ? "tags!" : "";
100+
$scope.query.query = $scope.tagsOnly ? "tags!:" : "";
83101
$scope.giveSearchFocus = $scope.giveSearchFocus + 1;
84102
$scope.selectedIndex = -1;
85103
};
@@ -92,7 +110,7 @@ function (angular, _, config, $) {
92110

93111
if (queryStr.indexOf('m:') !== 0) {
94112
queryStr = queryStr.replace(' and ', ' AND ');
95-
$scope.elasticsearch_dashboards(queryStr);
113+
$scope.searchDasboards(queryStr);
96114
return;
97115
}
98116

@@ -115,10 +133,10 @@ function (angular, _, config, $) {
115133

116134
results.then(function(results) {
117135
if (results && results.hits && results.hits.hits.length > 0) {
118-
$scope.search_results = { metrics: results.hits.hits };
136+
$scope.results.metrics = { metrics: results.hits.hits };
119137
}
120138
else {
121-
$scope.search_results = { metric: [] };
139+
$scope.results.metrics = { metric: [] };
122140
}
123141
});
124142
};

src/app/partials/search.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@
4141
</span>
4242
</div>
4343

44-
<h6 ng-hide="search_results.dashboards.length || search_results.metrics.length">No dashboards or metrics matching your query found</h6>
44+
<h6 ng-hide="results.dashboards.length || results.metrics.length">No dashboards or metrics matching your query found</h6>
4545

4646
<table class="table table-condensed table-striped" ng-if="tagsOnly">
47-
<tr ng-repeat="tag in tags" ng-class="{'selected-tag': $index === selectedIndex }">
47+
<tr ng-repeat="tag in results.tags" ng-class="{'selected-tag': $index === selectedIndex }">
4848
<td>
4949
<a ng-click="filterByTag(tag.term, $event)" class="label label-tag">
50-
{{tag.term}}
50+
{{tag.term}} &nbsp;({{tag.count}})
5151
</a>
5252
</td>
53-
<td style="width:100%;">
54-
{{tag.count}}
53+
<td style="width:100%;padding-left: 10px;font-weight: bold;">
54+
5555
</td>
5656
</tr>
5757
</table>
5858

5959
<table class="table table-condensed table-striped" ng-if="!tagsOnly">
60-
<tr bindonce ng-repeat="row in search_results.metrics"
60+
<tr bindonce ng-repeat="row in results.metrics"
6161
class="grafana-search-metric-result"
6262
ng-class="{'selected': $index === selectedIndex }">
6363
<td><span class="label label-info">metric</span></td>
@@ -72,7 +72,7 @@ <h6 ng-hide="search_results.dashboards.length || search_results.metrics.length">
7272
</tr>
7373

7474
<tr bindonce
75-
ng-repeat="row in search_results.dashboards"
75+
ng-repeat="row in results.dashboards"
7676
ng-class="{'selected': $index === selectedIndex }">
7777
<td><a confirm-click="elasticsearch_delete(row._id)" confirmation="Are you sure you want to delete the {{row._id}} dashboard"><i class="icon-remove"></i></a></td>
7878
<td style="width:100%">

0 commit comments

Comments
 (0)