001: package org.jahia.sqlprofiler.gui;
002:
003: import org.jCharts.Chart;
004: import org.jahia.sqlprofiler.QueryEntry;
005: import java.util.Iterator;
006: import java.util.ArrayList;
007: import java.text.SimpleDateFormat;
008: import java.util.Date;
009: import org.jCharts.chartData.DataSeries;
010: import java.awt.Paint;
011: import java.awt.Color;
012: import org.jCharts.chartData.AxisChartDataSet;
013: import org.jCharts.types.ChartType;
014: import org.jCharts.chartData.ChartDataException;
015: import org.jCharts.axisChart.AxisChart;
016: import org.jCharts.properties.LineChartProperties;
017: import org.jCharts.properties.ChartProperties;
018: import org.jCharts.properties.AxisProperties;
019: import org.jCharts.properties.LegendProperties;
020: import java.awt.Stroke;
021: import java.awt.Shape;
022: import java.awt.Dimension;
023: import java.util.SortedSet;
024:
025: /**
026: * <p>Title: </p>
027: * <p>Description: </p>
028: * <p>Copyright: Copyright (c) 2003</p>
029: * <p>Company: Jahia Ltd</p>
030: * @author not attributable
031: * @version 1.0
032: */
033:
034: public class QueryTrafficChartModel extends AbstractChartModel {
035:
036: private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger
037: .getLogger(QueryTrafficChartModel.class);
038:
039: private ProfileStatementTableModel profileStatementModel;
040:
041: private long inboundTotalBytes = 0;
042: private long outboundTotalBytes = 0;
043:
044: private int lastQueryTotal = 0;
045: private LineChartProperties lineChartProperties;
046: private ChartProperties chartProperties = new ChartProperties();
047: private AxisProperties axisProperties = new AxisProperties();
048: private LegendProperties legendProperties = new LegendProperties();
049: private DataSeries dataSeries = null;
050:
051: public QueryTrafficChartModel(
052: ProfileStatementTableModel profileStatementModel) {
053:
054: this .profileStatementModel = profileStatementModel;
055: Stroke[] strokes = { LineChartProperties.DEFAULT_LINE_STROKE,
056: LineChartProperties.DEFAULT_LINE_STROKE };
057: Shape[] shapes = { 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[2][slices];
101:
102: int curSlice = 0;
103: inboundTotalBytes = 0;
104: outboundTotalBytes = 0;
105: SortedSet sortedQueryEntries = profileStatementModel
106: .getSortedQueryEntries();
107: Iterator queryIter = sortedQueryEntries.iterator();
108: double curSampleStartTime = lowestTime;
109: double curSampleEndTime = lowestTime + timeIncrement;
110: while (queryIter.hasNext()) {
111: QueryEntry curEntry = (QueryEntry) queryIter.next();
112: while (curEntry.getTime() > curSampleEndTime) {
113: xAxisLabels[curSlice] = Long
114: .toString(new Double(curSampleStartTime
115: - lowestTime).longValue());
116: data[0][curSlice] = outboundTotalBytes;
117: data[1][curSlice] = inboundTotalBytes;
118: inboundTotalBytes = 0;
119: outboundTotalBytes = 0;
120: curSlice++;
121: curSampleStartTime = lowestTime + timeIncrement
122: * ((double) curSlice);
123: curSampleEndTime = lowestTime + timeIncrement
124: * ((double) curSlice + 1);
125: }
126: if (curEntry.getCategory() != null) {
127: if (curEntry.getCategory().toLowerCase()
128: .equals("statement")) {
129: outboundTotalBytes += curEntry
130: .getSqlStatement().length();
131: } else if (curEntry.getCategory().toLowerCase()
132: .equals("resultset")) {
133: inboundTotalBytes += curEntry
134: .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 = { "Outgoing bytes / second",
146: "Incoming bytes / second" };
147: Paint[] paints = { Color.blue, Color.red };
148:
149: AxisChartDataSet axisChartDataSet = new AxisChartDataSet(
150: data, legendLabels, paints, ChartType.LINE,
151: lineChartProperties);
152: dataSeries.addIAxisPlotDataSet(axisChartDataSet);
153:
154: fireChartDataChanged();
155:
156: }
157:
158: } catch (ChartDataException chartDataException) {
159: chartDataException.printStackTrace();
160: }
161: }
162:
163: }
|