Skip to content

Commit

Permalink
[frontend] handle Distribution.entity that might be null
Browse files Browse the repository at this point in the history
  • Loading branch information
labo-flg committed Mar 26, 2024
1 parent 9a79388 commit 2c6d529
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ const WidgetDonut = ({
const chartData = data.map((n) => n.value);
// eslint-disable-next-line no-nested-ternary
const labels = data.map((n) => (groupBy.endsWith('_id')
? defaultValue(n.entity)
? defaultValue(n.entity, 'Restricted')
: groupBy === 'entity_type' && t_i18n(`entity_${n.label}`) !== `entity_${n.label}`
? t_i18n(`entity_${n.label}`)
: n.label));

let chartColors = [];
if (data.at(0)?.entity?.color) {
chartColors = data.map((n) => (theme.palette.mode === 'light' && n.entity.color === '#ffffff'
chartColors = data.map((n) => (theme.palette.mode === 'light' && n.entity?.color === '#ffffff'
? '#000000'
: n.entity.color));
: n.entity?.color));
}
if (data.at(0)?.entity?.x_opencti_color) {
chartColors = data.map((n) => (theme.palette.mode === 'light'
&& n.entity.x_opencti_color === '#ffffff'
&& n.entity?.x_opencti_color === '#ffffff'
? '#000000'
: n.entity.x_opencti_color));
: n.entity?.x_opencti_color));
}
if (data.at(0)?.entity?.template?.color) {
chartColors = data.map((n) => (theme.palette.mode === 'light' && n.entity.template.color === '#ffffff'
chartColors = data.map((n) => (theme.palette.mode === 'light' && n.entity?.template.color === '#ffffff'
? '#000000'
: n.entity.template.color));
: n.entity?.template.color));
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const GroupingsDonut = (props) => {
let data = resultProps.groupingsDistribution;
if (field && field.includes('internal_id')) {
data = R.map(
(n) => R.assoc('label', n.entity.name, n),
(n) => R.assoc('label', n.entity?.name ?? 'Restricted', n),
resultProps.groupingsDistribution,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class GroupingsHorizontalBars extends Component {
&& props.groupingsDistribution.length > 0
) {
const data = props.groupingsDistribution.map((n) => ({
x: n.entity.name,
x: n.entity?.name ?? 'Restricted',
y: n.value,
}));
const chartData = [{ name: t('Number of groupings'), data }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class StixCoreObjectGroupingsDonut extends Component {
let data = props.groupingsDistribution;
if (field && field.includes('internal_id')) {
data = R.map(
(n) => R.assoc('label', n.entity.name, n),
(n) => R.assoc('label', n.entity?.name ?? 'Restricted', n),
props.groupingsDistribution,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StixCoreObjectGroupingsHorizontalBars extends Component {
&& props.groupingsDistribution.length > 0
) {
const data = props.groupingsDistribution.map((n) => ({
x: n.entity.name,
x: n.entity?.name ?? 'Restricted',
y: n.value,
}));
const chartData = [{ name: t('Number of groupings'), data }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ReportsDonut extends Component {
let data = props.reportsDistribution;
if (field && field.includes('internal_id')) {
data = R.map(
(n) => R.assoc('label', n.entity.name, n),
(n) => R.assoc('label', n.entity?.name ?? 'Restricted', n),
props.reportsDistribution,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ReportsHorizontalBars extends Component {
&& props.reportsDistribution.length > 0
) {
const data = props.reportsDistribution.map((n) => ({
x: n.entity.name,
x: n.entity?.name ?? 'Restricted',
y: n.value,
}));
const chartData = [{ name: t('Number of reports'), data }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StixCoreObjectReportsHorizontalBars extends Component {
&& props.reportsDistribution.length > 0
) {
const data = props.reportsDistribution.map((n) => ({
x: n.entity.name,
x: n.entity?.name ?? 'Restricted',
y: n.value,
}));
const chartData = [{ name: t('Number of reports'), data }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,13 @@ const AuditsDistributionList = ({
selection.attribute.endsWith('.id')
|| selection.attribute.endsWith('_id')
|| selection.attribute.endsWith('_ids')
? o.entity.id
? o.entity?.id
: null,
type:
selection.attribute.endsWith('.id')
|| selection.attribute.endsWith('_id')
|| selection.attribute.endsWith('_ids')
? o.entity.entity_type
? o.entity?.entity_type
: o.label,
}));
return <WidgetDistributionList data={data} hasSettingAccess={hasSetAccess} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ const AuditsHorizontalBars = ({
selection.attribute.endsWith('.id')
|| selection.attribute.endsWith('_id')
|| selection.attribute.endsWith('_ids')
? itemColor(n.entity.entity_type)
? itemColor(n.entity?.entity_type)
: itemColor(n.label),
}));
const chartData = [
Expand All @@ -305,11 +305,11 @@ const AuditsHorizontalBars = ({
|| selection.attribute.endsWith('_id')
|| selection.attribute.endsWith('_ids')
? props.auditsDistribution.map((n) => ({
id: n.entity.id,
id: n.entity?.id,
entity_type:
n.entity.entity_type === 'Workspace'
? n.entity.type
: n.entity.entity_type,
n.entity?.entity_type === 'Workspace'
? n.entity.type
: n.entity?.entity_type,
}))
: null;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class GlobalVictimologyMap extends Component {
x.entity,
),
R.filter(
(n) => n.entity.entity_type === 'Country',
(n) => n.entity?.entity_type === 'Country',
props.stixCoreRelationshipsDistribution,
),
);
const cities = R.map(
(x) => x.entity,
R.filter(
(n) => n.entity.entity_type === 'City',
(n) => n.entity?.entity_type === 'City',
props.stixCoreRelationshipsDistribution,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const StixCoreObjectsDistributionList = ({
: o.label,
value: o.value,
color: o.entity?.color ?? o.entity?.x_opencti_color,
id: selection.attribute.endsWith('_id') ? o.entity.id : null,
id: selection.attribute.endsWith('_id') ? o.entity?.id : null,
type: o.entity?.entity_type ?? o.label,
}));
return <WidgetDistributionList data={data} hasSettingAccess={hasSetAccess} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ const StixCoreObjectsHorizontalBars = ({
) {
const data = props.stixCoreObjectsDistribution.map((n) => {
let color = selection.attribute.endsWith('_id')
? itemColor(n.entity.entity_type)
? itemColor(n.entity?.entity_type)
: itemColor(n.label);
if (n.entity?.color) {
color = theme.palette.mode === 'light' && n.entity.color === '#ffffff'
Expand All @@ -250,13 +250,13 @@ const StixCoreObjectsHorizontalBars = ({
}
if (n.entity?.x_opencti_color) {
color = theme.palette.mode === 'light'
&& n.entity.x_opencti_color === '#ffffff'
&& n.entity.x_opencti_color === '#ffffff'
? '#000000'
: n.entity.x_opencti_color;
}
if (n.entity?.template?.color) {
color = theme.palette.mode === 'light'
&& n.entity.template.color === '#ffffff'
&& n.entity.template.color === '#ffffff'
? '#000000'
: n.entity.template.color;
}
Expand All @@ -279,8 +279,8 @@ const StixCoreObjectsHorizontalBars = ({
];
const redirectionUtils = selection.attribute === 'name'
? props.stixCoreObjectsDistribution.map((n) => ({
id: n.entity.id,
entity_type: n.entity.entity_type,
id: n.entity?.id,
entity_type: n.entity?.entity_type,
}))
: undefined;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ const stixCoreObjectsMultiHorizontalBars = ({
) {
const data = props.stixCoreObjectsDistribution.map((n) => {
let color = selection.attribute.endsWith('_id')
? itemColor(n.entity.entity_type)
? itemColor(n.entity?.entity_type)
: itemColor(n.label);
if (n.entity?.color) {
color = theme.palette.mode === 'light' && n.entity.color === '#ffffff'
Expand All @@ -467,7 +467,7 @@ const stixCoreObjectsMultiHorizontalBars = ({
x:
// eslint-disable-next-line no-nested-ternary
selection.attribute.endsWith('_id')
? defaultValue(n.entity)
? defaultValue(n.entity, 'Restricted')
: selection.attribute === 'entity_type'
? t_i18n(`entity_${n.label}`)
: n.label,
Expand All @@ -483,8 +483,8 @@ const stixCoreObjectsMultiHorizontalBars = ({
];
const redirectionUtils = selection.attribute === 'name'
? props.stixCoreObjectsDistribution.map((n) => ({
id: n.entity.id,
entity_type: n.entity.entity_type,
id: n.entity?.id,
entity_type: n.entity?.entity_type,
}))
: null;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ const EntityStixCoreRelationshipsHorizontalBars = (
x:
// eslint-disable-next-line no-nested-ternary
field === 'internal_id'
? defaultValue(n.entity)
? defaultValue(n.entity, 'Restricted')
: field === 'entity_type'
? t_i18n(`entity_${n.label}`)
: n.label,
y: n.value,
fillColor:
field === 'internal_id'
? itemColor(n.entity.entity_type)
? itemColor(n.entity?.entity_type)
: itemColor(n.label),
}));
const chartData = [
Expand All @@ -257,7 +257,7 @@ const EntityStixCoreRelationshipsHorizontalBars = (
const redirectionUtils = (field === 'internal_id') ? props.stixCoreRelationshipsDistribution.map(
(n) => ({
id: n.label,
entity_type: n.entity.entity_type,
entity_type: n.entity?.entity_type,
}),
) : null;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ const StixRelationshipsDistributionList = ({
? defaultValue(o.entity)
: o.label,
value: o.value,
id: finalField.endsWith('_id') ? o.entity.id : null,
type: finalField.endsWith('_id') ? o.entity.entity_type : o.label,
id: finalField.endsWith('_id') ? o.entity?.id : null,
type: finalField.endsWith('_id') ? o.entity?.entity_type : o.label,
}));
return (
<WidgetDistributionList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,28 +260,28 @@ const StixRelationshipsHorizontalBars = ({
) {
const data = props.stixRelationshipsDistribution.map((n) => {
let color = selection.attribute.endsWith('_id')
? itemColor(n.entity.entity_type)
? itemColor(n.entity?.entity_type)
: itemColor(n.label);
if (n.entity?.color) {
color = theme.palette.mode === 'light' && n.entity.color === '#ffffff'
color = theme.palette.mode === 'light' && n.entity?.color === '#ffffff'
? '#000000'
: n.entity.color;
: n.entity?.color;
}
if (n.entity?.x_opencti_color) {
color = theme.palette.mode === 'light'
&& n.entity.x_opencti_color === '#ffffff'
&& n.entity?.x_opencti_color === '#ffffff'
? '#000000'
: n.entity.x_opencti_color;
: n.entity?.x_opencti_color;
}
if (n.entity?.template?.color) {
color = theme.palette.mode === 'light'
&& n.entity.template.color === '#ffffff'
&& n.entity?.template.color === '#ffffff'
? '#000000'
: n.entity.template.color;
: n.entity?.template.color;
}
return {
x: finalField.endsWith('_id')
? defaultValue(n.entity)
? defaultValue(n.entity, 'Restricted')
: n.label,
y: n.value,
fillColor: color,
Expand All @@ -291,7 +291,7 @@ const StixRelationshipsHorizontalBars = ({
const redirectionUtils = finalField.endsWith('_id')
? props.stixRelationshipsDistribution.map((n) => ({
id: n.label,
entity_type: n.entity.entity_type,
entity_type: n.entity?.entity_type,
}))
: undefined;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,17 +826,17 @@ const StixRelationshipsMultiHorizontalBars = ({
&& props.stixRelationshipsDistribution
&& props.stixRelationshipsDistribution.length > 0
) {
const categories = props.stixRelationshipsDistribution.map((n) => defaultValue(n.entity));
const categories = props.stixRelationshipsDistribution.map((n) => defaultValue(n.entity, 'Restricted'));
const entitiesMapping = {};
for (const distrib of props.stixRelationshipsDistribution) {
for (const subDistrib of distrib.entity[key]) {
entitiesMapping[
finalSubDistributionField === 'internal_id'
? defaultValue(subDistrib.entity)
? defaultValue(subDistrib.entity, 'Restricted')
: subDistrib.label
] = (entitiesMapping[
finalSubDistributionField === 'internal_id'
? defaultValue(subDistrib.entity)
? defaultValue(subDistrib.entity, 'Restricted')
: subDistrib.label
] || 0) + subDistrib.value;
}
Expand All @@ -849,7 +849,7 @@ const StixRelationshipsMultiHorizontalBars = ({
for (const distrib of props.stixRelationshipsDistribution) {
for (const sortedEntity of sortedEntityMapping) {
const entityData = R.head(
distrib.entity[key].filter(
distrib.entity?.[key].filter(
(n) => (finalSubDistributionField === 'internal_id'
? defaultValue(n.entity)
: n.label) === sortedEntity[0],
Expand Down Expand Up @@ -907,14 +907,14 @@ const StixRelationshipsMultiHorizontalBars = ({
const redirectionUtils = finalField === 'internal_id'
? props.stixRelationshipsDistribution.map((n) => ({
id: n.label,
entity_type: n.entity.entity_type,
entity_type: n.entity?.entity_type,
series: subSectionIdsOrder.map((subSectionId) => {
const [entity] = n.entity[key].filter(
const [entity] = n.entity[key]?.filter(
(e) => e.label === subSectionId,
);
) ?? [];
return {
id: subSectionId,
entity_type: entity ? entity.entity.entity_type : null,
entity_type: entity ? entity.entity?.entity_type : null,
};
}),
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ const StixRelationshipsPolarArea = ({
data = R.map(
(n) => R.assoc(
'label',
defaultValue(n.entity),
defaultValue(n.entity, 'Restricted'),
n,
),
props.stixRelationshipsDistribution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Chart from '../../common/charts/Chart';
import { QueryRenderer } from '../../../../relay/environment';
import inject18n from '../../../../components/i18n';
import { donutChartOptions } from '../../../../utils/Charts';
import { defaultValue } from '../../../../utils/Graph';

const styles = () => ({
paper: {
Expand Down Expand Up @@ -219,11 +220,9 @@ class EntityStixSightingRelationshipsDonut extends Component {
(n) => R.assoc(
'label',
`${
toTypes.length > 1
? `[${t(`entity_${n.entity.entity_type}`)}] ${
n.entity.name
}`
: `${n.entity.name}`
toTypes.length > 1 && n.entity
? `[${t(`entity_${n.entity.entity_type}`)}] ${n.entity.name}`
: `${defaultValue(n.entity, 'Restricted')}`
}`,
n,
),
Expand Down

0 comments on commit 2c6d529

Please sign in to comment.