search.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. 'jquery'
  6. ],
  7. function (angular, _, config, $) {
  8. 'use strict';
  9. var module = angular.module('grafana.controllers');
  10. module.controller('SearchCtrl', function($scope, $rootScope, $element, $location, datasourceSrv, $timeout) {
  11. $scope.init = function() {
  12. $scope.giveSearchFocus = 0;
  13. $scope.selectedIndex = -1;
  14. $scope.results = {dashboards: [], tags: [], metrics: []};
  15. $scope.query = { query: 'title:' };
  16. $scope.db = datasourceSrv.getGrafanaDB();
  17. $scope.currentSearchId = 0;
  18. $timeout(function() {
  19. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  20. $scope.query.query = 'title:';
  21. $scope.search();
  22. }, 100);
  23. };
  24. $scope.keyDown = function (evt) {
  25. if (evt.keyCode === 27) {
  26. $scope.appEvent('hide-dash-editor');
  27. }
  28. if (evt.keyCode === 40) {
  29. $scope.moveSelection(1);
  30. }
  31. if (evt.keyCode === 38) {
  32. $scope.moveSelection(-1);
  33. }
  34. if (evt.keyCode === 13) {
  35. if ($scope.tagsOnly) {
  36. var tag = $scope.results.tags[$scope.selectedIndex];
  37. if (tag) {
  38. $scope.filterByTag(tag.term);
  39. }
  40. return;
  41. }
  42. var selectedDash = $scope.results.dashboards[$scope.selectedIndex];
  43. if (selectedDash) {
  44. $location.search({});
  45. $location.path("/dashboard/db/" + selectedDash.id);
  46. setTimeout(function() {
  47. $('body').click(); // hack to force dropdown to close;
  48. });
  49. }
  50. }
  51. };
  52. $scope.moveSelection = function(direction) {
  53. $scope.selectedIndex = Math.max(Math.min($scope.selectedIndex + direction, $scope.resultCount - 1), 0);
  54. };
  55. $scope.goToDashboard = function(id) {
  56. $location.search({});
  57. $location.path("/dashboard/db/" + id);
  58. };
  59. $scope.shareDashboard = function(title, id, $event) {
  60. $event.stopPropagation();
  61. var baseUrl = window.location.href.replace(window.location.hash,'');
  62. $scope.share = {
  63. title: title,
  64. url: baseUrl + '#dashboard/db/' + encodeURIComponent(id)
  65. };
  66. };
  67. $scope.searchDashboards = function(queryString) {
  68. // bookeeping for determining stale search requests
  69. var searchId = $scope.currentSearchId + 1;
  70. $scope.currentSearchId = searchId > $scope.currentSearchId ? searchId : $scope.currentSearchId;
  71. return $scope.db.searchDashboards(queryString)
  72. .then(function(results) {
  73. // since searches are async, it's possible that these results are not for the latest search. throw
  74. // them away if so
  75. if (searchId < $scope.currentSearchId) {
  76. return;
  77. }
  78. $scope.tagsOnly = results.tagsOnly;
  79. $scope.results.dashboards = results.dashboards;
  80. $scope.results.tags = results.tags;
  81. $scope.resultCount = results.tagsOnly ? results.tags.length : results.dashboards.length;
  82. });
  83. };
  84. $scope.filterByTag = function(tag, evt) {
  85. $scope.query.query = "tags:" + tag + " AND title:";
  86. $scope.search();
  87. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  88. if (evt) {
  89. evt.stopPropagation();
  90. evt.preventDefault();
  91. }
  92. };
  93. $scope.showTags = function() {
  94. $scope.tagsOnly = !$scope.tagsOnly;
  95. $scope.query.query = $scope.tagsOnly ? "tags!:" : "";
  96. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  97. $scope.selectedIndex = -1;
  98. $scope.search();
  99. };
  100. $scope.search = function() {
  101. $scope.showImport = false;
  102. $scope.selectedIndex = 0;
  103. $scope.searchDashboards($scope.query.query);
  104. };
  105. $scope.deleteDashboard = function(dash, evt) {
  106. evt.stopPropagation();
  107. $scope.appEvent('delete-dashboard', { id: dash.id, title: dash.title });
  108. $scope.results.dashboards = _.without($scope.results.dashboards, dash);
  109. };
  110. $scope.addMetricToCurrentDashboard = function (metricId) {
  111. $scope.dashboard.rows.push({
  112. title: '',
  113. height: '250px',
  114. editable: true,
  115. panels: [
  116. {
  117. type: 'graphite',
  118. title: 'test',
  119. span: 12,
  120. targets: [{ target: metricId }]
  121. }
  122. ]
  123. });
  124. };
  125. $scope.toggleImport = function () {
  126. $scope.showImport = !$scope.showImport;
  127. };
  128. $scope.newDashboard = function() {
  129. $location.url('/dashboard/file/empty.json');
  130. };
  131. });
  132. module.directive('xngFocus', function() {
  133. return function(scope, element, attrs) {
  134. element.click(function(e) {
  135. e.stopPropagation();
  136. });
  137. scope.$watch(attrs.xngFocus,function (newValue) {
  138. if (!newValue) {
  139. return;
  140. }
  141. setTimeout(function() {
  142. element.focus();
  143. var pos = element.val().length * 2;
  144. element[0].setSelectionRange(pos, pos);
  145. }, 200);
  146. },true);
  147. };
  148. });
  149. module.directive('tagColorFromName', function() {
  150. function djb2(str) {
  151. var hash = 5381;
  152. for (var i = 0; i < str.length; i++) {
  153. hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
  154. }
  155. return hash;
  156. }
  157. return function (scope, element) {
  158. var name = _.isString(scope.tag) ? scope.tag : scope.tag.term;
  159. var hash = djb2(name.toLowerCase());
  160. var colors = [
  161. "#E24D42","#1F78C1","#BA43A9","#705DA0","#466803",
  162. "#508642","#447EBC","#C15C17","#890F02","#757575",
  163. "#0A437C","#6D1F62","#584477","#629E51","#2F4F4F",
  164. "#BF1B00","#806EB7","#8a2eb8", "#699e00","#000000",
  165. "#3F6833","#2F575E","#99440A","#E0752D","#0E4AB4",
  166. "#58140C","#052B51","#511749","#3F2B5B",
  167. ];
  168. var color = colors[Math.abs(hash % colors.length)];
  169. element.css("background-color", color);
  170. };
  171. });
  172. });