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;
|
|
|
}
|