org/sonews/web/SonewsChartServlet.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     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.
     9  *
    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.
    14  *
    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/>.
    17  */
    18 
    19 package org.sonews.web;
    20 
    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;
    26 
    27 /**
    28  * Servlet that creates chart images and returns them as raw PNG images.
    29  * @author Christian Lins
    30  * @since sonews/0.5.0
    31  */
    32 public class SonewsChartServlet extends AbstractSonewsServlet
    33 {
    34   
    35   private ITrace2D createProcessMails24(String title, String cmd)
    36     throws IOException
    37   {
    38     int[] data = read24Values(cmd);
    39     ITrace2D trace = new Trace2DSimple(title);
    40     trace.addPoint(0.0, 0.0); // Start
    41     
    42     for(int n = 0; n < 24; n++)
    43     {
    44       trace.addPoint(n, data[n]);
    45     }
    46 
    47     return trace;
    48   }
    49   
    50   @Override
    51   public void doGet(HttpServletRequest req, HttpServletResponse resp)
    52     throws IOException
    53   {
    54     synchronized(this)
    55     {
    56       MemoryBitmapChart chart = new MemoryBitmapChart();
    57 
    58       String name  = req.getParameter("name");
    59       String group = req.getParameter("group");
    60       ITrace2D trace;
    61       String   cmd = "XDAEMON LOG";
    62 
    63       if(name.equals("feedednewsyesterday"))
    64       {
    65         cmd = cmd + " TRANSMITTED_NEWS";
    66         cmd = group != null ? cmd + " " + group : cmd;
    67         trace = createProcessMails24(
    68           "To peers transmitted mails yesterday", cmd);
    69       }
    70       else if(name.equals("gatewayednewsyesterday"))
    71       {
    72         cmd = cmd + " GATEWAYED_NEWS";
    73         cmd = group != null ? cmd + " " + group : cmd;
    74         trace = createProcessMails24(
    75           "Gatewayed mails yesterday", cmd);
    76       }
    77       else
    78       {
    79         cmd = cmd + " POSTED_NEWS";
    80         cmd = group != null ? cmd + " " + group : cmd;
    81         trace = createProcessMails24(
    82           "Posted mails yesterday", cmd);
    83       }
    84       chart.addTrace(trace);
    85 
    86       resp.getOutputStream().write(chart.getRawData(500, 400));
    87       resp.setContentType(chart.getContentType());
    88       resp.setStatus(HttpServletResponse.SC_OK);
    89     }
    90   }
    91   
    92   private int[] read24Values(String command)
    93     throws IOException
    94   {
    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!");
   101     
   102     for(int n = 0; n < 24; n++)
   103     {
   104       line = readlnFromNewsserver();
   105       values[n] = Integer.parseInt(line.split(" ")[1]);
   106     }
   107     
   108     line = readlnFromNewsserver(); // "."
   109     
   110     disconnectFromNewsserver();
   111     return values;
   112   }
   113   
   114 }