|
|
@@ -0,0 +1,92 @@
|
|
|
+package cn.seecoder.data.server.schedule;
|
|
|
+
|
|
|
+import cn.seecoder.data.server.domain.sonar.SonarCoderDao;
|
|
|
+import cn.seecoder.data.server.dto.coder.CoderDto;
|
|
|
+import cn.seecoder.data.server.dto.coder.CoderSonarDto;
|
|
|
+import cn.seecoder.data.server.dto.coder.Response;
|
|
|
+import cn.seecoder.data.server.repository.SonarCoderRepository;
|
|
|
+import cn.seecoder.data.server.service.ExtractionLogService;
|
|
|
+import cn.seecoder.data.server.utils.aspects.dataExtraction.DataExtractor;
|
|
|
+import cn.seecoder.data.server.utils.enums.ErrorCodeEnum;
|
|
|
+import cn.seecoder.data.server.utils.enums.TimeType;
|
|
|
+import cn.seecoder.data.server.utils.exception.GlobalException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.ParameterizedTypeReference;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class SonarSchedule {
|
|
|
+
|
|
|
+ private final String source = "coderSonar";
|
|
|
+
|
|
|
+ @Value("${remoteUrls.coder}")
|
|
|
+ private String coderBaseUrl;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ExtractionLogService extractionLogService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SonarCoderRepository sonarCoderRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ // @Scheduled(cron = "00 00 00 * * ?")
|
|
|
+ @DataExtractor(source)
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void coderSoanrDataExtraction() throws Exception {
|
|
|
+ LocalDateTime start = extractionLogService.getExtractionStartTime(source,1, TimeType.DAY);
|
|
|
+ LocalDateTime end = extractionLogService.getExtractionEndTime(source);
|
|
|
+
|
|
|
+ Map<String,String> param1 = buildParam(start, end);
|
|
|
+
|
|
|
+ String sonarCoderApi = "/api/statistic/getSonar?startAt={startTime}&endAt={endTime}";
|
|
|
+ String url = coderBaseUrl + sonarCoderApi;
|
|
|
+ ParameterizedTypeReference<Response<List<CoderSonarDto>>> responseType = new ParameterizedTypeReference<Response<List<CoderSonarDto>>>() {};
|
|
|
+
|
|
|
+ List<CoderSonarDto> coderSonarDtos = getCoderSonarData(url,param1,responseType);
|
|
|
+ List<SonarCoderDao> sonarCoderDaos = dataFilter(coderSonarDtos);
|
|
|
+ sonarCoderRepository.saveAll(sonarCoderDaos);
|
|
|
+ }
|
|
|
+
|
|
|
+ private SonarCoderDao dtoToDaoMapper(CoderSonarDto sonarDto) {
|
|
|
+ return SonarCoderDao.builder()
|
|
|
+ .sonarName(sonarDto.getSonarName())
|
|
|
+ .userId(sonarDto.getUserId())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<SonarCoderDao> dataFilter(List<CoderSonarDto> inp) {
|
|
|
+ Map<String,SonarCoderDao> map = new HashMap<>();
|
|
|
+ inp.forEach(item -> {
|
|
|
+ if(!map.containsKey(item.getSonarName())) {
|
|
|
+ map.put(item.getSonarName(),dtoToDaoMapper(item));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return (List<SonarCoderDao>) map.values();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String,String> buildParam(LocalDateTime start,LocalDateTime end) {
|
|
|
+ Map<String,String> param = new HashMap<>();
|
|
|
+ param.put("startTime",start.toString());
|
|
|
+ param.put("endTime",end.toString());
|
|
|
+ return param;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<CoderSonarDto> getCoderSonarData(String url, Map<String,String> param, ParameterizedTypeReference<Response<List<CoderSonarDto>>> responseType) {
|
|
|
+ ResponseEntity<Response<List<CoderSonarDto>>> r = restTemplate.exchange(url, HttpMethod.GET,null,responseType,param );
|
|
|
+ Response<List<CoderSonarDto>> body = r.getBody();
|
|
|
+ if (body == null || body.getCode()!=200) throw new GlobalException(ErrorCodeEnum.SERVICE_ERROR_C0002);
|
|
|
+ List<CoderSonarDto> source = body.getSource();
|
|
|
+ if(source == null || source.size()==0) throw new GlobalException(ErrorCodeEnum.SERVICE_ERROR_C0003);
|
|
|
+ return source;
|
|
|
+ }
|
|
|
+}
|