OpenRow.tsx 8.94 KB
Newer Older
Lijiao's avatar
Lijiao committed
1
import * as React from 'react';
2
import * as copy from 'copy-to-clipboard';
3
import { Stack, PrimaryButton, Pivot, PivotItem, DefaultButton } from '@fluentui/react';
4
import JSONTree from 'react-json-tree';
Lijiaoa's avatar
Lijiaoa committed
5
6
7
8
9
10
11
12
13
14
import { Trial } from '@model/trial';
import { MANAGER_IP, RETIARIIPARAMETERS } from '@static/const';
import { EXPERIMENT, TRIALS } from '@static/datamodel';
import { reformatRetiariiParameter } from '@static/function';
import PaiTrialLog from './PaiTrialLog';
import TrialLog from './TrialLog';
import MessageInfo from './MessageInfo';
import PanelMonacoEditor from './PanelMonacoEditor';
import '@style/experiment/overview/overview.scss';
import '@style/openRow.scss';
Lijiao's avatar
Lijiao committed
15

liuzhe-lz's avatar
liuzhe-lz committed
16
17
18
19
20
/**
 *  netron URL must be synchronized with ts/nni_manager/rest_server/index.ts`.
 *  Remember to update it if the value is changed or this file is moved.
 **/

Lijiao's avatar
Lijiao committed
21
interface OpenRowProps {
22
    trialId: string;
Lijiao's avatar
Lijiao committed
23
24
}

Lijiao's avatar
Lijiao committed
25
interface OpenRowState {
26
27
28
    typeInfo: string;
    info: string;
    isHidenInfo: boolean;
29
    showRetiaParamPanel: boolean;
Lijiao's avatar
Lijiao committed
30
31
32
}

class OpenRow extends React.Component<OpenRowProps, OpenRowState> {
Lijiao's avatar
Lijiao committed
33
34
    constructor(props: OpenRowProps) {
        super(props);
Lijiao's avatar
Lijiao committed
35
        this.state = {
36
37
            typeInfo: '',
            info: '',
38
39
            isHidenInfo: true,
            showRetiaParamPanel: false
Lijiao's avatar
Lijiao committed
40
        };
41
42
    }

43
44
    hideMessageInfo = (): void => {
        this.setState(() => ({ isHidenInfo: true }));
45
    };
Lijiao's avatar
Lijiao committed
46

47
48
49
50
51
52
53
54
    hideRetiaParam = (): void => {
        this.setState(() => ({ showRetiaParamPanel: false }));
    };

    isshowRetiaParamPanel = (): void => {
        this.setState(() => ({ showRetiaParamPanel: true }));
    };

55
56
57
    /**
     * info: message content
     * typeInfo: message type: success | error...
58
     * continuousTime: show time, 2000ms
59
60
61
62
     */
    getCopyStatus = (info: string, typeInfo: string): void => {
        this.setState(() => ({ info, typeInfo, isHidenInfo: false }));
        setTimeout(this.hideMessageInfo, 2000);
63
    };
Lijiao's avatar
Lijiao committed
64

65
66
    copyParams = (trial: Trial): void => {
        // get copy parameters
67
        const params = JSON.stringify(reformatRetiariiParameter(trial.description.parameters as any), null, 4);
68
69
        if (copy.default(params)) {
            this.getCopyStatus('Success copy parameters to clipboard in form of python dict !', 'success');
70
        } else {
71
            this.getCopyStatus('Failed !', 'error');
72
        }
73
    };
74

Yuge Zhang's avatar
Yuge Zhang committed
75
76
77
78
79
80
81
    openTrialLog = (filename: string): void => {
        window.open(`${MANAGER_IP}/trial-file/${this.props.trialId}/${filename}`);
    };

    openModelOnnx = (): void => {
        // TODO: netron might need prefix.
        window.open(`/netron/index.html?url=${MANAGER_IP}/trial-file/${this.props.trialId}/model.onnx`);
82
    };
83

Lijiao's avatar
Lijiao committed
84
    render(): React.ReactNode {
85
        const { isHidenInfo, typeInfo, info, showRetiaParamPanel } = this.state;
86
87
        const trialId = this.props.trialId;
        const trial = TRIALS.getTrial(trialId);
88
        const logPathRow = trial.info.logPath || "This trial's log path is not available.";
89
90
        const originParameters = trial.description.parameters;
        const hasVisualHyperParams = RETIARIIPARAMETERS in originParameters;
Lijiao's avatar
Lijiao committed
91
        return (
92
93
            <Stack className='openRow'>
                <Stack className='openRowContent'>
94
                    <Pivot>
95
96
97
98
99
100
101
102
                        <PivotItem headerText='Parameters' key='1' itemIcon='TestParameter'>
                            {trial.info.hyperParameters !== undefined ? (
                                <Stack id='description'>
                                    <Stack className='bgHyper'>
                                        <JSONTree
                                            hideRoot={true}
                                            shouldExpandNode={(): boolean => true} // default expandNode
                                            getItemString={(): null => null} // remove the {} items
103
                                            data={reformatRetiariiParameter(originParameters as any)}
104
                                        />
105
                                    </Stack>
106
107
108
109
110
111
                                    <Stack horizontal className='copy'>
                                        <PrimaryButton
                                            onClick={this.copyParams.bind(this, trial)}
                                            text='Copy as json'
                                            styles={{ root: { width: 128, marginRight: 10 } }}
                                        />
112
113
114
115
116
117
                                        {hasVisualHyperParams && (
                                            <DefaultButton
                                                onClick={this.isshowRetiaParamPanel}
                                                text='Original parameters'
                                            />
                                        )}
118
119
                                        {/* copy success | failed message info */}
                                        {!isHidenInfo && <MessageInfo typeInfo={typeInfo} info={info} />}
120
121
122
123
124
125
126
                                        {showRetiaParamPanel && (
                                            <PanelMonacoEditor
                                                hideConfigPanel={this.hideRetiaParam}
                                                panelName='Retiarii parameters'
                                                panelContent={JSON.stringify(originParameters, null, 2)}
                                            />
                                        )}
127
                                    </Stack>
128
129
130
131
132
133
134
                                </Stack>
                            ) : (
                                <Stack className='logpath'>
                                    <span className='logName'>Error: </span>
                                    <span className='error'>{`This trial's parameters are not available.'`}</span>
                                </Stack>
                            )}
135
                        </PivotItem>
136
                        <PivotItem headerText='Log' key='2' itemIcon='M365InvoicingLogo'>
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
                            {
                                // FIXME: this should not be handled in web UI side
                                EXPERIMENT.trainingServicePlatform !== 'local' ? (
                                    <PaiTrialLog
                                        logStr={logPathRow}
                                        id={trialId}
                                        logCollection={EXPERIMENT.logCollectionEnabled}
                                    />
                                ) : (
                                    <div>
                                        <TrialLog logStr={logPathRow} id={trialId} />
                                        {/* view each trial log in drawer*/}
                                        <div id='trialog'>
                                            <div className='copy' style={{ marginTop: 15 }}>
                                                <PrimaryButton
Yuge Zhang's avatar
Yuge Zhang committed
152
                                                    onClick={this.openTrialLog.bind(this, 'trial.log')}
153
154
155
                                                    text='View trial log'
                                                />
                                                <PrimaryButton
Yuge Zhang's avatar
Yuge Zhang committed
156
                                                    onClick={this.openTrialLog.bind(this, 'stderr')}
157
158
159
160
                                                    text='View trial error'
                                                    styles={{ root: { marginLeft: 15 } }}
                                                />
                                                <PrimaryButton
Yuge Zhang's avatar
Yuge Zhang committed
161
                                                    onClick={this.openTrialLog.bind(this, 'stdout')}
162
163
164
165
                                                    text='View trial stdout'
                                                    styles={{ root: { marginLeft: 15 } }}
                                                />
                                            </div>
166
167
                                        </div>
                                    </div>
168
169
                                )
                            }
170
                        </PivotItem>
Yuge Zhang's avatar
Yuge Zhang committed
171
172
173
174
175
176
177
178
179
180
181
182
                        {EXPERIMENT.metadata.tag.includes('retiarii') ? (
                            <PivotItem headerText='Visualization' key='3' itemIcon='FlowChart'>
                                <div id='visualization'>
                                    <div id='visualizationText'>Visualize models with 3rd-party tools.</div>
                                    <PrimaryButton
                                        onClick={this.openModelOnnx.bind(this)}
                                        text='Netron'
                                        styles={{ root: { marginLeft: 15 } }}
                                    />
                                </div>
                            </PivotItem>
                        ) : null}
183
                    </Pivot>
184
                </Stack>
185
            </Stack>
Lijiao's avatar
Lijiao committed
186
187
188
189
        );
    }
}

190
export default OpenRow;