Skip to content

Commit 945c4b6

Browse files
committed
Add stats for huskelapp
1 parent 2d3ff5c commit 945c4b6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package no.nav.pto.veilarbportefolje.huskelapp;
2+
3+
import io.micrometer.core.instrument.Gauge;
4+
import io.micrometer.core.instrument.MeterRegistry;
5+
import io.micrometer.core.instrument.Tags;
6+
import io.micrometer.core.instrument.binder.MeterBinder;
7+
import lombok.NonNull;
8+
import lombok.RequiredArgsConstructor;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.beans.factory.annotation.Qualifier;
11+
import org.springframework.jdbc.core.JdbcTemplate;
12+
import org.springframework.scheduling.annotation.Scheduled;
13+
import org.springframework.stereotype.Component;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import java.util.concurrent.ConcurrentHashMap;
18+
19+
import static no.nav.pto.veilarbportefolje.database.PostgresTable.HUSKELAPP;
20+
21+
@Component
22+
@RequiredArgsConstructor
23+
@Slf4j
24+
public class HuskelappStats implements MeterBinder {
25+
26+
@Qualifier("PostgresJdbcReadOnly")
27+
private final JdbcTemplate jdbcTemplate;
28+
29+
Map<String, Integer> huskelappStats = new ConcurrentHashMap<>();
30+
31+
@Override
32+
public void bindTo(@NonNull MeterRegistry meterRegistry) {
33+
huskelappStats.keySet().forEach(enhet_id -> {
34+
Tags tags = Tags.of("enhet_id", enhet_id);
35+
Gauge.builder("huskelapp_antall", huskelappStats, map -> map.get(enhet_id))
36+
.tags(tags)
37+
.register(meterRegistry);
38+
});
39+
40+
}
41+
42+
@Scheduled(cron = "0 */6 * * 1-5")
43+
public void oppdaterMetrikk() {
44+
try {
45+
huskelappStats.clear();
46+
47+
String query = String.format("select %s, count(*) as huskelapp_antall from %s where %s = 'AKTIV' group by %s", HUSKELAPP.ENHET_ID, HUSKELAPP.TABLE_NAME, HUSKELAPP.STATUS, HUSKELAPP.ENHET_ID);
48+
Map<String, Integer> huskelappAntall = this.jdbcTemplate.queryForObject(query, (rs, rowNum) -> {
49+
Map<String, Integer> stats = new HashMap<>();
50+
while (rs.next()) {
51+
huskelappStats.put(rs.getString(HUSKELAPP.ENHET_ID), rs.getInt("huskelapp_antall"));
52+
}
53+
return stats;
54+
}
55+
);
56+
if (huskelappAntall != null) {
57+
huskelappStats.putAll(huskelappAntall);
58+
}
59+
} catch (Exception e) {
60+
log.error("Can not fetch huskelapp metrics");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)