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

Implemented fallback mechanism to TRAX v2 endpoints that obtain data… #394

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JavaDeveloper456788
Copy link

… from redis cache for school and district service, added unit tests, added new endpoints

…from redis cache for school and district service, added unit tests, added new endpoints
Copy link

sonarqubecloud bot commented Jan 7, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
66.3% Coverage on New Code (required ≥ 80%)
B Maintainability Rating on New Code (required ≥ A)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

log.debug("Get Schools from Redis Cache returned empty");
List<School> schools = getSchoolsFromInstituteApi();
if ((schools != null) && (!schools.isEmpty())) {
loadSchoolsIntoRedisCache(schools);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove code block here. Cache is initialized on load by a single pod using a cache key. The only reason the cache returns empty is the edge case when one pod is loading cache while another gets the request at the controller. Just return the schools List from Institute and we should be good.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, created a new PR #395

* @throws JsonProcessingException the json process exception
*
*/
public static HashMap<String, String> searchStringsToHTTPParams(HashMap<String, String> searchStringMap) throws JsonProcessingException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consume and return an interface (Map) instead of an implementation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, created a new PR #395

HashMap<String, String> searchInput = new HashMap<>();
searchInput.put(key, value);

try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested catch block is unnecessary.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, created a new PR #395

List <DistrictEntity> districtEntities = this.restService.get(constants.getDistrictsPaginated(),params,
List.class, webClient);
return districtTransformer.transformToDTO(districtEntities);
} catch (WebClientResponseException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception and WebClientException are caught in the RESTService.get method already which throws a ServiceException. Either catch the service exception and deal with it here or pass it up to the controller level where it can return an error to the caller.

Also, be aware that you can utilize the RestErrorHandler class to capture thrown errors and deal with them at the controller level

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, created a new PR #395

public List<District> getDistrictsFromRedisCache() {
log.debug("**** Getting districts from Redis Cache.");
return districtTransformer.transformToDTO(districtRedisRepository.findAll());
Iterable<DistrictEntity> districtEntities= districtRedisRepository.findAll();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this. We don't want to trigger a wholesale refresh of the cache. We are only interested in specific districts, not for get all.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, created a new PR #395

if ( districtEntity == null){
log.debug("Getting district by district no from Redis Cache returned empty");
List<District> districts = this.getDistrictsBySearchCriteriaFromInstituteApi("districtNumber",districtNumber );
if ((districts != null) &&(!districts.isEmpty())){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on getDistrictByIdFromRedisCache method regarding null checks on Optional

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed created a new PR #395

@@ -93,6 +155,20 @@ public List<District> getDistrictsBySchoolCategoryCode(String schoolCategoryCode
return districts;
}

public District getDistrictByIdFromInstituteApi(String districtId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again no need to catch Exceptions here. You can throw a ServiceException like the updateDistrictCache method below and also expect a District object returned instead of an Optional.

Copy link
Author

@JavaDeveloper456788 JavaDeveloper456788 Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed created a new PR #395. The method already returns District object

if (schoolEntity == null) {
log.debug("Get School by Mincode from Redis Cache returned null");
List<School> schools = getSchoolsBySearchCriteriaFromInstituteApi("mincode", mincode);
if ((schools != null) && (!schools.isEmpty())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on DistrictService.getDistrictByIdFromRedisCache method regarding null checks on Optional

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed created a new PR #395

@@ -97,6 +121,33 @@ public SchoolDetail getSchoolDetailByIdFromInstituteApi(String schoolId) {
return null;
}



public List <School> getSchoolsBySearchCriteriaFromInstituteApi(String key, String value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments on getDistrictsBySearchCriteriaFromInstituteApi. Please change accordingly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed created a new PR #395

}

public List<SchoolDetail> getSchoolDetailsFromRedisCache() {
log.debug("**** Getting school Details from Redis Cache.");
return schoolDetailTransformer.transformToDTO(schoolDetailRedisRepository.findAll());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this code block. We don't want to trigger a wholesale refresh of cache at this point. We are only interested in calling back to institute for individual institutes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed created a new PR #395

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants