App.tsx 11.4 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
import * as React from 'react';
Lijiaoa's avatar
Lijiaoa committed
2
import { Outlet } from 'react-router-dom';
3
import { Stack } from '@fluentui/react';
Lijiaoa's avatar
Lijiaoa committed
4
5
6
7
8
9
10
11
12
13
import { SlideNavBtns } from '@components/nav/slideNav/SlideNavBtns';
import { EXPERIMENT, TRIALS } from '@static/datamodel';
import NavCon from '@components/nav/Nav';
import MessageInfo from '@components/common/MessageInfo';
import { COLUMN } from '@static/const';
import { isManagerExperimentPage } from '@static/function';
import '@style/App.scss';
import '@style/common/common.scss';
import '@style/experiment/trialdetail/trialsDetail.scss';

14
15
16
17
const echarts = require('echarts/lib/echarts');
echarts.registerTheme('nni_theme', {
    color: '#3c8dbc'
});
Deshui Yu's avatar
Deshui Yu committed
18

19
20
21
22
23
24
25
export const AppContext = React.createContext({
    interval: 10, // sendons
    columnList: COLUMN,
    experimentUpdateBroadcast: 0,
    trialsUpdateBroadcast: 0,
    metricGraphMode: 'max',
    bestTrialEntries: '10',
26
    maxDurationUnit: 'm',
27
    expandRowIDs: new Set(['']),
28
29
30
31
32
33
34
35
36
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    changeColumn: (_val: string[]): void => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    changeMetricGraphMode: (_val: 'max' | 'min'): void => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    changeMaxDurationUnit: (_val: string): void => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    changeEntries: (_val: string): void => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
37
38
    updateOverviewPage: () => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
39
40
    updateDetailPage: () => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
Lijiaoa's avatar
Lijiaoa committed
41
42
43
44
45
46
47
    changeExpandRowIDs: (_val: string, _type?: string): void => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    startTimer: () => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    closeTimer: (): void => {},
    // eslint-disable-next-line @typescript-eslint/no-empty-function
    refreshDetailTable: (): void => {}
48
49
});

Lijiaoa's avatar
Lijiaoa committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
interface AppState {
    interval: number;
    columnList: string[];
    experimentUpdateBroadcast: number;
    trialsUpdateBroadcast: number;
    maxDurationUnit: string;
    metricGraphMode: 'max' | 'min'; // tuner's optimize_mode filed
    isillegalFinal: boolean;
    expWarningMessage: string;
    bestTrialEntries: string; // for overview page: best trial entreis
    expandRowIDs: Set<string>;
    timerIdList: number[];
}

64
class App extends React.Component<{}, AppState> {
Lijiaoa's avatar
Lijiaoa committed
65
66
    private timerId = 0;

67
68
69
70
71
72
73
    constructor(props: {}) {
        super(props);
        this.state = {
            interval: 10, // sendons
            columnList: COLUMN,
            experimentUpdateBroadcast: 0,
            trialsUpdateBroadcast: 0,
74
            metricGraphMode: 'max',
75
            maxDurationUnit: 'm',
76
            isillegalFinal: false,
Lijiaoa's avatar
Lijiaoa committed
77
            expWarningMessage: '',
Lijiaoa's avatar
Lijiaoa committed
78
            bestTrialEntries: '10',
Lijiaoa's avatar
Lijiaoa committed
79
80
            expandRowIDs: new Set(),
            timerIdList: []
81
        };
82
83
    }

Lijiao's avatar
Lijiao committed
84
    async componentDidMount(): Promise<void> {
85
        await Promise.all([EXPERIMENT.init(), TRIALS.init()]);
86
87
        this.setState(state => ({
            experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1,
Lijiaoa's avatar
Lijiaoa committed
88
            trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1,
89
            metricGraphMode: EXPERIMENT.optimizeMode === 'minimize' ? 'min' : 'max'
Lijiaoa's avatar
Lijiaoa committed
90
        }));
91

Lijiaoa's avatar
Lijiaoa committed
92
        this.startTimer();
Lijiaoa's avatar
Lijiaoa committed
93
94
    }

95
    render(): React.ReactNode {
96
97
98
99
100
101
102
103
        const {
            interval,
            columnList,
            experimentUpdateBroadcast,
            trialsUpdateBroadcast,
            metricGraphMode,
            isillegalFinal,
            expWarningMessage,
104
            bestTrialEntries,
105
106
            maxDurationUnit,
            expandRowIDs
107
        } = this.state;
108
        if (experimentUpdateBroadcast === 0 || trialsUpdateBroadcast === 0) {
Lijiaoa's avatar
Lijiaoa committed
109
            return null;
110
        }
Lijiaoa's avatar
Lijiaoa committed
111
112
113
114
115
116
117
118
        const errorList = [
            { errorWhere: TRIALS.jobListError(), errorMessage: TRIALS.getJobErrorMessage() },
            { errorWhere: EXPERIMENT.experimentError(), errorMessage: EXPERIMENT.getExperimentMessage() },
            { errorWhere: EXPERIMENT.statusError(), errorMessage: EXPERIMENT.getStatusMessage() },
            { errorWhere: TRIALS.MetricDataError(), errorMessage: TRIALS.getMetricDataErrorMessage() },
            { errorWhere: TRIALS.latestMetricDataError(), errorMessage: TRIALS.getLatestMetricDataErrorMessage() },
            { errorWhere: TRIALS.metricDataRangeError(), errorMessage: TRIALS.metricDataRangeErrorMessage() }
        ];
119

120
        return (
121
122
123
124
125
126
            <React.Fragment>
                {isManagerExperimentPage() ? null : (
                    <Stack className='nni' style={{ minHeight: window.innerHeight }}>
                        <div className='header'>
                            <div className='headerCon'>
                                <NavCon changeInterval={this.changeInterval} refreshFunction={this.lastRefresh} />
127
                            </div>
128
129
130
                        </div>
                        <Stack className='contentBox'>
                            <Stack className='content'>
Lijiaoa's avatar
Lijiaoa committed
131
                                {/* search space & config & dispatcher, nnimanagerlog*/}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
                                <SlideNavBtns />
                                {/* if api has error field, show error message */}
                                {errorList.map(
                                    (item, key) =>
                                        item.errorWhere && (
                                            <div key={key} className='warning'>
                                                <MessageInfo info={item.errorMessage} typeInfo='error' />
                                            </div>
                                        )
                                )}
                                {isillegalFinal && (
                                    <div className='warning'>
                                        <MessageInfo info={expWarningMessage} typeInfo='warning' />
                                    </div>
                                )}
Lijiaoa's avatar
Lijiaoa committed
147
                                {/* <AppContext.Provider */}
148
149
150
151
152
153
154
155
156
                                <AppContext.Provider
                                    value={{
                                        interval,
                                        columnList,
                                        changeColumn: this.changeColumn,
                                        experimentUpdateBroadcast,
                                        trialsUpdateBroadcast,
                                        metricGraphMode,
                                        maxDurationUnit,
Lijiaoa's avatar
Lijiaoa committed
157
                                        bestTrialEntries,
158
159
160
                                        changeMaxDurationUnit: this.changeMaxDurationUnit,
                                        changeMetricGraphMode: this.changeMetricGraphMode,
                                        changeEntries: this.changeEntries,
161
                                        expandRowIDs,
Lijiaoa's avatar
Lijiaoa committed
162
163
164
165
166
167
                                        changeExpandRowIDs: this.changeExpandRowIDs,
                                        updateOverviewPage: this.updateOverviewPage,
                                        updateDetailPage: this.updateDetailPage, // update current record without fetch api
                                        refreshDetailTable: this.refreshDetailTable, // update record with fetch api
                                        startTimer: this.startTimer,
                                        closeTimer: this.closeTimer
168
169
                                    }}
                                >
Lijiaoa's avatar
Lijiaoa committed
170
                                    <Outlet />
171
172
173
174
                                    {this.props.children}
                                </AppContext.Provider>
                            </Stack>
                        </Stack>
175
                    </Stack>
176
177
                )}
            </React.Fragment>
178
179
180
        );
    }

Lijiao's avatar
Lijiao committed
181
    private refresh = async (): Promise<void> => {
Lijiaoa's avatar
Lijiaoa committed
182
183
184
185
186
187
        const [experimentUpdated, trialsUpdated] = await Promise.all([EXPERIMENT.update(), TRIALS.update()]);
        if (experimentUpdated) {
            this.setState(state => ({ experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1 }));
        }
        if (trialsUpdated) {
            this.setState(state => ({ trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1 }));
188
189
        }

Lijiaoa's avatar
Lijiaoa committed
190
        // experiment status and /trial-jobs api's status could decide website update
191
        if (['DONE', 'ERROR', 'STOPPED', 'VIEWED'].includes(EXPERIMENT.status) || TRIALS.jobListError()) {
192
            // experiment finished, refresh once more to ensure consistency
Lijiaoa's avatar
Lijiaoa committed
193
194
            this.setState(() => ({ interval: 0 }));
            this.closeTimer();
195
            return;
196
        }
197

Lijiaoa's avatar
Lijiaoa committed
198
        this.startTimer();
199
    };
200

Lijiaoa's avatar
Lijiaoa committed
201
    public lastRefresh = async (): Promise<void> => {
202
203
        await EXPERIMENT.update();
        await TRIALS.update(true);
204
205
206
207
        this.setState(state => ({
            experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1,
            trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1
        }));
Lijiaoa's avatar
Lijiaoa committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
    };

    public changeInterval = (interval: number): void => {
        this.setState(() => ({ interval: interval })); // reset interval val
        this.closeTimer(); // close page auto refresh
        if (interval !== 0) {
            this.refresh();
        }
    };

    public changeColumn = (columnList: string[]): void => {
        this.setState({ columnList: columnList });
    };

    public changeExpandRowIDs = (id: string, type?: string): void => {
        const currentExpandRowIDs = this.state.expandRowIDs;

        if (!currentExpandRowIDs.has(id)) {
            currentExpandRowIDs.add(id);
        } else {
            if (!(type !== undefined && type === 'chart')) {
                currentExpandRowIDs.delete(id);
            }
        }

        this.setState({ expandRowIDs: currentExpandRowIDs });
    };

    public changeMetricGraphMode = (val: 'max' | 'min'): void => {
        this.setState({ metricGraphMode: val });
    };

    // overview best trial module
    public changeEntries = (entries: string): void => {
        this.setState({ bestTrialEntries: entries });
    };

    // overview max duration unit
    public changeMaxDurationUnit = (unit: string): void => {
        this.setState({ maxDurationUnit: unit });
    };

    public updateOverviewPage = (): void => {
        this.setState(state => ({
            experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1
        }));
    };

    public updateDetailPage = async (): Promise<void> => {
        this.setState(state => ({
            trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1
        }));
    };

    // fetch api to update table record data
    public refreshDetailTable = async (): Promise<void> => {
        await TRIALS.update(true);
        this.setState(state => ({
            trialsUpdateBroadcast: state.trialsUpdateBroadcast + 1
        }));
    };

    // start to refresh page automatically
    public startTimer = (): void => {
        this.timerId = window.setTimeout(this.refresh, this.state.interval * 1000);
        const storeTimerList = this.state.timerIdList;
        storeTimerList.push(this.timerId);
        this.setState(() => ({ timerIdList: storeTimerList }));
    };

    public closeTimer = (): void => {
        const { timerIdList } = this.state;
        timerIdList.forEach(item => {
            window.clearTimeout(item);
        });
    };
Deshui Yu's avatar
Deshui Yu committed
284
285
286
}

export default App;