common.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { map } from 'lodash';
  2. import { rangeUtil } from '@grafana/data';
  3. import { VariableWithMultiSupport } from 'app/features/variables/types';
  4. import TimegrainConverter from '../time_grain_converter';
  5. import { AzureMonitorOption } from '../types';
  6. export const hasOption = (options: AzureMonitorOption[], value: string): boolean =>
  7. options.some((v) => (v.options ? hasOption(v.options, value) : v.value === value));
  8. export const findOptions = (options: AzureMonitorOption[], values: string[] = []) => {
  9. if (values.length === 0) {
  10. return [];
  11. }
  12. const set = values.reduce((accum, item) => {
  13. accum.add(item);
  14. return accum;
  15. }, new Set());
  16. return options.filter((option) => set.has(option.value));
  17. };
  18. export const toOption = (v: { text: string; value: string }) => ({ value: v.value, label: v.text });
  19. export function convertTimeGrainsToMs<T extends { value: string }>(timeGrains: T[]) {
  20. const allowedTimeGrainsMs: number[] = [];
  21. timeGrains.forEach((tg: any) => {
  22. if (tg.value !== 'auto') {
  23. allowedTimeGrainsMs.push(rangeUtil.intervalToMs(TimegrainConverter.createKbnUnitFromISO8601Duration(tg.value)));
  24. }
  25. });
  26. return allowedTimeGrainsMs;
  27. }
  28. // Route definitions shared with the backend.
  29. // Check: /pkg/tsdb/azuremonitor/azuremonitor-resource-handler.go <registerRoutes>
  30. export const routeNames = {
  31. azureMonitor: 'azuremonitor',
  32. logAnalytics: 'loganalytics',
  33. appInsights: 'appinsights',
  34. resourceGraph: 'resourcegraph',
  35. };
  36. export function interpolateVariable(value: any, variable: VariableWithMultiSupport) {
  37. if (typeof value === 'string') {
  38. // When enabling multiple responses, quote the value to mimic the array result below
  39. // even if only one response is selected. This does not apply if only the "include all"
  40. // option is enabled but with a custom value.
  41. if (variable.multi || (variable.includeAll && !variable.allValue)) {
  42. return "'" + value + "'";
  43. } else {
  44. return value;
  45. }
  46. }
  47. if (typeof value === 'number') {
  48. return value;
  49. }
  50. const quotedValues = map(value, (val) => {
  51. if (typeof value === 'number') {
  52. return value;
  53. }
  54. return "'" + val + "'";
  55. });
  56. return quotedValues.join(',');
  57. }