• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-wings / #125

31 Aug 2024 04:46AM UTC coverage: 63.579% (+0.7%) from 62.919%
#125

push

web-flow
Merge pull request #290 from trydofor/develop

3.2.130

1428 of 2191 new or added lines in 106 files covered. (65.18%)

41 existing lines in 24 files now uncovered.

12923 of 20326 relevant lines covered (63.58%)

0.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.3
/wings/slardar/src/main/java/pro/fessional/wings/slardar/monitor/viewer/LogViewer.java
1
package pro.fessional.wings.slardar.monitor.viewer;
2

3
import lombok.Getter;
4
import lombok.extern.slf4j.Slf4j;
5
import org.apache.commons.io.IOUtils;
6
import org.cache2k.Cache;
7
import org.jetbrains.annotations.NotNull;
8
import pro.fessional.mirana.id.Ulid;
9
import pro.fessional.wings.slardar.cache.cache2k.WingsCache2k;
10
import pro.fessional.wings.slardar.monitor.WarnFilter;
11
import pro.fessional.wings.slardar.monitor.WarnMetric;
12

13
import java.io.BufferedReader;
14
import java.io.File;
15
import java.io.FileInputStream;
16
import java.io.FileReader;
17
import java.io.IOException;
18
import java.io.OutputStream;
19
import java.util.ArrayList;
20
import java.util.Collection;
21
import java.util.HashSet;
22
import java.util.Iterator;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Set;
26
import java.util.regex.Pattern;
27

28
/**
29
 * @author trydofor
30
 * @since 2021-07-20
31
 */
32
@Slf4j
1✔
33
public class LogViewer implements WarnFilter {
34

35
    @Getter
36
    private final LogConf conf;
37
    private final Set<String> keys = new HashSet<>();
1✔
38
    private final Cache<String, String> cache;
39

40
    public LogViewer(@NotNull LogConf conf, @NotNull Collection<String> keys) {
1✔
41
        this.conf = conf;
1✔
42
        this.cache = WingsCache2k.builder(LogViewer.class, "cache", 2_000, conf.getAlive(), null, String.class, String.class).build();
1✔
43
        this.keys.addAll(keys);
1✔
44
    }
1✔
45

46
    public void view(String id, OutputStream output) throws IOException {
47
        if (id == null) return;
×
48
        final String log = cache.get(id);
×
49
        if (log == null) return;
×
50
        File file = new File(log);
×
51
        if (!file.canRead()) return;
×
52

53
        try (FileInputStream fis = new FileInputStream(file)) {
×
54
            final long len = conf.getLength().toBytes();
×
NEW
55
            IOUtils.copyLarge(fis, output, 0L, len);
×
56
            if (file.length() - len > 0) {
×
57
                final String more = String.format("\n\n...... %,d / %,d bytes", len, file.length());
×
NEW
58
                output.write(more.getBytes());
×
59
            }
60
        }
61
    }
×
62

63
    @Override
64
    public void filter(Map<String, List<WarnMetric.Warn>> warns) {
65
        List<WarnMetric.Warn> flt = new ArrayList<>();
×
66
        for (List<WarnMetric.Warn> list : warns.values()) {
×
67
            for (Iterator<WarnMetric.Warn> iter = list.iterator(); iter.hasNext(); ) {
×
68
                WarnMetric.Warn next = iter.next();
×
69
                if (next.getType() == WarnMetric.Type.File) {
×
70
                    if (canIgnoreHead(next.getWarn())) {
×
71
                        log.debug("remove ignored warning");
×
72
                        iter.remove();
×
73
                    }
74
                    else {
75
                        WarnMetric.Warn wd = new WarnMetric.Warn();
×
76
                        wd.setType(WarnMetric.Type.Link);
×
77
                        wd.setKey(next.getKey());
×
78
                        wd.setRule(next.getRule());
×
79
                        final String id = Ulid.next();
×
80
                        cache.put(id, next.getWarn());
×
81
                        wd.setWarn(conf.getDomain() + conf.getMapping() + "?id=" + id);
×
82
                        flt.add(wd);
×
83
                    }
84
                }
85
            }
×
86
        }
×
87

88
        warns.entrySet().removeIf(it -> it.getValue().isEmpty());
×
89

90
        if (!flt.isEmpty()) {
×
91
            final List<WarnMetric.Warn> old = warns.get(LogConf.Key);
×
92
            if (old == null) {
×
93
                warns.put(LogConf.Key, flt);
×
94
            }
95
            else {
96
                old.addAll(flt);
×
97
            }
98
        }
99
    }
×
100

101
    protected boolean canIgnoreHead(String out) {
102
        final Collection<String> ignores = conf.getIgnore().values();
1✔
103
        if (ignores.isEmpty()) return false;
1✔
104

105
        long max = conf.getLength().toBytes();
1✔
106
        final File file = new File(out);
1✔
107
        if (file.length() > max || !file.canRead()) return false;
1✔
108

109
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
1✔
110
            String line;
111
            int tol = 0;
1✔
112
            int cnt = 0;
1✔
113
            final Pattern head = conf.getHeader();
1✔
114
            while ((line = reader.readLine()) != null && max > 0) {
1✔
115
                max -= line.length() + 1; // loose calculation
1✔
116

117
                if (ignoreLine(line, head)) {
1✔
118
                    continue;
1✔
119
                }
120

121
                // only match header line
122
                tol++;
1✔
123
                for (String s : ignores) {
1✔
124
                    if (line.contains(s)) {
1✔
125
                        cnt++;
1✔
126
                        break;
1✔
127
                    }
128
                }
1✔
129
            }
130
            return tol == cnt;
1✔
131
        }
132
        catch (Exception e) {
×
NEW
133
            return false;
×
134
        }
135
    }
136

137
    private boolean ignoreLine(String line, Pattern head) {
138
        if (line.isEmpty()) return true;
1✔
139

140
        if (head != null && !head.matcher(line).find()) {
1✔
141
            return true;
1✔
142
        }
143

144
        for (String key : keys) {
1✔
145
            if (line.contains(key)) {
1✔
146
                return false;
1✔
147
            }
148
        }
1✔
149

150
        return true;
1✔
151
    }
152
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc