query_ctrl.ts 11 KB

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