query_ctrl.ts 11 KB

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