"vscode:/vscode.git/clone" did not exist on "d99697e44c22393a9bbe936f9dbdf17c9ed7c6a1"
ExperimentSummaryPanel.tsx 4.35 KB
Newer Older
v-liguo's avatar
v-liguo committed
1
2
import * as React from 'react';
import { downFile } from '../../static/function';
3
import { Stack, PrimaryButton, DefaultButton, Panel, StackItem } from '@fluentui/react';
4
5
import { DRAWEROPTION } from '../../static/const';
import { EXPERIMENT, TRIALS } from '../../static/datamodel';
6
import { caclMonacoEditorHeight } from '../../static/function';
v-liguo's avatar
v-liguo committed
7
8
9
10
11
import MonacoEditor from 'react-monaco-editor';
import '../../static/style/logDrawer.scss';

interface ExpDrawerProps {
    closeExpDrawer: () => void;
12
    experimentProfile: object;
v-liguo's avatar
v-liguo committed
13
14
15
16
}

interface ExpDrawerState {
    experiment: string;
Lijiao's avatar
Lijiao committed
17
    expDrawerHeight: number;
v-liguo's avatar
v-liguo committed
18
19
}

20
class ExperimentSummaryPanel extends React.Component<ExpDrawerProps, ExpDrawerState> {
21
22
23
    public _isExperimentMount!: boolean;
    private refreshId!: number | undefined;

v-liguo's avatar
v-liguo committed
24
25
26
27
    constructor(props: ExpDrawerProps) {
        super(props);

        this.state = {
Lijiao's avatar
Lijiao committed
28
            experiment: '',
29
            expDrawerHeight: window.innerHeight
v-liguo's avatar
v-liguo committed
30
31
32
        };
    }

Lijiao's avatar
Lijiao committed
33
    getExperimentContent = (): void => {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        const experimentData = JSON.parse(JSON.stringify(this.props.experimentProfile));
        if (experimentData.params.searchSpace) {
            experimentData.params.searchSpace = JSON.parse(experimentData.params.searchSpace);
        }
        const trialMessagesArr = TRIALS.getTrialJobList();
        const interResultList = TRIALS.getMetricsList();
        Object.keys(trialMessagesArr).map(item => {
            // not deal with trial's hyperParameters
            const trialId = trialMessagesArr[item].jobId;
            // add intermediate result message
            trialMessagesArr[item].intermediate = [];
            Object.keys(interResultList).map(key => {
                const interId = interResultList[key].trialJobId;
                if (trialId === interId) {
                    trialMessagesArr[item].intermediate.push(interResultList[key]);
v-liguo's avatar
v-liguo committed
49
                }
50
51
52
53
54
55
56
57
58
            });
        });
        const result = {
            experimentParameters: experimentData,
            trialMessage: trialMessagesArr
        };
        if (this._isExperimentMount === true) {
            this.setState({ experiment: JSON.stringify(result, null, 4) });
        }
v-liguo's avatar
v-liguo committed
59

60
        if (['DONE', 'ERROR', 'STOPPED'].includes(EXPERIMENT.status)) {
61
            if (this.refreshId !== null || this.refreshId !== undefined) {
62
63
                window.clearInterval(this.refreshId);
            }
64
65
66
        }
    };

Lijiao's avatar
Lijiao committed
67
    downExperimentParameters = (): void => {
v-liguo's avatar
v-liguo committed
68
69
        const { experiment } = this.state;
        downFile(experiment, 'experiment.json');
70
    };
v-liguo's avatar
v-liguo committed
71

Lijiao's avatar
Lijiao committed
72
    onWindowResize = (): void => {
73
        this.setState(() => ({ expDrawerHeight: window.innerHeight }));
74
    };
Lijiao's avatar
Lijiao committed
75

Lijiao's avatar
Lijiao committed
76
    componentDidMount(): void {
77
        this._isExperimentMount = true;
v-liguo's avatar
v-liguo committed
78
        this.getExperimentContent();
79
        this.refreshId = window.setInterval(this.getExperimentContent, 10000);
Lijiao's avatar
Lijiao committed
80
        window.addEventListener('resize', this.onWindowResize);
v-liguo's avatar
v-liguo committed
81
82
    }

Lijiao's avatar
Lijiao committed
83
    componentWillUnmount(): void {
84
85
        this._isExperimentMount = false;
        window.clearTimeout(this.refreshId);
Lijiao's avatar
Lijiao committed
86
        window.removeEventListener('resize', this.onWindowResize);
v-liguo's avatar
v-liguo committed
87
88
    }

Lijiao's avatar
Lijiao committed
89
    render(): React.ReactNode {
90
        const { closeExpDrawer } = this.props;
Lijiao's avatar
Lijiao committed
91
        const { experiment, expDrawerHeight } = this.state;
92
93
        const monacoEditorHeight = caclMonacoEditorHeight(expDrawerHeight);

v-liguo's avatar
v-liguo committed
94
        return (
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
            <Panel isOpen={true} hasCloseButton={false} isLightDismiss={true} onLightDismissClick={closeExpDrawer}>
                <div className='panel'>
                    <div className='panelName'>Experiment summary</div>
                    <MonacoEditor
                        width='100%'
                        height={monacoEditorHeight}
                        language='json'
                        value={experiment}
                        options={DRAWEROPTION}
                    />
                    <Stack horizontal className='buttons'>
                        <StackItem grow={50} className='download'>
                            <PrimaryButton text='Download' onClick={this.downExperimentParameters} />
                        </StackItem>
                        <StackItem grow={50} className='close'>
                            <DefaultButton text='Close' onClick={closeExpDrawer} />
                        </StackItem>
                    </Stack>
                </div>
            </Panel>
v-liguo's avatar
v-liguo committed
115
116
117
118
        );
    }
}

119
export default ExperimentSummaryPanel;