package zeta.util;
import java.awt.Color;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.CombinedXYPlot;
import com.jrefinery.chart.HorizontalDateAxis;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.NumberTickUnit;
import com.jrefinery.chart.PiePlot;
import com.jrefinery.chart.ValueAxis;
import com.jrefinery.chart.VerticalNumberAxis;
import com.jrefinery.chart.VerticalXYBarRenderer;
import com.jrefinery.chart.XYPlot;
import com.jrefinery.chart.data.LinearPlotFitAlgorithm;
import com.jrefinery.chart.data.MovingAveragePlotFitAlgorithm;
import com.jrefinery.chart.data.PlotFit;
import com.jrefinery.data.BasicTimeSeries;
import com.jrefinery.data.CombinedDataset;
import com.jrefinery.data.Day;
import com.jrefinery.data.DefaultPieDataset;
import com.jrefinery.data.Hour;
import com.jrefinery.data.Range;
import com.jrefinery.data.SeriesDataset;
import com.jrefinery.data.SubSeriesDataset;
import com.jrefinery.data.TimeSeriesCollection;
public class Charts {
public static void main(String[] args) {
java.io.FileOutputStream pngFile = null;
try {
final String[] verticalAxisLabels = { "Number of computers", "Number of new computers", "Number of active computers" };
final Paint[] colors = { Color.blue, Color.red, Color.green };
final int[] weight = { 5, 1, 1 };
final int[][] combination = { {0}, {1}, {2} };
BufferedImage image = Charts.generateChart(700, 500, "Number of computers", "Date", verticalAxisLabels, colors, weight, combination, Charts.createNumberOfComputers(zeta.tool.GetData.getConnection()));
com.sun.jimi.core.encoder.png.PNGEncoder encoder = new com.sun.jimi.core.encoder.png.PNGEncoder();
pngFile = new java.io.FileOutputStream("zeros.png");
encoder.encodeImage(com.sun.jimi.core.Jimi.createRasterImage(image.getSource()), pngFile);
} catch (Exception e) {
ThrowableHandler.handle(e);
} finally {
StreamUtils.close(pngFile);
}
System.exit(1);
}
public static BufferedImage generatePie(int imageWidth, int imageHeight, String[] names, Long[] values)
{
DefaultPieDataset d = new DefaultPieDataset();
if (names != null && values != null) {
for (int i = 0; i < names.length; ++i) {
d.setValue(names[i], values[i]);
}
}
JFreeChart chart = ChartFactory.createPieChart(null, d, true);
chart.setLegend(null);
chart.setBackgroundPaint(Color.white);
PiePlot pie = (PiePlot)chart.getPlot();
pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);
pie.setBackgroundPaint(Color.white);
pie.setBackgroundAlpha(0.6f);
pie.setForegroundAlpha(0.75f);
return chart.createBufferedImage(imageWidth, imageHeight);
}
public static BufferedImage generateChart(int imageWidth, int imageHeight, String title, String timeAxisLabel, final String[] verticalAxisLabels, Paint[] colors, int[] weight, int[][] combination, BasicTimeSeries[] bts) {
Locale.setDefault(Locale.ENGLISH);
ValueAxis timeAxis = new HorizontalDateAxis(timeAxisLabel);
timeAxis.setCrosshairVisible(false);
CombinedXYPlot combinedPlot = new CombinedXYPlot(timeAxis, CombinedXYPlot.VERTICAL);
TimeSeriesCollection tsc = new TimeSeriesCollection();
for (int i = 0; i < bts.length; ++i) {
bts[i].setName(verticalAxisLabels[i]);
tsc.addSeries(bts[i]);
}
CombinedDataset data = new CombinedDataset();
try {
data.add(new PlotFit(tsc, new MovingAveragePlotFitAlgorithm()).getFit());
} catch (IllegalArgumentException iae) {
data.add(new PlotFit(tsc, new LinearPlotFitAlgorithm()).getFit());
}
for (int i = 0; i < combination.length; ++i) {
SeriesDataset[] sd = new SeriesDataset[combination[i].length];
for (int j = 0; j < sd.length; ++j) {
sd[j] = new SubSeriesDataset(data, combination[i][j]);
}
CombinedDataset combData = new CombinedDataset(sd);
VerticalNumberAxis valueAxis = new VerticalNumberAxis("");
valueAxis.setCrosshairVisible(false);
valueAxis.setAutoRangeIncludesZero(false);
XYPlot subplot = null;
if (i == 0) {
subplot = new XYPlot(combData, null, valueAxis);
Range range = subplot.getVerticalDataRange();
if (range.getLength() > 10000000000.0) {
valueAxis.setTickUnit(new NumberTickUnit(50000000000.0, new java.text.DecimalFormat("#,##0")));
}
} else {
subplot = new XYPlot(combData, null, valueAxis, new VerticalXYBarRenderer(0.20));
}
for (int j = 0; j < sd.length; ++j) {
subplot.setSeriesPaint(j, colors[combination[i][j]]);
}
combinedPlot.add(subplot, weight[i]);
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, combinedPlot, true);
chart.setBackgroundPaint(Color.white);
return chart.createBufferedImage(imageWidth, imageHeight);
}
public static BasicTimeSeries[] createNumberOfComputedZeros(Connection con) throws SQLException {
BasicTimeSeries[] t = { new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries("") };
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT DAYS(res.stop),SUM(CAST(comp.range AS DECIMAL(20, 0))),"
+ "SUM(SQRT((comp.work_unit_id+comp.range)/12.566370614359)*148*1.245*1.246*comp.range)/1000000000000"
+ " FROM zeta.computation comp, zeta.result res"
+ " WHERE comp.task_id=res.task_id AND comp.work_unit_id=res.work_unit_id"
+ " GROUP BY DAYS(res.stop)"
+ " ORDER BY DAYS(res.stop)");
Calendar cal = Calendar.getInstance();
long lastDay = 0;
long lastValue = 0;
long lastFlops = 0;
double sum = 0.0;
Day d = null;
while (rs.next()) {
long day = DatabaseUtils.convertDaysToTimeMillis((long)rs.getInt(1));
long value = rs.getLong(2);
long flops = rs.getLong(3);
if (lastDay > 0) {
cal.setTime(new Date(lastDay));
sum += lastValue;
d = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR));
t[0].add(d, sum);
t[1].add(d, new Long(lastValue));
t[2].add(d, new Long(lastFlops));
if (t[3].getItemCount() == 0) {
t[3].add(d, new Long(1500000000L));
t[4].add(d, new Long(10000000000L));
t[5].add(d, new Long(50000000000L));
t[6].add(d, new Long(100000000000L));
t[7].add(d, new Long(1000000000000L));
}
}
lastDay = day; lastValue = value; lastFlops = flops;
}
if (d != null) {
t[3].add(d, new Long(1500000000L));
t[4].add(d, new Long(10000000000L));
t[5].add(d, new Long(50000000000L));
t[6].add(d, new Long(100000000000L));
t[7].add(d, new Long(1000000000000L));
}
rs.close();
} finally {
DatabaseUtils.close(stmt);
}
return t;
}
public static BasicTimeSeries[] createNumberOfParticipants(Connection con) throws SQLException {
BasicTimeSeries[] t = { new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries("") };
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MIN(DAYS(res.stop)) AS start_day,comp.server_id,comp.user_id"
+ " FROM zeta.computation comp, zeta.result res"
+ " WHERE res.task_id=comp.task_id AND res.work_unit_id=comp.work_unit_id"
+ " GROUP BY comp.server_id,comp.user_id ORDER BY start_day");
Calendar cal = Calendar.getInstance();
int count = 0;
int sum = 0;
long lastDay = 0;
while (rs.next()) {
long day = DatabaseUtils.convertDaysToTimeMillis((long)rs.getInt(1));
if (lastDay > 0 && lastDay != day) {
cal.setTime(new Date(lastDay));
sum += count;
Day d = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR));
t[0].add(d, new Integer(sum));
t[1].add(d, new Integer(count));
count = 0;
}
lastDay = day; ++count;
}
cal.setTime(new Date(lastDay));
sum += count;
Day d = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR));
t[0].add(d, new Integer(sum));
t[1].add(d, new Integer(count));
rs.close();
rs = stmt.executeQuery("WITH a (day,server_id,user_id) AS"
+ " (SELECT DISTINCT DAYS(res.stop),comp.server_id,comp.user_id"
+ " FROM zeta.computation comp, zeta.result res"
+ " WHERE res.task_id=comp.task_id AND res.work_unit_id=comp.work_unit_id)"
+ " SELECT day,COUNT(*) FROM a GROUP BY day ORDER BY day");
int lastValue = 0;
lastDay = 0;
while (rs.next()) {
long day = DatabaseUtils.convertDaysToTimeMillis((long)rs.getInt(1));
int value = rs.getInt(2);
if (lastDay > 0) {
cal.setTime(new Date(lastDay));
t[2].add(new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR)), new Integer(lastValue));
}
lastDay = day; lastValue = value;
}
rs.close();
} finally {
DatabaseUtils.close(stmt);
}
return t;
}
public static BasicTimeSeries[] createNumberOfComputers(Connection con) throws SQLException {
BasicTimeSeries[] t = { new BasicTimeSeries(""), new BasicTimeSeries(""), new BasicTimeSeries("") };
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MIN(DAYS(res.stop)) AS start_day,comp.server_id,comp.workstation_id"
+ " FROM zeta.computation comp, zeta.result res"
+ " WHERE res.task_id=comp.task_id AND res.work_unit_id=comp.work_unit_id"
+ " GROUP BY comp.server_id,comp.workstation_id ORDER BY start_day");
Calendar cal = Calendar.getInstance();
int count = 0;
int sum = 0;
long lastDay = 0;
while (rs.next()) {
long day = DatabaseUtils.convertDaysToTimeMillis((long)rs.getInt(1));
if (lastDay > 0 && lastDay != day) {
cal.setTime(new Date(lastDay));
sum += count;
Day d = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR));
t[0].add(d, new Integer(sum));
t[1].add(d, new Integer(count));
count = 0;
}
lastDay = day; ++count;
}
rs.close();
sum += count;
if (lastDay > 0) {
cal.setTime(new Date(lastDay));
Day d = new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR));
t[0].add(d, new Integer(sum));
t[1].add(d, new Integer(count));
}
rs = stmt.executeQuery("WITH a (day,server_id,workstation_id) AS"
+ " (SELECT DISTINCT DAYS(res.stop),comp.server_id,comp.workstation_id"
+ " FROM zeta.computation comp, zeta.result res"
+ " WHERE res.task_id=comp.task_id AND res.work_unit_id=comp.work_unit_id)"
+ " SELECT day,COUNT(*) FROM a GROUP BY day ORDER BY day");
int lastValue = 0;
lastDay = 0;
while (rs.next()) {
long day = DatabaseUtils.convertDaysToTimeMillis((long)rs.getInt(1));
int value = rs.getInt(2);
if (lastDay > 0) {
cal.setTime(new Date(lastDay));
t[2].add(new Day(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR)), new Double(lastValue));
}
lastDay = day; lastValue = value;
}
rs.close();
} finally {
DatabaseUtils.close(stmt);
}
return t;
}
public static BasicTimeSeries[] createWorkLoad(Connection con, Map workloadSummary) {
BasicTimeSeries[] t = { new BasicTimeSeries("", Hour.class), new BasicTimeSeries("", Hour.class), new BasicTimeSeries("", Hour.class), new BasicTimeSeries("", Hour.class) };
int day = 0;
Iterator i = workloadSummary.keySet().iterator();
while (i.hasNext()) {
day = Math.max(day, ((Integer)i.next()).intValue()/24);
}
int hours1 = workloadSummary.keySet().size();
int hours2 = 0;
i = workloadSummary.keySet().iterator();
while (i.hasNext()) {
if (((Integer)i.next()).intValue()/24 == day) {
++hours2;
}
}
hours1 -= hours2;
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
for (int j = 0; j <= 1; ++j, --day) {
cal.set(Calendar.DAY_OF_YEAR, day);
cal.add(Calendar.DAY_OF_YEAR, j);
long sum = 0;
for (int hour = 0; hour < 24; ++hour) {
Long range = null;
Object[] values = (Object[])workloadSummary.get(new Integer(day*24+hour));
if (values == null) {
if (j == 0 && hours2 > 0 || j == 1 && hours1 > 0) {
range = new Long(0);
} else {
break;
}
} else {
range = (Long)values[3];
sum += range.longValue();
if (j == 0) {
--hours2;
} else {
--hours1;
}
}
cal.set(Calendar.HOUR_OF_DAY, hour);
Hour h = new Hour(cal.getTime());
t[j].add(h, new Long(sum));
t[2+j].add(h, range);
}
}
return t;
}
public static BasicTimeSeries[] createNewReservedZeros(Connection con, int taskId) throws SQLException {
BasicTimeSeries[] t = { new BasicTimeSeries("", Hour.class), new BasicTimeSeries("", Hour.class), new BasicTimeSeries("", Hour.class), new BasicTimeSeries("", Hour.class) };
Statement stmt = null;
try {
stmt = con.createStatement();
long day = CachedQueries.getMaxDays(stmt);
ResultSet rs = stmt.executeQuery("SELECT HOUR(start),DAYS(start),SUM(range),COUNT(work_unit_id),MIN(start)"
+ " FROM zeta.computation WHERE DAYS(start) IN ("
+ day + ',' + (day-1)
+ ") GROUP BY HOUR(start),DAYS(start) ORDER BY HOUR(start)");
Calendar cal = Calendar.getInstance();
Hour prevH = null;
Long prevValue = null;
Integer prevCount = null;
while (rs.next()) {
int activeDay = rs.getInt(2);
long value = rs.getLong(3);
int count = rs.getInt(4);
Timestamp timestamp = rs.getTimestamp(5);
cal.setTime(timestamp);
if (activeDay == day) {
if (prevH != null) {
t[0].add(prevH, prevValue);
t[2].add(prevH, prevCount);
}
prevH = new Hour(cal.getTime());
prevValue = new Long(value);
prevCount = new Integer(count);
} else {
cal.add(Calendar.DAY_OF_YEAR, 1);
Hour h = new Hour(cal.getTime());
t[1].add(h, new Long(value));
t[3].add(h, new Integer(count));
}
}
rs.close();
} finally {
DatabaseUtils.close(stmt);
}
return t;
}
}