update 多线程查询,bolb权限问题

develop
hhb@hotmail.com 3 months ago
parent eb617c938f
commit cf3f5d300b

@ -0,0 +1,56 @@
package cn.teammodel.callAble;
import cn.teammodel.model.entity.school.LessonRecord;
import cn.teammodel.repository.LessonRecordRepository;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import org.springframework.data.domain.Slice;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
public class LessonRecordQueryTask implements Callable<List<LessonRecord>> {
private final LessonRecordRepository repository;
private final String lessonRecordKey;
private final Long startTime;
private final Long endTime;
private final String subjectId;
private final String tmdId;
private final String grade;
private final String periodId;
private static final int SLICE_SIZE = 1000;
public LessonRecordQueryTask(LessonRecordRepository repository, String lessonRecordKey,
long startTime, long endTime,
String subjectId, String tmdId, String grade,
String periodId) {
this.repository = repository;
this.lessonRecordKey = lessonRecordKey;
this.startTime = startTime;
this.endTime = endTime;
this.subjectId = subjectId;
this.tmdId = tmdId;
this.grade = grade;
this.periodId = periodId;
}
@Override
public List<LessonRecord> call() throws Exception {
List<LessonRecord> result = new ArrayList<>();
CosmosPageRequest pageRequest = new CosmosPageRequest(0, SLICE_SIZE, null);
while (true) {
Slice<LessonRecord> slice = repository.getLessonsByConditions(
lessonRecordKey, startTime, endTime, subjectId, tmdId, grade, periodId, pageRequest
);
result.addAll(slice.getContent());
if (!slice.hasNext()) {
break;
}
pageRequest = (CosmosPageRequest) slice.nextPageable();
}
return result;
}
}

@ -27,5 +27,12 @@ public class BlobController {
Map<String, Object> res = blobService.getBlobSas(blobSasDto,request); Map<String, Object> res = blobService.getBlobSas(blobSasDto,request);
return R.success(res); return R.success(res);
} }
@PostMapping("getSasR99")
@ApiOperation("获取某个容器的只读权限")
public R<Map<String, Object>> getSasR99(@Valid @RequestBody BlobSasDto blobSasDto, HttpServletRequest request) {
Map<String, Object> res = blobService.getSasR99(blobSasDto,request);
return R.success(res);
}
} }

@ -7,4 +7,5 @@ import java.util.Map;
public interface BlobService { public interface BlobService {
Map<String,Object> getBlobSas(BlobSasDto blobSasDto, HttpServletRequest request); Map<String,Object> getBlobSas(BlobSasDto blobSasDto, HttpServletRequest request);
Map<String,Object> getSasR99(BlobSasDto blobSasDto, HttpServletRequest request);
} }

@ -10,6 +10,7 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Map; import java.util.Map;
@Service @Service
@ -30,4 +31,19 @@ public class BlobServiceImpl implements BlobService {
} }
return sas; return sas;
} }
@Override
public Map<String, Object> getSasR99(BlobSasDto blobSasDto, HttpServletRequest request) {
Map<String, Object> sas;
String url = env.getProperty("ies.server-url-blob-sas-r-99");
try {
sas = GroupUtil.getGroupId(blobSasDto,new GroupUtil(env), request,url);
}catch (Exception e) {
throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据转换错误");
}
return sas;
}
} }

@ -18,8 +18,10 @@ import cn.teammodel.repository.LessonRecordRepository;
import cn.teammodel.repository.SchoolGroupListRepository; import cn.teammodel.repository.SchoolGroupListRepository;
import cn.teammodel.repository.SchoolRepository; import cn.teammodel.repository.SchoolRepository;
import cn.teammodel.repository.SchoolTeacherRepository; import cn.teammodel.repository.SchoolTeacherRepository;
import cn.teammodel.test.LessonRecordQueryService;
import cn.teammodel.utils.GroupUtil; import cn.teammodel.utils.GroupUtil;
import cn.teammodel.utils.SchoolDateUtil; import cn.teammodel.utils.SchoolDateUtil;
import com.azure.cosmos.CosmosException;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -35,6 +37,9 @@ import javax.servlet.http.HttpServletRequest;
import java.time.*; import java.time.*;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum; import static cn.teammodel.utils.SchoolDateUtil.calculateWeekNum;
@ -51,6 +56,7 @@ public class TeacherServiceImpl implements TeacherService {
private LessonRecordRepository lessonRecordRepository; private LessonRecordRepository lessonRecordRepository;
@Autowired @Autowired
private Environment env; private Environment env;
@Override @Override
public List<Map<String, Integer>> getTeacherList(TeacherDto teacherDto) { public List<Map<String, Integer>> getTeacherList(TeacherDto teacherDto) {
List<Map<String, Integer>> result = new ArrayList<>(); List<Map<String, Integer>> result = new ArrayList<>();
@ -435,8 +441,8 @@ public class TeacherServiceImpl implements TeacherService {
// 将 LocalDateTime 转换为 Long 类型的时间戳 // 将 LocalDateTime 转换为 Long 类型的时间戳
long startTimestamp = startDatetime.toInstant(ZoneOffset.UTC).toEpochMilli(); long startTimestamp = startDatetime.toInstant(ZoneOffset.UTC).toEpochMilli();
long endTimestamp = endDatetime.toInstant(ZoneOffset.UTC).toEpochMilli(); long endTimestamp = endDatetime.toInstant(ZoneOffset.UTC).toEpochMilli();
List<LessonRecord> lessonRecords = new ArrayList<>();
List<LessonRecord> lessonRecords = lessonRecordRepository.getLessonsByConditions(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), startTimestamp, endTimestamp, teacherDto.getSubjectId(),teacherDto.getTmdId(),teacherDto.getGrade(),teacherDto.getPeriodId()); //List<LessonRecord> lessonRecords = lessonRecordRepository.getLessonsByConditions(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), startTimestamp, endTimestamp, teacherDto.getSubjectId(),teacherDto.getTmdId(),teacherDto.getGrade(),teacherDto.getPeriodId());
int[] counts = {0, 0, 0, 0, 0}; int[] counts = {0, 0, 0, 0, 0};
String[] keys = {"cooperation", "internet", "task", "exam", "diffential"}; String[] keys = {"cooperation", "internet", "task", "exam", "diffential"};
@ -731,7 +737,7 @@ public class TeacherServiceImpl implements TeacherService {
@Override @Override
public Map<String, Object> getTeachingAndResearch(TeacherDto teacherDto, HttpServletRequest request) { public Map<String, Object> getTeachingAndResearch(TeacherDto teacherDto, HttpServletRequest request) {
final int SLICE_SIZE = 1000;
List<SugVo> sugVoList = getSugVos(teacherDto, request); List<SugVo> sugVoList = getSugVos(teacherDto, request);
//获取教师年级分配情况 //获取教师年级分配情况
Map<String, Object> Map = getTeacherGradeCount(teacherDto, request); Map<String, Object> Map = getTeacherGradeCount(teacherDto, request);
@ -745,7 +751,30 @@ public class TeacherServiceImpl implements TeacherService {
} }
} }
} }
List<LessonRecord> lessonRecords = lessonRecordRepository.getLessonsByConditions(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), teacherDto.getStartTime(), teacherDto.getEndTime(), teacherDto.getSubjectId(),teacherDto.getTmdId(),teacherDto.getGrade(),teacherDto.getPeriodId()); List<LessonRecord> records;
LessonRecordQueryService queryService = new LessonRecordQueryService(lessonRecordRepository);
try {
String lessonRecordKey = String.format(PK.PK_LESSON_RECORD, teacherDto.getCode());
Long startTime = teacherDto.getStartTime();
Long endTime = teacherDto.getEndTime();
String subjectId = teacherDto.getSubjectId();
String tmdId = teacherDto.getTmdId();
String grade = teacherDto.getGrade();
String periodId = teacherDto.getPeriodId();
records = queryService.queryLessonsInParallel(
lessonRecordKey, startTime, endTime, subjectId, tmdId, grade, periodId
);
System.out.println("查询到的记录数量: " + records.size());
} catch (InterruptedException | ExecutionException e) {
throw new ServiceException(ErrorCode.SYSTEM_ERROR.getCode(), "数据查询异常");
} finally {
queryService.shutdown();
}
//List<LessonRecord> lessonRecords = lessonRecordRepository.getLessonsByConditions(String.format(PK.PK_LESSON_RECORD, teacherDto.getCode()), teacherDto.getStartTime(), teacherDto.getEndTime(), teacherDto.getSubjectId(),teacherDto.getTmdId(),teacherDto.getGrade(),teacherDto.getPeriodId());
if(teacherDto.getTmdId() != null) { if(teacherDto.getTmdId() != null) {
List<SugVo> sugVos = sugVoList.stream().filter(sugVo -> sugVo.getHabook().equals(teacherDto.getTmdId())).collect(Collectors.toList()); List<SugVo> sugVos = sugVoList.stream().filter(sugVo -> sugVo.getHabook().equals(teacherDto.getTmdId())).collect(Collectors.toList());
@ -780,7 +809,7 @@ public class TeacherServiceImpl implements TeacherService {
mapTeach.put("DoubleGreenCount",scores[7]); mapTeach.put("DoubleGreenCount",scores[7]);
mapTeach.put("MaterialCount",scores[8]); mapTeach.put("MaterialCount",scores[8]);
mapTeach.put("TpcplanCount",scores[9]); mapTeach.put("TpcplanCount",scores[9]);
mapTeach.put("Lessons",lessonRecords.size()); mapTeach.put("Lessons",records.size());
// 检查 sugVos 是否为空 // 检查 sugVos 是否为空
if (!sugVos.isEmpty()) { if (!sugVos.isEmpty()) {
mapTeach.put("starLevel", sugVos.get(0).getStar_level()); mapTeach.put("starLevel", sugVos.get(0).getStar_level());
@ -832,7 +861,7 @@ public class TeacherServiceImpl implements TeacherService {
mapTeach.put("DoubleGreenCount",scores[7]); mapTeach.put("DoubleGreenCount",scores[7]);
mapTeach.put("MaterialCount",scores[8]); mapTeach.put("MaterialCount",scores[8]);
mapTeach.put("TpcplanCount",scores[9]); mapTeach.put("TpcplanCount",scores[9]);
mapTeach.put("Lessons",lessonRecords.size()); mapTeach.put("Lessons",records.size());
return mapTeach; return mapTeach;
}else { }else {
@ -865,7 +894,7 @@ public class TeacherServiceImpl implements TeacherService {
mapTeach.put("DoubleGreenCount",scores[7]); mapTeach.put("DoubleGreenCount",scores[7]);
mapTeach.put("MaterialCount",scores[8]); mapTeach.put("MaterialCount",scores[8]);
mapTeach.put("TpcplanCount",scores[9]); mapTeach.put("TpcplanCount",scores[9]);
mapTeach.put("Lessons",lessonRecords.size()); mapTeach.put("Lessons",records.size());
return mapTeach; return mapTeach;

@ -1,5 +1,6 @@
package cn.teammodel.model.dto.admin.exam; package cn.teammodel.model.dto.admin.exam;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
@ -7,4 +8,6 @@ public class BlobSasDto {
public String name; public String name;
public String role; public String role;
public String code; public String code;
@ApiModelProperty("容器名称")
public String containerName;
} }

@ -14,14 +14,14 @@ import java.util.List;
public interface LessonRecordRepository extends CosmosRepository<LessonRecord, String> { public interface LessonRecordRepository extends CosmosRepository<LessonRecord, String> {
@Query("select c.id from LessonRecord as c where " + @Query("select c.id from LessonRecord as c where " +
"c.code = @code and " + "c.code = @code and " +
"(IS_NULL(@startTime) or c.startTime >= @startTime) and " + "(IS_NULL(@startTime) OR c.startTime >= @startTime) and " +
"(IS_NULL(@endTime) or c.startTime <= @endTime) and "+ "(IS_NULL(@endTime) OR c.startTime <= @endTime) and "+
"(IS_NULL(@grade) or array_contains(c.grade,@grade)) and " + "(IS_NULL(@grade) OR ARRAY_CONTAINS(c.grade, @grade)) and " +
"c.status<>404 and "+ "c.status != 404 and "+
"(IS_NULL(@periodId) or c.periodId = @periodId) and "+ "(IS_NULL(@periodId) OR c.periodId = @periodId) and "+
"(IS_NULL(@subjectId) or c.subjectId = @subjectId) and "+ "(IS_NULL(@subjectId) OR c.subjectId = @subjectId) and "+
"(IS_NULL(@tmdId) or c.tmdid = @tmdId) ") "(IS_NULL(@tmdId) OR c.tmdid = @tmdId)")
List<LessonRecord> getLessonsByConditions(String code, Long startTime, Long endTime,String subjectId,String tmdId,String grade,String periodId); Slice<LessonRecord> getLessonsByConditions(String code, Long startTime, Long endTime,String subjectId,String tmdId,String grade,String periodId,Pageable pageable);
@Query("select c.startTime from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and c.tmdid = @tmdId and c.periodId = @periodId and" + @Query("select c.startTime from LessonRecord as c where c.code = @code and c.expire<=0 and c.status<>404 and c.tmdid = @tmdId and c.periodId = @periodId and" +

@ -0,0 +1,70 @@
package cn.teammodel.test;
import cn.teammodel.callAble.LessonRecordQueryTask;
import cn.teammodel.model.entity.school.LessonRecord;
import cn.teammodel.repository.LessonRecordRepository;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class LessonRecordQueryService {
//private static final int SLICE_SIZE = 1000; // 分页大小
private static final int THREAD_POOL_SIZE = 4; // 线程池大小
private final LessonRecordRepository repository;
private final ExecutorService executor;
public LessonRecordQueryService(LessonRecordRepository repository) {
this.repository = repository;
this.executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
}
public List<LessonRecord> queryLessonsInParallel(String lessonRecordKey,
Long startTime,
Long endTime,
String subjectId,
String tmdId,
String grade,
String periodId) throws InterruptedException, ExecutionException {
List<Future<List<LessonRecord>>> futures = new ArrayList<>();
// 按时间范围拆分任务
long timeRange = endTime - startTime;
int numTasks = THREAD_POOL_SIZE; // 拆分为与线程池大小相同的任务数
for (int i = 0; i < numTasks; i++) {
long chunkStart = startTime + (i * timeRange / numTasks);
long chunkEnd = (i == numTasks - 1) ? endTime : startTime + ((i + 1) * timeRange / numTasks);
//CosmosPageRequest pageRequest = new CosmosPageRequest(0, SLICE_SIZE, null);
LessonRecordQueryTask task = new LessonRecordQueryTask(
repository, lessonRecordKey, chunkStart, chunkEnd,
subjectId, tmdId, grade, periodId
);
futures.add(executor.submit(task));
}
// 合并结果
List<LessonRecord> allRecords = new ArrayList<>();
for (Future<List<LessonRecord>> future : futures) {
allRecords.addAll(future.get());
}
return allRecords;
}
public void shutdown() {
executor.shutdown();
try {
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow(); // 强制关闭未完成的任务
}
} catch (InterruptedException e) {
executor.shutdownNow();
}
}
}

@ -20,23 +20,23 @@ spring:
# 大小限制 # 大小限制
max-file-size: 10MB max-file-size: 10MB
#测试站数据库 #测试站数据库
env: dev # env: dev
cloud:
azure:
cosmos:
endpoint: https://cdhabookdep-free.documents.azure.cn:443
database: TEAMModelOS
key: JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A==
populate-query-metrics: true
#正式站数据库
# env: prod
# cloud: # cloud:
# azure: # azure:
# cosmos: # cosmos:
# endpoint: https://teammodelos.documents.azure.cn:443 # endpoint: https://cdhabookdep-free.documents.azure.cn:443
# database: TEAMModelOS # database: TEAMModelOS
# key: clF73GwPECfP1lKZTCvs8gLMMyCZig1HODFbhDUsarsAURO7TcOjVz6ZFfPqr1HzYrfjCXpMuVD5TlEG5bFGGg== # key: JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A==
# populate-query-metrics: true # populate-query-metrics: true
#正式站数据库
env: prod
cloud:
azure:
cosmos:
endpoint: https://teammodelos.documents.azure.cn:443
database: TEAMModelOS
key: clF73GwPECfP1lKZTCvs8gLMMyCZig1HODFbhDUsarsAURO7TcOjVz6ZFfPqr1HzYrfjCXpMuVD5TlEG5bFGGg==
populate-query-metrics: true
security: security:
oauth2: oauth2:
resourceserver: resourceserver:
@ -78,6 +78,7 @@ ies:
server-url-exam-rate: /common/exam/get-exam-point server-url-exam-rate: /common/exam/get-exam-point
server-url-art-analysis: /analysis/art/statistics server-url-art-analysis: /analysis/art/statistics
server-url-blob-sas-rcwld: /blob/sas-rcwld server-url-blob-sas-rcwld: /blob/sas-rcwld
server-url-blob-sas-r-99: /blob/sas-r-99
server-url-exam-analysis-list: /common/exam/find-by-analysis server-url-exam-analysis-list: /common/exam/find-by-analysis
server-url-exam-find-record: /common/exam/find-summary-record server-url-exam-find-record: /common/exam/find-summary-record
server-url-analysis-simple: /analysis/simple server-url-analysis-simple: /analysis/simple

Loading…
Cancel
Save