/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ /** * Renders the table view for a list of validation results. */ import Box from 'aws-northstar/layouts/Box'; import Card from 'aws-northstar/components/Card'; import ProgressBar from 'aws-northstar/components/ProgressBar'; import React, { Fragment } from 'react'; import Table from 'aws-northstar/components/Table'; import LineChart, { Line } from 'aws-northstar/charts/LineChart'; import { GetCostOutput } from '@ada/api'; import { useCostContext } from '../../context'; import Button from 'aws-northstar/components/Button'; import ExpandableSection from 'aws-northstar/components/ExpandableSection'; import Heading from 'aws-northstar/components/Heading'; import Inline from 'aws-northstar/layouts/Inline'; import Stack from 'aws-northstar/layouts/Stack'; import _ from 'lodash'; export interface AccountCostsProps {} const EMPTY_VALUE = '-'; let highestCost: { date: string; cost: number } = { date: '', cost: 0 }; export const findTotalCost = (cost: GetCostOutput) => cost && _(cost.resultsByTime) .map((result) => { const _cost = _(result.groups) .map((group) => parseFloat(group.metrics?.blendedCost?.amount || '0')) .sum(); if (_cost > highestCost.cost && result.timePeriod?.start) highestCost = { date: result.timePeriod.start, cost: _cost }; return _cost; }) .sum(); const getGraphData = (cost: GetCostOutput) => { return ( cost && _(cost.resultsByTime) .map((result) => { const dailyCost = _(result.groups) .map((group) => parseFloat(group.metrics?.blendedCost?.amount || '0')) .sum(); return { name: result.timePeriod?.start, value: dailyCost.toPrecision(4) }; }) .value() ); }; const getNumberOfDays = (cost: GetCostOutput) => cost && _(cost.resultsByTime).size(); const columnDefinitions = (totalCost: number, priceFormat: Intl.NumberFormatOptions) => [ { id: 'date', width: 150, Header: 'Date', accessor: 'timePeriod.start', }, { id: 'costs', width: 150, Header: 'Cost (USD)', accessor: 'cost', Cell: ({ row }: any) => { if (row && row.original) { return _(row.original.groups) .map((group) => parseFloat(group.metrics.blendedCost.amount)) .sum() .toLocaleString(getLang(), priceFormat); } return 'null'; }, }, { id: 'percentOfTotal', width: 450, Header: 'Percent of Spend', Cell: ({ row }: any) => { if (row && row.original) { const costArray = _(row.original.groups).map((group) => { return parseFloat(group.metrics.blendedCost.amount); }); const dailyTotal: number = _(costArray).sum(); const dailyPercentage: number = parseFloat((Number(dailyTotal / totalCost) * 100).toFixed(2)); if (Number.isNaN(dailyPercentage) || dailyPercentage <= 0) { return EMPTY_VALUE; } return (