org/sonews/web/SonewsChartServlet.java
changeset 2 1090e2141798
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/web/SonewsChartServlet.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,114 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.web;
    1.23 +
    1.24 +import info.monitorenter.gui.chart.ITrace2D;
    1.25 +import info.monitorenter.gui.chart.traces.Trace2DSimple;
    1.26 +import java.io.IOException;
    1.27 +import javax.servlet.http.HttpServletRequest;
    1.28 +import javax.servlet.http.HttpServletResponse;
    1.29 +
    1.30 +/**
    1.31 + * Servlet that creates chart images and returns them as raw PNG images.
    1.32 + * @author Christian Lins
    1.33 + * @since sonews/0.5.0
    1.34 + */
    1.35 +public class SonewsChartServlet extends AbstractSonewsServlet
    1.36 +{
    1.37 +  
    1.38 +  private ITrace2D createProcessMails24(String title, String cmd)
    1.39 +    throws IOException
    1.40 +  {
    1.41 +    int[] data = read24Values(cmd);
    1.42 +    ITrace2D trace = new Trace2DSimple(title);
    1.43 +    trace.addPoint(0.0, 0.0); // Start
    1.44 +    
    1.45 +    for(int n = 0; n < 24; n++)
    1.46 +    {
    1.47 +      trace.addPoint(n, data[n]);
    1.48 +    }
    1.49 +
    1.50 +    return trace;
    1.51 +  }
    1.52 +  
    1.53 +  @Override
    1.54 +  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    1.55 +    throws IOException
    1.56 +  {
    1.57 +    synchronized(this)
    1.58 +    {
    1.59 +      MemoryBitmapChart chart = new MemoryBitmapChart();
    1.60 +
    1.61 +      String name  = req.getParameter("name");
    1.62 +      String group = req.getParameter("group");
    1.63 +      ITrace2D trace;
    1.64 +      String   cmd = "XDAEMON LOG";
    1.65 +
    1.66 +      if(name.equals("feedednewsyesterday"))
    1.67 +      {
    1.68 +        cmd = cmd + " TRANSMITTED_NEWS";
    1.69 +        cmd = group != null ? cmd + " " + group : cmd;
    1.70 +        trace = createProcessMails24(
    1.71 +          "To peers transmitted mails yesterday", cmd);
    1.72 +      }
    1.73 +      else if(name.equals("gatewayednewsyesterday"))
    1.74 +      {
    1.75 +        cmd = cmd + " GATEWAYED_NEWS";
    1.76 +        cmd = group != null ? cmd + " " + group : cmd;
    1.77 +        trace = createProcessMails24(
    1.78 +          "Gatewayed mails yesterday", cmd);
    1.79 +      }
    1.80 +      else
    1.81 +      {
    1.82 +        cmd = cmd + " POSTED_NEWS";
    1.83 +        cmd = group != null ? cmd + " " + group : cmd;
    1.84 +        trace = createProcessMails24(
    1.85 +          "Posted mails yesterday", cmd);
    1.86 +      }
    1.87 +      chart.addTrace(trace);
    1.88 +
    1.89 +      resp.getOutputStream().write(chart.getRawData(500, 400));
    1.90 +      resp.setContentType(chart.getContentType());
    1.91 +      resp.setStatus(HttpServletResponse.SC_OK);
    1.92 +    }
    1.93 +  }
    1.94 +  
    1.95 +  private int[] read24Values(String command)
    1.96 +    throws IOException
    1.97 +  {
    1.98 +    int[] values = new int[24];
    1.99 +    connectToNewsserver();
   1.100 +    printlnToNewsserver(command);
   1.101 +    String line = readlnFromNewsserver();
   1.102 +    if(!line.startsWith("200 "))
   1.103 +      throw new IOException(command + " not supported!");
   1.104 +    
   1.105 +    for(int n = 0; n < 24; n++)
   1.106 +    {
   1.107 +      line = readlnFromNewsserver();
   1.108 +      values[n] = Integer.parseInt(line.split(" ")[1]);
   1.109 +    }
   1.110 +    
   1.111 +    line = readlnFromNewsserver(); // "."
   1.112 +    
   1.113 +    disconnectFromNewsserver();
   1.114 +    return values;
   1.115 +  }
   1.116 +  
   1.117 +}