2020-03-03 09:07:54 +00:00
|
|
|
import { SUPPORTED_FORMATS } from '~/lib/utils/unit_format';
|
2020-01-24 15:09:00 +00:00
|
|
|
import {
|
|
|
|
uniqMetricsId,
|
|
|
|
parseEnvironmentsResponse,
|
|
|
|
removeLeadingSlash,
|
2020-02-27 00:09:19 +00:00
|
|
|
mapToDashboardViewModel,
|
2020-01-24 15:09:00 +00:00
|
|
|
} from '~/monitoring/stores/utils';
|
|
|
|
|
|
|
|
const projectPath = 'gitlab-org/gitlab-test';
|
2019-09-20 21:06:18 +00:00
|
|
|
|
2020-02-27 00:09:19 +00:00
|
|
|
describe('mapToDashboardViewModel', () => {
|
|
|
|
it('maps an empty dashboard', () => {
|
|
|
|
expect(mapToDashboardViewModel({})).toEqual({
|
|
|
|
dashboard: '',
|
|
|
|
panelGroups: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('maps a simple dashboard', () => {
|
|
|
|
const response = {
|
|
|
|
dashboard: 'Dashboard Name',
|
|
|
|
panel_groups: [
|
|
|
|
{
|
|
|
|
group: 'Group 1',
|
|
|
|
panels: [
|
|
|
|
{
|
|
|
|
title: 'Title A',
|
2020-03-27 12:07:43 +00:00
|
|
|
xLabel: '',
|
|
|
|
xAxis: {
|
|
|
|
name: '',
|
|
|
|
},
|
2020-02-27 00:09:19 +00:00
|
|
|
type: 'chart-type',
|
|
|
|
y_label: 'Y Label A',
|
|
|
|
metrics: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(mapToDashboardViewModel(response)).toEqual({
|
|
|
|
dashboard: 'Dashboard Name',
|
|
|
|
panelGroups: [
|
|
|
|
{
|
|
|
|
group: 'Group 1',
|
|
|
|
key: 'group-1-0',
|
|
|
|
panels: [
|
|
|
|
{
|
|
|
|
title: 'Title A',
|
|
|
|
type: 'chart-type',
|
2020-03-27 12:07:43 +00:00
|
|
|
xLabel: '',
|
|
|
|
xAxis: {
|
|
|
|
name: '',
|
|
|
|
},
|
2020-02-27 00:09:19 +00:00
|
|
|
y_label: 'Y Label A',
|
2020-03-03 09:07:54 +00:00
|
|
|
yAxis: {
|
|
|
|
name: 'Y Label A',
|
|
|
|
format: 'number',
|
|
|
|
precision: 2,
|
|
|
|
},
|
2020-02-27 00:09:19 +00:00
|
|
|
metrics: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('panel groups mapping', () => {
|
|
|
|
it('key', () => {
|
|
|
|
const response = {
|
|
|
|
dashboard: 'Dashboard Name',
|
|
|
|
panel_groups: [
|
|
|
|
{
|
|
|
|
group: 'Group A',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Group B',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: '',
|
|
|
|
unsupported_property: 'This should be removed',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(mapToDashboardViewModel(response).panelGroups).toEqual([
|
|
|
|
{
|
|
|
|
group: 'Group A',
|
|
|
|
key: 'group-a-0',
|
|
|
|
panels: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: 'Group B',
|
|
|
|
key: 'group-b-1',
|
|
|
|
panels: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: '',
|
|
|
|
key: 'default-2',
|
|
|
|
panels: [],
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-03 09:07:54 +00:00
|
|
|
describe('panel mapping', () => {
|
|
|
|
const panelTitle = 'Panel Title';
|
|
|
|
const yAxisName = 'Y Axis Name';
|
|
|
|
|
|
|
|
let dashboard;
|
|
|
|
|
|
|
|
const setupWithPanel = panel => {
|
|
|
|
dashboard = {
|
|
|
|
panel_groups: [
|
|
|
|
{
|
|
|
|
panels: [panel],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const getMappedPanel = () => mapToDashboardViewModel(dashboard).panelGroups[0].panels[0];
|
|
|
|
|
2020-03-27 12:07:43 +00:00
|
|
|
it('panel with x_label', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
title: panelTitle,
|
|
|
|
x_label: 'x label',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel()).toEqual({
|
|
|
|
title: panelTitle,
|
|
|
|
xLabel: 'x label',
|
|
|
|
xAxis: {
|
|
|
|
name: 'x label',
|
|
|
|
},
|
|
|
|
y_label: '',
|
|
|
|
yAxis: {
|
|
|
|
name: '',
|
|
|
|
format: SUPPORTED_FORMATS.number,
|
|
|
|
precision: 2,
|
|
|
|
},
|
|
|
|
metrics: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-03 09:07:54 +00:00
|
|
|
it('group y_axis defaults', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
title: panelTitle,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel()).toEqual({
|
|
|
|
title: panelTitle,
|
2020-03-27 12:07:43 +00:00
|
|
|
xLabel: '',
|
2020-03-03 09:07:54 +00:00
|
|
|
y_label: '',
|
2020-03-27 12:07:43 +00:00
|
|
|
xAxis: {
|
|
|
|
name: '',
|
|
|
|
},
|
2020-03-03 09:07:54 +00:00
|
|
|
yAxis: {
|
|
|
|
name: '',
|
|
|
|
format: SUPPORTED_FORMATS.number,
|
|
|
|
precision: 2,
|
|
|
|
},
|
|
|
|
metrics: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('panel with y_axis.name', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
y_axis: {
|
|
|
|
name: yAxisName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel().y_label).toBe(yAxisName);
|
|
|
|
expect(getMappedPanel().yAxis.name).toBe(yAxisName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('panel with y_axis.name and y_label, displays y_axis.name', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
y_label: 'Ignored Y Label',
|
|
|
|
y_axis: {
|
|
|
|
name: yAxisName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel().y_label).toBe(yAxisName);
|
|
|
|
expect(getMappedPanel().yAxis.name).toBe(yAxisName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('group y_label', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
y_label: yAxisName,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel().y_label).toBe(yAxisName);
|
|
|
|
expect(getMappedPanel().yAxis.name).toBe(yAxisName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('group y_axis format and precision', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
title: panelTitle,
|
|
|
|
y_axis: {
|
|
|
|
precision: 0,
|
|
|
|
format: SUPPORTED_FORMATS.bytes,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel().yAxis.format).toBe(SUPPORTED_FORMATS.bytes);
|
|
|
|
expect(getMappedPanel().yAxis.precision).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('group y_axis unsupported format defaults to number', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
title: panelTitle,
|
|
|
|
y_axis: {
|
|
|
|
format: 'invalid_format',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel().yAxis.format).toBe(SUPPORTED_FORMATS.number);
|
|
|
|
});
|
2020-04-01 18:07:56 +00:00
|
|
|
|
|
|
|
// This property allows single_stat panels to render percentile values
|
|
|
|
it('group maxValue', () => {
|
|
|
|
setupWithPanel({
|
|
|
|
max_value: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedPanel().maxValue).toBe(100);
|
|
|
|
});
|
2020-03-03 09:07:54 +00:00
|
|
|
});
|
|
|
|
|
2020-02-27 00:09:19 +00:00
|
|
|
describe('metrics mapping', () => {
|
|
|
|
const defaultLabel = 'Panel Label';
|
|
|
|
const dashboardWithMetric = (metric, label = defaultLabel) => ({
|
|
|
|
panel_groups: [
|
|
|
|
{
|
|
|
|
panels: [
|
|
|
|
{
|
|
|
|
y_label: label,
|
|
|
|
metrics: [metric],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const getMappedMetric = dashboard => {
|
|
|
|
return mapToDashboardViewModel(dashboard).panelGroups[0].panels[0].metrics[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
it('creates a metric', () => {
|
2020-04-09 15:09:29 +00:00
|
|
|
const dashboard = dashboardWithMetric({ label: 'Panel Label' });
|
2020-02-27 00:09:19 +00:00
|
|
|
|
|
|
|
expect(getMappedMetric(dashboard)).toEqual({
|
|
|
|
label: expect.any(String),
|
|
|
|
metricId: expect.any(String),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-13 00:09:34 +00:00
|
|
|
it('creates a metric with a correct id', () => {
|
2020-02-27 00:09:19 +00:00
|
|
|
const dashboard = dashboardWithMetric({
|
|
|
|
id: 'http_responses',
|
|
|
|
metric_id: 1,
|
|
|
|
});
|
|
|
|
|
2020-03-13 00:09:34 +00:00
|
|
|
expect(getMappedMetric(dashboard).metricId).toEqual('1_http_responses');
|
2020-02-27 00:09:19 +00:00
|
|
|
});
|
|
|
|
|
2020-04-09 15:09:29 +00:00
|
|
|
it('creates a metric without a default label', () => {
|
2020-02-27 00:09:19 +00:00
|
|
|
const dashboard = dashboardWithMetric({});
|
|
|
|
|
|
|
|
expect(getMappedMetric(dashboard)).toMatchObject({
|
2020-04-09 15:09:29 +00:00
|
|
|
label: undefined,
|
2020-02-27 00:09:19 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates a metric with an endpoint and query', () => {
|
|
|
|
const dashboard = dashboardWithMetric({
|
|
|
|
prometheus_endpoint_path: 'http://test',
|
|
|
|
query_range: 'http_responses',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedMetric(dashboard)).toMatchObject({
|
|
|
|
prometheusEndpointPath: 'http://test',
|
|
|
|
queryRange: 'http_responses',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates a metric with an ad-hoc property', () => {
|
|
|
|
// This behavior is deprecated and should be removed
|
|
|
|
// https://gitlab.com/gitlab-org/gitlab/issues/207198
|
|
|
|
|
|
|
|
const dashboard = dashboardWithMetric({
|
|
|
|
x_label: 'Another label',
|
|
|
|
unkown_option: 'unkown_data',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(getMappedMetric(dashboard)).toMatchObject({
|
|
|
|
x_label: 'Another label',
|
|
|
|
unkown_option: 'unkown_data',
|
|
|
|
});
|
2019-09-20 21:06:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('uniqMetricsId', () => {
|
|
|
|
[
|
2020-04-02 21:07:51 +00:00
|
|
|
{ input: { id: 1 }, expected: 'NO_DB_1' },
|
2019-09-20 21:06:18 +00:00
|
|
|
{ input: { metric_id: 2 }, expected: '2_undefined' },
|
|
|
|
{ input: { metric_id: 2, id: 21 }, expected: '2_21' },
|
|
|
|
{ input: { metric_id: 22, id: 1 }, expected: '22_1' },
|
|
|
|
{ input: { metric_id: 'aaa', id: '_a' }, expected: 'aaa__a' },
|
|
|
|
].forEach(({ input, expected }) => {
|
|
|
|
it(`creates unique metric ID with ${JSON.stringify(input)}`, () => {
|
|
|
|
expect(uniqMetricsId(input)).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-01-24 15:09:00 +00:00
|
|
|
|
|
|
|
describe('parseEnvironmentsResponse', () => {
|
|
|
|
[
|
|
|
|
{
|
|
|
|
input: null,
|
|
|
|
output: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: undefined,
|
|
|
|
output: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: [],
|
|
|
|
output: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: [
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
name: 'env-1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'env-1',
|
|
|
|
metrics_path: `${projectPath}/environments/1/metrics`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: [
|
|
|
|
{
|
|
|
|
id: 'gid://gitlab/Environment/12',
|
|
|
|
name: 'env-12',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
output: [
|
|
|
|
{
|
|
|
|
id: 12,
|
|
|
|
name: 'env-12',
|
|
|
|
metrics_path: `${projectPath}/environments/12/metrics`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
].forEach(({ input, output }) => {
|
|
|
|
it(`parseEnvironmentsResponse returns ${JSON.stringify(output)} with input ${JSON.stringify(
|
|
|
|
input,
|
|
|
|
)}`, () => {
|
|
|
|
expect(parseEnvironmentsResponse(input, projectPath)).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('removeLeadingSlash', () => {
|
|
|
|
[
|
|
|
|
{ input: null, output: '' },
|
|
|
|
{ input: '', output: '' },
|
|
|
|
{ input: 'gitlab-org', output: 'gitlab-org' },
|
|
|
|
{ input: 'gitlab-org/gitlab', output: 'gitlab-org/gitlab' },
|
|
|
|
{ input: '/gitlab-org/gitlab', output: 'gitlab-org/gitlab' },
|
|
|
|
{ input: '////gitlab-org/gitlab', output: 'gitlab-org/gitlab' },
|
|
|
|
].forEach(({ input, output }) => {
|
|
|
|
it(`removeLeadingSlash returns ${output} with input ${input}`, () => {
|
|
|
|
expect(removeLeadingSlash(input)).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|