forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetricAdmin.java
More file actions
135 lines (124 loc) · 4.79 KB
/
MetricAdmin.java
File metadata and controls
135 lines (124 loc) · 4.79 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package act.metric;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.cli.CliContext;
import act.cli.Command;
import act.cli.Optional;
import act.util.PropertySpec;
import org.osgl.$;
import org.osgl.util.C;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
/**
* Console app to access actframework metric data
*/
public class MetricAdmin {
@Command(name = "act.metric.sync")
public void updateMetricDataSync(
@Optional("pause sync") boolean pause,
CliContext context
) {
Act.metricPlugin().enableDataSync(!pause);
context.println(pause ? "metric data sync paused." : "metric data sync enabled");
}
@Command(name = "act.metric.counter.list", help = "list all counters")
@PropertySpec("name,count")
public Object getCounters(
@Optional("specify maximum items returned") Integer limit,
@Optional("display in tree view") boolean tree,
@Optional("specify depth of levels") Integer depth,
@Optional("specify search string") String q,
@Optional("including classloading metric") boolean classLoading
) {
List<MetricInfo> list = Act.metricPlugin().metricStore().timers();
if (!classLoading) {
list = withoutClassLoading(list);
}
return process(list, limit, q, tree, depth, MetricInfo.Comparator.COUNTER, MetricInfoTree.COUNTER);
}
@Command(name = "act.metric.timer.list", help = "list all timers")
@PropertySpec("name,accumulated,count,avg")
public Object getTimers(
@Optional("specify maximum items returned") Integer limit,
@Optional("display in tree view") boolean tree,
@Optional("specify depth of levels") Integer depth,
@Optional("specify search string") String q,
@Optional("including classloading metric") boolean classLoading
) {
List<MetricInfo> list = Act.metricPlugin().metricStore().timers();
if (!classLoading) {
list = withoutClassLoading(list);
}
return process(list, limit, q, tree, depth, MetricInfo.Comparator.TIMER, MetricInfoTree.TIMER);
}
private List<MetricInfo> withoutClassLoading(List<MetricInfo> list) {
return C.list(list).remove(new $.Predicate<MetricInfo>() {
@Override
public boolean test(MetricInfo metricInfo) {
return metricInfo.getName().startsWith(MetricInfo.CLASS_LOADING);
}
});
}
@Command(name = "act.metric.clear", help = "clear existing metric data")
public void clearMetricData() {
Act.metricPlugin().metricStore().clear();
}
private Object process(List<MetricInfo> list, Integer max, final String q,
boolean asTree, final Integer level, Comparator<MetricInfo> comp,
MetricInfoTree.NodeDecorator decorator) {
$.Predicate<String> filter = $.F.yes();
if (null != level) {
filter = (new $.Predicate<String>() {
@Override
public boolean test(String path) {
String[] sa = path.split(Metric.PATH_SEPARATOR);
return sa.length <= level;
}
});
}
if (null != q) {
final Pattern p = Pattern.compile(q);
filter = filter.and(new $.Predicate<String>() {
@Override
public boolean test(String path) {
return path.contains(q) || p.matcher(path).matches();
}
});
}
final $.Predicate<String> theFilter = filter;
if (!asTree) {
list = C.list(list).filter(new $.Predicate<MetricInfo>() {
@Override
public boolean test(MetricInfo metricInfo) {
return theFilter.test(metricInfo.getName());
}
});
if (null == max) {
return C.list(list).sorted(comp);
}
return C.list(list).take(max).sorted(comp);
} else {
MetricInfoTree tree = new MetricInfoTree(list, theFilter);
return tree.root(decorator);
}
}
}