1 package org.jahia.sqlprofiler.gui; 2 3 import org.jCharts.Chart; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import org.jahia.sqlprofiler.QueryEntry; 7 import org.jCharts.chartData.ChartDataException; 8 import java.text.SimpleDateFormat ; 9 import java.util.Date ; 10 import org.jCharts.chartData.DataSeries; 11 import java.awt.Paint ; 12 import java.awt.Stroke ; 13 import org.jCharts.properties.LineChartProperties; 14 import java.awt.Shape ; 15 import java.awt.Color ; 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 ; 24 import java.util.SortedSet ; 25 26 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 [] strokes = { 54 LineChartProperties.DEFAULT_LINE_STROKE, 55 LineChartProperties.DEFAULT_LINE_STROKE, 56 LineChartProperties.DEFAULT_LINE_STROKE, 57 LineChartProperties.DEFAULT_LINE_STROKE 58 }; 59 Shape [] shapes = { 60 null, 61 null, 62 null, 63 null 64 }; 65 lineChartProperties = new 66 LineChartProperties(strokes, shapes); 67 } 68 69 public Chart getChart(Dimension 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 double timeIncrement = 1000; 99 double sliceCount = timeInterval / timeIncrement; 100 int slices = new Double (sliceCount).intValue(); 101 102 if (slices < 2) { 103 return; 104 } 105 106 String [] xAxisLabels = new String [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 sortedQueryEntries = profileStatementModel.getSortedQueryEntries(); 115 Iterator 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 ( 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 175 String xAxisTitle = "Milliseconds"; 176 String yAxisTitle = "Queries"; 177 String title = "Queries over time"; 178 dataSeries = new DataSeries(xAxisLabels, xAxisTitle, 179 yAxisTitle, title); 180 181 String [] legendLabels = { 182 "Queries / second", 183 "Statements / second", 184 "Result sets / second", 185 "SELECT statements / second"}; 186 Paint [] 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 227 228 } | Popular Tags |