chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.web; chris@1: chris@1: import info.monitorenter.gui.chart.ITrace2D; chris@1: import info.monitorenter.gui.chart.traces.Trace2DSimple; chris@1: import java.io.IOException; chris@1: import javax.servlet.http.HttpServletRequest; chris@1: import javax.servlet.http.HttpServletResponse; chris@1: chris@1: /** chris@1: * Servlet that creates chart images and returns them as raw PNG images. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public class SonewsChartServlet extends AbstractSonewsServlet chris@1: { chris@1: chris@1: private ITrace2D createProcessMails24(String title, String cmd) chris@1: throws IOException chris@1: { chris@1: int[] data = read24Values(cmd); chris@1: ITrace2D trace = new Trace2DSimple(title); chris@1: trace.addPoint(0.0, 0.0); // Start chris@1: chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@1: trace.addPoint(n, data[n]); chris@1: } chris@1: chris@1: return trace; chris@1: } chris@1: chris@1: @Override chris@1: public void doGet(HttpServletRequest req, HttpServletResponse resp) chris@1: throws IOException chris@1: { chris@1: synchronized(this) chris@1: { chris@1: MemoryBitmapChart chart = new MemoryBitmapChart(); chris@1: chris@1: String name = req.getParameter("name"); chris@1: String group = req.getParameter("group"); chris@1: ITrace2D trace; chris@1: String cmd = "XDAEMON LOG"; chris@1: chris@1: if(name.equals("feedednewsyesterday")) chris@1: { chris@1: cmd = cmd + " TRANSMITTED_NEWS"; chris@1: cmd = group != null ? cmd + " " + group : cmd; chris@1: trace = createProcessMails24( chris@1: "To peers transmitted mails yesterday", cmd); chris@1: } chris@1: else if(name.equals("gatewayednewsyesterday")) chris@1: { chris@1: cmd = cmd + " GATEWAYED_NEWS"; chris@1: cmd = group != null ? cmd + " " + group : cmd; chris@1: trace = createProcessMails24( chris@1: "Gatewayed mails yesterday", cmd); chris@1: } chris@1: else chris@1: { chris@1: cmd = cmd + " POSTED_NEWS"; chris@1: cmd = group != null ? cmd + " " + group : cmd; chris@1: trace = createProcessMails24( chris@1: "Posted mails yesterday", cmd); chris@1: } chris@1: chart.addTrace(trace); chris@1: chris@1: resp.getOutputStream().write(chart.getRawData(500, 400)); chris@1: resp.setContentType(chart.getContentType()); chris@1: resp.setStatus(HttpServletResponse.SC_OK); chris@1: } chris@1: } chris@1: chris@1: private int[] read24Values(String command) chris@1: throws IOException chris@1: { chris@1: int[] values = new int[24]; chris@1: connectToNewsserver(); chris@1: printlnToNewsserver(command); chris@1: String line = readlnFromNewsserver(); chris@1: if(!line.startsWith("200 ")) chris@1: throw new IOException(command + " not supported!"); chris@1: chris@1: for(int n = 0; n < 24; n++) chris@1: { chris@1: line = readlnFromNewsserver(); chris@1: values[n] = Integer.parseInt(line.split(" ")[1]); chris@1: } chris@1: chris@1: line = readlnFromNewsserver(); // "." chris@1: chris@1: disconnectFromNewsserver(); chris@1: return values; chris@1: } chris@1: chris@1: }