Skip to content

Commit

Permalink
Code Rabbit Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sushantpatil1214 committed Oct 22, 2024
1 parent 7439648 commit a5dc6b0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ private Behavior<Event> getFieldCountHandler(final GetFieldCountRequest request)
request.replyTo.tell(new GetFieldCountResponse(getCount));
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
LOGGER.error("libMPI.findExpandedGoldenRecord failed for goldenId: {} with error: {}", e.getMessage());
LOGGER.error("libMPI.getFieldCount failed for goldenId: {} with error: {}", e.getMessage());
}
return Behaviors.same();
}
Expand All @@ -543,7 +543,7 @@ private Behavior<Event> getAgeGroupCountHandler(final GetAgeGroupCountRequest re
request.replyTo.tell(new GetAgeGroupCountResponse(getCount));
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
LOGGER.error("libMPI.findExpandedGoldenRecord failed for goldenId: {} with error: {}", e.getMessage());
LOGGER.error("libMPI.getAgeGroupCountHandler failed for goldenId: {} with error: {}", e.getMessage());
}
return Behaviors.same();
}
Expand All @@ -569,14 +569,14 @@ public static double calculateAvarageAge(final List<String> dobList) {
int count = 0;
// Iterate through the list of DOBs and calculate the age for each
for (String dob : dobList) {
if (!dob.isEmpty()) {
if (dob != null && !dob.isEmpty()) {
try {
LocalDate birthDate = LocalDate.parse(dob, formatter); // Try to convert DOB to LocalDate
int age = Period.between(birthDate, today).getYears(); // Calculate age in years
totalAge += age;
count++;
} catch (DateTimeParseException e) {
LOGGER.error("Invalid date format for dob: " + dob + ". Skipping this record.");
LOGGER.error("Invalid date format for dob: {}. Skipping this record.", dob);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import akka.http.javadsl.model.*;
import akka.http.javadsl.server.Route;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.libmpi.MpiServiceError;
Expand Down Expand Up @@ -559,10 +558,9 @@ public static Route getFieldCount(
if (!result.isSuccess()) {
return handleError(result.failed().get());
}
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = null;
try {
jsonResponse = objectMapper.readTree(result.get().genderCount());
jsonResponse = OBJECT_MAPPER.readTree(result.get().genderCount());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ static String getFieldCount(final ApiModels.CountFields countFields) {
String endDate = countFields.endDate(); // Assume endDate is of LocalDate type

boolean hasValues = fieldValues != null && !fieldValues.isEmpty();
boolean hasDateRange = startDate != null && endDate != null;
boolean hasDateRange = startDate != null && endDate != null && !startDate.isEmpty() && !endDate.isEmpty();

StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("query count() {");
Expand Down Expand Up @@ -1292,7 +1292,7 @@ public static List<String> getAllList(final ApiModels.AllList allListRequest) {

// Build the query dynamically based on date range availability
String query;
if (!startDate.isEmpty() && !endDate.isEmpty()) {
if (startDate != null && endDate != null && !startDate.isEmpty() && !endDate.isEmpty()) {
query = String.format("""
{
peopleInDateRange(func: has(GoldenRecord.%s))
Expand Down Expand Up @@ -1326,14 +1326,16 @@ private static List<String> parseDobFromResponse(final String responseJson) thro
List<String> dobList = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(responseJson);
JsonNode peopleArray = jsonNode.get("peopleInDateRange");
JsonNode recordArray = jsonNode.get("peopleInDateRange");

// Extract each `dob` and add to the list
for (JsonNode person : peopleArray) {
if (person.has("GoldenRecord.dob")) {
dobList.add(person.get("GoldenRecord.dob").asText());
if (recordArray != null) {
for (JsonNode person : recordArray) {
if (person.has("GoldenRecord.dob")) {
dobList.add(person.get("GoldenRecord.dob").asText());
}
}
}
}
return dobList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ public record ApiOffsetSearch(
Boolean sortAsc) {
}

@JsonInclude(JsonInclude.Include.NON_NULL)
public record AllList(String field, String startDate, String endDate) { }

public record AverageAgeResponse(double averageAge) { }
Expand Down

0 comments on commit a5dc6b0

Please sign in to comment.