1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.ebay.carad.os.vitalsigns.listeners;
22
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Locale;
29
30 import com.ebay.carad.os.vitalsigns.DataPoint;
31 import com.ebay.carad.os.vitalsigns.IDashboardAgent;
32 import com.ebay.carad.os.vitalsigns.IDashboardReport;
33 import com.ebay.carad.os.vitalsigns.ReportingException;
34
35 /***
36 * <p>Reports the data from a given report as a CSV file.</p>
37 *
38 * <p>WARNING: not thread safe! Use of a DateFormat makes this not thread safe. In current
39 * implementation, if it is only used in one DashboardAgent, this should be fine.</p>
40 *
41 * @author Jeremy Thomerson
42 * @author Jeremy Kraybill
43 * @version $Id$
44 */
45 public class CsvReportListener extends AbstractReportingListener {
46
47
48
49
50 private final DateFormat mDateFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS", Locale.getDefault());
51
52 public void reportRan(IDashboardAgent agent, IDashboardReport report) {
53 String chartBase = agent.getDestinationPath() + "report" + report.getID();
54 FileWriter fw = null;
55 try {
56 fw = new FileWriter(chartBase + ".csv");
57 fw.write(report.getTitle() + "\n" + report.getSubTitle() + "\n\n");
58 DataPoint[] data = agent.getDefaultDataDAO().getData(report);
59 for (int i = 0; i < data.length; i++) {
60 DataPoint row = data[i];
61 fw.write(mDateFormatter.format(new Date(row.getLogTime())) + "," + row.getData() + "\n");
62 }
63 } catch(IOException ioe) {
64 throw new ReportingException("Exception while writing report: " + ioe.getMessage(), ioe);
65 } finally {
66 try {
67 if (fw != null) {
68 fw.close();
69 }
70 } catch (IOException ioe) {
71 throw new ReportingException("Exception while closing stream: " + ioe.getMessage(), ioe);
72 }
73 }
74 }
75
76 }
77