LogPanel.tsx 6.06 KB
Newer Older
1
2
import * as React from 'react';
import axios from 'axios';
3
import { Stack, StackItem, Panel, PrimaryButton, DefaultButton, Pivot, PivotItem } from '@fluentui/react';
4
import { infoIcon } from '../buttons/Icon';
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { DOWNLOAD_IP } from '../../static/const';
import { downFile } from '../../static/function';
import MonacoHTML from '../public-child/MonacoEditor';
import '../../static/style/logDrawer.scss';

interface LogDrawerProps {
    closeDrawer: () => void;
    activeTab?: string;
}

interface LogDrawerState {
    nniManagerLogStr: string | null;
    dispatcherLogStr: string | null;
    isLoading: boolean;
    logDrawerHeight: number;
}

class LogDrawer extends React.Component<LogDrawerProps, LogDrawerState> {
    private timerId: number | undefined;

    constructor(props: LogDrawerProps) {
        super(props);

        this.state = {
            nniManagerLogStr: null,
            dispatcherLogStr: null,
            isLoading: true,
32
            logDrawerHeight: window.innerHeight
33
34
35
36
37
38
39
        };
    }

    downloadNNImanager = (): void => {
        if (this.state.nniManagerLogStr !== null) {
            downFile(this.state.nniManagerLogStr, 'nnimanager.log');
        }
40
    };
41
42
43
44
45

    downloadDispatcher = (): void => {
        if (this.state.dispatcherLogStr !== null) {
            downFile(this.state.dispatcherLogStr, 'dispatcher.log');
        }
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    };

    dispatcherHTML = (): React.ReactNode => (
        <div>
            <span>Dispatcher log</span>
            <span className='refresh' onClick={this.manualRefresh}>
                {infoIcon}
            </span>
        </div>
    );

    nnimanagerHTML = (): React.ReactNode => (
        <div>
            <span>NNImanager log</span>
            <span className='refresh' onClick={this.manualRefresh}>
                {infoIcon}
            </span>
        </div>
    );
65
66

    setLogDrawerHeight = (): void => {
67
        this.setState(() => ({ logDrawerHeight: window.innerHeight }));
68
    };
69
70
71
72
73
74

    async componentDidMount(): Promise<void> {
        this.refresh();
        window.addEventListener('resize', this.setLogDrawerHeight);
    }

75
    componentWillUnmount(): void {
76
77
78
79
80
81
82
        window.clearTimeout(this.timerId);
        window.removeEventListener('resize', this.setLogDrawerHeight);
    }

    render(): React.ReactNode {
        const { closeDrawer, activeTab } = this.props;
        const { nniManagerLogStr, dispatcherLogStr, isLoading, logDrawerHeight } = this.state;
83
84
        // tab[height: 56] + tab[margin-bottom: 20] + button[32] + button[margin-top: 45, -bottom: 7] + fluent-panel own paddingBottom[20] + title-border[2]
        const monacoHeight = logDrawerHeight - 182;
85
86
87
88
89
90
        return (
            <Stack>
                <Panel
                    isOpen={true}
                    hasCloseButton={false}
                    isFooterAtBottom={true}
91
92
                    isLightDismiss={true}
                    onLightDismissClick={closeDrawer}
Lijiaoa's avatar
Lijiaoa committed
93
                    className='logPanel'
94
                >
95
96
97
                    <Pivot selectedKey={activeTab} style={{ minHeight: 190 }}>
                        <PivotItem headerText='Dispatcher log' key='dispatcher'>
                            <div className='panel logMargin'>
98
99
100
                                <MonacoHTML
                                    content={dispatcherLogStr || 'Loading...'}
                                    loading={isLoading}
101
                                    height={monacoHeight}
102
                                />
103
104
105
                                <Stack horizontal className='buttons'>
                                    <StackItem grow={12} className='download'>
                                        <PrimaryButton text='Download' onClick={this.downloadDispatcher} />
106
                                    </StackItem>
107
108
                                    <StackItem grow={12} className='close'>
                                        <DefaultButton text='Close' onClick={closeDrawer} />
109
110
                                    </StackItem>
                                </Stack>
111
112
113
114
                            </div>
                        </PivotItem>
                        <PivotItem headerText='NNIManager log' key='nnimanager'>
                            <div className='panel logMargin'>
115
116
117
                                <MonacoHTML
                                    content={nniManagerLogStr || 'Loading...'}
                                    loading={isLoading}
118
                                    height={monacoHeight}
119
                                />
120
121
122
                                <Stack horizontal className='buttons'>
                                    <StackItem grow={12} className='download'>
                                        <PrimaryButton text='Download' onClick={this.downloadNNImanager} />
123
                                    </StackItem>
124
125
                                    <StackItem grow={12} className='close'>
                                        <DefaultButton text='Close' onClick={closeDrawer} />
126
127
                                    </StackItem>
                                </Stack>
128
129
130
                            </div>
                        </PivotItem>
                    </Pivot>
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
                </Panel>
            </Stack>
        );
    }

    private refresh = (): void => {
        window.clearTimeout(this.timerId);
        const dispatcherPromise = axios.get(`${DOWNLOAD_IP}/dispatcher.log`);
        const nniManagerPromise = axios.get(`${DOWNLOAD_IP}/nnimanager.log`);
        dispatcherPromise.then(res => {
            if (res.status === 200) {
                this.setState({ dispatcherLogStr: res.data });
            }
        });
        nniManagerPromise.then(res => {
            if (res.status === 200) {
                this.setState({ nniManagerLogStr: res.data });
            }
        });
        Promise.all([dispatcherPromise, nniManagerPromise]).then(() => {
            this.setState({ isLoading: false });
Lijiaoa's avatar
Lijiaoa committed
152
            this.timerId = window.setTimeout(this.refresh, 10000);
153
        });
154
    };
155
156
157
158

    private manualRefresh = (): void => {
        this.setState({ isLoading: true });
        this.refresh();
159
    };
160
161
162
}

export default LogDrawer;