search.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.emitAppEvent('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.path("/dashboard/db/" + id);
  57. };
  58. $scope.shareDashboard = function(title, id, $event) {
  59. $event.stopPropagation();
  60. var baseUrl = window.location.href.replace(window.location.hash,'');
  61. $scope.share = {
  62. title: title,
  63. url: baseUrl + '#dashboard/db/' + encodeURIComponent(id)
  64. };
  65. };
  66. $scope.searchDashboards = function(queryString) {
  67. // bookeeping for determining stale search requests
  68. var searchId = $scope.currentSearchId + 1;
  69. $scope.currentSearchId = searchId > $scope.currentSearchId ? searchId : $scope.currentSearchId;
  70. return $scope.db.searchDashboards(queryString)
  71. .then(function(results) {
  72. // since searches are async, it's possible that these results are not for the latest search. throw
  73. // them away if so
  74. if (searchId < $scope.currentSearchId) {
  75. return;
  76. }
  77. $scope.tagsOnly = results.tagsOnly;
  78. $scope.results.dashboards = results.dashboards;
  79. $scope.results.tags = results.tags;
  80. $scope.resultCount = results.tagsOnly ? results.tags.length : results.dashboards.length;
  81. });
  82. };
  83. $scope.filterByTag = function(tag, evt) {
  84. $scope.query.query = "tags:" + tag + " AND title:";
  85. $scope.search();
  86. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  87. if (evt) {
  88. evt.stopPropagation();
  89. evt.preventDefault();
  90. }
  91. };
  92. $scope.showTags = function() {
  93. $scope.tagsOnly = !$scope.tagsOnly;
  94. $scope.query.query = $scope.tagsOnly ? "tags!:" : "";
  95. $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
  96. $scope.selectedIndex = -1;
  97. $scope.search();
  98. };
  99. $scope.search = function() {
  100. $scope.showImport = false;
  101. $scope.selectedIndex = 0;
  102. $scope.searchDashboards($scope.query.query);
  103. };
  104. $scope.deleteDashboard = function(dash, evt) {
  105. evt.stopPropagation();
  106. $scope.emitAppEvent('delete-dashboard', { id: dash.id });
  107. $scope.results.dashboards = _.without($scope.results.dashboards, dash);
  108. };
  109. $scope.addMetricToCurrentDashboard = function (metricId) {
  110. $scope.dashboard.rows.push({
  111. title: '',
  112. height: '250px',
  113. editable: true,
  114. panels: [
  115. {
  116. type: 'graphite',
  117. title: 'test',
  118. span: 12,
  119. targets: [{ target: metricId }]
  120. }
  121. ]
  122. });
  123. };
  124. $scope.toggleImport = function () {
  125. $scope.showImport = !$scope.showImport;
  126. };
  127. $scope.newDashboard = function() {
  128. $location.url('/dashboard/file/empty.json');
  129. };
  130. });
  131. module.directive('xngFocus', function() {
  132. return function(scope, element, attrs) {
  133. element.click(function(e) {
  134. e.stopPropagation();
  135. });
  136. scope.$watch(attrs.xngFocus,function (newValue) {
  137. if (!newValue) {
  138. return;
  139. }
  140. setTimeout(function() {
  141. element.focus();
  142. var pos = element.val().length * 2;
  143. element[0].setSelectionRange(pos, pos);
  144. }, 200);
  145. },true);
  146. };
  147. });
  148. module.directive('tagColorFromName', function() {
  149. function djb2(str) {
  150. var hash = 5381;
  151. for (var i = 0; i < str.length; i++) {
  152. hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
  153. }
  154. return hash;
  155. }
  156. return function (scope, element) {
  157. var name = _.isString(scope.tag) ? scope.tag : scope.tag.term;
  158. var hash = djb2(name.toLowerCase());
  159. var colors = [
  160. "#E24D42","#1F78C1","#BA43A9","#705DA0","#466803",
  161. "#508642","#447EBC","#C15C17","#890F02","#757575",
  162. "#0A437C","#6D1F62","#584477","#629E51","#2F4F4F",
  163. "#BF1B00","#806EB7","#8a2eb8", "#699e00","#000000",
  164. "#3F6833","#2F575E","#99440A","#E0752D","#0E4AB4",
  165. "#58140C","#052B51","#511749","#3F2B5B",
  166. ];
  167. var color = colors[Math.abs(hash % colors.length)];
  168. element.css("background-color", color);
  169. };
  170. });
  171. });