TableList.tsx 16.3 KB
Newer Older
Lijiao's avatar
Lijiao committed
1
2
3
import * as React from 'react';
import axios from 'axios';
import ReactEcharts from 'echarts-for-react';
4
import {
5
    Row, Table, Button, Popconfirm, Modal, Checkbox
6
} from 'antd';
7
const CheckboxGroup = Checkbox.Group;
8
import { MANAGER_IP, trialJobStatus, COLUMN, COLUMN_INDEX } from '../../static/const';
Lijiao's avatar
Lijiao committed
9
10
11
import { convertDuration, intermediateGraphOption, killJob } from '../../static/function';
import { TableObj, TrialJob } from '../../static/interface';
import OpenRow from '../public-child/OpenRow';
12
13
// import DefaultMetric from '../public-child/DefaultMetrc';
import IntermediateVal from '../public-child/IntermediateVal';
14
import '../../static/style/search.scss';
Lijiao's avatar
Lijiao committed
15
16
require('../../static/style/tableStatus.css');
require('../../static/style/logPath.scss');
17
require('../../static/style/search.scss');
Lijiao's avatar
Lijiao committed
18
19
require('../../static/style/table.scss');
require('../../static/style/button.scss');
20
require('../../static/style/tableList.scss');
Lijiao's avatar
Lijiao committed
21
22
23
24
25
26
27
28
29
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 {
30
    entries: number;
Lijiao's avatar
Lijiao committed
31
    tableSource: Array<TableObj>;
Lijiao's avatar
Lijiao committed
32
    updateList: Function;
33
    platform: string;
34
    logCollection: boolean;
Lijiao's avatar
Lijiao committed
35
36
37
38
39
}

interface TableListState {
    intermediateOption: object;
    modalVisible: boolean;
40
41
42
43
44
45
46
47
    isObjFinal: boolean;
    isShowColumn: boolean;
    columnSelected: Array<string>; // user select columnKeys
}

interface ColumnIndex {
    name: string;
    index: number;
Lijiao's avatar
Lijiao committed
48
49
50
51
52
}

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

    public _isMounted = false;
53
54
55
    public intervalTrialLog = 10;
    public _trialId: string;

Lijiao's avatar
Lijiao committed
56
57
58
59
60
    constructor(props: TableListProps) {
        super(props);

        this.state = {
            intermediateOption: {},
61
62
63
            modalVisible: false,
            isObjFinal: false,
            isShowColumn: false,
64
            columnSelected: COLUMN
Lijiao's avatar
Lijiao committed
65
66
67
68
69
70
71
72
73
74
75
        };
    }

    showIntermediateModal = (id: string) => {

        axios(`${MANAGER_IP}/metric-data/${id}`, {
            method: 'GET'
        })
            .then(res => {
                if (res.status === 200) {
                    const intermediateArr: number[] = [];
76
                    // support intermediate result is dict
Lijiao's avatar
Lijiao committed
77
                    Object.keys(res.data).map(item => {
Lijiao's avatar
Lijiao committed
78
79
80
81
82
83
                        const temp = JSON.parse(res.data[item].data);
                        if (typeof temp === 'object') {
                            intermediateArr.push(temp.default);
                        } else {
                            intermediateArr.push(temp);
                        }
Lijiao's avatar
Lijiao committed
84
                    });
Lijiao's avatar
Lijiao committed
85
                    const intermediate = intermediateGraphOption(intermediateArr, id);
Lijiao's avatar
Lijiao committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                    if (this._isMounted) {
                        this.setState(() => ({
                            intermediateOption: intermediate
                        }));
                    }
                }
            });
        if (this._isMounted) {
            this.setState({
                modalVisible: true
            });
        }
    }

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

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
    hideShowColumnModal = () => {
        if (this._isMounted) {
            this.setState({
                isShowColumn: false
            });
        }
    }

    // click add column btn, just show the modal of addcolumn
    addColumn = () => {
        // show user select check button
        if (this._isMounted) {
            this.setState({
                isShowColumn: true
            });
        }
    }

    // checkbox for coloumn
    selectedColumn = (checkedValues: Array<string>) => {
        let count = 6;
        const want: Array<object> = [];
        const finalKeys: Array<string> = [];
        const wantResult: Array<string> = [];
        Object.keys(checkedValues).map(m => {
            switch (checkedValues[m]) {
Lijiao's avatar
Lijiao committed
134
135
136
137
                case 'Trial No.':
                case 'Id':
                case 'Duration':
                case 'Status':
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                case 'Operation':
                case 'Default':
                case 'Intermediate Result':
                    break;
                default:
                    finalKeys.push(checkedValues[m]);
            }
        });

        Object.keys(finalKeys).map(n => {
            want.push({
                name: finalKeys[n],
                index: count++
            });
        });

        Object.keys(checkedValues).map(item => {
            const temp = checkedValues[item];
            Object.keys(COLUMN_INDEX).map(key => {
                const index = COLUMN_INDEX[key];
                if (index.name === temp) {
                    want.push(index);
                }
            });
        });

        want.sort((a: ColumnIndex, b: ColumnIndex) => {
            return a.index - b.index;
        });

        Object.keys(want).map(i => {
            wantResult.push(want[i].name);
        });

        if (this._isMounted) {
            this.setState(() => ({ columnSelected: wantResult }));
        }
    }

Lijiao's avatar
Lijiao committed
177
    openRow = (record: TableObj) => {
178
        const { platform, logCollection } = this.props;
Lijiao's avatar
Lijiao committed
179
180
181
182
        return (
            <OpenRow
                trainingPlatform={platform}
                record={record}
183
                logCollection={logCollection}
Lijiao's avatar
Lijiao committed
184
185
186
187
            />
        );
    }

Lijiao's avatar
Lijiao committed
188
189
190
191
192
193
194
195
196
    componentDidMount() {
        this._isMounted = true;
    }

    componentWillUnmount() {
        this._isMounted = false;
    }

    render() {
197

198
        const { entries, tableSource, updateList } = this.props;
199
        const { intermediateOption, modalVisible, isShowColumn, columnSelected } = this.state;
200
        let showTitle = COLUMN;
201
202
203
        let bgColor = '';
        const trialJob: Array<TrialJob> = [];
        const showColumn: Array<object> = [];
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
        if (tableSource.length >= 1) {
            const temp = tableSource[0].acc;
            if (temp !== undefined && typeof temp === 'object') {
                if (this._isMounted) {
                    // concat default column and finalkeys
                    const item = Object.keys(temp);
                    const want: Array<string> = [];
                    Object.keys(item).map(key => {
                        if (item[key] !== 'default') {
                            want.push(item[key]);
                        }
                    });
                    showTitle = COLUMN.concat(want);
                }
            }
        }
Lijiao's avatar
Lijiao committed
220
221
222
223
224
225
        trialJobStatus.map(item => {
            trialJob.push({
                text: item,
                value: item
            });
        });
226
227
228
        Object.keys(columnSelected).map(key => {
            const item = columnSelected[key];
            switch (item) {
Lijiao's avatar
Lijiao committed
229
                case 'Trial No.':
230
231
232
233
234
235
236
                    showColumn.push({
                        title: 'Trial No.',
                        dataIndex: 'sequenceId',
                        key: 'sequenceId',
                        width: 120,
                        className: 'tableHead',
                        sorter:
Lijiao's avatar
Lijiao committed
237
                            (a: TableObj, b: TableObj) =>
238
239
240
                                (a.sequenceId as number) - (b.sequenceId as number)
                    });
                    break;
Lijiao's avatar
Lijiao committed
241
                case 'Id':
242
243
244
245
246
247
248
                    showColumn.push({
                        title: 'Id',
                        dataIndex: 'id',
                        key: 'id',
                        width: 60,
                        className: 'tableHead idtitle',
                        // the sort of string
Lijiao's avatar
Lijiao committed
249
250
                        sorter: (a: TableObj, b: TableObj): number => a.id.localeCompare(b.id),
                        render: (text: string, record: TableObj) => {
251
252
253
254
255
256
                            return (
                                <div>{record.id}</div>
                            );
                        }
                    });
                    break;
Lijiao's avatar
Lijiao committed
257
                case 'Duration':
258
259
260
261
262
263
                    showColumn.push({
                        title: 'Duration',
                        dataIndex: 'duration',
                        key: 'duration',
                        width: 140,
                        // the sort of number
Lijiao's avatar
Lijiao committed
264
265
                        sorter: (a: TableObj, b: TableObj) => (a.duration as number) - (b.duration as number),
                        render: (text: string, record: TableObj) => {
266
267
268
269
270
271
272
273
274
275
276
277
                            let duration;
                            if (record.duration !== undefined && record.duration > 0) {
                                duration = convertDuration(record.duration);
                            } else {
                                duration = 0;
                            }
                            return (
                                <div className="durationsty"><div>{duration}</div></div>
                            );
                        },
                    });
                    break;
Lijiao's avatar
Lijiao committed
278
                case 'Status':
279
280
281
282
283
284
                    showColumn.push({
                        title: 'Status',
                        dataIndex: 'status',
                        key: 'status',
                        width: 150,
                        className: 'tableStatus',
Lijiao's avatar
Lijiao committed
285
                        render: (text: string, record: TableObj) => {
286
287
288
289
290
291
                            bgColor = record.status;
                            return (
                                <span className={`${bgColor} commonStyle`}>{record.status}</span>
                            );
                        },
                        filters: trialJob,
292
293
294
295
                        onFilter: (value: string, record: TableObj) => {
                            return record.status.indexOf(value) === 0;
                        },
                        // onFilter: (value: string, record: TableObj) => record.status.indexOf(value) === 0,
Lijiao's avatar
Lijiao committed
296
                        sorter: (a: TableObj, b: TableObj): number => a.status.localeCompare(b.status)
297
298
299
300
301
302
303
                    });
                    break;
                case 'Default':
                    showColumn.push({
                        title: 'Default Metric',
                        dataIndex: 'acc',
                        key: 'acc',
304
                        width: 160,
Lijiao's avatar
Lijiao committed
305
                        sorter: (a: TableObj, b: TableObj) => {
306
307
308
309
                            const aa = a.description.intermediate;
                            const bb = b.description.intermediate;
                            if (aa !== undefined && bb !== undefined) {
                                return aa[aa.length - 1] - bb[bb.length - 1];
310
311
312
313
                            } else {
                                return NaN;
                            }
                        },
Lijiao's avatar
Lijiao committed
314
                        render: (text: string, record: TableObj) => {
315
                            return (
316
                                <IntermediateVal record={record} />
317
318
319
320
321
322
323
324
325
326
                            );
                        }
                    });
                    break;
                case 'Operation':
                    showColumn.push({
                        title: 'Operation',
                        dataIndex: 'operation',
                        key: 'operation',
                        width: 90,
Lijiao's avatar
Lijiao committed
327
                        render: (text: string, record: TableObj) => {
328
329
330
331
332
333
334
335
336
                            let trialStatus = record.status;
                            let flagKill = false;
                            if (trialStatus === 'RUNNING') {
                                flagKill = true;
                            } else {
                                flagKill = false;
                            }
                            return (
                                flagKill
337
                                    ?
338
339
340
                                    (
                                        <Popconfirm
                                            title="Are you sure to cancel this trial?"
Lijiao's avatar
Lijiao committed
341
342
                                            onConfirm={killJob.
                                                bind(this, record.key, record.id, record.status, updateList)}
343
344
345
346
                                        >
                                            <Button type="primary" className="tableButton">Kill</Button>
                                        </Popconfirm>
                                    )
347
                                    :
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
                                    (
                                        <Button
                                            type="primary"
                                            className="tableButton"
                                            disabled={true}
                                        >
                                            Kill
                                        </Button>
                                    )
                            );
                        },
                    });
                    break;

                case 'Intermediate Result':
                    showColumn.push({
                        title: 'Intermediate Result',
                        dataIndex: 'intermediate',
                        key: 'intermediate',
                        width: '16%',
Lijiao's avatar
Lijiao committed
368
                        render: (text: string, record: TableObj) => {
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
                            return (
                                <Button
                                    type="primary"
                                    className="tableButton"
                                    onClick={this.showIntermediateModal.bind(this, record.id)}
                                >
                                    Intermediate
                                </Button>
                            );
                        },
                    });
                    break;
                default:
                    showColumn.push({
                        title: item,
                        dataIndex: item,
                        key: item,
                        width: 150,
Lijiao's avatar
Lijiao committed
387
                        render: (text: string, record: TableObj) => {
388
389
390
391
392
393
394
395
396
397
398
                            return (
                                <div>
                                    {
                                        record.acc
                                            ?
                                            record.acc[item]
                                            :
                                            '--'
                                    }
                                </div>
                            );
Lijiao's avatar
Lijiao committed
399
                        }
400
                    });
Lijiao's avatar
Lijiao committed
401
            }
402
        });
Lijiao's avatar
Lijiao committed
403
404
405
406
407

        return (
            <Row className="tableList">
                <div id="tableList">
                    <Table
408
                        columns={showColumn}
Lijiao's avatar
Lijiao committed
409
                        expandedRowRender={this.openRow}
410
                        dataSource={tableSource}
Lijiao's avatar
Lijiao committed
411
                        className="commonTableStyle"
412
                        pagination={{ pageSize: entries }}
Lijiao's avatar
Lijiao committed
413
                    />
414
                    {/* Intermediate Result Modal */}
Lijiao's avatar
Lijiao committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
                    <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>
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
                {/* Add Column Modal */}
                <Modal
                    title="Table Title"
                    visible={isShowColumn}
                    onCancel={this.hideShowColumnModal}
                    footer={null}
                    destroyOnClose={true}
                    width="40%"
                >
                    <CheckboxGroup
                        options={showTitle}
                        defaultValue={columnSelected}
                        onChange={this.selectedColumn}
                        className="titleColumn"
                    />
                </Modal>
449

Lijiao's avatar
Lijiao committed
450
451
452
453
454
            </Row>
        );
    }
}

455
export default TableList;