query_ctrl.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. import appEvents from 'app/core/app_events';
  4. export interface QueryMeta {
  5. rawQuery: string;
  6. rawQueryString: string;
  7. metricLabels: { [key: string]: string[] };
  8. resourceLabels: { [key: string]: string[] };
  9. }
  10. export class StackdriverQueryCtrl extends QueryCtrl {
  11. static templateUrl = 'partials/query.editor.html';
  12. target: {
  13. project: {
  14. id: string;
  15. name: string;
  16. };
  17. metricType: string;
  18. refId: string;
  19. aggregation: {
  20. crossSeriesReducer: string;
  21. alignmentPeriod: string;
  22. perSeriesAligner: string;
  23. groupBys: string[];
  24. };
  25. filters: string[];
  26. };
  27. defaultDropdownValue = 'select metric';
  28. defaultFilterValue = 'select value';
  29. defaultRemoveGroupByValue = '-- remove group by --';
  30. defaultRemoveFilterValue = '-- remove filter --';
  31. defaults = {
  32. project: {
  33. id: 'default',
  34. name: 'loading project...',
  35. },
  36. metricType: this.defaultDropdownValue,
  37. aggregation: {
  38. crossSeriesReducer: 'REDUCE_MEAN',
  39. alignmentPeriod: '',
  40. perSeriesAligner: '',
  41. groupBys: [],
  42. },
  43. filters: [],
  44. };
  45. groupBySegments: any[];
  46. filterSegments: any[];
  47. removeSegment: any;
  48. aggOptions = [
  49. { text: 'none', value: 'REDUCE_NONE' },
  50. { text: 'mean', value: 'REDUCE_MEAN' },
  51. { text: 'min', value: 'REDUCE_MIN' },
  52. { text: 'max', value: 'REDUCE_MAX' },
  53. { text: 'sum', value: 'REDUCE_SUM' },
  54. { text: 'std. dev.', value: 'REDUCE_STDDEV' },
  55. { text: 'count', value: 'REDUCE_COUNT' },
  56. { text: '99th percentile', value: 'REDUCE_PERCENTILE_99' },
  57. { text: '95th percentile', value: 'REDUCE_PERCENTILE_95' },
  58. { text: '50th percentile', value: 'REDUCE_PERCENTILE_50' },
  59. { text: '5th percentile', value: 'REDUCE_PERCENTILE_05' },
  60. ];
  61. showHelp: boolean;
  62. showLastQuery: boolean;
  63. lastQueryMeta: QueryMeta;
  64. lastQueryError?: string;
  65. metricLabels: { [key: string]: string[] };
  66. resourceLabels: { [key: string]: string[] };
  67. /** @ngInject */
  68. constructor($scope, $injector, private uiSegmentSrv, private timeSrv) {
  69. super($scope, $injector);
  70. _.defaultsDeep(this.target, this.defaults);
  71. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  72. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  73. this.getCurrentProject()
  74. .then(this.getMetricTypes.bind(this))
  75. .then(this.getLabels.bind(this));
  76. this.initSegments();
  77. }
  78. initSegments() {
  79. this.groupBySegments = this.target.aggregation.groupBys.map(groupBy => {
  80. return this.uiSegmentSrv.getSegmentForValue(groupBy);
  81. });
  82. this.removeSegment = this.uiSegmentSrv.newSegment({ fake: true, value: '-- remove group by --' });
  83. this.ensurePlusButton(this.groupBySegments);
  84. this.filterSegments = [];
  85. this.target.filters.forEach((f, index) => {
  86. switch (index % 4) {
  87. case 0:
  88. this.filterSegments.push(this.uiSegmentSrv.newKey(f));
  89. break;
  90. case 1:
  91. this.filterSegments.push(this.uiSegmentSrv.newOperator(f));
  92. break;
  93. case 2:
  94. this.filterSegments.push(this.uiSegmentSrv.newKeyValue(f));
  95. break;
  96. case 3:
  97. this.filterSegments.push(this.uiSegmentSrv.newCondition(f));
  98. break;
  99. }
  100. });
  101. this.ensurePlusButton(this.filterSegments);
  102. }
  103. async getCurrentProject() {
  104. try {
  105. const projects = await this.datasource.getProjects();
  106. if (projects && projects.length > 0) {
  107. this.target.project = projects[0];
  108. } else {
  109. throw new Error('No projects found');
  110. }
  111. } catch (error) {
  112. let message = 'Projects cannot be fetched: ';
  113. message += error.statusText ? error.statusText + ': ' : '';
  114. if (error && error.data && error.data.error && error.data.error.message) {
  115. if (error.data.error.code === 403) {
  116. message += `
  117. A list of projects could not be fetched from the Google Cloud Resource Manager API.
  118. You might need to enable it first:
  119. https://console.developers.google.com/apis/library/cloudresourcemanager.googleapis.com`;
  120. } else {
  121. message += error.data.error.code + '. ' + error.data.error.message;
  122. }
  123. } else {
  124. message += 'Cannot connect to Stackdriver API';
  125. }
  126. appEvents.emit('ds-request-error', message);
  127. }
  128. }
  129. async getMetricTypes() {
  130. //projects/raintank-production/metricDescriptors/agent.googleapis.com/agent/api_request_count
  131. if (this.target.project.id !== 'default') {
  132. const metricTypes = await this.datasource.getMetricTypes(this.target.project.id);
  133. if (this.target.metricType === this.defaultDropdownValue && metricTypes.length > 0) {
  134. this.$scope.$apply(() => (this.target.metricType = metricTypes[0].name));
  135. }
  136. return metricTypes.map(mt => ({ value: mt.id, text: mt.id }));
  137. } else {
  138. return [];
  139. }
  140. }
  141. async getLabels() {
  142. const data = await this.datasource.getTimeSeries({
  143. targets: [
  144. {
  145. refId: this.target.refId,
  146. datasourceId: this.datasource.id,
  147. metricType: this.target.metricType,
  148. aggregation: {
  149. crossSeriesReducer: 'REDUCE_NONE',
  150. },
  151. view: 'HEADERS',
  152. },
  153. ],
  154. range: this.timeSrv.timeRange(),
  155. });
  156. this.metricLabels = data.results[this.target.refId].meta.metricLabels;
  157. this.resourceLabels = data.results[this.target.refId].meta.resourceLabels;
  158. }
  159. async onMetricTypeChange() {
  160. this.refresh();
  161. this.getLabels();
  162. }
  163. getGroupBys(segment, index, removeText?: string, removeUsed = true) {
  164. const metricLabels = Object.keys(this.metricLabels)
  165. .filter(ml => {
  166. if (!removeUsed) {
  167. return true;
  168. }
  169. return this.target.aggregation.groupBys.indexOf('metric.label.' + ml) === -1;
  170. })
  171. .map(l => {
  172. return this.uiSegmentSrv.newSegment({
  173. value: `metric.label.${l}`,
  174. expandable: false,
  175. });
  176. });
  177. const resourceLabels = Object.keys(this.resourceLabels)
  178. .filter(ml => {
  179. if (!removeUsed) {
  180. return true;
  181. }
  182. return this.target.aggregation.groupBys.indexOf('resource.label.' + ml) === -1;
  183. })
  184. .map(l => {
  185. return this.uiSegmentSrv.newSegment({
  186. value: `resource.label.${l}`,
  187. expandable: false,
  188. });
  189. });
  190. this.removeSegment.value = removeText || this.defaultRemoveGroupByValue;
  191. return Promise.resolve([...metricLabels, ...resourceLabels, this.removeSegment]);
  192. }
  193. groupByChanged(segment, index) {
  194. if (segment.value === this.removeSegment.value) {
  195. this.groupBySegments.splice(index, 1);
  196. } else {
  197. segment.type = 'value';
  198. }
  199. const reducer = (memo, seg) => {
  200. if (!seg.fake) {
  201. memo.push(seg.value);
  202. }
  203. return memo;
  204. };
  205. this.target.aggregation.groupBys = this.groupBySegments.reduce(reducer, []);
  206. this.ensurePlusButton(this.groupBySegments);
  207. this.refresh();
  208. }
  209. async getFilters(segment, index) {
  210. if (segment.type === 'condition') {
  211. return [this.uiSegmentSrv.newSegment('AND')];
  212. }
  213. if (segment.type === 'operator') {
  214. return this.uiSegmentSrv.newOperators(['=', '!=', '=~', '!=~']);
  215. }
  216. if (segment.type === 'key' || segment.type === 'plus-button') {
  217. return this.getGroupBys(null, null, this.defaultRemoveFilterValue, false);
  218. }
  219. if (segment.type === 'value') {
  220. const filterKey = this.filterSegments[index - 2].value;
  221. const shortKey = filterKey.substring(filterKey.indexOf('.label.') + 7);
  222. if (filterKey.startsWith('metric.label.') && this.metricLabels.hasOwnProperty(shortKey)) {
  223. return this.getValuesForFilterKey(this.metricLabels[shortKey]);
  224. }
  225. if (filterKey.startsWith('resource.label.') && this.resourceLabels.hasOwnProperty(shortKey)) {
  226. return this.getValuesForFilterKey(this.resourceLabels[shortKey]);
  227. }
  228. }
  229. return [];
  230. }
  231. getValuesForFilterKey(labels: any[]) {
  232. const filterValues = labels.map(l => {
  233. return this.uiSegmentSrv.newSegment({
  234. value: `${l}`,
  235. expandable: false,
  236. });
  237. });
  238. return filterValues;
  239. }
  240. filterSegmentUpdated(segment, index) {
  241. if (segment.type === 'plus-button') {
  242. this.addNewFilterSegments(segment, index);
  243. } else if (segment.type === 'key' && segment.value === this.defaultRemoveFilterValue) {
  244. this.removeFilterSegment(index);
  245. this.ensurePlusButton(this.filterSegments);
  246. } else if (segment.type === 'value' && segment.value !== this.defaultFilterValue) {
  247. this.ensurePlusButton(this.filterSegments);
  248. }
  249. this.target.filters = this.filterSegments.filter(s => s.type !== 'plus-button').map(seg => seg.value);
  250. this.refresh();
  251. }
  252. addNewFilterSegments(segment, index) {
  253. if (index > 2) {
  254. this.filterSegments.splice(index, 0, this.uiSegmentSrv.newCondition('AND'));
  255. }
  256. segment.type = 'key';
  257. this.filterSegments.push(this.uiSegmentSrv.newOperator('='));
  258. this.filterSegments.push(this.uiSegmentSrv.newFake(this.defaultFilterValue, 'value', 'query-segment-value'));
  259. }
  260. removeFilterSegment(index) {
  261. this.filterSegments.splice(index, 3);
  262. // remove trailing condition
  263. if (index > 2 && this.filterSegments[index - 1].type === 'condition') {
  264. this.filterSegments.splice(index - 1, 1);
  265. }
  266. // remove condition if it is first segment
  267. if (index === 0 && this.filterSegments[0].type === 'condition') {
  268. this.filterSegments.splice(0, 1);
  269. }
  270. }
  271. ensurePlusButton(segments) {
  272. const count = segments.length;
  273. const lastSegment = segments[Math.max(count - 1, 0)];
  274. if (!lastSegment || lastSegment.type !== 'plus-button') {
  275. segments.push(this.uiSegmentSrv.newPlusButton());
  276. }
  277. }
  278. onDataReceived(dataList) {
  279. this.lastQueryError = null;
  280. this.lastQueryMeta = null;
  281. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  282. if (anySeriesFromQuery) {
  283. this.lastQueryMeta = anySeriesFromQuery.meta;
  284. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  285. }
  286. }
  287. onDataError(err) {
  288. if (err.data && err.data.results) {
  289. const queryRes = err.data.results[this.target.refId];
  290. if (queryRes) {
  291. this.lastQueryMeta = queryRes.meta;
  292. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  293. let jsonBody;
  294. try {
  295. jsonBody = JSON.parse(queryRes.error);
  296. } catch {
  297. this.lastQueryError = queryRes.error;
  298. }
  299. this.lastQueryError = jsonBody.error.message;
  300. }
  301. }
  302. }
  303. }