});
args.res.flush();
} catch (IOException e) {
e.printStackTrace();
}
msg.reply(new Message(Message.MSG_INTERN).setLineString("logsize " + byteCounter.bytes).parse());
return 1;
}
};
cmdH.setUserLevel(-1);
kernel.registerCommandHandler(cmdH);
cmdH = new StandAloneCommandHandler(getCommandPrefix() + "/statsImage") {
class ByteCounter {
public int bytes;
}
public StandAloneMessageHandler setUserLevel(int userlevel) {
separator = "/";
return super.setUserLevel(userlevel);
}
public Message getAddHandlerMessage() {
return (new URLMessage()).setLineString(this.getCommand()).setMessagetype(Message.MSG_ADD_HANDLER).setFrom(this).parse();
}
public final Color availablecolors[] = { Color.gray,Color.blue,Color.red,Color.green,Color.yellow,Color.cyan,Color.magenta,Color.darkGray,Color.lightGray,Color.orange,Color.pink };
public int handleMessage(Message msg) {
HTTPServer.Data data = (HTTPServer.Data)msg.getFrom();
//Args args = new Args(data.request,data.response,data.session,msg,data);
final Args args = httpModule.createArgs(data,msg);
if(args.msg.getMsgParameter() < 1) return 0;
String jid = args.msg.getMsgParameter(0).toLowerCase();
boolean showmodes = Boolean.parseBoolean(args.req.getValue("showmodes","true"));
boolean showgames = Boolean.parseBoolean(args.req.getValue("showgames","true"));
int width = Integer.parseInt(args.req.getValue("width","500"));
int height = Integer.parseInt(args.req.getValue("height","500"));
args.res.setHeader("content-type:","image/png");
List<Map<String,Object>> getjidID = db.getListOfMaps("SELECT * FROM goim_stats_jid WHERE jid = ?",jid);
if(getjidID.size() > 0) {
final int DAYCOUNT = 14;
final int MAXTYPES = 10;
double[][] points = new double[ MAXTYPES ][ DAYCOUNT ];
String[] labels = new String[MAXTYPES];
String[] xAxisLabels = new String[DAYCOUNT];
int jidID = (Integer)getjidID.get(0).get("id");
List<Map<String, Object>> days = db.getListOfMaps("SELECT * FROM goim_stats_day WHERE jid = ? ORDER BY day DESC LIMIT 14",jidID);
int i = DAYCOUNT - days.size();
for(i = 0 ; i < (DAYCOUNT - days.size()) ; i++) {
xAxisLabels[i] = "undefined ?";
}
i = DAYCOUNT-1;
for(Map<String, Object> day : days) {
if(showmodes) {
List<Map<String, Object>> presences = db.getListOfMaps("SELECT * FROM goim_stats_presencestatus WHERE day = ?",day.get("id"));
for(Map<String,Object> presence : presences) {
String mode = (String)presence.get("mode");
int duration = (Integer)presence.get("duration");
int j = 0;
for(; j < MAXTYPES ; j++) {
if(labels[j] == null) {
labels[j] = mode; break;
} else if(labels[j].equals(mode))
break;
}
if(j == MAXTYPES) {
System.err.println("Too much modes ?! Mode: " + mode);
break;
}
points[j][i] = ((double)duration) / ((double)60);
}
}
if(showgames) {
List<Map<String, Object>> games = db.getListOfMaps("SELECT * FROM goim_stats_game WHERE day = ?",day.get("id"));
for(Map<String,Object> game : games) {
String gamestr = (String)game.get("game");
int duration = (Integer)game.get("duration");
int j = 0;
for(; j < MAXTYPES ; j++) {
if(labels[j] == null) {
labels[j] = gamestr; break;
} else if(labels[j].equals(gamestr))
break;
}
if(j == MAXTYPES) {
System.err.println("Too much modes ?! Mode: " + gamestr);
break;
}
points[j][i] = ((double)duration) / ((double)60);
}
}
xAxisLabels[i] = day.get("day").toString();
i--;
}
int realcount = -1;
for(i = 0 ; i < labels.length ; i++) {
if(labels[i] == null) { realcount = i; break; }
}
if(realcount != labels.length) {
double[][] originalpoints = points;
String[] originallabels = labels;
points = new double[ realcount ][ DAYCOUNT ];
labels = new String[ realcount ];
for(i = 0 ; i < realcount ; i++ ) {
points[i] = originalpoints[i];
labels[i] = originallabels[i];
}
}
Stroke[] strokes = new Stroke[realcount];
Shape[] shapes = new Shape[realcount];
for(int j = 0 ; j < realcount ; j++) {
strokes[j] = LineChartProperties.DEFAULT_LINE_STROKE;
shapes[j] = PointChartProperties.SHAPE_CIRCLE;
}
LineChartProperties lineChartProperties = new LineChartProperties(strokes,shapes);
try {
Paint[] paint = new Paint[labels.length];
for(int j = 0 ; j < paint.length ; j++) {
paint[j] = availablecolors[j];
}
AxisChartDataSet axisChartDataSet = new AxisChartDataSet(points,labels,paint,ChartType.LINE,lineChartProperties);
DataSeries dataSeries = new DataSeries(xAxisLabels,"Date","Time in Hours","Online Statistics for " + jid);
dataSeries.addIAxisPlotDataSet(axisChartDataSet);
ChartProperties chartProperties = new ChartProperties();
AxisProperties axisProperties = new AxisProperties( );
axisProperties.setXAxisLabelsAreVertical(true);
DataAxisProperties dataAxisProperties= (DataAxisProperties) axisProperties.getYAxisProperties();
dataAxisProperties.setRoundToNearest(-1);
LegendProperties legendProperties = new LegendProperties();
legendProperties.setPlacement(LegendProperties.RIGHT);
legendProperties.setNumColumns(1);
AxisChart axisChart = new AxisChart(dataSeries,chartProperties,axisProperties,legendProperties,width,height);
final ByteCounter byteCounter = new ByteCounter(); byteCounter.bytes = 0;
PNGEncoder.encode(axisChart,new OutputStream() {
public void write(int b) throws IOException {
args.res.print((char)b);
byteCounter.bytes++;
}
});
args.res.flush();
msg.reply(new Message(Message.MSG_INTERN).setLineString("logsize " + byteCounter.bytes).parse());
return 1;
} catch (ChartDataException e) {
e.printStackTrace();
} catch (PropertyException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.drawString("Error while creating Statistics.",5,15);
final ByteCounter byteCounter = new ByteCounter(); byteCounter.bytes = 0;
try {
ImageIO.write(image,"png",new OutputStream() {
public void write(int b) throws IOException {
args.res.print((char)b);
byteCounter.bytes++;
}
});
args.res.flush();
} catch (IOException e) {
e.printStackTrace();
}
msg.reply(new Message(Message.MSG_INTERN).setLineString("logsize " + byteCounter.bytes).parse());
//msg.reply(new Message(Message.MSG_INTERN).setLineString("logsize " + byteCounter.bytes).parse());
return 1;
}
};
cmdH.setUserLevel(-1);