Skip to content

Commit

Permalink
Add a separate metric + db view for user score
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkavrba committed Oct 5, 2024
1 parent 4203249 commit e5e7311
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 8 deletions.
3 changes: 0 additions & 3 deletions api/src/main/java/dev/vrba/dubs/domain/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,4 @@ public class Match {

@MappedProperty("count")
private BigInteger count;

@MappedProperty("points")
private BigInteger points;
}
43 changes: 43 additions & 0 deletions api/src/main/java/dev/vrba/dubs/domain/Score.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.vrba.dubs.domain;

import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
import io.micronaut.data.annotation.MappedProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.math.BigInteger;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@MappedEntity("score")
public class Score {
@Id
@MappedProperty("row_id")
private Integer id;

@MappedProperty("user_id")
private String userId;

@MappedProperty("user_name")
private String userName;

@MappedProperty("channel_id")
private String channelId;

@MappedProperty("channel_name")
private String channelName;

@MappedProperty("guild_id")
private String guildId;

@MappedProperty("guild_name")
private String guildName;

@MappedProperty("score")
private BigInteger score;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import java.util.stream.Collectors;

@Singleton
public class MatchesMetricConfiguration {
public class MatchesMetric {

@NonNull
private final MultiGauge gauge;

@NonNull
private final MatchRepository repository;

public MatchesMetricConfiguration(
public MatchesMetric(
final @NonNull MeterRegistry registry,
final @NonNull MatchRepository repository
) {
Expand Down Expand Up @@ -51,9 +51,7 @@ private MultiGauge.Row<Number> mapPatternToGaugeRow(final @NonNull Match match)
Tag.of("guild.name", match.getGuildName()),
Tag.of("pattern.name", match.getPatternName()),
Tag.of("pattern.rare", match.getPatternIsRare().toString()),
Tag.of("pattern.points", match.getPatternPoints().toString()),
Tag.of("points", match.getPoints().toString())

Tag.of("pattern.points", match.getPatternPoints().toString())
),
match.getCount()
);
Expand Down
56 changes: 56 additions & 0 deletions api/src/main/java/dev/vrba/dubs/metrics/UserScoreMetric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.vrba.dubs.metrics;

import dev.vrba.dubs.domain.Score;
import dev.vrba.dubs.repository.ScoreRepository;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MultiGauge;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.scheduling.annotation.Scheduled;
import jakarta.inject.Singleton;

import java.util.stream.Collectors;

@Singleton
public class UserScoreMetric {

@NonNull
private final MultiGauge gauge;

@NonNull
private final ScoreRepository repository;

public UserScoreMetric(
final @NonNull MeterRegistry registry,
final @NonNull ScoreRepository repository
) {
this.gauge = MultiGauge.builder("users.score").register(registry);
this.repository = repository;
}

@Scheduled(fixedRate = "PT1M")
public void refresh() {
gauge.register(
repository.findAll()
.stream()
.map(this::mapPatternToGaugeRow)
.collect(Collectors.toList()),
true
);
}

private MultiGauge.Row<Number> mapPatternToGaugeRow(final @NonNull Score score) {
return MultiGauge.Row.of(
Tags.of(
Tag.of("user.id", score.getUserId()),
Tag.of("user.name", score.getUserName()),
Tag.of("channel.id", score.getChannelId()),
Tag.of("channel.name", score.getChannelName()),
Tag.of("guild.id", score.getGuildId()),
Tag.of("guild.name", score.getGuildName())
),
score.getScore()
);
}
}
10 changes: 10 additions & 0 deletions api/src/main/java/dev/vrba/dubs/repository/ScoreRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.vrba.dubs.repository;

import dev.vrba.dubs.domain.Score;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface ScoreRepository extends CrudRepository<Score, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
drop view if exists matches;
create or replace view matches as
(
select row_number() over (order by users.id) as row_id,
pattern_name,
pattern_points,
pattern_is_rare,
users.id as user_id,
users.name as user_name,
channels.id as channel_id,
channels.name as channel_name,
guilds.id as guild_id,
guilds.name as guild_name,
count(*) as count
from matched_patterns
left join users on matched_patterns.user_id = users.id
left join channels on matched_patterns.channel_id = channels.id
left join guilds on channels.guild_id = guilds.id
group by users.id,
channels.id,
guilds.id,
pattern_name,
pattern_points,
pattern_is_rare
);

create or replace view score as
(
select row_number() over (order by user_id) as row_id,
user_id,
user_name,
channel_id,
channel_name,
guild_id,
guild_name,
sum(pattern_points * count) as score
from (select users.id as user_id,
users.name as user_name,
channels.id as channel_id,
channels.name as channel_name,
guilds.id as guild_id,
guilds.name as guild_name,
pattern_points as pattern_points,
count(*) as count
from matched_patterns
left join users on matched_patterns.user_id = users.id
left join channels on matched_patterns.channel_id = channels.id
left join guilds on channels.guild_id = guilds.id
group by users.id,
channels.id,
guilds.id,
pattern_points)
group by user_id,
user_name,
channel_id,
channel_name,
guild_id,
guild_name);

0 comments on commit e5e7311

Please sign in to comment.