-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend/frontend] adding decay chart in indicator lifecycle overview. (
- Loading branch information
1 parent
d0758d5
commit 0530e3c
Showing
10 changed files
with
448 additions
and
77 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
opencti-platform/opencti-front/src/private/components/observations/indicators/DecayChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
import { IndicatorDetails_indicator$data } from '@components/observations/indicators/__generated__/IndicatorDetails_indicator.graphql'; | ||
import Chart from '@components/common/charts/Chart'; | ||
import { ApexOptions } from 'apexcharts'; | ||
import moment from 'moment'; | ||
import { useTheme } from '@mui/styles'; | ||
import { Theme } from '@mui/material/styles/createTheme'; | ||
|
||
interface DecayChartProps { | ||
indicator: IndicatorDetails_indicator$data, | ||
} | ||
|
||
const DecayChart : FunctionComponent<DecayChartProps> = ({ indicator }) => { | ||
const theme = useTheme<Theme>(); | ||
|
||
const liveScoreColor = theme.palette.success.main; | ||
const reactionPointColor = theme.palette.text.primary; | ||
const scoreColor = theme.palette.info.main; | ||
const revokeColor = theme.palette.secondary.main; | ||
|
||
const chartLabelsTextColor = theme.palette.info.contrastText; | ||
const chartInfoTextColor = theme.palette.text.primary; | ||
const chartBackgroundColor = theme.palette.background.default; | ||
|
||
const graphLinesAnnotations = []; | ||
|
||
// Horizontal lines that shows reaction points | ||
if (indicator.decay_applied_rule?.decay_points) { | ||
indicator.decay_applied_rule.decay_points.forEach((reactionPoint) => { | ||
const lineReactionValue = { | ||
y: reactionPoint, | ||
borderColor: reactionPoint === indicator.x_opencti_score ? scoreColor : reactionPointColor, | ||
label: { | ||
borderColor: reactionPoint === indicator.x_opencti_score ? scoreColor : reactionPointColor, | ||
offsetY: 0, | ||
style: { | ||
color: chartLabelsTextColor, | ||
background: reactionPoint === indicator.x_opencti_score ? scoreColor : reactionPointColor, | ||
}, | ||
text: reactionPoint === indicator.x_opencti_score ? `Score: ${reactionPoint}` : `${reactionPoint}`, | ||
}, | ||
}; | ||
graphLinesAnnotations.push(lineReactionValue); | ||
}); | ||
|
||
// Horizontal "red" area that show the revoke zone | ||
const revokeScoreArea = { | ||
y: indicator.decay_applied_rule.decay_revoke_score + 1, // trick to have a red line even if revoke score is 0 | ||
y2: 0, | ||
borderColor: revokeColor, | ||
fillColor: revokeColor, | ||
label: { | ||
text: `Revoke zone: ${indicator.decay_applied_rule.decay_revoke_score}`, | ||
borderColor: revokeColor, | ||
style: { | ||
color: chartLabelsTextColor, | ||
background: revokeColor, | ||
}, | ||
}, | ||
}; | ||
graphLinesAnnotations.push(revokeScoreArea); | ||
} | ||
|
||
// Horizontal line that the current / live score | ||
const liveScoreLine = { | ||
y: indicator.decayLiveDetails?.live_score, | ||
borderColor: liveScoreColor, | ||
label: { | ||
borderColor: liveScoreColor, | ||
style: { | ||
color: chartLabelsTextColor, | ||
background: liveScoreColor, | ||
}, | ||
text: `Live score:${indicator.decayLiveDetails?.live_score}`, | ||
}, | ||
}; | ||
graphLinesAnnotations.push(liveScoreLine); | ||
|
||
// Time in millisecond cannot be set as number in GraphQL because it's too long | ||
// So the time in data is stored as Date and must be converted to time in ms to be drawn on the chart. | ||
const liveScoreApexFormat: { x: number; y: number }[] = []; | ||
if (indicator.decayChartData && indicator.decayChartData.live_score_serie) { | ||
indicator.decayChartData.live_score_serie.forEach((dataPoint) => { | ||
liveScoreApexFormat.push({ | ||
x: moment(dataPoint.time).valueOf(), | ||
y: dataPoint.score, | ||
}); | ||
}); | ||
} | ||
|
||
const pointAnnotations = []; | ||
pointAnnotations.push({ | ||
x: new Date().getTime(), | ||
y: indicator.decayLiveDetails?.live_score, | ||
marker: { | ||
fillColor: liveScoreColor, | ||
}, | ||
}); | ||
|
||
const chartOptions: ApexOptions = { | ||
chart: { | ||
id: 'Decay graph', | ||
toolbar: { show: false }, | ||
type: 'line', | ||
background: chartBackgroundColor, | ||
}, | ||
xaxis: { | ||
type: 'datetime', | ||
title: { | ||
text: 'Days', | ||
style: { | ||
color: chartInfoTextColor, | ||
}, | ||
}, | ||
labels: { | ||
style: { | ||
colors: chartInfoTextColor, | ||
}, | ||
}, | ||
}, | ||
yaxis: { | ||
min: 0, | ||
max: 100, | ||
title: { | ||
text: 'Score', | ||
style: { | ||
color: chartInfoTextColor, | ||
}, | ||
}, | ||
labels: { | ||
style: { | ||
colors: chartInfoTextColor, | ||
}, | ||
}, | ||
}, | ||
annotations: { | ||
yaxis: graphLinesAnnotations, | ||
points: pointAnnotations, | ||
}, | ||
grid: { show: false }, | ||
colors: [ | ||
theme.palette.primary.main, | ||
], | ||
stroke: { | ||
curve: 'smooth', | ||
}, | ||
tooltip: { | ||
theme: theme.palette.mode, // ApexChart uses 'dark'/'light', exactly the same values as we use in OpenCTI. | ||
}, | ||
}; | ||
|
||
const series = [ | ||
{ | ||
name: 'Score with decay', // this is the text on the popover | ||
data: liveScoreApexFormat, | ||
}, | ||
]; | ||
|
||
return ( | ||
<Chart | ||
series={series} | ||
options={chartOptions} | ||
/> | ||
); | ||
}; | ||
|
||
export default DecayChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.