Skip to content

Commit

Permalink
Use Cassandra to persist notes
Browse files Browse the repository at this point in the history
  • Loading branch information
geir-eilertsen committed Dec 12, 2024
1 parent 02d938a commit d12fd51
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class OpenhabSseEventReceiver {

private static final Log LOG = LogFactory.getLog(OpenhabSseEventReceiver.class);
private static final int DEBOUNCE_TIME_MS = 2000; // Debounce time in milliseconds
private static final int DEBOUNCE_TIME_MS = 3000; // Debounce time in milliseconds

@Resource
private ObserveUseCase observeUseCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ spring:
ai:
vectorstore:
cassandra:
initialize-schema: true
initialize-schema: true
1 change: 1 addition & 0 deletions marvin.interaction.web/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ spring:
contact-points: ${VECTORDB_ADDRESS}
password: admin
username: admin
schema-action: create_if_not_exists

server:
port: 9090
Expand Down
4 changes: 4 additions & 0 deletions marvin.persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
</dependencies>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.assetvisor.marvin.persistence.adapters.cassandra;

import java.time.Instant;
import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;

@Table
public class NoteBookEntry {

@Id
@PrimaryKeyColumn(
name = "id",
type = PrimaryKeyType.PARTITIONED
)
private UUID id;
@Column(
"created_date"
)
private Instant createdDate;
@Column(
"note_date"
)
private Instant noteDate;
@Column(
"note"
)
private String note;

public NoteBookEntry() {
}

public NoteBookEntry(UUID id, Instant createdDate, Instant noteDate, String note) {
this.id = id;
this.createdDate = createdDate;
this.noteDate = noteDate;
this.note = note;
}

public UUID getId() {
return id;
}

public void setId(UUID id) {
this.id = id;
}

public Instant getCreatedDate() {
return createdDate;
}

public void setCreatedDate(Instant createdDate) {
this.createdDate = createdDate;
}

public Instant getNoteDate() {
return noteDate;
}

public void setNoteDate(Instant noteDate) {
this.noteDate = noteDate;
}

public String getNote() {
return note;
}

public void setNote(String note) {
this.note = note;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.assetvisor.marvin.persistence.adapters.cassandra;

import java.util.UUID;
import org.springframework.data.cassandra.repository.CassandraRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface NotebookRepository extends CassandraRepository<NoteBookEntry, UUID> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.assetvisor.marvin.persistence.adapters.cassandra;

import com.assetvisor.marvin.equipment.notebook.CalendarNote;
import com.assetvisor.marvin.equipment.notebook.ForPersistingNotes;
import jakarta.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.Optional;
import java.util.UUID;
import org.springframework.stereotype.Component;

@Component
//@Profile("cassandra")
public class PersistingNotesCassandraAdapter implements ForPersistingNotes {

@Resource
private NotebookRepository notebookRepository;

@Override
public void persist(CalendarNote calendarNote) {
notebookRepository.save(toNoteBookEntry(calendarNote));
}

@Override
public Optional<CalendarNote> getFirstOverdueAndDelete() {
Optional<NoteBookEntry> first = notebookRepository.findAll().stream()
.sorted(Comparator.comparing(NoteBookEntry::getNoteDate))
.filter(note -> note.getNoteDate().isBefore(LocalDateTime.now().toInstant(ZoneOffset.UTC)))
.findFirst();

if(first.isPresent()) {
notebookRepository.deleteById(first.get().getId());
return Optional.of(toCalendarNote(first.get()));
} else {
return Optional.empty();
}
}

private NoteBookEntry toNoteBookEntry(CalendarNote calendarNote) {
return new NoteBookEntry(
UUID.randomUUID(),
Instant.now().atOffset(ZoneOffset.UTC).toInstant(),
Instant.from(calendarNote.noteDate().atOffset(ZoneOffset.UTC).toInstant()),
calendarNote.note()
);
}

private CalendarNote toCalendarNote(NoteBookEntry noteBookEntry) {
return new CalendarNote(
LocalDateTime.from(noteBookEntry.getNoteDate().atOffset(ZoneOffset.UTC)),
noteBookEntry.getNote()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.assetvisor.marvin.persistence.adapters.file;

import com.assetvisor.marvin.robot.domain.environment.EnvironmentDescription;
import com.assetvisor.marvin.robot.domain.environment.ForPersistingEnvironmentDescriptions;
import com.assetvisor.marvin.robot.domain.jobdescription.ForPersistingRobotDescription;
import com.assetvisor.marvin.robot.domain.jobdescription.RobotDescription;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component
public class PersistingLocalFileAdapter implements ForPersistingRobotDescription, ForPersistingEnvironmentDescriptions {

private final Log LOG = LogFactory.getLog(getClass());

@Override
public RobotDescription read() {
String userHome = System.getProperty("user.home");
Path filePath = Paths.get(userHome, "marvin-robot.txt");

try {
String content = Files.readString(filePath);
LOG.info("Read robot description from file: " + filePath);
return new RobotDescription(content);
} catch (IOException e) {
LOG.error("Error reading file: " + e.getMessage());
}
return new RobotDescription("");
}

@Override
public List<EnvironmentDescription> load() {
String userHome = System.getProperty("user.home");
Path filePath = Paths.get(userHome, "marvin-environments.txt");

try {
List<String> lines = Files.readAllLines(filePath);
LOG.info("Read environment descriptions from file: " + filePath);

return lines.stream()
.map(line -> {
try {
return new EnvironmentDescription(line);
} catch (Exception e) {
LOG.error("Error parsing line: " + line + " - " + e.getMessage());
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());

} catch (IOException e) {
LOG.error("Error reading environments file: " + e.getMessage());
}

return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

import java.time.LocalDateTime;

public record CalendarNote(LocalDateTime noteDate, String note) {

}
public record CalendarNote(LocalDateTime noteDate, String note) {}
Loading

0 comments on commit d12fd51

Please sign in to comment.