123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package com.gyee.impala.controller.diagnose;
- import com.alibaba.fastjson.JSONObject;
- 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.DataInfo;
- import com.gyee.impala.model.custom.diagnose.ExecuteInfo;
- import com.gyee.impala.model.master.Casefault;
- import com.gyee.impala.model.master.diagnose.Diagnosepoint;
- import com.gyee.impala.service.custom.diagnose.DataPointService;
- import com.gyee.impala.service.master.CasefaultService;
- import com.gyee.impala.service.master.diagnose.TrainDataModeService;
- import org.apache.kudu.client.ListTablesResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * 数据源方式训练模型
- */
- @CrossOrigin
- @RestController
- @RequestMapping("/api/traindatamode")
- public class TrainDataModeController {
- @Autowired
- private DataPointService dataService;
- @Autowired
- private CasefaultService casefaultService;
- @Autowired
- TrainDataModeService trainDataModeService;
- /**
- * 查询数据库的表
- * @return
- */
- @GetMapping("/tables")
- public JSONObject getListTables(){
- List<Map<String, String>> list = new ArrayList<>();
- try {
- List<ListTablesResponse.TableInfo> tables = trainDataModeService.getListTables();
- tables.stream().filter(a -> a.getTableName().equals("impala::gyee_sample_kudu.casefault")).forEach(obj -> {
- Map<String, String> map = new HashMap<>();
- String name = obj.getTableName().substring(obj.getTableName().lastIndexOf(".") + 1);
- map.put("tableId", obj.getTableId());
- map.put("tableName", name);
- list.add(map);
- });
- } catch (Exception e) { e.getMessage(); }
- return JsonResult.successData(ResultCode.SUCCESS,list);
- }
- /**
- * 查询数据库表的列
- * @param table
- * @return
- */
- @GetMapping("/columns")
- public JSONObject getColumns(String table){
- Object columns = null;
- try {
- columns = trainDataModeService.getColumns(table);
- } catch (Exception e) { e.getMessage(); }
- return JsonResult.successData(ResultCode.SUCCESS, columns);
- }
- /** 查询样本原始数据 **/
- @GetMapping("/data")
- public JSONObject getData(String sql){
- List<Casefault> list = casefaultService.executeSql(sql);
- return JsonResult.successData(ResultCode.SUCCESS, list);
- }
- /** 开始训练 查询 golden 所有原始数据
- * flag ture: 所有数据
- * flag false: 前10条数据
- * **/
- @PostMapping("/pointdata")
- public JSONObject getPointData(@RequestBody JSONObject json){
- if (json == null)
- return JsonResult.error(ResultCode.PARAM_IS_BLANK);
- json = json.getJSONObject("params");
- boolean flag = json.getBooleanValue("flag");
- List<Diagnosepoint> points = JSONObject.parseArray(json.getJSONArray("points").toString(), Diagnosepoint.class);
- List<Casefault> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefault.class);
- Map<String, Object> map = new HashMap<>();
- /** 组装数据 删除添加的故障类型**/
- dataService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
- ExecuteInfo executeInfo = new ExecuteInfo();
- Calendar cal = Calendar.getInstance();
- DataInfo[] dataInfos = new DataInfo[faults.size()];
- for (int i = 0; i < faults.size(); i++){
- DataInfo data = new DataInfo();
- data.setId(Long.valueOf(faults.get(i).getId()));
- data.setStationId(faults.get(i).getStationen());
- data.setThingId(faults.get(i).getWindturbineid());
- data.setModelId(faults.get(i).getModel());
- data.setTag(faults.get(i).getFaultcode());
- data.setFaultTime(faults.get(i).getStarttime());
- data.setStartTs(DateUtil.covertDateTimestamp(faults.get(i).getStarttime()).toString());
- if (flag){
- data.setEndTs(DateUtil.covertDateTimestamp(faults.get(i).getEndtime()).toString());
- }else{
- //查看前10条数据时结束时间往后推10s
- cal.setTime(DateUtil.parseStrtoDate(faults.get(i).getStarttime(), DateUtil.DATE_TIME_PATTERN));
- cal.add(Calendar.SECOND, 10);
- data.setEndTs(cal.getTimeInMillis() + "");
- }
- dataInfos[i] = data;
- }
- executeInfo.setDataInfos(dataInfos);
- if (flag){
- String file = dataService.getFormDataAll(executeInfo);
- map.put("info", executeInfo);
- map.put("filename", file);
- return JsonResult.successData(ResultCode.SUCCESS, map);
- }else {
- Map<String, Object> mp = dataService.getFormData(executeInfo);
- return JsonResult.successData(ResultCode.SUCCESS, mp);
- }
- }
- }
|