kongdeqiang
2023-02-14 18d6ac5798b9ef96077f592ccc3f94b365006e6a
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
package com.wgcloud.service;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgcloud.dto.SubtitleDto;
import com.wgcloud.entity.AppState;
import com.wgcloud.mapper.AppStateMapper;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.FormatUtil;
import com.wgcloud.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
 
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:AppStateService.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: AppStateService.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class AppStateService {
 
    public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<AppState> list = appStateMapper.selectByParams(params);
        PageInfo<AppState> pageInfo = new PageInfo<AppState>(list);
        return pageInfo;
    }
 
    public void save(AppState AppState) throws Exception {
        AppState.setId(UUIDUtil.getUUID());
        AppState.setCreateTime(DateUtil.getNowTime());
//        AppState.setDateStr(DateUtil.getDateTimeString(AppState.getCreateTime()));
        appStateMapper.save(AppState);
    }
 
    public void saveRecord(List<AppState> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        for (AppState as : recordList) {
            as.setId(UUIDUtil.getUUID());
//             as.setDateStr(DateUtil.getDateTimeString(as.getCreateTime()));
        }
        appStateMapper.insertList(recordList);
    }
 
    public int deleteByAppInfoId(String appInfoId) throws Exception {
        return appStateMapper.deleteByAppInfoId(appInfoId);
    }
 
 
    public int deleteById(String[] id) throws Exception {
        return appStateMapper.deleteById(id);
    }
 
    public AppState selectById(String id) throws Exception {
        return appStateMapper.selectById(id);
    }
 
    public List<AppState> selectAllByParams(Map<String, Object> params) throws Exception {
        return appStateMapper.selectAllByParams(params);
    }
 
    public int deleteByDate(Map<String, Object> map) throws Exception {
        return appStateMapper.deleteByDate(map);
    }
 
    /**
     * 设置图表的副标题,cpu、内存、线程数的最高、平均、最低值
     *
     * @param model
     * @param appStateList 进程状态数据
     */
    public void setSubtitle(Model model, List<AppState> appStateList) {
        //cpu使用率最高
        Double maxCpu = 0d;
        //cpu使用率平均
        Double avgCpu = 0d;
        //cpu使用率最低
        Double minCpu = 1000d;
        Double sumCpu = 0d;
 
        //内存使用率最高
        Double maxMem = 0d;
        //内存使用率最低
        Double minMem = 1000d;
        //内存使用率平均
        Double avgMem = 0d;
        Double sumMem = 0d;
 
        //线程数最高
        Integer maxThreads = 0;
        //线程数最低
        Integer minThreads = 1000;
        //线程数平均
        Double avgThreads = 0d;
        Integer sumThreads = 0;
 
        double cpuPerTmp = 0d;
        double memPerTmp = 0d;
        Integer threadsTmp = 0;
        for (AppState appState : appStateList) {
            if (null != appState.getCpuPer()) {
                cpuPerTmp = Double.valueOf(appState.getCpuPer());
                if (cpuPerTmp > maxCpu) {
                    maxCpu = cpuPerTmp;
                }
                if (cpuPerTmp < minCpu) {
                    minCpu = cpuPerTmp;
                }
                sumCpu += cpuPerTmp;
            }
            if (null != appState.getMemPer()) {
                memPerTmp = Double.valueOf(appState.getMemPer());
                if (memPerTmp > maxMem) {
                    maxMem = memPerTmp;
                }
                if (memPerTmp < minMem) {
                    minMem = memPerTmp;
                }
                sumMem += memPerTmp;
            }
            if (!StringUtils.isEmpty(appState.getThreadsNum())) {
                threadsTmp = Integer.valueOf(appState.getThreadsNum());
                if (threadsTmp > maxThreads) {
                    maxThreads = threadsTmp;
                }
                if (threadsTmp < minThreads) {
                    minThreads = threadsTmp;
                }
                sumThreads += threadsTmp;
            }
        }
        if (appStateList.size() > 0) {
            avgCpu = sumCpu / appStateList.size();
            avgMem = sumMem / appStateList.size();
            avgThreads = sumThreads / (double) appStateList.size();
        } else {
            minCpu = 0d;
            minMem = 0d;
            minThreads = 0;
        }
        SubtitleDto cpuSubtitleDto = new SubtitleDto();
        cpuSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgCpu, 2) + "");
        cpuSubtitleDto.setMaxValue(maxCpu + "");
        cpuSubtitleDto.setMinValue(minCpu + "");
        model.addAttribute("cpuSubtitleDto", cpuSubtitleDto);
 
        SubtitleDto memSubtitleDto = new SubtitleDto();
        memSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgMem, 2) + "");
        memSubtitleDto.setMaxValue(maxMem + "");
        memSubtitleDto.setMinValue(minMem + "");
        model.addAttribute("memSubtitleDto", memSubtitleDto);
 
        SubtitleDto threadsSubtitleDto = new SubtitleDto();
        threadsSubtitleDto.setAvgValue(FormatUtil.formatDouble(avgThreads, 2) + "");
        threadsSubtitleDto.setMaxValue(maxThreads + "");
        threadsSubtitleDto.setMinValue(minThreads + "");
        model.addAttribute("threadsSubtitleDto", threadsSubtitleDto);
    }
 
    @Autowired
    private AppStateMapper appStateMapper;
 
 
}