1 package org.jahia.sqlprofiler.gui; 2 3 import org.jCharts.Chart; 4 import org.jahia.sqlprofiler.QueryEntry; 5 import java.util.Iterator ; 6 import java.util.ArrayList ; 7 import java.text.SimpleDateFormat ; 8 import java.util.Date ; 9 import org.jCharts.chartData.DataSeries; 10 import java.awt.Paint ; 11 import java.awt.Color ; 12 import org.jCharts.chartData.AxisChartDataSet; 13 import org.jCharts.types.ChartType; 14 import org.jCharts.chartData.ChartDataException; 15 import org.jCharts.axisChart.AxisChart; 16 import org.jCharts.properties.LineChartProperties; 17 import org.jCharts.properties.ChartProperties; 18 import org.jCharts.properties.AxisProperties; 19 import org.jCharts.properties.LegendProperties; 20 import java.awt.Stroke ; 21 import java.awt.Shape ; 22 import java.awt.Dimension ; 23 import java.util.SortedSet ; 24 25 33 34 public class QueryTrafficChartModel extends AbstractChartModel { 35 36 private static final org.apache.log4j.Logger logger = org.apache.log4j. 37 Logger.getLogger(QueryTrafficChartModel.class); 38 39 private ProfileStatementTableModel profileStatementModel; 40 41 private long inboundTotalBytes = 0; 42 private long outboundTotalBytes = 0; 43 44 private int lastQueryTotal = 0; 45 private LineChartProperties lineChartProperties; 46 private ChartProperties chartProperties = new ChartProperties(); 47 private AxisProperties axisProperties = new AxisProperties(); 48 private LegendProperties legendProperties = new LegendProperties(); 49 private DataSeries dataSeries = null; 50 51 public QueryTrafficChartModel(ProfileStatementTableModel 52 profileStatementModel) { 53 54 this.profileStatementModel = profileStatementModel; 55 Stroke [] strokes = { 56 LineChartProperties.DEFAULT_LINE_STROKE, 57 LineChartProperties.DEFAULT_LINE_STROKE 58 }; 59 Shape [] shapes = { 60 null, 61 null 62 }; 63 lineChartProperties = new 64 LineChartProperties(strokes, shapes); 65 } 66 67 public Chart getChart(Dimension dimension) { 68 if (dataSeries == null) { 69 return null; 70 } 71 AxisChart axisChart = new AxisChart(dataSeries, chartProperties, 72 axisProperties, legendProperties, 73 (int) dimension.getWidth(), 74 (int) dimension.getHeight()); 75 return axisChart; 76 } 77 78 public void update() { 79 80 if (profileStatementModel.getQueryEntries().size() == lastQueryTotal) { 81 return; 82 } 83 lastQueryTotal = profileStatementModel.getQueryEntries().size(); 84 85 try { 86 87 long lowestTime = profileStatementModel.getLowestQueryTime(); 88 long highestTime = profileStatementModel.getHighestQueryTime(); 89 90 if ( (lowestTime != Long.MAX_VALUE) && 91 (highestTime != Long.MIN_VALUE)) { 92 93 double timeInterval = highestTime - lowestTime; 94 double timeIncrement = 1000; 97 double sliceCount = timeInterval / timeIncrement; 98 int slices = new Double (sliceCount).intValue(); 99 100 if (slices < 2) { 101 return; 102 } 103 104 String [] xAxisLabels = new String [slices]; 105 double[][] data = new double[2][slices]; 106 107 int curSlice = 0; 108 inboundTotalBytes = 0; 109 outboundTotalBytes = 0; 110 SortedSet sortedQueryEntries = profileStatementModel.getSortedQueryEntries(); 111 Iterator queryIter = sortedQueryEntries.iterator(); 112 double curSampleStartTime = lowestTime; 113 double curSampleEndTime = lowestTime + timeIncrement; 114 while (queryIter.hasNext()) { 115 QueryEntry curEntry = (QueryEntry) queryIter.next(); 116 while (curEntry.getTime() > curSampleEndTime) { 117 xAxisLabels[curSlice] = Long.toString(new Double ( 118 curSampleStartTime-lowestTime).longValue()); 119 data[0][curSlice] = outboundTotalBytes; 120 data[1][curSlice] = inboundTotalBytes; 121 inboundTotalBytes = 0; 122 outboundTotalBytes = 0; 123 curSlice++; 124 curSampleStartTime = lowestTime + 125 timeIncrement * ( (double) curSlice); 126 curSampleEndTime = lowestTime + 127 timeIncrement * ( (double) curSlice + 1); 128 } 129 if (curEntry.getCategory() != null) { 130 if (curEntry.getCategory().toLowerCase().equals("statement")) { 131 outboundTotalBytes += curEntry.getSqlStatement().length(); 132 } else if (curEntry.getCategory().toLowerCase().equals( 133 "resultset")) { 134 inboundTotalBytes += curEntry.getSqlStatement().length(); 135 } 136 } 137 } 138 139 String xAxisTitle = "Milliseconds"; 140 String yAxisTitle = "Bytes"; 141 String title = "Query traffic over time"; 142 dataSeries = new DataSeries(xAxisLabels, xAxisTitle, 143 yAxisTitle, title); 144 145 String [] legendLabels = { 146 "Outgoing bytes / second", 147 "Incoming bytes / second" 148 }; 149 Paint [] paints = { 150 Color.blue, Color.red 151 }; 152 153 AxisChartDataSet axisChartDataSet = new AxisChartDataSet(data, 154 legendLabels, paints, ChartType.LINE, lineChartProperties); 155 dataSeries.addIAxisPlotDataSet(axisChartDataSet); 156 157 fireChartDataChanged(); 158 159 } 160 161 } catch (ChartDataException chartDataException) { 162 chartDataException.printStackTrace(); 163 } 164 } 165 166 } | Popular Tags |