use of com.sohu.cache.entity.AppClientCostTimeStat in project cachecloud by sohutv.
the class ClientReportCostDistriServiceImpl method batchSave.
@Override
public void batchSave(ClientReportBean clientReportBean) {
try {
// 1.client上报
final String clientIp = clientReportBean.getClientIp();
final long collectTime = clientReportBean.getCollectTime();
final long reportTime = clientReportBean.getReportTimeStamp();
final List<Map<String, Object>> datas = clientReportBean.getDatas();
if (datas == null || datas.isEmpty()) {
logger.warn("datas field {} is empty", clientReportBean);
return;
}
// 2.结果集
List<AppClientCostTimeStat> appClientCostTimeStatList = new ArrayList<AppClientCostTimeStat>();
// 3.解析结果
for (Map<String, Object> map : datas) {
Integer clientDataType = MapUtils.getInteger(map, ClientReportConstant.CLIENT_DATA_TYPE, -1);
ClientCollectDataTypeEnum clientCollectDataTypeEnum = ClientCollectDataTypeEnum.MAP.get(clientDataType);
if (clientCollectDataTypeEnum == null) {
continue;
}
if (ClientCollectDataTypeEnum.COST_TIME_DISTRI_TYPE.equals(clientCollectDataTypeEnum)) {
AppClientCostTimeStat appClientCostTimeStat = generate(clientIp, collectTime, reportTime, map);
if (appClientCostTimeStat != null) {
appClientCostTimeStatList.add(appClientCostTimeStat);
}
}
}
if (CollectionUtils.isNotEmpty(appClientCostTimeStatList)) {
// 4.批量保存
appClientCostTimeStatDao.batchSave(appClientCostTimeStatList);
// 5.合并app统计结果
List<AppClientCostTimeTotalStat> appClientCostTimeTotalStatList = mergeAppClientCostTimeStat(appClientCostTimeStatList);
if (CollectionUtils.isNotEmpty(appClientCostTimeTotalStatList)) {
appClientCostTimeTotalStatDao.batchSave(appClientCostTimeTotalStatList);
}
// 6.保存应用下节点和客户端关系
appInstanceClientRelationService.batchSave(appClientCostTimeStatList);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
use of com.sohu.cache.entity.AppClientCostTimeStat in project cachecloud by sohutv.
the class ClientReportCostDistriServiceImpl method mergeAppClientCostTimeStat.
/**
* 合并以app为单位的
* @param appClientCostTimeStatList
*/
private List<AppClientCostTimeTotalStat> mergeAppClientCostTimeStat(List<AppClientCostTimeStat> appClientCostTimeStatList) {
// 1. merge后的结果
List<AppClientCostTimeTotalStat> resultList = new ArrayList<AppClientCostTimeTotalStat>();
// 2. 以appid_command_collectTime为key,拆分appClientCostTimeStatList
Map<String, List<AppClientCostTimeStat>> map = new HashMap<String, List<AppClientCostTimeStat>>();
for (AppClientCostTimeStat appClientCostTimeStat : appClientCostTimeStatList) {
long appId = appClientCostTimeStat.getAppId();
String command = appClientCostTimeStat.getCommand();
long collectTime = appClientCostTimeStat.getCollectTime();
String key = appId + "_" + command + "_" + collectTime;
if (map.containsKey(key)) {
map.get(key).add(appClientCostTimeStat);
} else {
List<AppClientCostTimeStat> list = new ArrayList<AppClientCostTimeStat>();
list.add(appClientCostTimeStat);
map.put(key, list);
}
}
// 3.生成结果
for (Entry<String, List<AppClientCostTimeStat>> entry : map.entrySet()) {
String key = entry.getKey();
String[] items = key.split("_");
long appId = NumberUtils.toLong(items[0]);
String command = items[1];
long collectTime = NumberUtils.toLong(items[2]);
double totalCost = 0.0;
long totalCount = 0;
int median = 0;
int ninetyPercentMax = 0;
int ninetyNinePercentMax = 0;
int hundredMax = 0;
String maxInstanceHost = "";
int maxInstancePort = 0;
long maxInstanceId = 0;
String maxClientIp = "";
double mean = 0.0;
for (AppClientCostTimeStat appClientCostTimeStat : entry.getValue()) {
AppClientCostTimeTotalStat appClientCostTimeTotalStat = AppClientCostTimeTotalStat.getFromAppClientCostTimeStat(appClientCostTimeStat);
totalCost += appClientCostTimeTotalStat.getTotalCost();
totalCount += appClientCostTimeTotalStat.getTotalCount();
if (appClientCostTimeTotalStat.getMedian() > median) {
median = appClientCostTimeTotalStat.getMedian();
}
if (appClientCostTimeTotalStat.getNinetyPercentMax() > ninetyPercentMax) {
ninetyPercentMax = appClientCostTimeTotalStat.getNinetyPercentMax();
}
if (appClientCostTimeTotalStat.getNinetyNinePercentMax() > ninetyNinePercentMax) {
ninetyNinePercentMax = appClientCostTimeTotalStat.getNinetyNinePercentMax();
}
if (appClientCostTimeTotalStat.getHundredMax() > hundredMax) {
hundredMax = appClientCostTimeTotalStat.getHundredMax();
maxInstanceHost = appClientCostTimeTotalStat.getMaxInstanceHost();
maxInstancePort = appClientCostTimeTotalStat.getMaxInstancePort();
maxInstanceId = appClientCostTimeTotalStat.getMaxInstanceId();
maxClientIp = appClientCostTimeTotalStat.getMaxClientIp();
}
}
DecimalFormat df = new DecimalFormat("0.00");
totalCost = NumberUtils.toDouble(df.format(totalCost));
//平均值
if (totalCount > 0) {
mean = totalCost / totalCount;
mean = NumberUtils.toDouble(df.format(mean));
}
//添加到结果集
resultList.add(new AppClientCostTimeTotalStat(-1, appId, collectTime, new Date(), command, totalCount, totalCost, median, mean, ninetyPercentMax, ninetyNinePercentMax, hundredMax, maxInstanceHost, maxInstancePort, maxInstanceId, maxClientIp));
}
return resultList;
}
use of com.sohu.cache.entity.AppClientCostTimeStat in project cachecloud by sohutv.
the class AppClientDataShowController method doGetAppClientInstanceCommandCost.
/**
* 获取指定时间内某个命令某个客户端和实例的统计数据
* @param appId
*/
@RequestMapping("/getAppClientInstanceCommandCost")
public ModelAndView doGetAppClientInstanceCommandCost(HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
final String costDistriDateFormat = "yyyy-MM-dd";
long appId = NumberUtils.toLong(request.getParameter("appId"));
//时间转换
String costDistriStartDate = request.getParameter("costDistriStartDate");
String costDistriEndDate = request.getParameter("costDistriEndDate");
Date startDate = DateUtil.parse(costDistriStartDate, costDistriDateFormat);
Date endDate = DateUtil.parse(costDistriEndDate, costDistriDateFormat);
long startTime = NumberUtils.toLong(DateUtil.formatDate(startDate, COLLECT_TIME_FORMAT));
long endTime = NumberUtils.toLong(DateUtil.formatDate(endDate, COLLECT_TIME_FORMAT));
String firstCommand = request.getParameter("firstCommand");
long instanceId = NumberUtils.toLong(request.getParameter("instanceId"));
String clientIp = request.getParameter("clientIp");
//客户端和实例统计
List<AppClientCostTimeStat> clientInstanceChartStatList = clientReportCostDistriService.getAppCommandClientToInstanceStat(appId, firstCommand, instanceId, clientIp, startTime, endTime);
//缩减字段
List<Map<String, Object>> clientInstanceStat = new ArrayList<Map<String, Object>>();
for (AppClientCostTimeStat appClientCostTimeStat : clientInstanceChartStatList) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("timeStamp", appClientCostTimeStat.getTimeStamp());
map.put("count", appClientCostTimeStat.getCount());
map.put("mean", appClientCostTimeStat.getMean());
map.put("median", appClientCostTimeStat.getMedian());
map.put("max90", appClientCostTimeStat.getNinetyPercentMax());
map.put("max99", appClientCostTimeStat.getNinetyNinePercentMax());
map.put("max100", appClientCostTimeStat.getHundredMax());
clientInstanceStat.add(map);
}
//生成数据map json
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("clientInstanceStat", clientInstanceStat);
sendMessage(response, JSONObject.toJSONString(resultMap));
return null;
}
use of com.sohu.cache.entity.AppClientCostTimeStat in project cachecloud by sohutv.
the class ClientReportCostDistriServiceImpl method generate.
private AppClientCostTimeStat generate(String clientIp, long collectTime, long reportTime, Map<String, Object> map) {
try {
Integer count = MapUtils.getInteger(map, ClientReportConstant.COST_COUNT, 0);
String command = MapUtils.getString(map, ClientReportConstant.COST_COMMAND, "");
if (StringUtils.isBlank(command)) {
logger.warn("command is empty!");
return null;
}
String hostPort = MapUtils.getString(map, ClientReportConstant.COST_HOST_PORT, "");
if (StringUtils.isBlank(hostPort)) {
logger.warn("hostPort is empty", hostPort);
return null;
}
int index = hostPort.indexOf(":");
if (index <= 0) {
logger.warn("hostPort {} format is wrong", hostPort);
return null;
}
String host = hostPort.substring(0, index);
int port = NumberUtils.toInt(hostPort.substring(index + 1));
// 实例信息
InstanceInfo instanceInfo = clientReportInstanceService.getInstanceInfoByHostPort(host, port);
if (instanceInfo == null) {
// logger.warn("instanceInfo is empty, host is {}, port is {}", host, port);
return null;
}
long appId = instanceInfo.getAppId();
// 耗时分布详情
double mean = MapUtils.getDouble(map, ClientReportConstant.COST_TIME_MEAN, 0.0);
Integer median = MapUtils.getInteger(map, ClientReportConstant.COST_TIME_MEDIAN, 0);
Integer ninetyPercentMax = MapUtils.getInteger(map, ClientReportConstant.COST_TIME_90_MAX, 0);
Integer ninetyNinePercentMax = MapUtils.getInteger(map, ClientReportConstant.COST_TIME_99_MAX, 0);
Integer hunredMax = MapUtils.getInteger(map, ClientReportConstant.COST_TIME_100_MAX, 0);
AppClientCostTimeStat stat = new AppClientCostTimeStat();
stat.setAppId(appId);
stat.setClientIp(clientIp);
stat.setReportTime(new Date(reportTime));
stat.setCollectTime(collectTime);
stat.setCreateTime(new Date());
stat.setCommand(command);
stat.setCount(count);
stat.setInstanceHost(host);
stat.setInstancePort(port);
stat.setMean(NumberUtils.toDouble(new DecimalFormat("#.00").format(mean)));
stat.setMedian(median);
stat.setNinetyPercentMax(ninetyPercentMax);
stat.setNinetyNinePercentMax(ninetyNinePercentMax);
stat.setHundredMax(hunredMax);
stat.setInstanceId(instanceInfo.getId());
return stat;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
Aggregations