Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructured visMetaData and dataConfig Objects in all visualizations #1106

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
display: flex;
justify-content: center;
align-items: center;
height: 100%;
font-size: 20px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,12 @@ export const getPropName = (queriedVizObj: {
name: string;
label: string;
}) => {
if (queriedVizObj[CUSTOM_LABEL] === '' || queriedVizObj[CUSTOM_LABEL] === undefined) {
return `${queriedVizObj.aggregation}(${queriedVizObj.name})`;
if (queriedVizObj) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we take look at reviews from previous PRs and having one more reviewer internally to review to avoid spending effort reviewing the same issues? Previously we've reviewed some similar code already like this, below code piece can be further simplified as

if (queriedVizObj && !isEmpty(queriedVizObj[CUSTOM_LABEL]))  return `${queriedVizObj.aggregation}(${queriedVizObj.name})`;
return '';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mengweieric Sure, will ensure in next PRs that such small issues can be addressed internally.

if (queriedVizObj[CUSTOM_LABEL] === '' || queriedVizObj[CUSTOM_LABEL] === undefined) {
return `${queriedVizObj.aggregation}(${queriedVizObj.name})`;
}
return queriedVizObj[CUSTOM_LABEL];
} else {
return '';
}
return queriedVizObj[CUSTOM_LABEL];
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { find, isEmpty, last, some } from 'lodash';
import React, { useMemo } from 'react';
import {
AGGREGATIONS,
DEFAULT_BAR_CHART_STYLES,
BREAKDOWNS,
DEFAULT_BAR_CHART_STYLES,
GROUPBY,
} from '../../../../../common/constants/explorer';
import {
Expand Down Expand Up @@ -37,36 +37,61 @@ export const Bar = ({ visualizations, layout, config }: any) => {
data: queriedVizData,
metadata: { fields },
},
userConfigs,
userConfigs: {
dataConfig: {
colorTheme = [],
chartStyles = {},
span = {},
legend = {},
panelOptions = {},
[GROUPBY]: dimensions = [],
[AGGREGATIONS]: series = [],
[BREAKDOWNS]: breakdowns = [],
} = {},
layoutConfig = {},
availabilityConfig = {},
},
},
vis: {
type,
icontype,
fillopacity,
orientation,
labelangle,
linewidth,
barwidth,
groupwidth,
showlegend,
legendposition,
},
vis: visMetaData,
}: IVisualizationContainerProps = visualizations;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could destruct visMetaData at line 30 directly without renaming. Please apply this code review to all other charts that have this similar problem.

Copy link
Contributor Author

@Koustubh5585 Koustubh5585 Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mengweieric, this is already taken care, can you please check once https://github.com/opensearch-project/observability/pull/1106/files (commit id: 95fef81)

const lastIndex = fields.length - 1;
const { dataConfig = {}, layoutConfig = {}, availabilityConfig = {} } = userConfigs;

/**
* determine stylings
*/
const barOrientation = dataConfig.chartStyles?.orientation || visMetaData.orientation;
const barOrientation = chartStyles.orientation || orientation;
const isVertical = barOrientation === BarOrientation.vertical;

const tickAngle = dataConfig?.chartStyles?.rotateBarLabels || visMetaData.labelangle;
const lineWidth = dataConfig?.chartStyles?.lineWidth || visMetaData.linewidth;
const tickAngle = chartStyles.rotateBarLabels || labelangle;
const lineWidth = chartStyles.lineWidth || linewidth;
const fillOpacity =
dataConfig?.chartStyles?.fillOpacity !== undefined
? dataConfig?.chartStyles?.fillOpacity / FILLOPACITY_DIV_FACTOR
: visMetaData.fillopacity / FILLOPACITY_DIV_FACTOR;
const barWidth = 1 - (dataConfig?.chartStyles?.barWidth || visMetaData.barwidth);
const groupWidth = 1 - (dataConfig?.chartStyles?.groupWidth || visMetaData.groupwidth);
const showLegend = !(
dataConfig?.legend?.showLegend && dataConfig.legend.showLegend !== visMetaData.showlegend
);
const legendPosition = dataConfig?.legend?.position || visMetaData.legendposition;
const labelSize = dataConfig?.chartStyles?.labelSize || DEFAULT_BAR_CHART_STYLES.LabelSize;
const legendSize = dataConfig?.legend?.legendSize;
chartStyles.fillOpacity !== undefined
? chartStyles.fillOpacity / FILLOPACITY_DIV_FACTOR
: fillopacity / FILLOPACITY_DIV_FACTOR;
const barWidth = 1 - (chartStyles.barWidth || barwidth);
const groupWidth = 1 - (chartStyles.groupWidth || groupwidth);
const showLegend = !(legend.showLegend && legend.showLegend !== showlegend);
const legendPosition = legend.position || legendposition;

visualizations.data?.rawVizData?.dataConfig?.metrics
? visualizations.data.rawVizData.dataConfig.metrics
: [];
const labelSize = chartStyles.labelSize || DEFAULT_BAR_CHART_STYLES.LabelSize;
const legendSize = legend.legendSize;
const getSelectedColorTheme = (field: any, index: number) =>
(dataConfig?.colorTheme?.length > 0 &&
dataConfig.colorTheme.find((colorSelected) => colorSelected.name.name === field)?.color) ||
(colorTheme.length > 0 &&
colorTheme.find((colorSelected) => colorSelected.name.name === field.label)?.color) ||
PLOTLY_COLOR[index % PLOTLY_COLOR.length];

let bars;
Expand All @@ -76,30 +101,29 @@ export const Bar = ({ visualizations, layout, config }: any) => {
*/
const xaxes = useMemo(() => {
// breakdown selections
if (dataConfig[BREAKDOWNS]) {
if (breakdowns) {
return [
...dataConfig[GROUPBY].filter(
(dimension) =>
!some(dataConfig[BREAKDOWNS], (breakdown) => breakdown.label === dimension.label)
...dimensions.filter(
(dimension) => !some(breakdowns, (breakdown) => breakdown.label === dimension.label)
),
];
}

// span selection
const timestampField = find(fields, (field) => field.type === 'timestamp');
if (dataConfig.span && dataConfig.span.time_field && timestampField) {
return [timestampField, ...dataConfig[GROUPBY]];
if (span && span.time_field && timestampField) {
return [timestampField, ...dimensions];
}

return dataConfig[GROUPBY];
}, [dataConfig[GROUPBY], dataConfig[BREAKDOWNS]]);
return [...dimensions];
}, [dimensions, breakdowns]);

/**
* determine y axis
*/
const yaxes = useMemo(() => {
return Array.isArray(dataConfig[AGGREGATIONS]) ? [...dataConfig[AGGREGATIONS]] : [];
}, [dataConfig[AGGREGATIONS]]);
return Array.isArray(series) ? [...series] : [];
}, [series]);

/**
* prepare data for visualization, map x-xais to y-xais
Expand All @@ -124,7 +148,7 @@ export const Bar = ({ visualizations, layout, config }: any) => {
return {
y: isVertical ? queriedVizData[getPropName(yMetric)] : chartAxis,
x: isVertical ? chartAxis : queriedVizData[getPropName(yMetric)],
type: visMetaData.type,
type: type,
marker: {
color: fillColor,
line: {
Expand All @@ -139,12 +163,12 @@ export const Bar = ({ visualizations, layout, config }: any) => {

if (
isEmpty(queriedVizData) ||
!Array.isArray(dataConfig[GROUPBY]) ||
!Array.isArray(dataConfig[AGGREGATIONS]) ||
(dataConfig[BREAKDOWNS] && !Array.isArray(dataConfig[BREAKDOWNS])) ||
!Array.isArray(dimensions) ||
!Array.isArray(series) ||
(breakdowns && !Array.isArray(breakdowns)) ||
yaxes.length === 0
)
return <EmptyPlaceholder icon={visMetaData?.icontype} />;
return <EmptyPlaceholder icon={icontype} />;

// If chart has length of result buckets < 16
// then use the LONG_CHART_COLOR for all the bars in the chart
Expand All @@ -155,8 +179,8 @@ export const Bar = ({ visualizations, layout, config }: any) => {
colorway: plotlyColorway,
...layout,
...(layoutConfig.layout && layoutConfig.layout),
title: dataConfig?.panelOptions?.title || layoutConfig.layout?.title || '',
barmode: dataConfig?.chartStyles?.mode || visualizations.vis.mode,
title: panelOptions.title || layoutConfig.layout?.title || '',
barmode: chartStyles.mode || visualizations.vis.mode,
xaxis: {
...(isVertical && { tickangle: tickAngle }),
automargin: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,33 @@ export const DataTable = ({ visualizations, layout, config }: any) => {
jsonData,
metadata: { fields = [] },
},
userConfigs,
userConfigs: {
dataConfig: { chartStyles = {} },
},
},
vis: visMetaData,
}: IVisualizationContainerProps = visualizations;

const { dataConfig = {} } = userConfigs;
const enablePagination =
typeof dataConfig?.chartStyles?.enablePagination !== 'undefined'
? dataConfig?.chartStyles?.enablePagination
typeof chartStyles.enablePagination !== 'undefined'
? chartStyles.enablePagination
: visualizations.vis.enablepagination;

const showTableHeader =
typeof dataConfig?.chartStyles?.showTableHeader !== 'undefined'
? dataConfig?.chartStyles?.showTableHeader
typeof chartStyles.showTableHeader !== 'undefined'
? chartStyles.showTableHeader
: visualizations.vis.showtableheader;

const colunmFilter =
typeof dataConfig?.chartStyles?.colunmFilter !== 'undefined'
? dataConfig?.chartStyles?.colunmFilter
typeof chartStyles.colunmFilter !== 'undefined'
? chartStyles.colunmFilter
: visualizations.vis.colunmfilter;

const columnAlignment =
dataConfig?.chartStyles?.columnAlignment || visualizations.vis.columnalignment;
const columnAlignment = chartStyles.columnAlignment || visualizations.vis.columnalignment;

const columnWidth =
typeof dataConfig?.chartStyles?.columnWidth !== 'undefined'
? dataConfig?.chartStyles?.columnWidth
typeof chartStyles.columnWidth !== 'undefined'
? chartStyles.columnWidth
: visualizations.vis.columnwidth;

useEffect(() => {
Expand Down Expand Up @@ -127,14 +127,14 @@ export const DataTable = ({ visualizations, layout, config }: any) => {
}, [colunmFilter, columnAlignment]);

useEffect(() => {
if (!dataConfig?.chartStyles?.columnWidth) {
if (!chartStyles.columnWidth) {
gridRef?.current?.api?.sizeColumnsToFit();
} else {
columns.forEach((col: any) =>
gridRef?.current?.columnApi?.setColumnWidth(col.field, Number(columnWidth))
);
}
}, [columnWidth, columns, dataConfig]);
}, [columnWidth, columns]);

const onPageSizeChanged = useCallback(
(val: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useMemo } from 'react';
import Plotly from 'plotly.js-dist';
import { find, isEmpty } from 'lodash';
import { Plt } from '../../../plotly/plot';
import Plotly from 'plotly.js-dist';
import React, { useMemo } from 'react';
import {
AGGREGATIONS,
GROUPBY,
PLOTLY_GAUGE_COLUMN_NUMBER,
DEFAULT_GAUGE_CHART_PARAMETERS,
} from '../../../../../../common/constants/explorer';
import { IVisualizationContainerProps } from '../../../../../../common/types/explorer';
import { PLOT_MARGIN } from '../../../../../../common/constants/shared';
import { ThresholdUnitType } from '../../../../event_analytics/explorer/visualizations/config_panel/config_panes/config_controls/config_thresholds';
import { EmptyPlaceholder } from '../../../../event_analytics/explorer/visualizations/shared_components/empty_placeholder';
import { IVisualizationContainerProps } from '../../../../../../common/types/explorer';
import { getPropName } from '../../../../event_analytics/utils/utils';
import { Plt } from '../../../plotly/plot';

const {
GaugeTitleSize,
Expand All @@ -34,43 +34,45 @@ export const Gauge = ({ visualizations, layout, config }: any) => {
data: queriedVizData,
metadata: { fields },
},
userConfigs,
userConfigs: {
dataConfig: {
span = {},
chartStyles = {},
panelOptions = {},
thresholds = [],
[GROUPBY]: dimensions = [],
[AGGREGATIONS]: series = [],
},
layoutConfig = {},
},
},
vis: visMetaData,
vis: { icontype },
}: IVisualizationContainerProps = visualizations;

// data config parametrs
const { dataConfig = {}, layoutConfig = {} } = userConfigs;
let xaxes = dataConfig[GROUPBY] ?? [];
const series = dataConfig[AGGREGATIONS] ?? [];
const seriesLength = series.length;
const numberOfGauges = dataConfig?.panelOptions?.numberOfGauges || DisplayDefaultGauges;
const numberOfGauges = panelOptions.numberOfGauges || DisplayDefaultGauges;

// style parameters
const thresholds = dataConfig?.thresholds || [];
const titleSize = dataConfig?.chartStyles?.titleSize || GaugeTitleSize;
const valueSize = dataConfig?.chartStyles?.valueSize;
const showThresholdMarkers = dataConfig?.chartStyles?.showThresholdMarkers || false;
const showThresholdLabels = dataConfig?.chartStyles?.showThresholdLabels || false;
const orientation = dataConfig?.chartStyles?.orientation || OrientationDefault;
const legendPlacement = dataConfig?.chartStyles?.legendPlacement || LegendPlacement;

const titleSize = chartStyles.titleSize || GaugeTitleSize;
const valueSize = chartStyles.valueSize;
const showThresholdMarkers = chartStyles.showThresholdMarkers || false;
const showThresholdLabels = chartStyles.showThresholdLabels || false;
const orientation = chartStyles.orientation || OrientationDefault;
const legendPlacement = chartStyles.legendPlacement || LegendPlacement;

let xaxes = dimensions;
const isEmptyPlot = !seriesLength || isEmpty(queriedVizData);

const timestampField = find(fields, (field) => field.type === 'timestamp');

if (dataConfig.span && dataConfig.span.time_field && timestampField) {
if (span && span.time_field && timestampField) {
xaxes = [timestampField, ...xaxes];
}

const xaxesLength = xaxes.length;

if (isEmptyPlot) return <EmptyPlaceholder icon={visMetaData?.icontype} />;
if (isEmptyPlot) return <EmptyPlaceholder icon={icontype} />;

const gaugeData: Plotly.Data[] = useMemo(() => {
let calculatedGaugeData: Plotly.Data[] = [];
// case 1,2: no dimension, single/multiple metrics
if (!xaxesLength && seriesLength >= 1) {
if (!xaxes.length && seriesLength >= 1) {
calculatedGaugeData = series.map((seriesItem: any) => {
return {
field_name: getPropName(seriesItem),
Expand All @@ -80,7 +82,7 @@ export const Gauge = ({ visualizations, layout, config }: any) => {
}

// case 3: multiple dimensions and multiple metrics
if (xaxesLength && seriesLength) {
if (xaxes.length && seriesLength) {
const selectedDimensionsData = xaxes
.map((dimension: any) => {
return queriedVizData[dimension.name]
Expand Down Expand Up @@ -193,13 +195,13 @@ export const Gauge = ({ visualizations, layout, config }: any) => {
},
...layout,
...(layoutConfig.layout && layoutConfig.layout),
title: dataConfig?.panelOptions?.title || layoutConfig.layout?.title || '',
title: panelOptions.title || layoutConfig.layout?.title || '',
margin: {
...PLOT_MARGIN,
t: 100,
},
};
}, [layout, gaugeData.length, layoutConfig.layout, dataConfig?.panelOptions?.title, orientation]);
}, [layout, gaugeData.length, layoutConfig.layout, panelOptions.title, orientation]);

const mergedConfigs = {
...config,
Expand Down
Loading