query_ctrl.ts 11 KB

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