Przeglądaj źródła

样本库接口修改,故障诊断增加数据

chenminghua 3 lat temu
rodzic
commit
94e5b83bc5

+ 2 - 1
gyee-sample-impala/src/main/java/com/gyee/impala/common/cache/InfoCache.java

@@ -9,6 +9,7 @@ import org.springframework.stereotype.Component;
 
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
 /**
@@ -27,7 +28,7 @@ public class InfoCache {
     /**
      * 故障报警缓存
      */
-    public Map<String, FaultInfo> faultMap = new HashMap<>();
+    public Map<String, FaultInfo> faultMap = new LinkedHashMap<>();
 
     /**
      * 坐标信息

+ 6 - 6
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/SupervisedController.java

@@ -14,10 +14,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 
 /**
@@ -170,8 +167,11 @@ public class SupervisedController {
      */
     @GetMapping("/data")
     public JSONObject getData() {
-        String str = dataService.getFormData(supervisedCmdService.getExecuteInfo());
-        return JsonResult.successData(ResultCode.SUCCESS, str);
+        Map<String, Object> map = new HashMap<>();
+        String data = dataService.getFormData(supervisedCmdService.getExecuteInfo());
+        map.put("info", executeInfo);
+        map.put("data", data);
+        return JsonResult.successData(ResultCode.SUCCESS, data);
     }
 
     /**

+ 46 - 21
gyee-sample-impala/src/main/java/com/gyee/impala/controller/sample/know/BasicQueryController.java

@@ -8,6 +8,7 @@ import com.gyee.impala.common.spring.InitialRunner;
 import com.gyee.impala.model.master.Windpowerstation;
 import com.gyee.impala.model.master.Windturbine;
 import com.gyee.impala.service.master.WindpowerstationService;
+import com.gyee.impala.service.master.WindturbineService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
@@ -27,13 +28,9 @@ import java.util.List;
 public class BasicQueryController {
 
     @Autowired
-    private WindpowerstationService windpowerstationService;
-
-
-    @GetMapping("/test")
-    public String test(){
-        return "test";
-    }
+    private WindpowerstationService stationService;
+    @Autowired
+    private WindturbineService windturbineService;
 
 
     /**
@@ -53,45 +50,73 @@ public class BasicQueryController {
      * @return
      */
     @GetMapping("/station/all")
-    @ResponseBody
     public JSONObject stationAll(){
         List<Windpowerstation> list = InitialRunner.wpList;
         return JsonResult.successData(ResultCode.SUCCESS, list);
     }
 
 
+    @PostMapping("/station/insert")
+    public JSONObject stationInsert(@RequestBody Windpowerstation obj){
+        stationService.insertItem(obj);
+        return JsonResult.success(ResultCode.SUCCESS);
+    }
+
+
+    @PostMapping("/station/edit")
+    public JSONObject stationEdit(@RequestBody Windpowerstation obj){
+        stationService.editItem(obj);
+        return JsonResult.success(ResultCode.SUCCESS);
+    }
+
+
+    @GetMapping("/station/delete")
+    public JSONObject stationDelete(String id){
+        stationService.deleteItem(id);
+        return JsonResult.success(ResultCode.SUCCESS);
+    }
+
+
     /**
      * 查询场站的所有风机
      * @return
      */
     @GetMapping("/windturbine/all")
-    @ResponseBody
     public JSONObject windsAll(String station){
         List<Windturbine> list = InitialRunner.wpMap.get(station);
         return JsonResult.successData(ResultCode.SUCCESS, list);
     }
 
 
-    @PostMapping("/station/insert")
-    @ResponseBody
-    public JSONObject stationInsert(Windpowerstation obj){
-        windpowerstationService.insertItem(obj);
+    /**
+     * 新增风机
+     * @return
+     */
+    @PostMapping("/windturbine/insert")
+    public JSONObject insertWindsAll(@RequestBody List<Windturbine> list){
+        windturbineService.insertBatch(list);
         return JsonResult.success(ResultCode.SUCCESS);
     }
 
 
-    @PostMapping("/station/edit")
-    @ResponseBody
-    public JSONObject stationEdit(Windpowerstation obj){
-        windpowerstationService.editItem(obj);
+    /**
+     * 新增风机
+     * @return
+     */
+    @PostMapping("/windturbine/edit")
+    public JSONObject insertWindsEdit(@RequestBody Windturbine obj){
+        windturbineService.editItem(obj);
         return JsonResult.success(ResultCode.SUCCESS);
     }
 
 
-    @PostMapping("/station/delete")
-    @ResponseBody
-    public JSONObject stationDelete(String id){
-        windpowerstationService.deleteItem(id);
+    /**
+     * 新增风机
+     * @return
+     */
+    @GetMapping("/windturbine/delete")
+    public JSONObject insertWindsDelete(String id){
+        windturbineService.deleteItem(id);
         return JsonResult.success(ResultCode.SUCCESS);
     }
 

+ 13 - 4
gyee-sample-impala/src/main/java/com/gyee/impala/model/master/Windpowerstation.java

@@ -38,9 +38,9 @@ public class Windpowerstation extends Model<Windpowerstation> {
 
     private Integer quantity;
 
-    private String longitude;
+    private Double longitude;
 
-    private String latitude;
+    private Double latitude;
 
     private String model;
 
@@ -50,12 +50,21 @@ public class Windpowerstation extends Model<Windpowerstation> {
 
     private String remark;
 
+
+    public String getType() {
+        return StringUtils.isEmpty(type) ? "FJ" : type;
+    }
+
     public void setType(String type) {
-        this.type = StringUtils.isEmpty(type) ? "FJ" : type;
+        this.type = type;
+    }
+
+    public String getCapacityunit() {
+        return StringUtils.isEmpty(capacityunit) ? "MW" : capacityunit;
     }
 
     public void setCapacityunit(String capacityunit) {
-        this.capacityunit = StringUtils.isEmpty(capacityunit) ? "MW" : capacityunit;
+        this.capacityunit = capacityunit;
     }
 
     @Override

+ 3 - 1
gyee-sample-impala/src/main/java/com/gyee/impala/model/master/Windturbine.java

@@ -37,7 +37,7 @@ public class Windturbine extends Model<Windturbine> {
 
     private String lineid;
 
-    private String integratedtime;
+    private String time;
 
     private String name;
 
@@ -45,6 +45,8 @@ public class Windturbine extends Model<Windturbine> {
 
     private String category;
 
+    private String manufacturer;
+
 
     @Override
     protected Serializable pkVal() {

+ 25 - 10
gyee-sample-impala/src/main/java/com/gyee/impala/service/custom/diagnose/FaultRefreshService.java

@@ -14,9 +14,7 @@ import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -73,6 +71,18 @@ public class FaultRefreshService implements ApplicationRunner {
         calendar.add(Calendar.MINUTE, -10);
         String s = dateFormat.format(calendar.getTime());
 
+        /** 每次启动先缓存一次 **/
+        if (infoCache.faultMap.size() == 0){
+            List<FaultInfo> faultRecent = shardingBuilder.sharding().getFaultRecent(s);
+            List<FaultInfo> collect = faultRecent.stream().filter(f -> f.getCategory1().equals("FJ") && f.getCategory2().equals("GZ")).collect(Collectors.toList());
+            Map<Long, FaultInfo> map = collect.stream().collect(Collectors.toMap(FaultInfo::getId, Function.identity(), (key1, key2) -> key2));
+            for (FaultInfo fi : map.values()) {
+                if (!infoCache.faultMap.containsKey(fi.getId()+fi.getFaultTime()+fi.getSnapID())) {
+                    infoCache.faultMap.put(fi.getId()+fi.getFaultTime()+fi.getSnapID(), fi);
+                }
+            }
+        }
+
         List<FaultInfo> faultRecent = shardingBuilder.sharding().getFaultRecent(s);
         List<FaultInfo> collect = faultRecent.stream().filter(f -> f.getCategory1().equals("FJ") && f.getCategory2().equals("GZ")).collect(Collectors.toList());
         Map<Long, FaultInfo> map = collect.stream().collect(Collectors.toMap(FaultInfo::getId, Function.identity(), (key1, key2) -> key2));
@@ -80,16 +90,21 @@ public class FaultRefreshService implements ApplicationRunner {
             if (!infoCache.faultMap.containsKey(fi.getId()+fi.getFaultTime()+fi.getSnapID())) {
                 autoCmdService.exec(fi);
                 infoCache.faultMap.put(fi.getId()+fi.getFaultTime()+fi.getSnapID(), fi);
-                log.info(fi.toString());
+            }
+        }
+
+        /** 缓存数据量太大于2000条时  清除部分缓存 **/
+        if (infoCache.faultMap.size() > 1000){
+            int count = 0;
+            for (Iterator<Map.Entry<String, FaultInfo>> it = infoCache.faultMap.entrySet().iterator(); it.hasNext();){
+                it.next();
+                if (count < 500)
+                    it.remove();
+                count ++;
             }
         }
     }
 
-    public static void main(String[] args){
-        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-        Calendar calendar = Calendar.getInstance();
-        System.out.println(dateFormat.format(calendar.getTime()));
-        calendar.add(Calendar.MINUTE, -5);
-        System.out.println(dateFormat.format(calendar.getTime()));
+    public static void main(String[] args) {
     }
 }

+ 4 - 2
gyee-sample-impala/src/main/java/com/gyee/impala/service/impl/master/WindpowerstationServiceImpl.java

@@ -55,6 +55,7 @@ public class WindpowerstationServiceImpl extends ServiceImpl<WindpowerstationMap
     public void editItem(Windpowerstation obj) {
         try {
             baseMapper.updateById(obj);
+            initialRunner.cacheStation();
         } catch (CustomException e){
             log.error(e.getMessage());
             throw new CustomException(ResultCode.ERROR_DATA);
@@ -78,6 +79,7 @@ public class WindpowerstationServiceImpl extends ServiceImpl<WindpowerstationMap
         query.eq("id", id);
         try {
             baseMapper.delete(query);
+            initialRunner.cacheStation();
         } catch (CustomException e){
             log.error(e.getMessage());
             throw new CustomException(ResultCode.ERROR_DATA);
@@ -97,14 +99,14 @@ public class WindpowerstationServiceImpl extends ServiceImpl<WindpowerstationMap
         Insert insert = kuduTable.newInsert();
         // 获取Row对象,设置插入的值
         PartialRow row = insert.getRow();
-        row.addObject("id", SnowFlakeUtil.generateId());
+        row.addObject("id", obj.getId());
         row.addObject("name", obj.getName());
         row.addObject("address", obj.getAddress());
         row.addObject("telephone", obj.getTelephone());
         row.addObject("type", obj.getType());
         row.addObject("capacity", obj.getCapacity());
         row.addObject("capacityunit", obj.getCapacityunit());
-        row.addObject("quanity", obj.getQuantity());
+        row.addObject("quantity", obj.getQuantity());
         row.addObject("longitude", obj.getLongitude());
         row.addObject("latitude", obj.getLatitude());
         row.addObject("model", obj.getModel());

+ 94 - 2
gyee-sample-impala/src/main/java/com/gyee/impala/service/impl/master/WindturbineServiceImpl.java

@@ -2,18 +2,24 @@ package com.gyee.impala.service.impl.master;
 
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.gyee.impala.common.config.datasource.KuduDataSourceConfig;
 import com.gyee.impala.common.exception.CustomException;
 import com.gyee.impala.common.result.ResultCode;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.gyee.impala.common.spring.InitialRunner;
+import com.gyee.impala.common.util.DateUtil;
 import com.gyee.impala.mapper.master.WindturbineMapper;
 import com.gyee.impala.model.master.Windturbine;
 import com.gyee.impala.service.master.WindturbineService;
+import org.apache.kudu.client.*;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+
 import java.util.List;
 
 /**
  * <p>
- *  服务实现类
+ * 服务实现类
  * </p>
  *
  * @author chenmh
@@ -22,6 +28,11 @@ import java.util.List;
 @Service
 public class WindturbineServiceImpl extends ServiceImpl<WindturbineMapper, Windturbine> implements WindturbineService {
 
+    @Autowired
+    private InitialRunner initialRunner;
+    @Autowired
+    private KuduDataSourceConfig kuduConfig;
+
     @Override
     public List<Windturbine> getWindTurbineId(String station) {
         try {
@@ -29,9 +40,90 @@ public class WindturbineServiceImpl extends ServiceImpl<WindturbineMapper, Windt
             wrapper.eq("station", station);
             wrapper.orderByAsc("id");
             return baseMapper.selectList(wrapper);
-        } catch (CustomException e){
+        } catch (CustomException e) {
+            log.error(e.getMessage());
+            throw new CustomException(ResultCode.ERROR_DATA);
+        }
+    }
+
+    @Override
+    public void insertBatch(List<Windturbine> list) {
+        if (list == null || list.size() == 0)
+            return;
+        try {
+            insert(list);
+            initialRunner.cacheStation();
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw new CustomException(ResultCode.ERROR_DATA);
+        }
+    }
+
+    @Override
+    public void editItem(Windturbine obj) {
+        try {
+            baseMapper.updateById(obj);
+            initialRunner.cacheStation();
+        } catch (CustomException e) {
             log.error(e.getMessage());
             throw new CustomException(ResultCode.ERROR_DATA);
         }
     }
+
+
+    @Override
+    public void deleteItem(String id) {
+        QueryWrapper<Windturbine> query = new QueryWrapper<>();
+        query.eq("id", id);
+
+        try {
+            baseMapper.delete(query);
+            initialRunner.cacheStation();
+        } catch (CustomException e) {
+            log.error(e.getMessage());
+            throw new CustomException(ResultCode.ERROR_DATA);
+        }
+    }
+
+
+    /**
+     * 由于mybatis-plus存储的中文乱码
+     * 采用原生写法
+     *
+     * @param list
+     * @return
+     */
+    private void insert(List<Windturbine> list) throws KuduException {
+        KuduTable kuduTable = kuduConfig.kuduClient.openTable("impala::gyee_sample_kudu.windturbine");
+        KuduSession kuduSession = kuduConfig.kuduSession();
+        int i = 0;
+        for (Windturbine obj : list) {
+            Insert insert = kuduTable.newInsert();
+            // 获取Row对象,设置插入的值
+            PartialRow row = insert.getRow();
+            row.addObject("id", obj.getId());
+            row.addObject("station", obj.getStation());
+            row.addObject("longitude", obj.getLongitude());
+            row.addObject("latitude", obj.getLatitude());
+            row.addObject("model", obj.getModel());
+            row.addObject("projectid", obj.getProjectid());
+            row.addObject("lineid", obj.getLineid());
+            row.addObject("name", obj.getName());
+            row.addObject("remark", obj.getRemark());
+            row.addObject("category", obj.getCategory());
+            row.addObject("manufacturer", obj.getManufacturer());
+            row.addObject("time", DateUtil.getCurrentDate());
+
+            // 先不提交kudu
+            kuduSession.apply(insert);
+            i++;
+            if (i % kuduConfig.getCount() == 0)
+                kuduSession.flush(); //批量写入kudu
+        }
+
+        // 最后,补加一条批量写入
+        kuduSession.flush();
+        // 关闭和kudu的会话
+        kuduSession.close();
+    }
 }

+ 18 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/service/master/WindturbineService.java

@@ -21,4 +21,22 @@ public interface WindturbineService extends IService<Windturbine> {
      * @return
      */
     List<Windturbine> getWindTurbineId(String station);
+
+    /**
+     * 批量新增
+     * @param list
+     */
+    void insertBatch(List<Windturbine> list);
+
+    /**
+     * 编辑
+     * @param obj
+     */
+    void editItem(Windturbine obj);
+
+    /**
+     * 根据ID删除
+     * @param id
+     */
+    void deleteItem(String id);
 }

+ 3 - 2
gyee-sample-impala/src/main/resources/mapper/master/WindturbineMapper.xml

@@ -11,15 +11,16 @@
         <result column="model" property="model" />
         <result column="projectid" property="projectid" />
         <result column="lineid" property="lineid" />
-        <result column="integratedtime" property="integratedtime" />
+        <result column="time" property="time" />
         <result column="name" property="name" />
         <result column="remark" property="remark" />
         <result column="category" property="category" />
+        <result column="manufacturer" property="manufacturer" />
     </resultMap>
 
     <!-- 通用查询结果列 -->
     <sql id="Base_Column_List">
-        id, station, longitude, latitude, model, projectid, lineid, integratedtime, name, remark, category
+        id, station, longitude, latitude, model, projectid, lineid, integratedtime, name, remark, category, manufacturer
     </sql>
 
 </mapper>