DatabaseDataFetcher.java
1.9 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
package com.dianping.cat.demo;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.unidal.helper.Files;
import org.unidal.helper.Urls;
import org.unidal.webres.json.JsonArray;
import org.unidal.webres.json.JsonObject;
public class DatabaseDataFetcher {
public String url = "http://cat.dp/cat/r/database?op=view&date=2015060%s23&domain=cat&product=db-mysql-rcpt01.nh[10.1.101.131]&timeRange=24&forceDownload=json";
@Test
public void test() {
for (int n = 2; n < 7; n++) {
try {
InputStream stream = Urls.forIO().connectTimeout(5000).readTimeout(10000).openStream(String.format(url, n));
String result = Files.forIO().readFrom(stream, "utf-8");
JsonObject jo = new JsonObject(result);
JsonArray array = jo.getJSONArray("lineCharts");
Map<Long, Double> datas = new LinkedHashMap<Long, Double>();
for (int i = 0; i < array.length(); i++) {
JsonObject o = array.getJSONObject(i);
String title = o.getString("title");
if (title.contains("THREADS_RUNNING")) {
JsonArray arys = o.getJSONArray("datas");
JsonObject object = arys.getJSONObject(0);
JsonArray names = object.names();
List<Long> sortedNames = new ArrayList<Long>();
for (int k = 0; k < names.length(); k++) {
String key = names.getString(k);
sortedNames.add(Long.parseLong(key));
}
Collections.sort(sortedNames);
for (int j = 0; j < sortedNames.size(); j++) {
Long key = sortedNames.get(j);
datas.put(key, Double.parseDouble(object.getString(String.valueOf(key))));
}
}
}
System.out.println(datas);
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}