ExperimentPanel.tsx 4.84 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, Pivot, PivotItem } from '@fluentui/react';
4
5
import { DRAWEROPTION } from '../../static/const';
import { EXPERIMENT, TRIALS } from '../../static/datamodel';
v-liguo's avatar
v-liguo committed
6
7
8
9
10
import MonacoEditor from 'react-monaco-editor';
import '../../static/style/logDrawer.scss';

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

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

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

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

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

Lijiao's avatar
Lijiao committed
32
    getExperimentContent = (): void => {
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
        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
48
                }
49
50
51
52
53
54
55
56
57
            });
        });
        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
58

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

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

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

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

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

Lijiao's avatar
Lijiao committed
88
    render(): React.ReactNode {
89
        const { closeExpDrawer } = this.props;
Lijiao's avatar
Lijiao committed
90
        const { experiment, expDrawerHeight } = this.state;
v-liguo's avatar
v-liguo committed
91
        return (
92
            <Stack className='logDrawer'>
93
                <Panel
94
                    isOpen={true}
95
                    hasCloseButton={false}
96
97
                    isLightDismiss={true}
                    onLightDismissClick={closeExpDrawer}
98
                    styles={{ root: { height: expDrawerHeight, paddingTop: 15 } }}
v-liguo's avatar
v-liguo committed
99
                >
100
101
102
                    <Pivot style={{ minHeight: 190 }} className='log-tab-body'>
                        <PivotItem headerText='Experiment parameters'>
                            <div className='just-for-log'>
103
                                <MonacoEditor
104
                                    width='100%'
105
106
                                    // 92 + marginTop[16]
                                    height={expDrawerHeight - 108}
107
                                    language='json'
108
109
110
111
                                    value={experiment}
                                    options={DRAWEROPTION}
                                />
                            </div>
112
113
114
                            <Stack horizontal className='buttons'>
                                <StackItem grow={50} className='download'>
                                    <PrimaryButton text='Download' onClick={this.downExperimentParameters} />
115
                                </StackItem>
116
117
                                <StackItem grow={50} className='close'>
                                    <DefaultButton text='Close' onClick={closeExpDrawer} />
118
119
120
121
122
123
                                </StackItem>
                            </Stack>
                        </PivotItem>
                    </Pivot>
                </Panel>
            </Stack>
v-liguo's avatar
v-liguo committed
124
125
126
127
128
        );
    }
}

export default ExperimentDrawer;