Skip to content

Commit

Permalink
Display placeholders (elastic#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSh authored Jan 3, 2022
1 parent 776fcff commit c3c4588
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

import React from 'react';
import styled from 'styled-components';
import { EuiStat, EuiFlexItem, EuiPanel, EuiIcon, EuiFlexGrid } from '@elastic/eui';
import {
EuiStat,
EuiFlexItem,
EuiPanel,
EuiIcon,
EuiFlexGrid,
EuiText,
EuiFlexGroup,
} from '@elastic/eui';
import { Chart, Settings, LineSeries } from '@elastic/charts';
import { useCloudPostureStatsApi } from '../../../common/api';
import { statusColors } from '../../../common/constants';
Expand Down Expand Up @@ -38,9 +46,19 @@ const getScoreTrendPercentage = (scoreTrend: Trend) => {
return Number((last - beforeLast).toFixed(1));
};

const placeholder = (
<EuiText size="xs" color="subdued">
{'No data to display'}
</EuiText>
);

export const ComplianceStats = () => {
const getStats = useCloudPostureStatsApi();
const postureScore = getStats.isSuccess && getStats.data.postureScore;
const benchmarks = getStats.isSuccess && getStats.data.benchmarksStats;

// TODO: in case we dont have a full length trend we will need to handle the sparkline chart alone. not rendering anything is just a temporary solution
if (!benchmarks || !postureScore) return null;

const scoreTrend = [
[0, 0],
Expand All @@ -50,11 +68,8 @@ export const ComplianceStats = () => {
[4, postureScore],
] as Trend;

// TODO: in case we dont have a full length trend we will need to handle the sparkline chart alone. not rendering anything is just a temporary solution
if (!postureScore || scoreTrend.length < 2) return null;

const scoreChange = getScoreTrendPercentage(scoreTrend);
const isPositiveChange = scoreChange > 0;
// const isPositiveChange = scoreChange > 0;

const stats = [
{
Expand All @@ -64,45 +79,52 @@ export const ComplianceStats = () => {
iconType: getScoreIcon(postureScore),
},
{
title: (
<span>
<EuiIcon size="xl" type={isPositiveChange ? 'sortUp' : 'sortDown'} />
{`${scoreChange}%`}
</span>
),
// TODO: remove placeholder for the commented out component, needs BE
title: placeholder,
description: 'Posture Score Trend',
titleColor: isPositiveChange ? 'success' : 'danger',
renderBody: (
<>
<Chart size={{ height: 30 }}>
<Settings
showLegend={false}
tooltip="none"
theme={{
lineSeriesStyle: {
point: {
visible: false,
},
},
}}
/>
<LineSeries
id="posture-score-trend-sparkline"
data={scoreTrend}
xAccessor={0}
yAccessors={[1]}
color={isPositiveChange ? statusColors.success : statusColors.danger}
/>
</Chart>
</>
),
},
// {
// title: (
// <span>
// <EuiIcon size="xl" type={isPositiveChange ? 'sortUp' : 'sortDown'} />
// {`${scoreChange}%`}
// </span>
// ),
// description: 'Posture Score Trend',
// titleColor: isPositiveChange ? 'success' : 'danger',
// renderBody: (
// <>
// <Chart size={{ height: 30 }}>
// <Settings
// showLegend={false}
// tooltip="none"
// theme={{
// lineSeriesStyle: {
// point: {
// visible: false,
// },
// },
// }}
// />
// <LineSeries
// id="posture-score-trend-sparkline"
// data={scoreTrend}
// xAccessor={0}
// yAccessors={[1]}
// color={isPositiveChange ? statusColors.success : statusColors.danger}
// />
// </Chart>
// </>
// ),
// },
{
title: '1',
// TODO: this should count only ACTIVE benchmarks. needs BE
title: benchmarks.length,
description: 'Active Frameworks',
},
{
title: '1,369',
// TODO: should be relatively simple to return from BE. needs BE
title: placeholder,
description: 'Total Resources',
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ interface ComplianceTrendChartProps {

export const dateValueToTuple = ({ date, value }: { date: number; value: number }) => [date, value];

export const ComplianceTrendChart = ({ data: { postureScore } }: ComplianceTrendChartProps) => {
if (postureScore === undefined) return null;

const complianceScoreTrend = [
{ date: Date.now(), value: postureScore },
{ date: Date.now() - 10000, value: 53 },
{ date: Date.now() - 30000, value: 91 },
{ date: Date.now() - 60000, value: 34 },
{ date: Date.now() - 90000, value: 10 },
];

export const ComplianceTrendChart = ({ data }: ComplianceTrendChartProps) => {
return (
<Chart size={{ height: 200 }}>
<Settings
Expand All @@ -47,7 +37,8 @@ export const ComplianceTrendChart = ({ data: { postureScore } }: ComplianceTrend
<AreaSeries
id="compliance_score"
name="Compliance Score"
data={complianceScoreTrend.map(dateValueToTuple)}
// TODO: no api for this chart yet, using empty state for now. needs BE
data={[]}
xScaleType="time"
xAccessor={0}
yAccessors={[1]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const BenchmarksSection = () => {
<EuiDescriptionList
listItems={[
{
// TODO: this shows the failed/passed ratio and not the calculated score. needs product
title: 'Compliance Score',
description: (
<ChartPanel
Expand All @@ -77,7 +78,8 @@ export const BenchmarksSection = () => {
<ChartPanel
hasBorder={false}
chart={ComplianceTrendChart}
data={benchmark}
// TODO: no api for this chart yet, using empty state for now. needs BE
data={[]}
isLoading={getStats.isLoading}
isError={getStats.isError}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const SummarySection = () => {
chart={ScorePerAccountChart}
title="Score Per Account / Cluster"
description="Non compliant first"
// TODO: no api for this chart yet, using empty state for now
// TODO: no api for this chart yet, using empty state for now. needs BE
data={[]}
isLoading={getStats.isLoading}
isError={getStats.isError}
Expand Down

0 comments on commit c3c4588

Please sign in to comment.