|
@@ -0,0 +1,170 @@
|
|
|
+package com.gyee.impala.service.custom.diagnose;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.parser.Feature;
|
|
|
+import com.gyee.impala.common.cache.InfoCache;
|
|
|
+import com.gyee.impala.common.config.GyeeConfig;
|
|
|
+import com.gyee.impala.common.feign.RemoteServiceBuilder;
|
|
|
+import com.gyee.impala.common.util.DateUtil;
|
|
|
+import com.gyee.impala.common.util.FileUtil;
|
|
|
+import com.gyee.impala.model.custom.diagnose.*;
|
|
|
+import com.gyee.impala.model.master.diagnose.Diagnosepoint;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.*;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据处理服务 在线工具
|
|
|
+ *
|
|
|
+ * @author cmh
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class DataPointService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GyeeConfig gyeeConfig;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据适配器
|
|
|
+ */
|
|
|
+ @Resource
|
|
|
+ private RemoteServiceBuilder dataAdapterBuilder;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * UniformCode信息
|
|
|
+ */
|
|
|
+ private Map<String, List<Diagnosepoint>> uniformCodeInfoMap;
|
|
|
+
|
|
|
+ public void formatUniformcode(List<Diagnosepoint> list) {
|
|
|
+ uniformCodeInfoMap = list.stream().collect(Collectors.groupingBy(Diagnosepoint::getStationen));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有原始数据
|
|
|
+ */
|
|
|
+ public String getFormDataAll(ExecuteInfo executeInfo) {
|
|
|
+ System.out.println("有监督学习获取数据...");
|
|
|
+ String fileName = gyeeConfig.getDiagnoseFile() + executeInfo.getDataInfos()[0].getStationId() + "-"
|
|
|
+ + executeInfo.getDataInfos()[0].getModelId() + "-"
|
|
|
+ + DateUtil.dateTimeNow() + ".csv";
|
|
|
+ log.info("===filename: " + fileName);
|
|
|
+ boolean isFirst = true;
|
|
|
+
|
|
|
+ for (DataInfo di : executeInfo.getDataInfos()) {
|
|
|
+ if (!uniformCodeInfoMap.containsKey(di.getStationId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Map<String, List<PointData>> data = getData(di);
|
|
|
+ String s = getDataString(data, di.getTag(), isFirst);
|
|
|
+ FileUtil.writeFile(fileName, s);
|
|
|
+ isFirst = false;
|
|
|
+ }
|
|
|
+ System.out.println("有监督学习获取数据结束...");
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前10条原始数据
|
|
|
+ */
|
|
|
+ public Map<String, Object> getFormData(ExecuteInfo executeInfo) {
|
|
|
+ System.out.println("有监督学习获取测试数据...");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ boolean isFirst = true;
|
|
|
+ long count = 0;
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (DataInfo di : executeInfo.getDataInfos()) {
|
|
|
+ if (!uniformCodeInfoMap.containsKey(di.getStationId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 查询一台风机的原始数据
|
|
|
+ if (count == 0){
|
|
|
+ Map<String, List<PointData>> data = getData(di);
|
|
|
+ String s = getDataString(data, di.getTag(), isFirst);
|
|
|
+ sb.append(s);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 评估多少数据量 **/
|
|
|
+ count += (Long.valueOf(di.getEndTs()) - Long.valueOf(di.getStartTs())) / 1000;
|
|
|
+ }
|
|
|
+ map.put("count", count);
|
|
|
+ map.put("data", sb.toString());
|
|
|
+ System.out.println("有监督学习获取测试数据结束...");
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getDataString(Map<String, List<PointData>> data, String tag, boolean isFirst) {
|
|
|
+ List<List<PointData>> vals = new ArrayList<>(data.values());
|
|
|
+ if(vals.size()<=0){
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ if (isFirst) {
|
|
|
+ stringBuilder.append(getFileTitle(data.keySet()));
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < vals.get(0).size(); ++i) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Date dt = new Date(vals.get(0).get(i).getTs());
|
|
|
+ sb.append(sdf.format(dt)).append(',');
|
|
|
+ for (List<PointData> vv : vals) {
|
|
|
+ if (i >= vv.size()) {
|
|
|
+ sb.append(0).append(',');
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ sb.append(vv.get(i).getValue()).append(',');
|
|
|
+ }
|
|
|
+ if (tag != null) {
|
|
|
+ sb.append(tag);
|
|
|
+ }
|
|
|
+ sb.append('\n');
|
|
|
+ stringBuilder.append(sb);
|
|
|
+ }
|
|
|
+ return stringBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param di
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, List<PointData>> getData(DataInfo di) {
|
|
|
+ List<Diagnosepoint> list = uniformCodeInfoMap.get(di.getStationId());
|
|
|
+ Map<String, List<PointData>> pairs = new HashMap<>();
|
|
|
+ for (Diagnosepoint ci : list) {
|
|
|
+ if (pairs.containsKey(ci.getName())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<PointData> pds = dataAdapterBuilder.adapter().getHistoryByUniformCode(ci.getUniformcode(), di.getStartTs(), di.getEndTs(), di.getThingId());
|
|
|
+ pairs.put(ci.getName(), pds);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return pairs;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String getFileTitle(Set<String> keySet) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("时间,");
|
|
|
+ for (String st : keySet) {
|
|
|
+ sb.append(st).append(',');
|
|
|
+ }
|
|
|
+ sb.append("故障类型");
|
|
|
+ sb.append('\n');
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+}
|