query_ctrl.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import './add_graphite_func';
  2. import './func_editor';
  3. import _ from 'lodash';
  4. import gfunc from './gfunc';
  5. import GraphiteQuery from './graphite_query';
  6. import { QueryCtrl } from 'app/plugins/sdk';
  7. import appEvents from 'app/core/app_events';
  8. const GRAPHITE_TAG_OPERATORS = ['=', '!=', '=~', '!=~'];
  9. const TAG_PREFIX = 'tag: ';
  10. export class GraphiteQueryCtrl extends QueryCtrl {
  11. static templateUrl = 'partials/query.editor.html';
  12. queryModel: GraphiteQuery;
  13. segments: any[];
  14. addTagSegments: any[];
  15. removeTagValue: string;
  16. supportsTags: boolean;
  17. /** @ngInject **/
  18. constructor($scope, $injector, private uiSegmentSrv, private templateSrv) {
  19. super($scope, $injector);
  20. this.supportsTags = this.datasource.supportsTags;
  21. if (this.target) {
  22. this.target.target = this.target.target || '';
  23. this.queryModel = new GraphiteQuery(this.target, templateSrv);
  24. this.buildSegments();
  25. }
  26. this.removeTagValue = '-- remove tag --';
  27. }
  28. parseTarget() {
  29. this.queryModel.parseTarget();
  30. this.buildSegments();
  31. }
  32. toggleEditorMode() {
  33. this.target.textEditor = !this.target.textEditor;
  34. this.parseTarget();
  35. }
  36. buildSegments() {
  37. this.segments = _.map(this.queryModel.segments, segment => {
  38. return this.uiSegmentSrv.newSegment(segment);
  39. });
  40. let checkOtherSegmentsIndex = this.queryModel.checkOtherSegmentsIndex || 0;
  41. this.checkOtherSegments(checkOtherSegmentsIndex);
  42. if (this.queryModel.seriesByTagUsed) {
  43. this.fixTagSegments();
  44. }
  45. }
  46. addSelectMetricSegment() {
  47. this.queryModel.addSelectMetricSegment();
  48. this.segments.push(this.uiSegmentSrv.newSelectMetric());
  49. }
  50. checkOtherSegments(fromIndex) {
  51. if (
  52. this.queryModel.segments.length === 1 &&
  53. this.queryModel.segments[0].type === 'series-ref'
  54. ) {
  55. return;
  56. }
  57. if (fromIndex === 0) {
  58. this.addSelectMetricSegment();
  59. return;
  60. }
  61. var path = this.queryModel.getSegmentPathUpTo(fromIndex + 1);
  62. if (path === '') {
  63. return Promise.resolve();
  64. }
  65. return this.datasource
  66. .metricFindQuery(path)
  67. .then(segments => {
  68. if (segments.length === 0) {
  69. if (path !== '') {
  70. this.queryModel.segments = this.queryModel.segments.splice(
  71. 0,
  72. fromIndex
  73. );
  74. this.segments = this.segments.splice(0, fromIndex);
  75. this.addSelectMetricSegment();
  76. }
  77. } else if (segments[0].expandable) {
  78. if (this.segments.length === fromIndex) {
  79. this.addSelectMetricSegment();
  80. } else {
  81. return this.checkOtherSegments(fromIndex + 1);
  82. }
  83. }
  84. })
  85. .catch(err => {
  86. appEvents.emit('alert-error', ['Error', err]);
  87. });
  88. }
  89. setSegmentFocus(segmentIndex) {
  90. _.each(this.segments, (segment, index) => {
  91. segment.focus = segmentIndex === index;
  92. });
  93. }
  94. getAltSegments(index) {
  95. var query =
  96. index === 0 ? '*' : this.queryModel.getSegmentPathUpTo(index) + '.*';
  97. var options = {
  98. range: this.panelCtrl.range,
  99. requestId: 'get-alt-segments',
  100. };
  101. return this.datasource
  102. .metricFindQuery(query, options)
  103. .then(segments => {
  104. var altSegments = _.map(segments, segment => {
  105. return this.uiSegmentSrv.newSegment({
  106. value: segment.text,
  107. expandable: segment.expandable,
  108. });
  109. });
  110. if (altSegments.length === 0) {
  111. return altSegments;
  112. }
  113. // add query references
  114. if (index === 0) {
  115. _.eachRight(this.panelCtrl.panel.targets, target => {
  116. if (target.refId === this.queryModel.target.refId) {
  117. return;
  118. }
  119. altSegments.unshift(
  120. this.uiSegmentSrv.newSegment({
  121. type: 'series-ref',
  122. value: '#' + target.refId,
  123. expandable: false,
  124. })
  125. );
  126. });
  127. }
  128. // add template variables
  129. _.eachRight(this.templateSrv.variables, variable => {
  130. altSegments.unshift(
  131. this.uiSegmentSrv.newSegment({
  132. type: 'template',
  133. value: '$' + variable.name,
  134. expandable: true,
  135. })
  136. );
  137. });
  138. // add wildcard option
  139. altSegments.unshift(this.uiSegmentSrv.newSegment('*'));
  140. if (this.supportsTags && index === 0) {
  141. this.removeTaggedEntry(altSegments);
  142. return this.addAltTagSegments(index, altSegments);
  143. } else {
  144. return altSegments;
  145. }
  146. })
  147. .catch(err => {
  148. return [];
  149. });
  150. }
  151. addAltTagSegments(index, altSegments) {
  152. return this.getTagsAsSegments().then(tagSegments => {
  153. tagSegments = _.map(tagSegments, segment => {
  154. segment.value = TAG_PREFIX + segment.value;
  155. return segment;
  156. });
  157. return altSegments.concat(...tagSegments);
  158. });
  159. }
  160. removeTaggedEntry(altSegments) {
  161. altSegments = _.remove(altSegments, s => s.value === '_tagged');
  162. }
  163. segmentValueChanged(segment, segmentIndex) {
  164. this.error = null;
  165. this.queryModel.updateSegmentValue(segment, segmentIndex);
  166. if (
  167. this.queryModel.functions.length > 0 &&
  168. this.queryModel.functions[0].def.fake
  169. ) {
  170. this.queryModel.functions = [];
  171. }
  172. if (segment.type === 'tag') {
  173. let tag = removeTagPrefix(segment.value);
  174. this.addSeriesByTagFunc(tag);
  175. return;
  176. }
  177. if (segment.expandable) {
  178. return this.checkOtherSegments(segmentIndex + 1).then(() => {
  179. this.setSegmentFocus(segmentIndex + 1);
  180. this.targetChanged();
  181. });
  182. } else {
  183. this.spliceSegments(segmentIndex + 1);
  184. }
  185. this.setSegmentFocus(segmentIndex + 1);
  186. this.targetChanged();
  187. }
  188. spliceSegments(index) {
  189. this.segments = this.segments.splice(0, index);
  190. this.queryModel.segments = this.queryModel.segments.splice(0, index);
  191. }
  192. emptySegments() {
  193. this.queryModel.segments = [];
  194. this.segments = [];
  195. }
  196. targetTextChanged() {
  197. this.updateModelTarget();
  198. this.refresh();
  199. }
  200. updateModelTarget() {
  201. this.queryModel.updateModelTarget(this.panelCtrl.panel.targets);
  202. }
  203. targetChanged() {
  204. if (this.queryModel.error) {
  205. return;
  206. }
  207. var oldTarget = this.queryModel.target.target;
  208. this.updateModelTarget();
  209. if (this.queryModel.target !== oldTarget) {
  210. this.panelCtrl.refresh();
  211. }
  212. }
  213. addFunction(funcDef) {
  214. var newFunc = gfunc.createFuncInstance(funcDef, {
  215. withDefaultParams: true,
  216. });
  217. newFunc.added = true;
  218. this.queryModel.addFunction(newFunc);
  219. this.smartlyHandleNewAliasByNode(newFunc);
  220. if (this.segments.length === 1 && this.segments[0].fake) {
  221. this.emptySegments();
  222. }
  223. if (!newFunc.params.length && newFunc.added) {
  224. this.targetChanged();
  225. }
  226. if (newFunc.def.name === 'seriesByTag') {
  227. this.parseTarget();
  228. }
  229. }
  230. removeFunction(func) {
  231. this.queryModel.removeFunction(func);
  232. this.targetChanged();
  233. }
  234. addSeriesByTagFunc(tag) {
  235. let funcDef = gfunc.getFuncDef('seriesByTag');
  236. let newFunc = gfunc.createFuncInstance(funcDef, {
  237. withDefaultParams: false,
  238. });
  239. let tagParam = `${tag}=select tag value`;
  240. newFunc.params = [tagParam];
  241. this.queryModel.addFunction(newFunc);
  242. newFunc.added = true;
  243. this.emptySegments();
  244. this.targetChanged();
  245. this.parseTarget();
  246. }
  247. smartlyHandleNewAliasByNode(func) {
  248. if (func.def.name !== 'aliasByNode') {
  249. return;
  250. }
  251. for (var i = 0; i < this.segments.length; i++) {
  252. if (this.segments[i].value.indexOf('*') >= 0) {
  253. func.params[0] = i;
  254. func.added = false;
  255. this.targetChanged();
  256. return;
  257. }
  258. }
  259. }
  260. getAllTags() {
  261. return this.datasource.getTags().then(values => {
  262. let altTags = _.map(values, 'text');
  263. altTags.splice(0, 0, this.removeTagValue);
  264. return mapToDropdownOptions(altTags);
  265. });
  266. }
  267. getTags(index, tagPrefix) {
  268. let tagExpressions = this.queryModel.renderTagExpressions(index);
  269. return this.datasource
  270. .getTagsAutoComplete(tagExpressions, tagPrefix)
  271. .then(values => {
  272. let altTags = _.map(values, 'text');
  273. altTags.splice(0, 0, this.removeTagValue);
  274. return mapToDropdownOptions(altTags);
  275. });
  276. }
  277. getTagsAsSegments() {
  278. let tagExpressions = this.queryModel.renderTagExpressions();
  279. return this.datasource.getTagsAutoComplete(tagExpressions).then(values => {
  280. return _.map(values, val => {
  281. return this.uiSegmentSrv.newSegment({
  282. value: val.text,
  283. type: 'tag',
  284. expandable: false,
  285. });
  286. });
  287. });
  288. }
  289. getTagOperators() {
  290. return mapToDropdownOptions(GRAPHITE_TAG_OPERATORS);
  291. }
  292. getAllTagValues(tag) {
  293. let tagKey = tag.key;
  294. return this.datasource.getTagValues(tagKey).then(values => {
  295. let altValues = _.map(values, 'text');
  296. return mapToDropdownOptions(altValues);
  297. });
  298. }
  299. getTagValues(tag, index, valuePrefix) {
  300. let tagExpressions = this.queryModel.renderTagExpressions(index);
  301. let tagKey = tag.key;
  302. return this.datasource
  303. .getTagValuesAutoComplete(tagExpressions, tagKey, valuePrefix)
  304. .then(values => {
  305. let altValues = _.map(values, 'text');
  306. return mapToDropdownOptions(altValues);
  307. });
  308. }
  309. tagChanged(tag, tagIndex) {
  310. this.queryModel.updateTag(tag, tagIndex);
  311. this.targetChanged();
  312. }
  313. addNewTag(segment) {
  314. let newTagKey = segment.value;
  315. let newTag = { key: newTagKey, operator: '=', value: 'select tag value' };
  316. this.queryModel.addTag(newTag);
  317. this.targetChanged();
  318. this.fixTagSegments();
  319. }
  320. removeTag(index) {
  321. this.queryModel.removeTag(index);
  322. this.targetChanged();
  323. }
  324. fixTagSegments() {
  325. // Adding tag with the same name as just removed works incorrectly if single segment is used (instead of array)
  326. this.addTagSegments = [this.uiSegmentSrv.newPlusButton()];
  327. }
  328. showDelimiter(index) {
  329. return index !== this.queryModel.tags.length - 1;
  330. }
  331. }
  332. function mapToDropdownOptions(results) {
  333. return _.map(results, value => {
  334. return { text: value, value: value };
  335. });
  336. }
  337. function removeTagPrefix(value: string): string {
  338. return value.replace(TAG_PREFIX, '');
  339. }