| 1 |
import java.sql.*; |
| 2 |
import java.util.*; |
| 3 |
import java.awt.image.*; |
| 4 |
import java.io.*; |
| 5 |
|
| 6 |
import uk.ac.ukc.iscream.util.*; |
| 7 |
|
| 8 |
public class ReportMaker { |
| 9 |
|
| 10 |
public ReportMaker(Statement stmt, String dateDir, long startTime, long endTime) { |
| 11 |
_stmt = stmt; |
| 12 |
_dateDir = dateDir; |
| 13 |
_startTime = startTime; |
| 14 |
_endTime = endTime; |
| 15 |
} |
| 16 |
|
| 17 |
public void produce(ReportList reports, String machine) { |
| 18 |
|
| 19 |
if (1==1) |
| 20 |
return; |
| 21 |
|
| 22 |
System.out.println("Producing reports for " + machine + "."); |
| 23 |
|
| 24 |
Iterator reportIt = reports.iterator(); |
| 25 |
while (reportIt.hasNext()) { |
| 26 |
Report report = (Report)reportIt.next(); |
| 27 |
report.setPlotData(null); |
| 28 |
report.setPlotData(new PlotData(_startTime, _endTime)); |
| 29 |
} |
| 30 |
|
| 31 |
String sql = "SELECT receipt_date, xml FROM ipacket WHERE machine_name='" + machine + "' ORDER BY receipt_date;"; |
| 32 |
|
| 33 |
ResultSet rs = null; |
| 34 |
try { |
| 35 |
rs = _stmt.executeQuery(sql); |
| 36 |
} |
| 37 |
catch (SQLException e) { |
| 38 |
System.out.println("Unable to select data for " + machine + "."); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
System.out.println("[DEBUG]Populating PlotData objects."); |
| 43 |
|
| 44 |
// Go through each selected XML string and add the necessary |
| 45 |
// data to the PlotData object associated with that report. |
| 46 |
try { |
| 47 |
XMLPacketMaker pm = null; |
| 48 |
XMLPacket packet = null; |
| 49 |
String xml = null; |
| 50 |
while(rs.next()) { |
| 51 |
xml = rs.getString("xml"); |
| 52 |
pm = new XMLPacketMaker(xml); |
| 53 |
packet = pm.createXMLPacket(); |
| 54 |
reportIt = reports.iterator(); |
| 55 |
while (reportIt.hasNext()) { |
| 56 |
Report report = (Report)reportIt.next(); |
| 57 |
String reportField = report.getReportField(); |
| 58 |
PlotData plotData = report.getPlotData(); |
| 59 |
long x = rs.getLong("receipt_date"); |
| 60 |
double y; |
| 61 |
try { |
| 62 |
y = Double.parseDouble(packet.getParam(reportField)); |
| 63 |
} |
| 64 |
catch (NumberFormatException e) { |
| 65 |
System.out.println("Skipped a non-numerical field for " + reportField + "."); |
| 66 |
continue; |
| 67 |
} |
| 68 |
plotData.addPoint(x, y); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
catch (SQLException e) { |
| 73 |
System.out.println("SQLException: " + e); |
| 74 |
} |
| 75 |
|
| 76 |
System.out.println("[DEBUG]Writing graphs to disk."); |
| 77 |
|
| 78 |
reportIt = reports.iterator(); |
| 79 |
while (reportIt.hasNext()) { |
| 80 |
Report report = (Report)reportIt.next(); |
| 81 |
String reportField = report.getReportField(); |
| 82 |
PlotData plotData = report.getPlotData(); |
| 83 |
plotData.flushPoints(); |
| 84 |
plotData.scaleToFitX(SHTMLLayout.CHART_WIDTH, SHTMLLayout.CHART_HEIGHT); |
| 85 |
IscreamChart chart = new IscreamChart(_startTime, _endTime); |
| 86 |
double maxY = report.getMaxY(); |
| 87 |
if (!report.isScaled()) { |
| 88 |
maxY = plotData.getMaxY(); |
| 89 |
} |
| 90 |
BufferedImage img = chart.drawChart(plotData, SHTMLLayout.CHART_WIDTH, SHTMLLayout.CHART_HEIGHT, 24, 10, maxY); |
| 91 |
IscreamChartWriter imgWriter = new IscreamChartWriter(); |
| 92 |
imgWriter.writeChart(img, _dateDir + "_" + machine + "_" + reportField + ".gif"); |
| 93 |
System.out.println("[DEBUG]Point count: " + plotData.countPoints()); |
| 94 |
report.clearPlotData(); |
| 95 |
System.out.println("[DEBUG]Point count: " + plotData.countPoints()); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
private Statement _stmt = null; |
| 101 |
private String _dateDir; |
| 102 |
private long _startTime; |
| 103 |
private long _endTime; |
| 104 |
} |