3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.web;
21 import info.monitorenter.gui.chart.ITrace2D;
22 import info.monitorenter.gui.chart.traces.Trace2DSimple;
23 import java.io.IOException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
28 * Servlet that creates chart images and returns them as raw PNG images.
29 * @author Christian Lins
32 public class SonewsChartServlet extends AbstractSonewsServlet
35 private ITrace2D createProcessMails24(String title, String cmd)
38 int[] data = read24Values(cmd);
39 ITrace2D trace = new Trace2DSimple(title);
40 trace.addPoint(0.0, 0.0); // Start
42 for(int n = 0; n < 24; n++)
44 trace.addPoint(n, data[n]);
51 public void doGet(HttpServletRequest req, HttpServletResponse resp)
56 MemoryBitmapChart chart = new MemoryBitmapChart();
58 String name = req.getParameter("name");
59 String group = req.getParameter("group");
61 String cmd = "XDAEMON LOG";
63 if(name.equals("feedednewsyesterday"))
65 cmd = cmd + " TRANSMITTED_NEWS";
66 cmd = group != null ? cmd + " " + group : cmd;
67 trace = createProcessMails24(
68 "To peers transmitted mails yesterday", cmd);
70 else if(name.equals("gatewayednewsyesterday"))
72 cmd = cmd + " GATEWAYED_NEWS";
73 cmd = group != null ? cmd + " " + group : cmd;
74 trace = createProcessMails24(
75 "Gatewayed mails yesterday", cmd);
79 cmd = cmd + " POSTED_NEWS";
80 cmd = group != null ? cmd + " " + group : cmd;
81 trace = createProcessMails24(
82 "Posted mails yesterday", cmd);
84 chart.addTrace(trace);
86 resp.getOutputStream().write(chart.getRawData(500, 400));
87 resp.setContentType(chart.getContentType());
88 resp.setStatus(HttpServletResponse.SC_OK);
92 private int[] read24Values(String command)
95 int[] values = new int[24];
96 connectToNewsserver();
97 printlnToNewsserver(command);
98 String line = readlnFromNewsserver();
99 if(!line.startsWith("200 "))
100 throw new IOException(command + " not supported!");
102 for(int n = 0; n < 24; n++)
104 line = readlnFromNewsserver();
105 values[n] = Integer.parseInt(line.split(" ")[1]);
108 line = readlnFromNewsserver(); // "."
110 disconnectFromNewsserver();