TrialConfigPanel.tsx 4.91 KB
Newer Older
1
import * as React from 'react';
2
3
import { Stack, Panel, PrimaryButton } from '@fluentui/react';
import { EXPERIMENT } from '../../static/datamodel';
4
import MonacoEditor from 'react-monaco-editor';
5
6
7
8
import { MONACO } from '../../static/const';
import { AppContext } from '../../App';
import { convertDuration, convertTimeAsUnit, caclMonacoEditorHeight } from '../../static/function';
import { prettyStringify } from '../../static/json_util';
9
import lodash from 'lodash';
10
import '../../static/style/logDrawer.scss';
11
12
13

interface LogDrawerProps {
    hideConfigPanel: () => void;
14
    panelName: string;
15
16
17
18
}

interface LogDrawerState {
    panelInnerHeight: number;
19
    innerWidth: number;
20
21
}

22
23
24
25
26
27
/**
 * search space
 * config
 * model
 */

28
29
30
31
32
class TrialConfigPanel extends React.Component<LogDrawerProps, LogDrawerState> {
    constructor(props: LogDrawerProps) {
        super(props);

        this.state = {
33
34
            panelInnerHeight: window.innerHeight,
            innerWidth: window.innerWidth
35
36
37
        };
    }

38
39
40
41
    // use arrow function for change window size met error: this.setState is not a function
    setLogDrawerHeight = (): void => {
        this.setState(() => ({ panelInnerHeight: window.innerHeight, innerWidth: window.innerWidth }));
    };
42
43
44
45
46
47
48
49
50
51

    async componentDidMount(): Promise<void> {
        window.addEventListener('resize', this.setLogDrawerHeight);
    }

    componentWillUnmount(): void {
        window.removeEventListener('resize', this.setLogDrawerHeight);
    }

    render(): React.ReactNode {
52
        const { hideConfigPanel, panelName } = this.props;
53
        const { panelInnerHeight, innerWidth } = this.state;
54
        const monacoEditorHeight = caclMonacoEditorHeight(panelInnerHeight);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
        const blacklist = [
            'id',
            'logDir',
            'startTime',
            'endTime',
            'experimentName',
            'searchSpace',
            'trainingServicePlatform'
        ];
        const filter = (key: string, val: any): any => {
            return blacklist.includes(key) ? undefined : val;
        };
        const profile = lodash.cloneDeep(EXPERIMENT.profile);
        profile.execDuration = convertDuration(profile.execDuration);
69
70
71

        const prettyWidth = innerWidth > 1400 ? 100 : 60;

72
        return (
73
74
75
            <AppContext.Consumer>
                {(value): React.ReactNode => {
                    const unit = value.maxDurationUnit;
liuzhe-lz's avatar
liuzhe-lz committed
76
                    profile.params.maxExperimentDuration = `${convertTimeAsUnit(
77
                        unit,
liuzhe-lz's avatar
liuzhe-lz committed
78
                        profile.params.maxExperimentDuration
79
80
81
82
83
84
85
86
87
88
89
                    )}${unit}`;
                    const showProfile = JSON.stringify(profile, filter, 2);
                    return (
                        <Stack>
                            <Panel
                                isOpen={true}
                                hasCloseButton={false}
                                isFooterAtBottom={true}
                                isLightDismiss={true}
                                onLightDismissClick={hideConfigPanel}
                            >
90
91
92
93
                                <div className='panel'>
                                    {panelName === 'search space' ? (
                                        <div>
                                            <div className='panelName'>Search space</div>
94
95
96
97
98
99
100
                                            <MonacoEditor
                                                height={monacoEditorHeight}
                                                language='json'
                                                theme='vs-light'
                                                value={prettyStringify(EXPERIMENT.searchSpace, prettyWidth, 2)}
                                                options={MONACO}
                                            />
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                                        </div>
                                    ) : (
                                        <div className='profile'>
                                            <div className='panelName'>Config</div>
                                            <MonacoEditor
                                                width='100%'
                                                height={monacoEditorHeight}
                                                language='json'
                                                theme='vs-light'
                                                value={showProfile}
                                                options={MONACO}
                                            />
                                        </div>
                                    )}
                                    <PrimaryButton text='Close' className='configClose' onClick={hideConfigPanel} />
116
                                </div>
117
118
119
120
121
                            </Panel>
                        </Stack>
                    );
                }}
            </AppContext.Consumer>
122
123
124
125
126
        );
    }
}

export default TrialConfigPanel;