Преглед на файлове

故障诊断准确率统计

chenminghua преди 3 години
родител
ревизия
14f83d674b

+ 261 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/FaultFilterMain.java

@@ -0,0 +1,261 @@
+package com.gyee.impala;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.gyee.impala.common.feign.IShardingService;
+import com.gyee.impala.common.util.DateUtil;
+import com.gyee.impala.model.custom.AlertHistory;
+import feign.Feign;
+import feign.Request;
+import feign.Retryer;
+import feign.jackson.JacksonDecoder;
+import feign.jackson.JacksonEncoder;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.sql.*;
+import java.util.*;
+import java.util.Date;
+import java.util.stream.Collectors;
+
+public class FaultFilterMain {
+
+    private static String user = "nxfdprod";
+    private static String password = "gdnxfd123";
+    private static String driver = "oracle.jdbc.OracleDriver";
+    private static String url = "jdbc:oracle:thin:@192.168.1.105:1521:gdnxfd";
+
+    private static Connection conn;
+
+    public static void main(String[] args){
+        String station = "XS_FDC";
+        String model = "UP97";
+        String st = "2020-01-20 00:00:00";
+        String et = "2022-05-24 00:00:00";
+        long interval = 30 * 60 * 1000;
+
+        Calendar cal = Calendar.getInstance();
+
+        Map<String, String> typeMap = new HashMap<>();
+        typeMap.put("ZD", "振动故障");
+        typeMap.put("KZ", "控制系统故障");
+        typeMap.put("UPS", "UPS故障");
+        typeMap.put("TX", "通讯故障");
+        typeMap.put("CF", "测风类故障");
+        typeMap.put("LDBH", "防雷保护故障");
+        typeMap.put("JX", "机械故障");
+        typeMap.put("AQL", "安全链故障");
+        typeMap.put("LQ", "冷却类故障");
+        typeMap.put("JR", "加热类故障");
+        typeMap.put("ZS", "转速类故障");
+        typeMap.put("BPQ", "变频器故障");
+        typeMap.put("BPQ", "轴承故障");
+        typeMap.put("DC", "电池类故障");
+        typeMap.put("HH", "滑环故障");
+        typeMap.put("XB", "箱变故障");
+        typeMap.put("FDJ", "发电机故障");
+        typeMap.put("CLX", "齿轮箱故障");
+        typeMap.put("BJ", "变桨系统故障");
+        typeMap.put("PH", "偏航系统故障");
+        typeMap.put("YY", "液压系统故障");
+        typeMap.put("JC", "机舱类故障");
+        typeMap.put("ZKG", "控制柜故障");
+        typeMap.put("CGQ", "传感器异常");
+
+
+        IShardingService sharding = Feign.builder()
+                .encoder(new JacksonEncoder())
+                .decoder(new JacksonDecoder())
+                .options(new Request.Options(5000, 600000))
+                .retryer(new Retryer.Default(10000, 10000, 3))
+                .target(IShardingService.class, "http://192.168.1.14:8075");
+        String filename = "D:/" + station + "_" + model + ".xlsx";
+        //创建一个workbook对应一个excel文件
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        //在workbook中创建一个sheet对应excel中的sheet
+        Sheet sheetw = workbook.createSheet(station + "_" + model);
+        Row row = sheetw.createRow(0);
+        row.createCell(0).setCellValue("id");
+        row.createCell(1).setCellValue("tag");
+        row.createCell(2).setCellValue("faultid");
+        row.createCell(3).setCellValue("stationcn");
+        row.createCell(4).setCellValue("windturbineid");
+        row.createCell(5).setCellValue("faulttype");
+        row.createCell(6).setCellValue("faultcode");
+        row.createCell(7).setCellValue("symptom");
+        row.createCell(8).setCellValue("symptomcode");
+        row.createCell(9).setCellValue("starttime");
+        row.createCell(10).setCellValue("endtime");
+        row.createCell(11).setCellValue("manufacturer");
+        row.createCell(12).setCellValue("model");
+        row.createCell(13).setCellValue("stationen");
+        row.createCell(14).setCellValue("remark");
+        row.createCell(15).setCellValue("category");
+
+        List<Object> list = query("select h.ID, s.STATIONID, s.STATIONNAME, s.WINDTURBINEID, w.MODELID, h.FAULTTIME, h.SNAPID from FAULTHISTORY h left join FAULTSNAP s on s.id = h.snapid left join windturbine w on w.id = s.windturbineid " +
+                "where w.modelid = '"+ model +"' and h.MESSAGETYPE = '1' and s.stationid = '"+ station +"' and s.CATEGORY2 = 'GZ' " +
+                "and h.FAULTTIME >= to_date('"+st+"','yyyy-mm-dd hh24:mi:ss') " +
+                "and h.FAULTTIME <= to_date('"+et+"','yyyy-mm-dd hh24:mi:ss')");
+        String key = "";
+        int count = 0;
+        try{
+            for (int i = 1; i < list.size(); i++) {
+                Map<String, Object> map = (Map<String, Object>) list.get(i - 1);
+                cal.setTime((Date) map.get("FAULTTIME"));
+                cal.add(Calendar.MINUTE, -30);
+                String stime = DateUtil.format(cal.getTime(), DateUtil.YYYY_MM_DD_HH_MM_SS);
+                cal.add(Calendar.MINUTE, 35);
+                String etime = DateUtil.format(cal.getTime(), DateUtil.YYYY_MM_DD_HH_MM_SS);
+                System.out.println((String) map.get("WINDTURBINEID"));
+                JSONObject json = sharding.getAlertHistory("windturbine", stime, etime, station, (String) map.get("WINDTURBINEID"), 1);
+                if (json == null)
+                    continue;
+
+                JSONArray records = json.getJSONArray("records");
+                List<AlertHistory> array = JSONArray.parseArray(records.toString(), AlertHistory.class);
+                Map<String, List<AlertHistory>> collect = array.stream()
+                        .filter(s -> !s.getAlertText().equals("总故障") && !s.getAlertText().equals("故障停机状态"))
+                        .collect(Collectors.groupingBy(AlertHistory::getCategory2));
+
+                for (Map.Entry<String, List<AlertHistory>> entry : collect.entrySet()) {
+                        String k = entry.getKey();
+                        List<AlertHistory> v = entry.getValue();
+                        if (!k.equals("other")) {
+                            if (v.size() > count) {
+                                key = k;
+                            }
+                        }
+                    }
+                Row row1 = sheetw.createRow(i);
+                row1.createCell(1).setCellValue("0");
+                row1.createCell(2).setCellValue(String.valueOf(map.get("ID")));
+                row1.createCell(3).setCellValue((String) map.get("STATIONNAME"));
+                row1.createCell(4).setCellValue((String) map.get("WINDTURBINEID"));
+                row1.createCell(5).setCellValue(typeMap.get(key.toUpperCase()));
+                row1.createCell(6).setCellValue(key.toUpperCase());
+                row1.createCell(7).setCellValue("");
+                row1.createCell(8).setCellValue("");
+                row1.createCell(9).setCellValue(DateUtil.format((Date) map.get("FAULTTIME"), DateUtil.YYYY_MM_DD_HH_MM_SS));
+                row1.createCell(10).setCellValue("");
+                row1.createCell(11).setCellValue("联合动力");
+                row1.createCell(12).setCellValue((String) map.get("MODELID"));
+                row1.createCell(13).setCellValue((String) map.get("STATIONID"));
+                row1.createCell(14).setCellValue("");
+                row1.createCell(15).setCellValue("");
+            }
+
+            //将文件保存到指定的位置
+            FileOutputStream out = null;
+            try {
+                out = new FileOutputStream(filename);
+                workbook.write(out);
+                out.flush();
+                System.out.println(filename + "-文件写入成功");
+            } catch (Exception e) {
+                System.out.println("文件写入失败" + e.getMessage());
+            } finally {
+                if (null != out) {
+                    try {
+                        out.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+
+            Thread.sleep(5000);
+        }catch (Exception e){
+            e.getMessage();
+            e.getStackTrace();
+        }finally {
+            close();
+        }
+
+    }
+
+    private static void init() {
+        try {
+            Class.forName(driver);
+            conn = DriverManager.getConnection(url, user, password);
+        } catch (ClassNotFoundException e) {
+            System.out.println("===1: " + e.getMessage());
+            e.printStackTrace();
+        } catch (SQLException e) {
+            System.out.println("===2: " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    public static List<Object> query(String sql) {
+        List<Object> list = new ArrayList<>();
+        Statement stmt = null;
+        ResultSet rs = null;
+
+        if (conn == null)
+            init();
+
+        try {
+            stmt = conn.createStatement();
+            rs = stmt.executeQuery(sql);
+            list = toList(rs);
+        } catch (SQLException e) {
+            e.printStackTrace();
+        } finally {
+            if (rs != null) {
+                try {
+                    rs.close();
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
+
+            if (stmt != null) {
+                try {
+                    stmt.close();
+                } catch (SQLException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return list;
+    }
+
+    private static List toList(ResultSet rs) throws SQLException {
+        ResultSetMetaData md = rs.getMetaData();
+        int columnCount = md.getColumnCount();
+        List list = new ArrayList();
+        while (rs.next()) {
+            Map rowData = new HashMap(columnCount);
+            for (int i = 1; i <= columnCount; ++i) {
+                Object obFieldValue;
+                if (rs.getObject(i) == null) {
+                    obFieldValue = "";
+                } else {
+                    obFieldValue = rs.getObject(i);
+                }
+                rowData.put(md.getColumnName(i), obFieldValue);
+            }
+
+            list.add(rowData);
+        }
+
+        return list;
+    }
+
+    public static void close() {
+        if (conn != null) {
+            try {
+                conn.close();
+            } catch (SQLException throwables) {
+                throwables.printStackTrace();
+            }
+        }
+    }
+}

+ 40 - 3
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/DiagnoseFaultController.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.gyee.impala.common.feign.RemoteServiceBuilder;
 import com.gyee.impala.common.result.JsonResult;
 import com.gyee.impala.common.result.ResultCode;
+import com.gyee.impala.common.util.DateUtil;
 import com.gyee.impala.model.custom.diagnose.FaultInfo;
 import com.gyee.impala.model.master.Casefaultalg;
 import com.gyee.impala.service.custom.diagnose.AutoCmdService;
@@ -11,9 +12,8 @@ import com.gyee.impala.service.master.CasefaultalgService;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
+
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -32,6 +32,10 @@ public class DiagnoseFaultController {
     private RemoteServiceBuilder remoteServiceBuilder;
 
 
+    /**
+     * 离线列表数据
+     * @return
+     */
     @GetMapping("/offline")
     public JSONObject offline(@RequestParam(value = "station", required = false) String station,
                               @RequestParam(value = "windturbineid", required = false) String wtId,
@@ -71,6 +75,10 @@ public class DiagnoseFaultController {
     }
 
 
+    /**
+     * 离线诊断
+     * @return
+     */
     @PostMapping("/offlinediagnose")
     public JSONObject offlineDiagnose(@RequestBody JSONObject json){
         if (json == null)
@@ -89,6 +97,34 @@ public class DiagnoseFaultController {
     }
 
 
+    /**
+     * 诊断准确率统计
+     * @return
+     */
+    @GetMapping("/statistic")
+    public JSONObject statistic(String[] station, String st, String et){
+        if (StringUtils.isEmpty(st) || StringUtils.isEmpty(et)){
+            Calendar cal = Calendar.getInstance();
+            int year = cal.get(Calendar.YEAR);
+            st = year + "-01-01 00:00:00";
+            et = year + "-12-30 23:59:59";
+        }
+
+        List<Object> list = new ArrayList<>();
+        for (String pt : station){
+            List<Casefaultalg> collect = casefaultalgService.getAll(pt, null, null, null, null, null, st, et, null, null);
+            int size = collect.stream().filter(a -> a.getConfirm()).collect(Collectors.toList()).size();
+            int count = collect.stream().filter(a -> a.getAlgcode().equals(a.getFaultcode())).collect(Collectors.toList()).size();
+
+            Map<String, Object> map = new HashMap<>();
+            map.put("station", station);
+            map.put("accuracy", count/size);
+            list.add(map);
+        }
+        return JsonResult.successData(ResultCode.SUCCESS, list);
+    }
+
+
     private Casefaultalg format(FaultInfo info){
         Casefaultalg fault = new Casefaultalg();
         fault.setStationcn(info.getStationName());
@@ -102,4 +138,5 @@ public class DiagnoseFaultController {
         fault.setCategory("0");
         return fault;
     }
+
 }

+ 20 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/mapper/slave/FaultFilterMapper.java

@@ -0,0 +1,20 @@
+package com.gyee.impala.mapper.slave;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+
+import java.util.Date;
+
+@Mapper
+public interface FaultFilterMapper extends BaseMapper<JSONObject> {
+
+    @Select("select h.id, s.WINDTURBINEID, w.MODELID, h.FAULTTIME, h.SNAPID from FAULTHISTORY h " +
+            "left join FAULTSNAP s on s.id = h.snapid left join windturbine w on w.id = s.windturbineid\n" +
+            "where w.modelid = #{model} and h.MESSAGETYPE = '1' and s.stationid = #{station} and s.CATEGORY2 = 'GZ'\n" +
+            "and h.FAULTTIME >= to_date(#{st},'yyyy-mm-dd hh24:mi:ss')\n" +
+            "and h.FAULTTIME <= to_date(#{et},'yyyy-mm-dd hh24:mi:ss')")
+    JSONObject selectFault(String station, String model, Date st, Date et);
+
+}

+ 3 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/service/impl/master/CasefaultalgServiceImpl.java

@@ -6,6 +6,7 @@ import com.gyee.impala.common.base.ExcludeQueryWrapper;
 import com.gyee.impala.common.config.datasource.KuduDataSourceConfig;
 import com.gyee.impala.common.exception.CustomException;
 import com.gyee.impala.common.result.ResultCode;
+import com.gyee.impala.common.spring.InitialRunner;
 import com.gyee.impala.common.util.DateUtil;
 import com.gyee.impala.common.util.SnowFlakeUtil;
 import com.gyee.impala.mapper.master.CasefaultalgMapper;
@@ -77,6 +78,8 @@ public class CasefaultalgServiceImpl extends ServiceImpl<CasefaultalgMapper, Cas
     @Override
     public void editBatch(List<Casefaultalg> list) {
         try {
+            for (Casefaultalg fault : list)
+                fault.setFaulttype(InitialRunner.faultTypeMap.get(fault.getFaultcode()));
             updateBatchById(list, list.size());
         } catch (CustomException e) {
             log.error(e.getMessage());

+ 4 - 3
gyee-sample-impala/src/test/java/com/gyee/impala/FaultHistoryFilterTest.java

@@ -28,6 +28,7 @@ import java.text.DateFormat;
 import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
@@ -97,10 +98,10 @@ public class FaultHistoryFilterTest {
                 if (lists == null || lists.size() == 0)
                     continue;
                 Faultsnap snap = lists.get(0);
-                // 时间提起那2小时
+                // 时间提起5小时
                 Calendar cal = Calendar.getInstance();
                 cal.setTime(starttime);
-                cal.set(Calendar.HOUR , Calendar.HOUR -100);
+                cal.add(Calendar.HOUR, -72);
                 Date s = cal.getTime();
                 List<Faulthistory> list = faulthistoryService.getListBySnapIdAndFaultTime(snap.getId(), s, starttime);
 
@@ -121,7 +122,7 @@ public class FaultHistoryFilterTest {
                 row1.createCell(14).setCellValue(row.getCell(14, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue());
                 row1.createCell(15).setCellValue(row.getCell(15, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue());
                 if (list != null && list.size() > 0){
-                    Faulthistory fault = list.get(0);
+                    Faulthistory fault = list.get(list.size() - 1);
                     row1.createCell(2).setCellValue(fault.getId());
                     row1.createCell(9).setCellValue(format.format(fault.getFaulttime()));
                 }else{

+ 12 - 0
gyee-sample-impala/src/test/java/com/gyee/impala/FilterFaultTest.java

@@ -0,0 +1,12 @@
+package com.gyee.impala;
+
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class FilterFaultTest {
+
+}