001: package org.jahia.sqlprofiler.gui;
002:
003: import org.jCharts.Chart;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import org.jahia.sqlprofiler.QueryEntry;
007: import org.jCharts.chartData.ChartDataException;
008: import java.text.SimpleDateFormat;
009: import java.util.Date;
010: import org.jCharts.chartData.DataSeries;
011: import java.awt.Paint;
012: import java.awt.Stroke;
013: import org.jCharts.properties.LineChartProperties;
014: import java.awt.Shape;
015: import java.awt.Color;
016: import org.jCharts.chartData.AxisChartDataSet;
017: import org.jCharts.properties.ChartProperties;
018: import org.jCharts.properties.AxisProperties;
019: import org.jCharts.properties.LegendProperties;
020: import org.jCharts.axisChart.AxisChart;
021: import org.jCharts.types.ChartType;
022: import org.jCharts.properties.PropertyException;
023: import java.awt.Dimension;
024: import java.util.SortedSet;
025:
026: /**
027: * <p>Title: </p>
028: * <p>Description: </p>
029: * <p>Copyright: Copyright (c) 2003</p>
030: * <p>Company: Jahia Ltd</p>
031: * @author not attributable
032: * @version 1.0
033: */
034:
035: public class QueryCountChartModel extends AbstractChartModel {
036:
037: private ProfileStatementTableModel profileStatementModel;
038:
039: private int statementCount = 0;
040: private int resultSetCount = 0;
041: private int selectStatementCount = 0;
042: private int lastQueryTotal = 0;
043: private LineChartProperties lineChartProperties;
044: private ChartProperties chartProperties = new ChartProperties();
045: private AxisProperties axisProperties = new AxisProperties();
046: private LegendProperties legendProperties = new LegendProperties();
047: private DataSeries dataSeries = null;
048:
049: public QueryCountChartModel(
050: ProfileStatementTableModel profileStatementModel) {
051:
052: this .profileStatementModel = profileStatementModel;
053: Stroke[] strokes = { LineChartProperties.DEFAULT_LINE_STROKE,
054: LineChartProperties.DEFAULT_LINE_STROKE,
055: LineChartProperties.DEFAULT_LINE_STROKE,
056: LineChartProperties.DEFAULT_LINE_STROKE };
057: Shape[] shapes = { null, null, null, null };
058: lineChartProperties = new LineChartProperties(strokes, shapes);
059: }
060:
061: public Chart getChart(Dimension dimension) {
062: if (dataSeries == null) {
063: return null;
064: }
065: AxisChart axisChart = new AxisChart(dataSeries,
066: chartProperties, axisProperties, legendProperties,
067: (int) dimension.getWidth(), (int) dimension.getHeight());
068: return axisChart;
069: }
070:
071: public void update() {
072:
073: if (profileStatementModel.getQueryEntries().size() == lastQueryTotal) {
074: return;
075: }
076: lastQueryTotal = profileStatementModel.getQueryEntries().size();
077:
078: try {
079:
080: long lowestTime = profileStatementModel
081: .getLowestQueryTime();
082: long highestTime = profileStatementModel
083: .getHighestQueryTime();
084:
085: if ((lowestTime != Long.MAX_VALUE)
086: && (highestTime != Long.MIN_VALUE)) {
087:
088: double timeInterval = highestTime - lowestTime;
089: //final int slices = 100;
090: // double timeIncrement = timeInterval / ((double) slices);
091: double timeIncrement = 1000;
092: double sliceCount = timeInterval / timeIncrement;
093: int slices = new Double(sliceCount).intValue();
094:
095: if (slices < 2) {
096: return;
097: }
098:
099: String[] xAxisLabels = new String[slices];
100: double[][] data = new double[4][slices];
101:
102: int curSlice = 0;
103: statementCount = 0;
104: resultSetCount = 0;
105: selectStatementCount = 0;
106: int queriesInSlice = 0;
107: SortedSet sortedQueryEntries = profileStatementModel
108: .getSortedQueryEntries();
109: Iterator queryIter = sortedQueryEntries.iterator();
110: double curSampleStartTime = lowestTime;
111: double curSampleEndTime = lowestTime + timeIncrement;
112: while (queryIter.hasNext()) {
113: QueryEntry curEntry = (QueryEntry) queryIter.next();
114: while (curEntry.getTime() > curSampleEndTime) {
115: xAxisLabels[curSlice] = Long
116: .toString(new Double(curSampleStartTime
117: - lowestTime).longValue());
118: data[0][curSlice] = queriesInSlice;
119: data[1][curSlice] = statementCount;
120: data[2][curSlice] = resultSetCount;
121: data[3][curSlice] = selectStatementCount;
122: statementCount = 0;
123: resultSetCount = 0;
124: selectStatementCount = 0;
125: queriesInSlice = 0;
126: curSlice++;
127: curSampleStartTime = lowestTime + timeIncrement
128: * ((double) curSlice);
129: curSampleEndTime = lowestTime + timeIncrement
130: * ((double) curSlice + 1);
131: }
132: queriesInSlice++;
133: if (curEntry.getCategory() != null) {
134: if (curEntry.getCategory().toLowerCase()
135: .equals("statement")) {
136: statementCount++;
137: if (curEntry.getSqlStatement()
138: .toLowerCase().startsWith("select")) {
139: selectStatementCount++;
140: }
141: } else if (curEntry.getCategory().toLowerCase()
142: .equals("resultset")) {
143: resultSetCount++;
144: }
145: }
146: }
147:
148: /*
149: for (int i = 0; i < slices; i++) {
150: double sampleStartTime = lowestTime +
151: timeIncrement * ( (double) i);
152: double sampleEndTime = lowestTime +
153: timeIncrement * ( (double) i + 1);
154: xAxisLabels[i] = Long.toString(new Double(
155: sampleStartTime-lowestTime).longValue());
156:
157: ArrayList queriesInInterval = profileStatementModel.
158: getQueriesBetweenTime(new
159: Double(sampleStartTime).longValue(),
160: new Double(sampleEndTime).
161: longValue());
162:
163: updateQueryTypeCount(queriesInInterval);
164:
165: data[0][i] = queriesInInterval.size();
166: data[1][i] = statementCount;
167: data[2][i] = resultSetCount;
168: data[3][i] = selectStatementCount;
169: }
170: */
171: String xAxisTitle = "Milliseconds";
172: String yAxisTitle = "Queries";
173: String title = "Queries over time";
174: dataSeries = new DataSeries(xAxisLabels, xAxisTitle,
175: yAxisTitle, title);
176:
177: String[] legendLabels = { "Queries / second",
178: "Statements / second", "Result sets / second",
179: "SELECT statements / second" };
180: Paint[] paints = { Color.blue, Color.red, Color.cyan,
181: Color.yellow };
182:
183: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(
184: data, legendLabels, paints, ChartType.LINE,
185: lineChartProperties);
186: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
187:
188: fireChartDataChanged();
189:
190: }
191:
192: } catch (ChartDataException chartDataException) {
193: chartDataException.printStackTrace();
194: }
195: }
196:
197: /*
198: private void updateQueryTypeCount(ArrayList queryList) {
199: statementCount = 0;
200: resultSetCount = 0;
201: selectStatementCount = 0;
202:
203: Iterator queryIter = queryList.iterator();
204: while (queryIter.hasNext()) {
205: QueryEntry curEntry = (QueryEntry) queryIter.next();
206: if (curEntry.getCategory() != null) {
207: if (curEntry.getCategory().toLowerCase().equals("statement")) {
208: statementCount++;
209: if (curEntry.getSqlStatement().toLowerCase().startsWith(
210: "select")) {
211: selectStatementCount++;
212: }
213: } else if (curEntry.getCategory().toLowerCase().equals(
214: "resultset")) {
215: resultSetCount++;
216: }
217: }
218: }
219:
220: }
221: */
222:
223: }
|