MetricConfigManager.java
8.35 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.dianping.cat.consumer.metric;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.codehaus.plexus.logging.LogEnabled;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.unidal.dal.jdbc.DalException;
import org.unidal.dal.jdbc.DalNotFoundException;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.util.StringUtils;
import org.xml.sax.SAXException;
import com.dianping.cat.Cat;
import com.dianping.cat.config.content.ContentFetcher;
import com.dianping.cat.consumer.config.ProductLineConfig;
import com.dianping.cat.consumer.config.ProductLineConfigManager;
import com.dianping.cat.consumer.metric.MetricAnalyzer.ConfigItem;
import com.dianping.cat.consumer.metric.config.entity.MetricConfig;
import com.dianping.cat.consumer.metric.config.entity.MetricItemConfig;
import com.dianping.cat.consumer.metric.config.entity.Tag;
import com.dianping.cat.consumer.metric.config.transform.DefaultSaxParser;
import com.dianping.cat.core.config.Config;
import com.dianping.cat.core.config.ConfigDao;
import com.dianping.cat.core.config.ConfigEntity;
public class MetricConfigManager implements Initializable, LogEnabled {
@Inject
protected ConfigDao m_configDao;
@Inject
private ContentFetcher m_fetcher;
@Inject
private ProductLineConfigManager m_productLineConfigManager;
private int m_configId;
private volatile MetricConfig m_metricConfig;
private long m_modifyTime;
private Logger m_logger;
private static final String CONFIG_NAME = "metricConfig";
public static final String DEFAULT_TAG = "业务大盘";
public String buildMetricKey(String domain, String type, String metricKey) {
return domain + ":" + type + ":" + metricKey;
}
public boolean deleteBatchDomainConfig(String domain) {
Set<String> keys = new HashSet<String>();
Map<String, MetricItemConfig> metricItemConfigs = getMetricConfig().getMetricItemConfigs();
for (Entry<String, MetricItemConfig> entry : metricItemConfigs.entrySet()) {
String currentKey = entry.getKey();
if (currentKey.startsWith(domain + ":Metric:")) {
keys.add(currentKey);
}
}
for (String key : keys) {
getMetricConfig().removeMetricItemConfig(key);
}
return storeConfig();
}
public boolean deleteDomainConfig(String key) {
getMetricConfig().removeMetricItemConfig(key);
return storeConfig();
}
protected void deleteUnusedConfig() {
try {
Map<String, MetricItemConfig> configs = m_metricConfig.getMetricItemConfigs();
List<String> unused = new ArrayList<String>();
for (MetricItemConfig config : configs.values()) {
String domain = config.getDomain();
String productLine = m_productLineConfigManager.queryProductLineByDomain(domain);
ProductLineConfig productLineConfig = m_productLineConfigManager.queryProductLine(productLine);
if (ProductLineConfig.METRIC.equals(productLineConfig)) {
unused.add(config.getId());
}
}
for (String id : unused) {
m_logger.info("delete metric item " + id);
m_metricConfig.removeMetricItemConfig(id);
}
storeConfig();
} catch (Exception e) {
m_logger.error(e.getMessage(), e);
}
}
@Override
public void enableLogging(Logger logger) {
m_logger = logger;
}
public MetricConfig getMetricConfig() {
synchronized (this) {
return m_metricConfig;
}
}
@Override
public void initialize() throws InitializationException {
try {
Config config = m_configDao.findByName(CONFIG_NAME, ConfigEntity.READSET_FULL);
String content = config.getContent();
m_configId = config.getId();
m_metricConfig = DefaultSaxParser.parse(content);
m_modifyTime = config.getModifyDate().getTime();
} catch (DalNotFoundException e) {
try {
String content = m_fetcher.getConfigContent(CONFIG_NAME);
Config config = m_configDao.createLocal();
config.setName(CONFIG_NAME);
config.setContent(content);
m_configDao.insert(config);
m_configId = config.getId();
m_metricConfig = DefaultSaxParser.parse(content);
m_modifyTime = new Date().getTime();
} catch (Exception ex) {
Cat.logError(ex);
}
} catch (Exception e) {
Cat.logError(e);
}
if (m_metricConfig == null) {
m_metricConfig = new MetricConfig();
}
}
public boolean insertMetricIfNotExist(String domain, String type, String metricKey, ConfigItem item) {
String key = buildMetricKey(domain, type, metricKey);
MetricItemConfig config = m_metricConfig.findMetricItemConfig(key);
if (config != null) {
return true;
} else {
config = new MetricItemConfig();
config.setId(key);
config.setDomain(domain);
config.setType(type);
config.setMetricKey(metricKey);
config.setTitle(item.getTitle());
config.setShowAvg(item.isShowAvg());
config.setShowCount(item.isShowCount());
config.setShowSum(item.isShowSum());
m_logger.info("insert metric config info " + config.toString());
return insertMetricItemConfig(config);
}
}
public boolean insertMetricItemConfig(MetricItemConfig config) {
getMetricConfig().addMetricItemConfig(config);
return storeConfig();
}
public MetricItemConfig queryMetricItemConfig(String id) {
return getMetricConfig().findMetricItemConfig(id);
}
public List<MetricItemConfig> queryMetricItemConfigs(Collection<String> domains) {
List<MetricItemConfig> configs = new ArrayList<MetricItemConfig>();
Map<String, MetricItemConfig> metricConfig = getMetricConfig().getMetricItemConfigs();
for (Entry<String, MetricItemConfig> entry : metricConfig.entrySet()) {
MetricItemConfig item = entry.getValue();
if (domains.contains(item.getDomain())) {
configs.add(item);
}
}
Collections.sort(configs, new Comparator<MetricItemConfig>() {
@Override
public int compare(MetricItemConfig m1, MetricItemConfig m2) {
return (int) ((m1.getViewOrder() - m2.getViewOrder()) * 100);
}
});
return configs;
}
public List<MetricItemConfig> queryMetricItemConfigs(String tag) {
List<MetricItemConfig> metricItemConfigs = new ArrayList<MetricItemConfig>();
try {
Collection<MetricItemConfig> configs = getMetricConfig().getMetricItemConfigs().values();
for (MetricItemConfig metricItemConfig : configs) {
for (Tag itemTag : metricItemConfig.getTags()) {
String tagName = itemTag.getName();
if (tag.equals(tagName)) {
metricItemConfigs.add(metricItemConfig);
break;
}
}
}
} catch (Exception ex) {
Cat.logError(ex);
}
return metricItemConfigs;
}
public List<String> queryTags() {
Set<String> tags = new HashSet<String>();
try {
Collection<MetricItemConfig> configs = getMetricConfig().getMetricItemConfigs().values();
for (MetricItemConfig metricItemConfig : configs) {
for (Tag tag : metricItemConfig.getTags()) {
String tagName = tag.getName();
if (!StringUtils.isEmpty(tagName)) {
tags.add(tagName);
}
}
}
} catch (Exception ex) {
Cat.logError(ex);
}
List<String> result = new ArrayList<String>(tags);
Collections.sort(result, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.equals(DEFAULT_TAG)) {
return -1;
}
if (o2.endsWith(DEFAULT_TAG)) {
return 1;
}
return o1.compareTo(o2);
}
});
return result;
}
public void refreshConfig() throws DalException, SAXException, IOException {
Config config = m_configDao.findByName(CONFIG_NAME, ConfigEntity.READSET_FULL);
long modifyTime = config.getModifyDate().getTime();
synchronized (this) {
if (modifyTime > m_modifyTime) {
String content = config.getContent();
MetricConfig metricConfig = DefaultSaxParser.parse(content);
m_metricConfig = metricConfig;
m_modifyTime = modifyTime;
}
}
}
private boolean storeConfig() {
synchronized (this) {
try {
Config config = m_configDao.createLocal();
config.setId(m_configId);
config.setKeyId(m_configId);
config.setName(CONFIG_NAME);
config.setContent(getMetricConfig().toString());
m_configDao.updateByPK(config, ConfigEntity.UPDATESET_FULL);
} catch (Exception e) {
Cat.logError(e);
return false;
}
}
return true;
}
}