KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > sqlprofiler > gui > QueryCountChartModel


1 package org.jahia.sqlprofiler.gui;
2
3 import org.jCharts.Chart;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import org.jahia.sqlprofiler.QueryEntry;
7 import org.jCharts.chartData.ChartDataException;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.Date JavaDoc;
10 import org.jCharts.chartData.DataSeries;
11 import java.awt.Paint JavaDoc;
12 import java.awt.Stroke JavaDoc;
13 import org.jCharts.properties.LineChartProperties;
14 import java.awt.Shape JavaDoc;
15 import java.awt.Color JavaDoc;
16 import org.jCharts.chartData.AxisChartDataSet;
17 import org.jCharts.properties.ChartProperties;
18 import org.jCharts.properties.AxisProperties;
19 import org.jCharts.properties.LegendProperties;
20 import org.jCharts.axisChart.AxisChart;
21 import org.jCharts.types.ChartType;
22 import org.jCharts.properties.PropertyException;
23 import java.awt.Dimension JavaDoc;
24 import java.util.SortedSet JavaDoc;
25
26 /**
27  * <p>Title: </p>
28  * <p>Description: </p>
29  * <p>Copyright: Copyright (c) 2003</p>
30  * <p>Company: Jahia Ltd</p>
31  * @author not attributable
32  * @version 1.0
33  */

34
35 public class QueryCountChartModel extends AbstractChartModel {
36
37     private ProfileStatementTableModel profileStatementModel;
38
39     private int statementCount = 0;
40     private int resultSetCount = 0;
41     private int selectStatementCount = 0;
42     private int lastQueryTotal = 0;
43     private LineChartProperties lineChartProperties;
44     private ChartProperties chartProperties = new ChartProperties();
45     private AxisProperties axisProperties = new AxisProperties();
46     private LegendProperties legendProperties = new LegendProperties();
47     private DataSeries dataSeries = null;
48
49     public QueryCountChartModel(ProfileStatementTableModel
50                                 profileStatementModel) {
51
52         this.profileStatementModel = profileStatementModel;
53         Stroke JavaDoc[] strokes = {
54             LineChartProperties.DEFAULT_LINE_STROKE,
55             LineChartProperties.DEFAULT_LINE_STROKE,
56             LineChartProperties.DEFAULT_LINE_STROKE,
57             LineChartProperties.DEFAULT_LINE_STROKE
58         };
59         Shape JavaDoc[] shapes = {
60             null,
61             null,
62             null,
63             null
64         };
65         lineChartProperties = new
66             LineChartProperties(strokes, shapes);
67     }
68
69     public Chart getChart(Dimension JavaDoc dimension) {
70         if (dataSeries == null) {
71             return null;
72         }
73         AxisChart axisChart = new AxisChart(dataSeries, chartProperties,
74                                             axisProperties, legendProperties,
75                                             (int) dimension.getWidth(),
76                                             (int) dimension.getHeight());
77         return axisChart;
78     }
79
80     public void update() {
81
82         if (profileStatementModel.getQueryEntries().size() == lastQueryTotal) {
83             return;
84         }
85         lastQueryTotal = profileStatementModel.getQueryEntries().size();
86
87         try {
88
89             long lowestTime = profileStatementModel.getLowestQueryTime();
90             long highestTime = profileStatementModel.getHighestQueryTime();
91
92             if ( (lowestTime != Long.MAX_VALUE) &&
93                 (highestTime != Long.MIN_VALUE)) {
94
95                 double timeInterval = highestTime - lowestTime;
96                 //final int slices = 100;
97
// double timeIncrement = timeInterval / ((double) slices);
98
double timeIncrement = 1000;
99                 double sliceCount = timeInterval / timeIncrement;
100                 int slices = new Double JavaDoc(sliceCount).intValue();
101
102                 if (slices < 2) {
103                     return;
104                 }
105
106                 String JavaDoc[] xAxisLabels = new String JavaDoc[slices];
107                 double[][] data = new double[4][slices];
108
109                 int curSlice = 0;
110                 statementCount = 0;
111                 resultSetCount = 0;
112                 selectStatementCount = 0;
113                 int queriesInSlice = 0;
114                 SortedSet JavaDoc sortedQueryEntries = profileStatementModel.getSortedQueryEntries();
115                 Iterator JavaDoc queryIter = sortedQueryEntries.iterator();
116                 double curSampleStartTime = lowestTime;
117                 double curSampleEndTime = lowestTime + timeIncrement;
118                 while (queryIter.hasNext()) {
119                     QueryEntry curEntry = (QueryEntry) queryIter.next();
120                     while (curEntry.getTime() > curSampleEndTime) {
121                         xAxisLabels[curSlice] = Long.toString(new Double JavaDoc(
122                             curSampleStartTime-lowestTime).longValue());
123                         data[0][curSlice] = queriesInSlice;
124                         data[1][curSlice] = statementCount;
125                         data[2][curSlice] = resultSetCount;
126                         data[3][curSlice] = selectStatementCount;
127                         statementCount = 0;
128                         resultSetCount = 0;
129                         selectStatementCount = 0;
130                         queriesInSlice = 0;
131                         curSlice++;
132                         curSampleStartTime = lowestTime +
133                             timeIncrement * ( (double) curSlice);
134                         curSampleEndTime = lowestTime +
135                             timeIncrement * ( (double) curSlice + 1);
136                     }
137                     queriesInSlice++;
138                     if (curEntry.getCategory() != null) {
139                         if (curEntry.getCategory().toLowerCase().equals("statement")) {
140                             statementCount++;
141                             if (curEntry.getSqlStatement().toLowerCase().startsWith(
142                                 "select")) {
143                                 selectStatementCount++;
144                             }
145                         } else if (curEntry.getCategory().toLowerCase().equals(
146                             "resultset")) {
147                             resultSetCount++;
148                         }
149                     }
150                 }
151
152                 /*
153                 for (int i = 0; i < slices; i++) {
154                     double sampleStartTime = lowestTime +
155                         timeIncrement * ( (double) i);
156                     double sampleEndTime = lowestTime +
157                         timeIncrement * ( (double) i + 1);
158                     xAxisLabels[i] = Long.toString(new Double(
159                         sampleStartTime-lowestTime).longValue());
160
161                     ArrayList queriesInInterval = profileStatementModel.
162                         getQueriesBetweenTime(new
163                                               Double(sampleStartTime).longValue(),
164                                               new Double(sampleEndTime).
165                                               longValue());
166
167                     updateQueryTypeCount(queriesInInterval);
168
169                     data[0][i] = queriesInInterval.size();
170                     data[1][i] = statementCount;
171                     data[2][i] = resultSetCount;
172                     data[3][i] = selectStatementCount;
173                 }
174 */

175                 String JavaDoc xAxisTitle = "Milliseconds";
176                 String JavaDoc yAxisTitle = "Queries";
177                 String JavaDoc title = "Queries over time";
178                 dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
179                                             yAxisTitle, title);
180
181                 String JavaDoc[] legendLabels = {
182                     "Queries / second",
183                     "Statements / second",
184                     "Result sets / second",
185                     "SELECT statements / second"};
186                 Paint JavaDoc[] paints = {
187                     Color.blue, Color.red, Color.cyan, Color.yellow};
188
189                 AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data,
190                     legendLabels, paints, ChartType.LINE, lineChartProperties);
191                 dataSeries.addIAxisPlotDataSet(axisChartDataSet);
192
193                 fireChartDataChanged();
194
195             }
196
197         } catch (ChartDataException chartDataException) {
198             chartDataException.printStackTrace();
199         }
200     }
201
202     /*
203     private void updateQueryTypeCount(ArrayList queryList) {
204         statementCount = 0;
205         resultSetCount = 0;
206         selectStatementCount = 0;
207
208         Iterator queryIter = queryList.iterator();
209         while (queryIter.hasNext()) {
210             QueryEntry curEntry = (QueryEntry) queryIter.next();
211             if (curEntry.getCategory() != null) {
212                 if (curEntry.getCategory().toLowerCase().equals("statement")) {
213                     statementCount++;
214                     if (curEntry.getSqlStatement().toLowerCase().startsWith(
215                         "select")) {
216                         selectStatementCount++;
217                     }
218                 } else if (curEntry.getCategory().toLowerCase().equals(
219                     "resultset")) {
220                     resultSetCount++;
221                 }
222             }
223         }
224
225     }
226     */

227
228 }
Popular Tags