annotations_query_ctrl.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { TemplateSrv } from 'app/features/templating/template_srv';
  2. export class AzureMonitorAnnotationsQueryCtrl {
  3. static templateUrl = 'partials/annotations.editor.html';
  4. datasource: any;
  5. annotation: any;
  6. workspaces: any[];
  7. subscriptions: Array<{ text: string; value: string }>;
  8. defaultQuery =
  9. '<your table>\n| where $__timeFilter() \n| project TimeGenerated, Text=YourTitleColumn, Tags="tag1,tag2"';
  10. /** @ngInject */
  11. constructor(private templateSrv: TemplateSrv) {
  12. this.annotation.queryType = this.annotation.queryType || 'Azure Log Analytics';
  13. this.annotation.rawQuery = this.annotation.rawQuery || this.defaultQuery;
  14. this.initDropdowns();
  15. }
  16. async initDropdowns() {
  17. await this.getSubscriptions();
  18. await this.getWorkspaces();
  19. }
  20. async getSubscriptions() {
  21. if (!this.datasource.azureMonitorDatasource.isConfigured()) {
  22. return;
  23. }
  24. return this.datasource.azureMonitorDatasource.getSubscriptions().then((subs: any[]) => {
  25. this.subscriptions = subs;
  26. if (!this.annotation.subscription && this.annotation.queryType === 'Azure Log Analytics') {
  27. this.annotation.subscription = this.datasource.azureLogAnalyticsDatasource.subscriptionId;
  28. }
  29. if (!this.annotation.subscription && this.subscriptions.length > 0) {
  30. this.annotation.subscription = this.subscriptions[0].value;
  31. }
  32. });
  33. }
  34. async getWorkspaces(bustCache?: boolean) {
  35. if (!bustCache && this.workspaces && this.workspaces.length > 0) {
  36. return this.workspaces;
  37. }
  38. return this.datasource
  39. .getAzureLogAnalyticsWorkspaces(this.annotation.subscription)
  40. .then((list: any[]) => {
  41. this.workspaces = list;
  42. if (list.length > 0 && !this.annotation.workspace) {
  43. this.annotation.workspace = list[0].value;
  44. }
  45. return this.workspaces;
  46. })
  47. .catch(() => {});
  48. }
  49. getAzureLogAnalyticsSchema = () => {
  50. return this.getWorkspaces()
  51. .then(() => {
  52. return this.datasource.azureLogAnalyticsDatasource.getSchema(this.annotation.workspace);
  53. })
  54. .catch(() => {});
  55. };
  56. onSubscriptionChange = () => {
  57. this.getWorkspaces(true);
  58. };
  59. onLogAnalyticsQueryChange = (nextQuery: string) => {
  60. this.annotation.rawQuery = nextQuery;
  61. };
  62. get templateVariables() {
  63. return this.templateSrv.variables.map((t: any) => '$' + t.name);
  64. }
  65. }