Skip to content

Commit

Permalink
Merge pull request #117 from bekk/fix-serialization-and-tablename
Browse files Browse the repository at this point in the history
Fix serialization and tablename
  • Loading branch information
geirsagberg authored Jul 10, 2024
2 parents 2a7328f + 53a3b32 commit 8508368
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ LogLogic logLogic(
DataSource dataSource,
Caching caching,
DbSchedulerCustomizer customizer,
DbSchedulerUiProperties properties) {
DbSchedulerUiProperties properties,
@Value("${db-scheduler-log.table-name:scheduled_execution_logs}") String logTableName) {
return new LogLogic(
dataSource,
customizer.serializer().orElse(Serializer.DEFAULT_JAVA_SERIALIZER),
caching,
properties.isTaskData());
properties.isTaskData(),
logTableName);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
*/
package no.bekk.dbscheduler.ui.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class TaskModel {

private String taskName;
private List<String> taskInstance;
private List<Object> taskData;
Expand All @@ -31,127 +37,4 @@ public class TaskModel {
private List<Integer> consecutiveFailures;
private Instant lastHeartbeat;
private int version;

private static final ObjectMapper objectMapper = new ObjectMapper();

public TaskModel(
String taskName,
List<String> taskInstance,
List<Object> inputTaskData,
List<Instant> executionTime,
List<Boolean> picked,
List<String> pickedBy,
List<Instant> lastSuccess,
Instant lastFailure,
List<Integer> consecutiveFailures,
Instant lastHeartbeat,
int version) {
this.taskName = taskName;
this.taskInstance = taskInstance;
this.taskData = serializeTaskData(inputTaskData);
this.executionTime = executionTime;
this.picked = picked;
this.pickedBy = pickedBy;
this.lastSuccess = lastSuccess;
this.lastFailure = lastFailure;
this.consecutiveFailures = consecutiveFailures;
this.lastHeartbeat = lastHeartbeat;
this.version = version;
}

public TaskModel() {}

private List<Object> serializeTaskData(List<Object> inputTaskDataList) {
return inputTaskDataList.stream()
.map(
data -> {
try {
String serializedData = objectMapper.writeValueAsString(data);
return objectMapper.readValue(serializedData, Object.class);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
}

public String getTaskName() {
return taskName;
}

public List<String> getTaskInstance() {
return taskInstance;
}

public void setTaskInstance(List<String> taskInstance) {
this.taskInstance = taskInstance;
}

public List<Object> getTaskData() {
return taskData;
}

public void setTaskData(List<Object> inputTaskData) {
this.taskData = serializeTaskData(inputTaskData);
}

public void setExecutionTime(List<Instant> executionTime) {
this.executionTime = executionTime;
}

public List<Instant> getExecutionTime() {
return executionTime;
}

public List<Boolean> isPicked() {
return picked;
}

public void setPicked(List<Boolean> picked) {
this.picked = picked;
}

public List<String> getPickedBy() {
return pickedBy;
}

public void setPickedBy(List<String> pickedBy) {
this.pickedBy = pickedBy;
}

public List<Instant> getLastSuccess() {
return lastSuccess;
}

public void setLastSuccess(List<Instant> lastSuccess) {
this.lastSuccess = lastSuccess;
}

public Instant getLastFailure() {
return lastFailure;
}

public void setLastFailure(Instant lastFailure) {
this.lastFailure = lastFailure;
}

public List<Integer> getConsecutiveFailures() {
return consecutiveFailures;
}

public Instant getLastHeartbeat() {
return lastHeartbeat;
}

public void setLastHeartbeat(Instant lastHeartbeat) {
this.lastHeartbeat = lastHeartbeat;
}

public int getVersion() {
return version;
}

public void setConsecutiveFailures(List<Integer> consecutiveFailures) {
this.consecutiveFailures = consecutiveFailures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package no.bekk.dbscheduler.ui.service;

import com.github.kagkarlsson.scheduler.serializer.Serializer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
Expand All @@ -35,21 +37,33 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class LogLogic {

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private static final int DEFAULT_LIMIT = 500;
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private final Caching caching;
private final LogModelRowMapper logModelRowMapper;

public LogLogic(DataSource dataSource, Serializer serializer, Caching caching, boolean showData) {
private final String logTableName;
private final String databaseProductName;

public LogLogic(
DataSource dataSource,
Serializer serializer,
Caching caching,
boolean showData,
String logTableName) {
try (Connection connection = dataSource.getConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
databaseProductName = metaData.getDatabaseProductName();
} catch (SQLException e) {
throw new RuntimeException(e);
}
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
// currently we have no paging in the UI
this.namedParameterJdbcTemplate.getJdbcTemplate().setMaxRows(DEFAULT_LIMIT);
this.caching = caching;
this.logTableName = logTableName;
this.logModelRowMapper =
new LogModelRowMapper(
showData, serializer == null ? Serializer.DEFAULT_JAVA_SERIALIZER : serializer);
Expand Down Expand Up @@ -81,7 +95,7 @@ public LogPollResponse pollLogs(TaskDetailsRequestParams requestParams) {
}

public List<LogModel> getLogsDirectlyFromDB(TaskDetailsRequestParams requestParams) {
QueryBuilder queryBuilder = QueryBuilder.selectFromTable("scheduled_execution_logs");
QueryBuilder queryBuilder = QueryBuilder.selectFromTable(logTableName);
if (requestParams.getStartTime() != null) {
queryBuilder.andCondition(
new TimeCondition(
Expand All @@ -98,7 +112,8 @@ public List<LogModel> getLogsDirectlyFromDB(TaskDetailsRequestParams requestPara
}
if (requestParams.getFilter() != null
&& requestParams.getFilter() != TaskRequestParams.TaskFilter.ALL) {
queryBuilder.andCondition(new FilterCondition(requestParams.getFilter()));
queryBuilder.andCondition(
new FilterCondition(requestParams.getFilter(), databaseProductName));
}
if (requestParams.getSearchTermTaskName() != null) {
queryBuilder.andCondition(
Expand All @@ -124,7 +139,23 @@ public List<LogModel> getLogsDirectlyFromDB(TaskDetailsRequestParams requestPara
queryBuilder.getQuery(), queryBuilder.getParameters(), logModelRowMapper);
}

private enum Operators {
GREATER_THAN_OR_EQUALS(">="),
LESS_THAN_OR_EQUALS("<=");

private final String operator;

Operators(String operator) {
this.operator = operator;
}

public String getOperator() {
return operator;
}
}

private static class TimeCondition implements AndCondition {

private final String varName;
private final String operator;
private final Instant value;
Expand All @@ -147,6 +178,7 @@ public void setParameters(MapSqlParameterSource p) {
}

private static class SearchCondition implements AndCondition {

private final String searchTerm;
private final Map<String, Object> params;

Expand Down Expand Up @@ -176,17 +208,21 @@ public void setParameters(MapSqlParameterSource p) {
}

public static class FilterCondition implements AndCondition {

private final TaskRequestParams.TaskFilter filterCondition;
private final String databaseProductName;

public FilterCondition(TaskRequestParams.TaskFilter filterCondition) {
public FilterCondition(
TaskRequestParams.TaskFilter filterCondition, String databaseProductName) {
this.filterCondition = filterCondition;
this.databaseProductName = databaseProductName;
}

@Override
public String getQueryPart() {
return filterCondition == TaskRequestParams.TaskFilter.SUCCEEDED
? "succeeded = TRUE"
: "succeeded = FALSE";
? databaseProductName.equals("Oracle") ? "succeeded = 1" : "succeeded = TRUE"
: databaseProductName.equals("Oracle") ? "succeeded = 0" : "succeeded = FALSE";
}

@Override
Expand All @@ -197,6 +233,7 @@ public void setParameters(MapSqlParameterSource p) {

@RequiredArgsConstructor
public static class LogModelRowMapper implements RowMapper<LogModel> {

private final boolean showData;
private final Serializer serializer;

Expand Down Expand Up @@ -224,19 +261,4 @@ public LogModel mapRow(ResultSet rs, int rowNum) throws SQLException {
rs.getString("exception_stacktrace"));
}
}

private enum Operators {
GREATER_THAN_OR_EQUALS(">="),
LESS_THAN_OR_EQUALS("<=");

private final String operator;

Operators(String operator) {
this.operator = operator;
}

public String getOperator() {
return operator;
}
}
}
Loading

0 comments on commit 8508368

Please sign in to comment.