Sessionpro.tsx 19.6 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
import * as React from 'react';
import axios from 'axios';
3
import { Table, Select, Row, Col, Icon, Button } from 'antd';
4
import { MANAGER_IP, overviewItem } from '../const';
Deshui Yu's avatar
Deshui Yu committed
5
6
7
const Option = Select.Option;
import JSONTree from 'react-json-tree';
require('../style/sessionpro.css');
Lijiao's avatar
Lijiao committed
8
require('../style/logPath.css');
Deshui Yu's avatar
Deshui Yu committed
9
10
11
12
13
14
15
16
17

interface TableObj {
    key: number;
    id: string;
    duration: number;
    start: string;
    end: string;
    status: string;
    acc?: number;
Lijiao's avatar
Lijiao committed
18
    description: Parameters;
Deshui Yu's avatar
Deshui Yu committed
19
20
21
22
23
}

interface Parameters {
    parameters: object;
    logPath?: string;
Lijiao's avatar
Lijiao committed
24
    isLink?: boolean;
Deshui Yu's avatar
Deshui Yu committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
}

interface Experiment {
    id: string;
    author: string;
    experName: string;
    runConcurren: number;
    maxDuration: number;
    execDuration: number;
    MaxTrialNum: number;
    startTime: string;
    endTime: string;
}

interface SessionState {
    tableData: Array<TableObj>;
    searchSpace: object;
Lijiao's avatar
Lijiao committed
42
    status: string;
Deshui Yu's avatar
Deshui Yu committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    trialProfile: Experiment;
    tunerAssessor: object;
    selNum: number;
    option: object;
    noData: string;
}

class Sessionpro extends React.Component<{}, SessionState> {

    public _isMounted = false;
    public intervalID = 0;
    public intervalProfile = 1;

    constructor(props: {}) {
        super(props);
        this.state = {
            searchSpace: {},
Lijiao's avatar
Lijiao committed
60
            status: '',
Deshui Yu's avatar
Deshui Yu committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
            trialProfile: {
                id: '',
                author: '',
                experName: '',
                runConcurren: 0,
                maxDuration: 0,
                execDuration: 0,
                MaxTrialNum: 0,
                startTime: '',
                endTime: ''
            },
            tunerAssessor: {},
            tableData: [{
                key: 0,
                id: '',
                duration: 0,
                start: '',
                end: '',
                status: '',
                acc: 0,
Lijiao's avatar
Lijiao committed
81
82
83
                description: {
                    parameters: {}
                }
Deshui Yu's avatar
Deshui Yu committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
            }],
            selNum: overviewItem,
            option: {},
            noData: ''
        };
    }

    // show session
    showSessionPro = () => {
        axios(`${MANAGER_IP}/experiment`, {
            method: 'GET'
        })
            .then(res => {
                if (res.status === 200) {
                    let sessionData = res.data;
                    let tunerAsstemp = [];
                    let trialPro = [];
Lijiao's avatar
Lijiao committed
101
                    const startExper = new Date(sessionData.startTime).toLocaleString('en-US');
102
103
                    let experEndStr: string;
                    if (sessionData.endTime !== undefined) {
Lijiao's avatar
Lijiao committed
104
                        experEndStr = new Date(sessionData.endTime).toLocaleString('en-US');
105
106
107
                    } else {
                        experEndStr = 'not over';
                    }
Deshui Yu's avatar
Deshui Yu committed
108
109
110
111
112
113
114
115
                    trialPro.push({
                        id: sessionData.id,
                        author: sessionData.params.authorName,
                        experName: sessionData.params.experimentName,
                        runConcurren: sessionData.params.trialConcurrency,
                        maxDuration: sessionData.params.maxExecDuration,
                        execDuration: sessionData.execDuration,
                        MaxTrialNum: sessionData.params.maxTrialNum,
116
117
                        startTime: startExper,
                        endTime: experEndStr
Deshui Yu's avatar
Deshui Yu committed
118
119
120
121
122
                    });
                    tunerAsstemp.push({
                        tuner: sessionData.params.tuner,
                        assessor: sessionData.params.assessor
                    });
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
                    // search space format loguniform max and min
                    const searchSpace = JSON.parse(sessionData.params.searchSpace);
                    Object.keys(searchSpace).map(item => {
                        const key = searchSpace[item]._type;
                        if (key === 'loguniform') {
                            let value = searchSpace[item]._value;
                            const a = Math.pow(10, value[0]);
                            const b = Math.pow(10, value[1]);
                            if (a < b) {
                                value = [a, b];
                            } else {
                                value = [b, a];
                            }
                            searchSpace[item]._value = value;
                        }
                    });
Deshui Yu's avatar
Deshui Yu committed
139
140
141
                    if (this._isMounted) {
                        this.setState({
                            trialProfile: trialPro[0],
142
                            searchSpace: searchSpace,
Deshui Yu's avatar
Deshui Yu committed
143
144
145
146
147
                            tunerAssessor: tunerAsstemp[0]
                        });
                    }
                }
            });
Lijiao's avatar
Lijiao committed
148
149
150
151
152
153
154
155
156
157
158

        axios(`${MANAGER_IP}/check-status`, {
            method: 'GET'
        })
            .then(res => {
                if (res.status === 200 && this._isMounted) {
                    this.setState({
                        status: res.data.status
                    });
                }
            });
Deshui Yu's avatar
Deshui Yu committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    }

    showTrials = () => {
        axios(`${MANAGER_IP}/trial-jobs`, {
            method: 'GET'
        })
            .then(res => {
                if (res.status === 200) {
                    const { selNum } = this.state;
                    const tableData = res.data;
                    const topTableData: Array<TableObj> = [];
                    Object.keys(tableData).map(item => {
                        if (tableData[item].status === 'SUCCEEDED') {
                            const desJobDetail: Parameters = {
                                parameters: {}
                            };
Lijiao's avatar
Lijiao committed
175
176
                            const startTime = new Date(tableData[item].startTime).toLocaleString('en-US');
                            const endTime = new Date(tableData[item].endTime).toLocaleString('en-US');
177
                            const duration = (tableData[item].endTime - tableData[item].startTime) / 1000;
Deshui Yu's avatar
Deshui Yu committed
178
179
                            let acc;
                            if (tableData[item].finalMetricData) {
180
                                acc = parseFloat(tableData[item].finalMetricData.data);
Deshui Yu's avatar
Deshui Yu committed
181
182
183
184
                            }
                            desJobDetail.parameters = JSON.parse(tableData[item].hyperParameters).parameters;
                            if (tableData[item].logPath !== undefined) {
                                desJobDetail.logPath = tableData[item].logPath;
Lijiao's avatar
Lijiao committed
185
186
187
188
                                const isSessionLink = /^http/gi.test(tableData[item].logPath);
                                if (isSessionLink) {
                                    desJobDetail.isLink = true;
                                }
Deshui Yu's avatar
Deshui Yu committed
189
190
191
192
193
                            }
                            topTableData.push({
                                key: topTableData.length,
                                id: tableData[item].id,
                                duration: duration,
194
195
                                start: startTime,
                                end: endTime,
Deshui Yu's avatar
Deshui Yu committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
                                status: tableData[item].status,
                                acc: acc,
                                description: desJobDetail
                            });
                        }
                    });
                    topTableData.sort((a: TableObj, b: TableObj) => {
                        if (a.acc && b.acc) {
                            return b.acc - a.acc;
                        } else {
                            return NaN;
                        }
                    });
                    topTableData.length = Math.min(selNum, topTableData.length);
                    if (this._isMounted) {
                        this.setState({
Lijiao's avatar
Lijiao committed
212
                            tableData: topTableData
Deshui Yu's avatar
Deshui Yu committed
213
214
215
216
217
218
219
220
221
222
223
224
                        });
                    }
                }
            });
    }

    handleChange = (value: string) => {
        let num = parseFloat(value);
        window.clearInterval(this.intervalID);
        if (this._isMounted) {
            this.setState({ selNum: num }, () => {
                this.showTrials();
Lijiao's avatar
Lijiao committed
225
                this.intervalID = window.setInterval(this.showTrials, 10000);
Deshui Yu's avatar
Deshui Yu committed
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
    downExperimentContent = () => {
        axios
            .all([
                axios.get(`${MANAGER_IP}/experiment`),
                axios.get(`${MANAGER_IP}/trial-jobs`)
            ])
            .then(axios.spread((res, res1) => {
                if (res.status === 200 && res1.status === 200) {
                    if (res.data.params.searchSpace) {
                        res.data.params.searchSpace = JSON.parse(res.data.params.searchSpace);
                    }
                    const contentOfExperiment = JSON.stringify(res.data, null, 2);
                    let trialMessagesArr = res1.data;
                    Object.keys(trialMessagesArr).map(item => {
                        trialMessagesArr[item].hyperParameters = JSON.parse(trialMessagesArr[item].hyperParameters);
                    });
                    const trialMessages = JSON.stringify(trialMessagesArr, null, 2);
                    const aTag = document.createElement('a');
                    const file = new Blob([contentOfExperiment, trialMessages], { type: 'application/json' });
                    aTag.download = 'experiment.txt';
                    aTag.href = URL.createObjectURL(file);
                    aTag.click();
                    URL.revokeObjectURL(aTag.href);
                    if (navigator.userAgent.indexOf('Firefox') > -1) {
                        const downTag = document.createElement('a');
                        downTag.addEventListener('click', function () {
                            downTag.download = 'experiment.txt';
                            downTag.href = URL.createObjectURL(file);
                        });
                        let eventMouse = document.createEvent('MouseEvents');
                        eventMouse.initEvent('click', false, false);
                        downTag.dispatchEvent(eventMouse);
                    }
                }
            }));
    }

Deshui Yu's avatar
Deshui Yu committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
    componentDidMount() {
        this.showSessionPro();
        this.showTrials();
        this._isMounted = true;
        this.intervalID = window.setInterval(this.showTrials, 10000);
        this.intervalProfile = window.setInterval(this.showSessionPro, 60000);
    }

    componentWillUnmount() {
        this._isMounted = false;
        window.clearInterval(this.intervalID);
        window.clearInterval(this.intervalProfile);
    }

    render() {
        // show better job details
        let bgColor = '';
        const columns = [{
            title: 'Id',
            dataIndex: 'id',
            key: 'id',
            width: 150,
            className: 'tableHead',
        }, {
            title: 'Duration/s',
            dataIndex: 'duration',
            key: 'duration',
            width: '9%'
        }, {
            title: 'Start',
            dataIndex: 'start',
            key: 'start',
            width: 150
        }, {
            title: 'End',
            dataIndex: 'end',
            key: 'end',
            width: 150
        }, {
            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>
                );
            }
        }, {
            title: 'Loss/Accuracy',
            dataIndex: 'acc',
            key: 'acc',
            width: 150
        }];

        const openRow = (record: TableObj) => {
Lijiao's avatar
Lijiao committed
325
326
327
328
329
330
331
332
            const openRowDataSource = {
                parameters: record.description.parameters
            };
            let isLogLink: boolean = false;
            const logPathRow = record.description.logPath;
            if (record.description.isLink !== undefined) {
                isLogLink = true;
            }
Deshui Yu's avatar
Deshui Yu committed
333
334
335
336
337
338
            return (
                <pre id="description" className="jsontree">
                    <JSONTree
                        hideRoot={true}
                        shouldExpandNode={() => true}  // default expandNode
                        getItemString={() => (<span />)}  // remove the {} items
Lijiao's avatar
Lijiao committed
339
                        data={openRowDataSource}
Deshui Yu's avatar
Deshui Yu committed
340
                    />
341
                    {
Lijiao's avatar
Lijiao committed
342
343
344
345
346
347
348
349
350
351
352
353
                        isLogLink
                            ?
                            <div className="logpath">
                                <span className="logName">logPath: </span>
                                <a className="logContent logHref" href={logPathRow} target="_blank">{logPathRow}</a>
                            </div>
                            :
                            <div className="logpath">
                                <span className="logName">logPath: </span>
                                <span className="logContent">{logPathRow}</span>
                            </div>
                    }
Deshui Yu's avatar
Deshui Yu committed
354
355
356
357
358
                </pre>
            );
        };

        const {
Lijiao's avatar
Lijiao committed
359
            trialProfile, searchSpace, tunerAssessor, tableData, status
Deshui Yu's avatar
Deshui Yu committed
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
        } = this.state;
        let running;
        if (trialProfile.endTime === 'not over') {
            running = trialProfile.maxDuration - trialProfile.execDuration;
        } else {
            running = 0;
        }
        return (
            <div className="session" id="session">
                <div className="head">
                    <div className="headCon">
                        <div className="author">
                            <div className="message">
                                <div className="proKey">
                                    <span>Author</span>
                                    <span className="messcont">{trialProfile.author}</span>
                                </div>
                                <span>Experiment&nbsp;Name</span>
                                <p className="messcont">{trialProfile.experName}</p>
                            </div>
                            <div className="logo">
                                <Icon className="bone" type="user" />
                            </div>
                        </div>
                        <div className="type">
                            <div className="message">
                                <div className="proKey">
                                    <span>id</span>
                                    <span className="messcont">{trialProfile.id}</span>
                                </div>
                                <p>
                                    <span>Duration</span>
                                    <span className="messcont">{trialProfile.maxDuration}s</span>
                                </p>
                                <p>
                                    <span>Still&nbsp;running</span>
                                    <span className="messcont">{running}s</span>
                                </p>
                            </div>
                            <div className="logo">
                                <Icon className="tyellow" type="bulb" />
                            </div>
                        </div>
                        <div className="runtime message">
                            <p className="proTime">
                                <span>Start Time</span><br />
                                <span className="messcont">{trialProfile.startTime}</span>
                            </p>
                            <span>End Time</span>
                            <p className="messcont">{trialProfile.endTime}</p>
                        </div>
                        <div className="cdf">
                            <div className="message">
                                <div className="proKey trialNum">
                                    Concurrency&nbsp;Trial
                                    <span className="messcont">{trialProfile.runConcurren}</span>
                                </div>
                                <p>
                                    Max&nbsp;Trial&nbsp;Number
                                    <span className="messcont">{trialProfile.MaxTrialNum}</span>
                                </p>
Lijiao's avatar
Lijiao committed
421
422
423
424
                                <p className="experStatus">
                                    Status
                                    <span className="messcont">{status}</span>
                                </p>
Deshui Yu's avatar
Deshui Yu committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
                            </div>
                            <div className="logo">
                                <Icon className="fogreen" type="picture" />
                            </div>
                        </div>
                    </div>
                </div>
                <div className="clear" />
                <div className="jsonbox">
                    <div>
                        <h2 className="searchTitle title">Search Space</h2>
                        <pre className="searchSpace jsontree">
                            <JSONTree
                                hideRoot={true}
                                shouldExpandNode={() => true}
                                getItemString={() => (<span />)}
                                data={searchSpace}
                            />
                        </pre>
                    </div>
                    <div>
                        <h2 className="searchTitle title">Trial Profile</h2>
                        <pre className="trialProfile jsontree">
                            <JSONTree
                                hideRoot={true}
                                shouldExpandNode={() => true}
                                getItemString={() => (<span />)}
                                data={tunerAssessor}
                            />
                        </pre>
                    </div>
                </div>
                <div className="clear" />
                <div className="comtable">
                    <div className="selectInline">
                        <Row>
                            <Col span={18}>
                                <h2>The trials that successed</h2>
                            </Col>
                            <Col span={6}>
                                <span className="tabuser1">top</span>
                                <Select
                                    style={{ width: 200 }}
Lijiao's avatar
Lijiao committed
468
                                    placeholder="50"
Deshui Yu's avatar
Deshui Yu committed
469
470
471
                                    optionFilterProp="children"
                                    onSelect={this.handleChange}
                                >
Lijiao's avatar
Lijiao committed
472
                                    <Option value="5">5</Option>
Deshui Yu's avatar
Deshui Yu committed
473
474
                                    <Option value="50">50</Option>
                                    <Option value="100">100</Option>
Lijiao's avatar
Lijiao committed
475
476
                                    <Option value="150">150</Option>
                                    <Option value="200">200</Option>
Deshui Yu's avatar
Deshui Yu committed
477
478
479
480
481
482
483
484
485
486
487
488
                                </Select>
                            </Col>
                        </Row>
                    </div>
                    <Table
                        columns={columns}
                        expandedRowRender={openRow}
                        dataSource={tableData}
                        className="tables"
                        bordered={true}
                    />
                </div>
489
490
491
492
493
494
495
496
497
                <div className="downExp">
                    <Button
                        type="primary"
                        className="tableButton"
                        onClick={this.downExperimentContent}
                    >
                        Down Experiment
                    </Button>
                </div>
Deshui Yu's avatar
Deshui Yu committed
498
499
500
501
502
            </div>
        );
    }
}
export default Sessionpro;