grafana/public/app/features/correlations/utils.test.ts
Piotr Jamróz 946da57b6a
Correlations: Allow creating correlations for provisioned data sources (#73737)
* Allow creating correlations for provisioned data sources

* Update docs

* Fix linting

* Add missing props

* Add missing props

* Fix linting

* Fix linting

* Clarify error name

* Removed error handling for a non-existing use case

* Create a list of deleted data datasources based on all configs

* Add org_id to correlations

* Add tests

* Allow org_id to be null in case org_id=0 is used

* Create organization to ensure stable id is generated

* Fix linting

* Ensure backwards compatibility

* Add deprecation information

* Update comments

* Override existing datasSource variable so the UID is retrieved correctly

* Migrate correlations indices

* Default org_id when migrating

* Remove redundant default

* Make PK non-nullable

* Post merge fixes

* Separate data sources / correlations provisioning

* Adjust comments

* Store new data sources in spy store so it can be used to test correlations as well

* Fix linting

* Update tests

* Ensure response is closed

* Avoid creating duplicates during provisioning

* Fix updating provisioned column and update tests

* Rename error message

* Fix linting errors

* Fix linting errors and rename variable

* Update test

* Update pkg/services/sqlstore/migrations/correlations_mig.go

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Remove unused error

* Fix lining

---------

Co-authored-by: Giordano Ricci <me@giordanoricci.com>
2023-09-13 15:10:09 +02:00

136 lines
4.3 KiB
TypeScript

import { DataFrame, DataSourceInstanceSettings, FieldType, toDataFrame } from '@grafana/data';
import { CorrelationData } from './useCorrelations';
import { attachCorrelationsToDataFrames } from './utils';
describe('correlations utils', () => {
it('attaches correlations defined in the configuration', () => {
const { testDataFrames, correlations, refIdMap, prometheus, elastic } = setup();
attachCorrelationsToDataFrames(testDataFrames, correlations, refIdMap);
// Loki line (no links)
expect(testDataFrames[0].fields[0].config.links).toHaveLength(0);
// Loki traceId (linked to Prometheus and Elastic)
expect(testDataFrames[0].fields[1].config.links).toHaveLength(2);
expect(testDataFrames[0].fields[1].config.links).toMatchObject([
{
title: 'logs to metrics',
internal: {
datasourceUid: prometheus.uid,
datasourceName: prometheus.name,
query: {
expr: 'target Prometheus query',
},
},
},
{
title: 'logs to logs',
internal: {
datasourceUid: elastic.uid,
datasourceName: elastic.name,
query: {
expr: 'target Elastic query',
},
},
},
]);
// Elastic line (no links)
expect(testDataFrames[1].fields[0].config.links).toHaveLength(0);
// Elastic traceId (no links)
expect(testDataFrames[1].fields[0].config.links).toHaveLength(0);
// Prometheus value (linked to Elastic)
expect(testDataFrames[2].fields[0].config.links).toHaveLength(1);
expect(testDataFrames[2].fields[0].config.links![0]).toMatchObject({
title: 'metrics to logs',
internal: {
datasourceUid: elastic.uid,
datasourceName: elastic.name,
query: {
expr: 'target Elastic query',
},
},
});
});
it('does not create duplicates when attaching links to the same data frame', () => {
const { testDataFrames, correlations, refIdMap } = setup();
attachCorrelationsToDataFrames(testDataFrames, correlations, refIdMap);
attachCorrelationsToDataFrames(testDataFrames, correlations, refIdMap);
// Loki traceId (linked to Prometheus and Elastic)
expect(testDataFrames[0].fields[1].config.links).toHaveLength(2);
// Elastic line (no links)
expect(testDataFrames[1].fields[0].config.links).toHaveLength(0);
// Prometheus value (linked to Elastic)
expect(testDataFrames[2].fields[0].config.links).toHaveLength(1);
});
});
function setup() {
const loki = { uid: 'loki-uid', name: 'loki' } as DataSourceInstanceSettings;
const elastic = { uid: 'elastic-uid', name: 'elastic' } as DataSourceInstanceSettings;
const prometheus = { uid: 'prometheus-uid', name: 'prometheus' } as DataSourceInstanceSettings;
const refIdMap = {
'Loki Query': loki.uid,
'Elastic Query': elastic.uid,
'Prometheus Query': prometheus.uid,
};
const testDataFrames: DataFrame[] = [
toDataFrame({
name: 'Loki Logs',
refId: 'Loki Query',
fields: [
{ name: 'line', values: [] },
{ name: 'traceId', values: [] },
],
}),
toDataFrame({
name: 'Elastic Logs',
refId: 'Elastic Query',
fields: [
{ name: 'line', values: [] },
{ name: 'traceId', values: [] },
],
}),
toDataFrame({
name: 'Prometheus Metrics',
refId: 'Prometheus Query',
fields: [{ name: 'value', type: FieldType.number, values: [1, 2, 3, 4, 5] }],
}),
];
const correlations: CorrelationData[] = [
{
uid: 'loki-to-prometheus',
label: 'logs to metrics',
source: loki,
target: prometheus,
config: { type: 'query', field: 'traceId', target: { expr: 'target Prometheus query' } },
provisioned: false,
},
// Test multiple correlations attached to the same field
{
uid: 'loki-to-elastic',
label: 'logs to logs',
source: loki,
target: elastic,
config: { type: 'query', field: 'traceId', target: { expr: 'target Elastic query' } },
provisioned: false,
},
{
uid: 'prometheus-to-elastic',
label: 'metrics to logs',
source: prometheus,
target: elastic,
config: { type: 'query', field: 'value', target: { expr: 'target Elastic query' } },
provisioned: false,
},
];
return { testDataFrames, correlations, refIdMap, prometheus, elastic };
}