Skip to content

Commit

Permalink
Use escaped JSON instead when returning from getJobStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Sep 20, 2023
1 parent 3fb0b63 commit 216d3df
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.datastax.oss.driver.api.querybuilder.schema.CreateTableWithOptions;
import com.datastax.oss.driver.api.querybuilder.schema.OngoingPartitionKey;
import com.datastax.oss.driver.internal.core.metadata.schema.parsing.DataTypeCqlNameParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -80,8 +82,8 @@ public synchronized void unregister() {
}

@Rpc(name = "jobStatus")
public Map<String, Object> getJobStatus(@RpcParam(name = "job_id") String jobId) {
Map<String, Object> resultMap = new HashMap<>();
public Map<String, String> getJobStatus(@RpcParam(name = "job_id") String jobId) {
Map<String, String> resultMap = new HashMap<>();
Job jobWithId = service.getJobWithId(jobId);
if (jobWithId == null) {
return resultMap;
Expand All @@ -94,15 +96,23 @@ public Map<String, Object> getJobStatus(@RpcParam(name = "job_id") String jobId)
if (jobWithId.getStatus() == Job.JobStatus.ERROR) {
resultMap.put("error", jobWithId.getError().getLocalizedMessage());
}
List<List<String>> statusChanges = new ArrayList<>();

List<Map<String, String>> statusChanges = new ArrayList<>();
for (Job.StatusChange statusChange : jobWithId.getStatusChanges()) {
statusChanges.add(
Lists.newArrayList(
statusChange.getStatus().name(),
Long.valueOf(statusChange.getChangeTime()).toString(),
statusChange.getMessage()));
Map<String, String> change = Maps.newHashMap();
change.put("status", statusChange.getStatus().name());
change.put("change_time", Long.valueOf(statusChange.getChangeTime()).toString());
change.put("message", statusChange.getMessage());
statusChanges.add(change);
}

ObjectMapper objectMapper = new ObjectMapper();
try {
String s = objectMapper.writeValueAsString(statusChanges);
resultMap.put("status_changes", s);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
resultMap.put("status_changes", statusChanges);

return resultMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
package com.datastax.mgmtapi.resources.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Job implements Serializable {
Expand Down Expand Up @@ -37,8 +40,6 @@ public enum JobStatus {
@JsonProperty(value = "error")
private String error;

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonPropertyOrder({"status", "change_time", "message"})
public static class StatusChange {
@JsonProperty(value = "status")
String status;
Expand Down Expand Up @@ -83,14 +84,32 @@ public Job(
@JsonProperty(value = "submit_time") long submitTime,
@JsonProperty(value = "end_time") long finishedTime,
@JsonProperty(value = "error") String error,
@JsonProperty(value = "status_changes") List<StatusChange> changes) {
@JsonProperty(value = "status_changes") String changes) {
this.jobId = jobId;
this.jobType = jobType;
this.status = JobStatus.valueOf(status);
this.submitTime = submitTime;
this.finishedTime = finishedTime;
this.error = error;
this.statusChanges = changes;
this.statusChanges = changes(changes);
}

public List<StatusChange> changes(String s) {
ObjectMapper objectMapper = new ObjectMapper();
if (s.length() < 2) {
return new ArrayList<>();
}
try {
JsonNode parent = objectMapper.readTree(s);

List<StatusChange> changes =
objectMapper.readValue(parent.traverse(), new TypeReference<List<StatusChange>>() {});

return changes;

} catch (IOException e) {
throw new RuntimeException(e);
}
}

public String getJobId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
Expand Down Expand Up @@ -592,9 +594,20 @@ public void testJobStatus() throws Exception {
jobDetailsRow.put("submit_time", String.valueOf(System.currentTimeMillis()));
jobDetailsRow.put("end_time", String.valueOf(System.currentTimeMillis()));

List<List<String>> statusChanges = new ArrayList<>();
statusChanges.add(Lists.newArrayList("SUCCESS", "1695183696663", "No message"));
jobDetailsRow.put("status_changes", statusChanges);
List<Map<String, String>> statusChanges = new ArrayList<>();
Map<String, String> change = Maps.newHashMap();
change.put("status", "SUCCESS");
change.put("change_time", "1695183696663");
change.put("message", "No message");
statusChanges.add(change);

ObjectMapper objectMapper = new ObjectMapper();
try {
String s = objectMapper.writeValueAsString(statusChanges);
jobDetailsRow.put("status_changes", s);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

when(mockRow.getObject(0)).thenReturn(jobDetailsRow);

Expand Down

0 comments on commit 216d3df

Please sign in to comment.