xyc
2024-11-07 769ba15e9071b01cb53a65e8fdf4c8f37ac005b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.zt.life.modules.mainPart.taskReliability.controller;
 
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zt.common.annotation.LogOperation;
import com.zt.common.annotation.QueryParam;
import com.zt.common.constant.Constant;
import com.zt.common.db.query.QueryFilter;
import com.zt.common.exception.RenException;
import com.zt.common.servlet.PageResult;
import com.zt.common.servlet.Result;
import com.zt.common.validator.AssertUtils;
import com.zt.common.validator.ValidatorUtils;
import com.zt.common.validator.group.DefaultGroup;
import com.zt.common.validator.group.UpdateGroup;
import com.zt.life.modules.mainPart.async.PythonLib;
import com.zt.life.modules.mainPart.taskReliability.dto.*;
import com.zt.life.modules.mainPart.taskReliability.model.*;
import com.zt.life.modules.mainPart.taskReliability.service.SimulatAssessService;
import com.zt.life.modules.mainPart.taskReliability.service.TaskService;
import com.zt.life.modules.mainPart.utils.OSUtils.ProcessUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
 
 
/**
 * simulat_assess
 *
 * @author zt generator
 * @since 1.0.0 2024-03-20
 */
@RestController
@RequestMapping("/taskReliability/SimulatAssess/")
@Api(tags = "simulat_assess")
public class SimulatAssessController {
    private static final Logger logger = LoggerFactory.getLogger(SimulatAssessController.class);
 
    @Autowired
    private RedisTemplate redisTemplate;
 
 
    @Autowired
    private SimulatAssessService simulatAssessService;
 
    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SimulatAssess> get(@PathVariable("id") Long id) {
        SimulatAssess data = simulatAssessService.get(id);
 
        return Result.ok(data);
    }
 
    @GetMapping("getSimulatList")
    @ApiOperation("信息")
    public Result<List<SimulatAssess>> getSimulatList(Long taskModelId) {
        List<SimulatAssess> data = simulatAssessService.getList(taskModelId);
 
        return Result.ok(data);
    }
 
    @GetMapping("getSimulatParams")
    @ApiOperation("信息")
    public Result<SimulatAssess> getSimulatParams(Long id, Long taskModelId) {
        SimulatAssess data = simulatAssessService.getParams(id, taskModelId);
        return Result.ok(data);
    }
 
    @GetMapping("verify")
    public Result<String> verify(Long taskId) {
        String result = simulatAssessService.verify(taskId);
        result = result.replaceAll("\n", "<br>");
        return Result.ok(result);
    }
 
    @PostMapping("analyze")
    public Result<Object> analyze(@RequestBody SimulatAssess simulatAssess) {
        //效验数据
//        Result result = pythonLib.callPython(simulatAssess);
        List<TaskModelCheckResultDto> result = simulatAssessService.simulateChecK(simulatAssess);
        if (result != null) {
            return Result.ok(result);
        }
        Boolean result2 = simulatAssessService.simulate(simulatAssess);
        if (result2)
            return Result.ok();
        else
            return Result.error("算法库计算失败!");
    }
 
    @GetMapping("process")
    @ApiOperation("获取算法库进程信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.Q.PAGE, value = Constant.QV.PAGE, required = true, dataType = Constant.QT.INT),
            @ApiImplicitParam(name = Constant.Q.LIMIT, value = Constant.QV.LIMIT, required = true, dataType = Constant.QT.INT),
            @ApiImplicitParam(name = "isAlive", value = "存活", dataType = Constant.QT.Boolean),
    })
    public PageResult<SimulatAssess> process(@ApiIgnore @QueryParam QueryFilter queryFilter) {
        List<SimulatAssess> page = simulatAssessService.process(queryFilter);
        return PageResult.ok(page);
    }
 
 
    @PostMapping("getResultXML")
    public Result<SimulaDataDto> getResultXML(@RequestBody SimulatAssess simulatAssess) {
        SimulaDataDto data = simulatAssessService.getResultXML(simulatAssess);
        return Result.ok(data);
    }
 
    @PostMapping("getResultData")
    public Result<SimulaDataDto> getData(@RequestBody SimulatAssess simulatAssess) {
        SimulaDataDto data = simulatAssessService.getResultData(simulatAssess);
        return Result.ok(data);
    }
 
    @GetMapping("getStatus")
    public Result<TimeDiagramDto> getTimeDiagram(Long productId, Long taskId, Long fzId, double smallWidth, double minPointNum) {
        TimeDiagramDto data = simulatAssessService.getTimeDiagram(productId, taskId, fzId, smallWidth, minPointNum);
        return Result.ok(data);
    }
 
    @GetMapping("getDiagram")
    @ApiOperation("查询流程图")
    @LogOperation("查询流程图")
    public Result<TimeDiagram> getDiagram(String projectId, String diagramId, String showType, String isShow, String digramParams, String majorId, Integer winWidth, Integer winHeight) throws Exception {
        if (StringUtils.isBlank(diagramId))
            diagramId = projectId;
        TimeDiagram diagram = simulatAssessService.getDiagram(projectId, diagramId, showType, isShow, digramParams, majorId, winWidth, winHeight);
        return Result.ok(diagram);
    }
 
    @PutMapping("saveDiagram")
    @ApiOperation("查询流程图")
    @LogOperation("查询流程图")
    public Result saveDiagram(@RequestBody TimeDiagram diagram) {
        // 效验数据
        //ValidatorUtils.validateEntity(project.getProject(), UpdateGroup.class, DefaultGroup.class);
        if (diagram.getConfigChange() == null)
            diagram.setConfigChange(0);
        diagram.setSaveStatus(2);
        simulatAssessService.updateDiagram(diagram);
        //simulatAssessService.combineDiagram(diagram);
        return Result.ok();
    }
 
    @GetMapping("getCalcProgress")
    public Result getCalcProgress(@RequestParam("taskList[]") Long[] taskList) {
        Integer progress = 0;
        String progress2 = "";
        String key = null;
        if (taskList.length == 1) {
            Long taskId = taskList[0];
            SimulatAssess simulatAssess = simulatAssessService.get(taskId);
            boolean isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid());
            key = taskId.toString() + simulatAssessService.RELIA_SIM_TASK_TYPE_PROGRESS;
            progress2 = (String) redisTemplate.opsForValue().get(key);
            if (progress2 == null) progress2 = "0";
            progress = Integer.parseInt(progress2);
            if (progress == null) progress = 0;
            updateSimulateAssess(simulatAssess, isProcessAlive, progress);
            if (progress < 100 && !isProcessAlive) {
                logger.error("算法库进程在计算途中异常终止了:taskId="+taskId+" pid="+simulatAssess.getPid()+" progress="+progress);
                simulatAssessService.deleteSimInfoInRedis(taskId);
                return Result.error("算法库进程在计算途中异常终止了!");
            }
            if (progress == 100) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid());
                simulatAssess.setProcessIsAlive(isProcessAlive);
                simulatAssessService.update(simulatAssess);
//                simulatAssessService.deleteSimInfoInRedis(taskId);
            }
        } else {
            for (Long taskId : taskList) {
                int progTmp = 0;
                SimulatAssess simulatAssess = simulatAssessService.get(taskId);
                boolean isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid());
                key = taskId.toString() + simulatAssessService.RELIA_SIM_TASK_TYPE_PROGRESS;
                progress2 = (String) redisTemplate.opsForValue().get(key);
                if (progress2 == null) progress2 = "0";
                progTmp = Integer.parseInt(progress2);
                updateSimulateAssess(simulatAssess, isProcessAlive, progTmp);
                if (progTmp < 100 && !isProcessAlive) {
                    logger.error("算法库进程在计算途中异常终止了:taskId="+taskId+" pid="+simulatAssess.getPid()+" progress="+progTmp);
//                    simulatAssessService.deleteSimInfoInRedis(taskId);
                    return Result.error("算法库进程在计算途中异常终止了!");
                }
                if (progTmp == 100) {
                    try {
                        Thread.sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid());
                    simulatAssess.setProcessIsAlive(isProcessAlive);
                    simulatAssessService.update(simulatAssess);
//                    simulatAssessService.deleteSimInfoInRedis(taskId);
                }
                progress = progress + progTmp;
            }
            progress = progress / taskList.length;
        }
 
        return Result.ok(progress.toString());
    }
 
    private void updateSimulateAssess(SimulatAssess simulatAssess, boolean isProcessAlive, int progress) {
        if (!isProcessAlive) {
            simulatAssess.setProcessIsAlive(false);
            simulatAssess.setProgress(progress);
            simulatAssess.setProcessEndTime(new Date());
            simulatAssessService.update(simulatAssess);
        } else {
            if (simulatAssess.getProgress() != progress) {
                simulatAssess.setProgress(progress);
                simulatAssessService.update(simulatAssess);
            }
        }
    }
 
    @GetMapping("ReliabilityWeakness")
    public Result<List<WeakDto>> getReliabilityWeakness(Long fzId, Long taskId, Long productId) {
        List<WeakDto> data = simulatAssessService.getReliabilityWeakness(fzId, taskId, productId);
        return Result.ok(data);
    }
 
    @GetMapping("SchemeCompar")
    public Result<SimulatResult> SchemeCompar(@RequestParam("taskList[]") String[] taskList, Long showProductId, Integer samplPeriod, Integer simulatFrequency) {
        SimulatResult data = simulatAssessService.SchemeCompar(taskList, showProductId, samplPeriod, simulatFrequency);
        return Result.ok(data);
    }
 
    @GetMapping("getEcharts")
    public Result<SchemeComparDto> getEcharts(@RequestParam("taskList[]") String[] taskList, Long showProductId) {
        SchemeComparDto data = simulatAssessService.getEcharts(taskList, showProductId);
        return Result.ok(data);
    }
 
    @GetMapping("killProcess")
    public Result<SchemeComparDto> killProcess(@RequestParam("taskId") Long taskId) {
        SimulatAssess simulatAssess = simulatAssessService.get(taskId);
        ProcessUtils.killProcess(simulatAssess.getPid());
        boolean isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid());
        simulatAssess.setProcessIsAlive(isProcessAlive);
        simulatAssessService.update(simulatAssess);
        return Result.ok();
    }
 
    @GetMapping("downloadXml")
    public void downloadXml(HttpServletRequest request, HttpServletResponse response, String type,Long id,Integer xml) {
        simulatAssessService.downloadXml(request, response,type,id,xml);
    }
 
    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody SimulatAssess simulatAssess) {
        //效验数据
        ValidatorUtils.validateEntity(simulatAssess, UpdateGroup.class, DefaultGroup.class);
        simulatAssessService.update(simulatAssess);
 
        return Result.ok();
    }
 
    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        simulatAssessService.delete(ids);
 
        return Result.ok();
    }
 
 
}