GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

io.mcarle.sciurus.monitor.LoggingMonitor.abbreviationOfChronoUnit(ChronoUnit)   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 4
rs 9.95
1 1
package io.mcarle.sciurus.monitor;
2
3
import io.mcarle.sciurus.annotation.LoggingMonitorParams;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6
7
import java.lang.reflect.Method;
8
import java.time.Duration;
9
import java.time.temporal.ChronoUnit;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.List;
13
import java.util.function.BiConsumer;
14
import java.util.stream.Stream;
15
16
public enum LoggingMonitor implements CustomMonitor {
17 1
    INSTANCE;
18
19 1
    private final static Logger LOG = LoggerFactory.getLogger(LoggingMonitor.class);
20
    private final static String BASE_LOG_TEMPLATE = "({} {}) - {}#{}";
21
    private final static String LOG_TEMPLATE_PARAMS = "({})";
22 1
    private final static String LOG_TEMPLATE_RESULT = " : {}";
23
24
    private static BiConsumer<String, Object[]> getLogger(final LoggingMonitorParams monitorParams, final Duration duration) {
25 1
        if (monitorParams.warnLimit() > 0) {
26 1
            if (Duration.of(monitorParams.warnLimit(), monitorParams.warnLimitUnit()).compareTo(duration) < 0) {
27 1
                return LOG::warn;
28
            }
29
        }
30 1
        return LOG::debug;
31
    }
32
33
    @SuppressWarnings("rawtypes")
34
    private static Object extractMethodResult(
35
            final Throwable throwable,
36
            final Class returnType,
37
            final Object returnValue
38
    ) {
39 1
        return throwable != null
40 1
                ? throwable.toString()
41 1
                : (returnType == Void.TYPE ? "void" : returnValue);
42
    }
43
44
    private static String extractLogTemplate(final LoggingMonitorParams monitorParams) {
45 1
        String result = BASE_LOG_TEMPLATE;
46 1
        if (monitorParams.logParameter()) {
47 1
            result += LOG_TEMPLATE_PARAMS;
48
        }
49 1
        if (monitorParams.logResult()) {
50 1
            result += LOG_TEMPLATE_RESULT;
51
        }
52 1
        return result;
53
    }
54
55
    private static Object[] extractLogParams(
56
            final LoggingMonitorParams monitorParams,
57
            final Duration duration,
58
            final String className,
59
            final String methodName,
60
            final Object[] methodArgs,
61
            final Object methodResult
62
    ) {
63 1
        ChronoUnit outputUnit = smallestUnitWithValueMaximal(duration);
64
65 1
        List<Object> result = new ArrayList<>(
66 1
                Arrays.asList(
67 1
                        String.format("%4s", getDurationIn(duration, outputUnit)),
68 1
                        abbreviationOfChronoUnit(outputUnit),
69 1
                        className,
70 1
                        methodName
71
                )
72
        );
73 1
        if (monitorParams.logParameter()) {
74 1
            result.add(methodArgs);
75
        }
76 1
        if (monitorParams.logResult()) {
77 1
            result.add(methodResult);
78
        }
79 1
        return result.toArray();
80
    }
81
82
    private static String abbreviationOfChronoUnit(ChronoUnit unit) {
83 1
        if (unit.equals(ChronoUnit.SECONDS)) {
84 1
            return "s ";
85 1
        } else if (unit.equals(ChronoUnit.MILLIS)) {
86 1
            return "ms";
87 1
        } else if (unit.equals(ChronoUnit.MICROS)) {
88 1
            return "µs";
89
        } else {
90 1
            return "ns";
91
        }
92
    }
93
94
    private static ChronoUnit smallestUnitWithValueMaximal(Duration duration) {
95 1
        return Stream.of(ChronoUnit.NANOS, ChronoUnit.MICROS, ChronoUnit.MILLIS, ChronoUnit.SECONDS)
96 1
                .filter(unit -> getDurationIn(duration, unit) < 9999)
97 1
                .findFirst()
98 1
                .orElse(ChronoUnit.SECONDS);
99
    }
100
101
    private static long getDurationIn(Duration duration, ChronoUnit unit) {
102 1
        if (unit == ChronoUnit.MICROS) {
103 1
            return (duration.toNanos() + 500) / 1_000;
104 1
        } else if (unit == ChronoUnit.MILLIS) {
105 1
            return (duration.toNanos() + 500_000) / 1_000_000;
106 1
        } else if (unit == ChronoUnit.SECONDS) {
107 1
            return (duration.toNanos() + 500_000_000) / 1_000_000_000;
108
        } else {
109 1
            return duration.toNanos();
110
        }
111
    }
112
113
    @SuppressWarnings("rawtypes")
114
    @Override
115
    public void monitored(
116
            Duration duration,
117
            String declaringTypeName,
118
            Method method,
119
            Object[] methodArgs,
120
            Throwable throwable,
121
            Class returnType,
122
            Object returnValue
123
    ) {
124 1
        LoggingMonitorParams loggingParams = getLoggingMonitorParams(method);
125 1
        getLogger(loggingParams, duration)
126 1
                .accept(
127 1
                        extractLogTemplate(loggingParams),
128 1
                        extractLogParams(
129 1
                                loggingParams,
130 1
                                duration,
131 1
                                declaringTypeName,
132 1
                                method.getName(),
133 1
                                methodArgs,
134 1
                                extractMethodResult(throwable, returnType, returnValue)
135
                        )
136
                );
137 1
    }
138
139
    private LoggingMonitorParams getLoggingMonitorParams(Method method) {
140 1
        LoggingMonitorParams params = method.getAnnotation(LoggingMonitorParams.class);
141 1
        if (params == null) {
142 1
            params = method.getDeclaringClass().getAnnotation(LoggingMonitorParams.class);
143 1
            if (params == null) {
144 1
                params = LoggingMonitorParamsAnnotationClass.class.getAnnotation(LoggingMonitorParams.class);
145
            }
146
        }
147 1
        return params;
148
    }
149
150
    @LoggingMonitorParams
151 1
    static class LoggingMonitorParamsAnnotationClass {
152
    }
153
}
154