View Javadoc

1   package org.sourceforge.mbeanmonitoring.report;
2   
3   /**
4    **      Author:
5    **              Laurent Le Grandois <Laurent.Le.Grandois@gmail.com>
6    **              Gilles Bardouillet  <Gilles.Bardouillet@gmail.com>
7    **
8    **  This program is free software; you can redistribute it and/or modify
9    ** it under the terms of the GNU General Public License as published by
10   **  the Free Software Foundation; either version 2 of the License, or
11   **  (at your option) any later version.
12   **
13   ** This program is distributed in the hope that it will be useful,
14   **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15   **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   **  GNU General Public License for more details.
17   **
18   **  You should have received a copy of the GNU General Public License
19   ** along with this program; if not, write to the Free Software
20   **
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Dimension;
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.text.ParseException;
29  import java.text.SimpleDateFormat;
30  import java.util.ArrayList;
31  import java.util.StringTokenizer;
32  
33  import javax.swing.JFrame;
34  import javax.swing.JPanel;
35  
36  import org.jfree.chart.ChartFactory;
37  import org.jfree.chart.ChartPanel;
38  import org.jfree.chart.JFreeChart;
39  import org.jfree.chart.axis.ValueAxis;
40  import org.jfree.chart.plot.XYPlot;
41  import org.jfree.data.time.Millisecond;
42  import org.jfree.data.time.TimeSeries;
43  import org.jfree.data.time.TimeSeriesCollection;
44  import org.jfree.ui.RefineryUtilities;
45  
46  public class Graph extends JFrame implements Runnable {
47  
48  	static final long serialVersionUID = 1L;
49  
50  	private static void displayGraphFromCapture(Capture capture) {
51  		String theCharSep = capture.params.getSeparateur();
52  		String file = capture.params.getGenereTo() + System.getProperty("file.separator") + Capture.CURRENT_DIRECTORY;
53  		int wait = capture.params.getDelay();
54  		long delay = new Long(wait).longValue();
55  		delay = delay * 1000;
56  
57  		File f = new File(file);
58          ArrayList<Graph> graphs = new ArrayList<Graph>();
59  
60  		if (f.isDirectory()) {
61  			String[] children = f.list();
62  			for (int i = 0; i < children.length; i++) {
63  				String fileName = file + System.getProperty("file.separator") + children[i];
64  				graphs.add(new Graph(fileName, delay, theCharSep));
65  			}
66  		}
67  
68  		for (int i = 0; i < graphs.size(); i++) {
69  			Graph graph = graphs.get(i);
70  			graph.pack();
71  			RefineryUtilities.centerFrameOnScreen(graph);
72  			graph.setVisible(true);
73  		}
74  	}
75  
76  	/**
77  	 * @param args
78  	 */
79  	public static void main(String[] args) {
80  		Capture cap = null;
81  		Capture.isGraphRuntime = true;
82  		if (args.length > 0) {
83  			for (int i = 0; i < args.length; i++) {
84  				cap = new Capture(args[i]);
85  				Graph.displayGraphFromCapture(cap);
86  			}
87  		} else {
88  			cap = new Capture();
89  			Graph.displayGraphFromCapture(cap);
90  		}
91  	}
92  
93  	public String dir = null;
94  
95  	BufferedReader in;
96  	String line = null;
97  
98  	String sep = null;
99  	TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
100 
101 	ArrayList<TimeSeries> timeSeriesVector = new ArrayList<TimeSeries>();
102 
103 	public long wait = 0;
104 
105 	/**
106 	 * @param fileName
107 	 * @param delay
108 	 * @param sep
109 	 */
110 	public Graph(String fileName, long delay, String sep) {
111 		super(fileName);
112 		this.wait = delay;
113 		this.sep = sep;
114 
115 		try {
116 			this.in = new BufferedReader(new FileReader(fileName));
117 			StringTokenizer st = new StringTokenizer(in.readLine(), this.sep);
118 
119 			st.nextToken();
120 			st.nextToken(); // date and hour
121 
122 			String column;
123 			while (st.hasMoreElements()) {
124 				column = st.nextToken();
125 				TimeSeries serie = new TimeSeries(column, Millisecond.class);
126 				timeSeriesVector.add(serie);
127 				timeseriescollection.addSeries(serie);
128 			}
129 
130 		} catch (Exception _) {
131 			_.printStackTrace();
132 		}
133 
134 		StringTokenizer st = new StringTokenizer(fileName, "\\");
135 		String title = null;
136 		while (st.hasMoreElements()) {
137 			title = st.nextToken();
138 		}
139 		JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title, "Time", "Value", timeseriescollection, true,
140 				true, false);
141 
142 		XYPlot xyplot = jfreechart.getXYPlot();
143 		ValueAxis valueaxis = xyplot.getDomainAxis();
144 		valueaxis.setAutoRange(true);
145 		valueaxis.setFixedAutoRange(60000D);
146 		valueaxis = xyplot.getRangeAxis();
147 		// valueaxis.setRange(0.0D, 10D);
148 
149 		JPanel jpanel = new JPanel(new BorderLayout());
150 		ChartPanel chartpanel = new ChartPanel(jfreechart);
151 
152 		// JButton jbutton = new JButton("Add New Data Item");
153 		// jbutton.setActionCommand("ADD_DATA");
154 		// jbutton.addActionListener(this);
155 		jpanel.add(chartpanel);
156 		// jpanel.add(jbutton, "South");
157 
158 		chartpanel.setPreferredSize(new Dimension(500, 270));
159 		setContentPane(jpanel);
160 
161 		Thread thread = new Thread(this);
162 
163 		thread.start();
164 
165 	}
166 
167 	private void push(String line) {
168 
169 		// System.out.println(line);
170 
171 		StringTokenizer st = new StringTokenizer(line, this.sep);
172 
173 		String date = st.nextToken();
174 		String heure = st.nextToken();
175 
176 		Millisecond m = null;
177 
178 		try {
179 
180 			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
181 			java.util.Date d = formatter.parse(date + " " + heure);
182 			m = new Millisecond(d);
183 
184 		} catch (ParseException _) {
185 			_.printStackTrace();
186 		}
187 
188 		// System.out.print( "date[" + date + "] heure[" + heure + "] = " );
189 
190 		int i = 0;
191 		String valueString = null;
192 		Double valueDouble = null;
193 		TimeSeries timeSerie = null;
194 
195 		while (st.hasMoreElements()) {
196 
197 			timeSerie = timeSeriesVector.get(i++);
198 			valueString = st.nextToken();
199 			valueDouble = new Double(valueString);
200 
201 			// System.out.print( " " + valueDouble );
202 			timeSerie.add(m, valueDouble);
203 		}
204 
205 		// System.out.println();
206 
207 	}
208 
209 	public void run() {
210 		while (true)
211 
212 			try {
213 
214 				line = in.readLine();
215 
216 				if (line != null || line.length() != 0) {
217 					this.push(line);
218 				}
219 
220 				Thread.sleep(this.wait);
221 
222 			} catch (Exception _) {
223                 // TODO !
224 			}
225 	}
226 }