query_ctrl.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. // import './css/query_editor.css';
  4. import TimegrainConverter from './time_grain_converter';
  5. import './editor/editor_component';
  6. import { TemplateSrv } from 'app/features/templating/template_srv';
  7. import { auto } from 'angular';
  8. import { DataFrame } from '@grafana/ui';
  9. export interface ResultFormat {
  10. text: string;
  11. value: string;
  12. }
  13. export class AzureMonitorQueryCtrl extends QueryCtrl {
  14. static templateUrl = 'partials/query.editor.html';
  15. defaultDropdownValue = 'select';
  16. target: {
  17. refId: string;
  18. queryType: string;
  19. subscription: string;
  20. azureMonitor: {
  21. resourceGroup: string;
  22. resourceName: string;
  23. metricDefinition: string;
  24. metricName: string;
  25. dimensionFilter: string;
  26. timeGrain: string;
  27. timeGrainUnit: string;
  28. timeGrains: any[];
  29. dimensions: any[];
  30. dimension: any;
  31. aggregation: string;
  32. aggOptions: string[];
  33. };
  34. azureLogAnalytics: {
  35. query: string;
  36. resultFormat: string;
  37. workspace: string;
  38. };
  39. appInsights: {
  40. metricName: string;
  41. rawQuery: boolean;
  42. rawQueryString: string;
  43. groupBy: string;
  44. timeGrainType: string;
  45. xaxis: string;
  46. yaxis: string;
  47. spliton: string;
  48. aggOptions: string[];
  49. aggregation: string;
  50. groupByOptions: string[];
  51. timeGrainUnit: string;
  52. timeGrain: string;
  53. };
  54. };
  55. defaults = {
  56. queryType: 'Azure Monitor',
  57. azureMonitor: {
  58. resourceGroup: this.defaultDropdownValue,
  59. metricDefinition: this.defaultDropdownValue,
  60. resourceName: this.defaultDropdownValue,
  61. metricName: this.defaultDropdownValue,
  62. dimensionFilter: '*',
  63. timeGrain: 'auto',
  64. },
  65. azureLogAnalytics: {
  66. query: [
  67. '//change this example to create your own time series query',
  68. '<table name> ' +
  69. '//the table to query (e.g. Usage, Heartbeat, Perf)',
  70. '| where $__timeFilter(TimeGenerated) ' +
  71. '//this is a macro used to show the full chart’s time range, choose the datetime column here',
  72. '| summarize count() by <group by column>, bin(TimeGenerated, $__interval) ' +
  73. '//change “group by column” to a column in your table, such as “Computer”. ' +
  74. 'The $__interval macro is used to auto-select the time grain. Can also use 1h, 5m etc.',
  75. '| order by TimeGenerated asc',
  76. ].join('\n'),
  77. resultFormat: 'time_series',
  78. workspace:
  79. this.datasource && this.datasource.azureLogAnalyticsDatasource
  80. ? this.datasource.azureLogAnalyticsDatasource.defaultOrFirstWorkspace
  81. : '',
  82. },
  83. appInsights: {
  84. metricName: this.defaultDropdownValue,
  85. rawQuery: false,
  86. rawQueryString: '',
  87. groupBy: 'none',
  88. timeGrainType: 'auto',
  89. xaxis: 'timestamp',
  90. yaxis: '',
  91. spliton: '',
  92. },
  93. };
  94. resultFormats: ResultFormat[];
  95. workspaces: any[];
  96. showHelp: boolean;
  97. showLastQuery: boolean;
  98. lastQuery: string;
  99. lastQueryError?: string;
  100. subscriptions: Array<{ text: string; value: string }>;
  101. /** @ngInject */
  102. constructor($scope: any, $injector: auto.IInjectorService, private templateSrv: TemplateSrv) {
  103. super($scope, $injector);
  104. _.defaultsDeep(this.target, this.defaults);
  105. this.migrateTimeGrains();
  106. this.migrateToFromTimes();
  107. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  108. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  109. this.resultFormats = [{ text: 'Time series', value: 'time_series' }, { text: 'Table', value: 'table' }];
  110. this.getSubscriptions();
  111. if (this.target.queryType === 'Azure Log Analytics') {
  112. this.getWorkspaces();
  113. }
  114. }
  115. onDataReceived(dataList: DataFrame[]) {
  116. this.lastQueryError = undefined;
  117. this.lastQuery = '';
  118. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  119. if (anySeriesFromQuery && anySeriesFromQuery.meta) {
  120. this.lastQuery = anySeriesFromQuery.meta.query;
  121. }
  122. }
  123. onDataError(err: any) {
  124. this.handleQueryCtrlError(err);
  125. }
  126. handleQueryCtrlError(err: any) {
  127. if (err.query && err.query.refId && err.query.refId !== this.target.refId) {
  128. return;
  129. }
  130. if (err.error && err.error.data && err.error.data.error && err.error.data.error.innererror) {
  131. if (err.error.data.error.innererror.innererror) {
  132. this.lastQueryError = err.error.data.error.innererror.innererror.message;
  133. } else {
  134. this.lastQueryError = err.error.data.error.innererror.message;
  135. }
  136. } else if (err.error && err.error.data && err.error.data.error) {
  137. this.lastQueryError = err.error.data.error.message;
  138. } else if (err.error && err.error.data) {
  139. this.lastQueryError = err.error.data.message;
  140. } else if (err.data && err.data.error) {
  141. this.lastQueryError = err.data.error.message;
  142. } else if (err.data && err.data.message) {
  143. this.lastQueryError = err.data.message;
  144. } else {
  145. this.lastQueryError = err;
  146. }
  147. }
  148. migrateTimeGrains() {
  149. if (this.target.azureMonitor.timeGrainUnit) {
  150. if (this.target.azureMonitor.timeGrain !== 'auto') {
  151. this.target.azureMonitor.timeGrain = TimegrainConverter.createISO8601Duration(
  152. this.target.azureMonitor.timeGrain,
  153. this.target.azureMonitor.timeGrainUnit
  154. );
  155. }
  156. delete this.target.azureMonitor.timeGrainUnit;
  157. this.onMetricNameChange();
  158. }
  159. }
  160. migrateToFromTimes() {
  161. this.target.azureLogAnalytics.query = this.target.azureLogAnalytics.query.replace(/\$__from\s/gi, '$__timeFrom() ');
  162. this.target.azureLogAnalytics.query = this.target.azureLogAnalytics.query.replace(/\$__to\s/gi, '$__timeTo() ');
  163. }
  164. replace(variable: string) {
  165. return this.templateSrv.replace(variable, this.panelCtrl.panel.scopedVars);
  166. }
  167. onQueryTypeChange() {
  168. if (this.target.queryType === 'Azure Log Analytics') {
  169. return this.getWorkspaces();
  170. }
  171. }
  172. getSubscriptions() {
  173. if (!this.datasource.azureMonitorDatasource.isConfigured()) {
  174. return;
  175. }
  176. return this.datasource.azureMonitorDatasource.getSubscriptions().then((subs: any) => {
  177. this.subscriptions = subs;
  178. if (!this.target.subscription && this.target.queryType === 'Azure Monitor') {
  179. this.target.subscription = this.datasource.azureMonitorDatasource.subscriptionId;
  180. } else if (!this.target.subscription && this.target.queryType === 'Azure Log Analytics') {
  181. this.target.subscription = this.datasource.azureLogAnalyticsDatasource.logAnalyticsSubscriptionId;
  182. }
  183. if (!this.target.subscription && this.subscriptions.length > 0) {
  184. this.target.subscription = this.subscriptions[0].value;
  185. }
  186. return this.subscriptions;
  187. });
  188. }
  189. onSubscriptionChange() {
  190. if (this.target.queryType === 'Azure Log Analytics') {
  191. return this.getWorkspaces();
  192. }
  193. if (this.target.queryType === 'Azure Monitor') {
  194. this.target.azureMonitor.resourceGroup = this.defaultDropdownValue;
  195. this.target.azureMonitor.metricDefinition = this.defaultDropdownValue;
  196. this.target.azureMonitor.resourceName = this.defaultDropdownValue;
  197. this.target.azureMonitor.metricName = this.defaultDropdownValue;
  198. this.target.azureMonitor.aggregation = '';
  199. this.target.azureMonitor.timeGrains = [];
  200. this.target.azureMonitor.timeGrain = '';
  201. this.target.azureMonitor.dimensions = [];
  202. this.target.azureMonitor.dimension = '';
  203. }
  204. }
  205. /* Azure Monitor Section */
  206. getResourceGroups(query: any) {
  207. if (this.target.queryType !== 'Azure Monitor' || !this.datasource.azureMonitorDatasource.isConfigured()) {
  208. return;
  209. }
  210. return this.datasource
  211. .getResourceGroups(
  212. this.replace(this.target.subscription || this.datasource.azureMonitorDatasource.subscriptionId)
  213. )
  214. .catch(this.handleQueryCtrlError.bind(this));
  215. }
  216. getMetricDefinitions(query: any) {
  217. if (
  218. this.target.queryType !== 'Azure Monitor' ||
  219. !this.target.azureMonitor.resourceGroup ||
  220. this.target.azureMonitor.resourceGroup === this.defaultDropdownValue
  221. ) {
  222. return;
  223. }
  224. return this.datasource
  225. .getMetricDefinitions(
  226. this.replace(this.target.subscription || this.datasource.azureMonitorDatasource.subscriptionId),
  227. this.replace(this.target.azureMonitor.resourceGroup)
  228. )
  229. .catch(this.handleQueryCtrlError.bind(this));
  230. }
  231. getResourceNames(query: any) {
  232. if (
  233. this.target.queryType !== 'Azure Monitor' ||
  234. !this.target.azureMonitor.resourceGroup ||
  235. this.target.azureMonitor.resourceGroup === this.defaultDropdownValue ||
  236. !this.target.azureMonitor.metricDefinition ||
  237. this.target.azureMonitor.metricDefinition === this.defaultDropdownValue
  238. ) {
  239. return;
  240. }
  241. return this.datasource
  242. .getResourceNames(
  243. this.replace(this.target.subscription || this.datasource.azureMonitorDatasource.subscriptionId),
  244. this.replace(this.target.azureMonitor.resourceGroup),
  245. this.replace(this.target.azureMonitor.metricDefinition)
  246. )
  247. .catch(this.handleQueryCtrlError.bind(this));
  248. }
  249. getMetricNames(query: any) {
  250. if (
  251. this.target.queryType !== 'Azure Monitor' ||
  252. !this.target.azureMonitor.resourceGroup ||
  253. this.target.azureMonitor.resourceGroup === this.defaultDropdownValue ||
  254. !this.target.azureMonitor.metricDefinition ||
  255. this.target.azureMonitor.metricDefinition === this.defaultDropdownValue ||
  256. !this.target.azureMonitor.resourceName ||
  257. this.target.azureMonitor.resourceName === this.defaultDropdownValue
  258. ) {
  259. return;
  260. }
  261. return this.datasource
  262. .getMetricNames(
  263. this.replace(this.target.subscription || this.datasource.azureMonitorDatasource.subscriptionId),
  264. this.replace(this.target.azureMonitor.resourceGroup),
  265. this.replace(this.target.azureMonitor.metricDefinition),
  266. this.replace(this.target.azureMonitor.resourceName)
  267. )
  268. .catch(this.handleQueryCtrlError.bind(this));
  269. }
  270. onResourceGroupChange() {
  271. this.target.azureMonitor.metricDefinition = this.defaultDropdownValue;
  272. this.target.azureMonitor.resourceName = this.defaultDropdownValue;
  273. this.target.azureMonitor.metricName = this.defaultDropdownValue;
  274. this.target.azureMonitor.aggregation = '';
  275. this.target.azureMonitor.timeGrains = [];
  276. this.target.azureMonitor.timeGrain = '';
  277. this.target.azureMonitor.dimensions = [];
  278. this.target.azureMonitor.dimension = '';
  279. }
  280. onMetricDefinitionChange() {
  281. this.target.azureMonitor.resourceName = this.defaultDropdownValue;
  282. this.target.azureMonitor.metricName = this.defaultDropdownValue;
  283. this.target.azureMonitor.aggregation = '';
  284. this.target.azureMonitor.timeGrains = [];
  285. this.target.azureMonitor.timeGrain = '';
  286. this.target.azureMonitor.dimensions = [];
  287. this.target.azureMonitor.dimension = '';
  288. }
  289. onResourceNameChange() {
  290. this.target.azureMonitor.metricName = this.defaultDropdownValue;
  291. this.target.azureMonitor.aggregation = '';
  292. this.target.azureMonitor.timeGrains = [];
  293. this.target.azureMonitor.timeGrain = '';
  294. this.target.azureMonitor.dimensions = [];
  295. this.target.azureMonitor.dimension = '';
  296. }
  297. onMetricNameChange() {
  298. if (!this.target.azureMonitor.metricName || this.target.azureMonitor.metricName === this.defaultDropdownValue) {
  299. return;
  300. }
  301. return this.datasource
  302. .getMetricMetadata(
  303. this.replace(this.target.subscription),
  304. this.replace(this.target.azureMonitor.resourceGroup),
  305. this.replace(this.target.azureMonitor.metricDefinition),
  306. this.replace(this.target.azureMonitor.resourceName),
  307. this.replace(this.target.azureMonitor.metricName)
  308. )
  309. .then((metadata: any) => {
  310. this.target.azureMonitor.aggOptions = metadata.supportedAggTypes || [metadata.primaryAggType];
  311. this.target.azureMonitor.aggregation = metadata.primaryAggType;
  312. this.target.azureMonitor.timeGrains = [{ text: 'auto', value: 'auto' }].concat(metadata.supportedTimeGrains);
  313. this.target.azureMonitor.timeGrain = 'auto';
  314. this.target.azureMonitor.dimensions = metadata.dimensions;
  315. if (metadata.dimensions.length > 0) {
  316. this.target.azureMonitor.dimension = metadata.dimensions[0].value;
  317. }
  318. return this.refresh();
  319. })
  320. .catch(this.handleQueryCtrlError.bind(this));
  321. }
  322. getAutoInterval() {
  323. if (this.target.azureMonitor.timeGrain === 'auto') {
  324. return TimegrainConverter.findClosestTimeGrain(
  325. this.templateSrv.getBuiltInIntervalValue(),
  326. _.map(this.target.azureMonitor.timeGrains, o =>
  327. TimegrainConverter.createKbnUnitFromISO8601Duration(o.value)
  328. ) || ['1m', '5m', '15m', '30m', '1h', '6h', '12h', '1d']
  329. );
  330. }
  331. return '';
  332. }
  333. /* Azure Log Analytics */
  334. getWorkspaces = () => {
  335. return this.datasource.azureLogAnalyticsDatasource
  336. .getWorkspaces(this.target.subscription)
  337. .then((list: any[]) => {
  338. this.workspaces = list;
  339. if (list.length > 0 && !this.target.azureLogAnalytics.workspace) {
  340. this.target.azureLogAnalytics.workspace = list[0].value;
  341. }
  342. })
  343. .catch(this.handleQueryCtrlError.bind(this));
  344. };
  345. getAzureLogAnalyticsSchema = () => {
  346. return this.getWorkspaces()
  347. .then(() => {
  348. return this.datasource.azureLogAnalyticsDatasource.getSchema(this.target.azureLogAnalytics.workspace);
  349. })
  350. .catch(this.handleQueryCtrlError.bind(this));
  351. };
  352. onLogAnalyticsQueryChange = (nextQuery: string) => {
  353. this.target.azureLogAnalytics.query = nextQuery;
  354. };
  355. onLogAnalyticsQueryExecute = () => {
  356. this.panelCtrl.refresh();
  357. };
  358. get templateVariables() {
  359. return this.templateSrv.variables.map(t => '$' + t.name);
  360. }
  361. /* Application Insights Section */
  362. getAppInsightsAutoInterval() {
  363. const interval = this.templateSrv.getBuiltInIntervalValue();
  364. if (interval[interval.length - 1] === 's') {
  365. return '1m';
  366. }
  367. return interval;
  368. }
  369. getAppInsightsMetricNames() {
  370. if (!this.datasource.appInsightsDatasource.isConfigured()) {
  371. return;
  372. }
  373. return this.datasource.getAppInsightsMetricNames().catch(this.handleQueryCtrlError.bind(this));
  374. }
  375. getAppInsightsColumns() {
  376. return this.datasource.getAppInsightsColumns(this.target.refId);
  377. }
  378. onAppInsightsColumnChange() {
  379. return this.refresh();
  380. }
  381. onAppInsightsMetricNameChange() {
  382. if (!this.target.appInsights.metricName || this.target.appInsights.metricName === this.defaultDropdownValue) {
  383. return;
  384. }
  385. return this.datasource
  386. .getAppInsightsMetricMetadata(this.replace(this.target.appInsights.metricName))
  387. .then((aggData: { supportedAggTypes: string[]; supportedGroupBy: string[]; primaryAggType: string }) => {
  388. this.target.appInsights.aggOptions = aggData.supportedAggTypes;
  389. this.target.appInsights.groupByOptions = aggData.supportedGroupBy;
  390. this.target.appInsights.aggregation = aggData.primaryAggType;
  391. return this.refresh();
  392. })
  393. .catch(this.handleQueryCtrlError.bind(this));
  394. }
  395. onAppInsightsQueryChange = (nextQuery: string) => {
  396. this.target.appInsights.rawQueryString = nextQuery;
  397. };
  398. onAppInsightsQueryExecute = () => {
  399. return this.refresh();
  400. };
  401. getAppInsightsQuerySchema = () => {
  402. return this.datasource.appInsightsDatasource.getQuerySchema().catch(this.handleQueryCtrlError.bind(this));
  403. };
  404. getAppInsightsGroupBySegments(query: any) {
  405. return _.map(this.target.appInsights.groupByOptions, option => {
  406. return { text: option, value: option };
  407. });
  408. }
  409. resetAppInsightsGroupBy() {
  410. this.target.appInsights.groupBy = 'none';
  411. this.refresh();
  412. }
  413. updateTimeGrainType() {
  414. if (this.target.appInsights.timeGrainType === 'specific') {
  415. this.target.appInsights.timeGrain = '1';
  416. this.target.appInsights.timeGrainUnit = 'minute';
  417. } else {
  418. this.target.appInsights.timeGrain = '';
  419. }
  420. this.refresh();
  421. }
  422. toggleEditorMode() {
  423. this.target.appInsights.rawQuery = !this.target.appInsights.rawQuery;
  424. }
  425. }