提交 a82d6d43 编写于 作者: J jialinsun

重构app graph linechart

上级 58e00dfd
......@@ -10,6 +10,7 @@ import java.util.Map.Entry;
import org.unidal.dal.jdbc.DalException;
import org.unidal.lookup.annotation.Inject;
import org.unidal.tuple.Pair;
import com.dianping.cat.Cat;
import com.dianping.cat.app.AppDataCommand;
......@@ -25,13 +26,11 @@ public class AppDataService {
@Inject
private AppConfigManager m_appConfigManager;
public static final String SUCCESS_RATIO = "successRatio";
public static final String SUCCESS_RATIO = "成功率";
public static final String REQUEST_COUNT = "requestCount";
public static final String REQUEST_COUNT = "请求数";
public static final String DELAY_AVG = "delayAvg";
private static final int MAX_SIZE = 288;
public static final String DELAY_AVG = "成功延时(ms)";
public void insert(Date period, int minute, int commandId, int city, int operator, int network, int appVersion,
int connectType, int code, int platform, int count, int responseSumTime, int requestPackage,
......@@ -57,7 +56,7 @@ public class AppDataService {
m_dao.insertData(proto);
}
public Map<String, double[]> queryValue(QueryEntity entity, String type) {
public double[] queryValue(QueryEntity entity, String type) {
int commandId = entity.getCommand();
Date period = entity.getDate();
int city = entity.getCity();
......@@ -70,28 +69,43 @@ public class AppDataService {
List<AppDataCommand> datas;
try {
datas = m_dao.findDataByMinute(commandId, period, city, operator, network, appVersion, connnectType, code,
platform, AppDataCommandEntity.READSET_COUNT_DATA);
int n = calculateSize(entity.getDate().getTime());
if (SUCCESS_RATIO.equals(type)) {
return querySuccessRatio(datas, n);
} else if (REQUEST_COUNT.equals(type)) {
return queryRequestCount(datas, n);
} else if (DELAY_AVG.equals(type)) {
return queryDelayAvg(datas, n);
datas = m_dao.findDataByMinuteCode(commandId, period, city, operator, network, appVersion, connnectType,
code, platform, AppDataCommandEntity.READSET_COUNT_DATA);
Pair<Integer, Map<Integer, List<AppDataCommand>>> dataPair = convert2AppDataCommandMap(datas);
return querySuccessRatio(dataPair);
} else {
datas = m_dao.findDataByMinute(commandId, period, city, operator, network, appVersion, connnectType, code,
platform, AppDataCommandEntity.READSET_COUNT_DATA);
Pair<Integer, Map<Integer, List<AppDataCommand>>> dataPair = convert2AppDataCommandMap(datas);
if (REQUEST_COUNT.equals(type)) {
return queryRequestCount(dataPair);
} else if (DELAY_AVG.equals(type)) {
return queryDelayAvg(dataPair);
}
}
} catch (DalException e) {
} catch (Exception e) {
Cat.logError(e);
}
return new LinkedHashMap<String, double[]>();
return null;
}
private Map<Integer, List<AppDataCommand>> convert2AppDataCommandMap(List<AppDataCommand> fromDatas) {
private Pair<Integer, Map<Integer, List<AppDataCommand>>> convert2AppDataCommandMap(List<AppDataCommand> fromDatas) {
Map<Integer, List<AppDataCommand>> dataMap = new LinkedHashMap<Integer, List<AppDataCommand>>();
int min = -1;
int max = -1;
for (AppDataCommand from : fromDatas) {
int minute = from.getMinuteOrder();
if (min < 0 || min > minute) {
min = minute;
}
if (max < 0 || max < minute) {
max = minute;
}
List<AppDataCommand> data = dataMap.get(minute);
if (data == null) {
......@@ -101,16 +115,15 @@ public class AppDataService {
}
data.add(from);
}
return dataMap;
int n = (max - min) / 5;
return new Pair<Integer, Map<Integer, List<AppDataCommand>>>(n, dataMap);
}
public Map<String, double[]> querySuccessRatio(List<AppDataCommand> datas, int n) {
Map<String, double[]> values = new LinkedHashMap<String, double[]>();
double[] value = new double[n];
public double[] querySuccessRatio(Pair<Integer, Map<Integer, List<AppDataCommand>>> dataPair) {
double[] value = new double[dataPair.getKey()];
Map<Integer, List<AppDataCommand>> dataMap = dataPair.getValue();
try {
Map<Integer, List<AppDataCommand>> dataMap = convert2AppDataCommandMap(datas);
for (Entry<Integer, List<AppDataCommand>> entry : dataMap.entrySet()) {
int key = entry.getKey();
long success = 0;
......@@ -130,8 +143,7 @@ public class AppDataService {
Cat.logError(e);
}
values.put(DELAY_AVG, value);
return values;
return value;
}
private boolean isSuccessStatus(AppDataCommand data) {
......@@ -146,44 +158,31 @@ public class AppDataService {
return false;
}
public Map<String, double[]> queryRequestCount(List<AppDataCommand> datas, int n) {
Map<String, double[]> values = new LinkedHashMap<String, double[]>();
double[] value = new double[n];
public double[] queryRequestCount(Pair<Integer, Map<Integer, List<AppDataCommand>>> dataPair) {
double[] value = new double[dataPair.getKey()];
for (AppDataCommand data : datas) {
long count = data.getAccessNumberSum();
for (Entry<Integer, List<AppDataCommand>> entry : dataPair.getValue().entrySet()) {
for (AppDataCommand data : entry.getValue()) {
long count = data.getAccessNumberSum();
value[data.getMinuteOrder() / 5] = count;
value[data.getMinuteOrder() / 5] = count;
}
}
values.put(DELAY_AVG, value);
return values;
return value;
}
public Map<String, double[]> queryDelayAvg(List<AppDataCommand> datas, int n) {
Map<String, double[]> values = new LinkedHashMap<String, double[]>();
double[] value = new double[n];
public double[] queryDelayAvg(Pair<Integer, Map<Integer, List<AppDataCommand>>> dataPair) {
double[] value = new double[dataPair.getKey()];
for (AppDataCommand data : datas) {
long count = data.getAccessNumberSum();
long sum = data.getResponseSumTimeSum();
for (Entry<Integer, List<AppDataCommand>> entry : dataPair.getValue().entrySet()) {
for (AppDataCommand data : entry.getValue()) {
long count = data.getAccessNumberSum();
long sum = data.getResponseSumTimeSum();
double avg = sum / count;
value[data.getMinuteOrder() / 5] = avg;
}
values.put(DELAY_AVG, value);
return values;
}
private int calculateSize(long startTime) {
int n = MAX_SIZE;
int oneDay = 24 * 3600 * 1000;
if (startTime + oneDay > System.currentTimeMillis()) {
long current = System.currentTimeMillis();
long endTime = current - current % 300000;
n = (int) (endTime - startTime) / 300000;
double avg = sum / count;
value[data.getMinuteOrder() / 5] = avg;
}
}
return n;
return value;
}
}
package com.dianping.cat.report.page.app;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import org.codehaus.plexus.util.StringUtils;
import org.unidal.lookup.annotation.Inject;
import org.unidal.web.mvc.PageHandler;
import org.unidal.web.mvc.annotation.InboundActionMeta;
......@@ -55,34 +50,9 @@ public class Handler implements PageHandler<Context> {
QueryEntity entity1 = payload.getQueryEntity1();
QueryEntity entity2 = payload.getQueryEntity2();
String type = payload.getType();
LineChart lineChart = m_appGraphCreator.buildLineChart(entity1, entity2, type);
if (StringUtils.isEmpty(type)) {
type = "successRatio";
}
LineChart lineCharts = new LineChart();
if (entity1 != null) {
LineChart lineChart1 = m_appGraphCreator.buildLineChart(entity1, type);
Iterator<Map<Long, Double>> idata = lineChart1.getDatas().iterator();
if (lineChart1.getDatas().size() == 1) {
lineCharts.add("查询1", idata.next());
}
}
if (entity2 != null) {
LineChart lineChart2 = m_appGraphCreator.buildLineChart(entity2, type);
Iterator<Map<Long, Double>> idata = lineChart2.getDatas().iterator();
if (lineChart2.getDatas().size() == 1) {
lineCharts.add("查询2", idata.next());
}
}
lineCharts.setId("app");
lineCharts.setHtmlTitle(type);
model.setLineChart(lineCharts);
model.setLineChart(lineChart);
if (!ctx.isProcessStopped()) {
m_jspViewer.view(ctx, model);
......
......@@ -2,8 +2,8 @@ package com.dianping.cat.report.page.app.graph;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import org.unidal.lookup.annotation.Inject;
......@@ -19,47 +19,45 @@ public class AppGraphCreator extends AbstractGraphCreator {
@Inject
private AppDataService m_appDataService;
public LineChart buildLineChart(QueryEntity queryEntity, String type) {
Map<String, double[]> values = prepareAllData(queryEntity, type);
public LineChart buildLineChart(QueryEntity queryEntity1, QueryEntity queryEntity2, String type) {
LinkedList<double[]> dataList = new LinkedList<double[]>();
double[] data1 = prepareAllData(queryEntity1, type);
dataList.add(data1);
long startTime = queryEntity.getDate().getTime();
long endTime = startTime + TimeUtil.ONE_DAY;
Date endDate = new Date(endTime);
Date startDate = new Date(startTime);
if (queryEntity2 != null) {
double[] values2 = prepareAllData(queryEntity2, type);
dataList.add(values2);
}
return buildChartData(values, startDate, endDate);
return buildChartData(dataList, type);
}
private Map<String, double[]> prepareAllData(QueryEntity queryEntity, String type) {
Map<String, double[]> value = m_appDataService.queryValue(queryEntity, type);
private double[] prepareAllData(QueryEntity queryEntity, String type) {
double[] value = m_appDataService.queryValue(queryEntity, type);
return value;
}
public LineChart buildChartData(final Map<String, double[]> datas, Date startDate, Date endDate) {
public LineChart buildChartData(final LinkedList<double[]> dataList, String type) {
LineChart lineChart = new LineChart();
lineChart.setId("app");
lineChart.setHtmlTitle(type);
int i = 1;
for (Entry<String, double[]> entry : datas.entrySet()) {
String key = entry.getKey();
lineChart.setId(startDate.toString());
lineChart.setHtmlTitle(key);
Map<Long, Double> all = convertToMap(datas.get(key), startDate, 5);
lineChart.add(startDate.toString(), all);
for (double[] data : dataList) {
lineChart.add("查询" + i++, data);
}
return lineChart;
}
@Override
protected Map<Long, Double> convertToMap(double[] data, Date start, int step) {
Map<Long, Double> map = new LinkedHashMap<Long, Double>();
int length = data.length;
long startTime = start.getTime();
long time = startTime;
int i = 0;
for (; i < length; i++) {
for (int i = 0; i < length; i++) {
time += step * TimeUtil.ONE_MINUTE;
map.put(time, data[i]);
}
......@@ -68,7 +66,7 @@ public class AppGraphCreator extends AbstractGraphCreator {
}
public class AppDataServiceMock extends AppDataService {
public Map<String, double[]> queryValue(QueryEntity entity, String type) {
public double[] queryValue(QueryEntity entity, String type) {
if (SUCCESS_RATIO.equals(type)) {
return querySuccessRatio(entity);
} else if (REQUEST_COUNT.equals(type)) {
......@@ -76,12 +74,11 @@ public class AppGraphCreator extends AbstractGraphCreator {
} else if (DELAY_AVG.equals(type)) {
return queryDelayAvg(entity);
} else {
return new LinkedHashMap<String, double[]>();
return null;
}
}
private Map<String, double[]> makeMockValue(String type) {
Map<String, double[]> map = new LinkedHashMap<String, double[]>();
private double[] makeMockValue(String type) {
long startTime = TimeUtil.getCurrentDay().getTime();
long current = System.currentTimeMillis();
long endTime = current - current % 300000;
......@@ -91,24 +88,22 @@ public class AppGraphCreator extends AbstractGraphCreator {
for (int i = 0; i < n; i++) {
value[i] = (new Random().nextDouble() + 1) * 100;
}
map.put(type, value);
return map;
return value;
}
private Map<String, double[]> querySuccessRatio(QueryEntity entity) {
private double[] querySuccessRatio(QueryEntity entity) {
return makeMockValue(SUCCESS_RATIO);
}
private Map<String, double[]> queryDelayAvg(QueryEntity entity) {
private double[] queryDelayAvg(QueryEntity entity) {
return makeMockValue(DELAY_AVG);
}
private Map<String, double[]> queryRequestCount(QueryEntity entity) {
private double[] queryRequestCount(QueryEntity entity) {
return makeMockValue(REQUEST_COUNT);
}
}
}
......@@ -105,7 +105,7 @@ function graphMetricChart(container, data) {
},
title : {
text : data.htmlTitle,
useHTML: true
useHTML : true
},
xAxis : {
type : 'datetime',
......@@ -121,9 +121,9 @@ function graphMetricChart(container, data) {
},
yAxis : {
min : ylabelMin,
title: {
text: data.unit,
}
title : {
text : data.unit,
}
},
credits : {
enabled : false
......@@ -147,17 +147,17 @@ function graphMetricChart(container, data) {
tooltip : {
allowPointSelect : false,
formatter : function() {
var number0 = Number(this.y).toFixed(0);
var number0 = Number(this.y).toFixed(0);
var number1 = Number(this.y).toFixed(1);
var number = number1;
if(Number(number1)==Number(number0)){
if (Number(number1) == Number(number0)) {
number = number0;
}
return Highcharts.dateFormat('%Y-%m-%d %H:%M',
this.x)
+ '<br/>['+ this.series.name + '] '+ '<b>' + number + '</b>';
return Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x)
+ '<br/>[' + this.series.name + '] ' + '<b>'
+ number + '</b>';
}
},
series : _data
......@@ -169,15 +169,7 @@ function parseMetricLineDataForApp(data) {
data.subTitles.forEach(function(title, i) {
var series = {}
series.name = title;
series.data = [];
var map = data.datas[i];
var j = 0;
for ( var key in map) {
var value = map[key];
series.data[j] = value;
j++;
}
series.data = data.values[i];
res.push(series);
});
return res;
......@@ -198,24 +190,24 @@ function graphMetricChartForApp(container, data) {
},
title : {
text : data.htmlTitle,
useHTML: true
useHTML : true
},
xAxis : {
type : "category",
labels : {
step : 12,
maxStaggerLines : 1,
formatter: function() {
return this.value / 12;
}
},
max : 288
labels : {
step : 12,
maxStaggerLines : 1,
formatter : function() {
return this.value / 12;
}
},
max : 288
},
yAxis : {
min : ylabelMin,
title: {
text: data.unit,
}
title : {
text : data.unit,
}
},
credits : {
enabled : false
......@@ -239,17 +231,17 @@ function graphMetricChartForApp(container, data) {
tooltip : {
allowPointSelect : false,
formatter : function() {
var number0 = Number(this.y).toFixed(0);
var number0 = Number(this.y).toFixed(0);
var number1 = Number(this.y).toFixed(1);
var number = number1;
if(Number(number1)==Number(number0)){
if (Number(number1) == Number(number0)) {
number = number0;
}
return Highcharts.dateFormat('%Y-%m-%d %H:%M',
this.x)
+ '<br/>['+ this.series.name + '] '+ '<b>' + number + '</b>';
return Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x)
+ '<br/>[' + this.series.name + '] ' + '<b>'
+ number + '</b>';
}
},
series : _data
......@@ -270,7 +262,7 @@ function graphLineChart(container, data) {
},
title : {
text : data.title,
useHTML: true
useHTML : true
},
xAxis : {
type : 'datetime',
......
......@@ -295,11 +295,11 @@
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-info"> <input type="radio"
name="typeCheckbox" value="successRatio" onclick="query()" >成功率
name="typeCheckbox" value="成功率" >成功率
</label> <label class="btn btn-info"> <input type="radio"
name="typeCheckbox" value="requestCount" onclick="query()" >请求数
name="typeCheckbox" value="请求数" >请求数
</label> <label class="btn btn-info"> <input type="radio"
name="typeCheckbox" value="delayAvg" onclick="query()" >成功延时(ms)
name="typeCheckbox" value="成功延时(ms)" >成功延时(ms)
</label>
</div>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册