Skip to content

Commit 3874759

Browse files
committed
Persist the last import timestamp so that we can get messages posted if the server is not running.
1 parent 899ecad commit 3874759

File tree

5 files changed

+77
-15
lines changed

5 files changed

+77
-15
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.objectcomputing.checkins.services.slack.kudos;
2+
3+
import io.micronaut.core.annotation.Introspected;
4+
import io.micronaut.data.annotation.TypeDef;
5+
import io.micronaut.data.model.DataType;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
8+
import jakarta.persistence.Column;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.Table;
12+
import jakarta.validation.constraints.NotNull;
13+
14+
import lombok.Getter;
15+
import lombok.Setter;
16+
17+
import java.time.LocalDateTime;
18+
19+
@Entity
20+
@Getter
21+
@Setter
22+
@Introspected
23+
@Table(name = "automated_kudos_read_time")
24+
public class KudosChannelReadTime {
25+
static final public String key = "Singleton";
26+
27+
@Id
28+
@Column(name = "id")
29+
@Schema(description = "the id of the kudos channel read time")
30+
private String id;
31+
32+
@NotNull
33+
@Column(name = "readtime")
34+
@TypeDef(type = DataType.TIMESTAMP)
35+
@Schema(description = "date the kudos were created")
36+
private LocalDateTime readTime;
37+
38+
public KudosChannelReadTime() {
39+
id = key;
40+
readTime = LocalDateTime.now();
41+
}
42+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.objectcomputing.checkins.services.slack.kudos;
2+
3+
import io.micronaut.data.jdbc.annotation.JdbcRepository;
4+
import io.micronaut.data.model.query.builder.sql.Dialect;
5+
import io.micronaut.data.repository.CrudRepository;
6+
7+
@JdbcRepository(dialect = Dialect.POSTGRES)
8+
public interface KudosChannelReadTimeStore extends CrudRepository<KudosChannelReadTime, String> {
9+
}

server/src/main/java/com/objectcomputing/checkins/services/slack/kudos/KudosChannelReader.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import jakarta.inject.Inject;
1515

1616
import java.util.List;
17+
import java.util.Optional;
1718
import java.time.LocalDateTime;
1819

1920
@Singleton
2021
public class KudosChannelReader {
2122
private static final Logger LOG = LoggerFactory.getLogger(KudosChannelReader.class);
2223

23-
private static LocalDateTime lastImport = null;
24+
@Inject
25+
private KudosChannelReadTimeStore kudosChannelReadTimeStore;
2426

2527
@Inject
2628
private CheckInsConfiguration configuration;
@@ -33,23 +35,23 @@ public class KudosChannelReader {
3335

3436
@Scheduled(fixedDelay = "10m")
3537
public void readChannel() {
36-
if (lastImport == null) {
37-
lastImport = LocalDateTime.now();
38-
}
38+
Optional<KudosChannelReadTime> readTime =
39+
kudosChannelReadTimeStore.findById(KudosChannelReadTime.key);
40+
boolean present = readTime.isPresent();
41+
LocalDateTime lastImport = present ? readTime.get().getReadTime()
42+
: LocalDateTime.now();
3943

4044
String channelId = configuration.getApplication()
4145
.getSlack().getKudosChannel();
46+
LOG.info("Reading messages from " + channelId +
47+
" as of " + lastImport.toString());
4248
List<Message> messages = slackReader.read(channelId, lastImport);
43-
updateLastImportTime();
49+
if (present) {
50+
kudosChannelReadTimeStore.update(new KudosChannelReadTime());
51+
} else {
52+
kudosChannelReadTimeStore.save(new KudosChannelReadTime());
53+
}
4454

4555
slackKudosCreator.store(messages);
4656
}
47-
48-
private LocalDateTime getLastImportTime() {
49-
return lastImport;
50-
}
51-
52-
private void updateLastImportTime() {
53-
lastImport = LocalDateTime.now();
54-
}
5557
}

server/src/main/java/com/objectcomputing/checkins/services/slack/kudos/SlackKudosCreator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public class SlackKudosCreator {
5050

5151
public void store(List<Message> messages) {
5252
for (Message message : messages) {
53-
if (message.getSubtype() == null &&
54-
message.getText().toLowerCase().contains("kudos")) {
53+
if (message.getSubtype() == null) {
5554
try {
5655
AutomatedKudosDTO kudosDTO = createFromMessage(message);
5756
if (kudosDTO.getRecipientIds().size() == 0) {
@@ -64,6 +63,8 @@ public void store(List<Message> messages) {
6463
} catch (Exception ex) {
6564
LOG.error("store: " + ex.toString());
6665
}
66+
} else {
67+
LOG.info("Skipping message: " + message.getText());
6768
}
6869
}
6970

server/src/main/resources/db/common/V121__automated_kudos_table.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ CREATE TABLE automated_kudos
99
senderid varchar REFERENCES member_profile (id),
1010
recipientids varchar[]
1111
);
12+
13+
DROP TABLE IF EXISTS automated_kudos_read_time;
14+
15+
CREATE TABLE automated_kudos_read_time
16+
(
17+
id varchar PRIMARY KEY,
18+
readtime timestamp
19+
);

0 commit comments

Comments
 (0)