View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.management.stats.printers;
8   
9   import org.mule.management.stats.FlowConstructStatistics;
10  import org.mule.management.stats.ServiceStatistics;
11  import org.mule.management.stats.RouterStatistics;
12  import org.mule.management.stats.SedaServiceStatistics;
13  
14  import java.io.OutputStream;
15  import java.io.PrintWriter;
16  import java.io.Writer;
17  import java.util.ArrayList;
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * <code>HtmlTablePrinter</code> prints event processing stats as a HTML table
26   */
27  public class AbstractTablePrinter extends PrintWriter
28  {
29      public AbstractTablePrinter(Writer out)
30      {
31          super(out, true);
32      }
33  
34      public AbstractTablePrinter(OutputStream out)
35      {
36          super(out, true);
37      }
38  
39      public String[] getHeaders()
40      {
41          String[] column = new String[41];
42          column[0] = "Name";
43          column[1] = "Service Pool Max Size";
44          column[2] = "Service Pool Size";
45          column[3] = "Thread Pool Size";
46          column[4] = "Current Queue Size";
47          column[5] = "Max Queue Size";
48          column[6] = "Avg Queue Size";
49          column[7] = "Sync Events Received";
50          column[8] = "Async Events Received";
51          column[9] = "Total Events Received";
52          column[10] = "Sync Events Sent";
53          column[11] = "Async Events Sent";
54          column[12] = "ReplyTo Events Sent";
55          column[13] = "Total Events Sent";
56          column[14] = "Executed Events";
57          column[15] = "Execution Messages";
58          column[16] = "Fatal Messages";
59          column[17] = "Min Execution Time";
60          column[18] = "Max Execution Time";
61          column[19] = "Avg Execution Time";
62          column[20] = "Total Execution Time";
63          column[21] = "Processed Events";
64          column[22] = "Min Processing Time";
65          column[23] = "Max Processing Time";
66          column[24] = "Avg Processing Time";
67          column[25] = "Total Processing Time";
68          column[26] = "In Router Statistics";
69          column[27] = "Total Received";
70          column[28] = "Total Routed";
71          column[29] = "Not Routed";
72          column[30] = "Caught Events";
73          column[31] = "By Provider";
74          column[32] = "";
75          column[33] = "Out Router Statistics";
76          column[34] = "Total Received";
77          column[35] = "Total Routed";
78          column[36] = "Not Routed";
79          column[37] = "Caught Events";
80          column[38] = "By Provider";
81          column[39] = "";
82          column[40] = "Sample Period";
83          return column;
84      }
85  
86      protected void getColumn(FlowConstructStatistics stats, String[] col)
87      {
88          if (stats == null)
89          {
90              return;
91          }
92          ServiceStatistics serviceStats = (stats instanceof ServiceStatistics) ? (ServiceStatistics) stats : null;
93  
94          Arrays.fill(col, "-");
95  
96          col[0] = stats.getName();
97  
98          //TODO RM* Handling custom stats objects
99          if (stats instanceof SedaServiceStatistics)
100         {
101             col[1] = ((SedaServiceStatistics) stats).getComponentPoolMaxSize() + "/"
102                     + ((SedaServiceStatistics) stats).getComponentPoolAbsoluteMaxSize();
103             col[2] = String.valueOf(((SedaServiceStatistics) stats).getComponentPoolSize());
104         }
105         else
106         {
107             col[1] = "-";
108             col[2] = "-";
109         }
110         col[3] = String.valueOf(stats.getThreadPoolSize());
111         if (serviceStats != null)
112         {
113             col[4] = String.valueOf(serviceStats.getQueuedEvents());
114             col[5] = String.valueOf(serviceStats.getMaxQueueSize());
115             col[6] = String.valueOf(serviceStats.getAverageQueueSize());
116         }
117         col[7] = String.valueOf(stats.getSyncEventsReceived());
118         col[8] = String.valueOf(stats.getAsyncEventsReceived());
119         col[9] = String.valueOf(stats.getTotalEventsReceived());
120         if (serviceStats != null)
121         {
122             col[10] = String.valueOf(serviceStats.getSyncEventsSent());
123             col[11] = String.valueOf(serviceStats.getAsyncEventsSent());
124             col[12] = String.valueOf(serviceStats.getReplyToEventsSent());
125             col[13] = String.valueOf(serviceStats.getTotalEventsSent());
126         }
127 
128         if (serviceStats != null)
129         {
130             col[14] = String.valueOf(serviceStats.getExecutedEvents());
131         }
132         col[15] = String.valueOf(stats.getExecutionErrors());
133         col[16] = String.valueOf(stats.getFatalErrors());
134         if (serviceStats != null)
135         {
136             col[17] = String.valueOf(serviceStats.getMinExecutionTime());
137             col[18] = String.valueOf(serviceStats.getMaxExecutionTime());
138             col[19] = String.valueOf(serviceStats.getAverageExecutionTime());
139             col[20] = String.valueOf(serviceStats.getTotalExecutionTime());
140         }
141 
142         col[21] = String.valueOf(stats.getProcessedEvents());
143         col[22] = String.valueOf(stats.getMinProcessingTime());
144         col[23] = String.valueOf(stats.getMaxProcessingTime());
145         col[24] = String.valueOf(stats.getAverageProcessingTime());
146         col[25] = String.valueOf(stats.getTotalProcessingTime());
147 
148         if (serviceStats != null)
149         {
150             int i = getRouterInfo(serviceStats.getInboundRouterStat(), col, 26);
151             i = getRouterInfo(serviceStats.getOutboundRouterStat(), col, i);
152         }
153 
154         col[40] = String.valueOf(stats.getSamplePeriod());
155     }
156 
157     protected int getRouterInfo(RouterStatistics stats, String[] col, int index)
158     {
159         // TODO what's the deal with the +/- signs?
160         if (stats.isInbound())
161         {
162             col[index++] = "-";
163         }
164         else
165         {
166             col[index++] = "-";
167         }
168 
169         col[index++] = String.valueOf(stats.getTotalReceived());
170         col[index++] = String.valueOf(stats.getTotalRouted());
171         col[index++] = String.valueOf(stats.getNotRouted());
172         col[index++] = String.valueOf(stats.getCaughtMessages());
173 
174         Map routed = stats.getRouted();
175 
176         col[index++] = "-";
177         if (!routed.isEmpty())
178         {
179             Iterator it = routed.entrySet().iterator();
180 
181             StringBuffer buf = new StringBuffer(40);
182             while (it.hasNext())
183             {
184                 Map.Entry e = (Map.Entry) it.next();
185                 buf.append(e.getKey()).append('=').append(e.getValue());
186                 if (it.hasNext())
187                 {
188                     buf.append(';');
189                 }
190             }
191             col[index++] = buf.toString();
192         }
193         else
194         {
195             col[index++] = "";
196         }
197         return index;
198     }
199 
200     protected String[][] getTable(Collection stats)
201     {
202         String[] cols = getHeaders();
203         String[][] table = new String[stats.size() + 1][cols.length];
204         for (int i = 0; i < cols.length; i++)
205         {
206             table[0][i] = cols[i];
207         }
208 
209         int i = 1;
210         for (Iterator iterator = stats.iterator(); iterator.hasNext(); i++)
211         {
212             getColumn((FlowConstructStatistics) iterator.next(), table[i]);
213         }
214 
215         return table;
216     }
217 
218     public void print(Object obj)
219     {
220         if (obj instanceof Collection)
221         {
222             print((Collection) obj);
223         }
224         else if (obj instanceof ServiceStatistics)
225         {
226             List l = new ArrayList();
227             l.add(obj);
228             print(l);
229         }
230         else
231         {
232             super.print(obj);
233         }
234     }
235 
236     public void println(Object obj)
237     {
238         print(obj);
239         println();
240     }
241 
242     public void print(Collection c)
243     {
244         throw new UnsupportedOperationException();
245     }
246 
247     // help IBM compiler, it complains helplessly about
248     // an abmiguously overloaded/overridden method.
249     public void println(String string)
250     {
251         this.println((Object) string);
252     }
253 
254     // help IBM compiler, it complains helplessly about
255     // an abmiguously overloaded/overridden method.
256     public void print(String string)
257     {
258         this.print((Object) string);
259     }
260 
261 }