AppDataFetcher.java
2.93 KB
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
package com.dianping.cat.demo;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import org.unidal.helper.Files;
import org.unidal.helper.Urls;
import org.unidal.lookup.ComponentTestCase;
import org.unidal.lookup.util.StringUtils;
import org.unidal.webres.json.JsonArray;
import org.unidal.webres.json.JsonObject;
import com.dianping.cat.config.app.AppConfigManager;
import com.dianping.cat.configuration.app.entity.Code;
import com.dianping.cat.configuration.app.entity.Command;
public class AppDataFetcher extends ComponentTestCase {
private String format = "http://cat.dianpingoa.com/cat/r/app?op=piechartJson&query1=2015-03-15;%s;;;;1;;;;00:00;23:59&groupByField=code&domains=All";
@Test
public void test() {
AppConfigManager manager = lookup(AppConfigManager.class);
Map<Integer, Command> rawCommands = manager.getRawCommands();
Map<Integer, Code> codes = manager.getConfig().getCodes();
for (Command command : rawCommands.values()) {
for (Code code : command.getCodes().values()) {
if (!codes.containsKey(code.getId())) {
codes.put(code.getId(), code);
}
}
}
Map<Integer, String> codeMap = new LinkedHashMap<Integer, String>();
System.out.print("命令字, 项目, 标题, ");
for (Code code : codes.values()) {
codeMap.put(code.getId(), code.getName());
}
for (Entry<Integer, String> code : codeMap.entrySet()) {
System.out.print(code.getValue() + ", ");
}
System.out.print("\n");
for (Command command : rawCommands.values()) {
try {
String url = String.format(format, command.getId());
InputStream input = Urls.forIO().connectTimeout(5000).openStream(url);
String result = Files.forIO().readFrom(input, "utf-8");
JsonObject obj = new JsonObject(result);
JsonArray piechart = obj.getJSONArray("pieChartDetails");
Map<Integer, BigDecimal> datas = new HashMap<Integer, BigDecimal>();
for (int i = 0; i < piechart.length(); i++) {
JsonObject detail = new JsonObject(piechart.getString(i));
int id = Integer.parseInt(detail.get("id").toString());
double value = Double.parseDouble(detail.get("requestSum").toString());
BigDecimal bigValue = new BigDecimal(value);
datas.put(id, bigValue);
}
String domain = command.getDomain();
if (StringUtils.isEmpty(domain) || domain.equals("0")) {
domain = "无";
}
String title = command.getTitle();
if (StringUtils.isEmpty(title) || title.equals("0")) {
title = "无";
}
System.out.print(command.getName() + ", " + domain + ", " + title + ", ");
for (Entry<Integer, String> code : codeMap.entrySet()) {
BigDecimal value = datas.get(code.getKey());
if (value == null) {
value = new BigDecimal(0);
}
System.out.print(value + ", ");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.print("\n");
}
}
}