query_ctrl.ts 11 KB

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