org/sonews/web/SonewsChartServlet.java
author chris <chris@marvin>
Wed Jul 01 10:48:22 2009 +0200 (2009-07-01)
changeset 2 1090e2141798
permissions -rw-r--r--
sonews/0.5.1 fixes merged
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.web;
chris@1
    20
chris@1
    21
import info.monitorenter.gui.chart.ITrace2D;
chris@1
    22
import info.monitorenter.gui.chart.traces.Trace2DSimple;
chris@1
    23
import java.io.IOException;
chris@1
    24
import javax.servlet.http.HttpServletRequest;
chris@1
    25
import javax.servlet.http.HttpServletResponse;
chris@1
    26
chris@1
    27
/**
chris@1
    28
 * Servlet that creates chart images and returns them as raw PNG images.
chris@1
    29
 * @author Christian Lins
chris@1
    30
 * @since sonews/0.5.0
chris@1
    31
 */
chris@1
    32
public class SonewsChartServlet extends AbstractSonewsServlet
chris@1
    33
{
chris@1
    34
  
chris@1
    35
  private ITrace2D createProcessMails24(String title, String cmd)
chris@1
    36
    throws IOException
chris@1
    37
  {
chris@1
    38
    int[] data = read24Values(cmd);
chris@1
    39
    ITrace2D trace = new Trace2DSimple(title);
chris@1
    40
    trace.addPoint(0.0, 0.0); // Start
chris@1
    41
    
chris@1
    42
    for(int n = 0; n < 24; n++)
chris@1
    43
    {
chris@1
    44
      trace.addPoint(n, data[n]);
chris@1
    45
    }
chris@1
    46
chris@1
    47
    return trace;
chris@1
    48
  }
chris@1
    49
  
chris@1
    50
  @Override
chris@1
    51
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
chris@1
    52
    throws IOException
chris@1
    53
  {
chris@1
    54
    synchronized(this)
chris@1
    55
    {
chris@1
    56
      MemoryBitmapChart chart = new MemoryBitmapChart();
chris@1
    57
chris@1
    58
      String name  = req.getParameter("name");
chris@1
    59
      String group = req.getParameter("group");
chris@1
    60
      ITrace2D trace;
chris@1
    61
      String   cmd = "XDAEMON LOG";
chris@1
    62
chris@1
    63
      if(name.equals("feedednewsyesterday"))
chris@1
    64
      {
chris@1
    65
        cmd = cmd + " TRANSMITTED_NEWS";
chris@1
    66
        cmd = group != null ? cmd + " " + group : cmd;
chris@1
    67
        trace = createProcessMails24(
chris@1
    68
          "To peers transmitted mails yesterday", cmd);
chris@1
    69
      }
chris@1
    70
      else if(name.equals("gatewayednewsyesterday"))
chris@1
    71
      {
chris@1
    72
        cmd = cmd + " GATEWAYED_NEWS";
chris@1
    73
        cmd = group != null ? cmd + " " + group : cmd;
chris@1
    74
        trace = createProcessMails24(
chris@1
    75
          "Gatewayed mails yesterday", cmd);
chris@1
    76
      }
chris@1
    77
      else
chris@1
    78
      {
chris@1
    79
        cmd = cmd + " POSTED_NEWS";
chris@1
    80
        cmd = group != null ? cmd + " " + group : cmd;
chris@1
    81
        trace = createProcessMails24(
chris@1
    82
          "Posted mails yesterday", cmd);
chris@1
    83
      }
chris@1
    84
      chart.addTrace(trace);
chris@1
    85
chris@1
    86
      resp.getOutputStream().write(chart.getRawData(500, 400));
chris@1
    87
      resp.setContentType(chart.getContentType());
chris@1
    88
      resp.setStatus(HttpServletResponse.SC_OK);
chris@1
    89
    }
chris@1
    90
  }
chris@1
    91
  
chris@1
    92
  private int[] read24Values(String command)
chris@1
    93
    throws IOException
chris@1
    94
  {
chris@1
    95
    int[] values = new int[24];
chris@1
    96
    connectToNewsserver();
chris@1
    97
    printlnToNewsserver(command);
chris@1
    98
    String line = readlnFromNewsserver();
chris@1
    99
    if(!line.startsWith("200 "))
chris@1
   100
      throw new IOException(command + " not supported!");
chris@1
   101
    
chris@1
   102
    for(int n = 0; n < 24; n++)
chris@1
   103
    {
chris@1
   104
      line = readlnFromNewsserver();
chris@1
   105
      values[n] = Integer.parseInt(line.split(" ")[1]);
chris@1
   106
    }
chris@1
   107
    
chris@1
   108
    line = readlnFromNewsserver(); // "."
chris@1
   109
    
chris@1
   110
    disconnectFromNewsserver();
chris@1
   111
    return values;
chris@1
   112
  }
chris@1
   113
  
chris@1
   114
}