pr-curve.tsx 5.4 KB
Newer Older
P
Peter Pan 已提交
1
import ChartPage, {WithChart} from '~/components/ChartPage';
2 3
import React, {FunctionComponent, useCallback, useEffect, useMemo, useState} from 'react';
import type {Run, StepInfo, Tag} from '~/resource/pr-curve';
P
Peter Pan 已提交
4 5 6 7 8 9 10 11 12 13
import {borderColor, rem} from '~/utils/style';

import {AsideSection} from '~/components/Aside';
import Content from '~/components/Content';
import Error from '~/components/Error';
import Field from '~/components/Field';
import PRCurveChart from '~/components/PRCurvePage/PRCurveChart';
import RunAside from '~/components/RunAside';
import StepSlider from '~/components/PRCurvePage/StepSlider';
import TimeModeSelect from '~/components/TimeModeSelect';
14
import {TimeType} from '~/resource/pr-curve';
P
Peter Pan 已提交
15 16 17 18 19 20
import Title from '~/components/Title';
import {cycleFetcher} from '~/utils/fetch';
import queryString from 'query-string';
import styled from 'styled-components';
import {useRunningRequest} from '~/hooks/useRequest';
import useTagFilter from '~/hooks/useTagFilter';
21
import {useTranslation} from 'react-i18next';
P
Peter Pan 已提交
22 23 24 25 26 27

const StepSliderWrapper = styled.div`
    max-height: 30vh;
    overflow: auto;
    overflow-x: hidden;
    overflow-y: auto;
P
Peter Pan 已提交
28
    flex-shrink: 0;
P
Peter Pan 已提交
29 30 31 32 33 34 35 36 37 38 39

    > ${AsideSection}:last-child {
        padding-bottom: ${rem(20)};
        margin-bottom: 0;
    }

    + .run-section {
        border-top: 1px solid ${borderColor};
        margin-top: 0;
        padding-top: ${rem(20)};
    }
P
Peter Pan 已提交
40 41 42 43

    &:empty + .run-section {
        border-top: none;
    }
P
Peter Pan 已提交
44 45
`;

46
const PRCurve: FunctionComponent = () => {
P
Peter Pan 已提交
47 48 49 50
    const {t} = useTranslation(['pr-curve', 'common']);

    const [running, setRunning] = useState(true);

51
    const {runs, tags, runsInTags, selectedRuns, onChangeRuns, loading} = useTagFilter('pr-curve', running);
P
Peter Pan 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64

    const [indexes, setIndexes] = useState<Record<string, number>>({});
    const onChangeIndexes = useCallback(
        (run: string, index: number) =>
            setIndexes(indexes => ({
                ...indexes,
                [run]: index
            })),
        []
    );
    useEffect(
        () =>
            setIndexes(indexes =>
P
Peter Pan 已提交
65
                runsInTags.reduce<typeof indexes>((m, c) => {
P
Peter Pan 已提交
66 67 68 69 70 71
                    if (indexes[c.label] != null) {
                        m[c.label] = indexes[c.label];
                    }
                    return m;
                }, {})
            ),
P
Peter Pan 已提交
72
        [runsInTags]
P
Peter Pan 已提交
73 74 75
    );

    const {data: stepInfo} = useRunningRequest<StepInfo[]>(
P
Peter Pan 已提交
76
        runsInTags.map(run => `/pr-curve/steps?${queryString.stringify({run: run.label})}`),
P
Peter Pan 已提交
77 78 79 80 81
        !!running,
        (...urls) => cycleFetcher(urls)
    );
    const runWithInfo = useMemo<Run[]>(
        () =>
P
Peter Pan 已提交
82
            runsInTags.map((run, i) => ({
P
Peter Pan 已提交
83 84 85
                ...run,
                index: indexes[run.label] ?? (stepInfo?.[i].length ?? 1) - 1,
                steps: stepInfo?.[i].map(j => j[1]) ?? [],
P
Peter Pan 已提交
86 87
                wallTimes: stepInfo?.[i].map(j => Math.floor(j[0])) ?? [],
                relatives: stepInfo?.[i].map(j => j[0] - stepInfo[i][0][0]) ?? []
P
Peter Pan 已提交
88
            })),
P
Peter Pan 已提交
89
        [runsInTags, stepInfo, indexes]
P
Peter Pan 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    );

    const [timeType, setTimeType] = useState<TimeType>(TimeType.Step);

    const prCurveTags = useMemo<Tag[]>(
        () =>
            tags.map(tag => ({
                ...tag,
                runs: tag.runs.map(run => ({
                    ...run,
                    index: 0,
                    steps: [] as Run['steps'],
                    wallTimes: [] as Run['wallTimes'],
                    relatives: [] as Run['relatives'],
                    ...runWithInfo.find(r => r.label === run.label)
                }))
            })),
        [tags, runWithInfo]
    );

    const aside = useMemo(
        () =>
            runs.length ? (
                <RunAside
                    runs={runs}
                    selectedRuns={selectedRuns}
                    onChangeRuns={onChangeRuns}
                    running={running}
                    onToggleRunning={setRunning}
                >
                    <AsideSection>
                        <Field label={t('pr-curve:time-display-type')}>
                            <TimeModeSelect value={timeType} onChange={setTimeType} />
                        </Field>
                    </AsideSection>
                    <StepSliderWrapper>
                        {runWithInfo.map(run => (
                            <AsideSection key={run.label}>
                                <StepSlider
                                    run={run}
                                    type={timeType}
                                    onChange={index => onChangeIndexes(run.label, index)}
                                />
                            </AsideSection>
                        ))}
                    </StepSliderWrapper>
                </RunAside>
            ) : null,
        [t, onChangeRuns, running, runs, selectedRuns, timeType, runWithInfo, onChangeIndexes]
    );

    const withChart = useCallback<WithChart<Tag>>(
        ({label, runs, ...args}) => <PRCurveChart runs={runs} tag={label} {...args} running={running} />,
        [running]
    );

    return (
        <>
            <Title>{t('common:pr-curve')}</Title>
149 150
            <Content aside={aside} loading={loading}>
                {!loading && !runs.length ? (
P
Peter Pan 已提交
151 152
                    <Error />
                ) : (
153
                    <ChartPage items={prCurveTags} withChart={withChart} loading={loading} />
P
Peter Pan 已提交
154 155 156 157 158 159 160
                )}
            </Content>
        </>
    );
};

export default PRCurve;