annotations_query_ctrl.ts 2.2 KB

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