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

Kai Reinhard
18.44.2021 6d56424f36c8eb2bdf2976fa405a8c3f544e1d44
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package de.micromata.borgbutler.server.rest.queue;
 
import com.fasterxml.jackson.annotation.JsonProperty;
import de.micromata.borgbutler.BorgJob;
import de.micromata.borgbutler.jobs.AbstractJob;
import de.micromata.borgbutler.json.borg.ProgressInfo;
import de.micromata.borgbutler.server.user.UserUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
 
public class JsonJob {
    private boolean cancellationRequested;
    private AbstractJob.Status status;
    private String title;
    private String description;
    private String progressText;
    private ProgressInfo progressInfo;
    private String commandLineAsString;
    private long uniqueJobNumber;
    private String[] environmentVariables;
    private String createTime;
    private String startTime;
    private String stopTime;
 
    public JsonJob() {
    }
 
    public JsonJob(BorgJob<?> borgJob) {
        this.uniqueJobNumber = borgJob.getUniqueJobNumber();
        this.cancellationRequested = borgJob.isCancellationRequested();
        this.status = borgJob.getStatus();
        this.title = borgJob.getTitle();
        ProgressInfo progressInfo = borgJob.getProgressInfo();
        if (progressInfo != null) {
            this.progressInfo = progressInfo;
            buildProgressText();
        }
        this.commandLineAsString = borgJob.getCommandLineAsString();
        this.description = borgJob.getDescription();
        environmentVariables = borgJob.getCommand().getRepoConfig().getEnvironmentVariables();
        this.createTime = borgJob.getCreateTime();
        this.startTime = borgJob.getStartTime();
        this.stopTime = borgJob.getStopTime();
    }
 
    /**
     * Builds and sets progressText from the progressInfo object if given.
     *
     * @return progressText
     */
    public String buildProgressText() {
        if (progressInfo == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        if (progressInfo.getCurrent() > 0) {
            if (StringUtils.indexOf(progressInfo.getMessage(), '%') < 0) {
                // No percentage given by borg, try to create an own one:
                short percentage = getProgressPercent();
                if (percentage >= 0) {
                    sb.append(percentage).append("%");
                }
            }
            sb.append(" (");
            if ("extract".equals(progressInfo.getMsgid())) {
                sb.append(FileUtils.byteCountToDisplaySize(progressInfo.getCurrent()));
            } else {
                sb.append(UserUtils.formatNumber(progressInfo.getCurrent()));
            }
            if (progressInfo.getTotal() > 0) {
                sb.append("/");
                if ("extract".equals(progressInfo.getMsgid())) {
                    sb.append(FileUtils.byteCountToDisplaySize(progressInfo.getTotal()));
                } else {
 
                    sb.append(UserUtils.formatNumber(progressInfo.getTotal()));
                }
            }
            sb.append("): ");
        }
        if (progressInfo.getMessage() != null) {
            sb.append(progressInfo.getMessage());
        }
        if (progressInfo.isFinished()) {
            sb.append(" (finished)");
        }
        progressText = sb.toString();
        return progressText;
    }
 
    /**
     * If current and total of {@link ProgressInfo} is available, this value is given, otherwise this value is -1.
     */
    @JsonProperty
    public short getProgressPercent() {
        if (progressInfo == null || progressInfo.getTotal() <= 0) {
            return -1;
        }
        long value = 100 * progressInfo.getCurrent() / progressInfo.getTotal();
        if (value < 0) {
            return 0;
        }
        if (value >= 100) {
            return 100;
        }
        return (short) value;
    }
 
    public boolean isCancellationRequested() {
        return this.cancellationRequested;
    }
 
    public AbstractJob.Status getStatus() {
        return this.status;
    }
 
    public String getTitle() {
        return this.title;
    }
 
    public String getDescription() {
        return this.description;
    }
 
    public String getProgressText() {
        return this.progressText;
    }
 
    public ProgressInfo getProgressInfo() {
        return this.progressInfo;
    }
 
    public String getCommandLineAsString() {
        return this.commandLineAsString;
    }
 
    public long getUniqueJobNumber() {
        return this.uniqueJobNumber;
    }
 
    public String[] getEnvironmentVariables() {
        return this.environmentVariables;
    }
 
    public String getCreateTime() {
        return this.createTime;
    }
 
    public String getStartTime() {
        return this.startTime;
    }
 
    public String getStopTime() {
        return this.stopTime;
    }
 
    public JsonJob setCancellationRequested(boolean cancellationRequested) {
        this.cancellationRequested = cancellationRequested;
        return this;
    }
 
    public JsonJob setStatus(AbstractJob.Status status) {
        this.status = status;
        return this;
    }
 
    public JsonJob setTitle(String title) {
        this.title = title;
        return this;
    }
 
    public JsonJob setDescription(String description) {
        this.description = description;
        return this;
    }
 
    public JsonJob setProgressText(String progressText) {
        this.progressText = progressText;
        return this;
    }
 
    public JsonJob setProgressInfo(ProgressInfo progressInfo) {
        this.progressInfo = progressInfo;
        return this;
    }
 
    public JsonJob setCommandLineAsString(String commandLineAsString) {
        this.commandLineAsString = commandLineAsString;
        return this;
    }
 
    public JsonJob setUniqueJobNumber(long uniqueJobNumber) {
        this.uniqueJobNumber = uniqueJobNumber;
        return this;
    }
 
    public JsonJob setEnvironmentVariables(String[] environmentVariables) {
        this.environmentVariables = environmentVariables;
        return this;
    }
 
    public JsonJob setCreateTime(String createTime) {
        this.createTime = createTime;
        return this;
    }
 
    public JsonJob setStartTime(String startTime) {
        this.startTime = startTime;
        return this;
    }
 
    public JsonJob setStopTime(String stopTime) {
        this.stopTime = stopTime;
        return this;
    }
}