| 1 |
pjm2 |
1.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 |
pjm2 |
1.2 |
|
| 9 |
|
|
// ReportMaker class - used to produce each type of report for a machine. |
| 10 |
pjm2 |
1.1 |
public class ReportMaker { |
| 11 |
pjm2 |
1.2 |
|
| 12 |
|
|
// Constructor. This takes the database Statement. |
| 13 |
pjm2 |
1.4 |
public ReportMaker(Statement stmt, String webDir, String dateDir, long startTime, long endTime) { |
| 14 |
pjm2 |
1.1 |
_stmt = stmt; |
| 15 |
pjm2 |
1.4 |
_webDir = webDir; |
| 16 |
pjm2 |
1.1 |
_dateDir = dateDir; |
| 17 |
|
|
_startTime = startTime; |
| 18 |
|
|
_endTime = endTime; |
| 19 |
|
|
} |
| 20 |
|
|
|
| 21 |
pjm2 |
1.2 |
// Produce all reports in the report list for the machine. |
| 22 |
|
|
public void produce(ReportList reports, String machine) throws SQLException { |
| 23 |
pjm2 |
1.1 |
|
| 24 |
|
|
System.out.println("Producing reports for " + machine + "."); |
| 25 |
|
|
|
| 26 |
pjm2 |
1.2 |
// Make a list of PlotData objects to build the charts. |
| 27 |
|
|
LinkedList plots = new LinkedList(); |
| 28 |
pjm2 |
1.1 |
Iterator reportIt = reports.iterator(); |
| 29 |
|
|
while (reportIt.hasNext()) { |
| 30 |
pjm2 |
1.2 |
reportIt.next(); |
| 31 |
|
|
plots.add(new PlotData(_startTime, _endTime)); |
| 32 |
pjm2 |
1.1 |
} |
| 33 |
|
|
|
| 34 |
pjm2 |
1.2 |
|
| 35 |
|
|
// Select the data we nede from the database. |
| 36 |
|
|
String sql = "SELECT receipt_date, xml FROM ipacket WHERE machine_name='" + machine + "' AND receipt_date>=" + _startTime + " AND receipt_date<=" + _endTime + " ORDER BY receipt_date;"; |
| 37 |
pjm2 |
1.1 |
ResultSet rs = null; |
| 38 |
|
|
try { |
| 39 |
|
|
rs = _stmt.executeQuery(sql); |
| 40 |
|
|
} |
| 41 |
|
|
catch (SQLException e) { |
| 42 |
|
|
System.out.println("Unable to select data for " + machine + "."); |
| 43 |
|
|
return; |
| 44 |
|
|
} |
| 45 |
|
|
|
| 46 |
|
|
System.out.println("[DEBUG]Populating PlotData objects."); |
| 47 |
|
|
|
| 48 |
|
|
// Go through each selected XML string and add the necessary |
| 49 |
|
|
// data to the PlotData object associated with that report. |
| 50 |
|
|
try { |
| 51 |
|
|
XMLPacketMaker pm = null; |
| 52 |
|
|
XMLPacket packet = null; |
| 53 |
|
|
String xml = null; |
| 54 |
|
|
while(rs.next()) { |
| 55 |
|
|
xml = rs.getString("xml"); |
| 56 |
|
|
pm = new XMLPacketMaker(xml); |
| 57 |
|
|
packet = pm.createXMLPacket(); |
| 58 |
|
|
reportIt = reports.iterator(); |
| 59 |
pjm2 |
1.2 |
Iterator plotIt = plots.iterator(); |
| 60 |
pjm2 |
1.1 |
while (reportIt.hasNext()) { |
| 61 |
pjm2 |
1.2 |
PlotData plotData = (PlotData)plotIt.next(); |
| 62 |
pjm2 |
1.1 |
Report report = (Report)reportIt.next(); |
| 63 |
|
|
String reportField = report.getReportField(); |
| 64 |
|
|
long x = rs.getLong("receipt_date"); |
| 65 |
pjm2 |
1.5 |
double y = 0.0; |
| 66 |
pjm2 |
1.1 |
try { |
| 67 |
|
|
y = Double.parseDouble(packet.getParam(reportField)); |
| 68 |
|
|
} |
| 69 |
|
|
catch (NumberFormatException e) { |
| 70 |
|
|
System.out.println("Skipped a non-numerical field for " + reportField + "."); |
| 71 |
pjm2 |
1.5 |
continue; |
| 72 |
|
|
} |
| 73 |
|
|
catch (NullPointerException e) { |
| 74 |
pjm2 |
1.1 |
continue; |
| 75 |
|
|
} |
| 76 |
|
|
plotData.addPoint(x, y); |
| 77 |
|
|
} |
| 78 |
|
|
} |
| 79 |
pjm2 |
1.2 |
rs.close(); |
| 80 |
pjm2 |
1.1 |
} |
| 81 |
|
|
catch (SQLException e) { |
| 82 |
|
|
System.out.println("SQLException: " + e); |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
|
|
System.out.println("[DEBUG]Writing graphs to disk."); |
| 86 |
|
|
|
| 87 |
pjm2 |
1.2 |
// Write a chart of each report to disk. |
| 88 |
|
|
Iterator plotIt = plots.iterator(); |
| 89 |
pjm2 |
1.1 |
reportIt = reports.iterator(); |
| 90 |
|
|
while (reportIt.hasNext()) { |
| 91 |
|
|
Report report = (Report)reportIt.next(); |
| 92 |
|
|
String reportField = report.getReportField(); |
| 93 |
pjm2 |
1.2 |
PlotData plotData = (PlotData)plotIt.next(); |
| 94 |
pjm2 |
1.1 |
plotData.flushPoints(); |
| 95 |
|
|
plotData.scaleToFitX(SHTMLLayout.CHART_WIDTH, SHTMLLayout.CHART_HEIGHT); |
| 96 |
|
|
IscreamChart chart = new IscreamChart(_startTime, _endTime); |
| 97 |
|
|
double maxY = report.getMaxY(); |
| 98 |
|
|
if (!report.isScaled()) { |
| 99 |
|
|
maxY = plotData.getMaxY(); |
| 100 |
|
|
} |
| 101 |
|
|
BufferedImage img = chart.drawChart(plotData, SHTMLLayout.CHART_WIDTH, SHTMLLayout.CHART_HEIGHT, 24, 10, maxY); |
| 102 |
|
|
IscreamChartWriter imgWriter = new IscreamChartWriter(); |
| 103 |
pjm2 |
1.4 |
IscreamFilePlacer placer = new IscreamFilePlacer(_webDir); |
| 104 |
pjm2 |
1.3 |
File destination = placer.makeDirs(_dateDir, machine, reportField); |
| 105 |
|
|
imgWriter.writeChart(img, new File(destination, "chart.gif")); |
| 106 |
pjm2 |
1.2 |
System.out.println("[DEBUG]Point count: " + plotData.countPoints()); |
| 107 |
pjm2 |
1.1 |
} |
| 108 |
|
|
|
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
private Statement _stmt = null; |
| 112 |
pjm2 |
1.4 |
private String _webDir; |
| 113 |
pjm2 |
1.1 |
private String _dateDir; |
| 114 |
|
|
private long _startTime; |
| 115 |
|
|
private long _endTime; |
| 116 |
|
|
} |