import * as React from 'react'; import * as _ from 'lodash'; import { Box, Flex, RadioGroup, Text } from '@fluentui/react-northstar'; import { PerfData, PerfSample } from './PerfDataContext'; enum FILTER_BY { CI_BUILD = 'ci build', RELEASE = 'release', DAY = 'day', MONTH = 'month', } export type ComponentChartProps = { chartData; Filter?; Chart }; const ComponentChart: React.FunctionComponent = ({ chartData, Filter, Chart }) => { const { loading, error, data } = chartData; const [filterBy, setFilterBy] = React.useState(FILTER_BY.CI_BUILD); const [filter, setFilter] = React.useState(); let filteredData: PerfData = data; switch (filterBy) { case FILTER_BY.CI_BUILD: filteredData = data; break; case FILTER_BY.RELEASE: filteredData = data.filter(entry => entry.tag); if (!data[0]?.tag) { const unreleased = { ...data[0], tag: 'UNRELEASED' }; filteredData.unshift(unreleased); } break; case FILTER_BY.DAY: filteredData = []; _.forEachRight(data, (sample: PerfSample, i: number, arr) => { const prevSample = arr[i - 1]; if (!prevSample) { filteredData.push(sample); return; } const lastDate = new Date(prevSample.ts); const nextDate = new Date(sample.ts); if ( lastDate.getDate() !== nextDate.getDate() || lastDate.getMonth() !== nextDate.getMonth() || lastDate.getFullYear() !== nextDate.getFullYear() ) { filteredData.push(sample); } }); break; case FILTER_BY.MONTH: filteredData = []; _.forEachRight(data, (sample: PerfSample, i: number, arr) => { const prevSample = arr[i - 1]; if (!prevSample) { filteredData.push(sample); return; } const lastDate = new Date(prevSample.ts); const nextDate = new Date(sample.ts); if (lastDate.getMonth() !== nextDate.getMonth() || lastDate.getFullYear() !== nextDate.getFullYear()) { filteredData.push(sample); } }); break; default: break; } const handleFilterChange = React.useCallback((e, props) => { setFilterBy(props.value); }, []); return (
{process.env.NODE_ENV !== 'production' && ( )} {Filter && } {loading ? ( ) : error ? ( ) : ( )}
); }; export default ComponentChart;