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

Kai Reinhard
05.15.2019 e70f062550b0c611f076aa784f284a27e0ffe715
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
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 lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
 
public class JsonJob {
    @Getter
    @Setter
    private boolean cancellationRequested;
    @Getter
    @Setter
    private AbstractJob.Status status;
    @Getter
    @Setter
    private String title;
    @Getter
    @Setter
    private String description;
    @Getter
    @Setter
    private String progressText;
    @Getter
    @Setter
    private ProgressInfo progressInfo;
    @Getter
    @Setter
    private String commandLineAsString;
 
    public JsonJob() {
    }
 
    public JsonJob(BorgJob<?> borgJob) {
        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();
    }
 
    /**
     * 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.getMessage() != null) {
            sb.append(progressInfo.getMessage());
        }
        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(" ").append(percentage).append("%");
                }
            }
            sb.append(" (").append(UserUtils.formatNumber(progressInfo.getCurrent()));
            if (progressInfo.getTotal() > 0) {
                sb.append("/").append(UserUtils.formatNumber(progressInfo.getTotal()));
            }
            sb.append(")");
        }
        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;
    }
}