Skip to content

Commit

Permalink
GRAD2-2724: task is completed.
Browse files Browse the repository at this point in the history
GRAD2-2724: task is completed.
  • Loading branch information
infstar committed May 16, 2024
1 parent 96b84b0 commit 4cb5e37
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ public Step masterStepCertRegen(JobRepository jobRepository, PlatformTransaction
@Bean
public Step certRegenJobStep(JobRepository jobRepository, PlatformTransactionManager transactionManager, SkipSQLTransactionExceptionsListener skipListener) {
return new StepBuilder("certRegenJobStep", jobRepository)
.<UUID, Integer>chunk(1, transactionManager)
.<StudentCredentialDistribution, Integer>chunk(1, transactionManager)
.reader(itemReaderCertRegen())
.processor(itemProcessorCertRegen())
.writer(itemWriterCertRegen())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.GraduationStudentRecordDistribution;
import ca.bc.gov.educ.api.batchgraduation.model.StudentCredentialDistribution;
import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils;
import jakarta.validation.ValidationException;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;

import java.io.IOException;
import java.net.UnknownServiceException;
import java.util.UUID;

public class RunCertificateRegenerationProcessor implements ItemProcessor<UUID, Integer> {
public class RunCertificateRegenerationProcessor implements ItemProcessor<StudentCredentialDistribution, Integer> {

private static final Logger LOGGER = LoggerFactory.getLogger(RunCertificateRegenerationProcessor.class);

Expand All @@ -25,27 +32,41 @@ public class RunCertificateRegenerationProcessor implements ItemProcessor<UUID,
Long batchId;

@Override
public Integer process(UUID key) throws Exception {
GraduationStudentRecordDistribution stuRec = restUtils.getStudentData(key.toString());
if (stuRec != null) {
LOGGER.info("Processing partitionData: studentID = {}", stuRec.getStudentID());
@Nullable
public Integer process(@NotNull StudentCredentialDistribution item) throws Exception {
if (item.getStudentID() != null) {
LOGGER.info("Processing partitionData: studentID = {}", item.getStudentID());
summaryDTO.setBatchId(batchId);
try {
if (StringUtils.isBlank(item.getPen())) {
item.setPen(getPenNumber(item.getStudentID()));
}
summaryDTO.setProcessedCount(summaryDTO.getProcessedCount() + 1L);
Integer count = restUtils.runRegenerateStudentCertificate(stuRec.getPen());
Integer count = restUtils.runRegenerateStudentCertificate(item.getPen());
if (count > 0) {
restUtils.updateStudentGradRecord(key, batchId, "CERTREGEN");
restUtils.updateStudentGradRecord(item.getStudentID(), batchId, "CERTREGEN");
}
return count;
} catch(Exception e) {
LOGGER.error("Unexpected Error: {}", e.getLocalizedMessage());
summaryDTO.updateError(key,"GRAD-GRADUATION-API IS DOWN","Graduation API is unavailable at this moment");
summaryDTO.updateError(item.getStudentID(),"GRAD-GRADUATION-API IS DOWN","Graduation API is unavailable at this moment");
summaryDTO.setProcessedCount(summaryDTO.getProcessedCount() - 1L);
LOGGER.info("Failed STU-ID:{} Errors:{}",key,summaryDTO.getErrors().size());
LOGGER.info("Failed STU-ID:{} Errors:{}",item.getStudentID(),summaryDTO.getErrors().size());
return null;
}
}
LOGGER.warn("Skipped STU-ID:{} Errors:{}", item != null? item.getStudentID() : "UNKNOWN PEN#",summaryDTO.getErrors().size());
return null;
}

private String getPenNumber(UUID studentID) throws Exception {
String pen = null;
GraduationStudentRecordDistribution stuRec = restUtils.getStudentData(studentID.toString());
if (stuRec != null) {
pen = stuRec.getPen();
} else {
throw new IOException("Exception occurred in PEN API.");
}
return pen;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,40 @@
import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.ResponseObj;
import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.UUID;
public abstract class BaseReader {

public abstract class BaseReader implements ItemReader<UUID> {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseReader.class);

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
RestUtils restUtils;

@Value("#{stepExecutionContext['index']}")
protected Integer nxtStudentForProcessing;

@Value("#{stepExecutionContext['data']}")
protected List<UUID> studentList;

@Value("#{stepExecutionContext['summary']}")
AlgorithmSummaryDTO summaryDTO;

@Value("#{stepExecution.jobExecution}")
JobExecution jobExecution;

protected void aggregate(String summaryContextName) {
AlgorithmSummaryDTO totalSummaryDTO = (AlgorithmSummaryDTO)jobExecution.getExecutionContext().get(summaryContextName);
if (totalSummaryDTO == null) {
totalSummaryDTO = new AlgorithmSummaryDTO();
jobExecution.getExecutionContext().put(summaryContextName, totalSummaryDTO);
}
totalSummaryDTO.setBatchId(summaryDTO.getBatchId());
totalSummaryDTO.setReadCount(totalSummaryDTO.getReadCount() + summaryDTO.getReadCount());
totalSummaryDTO.setProcessedCount(totalSummaryDTO.getProcessedCount() + summaryDTO.getProcessedCount());
totalSummaryDTO.getErrors().putAll(summaryDTO.getErrors());
}

protected void fetchAccessToken() {
LOGGER.info("Fetching the access token from KeyCloak API");
ResponseObj res = restUtils.getTokenResponseObject();
if (res != null) {
summaryDTO.setAccessToken(res.getAccess_token());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ca.bc.gov.educ.api.batchgraduation.reader;

import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.ResponseObj;
import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.UUID;

public abstract class BaseStudentReader implements ItemReader<UUID> {

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
RestUtils restUtils;

@Value("#{stepExecutionContext['index']}")
protected Integer nxtStudentForProcessing;

@Value("#{stepExecutionContext['data']}")
protected List<UUID> studentList;

@Value("#{stepExecutionContext['summary']}")
AlgorithmSummaryDTO summaryDTO;

@Value("#{stepExecution.jobExecution}")
JobExecution jobExecution;

protected void fetchAccessToken() {
ResponseObj res = restUtils.getTokenResponseObject();
if (res != null) {
summaryDTO.setAccessToken(res.getAccess_token());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class RecalculateProjectedGradRunErrorReader extends BaseReader {
public class RecalculateProjectedGradRunErrorReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(RecalculateProjectedGradRunErrorReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class RecalculateProjectedGradRunErrorRetryReader extends BaseReader {
public class RecalculateProjectedGradRunErrorRetryReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(RecalculateProjectedGradRunErrorRetryReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class RecalculateProjectedGradRunReader extends BaseReader {
public class RecalculateProjectedGradRunReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(RecalculateProjectedGradRunReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class RecalculateStudentErrorReader extends BaseReader {
public class RecalculateStudentErrorReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(RecalculateStudentErrorReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class RecalculateStudentErrorRetryReader extends BaseReader {
public class RecalculateStudentErrorRetryReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(RecalculateStudentErrorRetryReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class RecalculateStudentReader extends BaseReader {
public class RecalculateStudentReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(RecalculateStudentReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import reactor.core.publisher.Mono;

import java.util.*;
import java.util.stream.Collectors;

import static ca.bc.gov.educ.api.batchgraduation.util.EducGradBatchGraduationApiConstants.SEARCH_REQUEST;

Expand Down Expand Up @@ -43,37 +42,39 @@ public Map<String, ExecutionContext> partition(int gridSize) {
if(parallelDTO != null) {
credentialList.addAll(parallelDTO.certificateList());
}
} else if (certificateRegenerationRequest.getPens() != null && !certificateRegenerationRequest.getPens().isEmpty()) {
certificateRegenerationRequest.getPens().forEach(p -> {
StudentCredentialDistribution scd = new StudentCredentialDistribution();
scd.setPen(p);
scd.setStudentID(getStudentIDByPen(p));
if (scd.getStudentID() != null)
credentialList.add(scd);
});
} else {
credentialList.addAll(getStudentsForUserReqRun(certificateRegenerationRequest));
}

Set<UUID> studentSet = new HashSet<>();
if(!credentialList.isEmpty()) {
studentSet = credentialList.stream().map(StudentCredentialDistribution::getStudentID).collect(Collectors.toSet());
}

List<UUID> studentList = new ArrayList<>(studentSet);
if(!studentList.isEmpty()) {
createTotalSummaryDTO("regenCertSummaryDTO");
updateBatchJobHistory(createBatchJobHistory(), (long) studentList.size());
int partitionSize = studentList.size()/gridSize + 1;
List<List<UUID>> partitions = new LinkedList<>();
for (int i = 0; i < studentList.size(); i += partitionSize) {
partitions.add(studentList.subList(i, Math.min(i + partitionSize, studentList.size())));
updateBatchJobHistory(createBatchJobHistory(), (long) credentialList.size());
int partitionSize = credentialList.size()/gridSize + 1;
List<List<StudentCredentialDistribution>> partitions = new LinkedList<>();
for (int i = 0; i < credentialList.size(); i += partitionSize) {
partitions.add(credentialList.subList(i, Math.min(i + partitionSize, credentialList.size())));
}
Map<String, ExecutionContext> map = new HashMap<>(partitions.size());
for (int i = 0; i < partitions.size(); i++) {
ExecutionContext executionContext = new ExecutionContext();
AlgorithmSummaryDTO summaryDTO = new AlgorithmSummaryDTO();
List<UUID> data = partitions.get(i);
List<StudentCredentialDistribution> data = partitions.get(i);
executionContext.put("data", data);
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
String key = "partition" + i;
map.put(key, executionContext);
}
LOGGER.info("Found {} in total running on {} partitions",studentList.size(),map.size());
LOGGER.info("Found {} in total running on {} partitions",credentialList.size(),map.size());
return map;
}
LOGGER.info("No Certificates are found to process them with null distribution date");
Expand All @@ -89,6 +90,15 @@ private List<StudentCredentialDistribution> getStudentsForUserReqRun(Certificate
}
}

private UUID getStudentIDByPen(String pen) {
try {
String accessToken = restUtils.fetchAccessToken();
return restUtils.getStudentIDByPen(pen, accessToken);
} catch (Exception e) {
return null;
}
}

@Override
protected JobExecution getJobExecution() {
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class SpecialGradRunStudentReader extends BaseReader {
public class SpecialGradRunStudentReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(SpecialGradRunStudentReader.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.UUID;

public class SpecialProjectedGradRunReader extends BaseReader {
public class SpecialProjectedGradRunReader extends BaseStudentReader {

private static final Logger LOGGER = LoggerFactory.getLogger(SpecialProjectedGradRunReader.class);

Expand Down

0 comments on commit 4cb5e37

Please sign in to comment.