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

GRAD2-3012: task is complete. #540

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -43,8 +43,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons

// username
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof JwtAuthenticationToken) {
JwtAuthenticationToken authenticationToken = (JwtAuthenticationToken) auth;
if (auth instanceof JwtAuthenticationToken authenticationToken) {
Jwt jwt = (Jwt) authenticationToken.getCredentials();
String username = JwtUtil.getName(jwt);
ThreadLocalStateUtil.setCurrentUser(username);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package ca.bc.gov.educ.api.batchgraduation.listener;

import ca.bc.gov.educ.api.batchgraduation.model.StudentSearchRequest;
import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils;
import ca.bc.gov.educ.api.batchgraduation.util.JsonTransformer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Date;
import java.util.List;

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

@Component
public class SpecialRunCompletionNotificationListener extends BaseRunCompletionNotificationListener {
Expand All @@ -29,31 +23,14 @@ public class SpecialRunCompletionNotificationListener extends BaseRunCompletionN
@Autowired
JsonTransformer jsonTransformer;

private static final String RUN_BY = "runBy";

@Override
public void afterJob(JobExecution jobExecution) {
if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
long elapsedTimeMillis = new Date().getTime() - jobExecution.getStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
LOGGER.info("=======================================================================================");
LOGGER.info("Special Job completed in {} s with jobExecution status {}", elapsedTimeMillis/1000, jobExecution.getStatus());
handleSummary(jobExecution, "spcRunAlgSummaryDTO", true);
processGradStudentRecordJobHistory(jobExecution);
LOGGER.info("=======================================================================================");
}
}

private void processGradStudentRecordJobHistory(JobExecution jobExecution) {

JobParameters jobParameters = jobExecution.getJobParameters();
Long batchId = jobExecution.getId();
String userName = jobParameters.getString(RUN_BY);

String searchRequest = jobParameters.getString(SEARCH_REQUEST, "{}");
StudentSearchRequest req = (StudentSearchRequest)jsonTransformer.unmarshall(searchRequest, StudentSearchRequest.class);

restUtils.updateStudentGradRecordHistory(List.of(), batchId, userName, StringUtils.upperCase(req.getActivityCode()));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class BasePartitioner extends SimplePartitioner {

private static final Logger LOGGER = LoggerFactory.getLogger(BasePartitioner.class);
private static final String RERUN_TYPE = "reRunType";
private static final String RUN_BY = "runBy";
protected static final String RUN_BY = "runBy";
private static final String PREV_BATCH_ID = "previousBatchId";
private static final String RERUN_ALL = "RERUN_ALL";
private static final String RERUN_FAILED = "RERUN_FAILED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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 ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,6 +22,9 @@ public abstract class BaseStudentReader implements ItemReader<UUID> {
@Value("#{stepExecutionContext['index']}")
protected Integer nxtStudentForProcessing;

@Value("#{stepExecutionContext['runBy']}")
protected String runBy;

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

Expand All @@ -35,4 +40,10 @@ protected void fetchAccessToken() {
summaryDTO.setAccessToken(res.getAccess_token());
}
}

protected void setUserName() {
if (StringUtils.isNotBlank(runBy) && StringUtils.isBlank(ThreadLocalStateUtil.getCurrentUser())) {
ThreadLocalStateUtil.setCurrentUser(runBy);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateProjectedGradRunErrorReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateProjectedGradRunErrorRetryReader extends BaseStudentRead
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateProjectedGradRunReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateStudentErrorReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateStudentErrorRetryReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RecalculateStudentReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
summaryDTO.setReadCount(studentList.size());

UUID nextStudent = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -31,6 +32,8 @@ public JobExecution getJobExecution() {
public Map<String, ExecutionContext> partition(int gridSize) {
initializeRunType();
BatchGradAlgorithmJobHistoryEntity jobHistory = createBatchJobHistory();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList;
if (runType == RunTypeEnum.NORMAL_JOB_PROCESS) {
studentList = restUtils.getStudentsForAlgorithm();
Expand All @@ -57,6 +60,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -30,6 +31,8 @@ public JobExecution getJobExecution() {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Long batchId = jobExecution.getId();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList = getInputDataForErroredStudents(batchId);
createTotalSummaryDTO("regGradAlgSummaryDTO");

Expand All @@ -48,6 +51,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.RunTypeEnum;
import ca.bc.gov.educ.api.batchgraduation.model.StudentSearchRequest;
import ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
Expand Down Expand Up @@ -37,6 +38,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
BatchGradAlgorithmJobHistoryEntity jobHistory = createBatchJobHistory();
List<UUID> studentList;
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
String searchRequest = jobParameters.getString(SEARCH_REQUEST, "{}");
StudentSearchRequest req = (StudentSearchRequest)jsonTransformer.unmarshall(searchRequest, StudentSearchRequest.class);
if (runType == RunTypeEnum.NORMAL_JOB_PROCESS) {
Expand Down Expand Up @@ -64,6 +66,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ca.bc.gov.educ.api.batchgraduation.model.AlgorithmSummaryDTO;
import ca.bc.gov.educ.api.batchgraduation.model.RunTypeEnum;
import ca.bc.gov.educ.api.batchgraduation.model.StudentSearchRequest;
import ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -34,6 +35,8 @@ public JobExecution getJobExecution() {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Long batchId = jobExecution.getId();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList = getInputDataForErroredStudents(batchId);
createTotalSummaryDTO("spcRunAlgSummaryDTO");

Expand All @@ -52,6 +55,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SpecialGradRunStudentReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SpecialProjectedGradRunReader extends BaseStudentReader {
@Override
public UUID read() throws Exception {
fetchAccessToken();
setUserName();
UUID nextStudent = null;

if (nxtStudentForProcessing < studentList.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -31,6 +32,8 @@ public JobExecution getJobExecution() {
public Map<String, ExecutionContext> partition(int gridSize) {
initializeRunType();
BatchGradAlgorithmJobHistoryEntity jobHistory = createBatchJobHistory();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList;
if (runType == RunTypeEnum.NORMAL_JOB_PROCESS) {
studentList = restUtils.getStudentsForProjectedAlgorithm();
Expand All @@ -57,6 +60,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Value;

Expand All @@ -30,6 +31,8 @@ public JobExecution getJobExecution() {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
Long batchId = jobExecution.getId();
JobParameters jobParameters = jobExecution.getJobParameters();
String username = jobParameters.getString(RUN_BY);
List<UUID> studentList = getInputDataForErroredStudents(batchId);
createTotalSummaryDTO("tvrRunSummaryDTO");

Expand All @@ -48,6 +51,7 @@ public Map<String, ExecutionContext> partition(int gridSize) {
summaryDTO.setReadCount(data.size());
executionContext.put("summary", summaryDTO);
executionContext.put("index",0);
executionContext.put("runBy", username);
String key = "partition" + i;
map.put(key, executionContext);
}
Expand Down
Loading
Loading