query_ctrl.ts 11 KB

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