-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a separate metric + db view for user score
- Loading branch information
Showing
6 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
api/src/main/java/dev/vrba/dubs/metrics/UserScoreMetric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
api/src/main/java/dev/vrba/dubs/repository/ScoreRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
58 changes: 58 additions & 0 deletions
58
api/src/main/resources/db/migration/V8__rework_score_leaderboard.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |