Пример #1
0
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    try {
      // From AxisChartServlet.java:init()
      LegendProperties legendProperties = new LegendProperties();
      ChartProperties chartProperties = new ChartProperties();
      AxisProperties axisProperties = new AxisProperties(false);

      ChartFont axisScaleFont =
          new ChartFont(new Font("Georgia Negreta cursiva", Font.PLAIN, 13), Color.black);
      axisProperties.getXAxisProperties().setScaleChartFont(axisScaleFont);
      axisProperties.getYAxisProperties().setScaleChartFont(axisScaleFont);

      ChartFont axisTitleFont =
          new ChartFont(new Font("Arial Narrow", Font.PLAIN, 14), Color.black);
      axisProperties.getXAxisProperties().setTitleChartFont(axisTitleFont);
      axisProperties.getYAxisProperties().setTitleChartFont(axisTitleFont);

      Stroke[] strokes = {
        LineChartProperties.DEFAULT_LINE_STROKE,
        LineChartProperties.DEFAULT_LINE_STROKE,
        LineChartProperties.DEFAULT_LINE_STROKE
      };
      Shape[] shapes = {
        PointChartProperties.SHAPE_TRIANGLE,
        PointChartProperties.SHAPE_DIAMOND,
        PointChartProperties.SHAPE_CIRCLE
      };
      LineChartProperties lineChartProperties = new LineChartProperties(strokes, shapes);

      String[] xAxisLabels = {"1998", "1999", "2000", "2001", "2002", "2003", "2004"};
      String xAxisTitle = "Years";
      String yAxisTitle = "Problems";
      String title = "Micro$oft At Work";
      DataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle, yAxisTitle, title);

      // From AxisChartServlet.java:createAxisChartDataSet
      double[][] data = TestDataGenerator.getRandomNumbers(3, 7, 200, 500);
      String[] legendLabels = {"Bugs", "Security Holes", "Backdoors"};
      Paint[] paints = TestDataGenerator.getRandomPaints(3);
      AxisChartDataSet acds =
          new AxisChartDataSet(data, legendLabels, paints, ChartType.LINE, lineChartProperties);
      dataSeries.addIAxisPlotDataSet(acds);
      AxisChart axisChart =
          new AxisChart(dataSeries, chartProperties, axisProperties, legendProperties, 550, 360);

      ServletEncoderHelper.encodeJPEG13(axisChart, 1.0f, response);

    } catch (Exception e) {
      System.out.println(e);
    }
  }
  private String generateAreaChart(HttpServletRequest request, Statement s)
      throws ChartDataException {

    boolean restrictTime = false;
    boolean minScale = false;
    boolean halfHourScale = false;
    boolean dayScale = false;

    short startYear = 0;
    short startMonth = 0;
    short startDay = 0;
    short startHour = 0;
    short startMin = 0;

    short endYear = 0;
    short endMonth = 0;
    short endDay = 0;
    short endHour = 0;
    short endMin = 0;

    short numDataPoints = 0;

    int bytesDivisor = 1;
    int port = 0;

    long start = 0, end = 0;
    long unit = 0;

    double[][] data;

    String title;
    String output = "";
    String[] legendLabels;

    Paint[] paints = null;

    ResultSet result = null;

    NumberFormat nf = NumberFormat.getNumberInstance();
    nf.setMinimumIntegerDigits(2);

    // ****************set some chart attributes*****************

    AreaChartProperties areaChartProperties = new AreaChartProperties();
    LegendProperties legendProperties = new LegendProperties();

    AxisProperties axisProperties = new AxisProperties(false);
    axisProperties.setXAxisLabelsAreVertical(true);

    ChartFont axisScaleFont =
        new ChartFont(new Font("Georgia Negreta cursiva", Font.PLAIN, 13), Color.black);

    LabelAxisProperties xAxisProperties = (LabelAxisProperties) axisProperties.getXAxisProperties();
    xAxisProperties.setScaleChartFont(axisScaleFont);

    LabelAxisProperties yAxisProperties = (LabelAxisProperties) axisProperties.getYAxisProperties();
    yAxisProperties.setScaleChartFont(axisScaleFont);

    ChartFont axisTitleFont = new ChartFont(new Font("Arial Narrow", Font.PLAIN, 14), Color.black);
    xAxisProperties.setTitleChartFont(axisTitleFont);
    yAxisProperties.setTitleChartFont(axisTitleFont);

    DataAxisProperties dataAxisProperties = (DataAxisProperties) yAxisProperties;
    dataAxisProperties.setNumItems(10);

    ChartFont titleFont =
        new ChartFont(new Font("Georgia Negreta cursiva", Font.PLAIN, 14), Color.black);
    ChartProperties chartProperties = new ChartProperties();
    ;
    chartProperties.setTitleFont(titleFont);

    String xAxisTitle = "Time";
    String yAxisTitle = "";

    String outputUnit = request.getParameter("unit");

    if (outputUnit.equalsIgnoreCase("bytes")) {
      yAxisTitle = "Bytes";
    } else if (outputUnit.equalsIgnoreCase("kilo")) {
      yAxisTitle = "Kilobytes";
      bytesDivisor = 1024;
    } else if (outputUnit.equalsIgnoreCase("mega")) {
      yAxisTitle = "Megabytes";
      bytesDivisor = 1024 * 1024;
    }

    // ************************************************************

    // check if time was restricted
    String[] checkValues = request.getParameterValues("checks");
    for (int i = 0; i < checkValues.length; i++) {
      if (checkValues[i].equalsIgnoreCase("restrictTime")) {
        restrictTime = true;
        break;
      }
    }

    if (!restrictTime) {
      // generate a 24h chart

      Calendar cal = GregorianCalendar.getInstance();
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
      cal.set(Calendar.MILLISECOND, 0);
      end = cal.getTimeInMillis();

      cal.add(Calendar.HOUR, -23);
      start = cal.getTimeInMillis();

      title = "Traffic over the last 24 hours";

    } else {

      startYear = Short.parseShort(request.getParameter("startYear"));
      startMonth = (short) (Short.parseShort(request.getParameter("startMonth")) - 1);
      startDay = Short.parseShort(request.getParameter("startDay"));
      startHour = Short.parseShort(request.getParameter("startHour"));
      startMin = Short.parseShort(request.getParameter("startMin"));

      endYear = Short.parseShort(request.getParameter("endYear"));
      endMonth = (short) (Short.parseShort(request.getParameter("endMonth")) - 1);
      endDay = Short.parseShort(request.getParameter("endDay"));
      endHour = Short.parseShort(request.getParameter("endHour"));
      endMin = Short.parseShort(request.getParameter("endMin"));

      Calendar startTime = new GregorianCalendar();
      startTime.set(startYear, startMonth, startDay, startHour, startMin);
      startTime.set(Calendar.SECOND, 0);
      startTime.set(Calendar.MILLISECOND, 0);
      start = startTime.getTimeInMillis();

      Calendar endTime = new GregorianCalendar();
      endTime.set(endYear, endMonth, endDay, endHour, endMin);
      endTime.set(Calendar.SECOND, 0);
      endTime.set(Calendar.MILLISECOND, 0);
      end = endTime.getTimeInMillis();

      title = "Traffic from " + startTime.getTime() + " to " + endTime.getTime();
    }

    if (start > end) {
      return "End time is before start time.";
    }

    long duration = end - start;

    if (duration <= 12 * 60 * 60 * 1000l) {
      minScale = true;
      numDataPoints = (short) (duration / (60 * 1000l));
      unit = 60 * 1000; // draw data every minute
    } else if (duration <= 2 * 24 * 60 * 60 * 1000l) {
      halfHourScale = true;
      numDataPoints = (short) (duration / (30 * 60 * 1000l));
      unit = 30 * 60 * 1000;
    } else {
      dayScale = true;
      numDataPoints = (short) (duration / (24 * 60 * 60 * 1000l));
      unit = 24 * 60 * 60 * 1000l;
      xAxisTitle = "Date";
    }

    String[] xAxisLabels = new String[numDataPoints];

    if (traffic) {
      data = new double[1][numDataPoints];
      legendLabels = new String[] {"Overall Traffic"};

    } else if (trafficSrc || trafficDst) {
      data = new double[6][numDataPoints];
      legendLabels = new String[6];
    } else { // at the moment just to have the variables initialized in any case
      data = new double[1][numDataPoints];
      legendLabels = new String[] {"Overall Traffic"};
    }

    long currXValue = start;

    Calendar tmpTime = new GregorianCalendar();

    for (int i = 0; i < numDataPoints; i++) {

      try {

        if (traffic) {

          paints = new Paint[] {Color.blue};

          String statement =
              "SELECT SUM(bytes) FROM trafficTmp WHERE firstSwitched BETWEEN "
                  + currXValue / 1000
                  + " AND "
                  + ((currXValue + unit) / 1000 - 1);

          result = s.executeQuery(statement);

          result.first();

          data[0][i] = (double) result.getLong(1) / bytesDivisor;

        } else if (trafficSrc || trafficDst) {

          int[] favouriteServices = new int[5];

          paints =
              new Paint[] {
                Color.blue, Color.red, Color.green, Color.yellow, Color.cyan, Color.gray
              };

          // first find out which services were seen most often
          String statement =
              "SELECT port, SUM(bytes) as amount FROM trafficTmp WHERE port<1024"
                  + " GROUP BY port ORDER BY amount DESC LIMIT 0,5";

          result = s.executeQuery(statement);

          int position = 0;

          while (result.next()) {

            favouriteServices[position] = result.getInt(1);

            if (favouriteServices[position] == 0) legendLabels[position] = "Layer3-Traffic";
            else legendLabels[position] = createPortOutput(favouriteServices[position], true);

            position++;
          }

          legendLabels[5] = "Other";

          // now get all the traffic and sort amount of bytes into categories
          statement =
              "SELECT port, SUM(bytes) as amount FROM trafficTmp WHERE firstSwitched BETWEEN "
                  + currXValue / 1000
                  + " AND "
                  + ((currXValue + unit) / 1000 - 1)
                  + " GROUP BY port";

          result = s.executeQuery(statement);

          while (result.next()) {

            port = result.getInt(1);

            if (port == favouriteServices[0])
              data[0][i] = (double) result.getLong(2) / bytesDivisor;
            else if (port == favouriteServices[1])
              data[1][i] = (double) result.getLong(2) / bytesDivisor;
            else if (port == favouriteServices[2])
              data[2][i] = (double) result.getLong(2) / bytesDivisor;
            else if (port == favouriteServices[3])
              data[3][i] = (double) result.getLong(2) / bytesDivisor;
            else if (port == favouriteServices[4])
              data[4][i] = (double) result.getLong(2) / bytesDivisor;
            else data[5][i] += (double) result.getLong(2) / bytesDivisor;
          }

        } else { // normally never reached
          paints =
              new Paint[] {
                Color.blue, Color.red, Color.green, Color.yellow, Color.cyan, Color.black
              };
        }

        if (minScale) {
          // if((currXValue%(5*60*1000))==0) {
          tmpTime.setTimeInMillis(currXValue);
          xAxisLabels[i] =
              nf.format(tmpTime.get(Calendar.HOUR_OF_DAY))
                  + ":"
                  + nf.format(tmpTime.get(Calendar.MINUTE));
          // } else
          //	xAxisLabels[i]= " ";
        } else if (halfHourScale) {
          // if((currXValue%(2*60*60*1000))==0) {
          tmpTime.setTimeInMillis(currXValue);
          xAxisLabels[i] =
              nf.format(tmpTime.get(Calendar.HOUR_OF_DAY))
                  + ":"
                  + nf.format(tmpTime.get(Calendar.MINUTE));
          // } else
          //	xAxisLabels[i]= " ";
        } else if (dayScale) {
          // if((currXValue%(5*24*60*60*1000))==0) {
          tmpTime.setTimeInMillis(currXValue);
          xAxisLabels[i] =
              nf.format(tmpTime.get(Calendar.DATE))
                  + "."
                  + nf.format(tmpTime.get(Calendar.MONTH) + 1);
          // } else
          //	xAxisLabels[i]= " ";
        }

        currXValue += unit;

      } catch (SQLException e) {
        return "Error getting values from database:" + e.getMessage();
      }
    }

    if (paints == null) {
      return "Paint array not initialized.";
    }

    IAxisDataSeries dataSeries = new DataSeries(xAxisLabels, xAxisTitle, yAxisTitle, title);

    try {
      if (traffic)
        dataSeries.addIAxisPlotDataSet(
            new AxisChartDataSet(data, legendLabels, paints, ChartType.AREA, areaChartProperties));
      else if (trafficSrc || trafficDst)
        dataSeries.addIAxisPlotDataSet(
            new AxisChartDataSet(
                data, legendLabels, paints, ChartType.AREA_STACKED, areaChartProperties));
    } catch (ChartDataException e) {
      output += e.getMessage();
      throw new ChartDataException("Error generating pie.");
    }

    AxisChart axisChart =
        new AxisChart(dataSeries, chartProperties, axisProperties, legendProperties, width, height);

    request.getSession().getServletContext().setAttribute("chart", axisChart);

    return output;
  }