TableList.tsx 12.4 KB
Newer Older
Lijiao's avatar
Lijiao committed
1
2
3
4
5
6
7
8
import * as React from 'react';
import axios from 'axios';
import JSONTree from 'react-json-tree';
import ReactEcharts from 'echarts-for-react';
import { Row, Table, Button, Popconfirm, Modal, message } from 'antd';
import { MANAGER_IP, trialJobStatus } from '../../static/const';
import { convertDuration } from '../../static/function';
import { TableObj, TrialJob } from '../../static/interface';
9
import LogPath from '../logPath/LogPath';
Lijiao's avatar
Lijiao committed
10
11
require('../../static/style/tableStatus.css');
require('../../static/style/logPath.scss');
12
require('../../static/style/search.scss');
Lijiao's avatar
Lijiao committed
13
14
15
16
17
18
19
20
21
22
23
require('../../static/style/table.scss');
require('../../static/style/button.scss');
const echarts = require('echarts/lib/echarts');
require('echarts/lib/chart/line');
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');
echarts.registerTheme('my_theme', {
    color: '#3c8dbc'
});

interface TableListProps {
24
    entries: number;
Lijiao's avatar
Lijiao committed
25
    tableSource: Array<TableObj>;
26
    searchResult: Array<TableObj>;
Lijiao's avatar
Lijiao committed
27
    updateList: Function;
28
    isHasSearch: boolean;
Lijiao's avatar
Lijiao committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
}

interface TableListState {
    intermediateOption: object;
    modalVisible: boolean;
}

class TableList extends React.Component<TableListProps, TableListState> {

    public _isMounted = false;
    constructor(props: TableListProps) {
        super(props);

        this.state = {
            intermediateOption: {},
            modalVisible: false
        };
    }

    showIntermediateModal = (id: string) => {

        axios(`${MANAGER_IP}/metric-data/${id}`, {
            method: 'GET'
        })
            .then(res => {
                if (res.status === 200) {
                    const intermediateArr: number[] = [];
                    Object.keys(res.data).map(item => {
                        intermediateArr.push(parseFloat(res.data[item].data));
                    });
                    const intermediate = this.intermediateGraphOption(intermediateArr, id);
                    if (this._isMounted) {
                        this.setState(() => ({
                            intermediateOption: intermediate
                        }));
                    }
                }
            });
        if (this._isMounted) {
            this.setState({
                modalVisible: true
            });
        }
    }

    hideIntermediateModal = () => {
        if (this._isMounted) {
            this.setState({
                modalVisible: false
            });
        }
    }

    intermediateGraphOption = (intermediateArr: number[], id: string) => {
        const sequence: number[] = [];
        const lengthInter = intermediateArr.length;
        for (let i = 1; i <= lengthInter; i++) {
            sequence.push(i);
        }
        return {
            title: {
                text: id,
                left: 'center',
                textStyle: {
                    fontSize: 16,
                    color: '#333',
                }
            },
            tooltip: {
                trigger: 'item'
            },
            xAxis: {
                name: 'Trial',
                data: sequence
            },
            yAxis: {
Lijiao's avatar
Lijiao committed
105
                name: 'Default Metric',
Lijiao's avatar
Lijiao committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
                type: 'value',
                data: intermediateArr
            },
            series: [{
                symbolSize: 6,
                type: 'scatter',
                data: intermediateArr
            }]
        };
    }

    // kill job
    killJob = (key: number, id: string, status: string) => {
        const { updateList } = this.props;
        axios(`${MANAGER_IP}/trial-jobs/${id}`, {
            method: 'DELETE',
            headers: {
                'Content-Type': 'application/json;charset=utf-8'
            }
        })
            .then(res => {
                if (res.status === 200) {
                    message.success('Cancel the job successfully');
                    // render the table
                    updateList();
                } else {
                    message.error('fail to cancel the job');
                }
            })
            .catch(error => {
                if (error.response.status === 500) {
137
138
139
140
141
                    if (error.response.data.error) {
                        message.error(error.response.data.error);
                    } else {
                        message.error('500 error, fail to cancel the job');
                    }
Lijiao's avatar
Lijiao committed
142
143
144
145
146
147
148
149
150
151
152
153
154
                }
            });
    }

    componentDidMount() {
        this._isMounted = true;
    }

    componentWillUnmount() {
        this._isMounted = false;
    }

    render() {
155

156
        const { entries, tableSource, searchResult, isHasSearch } = this.props;
Lijiao's avatar
Lijiao committed
157
158
159
160
161
162
163
164
165
        const { intermediateOption, modalVisible } = this.state;
        let bgColor = '';
        const trialJob: Array<TrialJob> = [];
        trialJobStatus.map(item => {
            trialJob.push({
                text: item,
                value: item
            });
        });
166

Lijiao's avatar
Lijiao committed
167
        const columns = [{
Lijiao's avatar
Lijiao committed
168
            title: 'Trial No.',
Lijiao's avatar
Lijiao committed
169
170
171
172
            dataIndex: 'sequenceId',
            key: 'sequenceId',
            width: 120,
            className: 'tableHead',
Lijiao's avatar
Lijiao committed
173
            sorter: (a: TableObj, b: TableObj) => (a.sequenceId as number) - (b.sequenceId as number)
Lijiao's avatar
Lijiao committed
174
175
176
177
        }, {
            title: 'Id',
            dataIndex: 'id',
            key: 'id',
Lijiao's avatar
Lijiao committed
178
179
            width: 60,
            className: 'tableHead idtitle',
Lijiao's avatar
Lijiao committed
180
            // the sort of string
Lijiao's avatar
Lijiao committed
181
182
183
184
185
186
            sorter: (a: TableObj, b: TableObj): number => a.id.localeCompare(b.id),
            render: (text: string, record: TableObj) => {
                return (
                    <div>{record.id}</div>
                );
            }
Lijiao's avatar
Lijiao committed
187
188
189
190
        }, {
            title: 'Duration',
            dataIndex: 'duration',
            key: 'duration',
Lijiao's avatar
Lijiao committed
191
            width: 140,
Lijiao's avatar
Lijiao committed
192
193
194
195
196
197
198
199
200
201
            // the sort of number
            sorter: (a: TableObj, b: TableObj) => (a.duration as number) - (b.duration as number),
            render: (text: string, record: TableObj) => {
                let duration;
                if (record.duration !== undefined && record.duration > 0) {
                    duration = convertDuration(record.duration);
                } else {
                    duration = 0;
                }
                return (
Lijiao's avatar
Lijiao committed
202
                    <div className="durationsty"><div>{duration}</div></div>
Lijiao's avatar
Lijiao committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
                );
            },
        }, {
            title: 'Status',
            dataIndex: 'status',
            key: 'status',
            width: 150,
            className: 'tableStatus',
            render: (text: string, record: TableObj) => {
                bgColor = record.status;
                return (
                    <span className={`${bgColor} commonStyle`}>{record.status}</span>
                );
            },
            filters: trialJob,
            onFilter: (value: string, record: TableObj) => record.status.indexOf(value) === 0,
            sorter: (a: TableObj, b: TableObj): number => a.status.localeCompare(b.status)
        }, {
            title: 'Default Metric',
            dataIndex: 'acc',
            key: 'acc',
            width: 200,
            sorter: (a: TableObj, b: TableObj) => (a.acc as number) - (b.acc as number),
            render: (text: string, record: TableObj) => {
227
228
229
230
231
232
233
234
                const accuracy = record.acc;
                let wei = 0;
                if (accuracy) {
                    if (accuracy.toString().indexOf('.') !== -1) {
                        wei = accuracy.toString().length - accuracy.toString().indexOf('.') - 1;
                    }
                }
                return (
Lijiao's avatar
Lijiao committed
235
236
237
238
                    <div>
                        {
                            record.acc
                                ?
239
240
241
242
243
                                wei > 6
                                    ?
                                    record.acc.toFixed(6)
                                    :
                                    record.acc
Lijiao's avatar
Lijiao committed
244
                                :
Lijiao's avatar
Lijiao committed
245
                                '--'
Lijiao's avatar
Lijiao committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
                        }
                    </div>
                );
            }
        }, {
            title: 'Operation',
            dataIndex: 'operation',
            key: 'operation',
            width: 90,
            render: (text: string, record: TableObj) => {
                let trialStatus = record.status;
                let flagKill = false;
                if (trialStatus === 'RUNNING') {
                    flagKill = true;
                } else {
                    flagKill = false;
                }
                return (
                    flagKill
                        ?
                        (
                            <Popconfirm
Lijiao's avatar
Lijiao committed
268
                                title="Are you sure to cancel this trial?"
Lijiao's avatar
Lijiao committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
                                onConfirm={this.killJob.bind(this, record.key, record.id, record.status)}
                            >
                                <Button type="primary" className="tableButton">Kill</Button>
                            </Popconfirm>
                        )
                        :
                        (
                            <Button
                                type="primary"
                                className="tableButton"
                                disabled={true}
                            >
                                Kill
                            </Button>
                        )
                );
            },
        }, {
287
288
289
            title: 'Intermediate Result',
            dataIndex: 'intermediate',
            key: 'intermediate',
Lijiao's avatar
Lijiao committed
290
291
292
293
294
295
            width: '16%',
            render: (text: string, record: TableObj) => {
                return (
                    <Button
                        type="primary"
                        className="tableButton"
296
                        onClick={this.showIntermediateModal.bind(this, record.id)}
Lijiao's avatar
Lijiao committed
297
                    >
298
                        Intermediate
Lijiao's avatar
Lijiao committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
                    </Button>
                );
            },
        }
        ];

        const openRow = (record: TableObj) => {
            let isHasParameters = true;
            if (record.description.parameters.error) {
                isHasParameters = false;
            }
            const parametersRow = {
                parameters: record.description.parameters
            };
313
314
315
316
317
            const logPathRow = record.description.logPath !== undefined
                ?
                record.description.logPath
                :
                'This trial\'s logPath are not available.';
Lijiao's avatar
Lijiao committed
318
319
            return (
                <pre id="allList" className="hyperpar">
320
                    {
Lijiao's avatar
Lijiao committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
                        isHasParameters
                            ?
                            < JSONTree
                                hideRoot={true}
                                shouldExpandNode={() => true}  // default expandNode
                                getItemString={() => (<span />)}  // remove the {} items
                                data={parametersRow}
                            />
                            :
                            <div className="logpath">
                                <span className="logName">Error: </span>
                                <span className="error">'This trial's parameters are not available.'</span>
                            </div>
                    }
335
                    <LogPath logStr={logPathRow} />
Lijiao's avatar
Lijiao committed
336
337
338
339
340
341
342
343
344
345
                </pre>
            );
        };

        return (
            <Row className="tableList">
                <div id="tableList">
                    <Table
                        columns={columns}
                        expandedRowRender={openRow}
346
                        dataSource={isHasSearch ? searchResult : tableSource}
Lijiao's avatar
Lijiao committed
347
                        className="commonTableStyle"
348
                        pagination={{ pageSize: entries }}
Lijiao's avatar
Lijiao committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
                    />
                    <Modal
                        title="Intermediate Result"
                        visible={modalVisible}
                        onCancel={this.hideIntermediateModal}
                        footer={null}
                        destroyOnClose={true}
                        width="80%"
                    >
                        <ReactEcharts
                            option={intermediateOption}
                            style={{
                                width: '100%',
                                height: 0.7 * window.innerHeight
                            }}
                            theme="my_theme"
                        />
                    </Modal>
                </div>
            </Row>
        );
    }
}

export default TableList;