TransactionDelegate.java
4.04 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
package com.dianping.cat.consumer.transaction;
import static com.dianping.cat.Constants.ALL;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.Constants;
import com.dianping.cat.config.server.ServerFilterConfigManager;
import com.dianping.cat.consumer.config.AllReportConfigManager;
import com.dianping.cat.consumer.transaction.model.entity.TransactionReport;
import com.dianping.cat.consumer.transaction.model.transform.DefaultNativeBuilder;
import com.dianping.cat.consumer.transaction.model.transform.DefaultNativeParser;
import com.dianping.cat.consumer.transaction.model.transform.DefaultSaxParser;
import com.dianping.cat.report.ReportDelegate;
import com.dianping.cat.task.TaskManager;
import com.dianping.cat.task.TaskManager.TaskProlicy;
public class TransactionDelegate implements ReportDelegate<TransactionReport> {
@Inject
private TaskManager m_taskManager;
@Inject
private ServerFilterConfigManager m_configManager;
@Inject
private AllReportConfigManager m_transactionManager;
private TransactionStatisticsComputer m_computer = new TransactionStatisticsComputer();
@Override
public void afterLoad(Map<String, TransactionReport> reports) {
}
@Override
public void beforeSave(Map<String, TransactionReport> reports) {
for (TransactionReport report : reports.values()) {
Set<String> domainNames = report.getDomainNames();
domainNames.clear();
domainNames.addAll(reports.keySet());
}
if (reports.size() > 0) {
TransactionReport all = createAggregatedReport(reports);
reports.put(all.getDomain(), all);
}
}
@Override
public byte[] buildBinary(TransactionReport report) {
return DefaultNativeBuilder.build(report);
}
@Override
public String buildXml(TransactionReport report) {
report.accept(m_computer);
new TransactionReportCountFilter().visitTransactionReport(report);
return report.toString();
}
public TransactionReport createAggregatedReport(Map<String, TransactionReport> reports) {
if (reports.size() > 0) {
TransactionReport first = reports.values().iterator().next();
TransactionReport all = makeReport(ALL, first.getStartTime().getTime(), Constants.HOUR);
TransactionReportTypeAggregator visitor = new TransactionReportTypeAggregator(all, m_transactionManager);
try {
for (TransactionReport report : reports.values()) {
String domain = report.getDomain();
if (!domain.equals(Constants.ALL)) {
all.getIps().add(domain);
all.getDomainNames().add(domain);
visitor.visitTransactionReport(report);
}
}
} catch (Exception e) {
Cat.logError(e);
}
return all;
} else {
return new TransactionReport(ALL);
}
}
@Override
public boolean createHourlyTask(TransactionReport report) {
String domain = report.getDomain();
if (domain.equals(Constants.ALL)) {
return m_taskManager.createTask(report.getStartTime(), domain, TransactionAnalyzer.ID,
TaskProlicy.ALL_EXCLUED_HOURLY);
} else if (m_configManager.validateDomain(domain)) {
return m_taskManager.createTask(report.getStartTime(), report.getDomain(), TransactionAnalyzer.ID,
TaskProlicy.ALL);
} else {
return true;
}
}
@Override
public String getDomain(TransactionReport report) {
return report.getDomain();
}
@Override
public TransactionReport makeReport(String domain, long startTime, long duration) {
TransactionReport report = new TransactionReport(domain);
report.setStartTime(new Date(startTime));
report.setEndTime(new Date(startTime + duration - 1));
return report;
}
@Override
public TransactionReport mergeReport(TransactionReport old, TransactionReport other) {
TransactionReportMerger merger = new TransactionReportMerger(old);
other.accept(merger);
return old;
}
@Override
public TransactionReport parseBinary(byte[] bytes) {
return DefaultNativeParser.parse(bytes);
}
@Override
public TransactionReport parseXml(String xml) throws Exception {
TransactionReport report = DefaultSaxParser.parse(xml);
return report;
}
}