Dashboards: Add variables with datasource to tracking (#114110)
This commit is contained in:
@@ -330,10 +330,16 @@ describe('DashboardSceneSerializer', () => {
|
|||||||
{
|
{
|
||||||
type: 'query',
|
type: 'query',
|
||||||
name: 'server',
|
name: 'server',
|
||||||
|
datasource: {
|
||||||
|
type: 'influxdb',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'query',
|
type: 'query',
|
||||||
name: 'host',
|
name: 'host',
|
||||||
|
datasource: {
|
||||||
|
type: 'elasticsearch',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'textbox',
|
type: 'textbox',
|
||||||
@@ -355,6 +361,10 @@ describe('DashboardSceneSerializer', () => {
|
|||||||
variable_type_textbox_count: 1,
|
variable_type_textbox_count: 1,
|
||||||
settings_nowdelay: undefined,
|
settings_nowdelay: undefined,
|
||||||
settings_livenow: true,
|
settings_livenow: true,
|
||||||
|
varsWithDataSource: [
|
||||||
|
{ type: 'query', datasource: 'influxdb' },
|
||||||
|
{ type: 'query', datasource: 'elasticsearch' },
|
||||||
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -688,16 +698,23 @@ describe('DashboardSceneSerializer', () => {
|
|||||||
schemaVersion: DASHBOARD_SCHEMA_VERSION,
|
schemaVersion: DASHBOARD_SCHEMA_VERSION,
|
||||||
settings_nowdelay: undefined,
|
settings_nowdelay: undefined,
|
||||||
settings_livenow: true,
|
settings_livenow: true,
|
||||||
|
panel_type_timeseries_count: 6,
|
||||||
|
variable_type_adhoc_count: 1,
|
||||||
|
variable_type_datasource_count: 1,
|
||||||
variable_type_custom_count: 1,
|
variable_type_custom_count: 1,
|
||||||
variable_type_query_count: 1,
|
variable_type_query_count: 1,
|
||||||
panel_type_timeseries_count: 6,
|
varsWithDataSource: [
|
||||||
|
{ type: 'query', datasource: 'cloudwatch' },
|
||||||
|
{ type: 'adhoc', datasource: 'opensearch' },
|
||||||
|
{ type: 'datasource', datasource: 'bigquery' },
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(dashboard.getDynamicDashboardsTrackingInformation()).toEqual({
|
expect(dashboard.getDynamicDashboardsTrackingInformation()).toEqual({
|
||||||
panelCount: 6,
|
panelCount: 6,
|
||||||
rowCount: 6,
|
rowCount: 6,
|
||||||
tabCount: 4,
|
tabCount: 4,
|
||||||
templateVariableCount: 2,
|
templateVariableCount: 4,
|
||||||
maxNestingLevel: 3,
|
maxNestingLevel: 3,
|
||||||
dashStructure:
|
dashStructure:
|
||||||
'[{"kind":"row","children":[{"kind":"row","children":[{"kind":"tab","children":[{"kind":"panel"},{"kind":"panel"},{"kind":"panel"}]},{"kind":"tab","children":[]}]},{"kind":"row","children":[{"kind":"row","children":[{"kind":"panel"}]}]}]},{"kind":"row","children":[{"kind":"row","children":[{"kind":"tab","children":[{"kind":"panel"}]},{"kind":"tab","children":[{"kind":"panel"}]}]}]}]',
|
'[{"kind":"row","children":[{"kind":"row","children":[{"kind":"tab","children":[{"kind":"panel"},{"kind":"panel"},{"kind":"panel"}]},{"kind":"tab","children":[]}]},{"kind":"row","children":[{"kind":"row","children":[{"kind":"panel"}]}]}]},{"kind":"row","children":[{"kind":"row","children":[{"kind":"tab","children":[{"kind":"panel"}]},{"kind":"tab","children":[{"kind":"panel"}]}]}]}]',
|
||||||
|
|||||||
@@ -1132,6 +1132,14 @@
|
|||||||
"sort": "disabled"
|
"sort": "disabled"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"kind": "AdhocVariable",
|
||||||
|
"datasource": { "name": "esmce00tbim8" },
|
||||||
|
"group": "opensearch",
|
||||||
|
"spec": {
|
||||||
|
"allowCustomValue": true
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"kind": "CustomVariable",
|
"kind": "CustomVariable",
|
||||||
"spec": {
|
"spec": {
|
||||||
@@ -1154,6 +1162,13 @@
|
|||||||
"query": "test",
|
"query": "test",
|
||||||
"skipUrlSync": false
|
"skipUrlSync": false
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"kind": "DatasourceVariable",
|
||||||
|
"spec": {
|
||||||
|
"name": "datasourceVar",
|
||||||
|
"pluginId": "bigquery"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ jest.mock('@grafana/runtime', () => ({
|
|||||||
dashboardNewLayouts: true,
|
dashboardNewLayouts: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
getDataSourceSrv: () => ({
|
||||||
|
getInstanceSettings: () => {
|
||||||
|
return { apiVersion: 'v1', meta: { multiValueFilterOperators: true } };
|
||||||
|
},
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// mock useSaveDashboardMutation
|
// mock useSaveDashboardMutation
|
||||||
@@ -72,7 +77,7 @@ describe('dashboard tracking', () => {
|
|||||||
isScene: true,
|
isScene: true,
|
||||||
tabCount: 4,
|
tabCount: 4,
|
||||||
rowCount: 2,
|
rowCount: 2,
|
||||||
templateVariableCount: 2,
|
templateVariableCount: 4,
|
||||||
maxNestingLevel: 3,
|
maxNestingLevel: 3,
|
||||||
panel_type_timeseries_count: 6,
|
panel_type_timeseries_count: 6,
|
||||||
panels_count: 6,
|
panels_count: 6,
|
||||||
@@ -89,6 +94,22 @@ describe('dashboard tracking', () => {
|
|||||||
uid: 'dashboard-test',
|
uid: 'dashboard-test',
|
||||||
variable_type_custom_count: 1,
|
variable_type_custom_count: 1,
|
||||||
variable_type_query_count: 1,
|
variable_type_query_count: 1,
|
||||||
|
variable_type_datasource_count: 1,
|
||||||
|
variable_type_adhoc_count: 1,
|
||||||
|
varsWithDataSource: [
|
||||||
|
{
|
||||||
|
datasource: 'cloudwatch',
|
||||||
|
type: 'query',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
datasource: 'opensearch',
|
||||||
|
type: 'adhoc',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
datasource: 'bigquery',
|
||||||
|
type: 'datasource',
|
||||||
|
},
|
||||||
|
],
|
||||||
hasEditPermissions: true,
|
hasEditPermissions: true,
|
||||||
hasSavePermissions: true,
|
hasSavePermissions: true,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ describe('trackDashboardLoaded', () => {
|
|||||||
],
|
],
|
||||||
templating: {
|
templating: {
|
||||||
list: [
|
list: [
|
||||||
{ type: 'query', name: 'Query 1' },
|
{ type: 'query', name: 'Query 1', datasource: { type: 'prometheus' } },
|
||||||
{ type: 'interval', name: 'Interval 1' },
|
{ type: 'interval', name: 'Interval 1' },
|
||||||
{ type: 'query', name: 'Query 2' },
|
{ type: 'query', name: 'Query 2', datasource: { type: 'cloudwatch' } },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
timepicker: {
|
timepicker: {
|
||||||
@@ -52,6 +52,10 @@ describe('trackDashboardLoaded', () => {
|
|||||||
panel_type_geomap_count: 2,
|
panel_type_geomap_count: 2,
|
||||||
settings_nowdelay: '1m',
|
settings_nowdelay: '1m',
|
||||||
settings_livenow: true,
|
settings_livenow: true,
|
||||||
|
varsWithDataSource: [
|
||||||
|
{ type: 'query', datasource: 'prometheus' },
|
||||||
|
{ type: 'query', datasource: 'cloudwatch' },
|
||||||
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
import { VariableModel } from '@grafana/schema/dist/esm/index';
|
import { VariableModel } from '@grafana/schema/dist/esm/index';
|
||||||
import { VariableKind } from '@grafana/schema/dist/esm/schema/dashboard/v2';
|
import {
|
||||||
|
AdhocVariableKind,
|
||||||
|
DatasourceVariableKind,
|
||||||
|
QueryVariableKind,
|
||||||
|
VariableKind,
|
||||||
|
} from '@grafana/schema/dist/esm/schema/dashboard/v2';
|
||||||
import { DashboardInteractions } from 'app/features/dashboard-scene/utils/interactions';
|
import { DashboardInteractions } from 'app/features/dashboard-scene/utils/interactions';
|
||||||
|
|
||||||
import { DashboardModel } from '../state/DashboardModel';
|
import { DashboardModel } from '../state/DashboardModel';
|
||||||
@@ -41,12 +46,23 @@ export function getPanelPluginCounts(panels: string[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getV1SchemaVariables(variableList: VariableModel[]) {
|
export function getV1SchemaVariables(variableList: VariableModel[]) {
|
||||||
return variableList
|
return {
|
||||||
.map((v) => v.type)
|
// Count variable types
|
||||||
.reduce((r: Record<string, number>, k) => {
|
...variableList.reduce<Record<string, number>>((variables, current) => {
|
||||||
r[variableName(k)] = 1 + r[variableName(k)] || 1;
|
variables[variableName(current.type)] = 1 + (variables[variableName(current.type)] || 0);
|
||||||
return r;
|
return variables;
|
||||||
}, {});
|
}, {}),
|
||||||
|
// List of variables with data source types
|
||||||
|
varsWithDataSource: variableList.reduce<Array<{ type: string; datasource: string }>>((variablesWithDs, current) => {
|
||||||
|
if (current.datasource?.type) {
|
||||||
|
variablesWithDs.push({
|
||||||
|
type: current.type,
|
||||||
|
datasource: current.datasource.type,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return variablesWithDs;
|
||||||
|
}, []),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapNewToOldTypes(type: VariableKind['kind']): VariableModel['type'] | undefined {
|
function mapNewToOldTypes(type: VariableKind['kind']): VariableModel['type'] | undefined {
|
||||||
@@ -73,14 +89,34 @@ function mapNewToOldTypes(type: VariableKind['kind']): VariableModel['type'] | u
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getV2SchemaVariables(variableList: VariableKind[]) {
|
export function getV2SchemaVariables(variableList: VariableKind[]) {
|
||||||
return variableList
|
return {
|
||||||
.map((v) => mapNewToOldTypes(v.kind))
|
// Count variable types
|
||||||
.filter((v) => v !== undefined)
|
...variableList.reduce<Record<string, number>>((variables, current) => {
|
||||||
.reduce((r: Record<string, number>, k) => {
|
const type = mapNewToOldTypes(current.kind);
|
||||||
r[variableName(k)] = 1 + r[variableName(k)] || 1;
|
if (type) {
|
||||||
return r;
|
variables[variableName(type)] = 1 + (variables[variableName(type)] || 0);
|
||||||
}, {});
|
}
|
||||||
|
return variables;
|
||||||
|
}, {}),
|
||||||
|
// List of variables with data source types
|
||||||
|
varsWithDataSource: variableList.reduce<Array<{ type: string; datasource: string }>>((variablesWithDs, current) => {
|
||||||
|
let datasource = '';
|
||||||
|
const type = mapNewToOldTypes(current.kind);
|
||||||
|
datasource = getDatasourceFromVar(current);
|
||||||
|
if (datasource && type) {
|
||||||
|
variablesWithDs.push({ type, datasource });
|
||||||
|
}
|
||||||
|
return variablesWithDs;
|
||||||
|
}, []),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const variableName = (type: string) => `variable_type_${type}_count`;
|
export const variableName = (type: string) => `variable_type_${type}_count`;
|
||||||
const panelName = (type: string) => `panel_type_${type}_count`;
|
const panelName = (type: string) => `panel_type_${type}_count`;
|
||||||
|
|
||||||
|
const isAdhocVar: (v: VariableKind) => v is AdhocVariableKind = (v) => v.kind === 'AdhocVariable';
|
||||||
|
const isDatasourceVar: (v: VariableKind) => v is DatasourceVariableKind = (v) => v.kind === 'DatasourceVariable';
|
||||||
|
const isQueryVar: (v: VariableKind) => v is QueryVariableKind = (v) => v.kind === 'QueryVariable';
|
||||||
|
|
||||||
|
const getDatasourceFromVar = (v: VariableKind) =>
|
||||||
|
isAdhocVar(v) ? v.group : isDatasourceVar(v) ? v.spec.pluginId : isQueryVar(v) ? v.spec?.query.group : '';
|
||||||
|
|||||||
Reference in New Issue
Block a user