7 changed files with 183 additions and 36 deletions
@ -0,0 +1,29 @@ |
|||
package com.userinformation.backend.controller; |
|||
|
|||
import com.userinformation.backend.service.QualityCompareService; |
|||
import com.userinformation.backend.util.RequestResult; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
/** |
|||
* @author: xiaowuler |
|||
* @createTime: 2022-05-05 16:31 |
|||
* @describe: 质控对比 控制层 |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/qualityCompare") |
|||
public class QualityCompareController { |
|||
|
|||
private final QualityCompareService qualityCompareService; |
|||
public QualityCompareController(QualityCompareService qualityCompareService) { |
|||
this.qualityCompareService = qualityCompareService; |
|||
} |
|||
|
|||
@RequestMapping("findByTimeAndElement") |
|||
public RequestResult findByTimeAndElement(String date, String var, String locate) throws IOException { |
|||
date = "5_1_10"; |
|||
return RequestResult.success(qualityCompareService.findByTimeAndElement(date, var, locate)); |
|||
} |
|||
} |
@ -0,0 +1,84 @@ |
|||
package com.userinformation.backend.service; |
|||
|
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.io.File; |
|||
import java.io.IOException; |
|||
import java.nio.file.Files; |
|||
import java.nio.file.Paths; |
|||
import java.time.Duration; |
|||
import java.time.LocalDateTime; |
|||
import java.time.format.DateTimeFormatter; |
|||
import java.util.*; |
|||
import java.util.concurrent.atomic.AtomicInteger; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* @author: xiaowuler |
|||
* @createTime: 2022-05-05 16:36 |
|||
* @describe: 质控对比服务层 |
|||
*/ |
|||
@Service |
|||
public class QualityCompareService { |
|||
|
|||
private static final String SEPARATOR = ","; |
|||
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy_M_d_HH"); |
|||
|
|||
@Value("${custom.quality-path}") |
|||
private String qualityPath; |
|||
|
|||
public List<Float[]> findByTimeAndElement(String date, String var, String locate) throws IOException { |
|||
// String filepath = qualityPath + var + "-" + date + ".CSV";
|
|||
String filepath = "C:\\Users\\xiaowuler\\Desktop\\原始产品数据\\MEXT-2021-05-01.CSV"; |
|||
LocalDateTime time = LocalDateTime.parse("2021_" + date, DATE_FORMATTER).withHour(0).plusDays(1); |
|||
return read(filepath, time); |
|||
} |
|||
|
|||
private List<Float[]> read(String filepath, LocalDateTime time) throws IOException { |
|||
List<String> lines = Files.readAllLines(Paths.get(filepath)); |
|||
// 将文件数据进行反转,从最后一行开始读取
|
|||
Collections.reverse(lines); |
|||
|
|||
List<Float[]> data = new ArrayList<>(288); |
|||
for(String line : lines){ |
|||
List<String> rows = Arrays.asList(line.split(SEPARATOR)); |
|||
// 相关数据时间转换,以比较,并按照10分钟间隔过滤
|
|||
LocalDateTime targetTime = LocalDateTime.parse(rows.get(0), TIME_FORMATTER); |
|||
if (targetTime.isAfter(time)) { |
|||
continue; |
|||
} |
|||
|
|||
time = fillNaN(time, targetTime, data); |
|||
data.add(getValues(rows)); |
|||
time = time.minusMinutes(5); |
|||
} |
|||
|
|||
Collections.reverse(data); |
|||
AtomicInteger index = new AtomicInteger(0); |
|||
// 匹配老师处理的数据
|
|||
return data.stream().filter(values -> index.getAndIncrement() % 12 != 0).collect(Collectors.toList()); |
|||
} |
|||
|
|||
private LocalDateTime fillNaN(LocalDateTime time, LocalDateTime targetTime, List<Float[]> data){ |
|||
if (Duration.between(targetTime, time).toMinutes() > 5) { |
|||
Float[] values = new Float[801]; |
|||
Arrays.fill(values, Float.NaN); |
|||
data.add(values); |
|||
time = time.minusMinutes(10); |
|||
fillNaN(time, targetTime, data); |
|||
} |
|||
return time; |
|||
} |
|||
|
|||
private Float[] getValues(List<String> rows){ |
|||
Float[] values = rows.stream().skip(7).limit(801).map(row -> Float.parseFloat(row)).toArray(Float[]::new); |
|||
int length = values.length; |
|||
if (length != 801) { |
|||
values = Arrays.copyOf(values, 801); |
|||
Arrays.fill(values, length - 1, 800, Float.NaN); |
|||
} |
|||
return values; |
|||
} |
|||
} |
Loading…
Reference in new issue