Skip to content

Commit

Permalink
feat: cache skolengo homework in redis
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceh121 committed Nov 30, 2023
1 parent 627054c commit c2c52ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@
<artifactId>jackson-databind</artifactId>
<version>2.15.3</version>
</dependency>

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
</dependency>

<!--
https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/me/vinceh121/knb/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

public class Config {
private String token, dbUrl, feedbackChannelId;
private String token, dbUrl, feedbackChannelId, redisUri;
private int delay;
private Collection<Long> admins;
private MetricConfig metrics;
Expand Down Expand Up @@ -50,6 +50,14 @@ public void setFeedbackChannelId(final String feedbackChannelId) {
this.feedbackChannelId = feedbackChannelId;
}

public String getRedisUri() {
return redisUri;
}

public void setRedisUri(String redisUri) {
this.redisUri = redisUri;
}

@JsonProperty(required = false)
public MetricConfig getMetrics() {
return this.metrics;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/me/vinceh121/knb/Knb.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -63,6 +64,7 @@
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import redis.clients.jedis.JedisPool;

public class Knb {
private static final Logger LOG = LogManager.getLogger(Knb.class);
Expand All @@ -78,6 +80,7 @@ public class Knb {
private final JobDetail kdecoleJob, skolengoJob;
private final Map<String, AbstractCommand> cmdMap = new HashMap<>();
private final MetricRegistry metricRegistry = new MetricRegistry();
private final JedisPool redisPool;

public static void main(final String[] args) {
final Knb knb = new Knb();
Expand Down Expand Up @@ -115,6 +118,10 @@ public Knb() {
this.tableKdecoleInstances = r.table("kdecoleInstances");
this.tableSkolengoInstances = r.table("skolengoInstances");

Knb.LOG.info("Connecting to Redis");

this.redisPool = new JedisPool(URI.create(this.config.getRedisUri()));

Knb.LOG.info("Connecting to Discord");

final JDABuilder build = JDABuilder.create(this.config.getToken(), Knb.INTENTS);
Expand Down Expand Up @@ -393,6 +400,10 @@ public Connection getDbCon() {
return dbCon;
}

public JedisPool getRedisPool() {
return redisPool;
}

public Table getTableKdecoleInstances() {
return tableKdecoleInstances;
}
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/me/vinceh121/knb/SkolengoCheckingJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
Expand Down Expand Up @@ -37,9 +36,12 @@
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.MessageEmbed.Field;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

public class SkolengoCheckingJob implements Job {
public static final int COLOR_ARTICLE = 0xff7b1c;
public static final String HOMEWORK_REDIS_PREFIX = "skolengo.homework.";
private static final Logger LOG = LogManager.getLogger(SkolengoCheckingJob.class);
private Counter metricNewsCount, metricEmailsCount, metricGradesCount;
private Timer metricProcessTime;
Expand Down Expand Up @@ -83,14 +85,19 @@ public void execute(final JobExecutionContext context) throws JobExecutionExcept
if (u.getRelays().contains(RelayType.ARTICLES)) {
this.processArticles(knb, sko, info, u);
}

if (u.getRelays().contains(RelayType.EMAILS)) {
this.processEmails(knb, sko, info, u);
}

if (u.getRelays().contains(RelayType.NOTES)) {
this.processGrades(knb, sko, info, u);
}

if (u.getRelays().contains(RelayType.DEVOIRS)) {
this.processHomework(knb, sko, info, u);
try (Jedis jedis = knb.getRedisPool().getResource()) {
this.processHomework(knb, sko, info, u, jedis);
}
}

u.setLastCheck(new Date());
Expand Down Expand Up @@ -252,16 +259,16 @@ private void processArticles(final Knb knb, final JSkolengo sko, final StudentUs
}

private void processHomework(final Knb knb, final JSkolengo sko, final StudentUserInfo info,
final SkolengoUserInstance ui) {
final SkolengoUserInstance ui, final Jedis redis) {
final TextChannel chan = knb.getJda().getTextChannelById(ui.getChannelId());
final List<Homework> hws;

try {
// FIXME
hws = sko.fetchHomeworkAssignments(
LocalDate
.from(ui.getLastCheck().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()),
LocalDate.now()).stream().collect(Collectors.toList());
hws = sko.fetchHomeworkAssignments(LocalDate.now().minusWeeks(1), LocalDate.now().plusWeeks(1))
.stream()
.filter(hw -> hw.getDueDate().isAfter(LocalDate.now()))
.filter(hw -> redis.exists(HOMEWORK_REDIS_PREFIX + hw.getId()))
.collect(Collectors.toList());
} catch (final Exception e) {
SkolengoCheckingJob.LOG
.error(new FormattedMessage("Error while getting homework for instance {}", ui.getId()), e);
Expand All @@ -275,16 +282,24 @@ private void processHomework(final Knb knb, final JSkolengo sko, final StudentUs
return;
}

for (final Homework hw : hws) {
redis.set(HOMEWORK_REDIS_PREFIX + hw.getId(), "",
SetParams.setParams()
.nx()
.exAt(hw.getDueDate().plusDays(1).atStartOfDay().toEpochSecond(ZoneOffset.UTC)));
}

final String estabName = info.getSchool().getName();

final Date oldest = ui.getLastCheck(); // FIXME
final LocalDate oldest
= Collections.min(hws, (o1, o2) -> o1.getDueDate().compareTo(o2.getDueDate())).getDueDate();

final EmbedBuilder embBuild = new EmbedBuilder();

embBuild.setAuthor("Kdecole", "https://github.com/vinceh121/kdecole-notification-bot",
"https://cdn.discordapp.com/avatars/691655008076300339/4f492132883b1aa4f5984fe2eab9fa09.png");
embBuild.setColor(SkolengoCheckingJob.COLOR_ARTICLE);
embBuild.setTimestamp(oldest.toInstant());
embBuild.setTimestamp(oldest);
embBuild.setTitle("Nouveaux devoirs");
embBuild.setFooter(estabName);

Expand Down

0 comments on commit c2c52ce

Please sign in to comment.