Skip to content

Commit

Permalink
[frontend] public widget : entities heatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
lndrtrbn committed Feb 16, 2024
1 parent 1ab67ff commit 691d549
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { graphql, PreloadedQuery, usePreloadedQuery } from 'react-relay';
import React from 'react';
import WidgetNoData from '../../../components/dashboard/WidgetNoData';
import type { PublicWidgetContainerProps } from './publicWidgetContainerProps';
import { useFormatter } from '../../../components/i18n';
import useQueryLoading from '../../../utils/hooks/useQueryLoading';
import WidgetContainer from '../../../components/dashboard/WidgetContainer';
import WidgetLoader from '../../../components/dashboard/WidgetLoader';
import { PublicStixCoreObjectsMultiHeatMapQuery } from './__generated__/PublicStixCoreObjectsMultiHeatMapQuery.graphql';
import WidgetMultiHeatMap from '../../../components/dashboard/WidgetMultiHeatMap';
import type { PublicManifestWidget } from './PublicManifest';

const publicStixCoreObjectsMultiHeatMapQuery = graphql`
query PublicStixCoreObjectsMultiHeatMapQuery(
$startDate: DateTime
$endDate: DateTime
$uriKey: String!
$widgetId : String!
) {
publicStixCoreObjectsMultiTimeSeries(
startDate: $startDate
endDate: $endDate
uriKey: $uriKey
widgetId : $widgetId
) {
data {
date
value
}
}
}
`;

interface PublicStixCoreObjectsMultiHeatMapComponentProps {
parameters: PublicManifestWidget['parameters']
dataSelection: PublicManifestWidget['dataSelection']
queryRef: PreloadedQuery<PublicStixCoreObjectsMultiHeatMapQuery>
}

const PublicStixCoreObjectsMultiHeatMapComponent = ({
parameters,
dataSelection,
queryRef,
}: PublicStixCoreObjectsMultiHeatMapComponentProps) => {
const { t_i18n } = useFormatter();
const { publicStixCoreObjectsMultiTimeSeries } = usePreloadedQuery(
publicStixCoreObjectsMultiHeatMapQuery,
queryRef,
);

if (publicStixCoreObjectsMultiTimeSeries) {
const allValues = publicStixCoreObjectsMultiTimeSeries
.map((serie) => (serie?.data ?? []).flatMap((o) => o?.value ?? []))
.flat();
const maxValue = Math.max(...allValues);
const minValue = Math.min(...allValues);

return (
<WidgetMultiHeatMap
data={publicStixCoreObjectsMultiTimeSeries.map((serie, i) => ({
name: dataSelection[i].label ?? t_i18n('Number of entities'),
data: (serie?.data ?? []).map((entry) => ({
x: new Date(entry?.date),
y: entry?.value,
})),
})).sort((a, b) => b.name.localeCompare(a.name))}
minValue={minValue}
maxValue={maxValue}
isStacked={parameters.stacked}
withExport={false}
readonly={true}
/>
);
}
return <WidgetNoData />;
};

const PublicStixCoreObjectsMultiHeatMap = ({
uriKey,
widget,
startDate,
endDate,
title,
}: PublicWidgetContainerProps) => {
const { t_i18n } = useFormatter();
const { id, parameters, dataSelection } = widget;
const queryRef = useQueryLoading<PublicStixCoreObjectsMultiHeatMapQuery>(
publicStixCoreObjectsMultiHeatMapQuery,
{
uriKey,
widgetId: id,
startDate,
endDate,
},
);

return (
<WidgetContainer
title={parameters.title ?? title ?? t_i18n('Entities number')}
variant="inLine"
>
{queryRef ? (
<React.Suspense fallback={<WidgetLoader />}>
<PublicStixCoreObjectsMultiHeatMapComponent
queryRef={queryRef}
parameters={parameters}
dataSelection={dataSelection}
/>
</React.Suspense>
) : (
<WidgetLoader />
)}
</WidgetContainer>
);
};

export default PublicStixCoreObjectsMultiHeatMap;
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import PublicStixCoreObjectsMultiAreaChart from './PublicStixCoreObjectsMultiAre
import PublicStixCoreObjectsTimeline from './PublicStixCoreObjectsTimeline';
import PublicStixCoreObjectsDonut from './PublicStixCoreObjectsDonut';
import PublicStixCoreObjectsRadar from './PublicStixCoreObjectsRadar';
import PublicStixCoreObjectsMultiHeatMap from './PublicStixCoreObjectsMultiHeatMap';

const usePublicDashboardWidgets = (uriKey: string, config?: PublicManifestConfig) => {
const startDate = config?.relativeDate ? computerRelativeDate(config.relativeDate) : config?.startDate;
Expand Down Expand Up @@ -103,7 +104,14 @@ const usePublicDashboardWidgets = (uriKey: string, config?: PublicManifestConfig
/>
);
case 'heatmap':
return 'Not implemented yet';
return (
<PublicStixCoreObjectsMultiHeatMap
startDate={startDate}
endDate={endDate}
uriKey={uriKey}
widget={widget}
/>
);
case 'tree':
return 'Not implemented yet';
default:
Expand Down

0 comments on commit 691d549

Please sign in to comment.