@grafana/data#DataTransformerConfig TypeScript Examples

The following examples show how to use @grafana/data#DataTransformerConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: getAlertingValidationMessage.ts    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
getAlertingValidationMessage = async (
  transformations: DataTransformerConfig[],
  targets: DataQuery[],
  datasourceSrv: DataSourceSrv,
  datasourceName: string
): Promise<string> => {
  if (targets.length === 0) {
    return 'Could not find any metric queries';
  }

  if (transformations && transformations.length) {
    return 'Transformations are not supported in alert queries';
  }

  let alertingNotSupported = 0;
  let templateVariablesNotSupported = 0;

  for (const target of targets) {
    const dsName = target.datasource || datasourceName;
    const ds = await datasourceSrv.get(dsName);
    if (!ds.meta.alerting) {
      alertingNotSupported++;
    } else if (ds.targetContainsTemplate && ds.targetContainsTemplate(target)) {
      templateVariablesNotSupported++;
    }
  }

  if (alertingNotSupported === targets.length) {
    return 'The datasource does not support alerting queries';
  }

  if (templateVariablesNotSupported === targets.length) {
    return 'Template variables are not supported in alert queries';
  }

  return '';
}
Example #2
Source File: PanelEditorTabs.tsx    From grafana-chinese with Apache License 2.0 6 votes vote down vote up
PanelEditorTabs: React.FC<PanelEditorTabsProps> = ({ panel, dashboard, tabs, data, onChangeTab }) => {
  const styles = getPanelEditorTabsStyles();
  const activeTab = tabs.find(item => item.active);

  if (tabs.length === 0) {
    return null;
  }

  const onTransformersChange = (transformers: DataTransformerConfig[]) => {
    panel.setTransformations(transformers);
  };

  return (
    <div className={styles.wrapper}>
      <TabsBar className={styles.tabBar}>
        {tabs.map(tab => {
          return <Tab key={tab.id} label={tab.text} active={tab.active} onChangeTab={() => onChangeTab(tab)} />;
        })}
      </TabsBar>
      <TabContent className={styles.tabContent}>
        {activeTab.id === PanelEditorTabId.Queries && <QueriesTab panel={panel} dashboard={dashboard} />}
        {activeTab.id === PanelEditorTabId.Alert && <AlertTab panel={panel} dashboard={dashboard} />}
        {activeTab.id === PanelEditorTabId.Visualization && <VisualizationTab panel={panel} />}
        {activeTab.id === PanelEditorTabId.Transform && data.state !== LoadingState.NotStarted && (
          <TransformationsEditor
            transformations={panel.transformations || []}
            onChange={onTransformersChange}
            dataFrames={data.series}
          />
        )}
      </TabContent>
    </div>
  );
}
Example #3
Source File: TransformationsEditor.tsx    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
onTransformationChange = (idx: number, config: DataTransformerConfig) => {
    const { transformations, onChange } = this.props;
    transformations[idx] = config;
    onChange(transformations);
    this.setState({ updateCounter: this.state.updateCounter + 1 });
  };
Example #4
Source File: PanelModel.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
setTransformations(transformations: DataTransformerConfig[]) {
    this.transformations = transformations;
    this.getQueryRunner().setTransformations(transformations);
  }
Example #5
Source File: PanelModel.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
transformations?: DataTransformerConfig[];
Example #6
Source File: PanelQueryRunner.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
setTransformations(transformations?: DataTransformerConfig[]) {
    this.transformations = transformations;
  }
Example #7
Source File: PanelQueryRunner.ts    From grafana-chinese with Apache License 2.0 5 votes vote down vote up
private transformations?: DataTransformerConfig[];
Example #8
Source File: getAlertingValidationMessage.test.ts    From grafana-chinese with Apache License 2.0 4 votes vote down vote up
describe('getAlertingValidationMessage', () => {
  describe('when called with some targets containing template variables', () => {
    it('then it should return false', async () => {
      let call = 0;
      const datasource: DataSourceApi = ({
        meta: ({ alerting: true } as any) as PluginMeta,
        targetContainsTemplate: () => {
          if (call === 0) {
            call++;
            return true;
          }
          return false;
        },
        name: 'some name',
      } as any) as DataSourceApi;
      const getMock = jest.fn().mockResolvedValue(datasource);
      const datasourceSrv: DataSourceSrv = {
        get: getMock,
      };
      const targets: ElasticsearchQuery[] = [
        { refId: 'A', query: '@hostname:$hostname', isLogsQuery: false },
        { refId: 'B', query: '@instance:instance', isLogsQuery: false },
      ];
      const transformations: DataTransformerConfig[] = [];

      const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);

      expect(result).toBe('');
      expect(getMock).toHaveBeenCalledTimes(2);
      expect(getMock).toHaveBeenCalledWith(datasource.name);
    });
  });

  describe('when called with some targets using a datasource that does not support alerting', () => {
    it('then it should return false', async () => {
      const alertingDatasource: DataSourceApi = ({
        meta: ({ alerting: true } as any) as PluginMeta,
        targetContainsTemplate: () => false,
        name: 'alertingDatasource',
      } as any) as DataSourceApi;
      const datasource: DataSourceApi = ({
        meta: ({ alerting: false } as any) as PluginMeta,
        targetContainsTemplate: () => false,
        name: 'datasource',
      } as any) as DataSourceApi;

      const datasourceSrv: DataSourceSrv = {
        get: (name: string) => {
          if (name === datasource.name) {
            return Promise.resolve(datasource);
          }

          return Promise.resolve(alertingDatasource);
        },
      };
      const targets: any[] = [
        { refId: 'A', query: 'some query', datasource: 'alertingDatasource' },
        { refId: 'B', query: 'some query', datasource: 'datasource' },
      ];
      const transformations: DataTransformerConfig[] = [];

      const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);

      expect(result).toBe('');
    });
  });

  describe('when called with all targets containing template variables', () => {
    it('then it should return false', async () => {
      const datasource: DataSourceApi = ({
        meta: ({ alerting: true } as any) as PluginMeta,
        targetContainsTemplate: () => true,
        name: 'some name',
      } as any) as DataSourceApi;
      const getMock = jest.fn().mockResolvedValue(datasource);
      const datasourceSrv: DataSourceSrv = {
        get: getMock,
      };
      const targets: ElasticsearchQuery[] = [
        { refId: 'A', query: '@hostname:$hostname', isLogsQuery: false },
        { refId: 'B', query: '@instance:$instance', isLogsQuery: false },
      ];
      const transformations: DataTransformerConfig[] = [];

      const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);

      expect(result).toBe('Template variables are not supported in alert queries');
      expect(getMock).toHaveBeenCalledTimes(2);
      expect(getMock).toHaveBeenCalledWith(datasource.name);
    });
  });

  describe('when called with all targets using a datasource that does not support alerting', () => {
    it('then it should return false', async () => {
      const datasource: DataSourceApi = ({
        meta: ({ alerting: false } as any) as PluginMeta,
        targetContainsTemplate: () => false,
        name: 'some name',
      } as any) as DataSourceApi;
      const getMock = jest.fn().mockResolvedValue(datasource);
      const datasourceSrv: DataSourceSrv = {
        get: getMock,
      };
      const targets: ElasticsearchQuery[] = [
        { refId: 'A', query: '@hostname:hostname', isLogsQuery: false },
        { refId: 'B', query: '@instance:instance', isLogsQuery: false },
      ];
      const transformations: DataTransformerConfig[] = [];

      const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);

      expect(result).toBe('The datasource does not support alerting queries');
      expect(getMock).toHaveBeenCalledTimes(2);
      expect(getMock).toHaveBeenCalledWith(datasource.name);
    });
  });

  describe('when called with transformations', () => {
    it('then it should return false', async () => {
      const datasource: DataSourceApi = ({
        meta: ({ alerting: true } as any) as PluginMeta,
        targetContainsTemplate: () => false,
        name: 'some name',
      } as any) as DataSourceApi;
      const getMock = jest.fn().mockResolvedValue(datasource);
      const datasourceSrv: DataSourceSrv = {
        get: getMock,
      };
      const targets: ElasticsearchQuery[] = [
        { refId: 'A', query: '@hostname:hostname', isLogsQuery: false },
        { refId: 'B', query: '@instance:instance', isLogsQuery: false },
      ];
      const transformations: DataTransformerConfig[] = [{ id: 'A', options: null }];

      const result = await getAlertingValidationMessage(transformations, targets, datasourceSrv, datasource.name);

      expect(result).toBe('Transformations are not supported in alert queries');
      expect(getMock).toHaveBeenCalledTimes(0);
    });
  });
});