ExperimentDrawer.tsx 5.52 KB
Newer Older
v-liguo's avatar
v-liguo committed
1
2
3
import * as React from 'react';
import axios from 'axios';
import { downFile } from '../../static/function';
4
5
6
import {
    Stack, PrimaryButton, DefaultButton, Panel, StackItem, Pivot, PivotItem
} from 'office-ui-fabric-react';
v-liguo's avatar
v-liguo committed
7
8
9
import { MANAGER_IP, DRAWEROPTION } from '../../static/const';
import MonacoEditor from 'react-monaco-editor';
import '../../static/style/logDrawer.scss';
10
import { TrialManager } from '../../static/model/trialmanager';
v-liguo's avatar
v-liguo committed
11
12
13
14
15
16
17
18

interface ExpDrawerProps {
    isVisble: boolean;
    closeExpDrawer: () => void;
}

interface ExpDrawerState {
    experiment: string;
Lijiao's avatar
Lijiao committed
19
    expDrawerHeight: number;
v-liguo's avatar
v-liguo committed
20
21
22
23
}

class ExperimentDrawer extends React.Component<ExpDrawerProps, ExpDrawerState> {

24
    public _isCompareMount!: boolean;
v-liguo's avatar
v-liguo committed
25
26
27
28
    constructor(props: ExpDrawerProps) {
        super(props);

        this.state = {
Lijiao's avatar
Lijiao committed
29
            experiment: '',
30
            expDrawerHeight: window.innerHeight
v-liguo's avatar
v-liguo committed
31
32
33
        };
    }

Lijiao's avatar
Lijiao committed
34
    getExperimentContent = (): void => {
v-liguo's avatar
v-liguo committed
35
36
37
38
39
40
        axios
            .all([
                axios.get(`${MANAGER_IP}/experiment`),
                axios.get(`${MANAGER_IP}/trial-jobs`),
                axios.get(`${MANAGER_IP}/metric-data`)
            ])
41
42
43
44
            .then(axios.spread((resExperiment, resTrialJobs, resMetricData) => {
                if (resExperiment.status === 200 && resTrialJobs.status === 200 && resMetricData.status === 200) {
                    if (resExperiment.data.params.searchSpace) {
                        resExperiment.data.params.searchSpace = JSON.parse(resExperiment.data.params.searchSpace);
v-liguo's avatar
v-liguo committed
45
                    }
46
47
                    const trialMessagesArr = TrialManager.expandJobsToTrials(resTrialJobs.data);
                    const interResultList = resMetricData.data;
v-liguo's avatar
v-liguo committed
48
                    Object.keys(trialMessagesArr).map(item => {
49
                        // not deal with trial's hyperParameters
v-liguo's avatar
v-liguo committed
50
51
52
53
                        const trialId = trialMessagesArr[item].id;
                        // add intermediate result message
                        trialMessagesArr[item].intermediate = [];
                        Object.keys(interResultList).map(key => {
54
                            const interId = `${interResultList[key].trialJobId}-${interResultList[key].parameterId}`;
v-liguo's avatar
v-liguo committed
55
56
57
58
59
60
                            if (trialId === interId) {
                                trialMessagesArr[item].intermediate.push(interResultList[key]);
                            }
                        });
                    });
                    const result = {
61
                        experimentParameters: resExperiment.data,
v-liguo's avatar
v-liguo committed
62
63
64
                        trialMessage: trialMessagesArr
                    };
                    if (this._isCompareMount === true) {
65
                        this.setState({ experiment: JSON.stringify(result, null, 4) });
v-liguo's avatar
v-liguo committed
66
67
68
69
70
                    }
                }
            }));
    }

Lijiao's avatar
Lijiao committed
71
    downExperimentParameters = (): void => {
v-liguo's avatar
v-liguo committed
72
73
74
75
        const { experiment } = this.state;
        downFile(experiment, 'experiment.json');
    }

Lijiao's avatar
Lijiao committed
76
    onWindowResize = (): void => {
77
        this.setState(() => ({ expDrawerHeight: window.innerHeight }));
Lijiao's avatar
Lijiao committed
78
79
    }

Lijiao's avatar
Lijiao committed
80
    componentDidMount(): void {
v-liguo's avatar
v-liguo committed
81
82
        this._isCompareMount = true;
        this.getExperimentContent();
Lijiao's avatar
Lijiao committed
83
        window.addEventListener('resize', this.onWindowResize);
v-liguo's avatar
v-liguo committed
84
85
    }

Lijiao's avatar
Lijiao committed
86
    componentWillReceiveProps(nextProps: ExpDrawerProps): void {
v-liguo's avatar
v-liguo committed
87
88
89
90
91
92
        const { isVisble } = nextProps;
        if (isVisble === true) {
            this.getExperimentContent();
        }
    }

Lijiao's avatar
Lijiao committed
93
    componentWillUnmount(): void {
v-liguo's avatar
v-liguo committed
94
        this._isCompareMount = false;
Lijiao's avatar
Lijiao committed
95
        window.removeEventListener('resize', this.onWindowResize);
v-liguo's avatar
v-liguo committed
96
97
    }

Lijiao's avatar
Lijiao committed
98
    render(): React.ReactNode {
v-liguo's avatar
v-liguo committed
99
        const { isVisble, closeExpDrawer } = this.props;
Lijiao's avatar
Lijiao committed
100
        const { experiment, expDrawerHeight } = this.state;
v-liguo's avatar
v-liguo committed
101
        return (
102
103
104
105
106
            <Stack className="logDrawer">
                <Panel
                    isOpen={isVisble}
                    hasCloseButton={false}
                    styles={{ root: { height: expDrawerHeight, paddingTop: 15 } }}
v-liguo's avatar
v-liguo committed
107
                >
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
                    <Pivot style={{ minHeight: 190 }} className="log-tab-body">
                        <PivotItem headerText="Experiment Parameters">
                            <div className="just-for-log">
                                <MonacoEditor
                                    width="100%"
                                    // 92 + marginTop[16]
                                    height={expDrawerHeight - 108}
                                    language="json"
                                    value={experiment}
                                    options={DRAWEROPTION}
                                />
                            </div>
                            <Stack horizontal className="buttons">
                                <StackItem grow={50} className="download">
                                    <PrimaryButton
                                        text="Download"
                                        onClick={this.downExperimentParameters}
v-liguo's avatar
v-liguo committed
125
                                    />
126
127
128
129
130
131
132
133
134
135
136
137
                                </StackItem>
                                <StackItem grow={50} className="close">
                                    <DefaultButton
                                        text="Close"
                                        onClick={closeExpDrawer}
                                    />
                                </StackItem>
                            </Stack>
                        </PivotItem>
                    </Pivot>
                </Panel>
            </Stack>
v-liguo's avatar
v-liguo committed
138
139
140
141
142
        );
    }
}

export default ExperimentDrawer;