1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.statistics.detailed.processing;
18
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.PrintStream;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import com.zanthan.sequence.Fasade;
28
29 import ch.elca.el4j.services.statistics.detailed.MeasureItem;
30 import ch.elca.el4j.util.codingsupport.Reject;
31
32
33
34
35
36
37
38
39
40
41 public class StatisticsOutputter {
42
43
44
45
46 private List<MeasureItem> m_measures;
47
48
49
50
51
52
53
54
55 public StatisticsOutputter(List<MeasureItem> measures) {
56 m_measures = measures;
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public void createDiagFile(String filename, String measureId,
72 int width, int height) {
73 Reject.ifNull(filename);
74 String newFilename = filename;
75
76
77 if (!newFilename.endsWith(".png")) {
78 newFilename = newFilename.concat(".png");
79 }
80
81 Fasade fasade = new Fasade();
82
83 if (width == 0 || height == 0) {
84 fasade.createSequenceDiagram(convertData(measureId), newFilename);
85 } else {
86 fasade.createSequenceDiagram(convertData(measureId), newFilename,
87 width, height);
88 }
89 }
90
91
92
93
94
95
96
97
98
99 public void createCVSFile(String filename, String measureId) {
100 Reject.ifNull(filename);
101 String newFilename = filename;
102
103 if (!newFilename.endsWith(".txt")) {
104 newFilename = newFilename.concat(".txt");
105 }
106
107 try {
108 PrintStream out
109 = new PrintStream(new FileOutputStream(newFilename));
110
111 Iterator<MeasureItem> iter = m_measures.iterator();
112 while (iter.hasNext()) {
113 MeasureItem item = iter.next();
114 if (item.getID().getFormattedString().equals(measureId)) {
115 out.println(item.getCsvString(","));
116 }
117 }
118 out.close();
119 } catch (FileNotFoundException e) {
120 e.printStackTrace();
121 }
122 }
123
124
125
126
127
128
129
130
131
132 private String convertData(String id) {
133 Map<Integer, MeasureItem> myMap = new HashMap<Integer, MeasureItem>();
134 int i = 1;
135 MeasureItem elem;
136 String hierarchy = "";
137 String oldHierarchy = "";
138 StringBuilder resBuilder = new StringBuilder();
139
140
141 for (MeasureItem m : m_measures) {
142 if (id.equalsIgnoreCase(m.getID().toString())) {
143 myMap.put(m.getSequence(), m);
144 }
145 }
146
147 if (myMap.get(i) == null) {
148 throw new RuntimeException("Measurement are messed up");
149 }
150
151
152
153 while ((elem = myMap.get(i)) != null) {
154 hierarchy = elem.getHierarchy();
155
156 if (hierarchy.length() > oldHierarchy.length()) {
157 resBuilder.append(printCall(elem));
158
159
160 } else if (hierarchy.length() == oldHierarchy.length()) {
161 resBuilder.append(")").append(printCall(elem));
162
163 } else if (hierarchy.length() < oldHierarchy.length()) {
164 while (hierarchy.length() != oldHierarchy.length()) {
165 resBuilder.append(") ");
166 oldHierarchy
167 = oldHierarchy.substring(0, oldHierarchy.length() - 2);
168 }
169 resBuilder.append(")").append(printCall(elem));
170
171 } else {
172 throw new RuntimeException("Measurements messed up");
173 }
174 oldHierarchy = elem.getHierarchy();
175 i++;
176 }
177
178 while (oldHierarchy.length() > 1) {
179 resBuilder.append(")");
180 oldHierarchy = oldHierarchy.substring(0, oldHierarchy.length() - 2);
181 }
182
183 resBuilder.append(")");
184 return resBuilder.toString();
185 }
186
187
188
189
190
191
192
193
194 private String trimClassNames(String className) {
195 int i = className.lastIndexOf('.');
196 if (i > -1) {
197 return className.substring(i + 1);
198 } else {
199 return className;
200 }
201 }
202
203
204
205
206
207
208
209
210 private String printCall(MeasureItem elem) {
211 return "(" + trimClassNames(elem.getEjbName())
212 + " " + elem.getMethodName()
213 + ":" + elem.getDuration() + "ms" + " " + "\"\"";
214 }
215 }