diff --git a/packages/server/src/main/java/db/migration/V13__NotificationEventProgress.java b/packages/server/src/main/java/db/migration/V13__NotificationEventProgress.java new file mode 100644 index 0000000000..3a6437f6c2 --- /dev/null +++ b/packages/server/src/main/java/db/migration/V13__NotificationEventProgress.java @@ -0,0 +1,70 @@ +package db.migration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.UUID; +import org.flywaydb.core.api.migration.BaseJavaMigration; +import org.flywaydb.core.api.migration.Context; + +public class V13__NotificationEventProgress extends BaseJavaMigration { + + ObjectMapper objectMapper = new ObjectMapper(); + + private String mapToString(final int progress) { + switch (progress) { + case 0: + return "CANCELLED"; + case 1: + return "COMPLETE"; + case 2: + return "ERROR"; + case 3: + return "FAILED"; + case 4: + return "QUEUED"; + case 5: + return "RETRIEVING"; + case 6: + return "RUNNING"; + } + return null; + } + + @Override + public void migrate(final Context context) throws Exception { + try (var statement = context.getConnection().createStatement()) { + + // Rename transform to transform_old + statement.execute("ALTER TABLE notification_event RENAME COLUMN state TO state_old;"); + + // Create new json column transform + statement.execute("ALTER TABLE notification_event ADD COLUMN state varchar(255);"); + + // Select all rows from the simulation_result_files table + final ResultSet resultSet = statement.executeQuery( + "SELECT id, state_old FROM notification_event WHERE state_old IS NOT NULL ORDER BY id;"); + + // Prepare the update statement for the simulation table + final PreparedStatement preparedStatement = + context.getConnection().prepareStatement("UPDATE notification_event SET state = ? WHERE id = ?;"); + + // Iterate through the result set + while (resultSet.next()) { + // Get the id and result_files + final UUID id = (UUID) resultSet.getObject("id"); + final int state = resultSet.getInt("state_old"); + + // Set the parameters for the update statement + preparedStatement.setString(1, mapToString(state)); + preparedStatement.setObject(2, id); + + // Execute the update statement + preparedStatement.executeUpdate(); + } + + // Create new json column transform + statement.execute("ALTER TABLE notification_event DROP COLUMN state_old"); + } + } +} diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/notification/NotificationEvent.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/notification/NotificationEvent.java index 0d37f5e150..bb7dbaf4ef 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/notification/NotificationEvent.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/models/notification/NotificationEvent.java @@ -7,6 +7,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.ManyToOne; import jakarta.validation.constraints.NotNull; import java.io.Serial; @@ -30,6 +32,8 @@ public class NotificationEvent extends TerariumEntity { private static final long serialVersionUID = -3382397588627700379L; private Double progress = 0.0; + + @Enumerated(EnumType.STRING) private ProgressState state = null; @ManyToOne diff --git a/packages/server/src/test/java/software/uncharted/terarium/hmiserver/service/notification/NotificationServiceTests.java b/packages/server/src/test/java/software/uncharted/terarium/hmiserver/service/notification/NotificationServiceTests.java index 3bff21a804..ca44d28550 100644 --- a/packages/server/src/test/java/software/uncharted/terarium/hmiserver/service/notification/NotificationServiceTests.java +++ b/packages/server/src/test/java/software/uncharted/terarium/hmiserver/service/notification/NotificationServiceTests.java @@ -15,6 +15,7 @@ import software.uncharted.terarium.hmiserver.configuration.MockUser; import software.uncharted.terarium.hmiserver.models.ClientEvent; import software.uncharted.terarium.hmiserver.models.ClientEventType; +import software.uncharted.terarium.hmiserver.models.dataservice.simulation.ProgressState; import software.uncharted.terarium.hmiserver.models.extractionservice.ExtractionStatusUpdate; import software.uncharted.terarium.hmiserver.models.notification.NotificationEvent; import software.uncharted.terarium.hmiserver.models.notification.NotificationGroup; @@ -59,11 +60,14 @@ public void testItCanCreateNotificationEvent() throws Exception { notificationService.createNotificationGroup(new NotificationGroup().setType("test")); notificationService.createNotificationEvent( - group.getId(), new NotificationEvent().setData(produceClientEvent(1.0, "", ""))); + group.getId(), + new NotificationEvent().setData(produceClientEvent(1.0, "", "")).setState(ProgressState.QUEUED)); notificationService.createNotificationEvent( - group.getId(), new NotificationEvent().setData(produceClientEvent(1.0, "", ""))); + group.getId(), + new NotificationEvent().setData(produceClientEvent(1.0, "", "")).setState(ProgressState.RUNNING)); notificationService.createNotificationEvent( - group.getId(), new NotificationEvent().setData(produceClientEvent(1.0, "", ""))); + group.getId(), + new NotificationEvent().setData(produceClientEvent(1.0, "", "")).setState(ProgressState.CANCELLED)); final NotificationGroup after = notificationService.getNotificationGroup(group.getId()).orElseThrow();