mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
17.59.2021 c6e77f6fa462e292db5f693a33e7c483b5a6e19e
1
2
3
4
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
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
105
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
137
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
import React from 'react';
import {Button, Collapse} from 'reactstrap';
import {getRestServiceUrl, isDevelopmentMode} from '../../../utilities/global';
import JobQueue from './JobQueue';
import ErrorAlert from '../../general/ErrorAlert';
import PropTypes from 'prop-types';
 
class JobMonitorPanel extends React.Component {
    state = {
        isFetching: false,
        isFetchingOldJobs: false,
        testMode: false,
        collapseOldJobs: false
    };
 
    componentDidMount = () => {
        this.fetchQueues(false);
        this.interval = setInterval(() => this.fetchJobs(), 2000);
    };
 
    componentWillUnmount() {
        clearInterval(this.interval);
    }
 
    toggleTestMode() {
        this.setState({
            testMode: !this.state.testMode
        });
    }
 
    toggleOldJobs() {
        if (!this.state.collapseOldJobs) {
            this.fetchQueues(true);
        }
        this.setState({collapseOldJobs: !this.state.collapseOldJobs});
    }
 
    fetchJobs() {
        if (!this.state.isFetching) { // Don't run twice at the same time
            this.fetchQueues(false);
        }
        if (this.state.collapseOldJobs && !this.state.isFetchingOldJobs) {
            this.fetchQueues(true);
        }
    }
 
    fetchQueues = (oldJobs) => {
        let queuesVar = 'queues';
        let isFetchingVar = 'isFetching';
        let failedVar = 'failed';
        if (oldJobs) {
            queuesVar = 'oldJobsQueues';
            isFetchingVar = 'isFetchingOldJobs';
            failedVar = 'oldJobsFailed';
        }
        this.setState({
            [isFetchingVar]: true,
            [failedVar]: false
        });
        fetch(getRestServiceUrl('jobs', {
            repo: this.props.repo,
            testMode: this.state.testMode,
            oldJobs: oldJobs
        }), {
            method: 'GET',
            headers: {
                'Accept': 'application/json'
            }
        })
            .then(response => response.json())
            .then(json => {
                const queues = json.map(queue => {
                    return queue;
                });
                this.setState({
                    [isFetchingVar]: false,
                    [queuesVar]: queues
                });
            })
            .catch(() => this.setState({[isFetchingVar]: false, [failedVar]: true}));
    };
 
    render() {
        let content = '';
 
        if (this.state.isFetching && !this.state.queues) {
            content = <i>Loading...</i>;
        } else if (this.state.failed) {
            content = <ErrorAlert
                title={'Cannot load job queues'}
                description={'Something went wrong during contacting the rest api.'}
                action={{
                    handleClick: this.fetchQueues,
                    title: 'Try again'
                }}
            />;
        } else if (this.state.queues) {
            if (this.state.queues.length > 0) {
                content = <React.Fragment>
                    {this.state.queues
                        .map((queue) => <JobQueue
                            embedded={this.props.embedded}
                            queue={queue}
                            key={queue.repo}
                        />)}
                </React.Fragment>;
            } else {
                content = <React.Fragment>No jobs are running or queued.</React.Fragment>
            }
        }
        let oldJobs = '';
 
        if (this.state.isFetchingOldJobs && !this.state.oldJobsQueues) {
            oldJobs = <i>Loading...</i>;
        } else if (this.state.oldJobsFailed) {
            oldJobs = <ErrorAlert
                title={'Cannot load old job queues'}
                description={'Something went wrong during contacting the rest api.'}
                action={{
                    handleClick: this.fetchQueues,
                    title: 'Try again'
                }}
            />;
        } else if (this.state.oldJobsQueues) {
            if (this.state.oldJobsQueues.length > 0) {
                oldJobs = <React.Fragment>
                    {this.state.oldJobsQueues
                        .map((queue) => <JobQueue
                            embedded={this.props.embedded}
                            queue={queue}
                            key={queue.repo}
                        />)}
                </React.Fragment>
            } else {
                oldJobs = <React.Fragment>No old jobs available.</React.Fragment>
            }
        }
        let testContent = '';
        if (isDevelopmentMode() && !this.props.embedded) {
            testContent = <React.Fragment><br/><br/><br/><Button className="btn-outline-info" onClick={this.toggleTestMode}>Test mode</Button></React.Fragment>;
        }
 
        return <React.Fragment>
            {content}
            <h5 className={'onclick'} onClick={this.toggleOldJobs}>Show old jobs
            </h5>
            <Collapse isOpen={this.state.collapseOldJobs}>
                {oldJobs}
            </Collapse>
            {testContent}
        </React.Fragment>;
    }
 
    constructor(props) {
        super(props);
 
        this.fetchQueues = this.fetchQueues.bind(this);
        this.toggleTestMode = this.toggleTestMode.bind(this);
        this.toggleOldJobs = this.toggleOldJobs.bind(this);
    }
}
 
JobMonitorPanel
    .propTypes = {
    embedded: PropTypes.bool,
    repo: PropTypes.string
};
 
JobMonitorPanel
    .defaultProps = {
    embedded: true,
    repo: null
};
 
 
export default JobMonitorPanel;