-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add new workflow to remove old rocketchat sessions
- Loading branch information
Showing
13 changed files
with
124 additions
and
83 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
79 changes: 79 additions & 0 deletions
79
...ain/java/de/caritas/cob/userservice/api/adapters/rocketchat/RocketChatMongoDbService.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,79 @@ | ||
package de.caritas.cob.userservice.api.adapters.rocketchat; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import de.caritas.cob.userservice.api.adapters.rocketchat.model.RocketchatSession; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import lombok.NonNull; | ||
import org.bson.Document; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class RocketChatMongoDbService implements InitializingBean { | ||
|
||
@Value("${spring.data.mongodb.rocketchat.uri}") | ||
String mongoUrl; | ||
|
||
@Value("${spring.data.mongodb.rocketchat.database}") | ||
String databaseName; | ||
|
||
@NonNull MongoClient mongoClient; | ||
|
||
public List<RocketchatSession> findInactiveSessions() { | ||
MongoDatabase database = mongoClient.getDatabase(databaseName); | ||
MongoCollection<Document> collection = database.getCollection("rocketchat_sessions"); | ||
Document sessionInactivityCriteria = sessionInactivityCriteria(); | ||
List<RocketchatSession> rocketchatSessions = Lists.newArrayList(); | ||
collection | ||
.find(sessionInactivityCriteria) | ||
.forEach(document -> rocketchatSessions.add(new RocketchatSession(document))); | ||
return rocketchatSessions; | ||
} | ||
|
||
private Document sessionInactivityCriteria() { | ||
Document closedAt = new Document("closedAt", null); | ||
closedAt.put("lastActivityAt", new Document("$lt", "2021-01-01T00:00:00.000Z")); | ||
return closedAt; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
initializeMongoClient(); | ||
} | ||
|
||
private void initializeMongoClient() { | ||
if (mongoClient == null) { | ||
mongoClient = MongoClients.create(mongoUrl); | ||
} | ||
} | ||
|
||
public void patchClosedAt(RocketchatSession rocketchatSession) { | ||
MongoDatabase database = mongoClient.getDatabase(databaseName); | ||
MongoCollection<Document> collection = database.getCollection("rocketchat_sessions"); | ||
Document originalSession = | ||
collection.find(new Document("sessionId", rocketchatSession.getSessionId())).first(); | ||
|
||
Document patchedDocument = patchDocumentWithClosedAt(rocketchatSession, originalSession); | ||
collection.replaceOne(originalSession, patchedDocument); | ||
} | ||
|
||
private Document patchDocumentWithClosedAt( | ||
RocketchatSession rocketchatSession, Document document) { | ||
var patchedDocument = cloneDocument(document); | ||
patchedDocument.put("closedAt", rocketchatSession.getClosedAt()); | ||
return patchedDocument; | ||
} | ||
|
||
private Document cloneDocument(Document document) { | ||
return new Document( | ||
document.entrySet().stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...main/java/de/caritas/cob/userservice/api/adapters/rocketchat/model/RocketchatSession.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,23 @@ | ||
package de.caritas.cob.userservice.api.adapters.rocketchat.model; | ||
|
||
import java.time.Instant; | ||
import java.util.Date; | ||
import lombok.Data; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
@Document(collection = "rocketchat_sessions") | ||
@Data | ||
public class RocketchatSession { | ||
private Instant closedAt; | ||
|
||
private String sessionId; | ||
|
||
private String userId; | ||
|
||
public RocketchatSession(org.bson.Document document) { | ||
this.sessionId = document.getString("sessionId"); | ||
this.userId = document.getString("userId"); | ||
Date closedAtNullable = document.getDate("closedAt"); | ||
this.closedAt = closedAtNullable != null ? closedAtNullable.toInstant() : null; | ||
} | ||
} |
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
15 changes: 0 additions & 15 deletions
15
src/main/java/de/caritas/cob/userservice/api/model/RocketchatSession.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
src/main/java/de/caritas/cob/userservice/api/port/out/MongoRocketchatSessionRepository.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/main/java/de/caritas/cob/userservice/api/port/out/RocketchatSessionRepository.java
This file was deleted.
Oops, something went wrong.
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
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
25 changes: 0 additions & 25 deletions
25
src/test/java/de/caritas/cob/userservice/api/testConfig/MongoTestConfig.java
This file was deleted.
Oops, something went wrong.
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