Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created API for get count of any fields and with field value search #328

Open
wants to merge 21 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7bf08ad
Created API for get count of any fields and with field value search
sushantpatil1214 Sep 26, 2024
1318fd3
Merge branch 'dev' into CU-86c0dgwg9_API-query-endpoint
sushantpatil1214 Sep 26, 2024
9621f5f
CU-86c0h13xz - Create API for get count of Age group
sushantpatil1214 Oct 2, 2024
8c75c9a
Changed count response
sushantpatil1214 Oct 2, 2024
5f8e409
Merge pull request #330 from jembi/CU-86c0h13xz_Create-API-for-get-co…
sushantpatil1214 Oct 2, 2024
d64123f
CU-86c0h142v - Average patient age (should be able to filter by optio…
sushantpatil1214 Oct 2, 2024
d8508b3
CU-86c0h142v - Average patient age (should be able to filter by optio…
sushantpatil1214 Oct 2, 2024
826aafd
CU-86c09p2cp Added Date Filters
sushantpatil1214 Oct 3, 2024
c76e41f
removed logic to get count of dob group from age, added based on dat…
sushantpatil1214 Oct 4, 2024
532fc8c
Added comments in Postman Collection
sushantpatil1214 Oct 4, 2024
d12ed69
Merge pull request #331 from jembi/CU-86c0h142v_Average-patient-age-s…
sushantpatil1214 Oct 4, 2024
41688ae
changed api name
sushantpatil1214 Oct 4, 2024
eda31f6
changed api name
sushantpatil1214 Oct 4, 2024
d0a0b0b
changed list Name
sushantpatil1214 Oct 4, 2024
ac8cbfc
changed list Name
sushantpatil1214 Oct 4, 2024
eafa50b
Added field for AgeCount
sushantpatil1214 Oct 4, 2024
7439648
Merge branch 'dev' into CU-86c0dgwg9_API-query-endpoint
MatthewErispe Oct 15, 2024
a5dc6b0
Code Rabbit Fixes
sushantpatil1214 Oct 22, 2024
0d28367
Code Rabbit Fixes
sushantpatil1214 Oct 22, 2024
74b9909
Code Rabbit Fixes removed unwanted code
sushantpatil1214 Oct 22, 2024
1e4bf02
Code Rabbit Fixes removed unwanted code
sushantpatil1214 Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,42 @@ static CompletionStage<BackEnd.GetConfigurationResponse> getConfiguration(
return stage.thenApply(response -> response);
}

static CompletionStage<BackEnd.GetAgeGroupCountResponse> getAgeGroupCount(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd,
final ApiModels.SearchAgeCountFields searchAgeCountFields) {
final CompletionStage<BackEnd.GetAgeGroupCountResponse> stage = AskPattern
.ask(backEnd,
replyTo -> new BackEnd.GetAgeGroupCountRequest(replyTo, searchAgeCountFields),
java.time.Duration.ofSeconds(GlobalConstants.TIMEOUT_DGRAPH_QUERY_SECS),
actorSystem.scheduler());
return stage.thenApply(response -> response);
}

static CompletionStage<BackEnd.GetAllListResponse> getAllList(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd,
final ApiModels.AllList allList) {
final CompletionStage<BackEnd.GetAllListResponse> stage = AskPattern
.ask(backEnd,
replyTo -> new BackEnd.GetAllListRequest(replyTo, allList),
java.time.Duration.ofSeconds(GlobalConstants.TIMEOUT_DGRAPH_QUERY_SECS),
actorSystem.scheduler());
return stage.thenApply(response -> response);
}

static CompletionStage<BackEnd.GetFieldCountResponse> getFieldCount(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd,
final ApiModels.CountFields countFields) {
final CompletionStage<BackEnd.GetFieldCountResponse> stage = AskPattern
.ask(backEnd,
replyTo -> new BackEnd.GetFieldCountRequest(replyTo, countFields),
java.time.Duration.ofSeconds(GlobalConstants.TIMEOUT_DGRAPH_QUERY_SECS),
actorSystem.scheduler());
return stage.thenApply(response -> response);
}

static CompletionStage<BackEnd.GetFieldsConfigurationResponse> getFieldsConfiguration(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public final class BackEnd extends AbstractBehavior<BackEnd.Event> {

Expand Down Expand Up @@ -165,6 +169,9 @@ public Receive<Event> actor() {
.onMessage(PostUploadCsvFileRequest.class, this::postUploadCsvFileHandler)
.onMessage(SQLDashboardDataRequest.class, this::getSqlDashboardDataHandler)
.onMessage(GetConfigurationRequest.class, this::getConfigurationHandler)
.onMessage(GetFieldCountRequest.class, this::getFieldCountHandler)
.onMessage(GetAgeGroupCountRequest.class, this::getAgeGroupCountHandler)
.onMessage(GetAllListRequest.class, this::getAllListHandler)
.onMessage(PostConfigurationRequest.class, this::postConfigurationHandler)
.onMessage(GetFieldsConfigurationRequest.class, this::getFieldsConfigurationHandler)
.build();
Expand Down Expand Up @@ -514,6 +521,69 @@ private Behavior<Event> getConfigurationHandler(final GetConfigurationRequest re
return Behaviors.same();
}

private Behavior<Event> getFieldCountHandler(final GetFieldCountRequest request) {
String getCount = null;
try {
getCount = libMPI.getFieldCount(request.countFields);
request.replyTo.tell(new GetFieldCountResponse(getCount));
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
LOGGER.error("libMPI.getFieldCount failed for goldenId: {} with error: {}", e.getMessage());
}
return Behaviors.same();
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

private Behavior<Event> getAgeGroupCountHandler(final GetAgeGroupCountRequest request) {
long getCount = 0;
try {
String startDob = request.searchAgeCountFields.startDate();
String endDob = request.searchAgeCountFields.endDate();
LOGGER.info("startDob: {}, endDob: {}", startDob, endDob);
getCount = libMPI.getAgeGroupCount(request.searchAgeCountFields);
request.replyTo.tell(new GetAgeGroupCountResponse(getCount));
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
LOGGER.error("libMPI.getAgeGroupCountHandler failed for goldenId: {} with error: {}", e.getMessage());
}
return Behaviors.same();
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

private Behavior<Event> getAllListHandler(final GetAllListRequest request) {
List<String> dobList = new ArrayList<>();
try {
dobList = libMPI.getAllList(request.allListRequest);
LOGGER.info("dobList size: {}", dobList.size());
// double allList = calculateAvarageAge(dobList);
request.replyTo.tell(new GetAllListResponse(dobList));
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
LOGGER.error("libMPI.getAllList failed for allListRequest: {} with error: {}", request.allListRequest, e.getMessage());
}
return Behaviors.same();
}

public static double calculateAvarageAge(final List<String> dobList) {
LocalDate today = LocalDate.now(); // Get today's date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); // DOB format in YYYYMMDD
int totalAge = 0;
int count = 0;
// Iterate through the list of DOBs and calculate the age for each
for (String dob : dobList) {
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: {}. Skipping this record.", dob);
}
}
}
// Calculate and return average age
return count > 0 ? (double) totalAge / count : 0;
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

private Behavior<Event> getFieldsConfigurationHandler(final GetFieldsConfigurationRequest request) {
final var separator = FileSystems.getDefault().getSeparator();
final String configDir = System.getenv("SYSTEM_CONFIG_DIRS");
Expand Down Expand Up @@ -617,6 +687,18 @@ public record GetConfigurationRequest(ActorRef<GetConfigurationResponse> replyTo

public record GetConfigurationResponse(Configuration configuration) implements EventResponse { }

public record GetFieldCountRequest(ActorRef<GetFieldCountResponse> replyTo, ApiModels.CountFields countFields) implements Event { }

public record GetFieldCountResponse(String genderCount) implements EventResponse { }

public record GetAgeGroupCountRequest(ActorRef<GetAgeGroupCountResponse> replyTo, ApiModels.SearchAgeCountFields searchAgeCountFields) implements Event { }

public record GetAgeGroupCountResponse(long ageGroupCount) implements EventResponse { }

public record GetAllListRequest(ActorRef<GetAllListResponse> replyTo, ApiModels.AllList allListRequest) implements Event { }

public record GetAllListResponse(List<String> records) implements EventResponse { }

public record GetFieldsConfigurationRequest(ActorRef<GetFieldsConfigurationResponse> replyTo) implements Event { }

public record GetFieldsConfigurationResponse(List<FieldsConfiguration.Field> fields) implements EventResponse { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import akka.http.javadsl.marshalling.Marshaller;
import akka.http.javadsl.model.*;
import akka.http.javadsl.server.Route;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jembi.jempi.libmpi.MpiServiceError;
Expand Down Expand Up @@ -548,6 +549,52 @@ public static Route getConfiguration(
});
}

public static Route getFieldCount(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd) {
return entity(Jackson.unmarshaller(ApiModels.CountFields.class),
request -> onComplete(Ask.getFieldCount(actorSystem, backEnd, request),
result -> {
if (!result.isSuccess()) {
return handleError(result.failed().get());
}
JsonNode jsonResponse = null;
try {
jsonResponse = OBJECT_MAPPER.readTree(result.get().genderCount());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
// Complete the request with JSON response
return complete(StatusCodes.OK, jsonResponse, JSON_MARSHALLER);
}));
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

public static Route getAgeGroupCount(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd) {
return entity(Jackson.unmarshaller(ApiModels.SearchAgeCountFields.class),
request -> onComplete(Ask.getAgeGroupCount(actorSystem, backEnd, request),
result -> {
if (!result.isSuccess()) {
return handleError(result.failed().get());
}
return complete(StatusCodes.OK, result.get().ageGroupCount(), JSON_MARSHALLER);
}));
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

private static Route getDataList(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd) {
return entity(Jackson.unmarshaller(ApiModels.AllList.class),
request -> onComplete(Ask.getAllList(actorSystem, backEnd, request),
result -> {
if (!result.isSuccess()) {
return handleError(result.failed().get());
}
return complete(StatusCodes.OK, result.get(), JSON_MARSHALLER);
}));
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

private static Route getFieldsConfiguration(
final ActorSystem<Void> actorSystem,
final ActorRef<BackEnd.Event> backEnd) {
Expand Down Expand Up @@ -661,6 +708,12 @@ public static Route createCoreAPIRoutes(
() -> Routes.getNotifications(actorSystem, backEnd)),
path(GlobalConstants.SEGMENT_POST_GOLDEN_RECORD,
() -> Routes.updateGoldenRecord(actorSystem, backEnd)),
path(GlobalConstants.SEGMENT_GET_FIELD_COUNT,
() -> Routes.getFieldCount(actorSystem, backEnd)),
path(GlobalConstants.SEGMENT_GET_AGE_GROUP_COUNT,
() -> Routes.getAgeGroupCount(actorSystem, backEnd)),
path(GlobalConstants.SEGMENT_GET_DATA_LIST,
() -> Routes.getDataList(actorSystem, backEnd)),
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved
path(GlobalConstants.SEGMENT_POST_FILTER_GIDS_WITH_INTERACTION_COUNT,
() -> Routes.postFilterGidsWithInteractionCount(actorSystem, backEnd)),
path(GlobalConstants.SEGMENT_PROXY_POST_CR_UPDATE_FIELDS,
Expand All @@ -677,7 +730,8 @@ public static Route createCoreAPIRoutes(
path(GlobalConstants.SEGMENT_GET_CONFIGURATION,
() -> Routes.getConfiguration(actorSystem, backEnd)),
path(GlobalConstants.SEGMENT_GET_FIELDS_CONFIGURATION,
() -> Routes.getFieldsConfiguration(actorSystem, backEnd)))));
() -> Routes.getFieldsConfiguration(actorSystem, backEnd))
)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public Either<MpiGeneralError, List<GoldenRecord>> findGoldenRecords(final List<
return Either.right(results.get().data());
}

public String getFieldCount(final ApiModels.CountFields countFields) {
client.connect();
return client.getFieldCount(countFields);
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

public long getAgeGroupCount(final ApiModels.SearchAgeCountFields searchAgeCountFields) {
client.connect();
return client.getAgeGroupCount(searchAgeCountFields);
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

public List<String> getAllList(final ApiModels.AllList allListRequest) {
client.connect();
return client.getAllList(allListRequest);
}
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

public ExpandedGoldenRecord findExpandedGoldenRecord(final String goldenId) {
client.connect();
final var results = client.findExpandedGoldenRecords(List.of(goldenId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ List<ExpandedSourceId> findExpandedSourceIdList(

PaginatedResultSet<ExpandedGoldenRecord> findExpandedGoldenRecords(List<String> goldenIds);

String getFieldCount(ApiModels.CountFields countFields);
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

long getAgeGroupCount(ApiModels.SearchAgeCountFields searchAgeCountFields);

List<String> getAllList(ApiModels.AllList allListRequest);
sushantpatil1214 marked this conversation as resolved.
Show resolved Hide resolved

List<String> findGoldenIds();

List<String> fetchGoldenIds(
Expand Down
Loading