scalars.tsx 4.8 KB
Newer Older
1
import ChartPage, {WithChart} from '~/components/ChartPage';
2
import {NextI18NextPage, useTranslation} from '~/utils/i18n';
P
Peter Pan 已提交
3
import React, {useCallback, useMemo, useState} from 'react';
4 5
import {sortingMethodMap, xAxisMap} from '~/resource/scalars';

P
Peter Pan 已提交
6
import {AsideSection} from '~/components/Aside';
7 8 9
import Checkbox from '~/components/Checkbox';
import Content from '~/components/Content';
import Field from '~/components/Field';
P
Peter Pan 已提交
10
import Preloader from '~/components/Preloader';
11 12 13
import RadioButton from '~/components/RadioButton';
import RadioGroup from '~/components/RadioGroup';
import RunAside from '~/components/RunAside';
14
import ScalarChart from '~/components/ScalarsPage/ScalarChart';
15 16
import Select from '~/components/Select';
import Slider from '~/components/Slider';
17
import {Tag} from '~/types';
18
import Title from '~/components/Title';
19 20
import {rem} from '~/utils/style';
import styled from 'styled-components';
21
import useTagFilter from '~/hooks/useTagFilter';
22 23

type XAxis = keyof typeof xAxisMap;
24
const xAxisValues = ['step', 'relative', 'wall'] as const;
25
type TooltipSorting = keyof typeof sortingMethodMap;
26 27 28 29 30 31 32 33 34 35 36 37 38
const toolTipSortingValues = ['default', 'descending', 'ascending', 'nearest'] as const;

const TooltipSortingDiv = styled.div`
    margin-top: ${rem(20)};
    display: flex;
    align-items: center;

    > :last-child {
        margin-left: ${rem(20)};
        flex-shrink: 1;
        flex-grow: 1;
    }
`;
39

40
const Scalars: NextI18NextPage = () => {
41 42
    const {t} = useTranslation(['scalars', 'common']);

P
Peter Pan 已提交
43 44
    const [running, setRunning] = useState(true);

45
    const {runs, tags, selectedRuns, onChangeRuns, loadingRuns, loadingTags} = useTagFilter('scalars', running);
46 47 48

    const [smoothing, setSmoothing] = useState(0.6);

49
    const [xAxis, setXAxis] = useState<XAxis>(xAxisValues[0]);
50

51
    const [tooltipSorting, setTooltipSorting] = useState<TooltipSorting>(toolTipSortingValues[0]);
52 53 54

    const [ignoreOutliers, setIgnoreOutliers] = useState(false);

P
Peter Pan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    const aside = useMemo(
        () => (
            <RunAside
                runs={runs}
                selectedRuns={selectedRuns}
                onChangeRuns={onChangeRuns}
                running={running}
                onToggleRunning={setRunning}
            >
                <AsideSection>
                    <Checkbox value={ignoreOutliers} onChange={setIgnoreOutliers}>
                        {t('scalars:ignore-outliers')}
                    </Checkbox>
                    <TooltipSortingDiv>
                        <span>{t('scalars:tooltip-sorting')}</span>
                        <Select
                            list={toolTipSortingValues.map(value => ({
                                label: t(`tooltip-sorting-value.${value}`),
                                value
                            }))}
                            value={tooltipSorting}
                            onChange={setTooltipSorting}
                        />
                    </TooltipSortingDiv>
                </AsideSection>
                <AsideSection>
                    <Field label={t('scalars:smoothing')}>
                        <Slider min={0} max={0.99} step={0.01} value={smoothing} onChangeComplete={setSmoothing} />
                    </Field>
                </AsideSection>
                <AsideSection>
                    <Field label={t('scalars:x-axis')}>
                        <RadioGroup value={xAxis} onChange={setXAxis}>
                            {xAxisValues.map(value => (
                                <RadioButton key={value} value={value}>
                                    {t(`x-axis-value.${value}`)}
                                </RadioButton>
                            ))}
                        </RadioGroup>
                    </Field>
                </AsideSection>
            </RunAside>
        ),
        [t, ignoreOutliers, onChangeRuns, running, runs, selectedRuns, smoothing, tooltipSorting, xAxis]
99 100
    );

101 102
    const withChart = useCallback<WithChart<Tag>>(
        ({label, runs, ...args}) => (
103
            <ScalarChart
104 105 106
                runs={runs}
                tag={label}
                {...args}
107 108 109 110 111 112 113 114 115 116 117 118
                smoothing={smoothing}
                xAxis={xAxis}
                sortingMethod={tooltipSorting}
                outlier={ignoreOutliers}
                running={running}
            />
        ),
        [smoothing, xAxis, tooltipSorting, ignoreOutliers, running]
    );

    return (
        <>
P
Peter Pan 已提交
119 120
            <Preloader url="/runs" />
            <Preloader url="/scalars/tags" />
121
            <Title>{t('common:scalars')}</Title>
P
Peter Pan 已提交
122
            <Content aside={aside} loading={loadingRuns}>
123
                <ChartPage items={tags} withChart={withChart} loading={loadingRuns || loadingTags} />
124 125 126 127 128
            </Content>
        </>
    );
};

129 130 131 132
Scalars.getInitialProps = () => ({
    namespacesRequired: ['scalars', 'common']
});

133
export default Scalars;