Example usage for org.jfree.chart ChartUtilities saveChartAsPNG

List of usage examples for org.jfree.chart ChartUtilities saveChartAsPNG

Introduction

In this page you can find the example usage for org.jfree.chart ChartUtilities saveChartAsPNG.

Prototype

public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height) throws IOException 

Source Link

Document

Saves a chart to the specified file in PNG format.

Usage

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.SuccessFailureCountByHostingServer.java

/**
 * {@inheritDoc}//www.j a  v a 2 s. co  m
 */
@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {
    if (!UserIdentityUtil.hasGlobalAdministratorRole(currentuser, "SUCCESS_FAILURE_COUNT_BY_HOSTING_SERVER",
            classification, ctx)) {
        data.append("<h2>Access for " + GetDisplayName() + " was denied for non-global admin users</h2>");
    }

    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;
        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append(
                "This represents Web Application Server utilization (host) by success and failure rates.<br />");
        data.append(
                "<table class=\"table table-hover\"><tr><th>Host</th><th>Successes</th><th>Failures</th></tr>");
        List<String> dcs = new ArrayList<String>();
        try {

            cmd = con.prepareStatement("select hostingsource from RawData group by hostingsource;");
            rs = cmd.executeQuery();
            while (rs.next()) {
                dcs.add(rs.getString(1));
            }
        } catch (Exception ex) {
            log.log(Level.WARN, null, ex);
        } finally {
            DBUtils.safeClose(rs);
            DBUtils.safeClose(cmd);
        }
        try {
            //          itemcount = dcs.size();
            for (int i = 0; i < dcs.size(); i++) {
                data.append("<tr><td>").append(Utility.encodeHTML(dcs.get(i))).append("</td>");
                int success = 0;
                int failures = 0;
                try {
                    cmd = con.prepareStatement(
                            "select count(*) from RawData where hostingsource=? and success=false and UTCdatetime > ? and UTCdatetime < ?;");
                    cmd.setString(1, dcs.get(i));
                    cmd.setLong(2, range.getStart().getTimeInMillis());
                    cmd.setLong(3, range.getEnd().getTimeInMillis());
                    rs = cmd.executeQuery();
                    try {
                        if (rs.next()) {
                            failures = rs.getInt(1);
                        }
                    } catch (Exception ex) {
                        log.log(Level.DEBUG, null, ex);
                    }
                } catch (Exception ex) {
                    log.log(Level.WARN, null, ex);
                } finally {
                    DBUtils.safeClose(rs);
                    DBUtils.safeClose(cmd);
                }

                try {
                    cmd = con.prepareStatement(
                            "select count(*) from RawData where hostingsource=? and success=true and UTCdatetime > ? and UTCdatetime < ?;");
                    cmd.setString(1, dcs.get(i));
                    cmd.setLong(2, range.getStart().getTimeInMillis());
                    cmd.setLong(3, range.getEnd().getTimeInMillis());
                    rs = cmd.executeQuery();
                    try {
                        if (rs.next()) {
                            success = rs.getInt(1);
                        }
                    } catch (Exception ex) {
                        log.log(Level.DEBUG, null, ex);
                    }
                } catch (Exception ex) {
                    log.log(Level.WARN, null, ex);
                } finally {
                    DBUtils.safeClose(rs);
                    DBUtils.safeClose(cmd);
                }
                data.append("<td>").append(success + "").append("</td>");
                data.append("<td>").append(failures + "").append("</td></tr>");
                if (success > 0) {
                    set.addValue(success, dcs.get(i) + " Success", dcs.get(i) + " Success");
                }
                if (failures > 0) {
                    set.addValue(failures, dcs.get(i) + " Failures", dcs.get(i) + " Failures");
                }

            }

        } catch (Exception ex) {
            log.log(Level.ERROR, "Error generating chart information.", ex);
        }
        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Hosting Servers", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, pixelHeightCalc(set.getRowCount()));
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }

        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.TotalMessageSizesByService.java

/**
 * {@inheritDoc}//from  w w  w . j  av  a 2  s.  c om
 */
@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {

    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;
        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append(
                "This represents the total transmitted message size, request + response, for each service over the given period of time.<br />");
        data.append(
                "<table class=\"table table-hover\"><tr><th>URL</th><th>Total Message Size (bytes)</th></tr>");
        for (int i = 0; i < urls.size(); i++) {
            if (!isPolicyTypeOf(urls.get(i), PolicyType.TRANSACTIONAL)) {
                continue;
            }
            //https://github.com/mil-oss/fgsms/issues/112
            if (!UserIdentityUtil.hasReadAccess(currentuser, "getReport", urls.get(i), classification, ctx)) {
                continue;
            }
            String url = Utility.encodeHTML(BaseReportGenerator.getPolicyDisplayName(urls.get(i)));
            long count = 0;
            try {
                cmd = con
                        .prepareStatement("select sum(requestsize + responsesize) from rawdata where uri=? and "
                                + "(UTCdatetime > ?) and (UTCdatetime < ?) and (requestsize>=0) and (responsesize>=0);");
                cmd.setString(1, urls.get(i));
                cmd.setLong(2, range.getStart().getTimeInMillis());
                cmd.setLong(3, range.getEnd().getTimeInMillis());
                rs = cmd.executeQuery();

                try {
                    if (rs.next()) {
                        count = rs.getLong(1);
                    }
                } catch (Exception ex) {
                    log.log(Level.DEBUG, null, ex);
                }
            } catch (Exception ex) {
                log.log(Level.WARN, null, ex);
            } finally {
                DBUtils.safeClose(rs);
                DBUtils.safeClose(cmd);
            }

            count = 0;
            try {
                cmd = con
                        .prepareStatement("select avg(requestsize + responsesize) from rawdata where uri=? and "
                                + "(UTCdatetime > ?) and (UTCdatetime < ?) ;");
                cmd.setString(1, urls.get(i));
                cmd.setLong(2, range.getStart().getTimeInMillis());
                cmd.setLong(3, range.getEnd().getTimeInMillis());
                rs = cmd.executeQuery();

                try {
                    if (rs.next()) {
                        count = rs.getLong(1);
                    }
                } catch (Exception ex) {
                    // log.log(Level.WARN, " error querying database for average message size url:" + urls.get(i), ex);
                }
            } catch (Exception ex) {
                log.log(Level.WARN, null, ex);
            } finally {
                DBUtils.safeClose(rs);
                DBUtils.safeClose(cmd);
            }
            data.append("<tr><td>").append(url).append("</td><td>");
            if (count > 0) {
                data.append(count + " bytes");
                set.addValue(count, url, url);
            } else
                data.append("N/A");
            data.append("</td></tr>");
        }
        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Service URL", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, pixelHeightCalc(set.getRowCount()));
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }

        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.ThroughputByHostingServer.java

@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {

    if (!UserIdentityUtil.hasGlobalAdministratorRole(currentuser, "INVOCATIONS_BY_HOSTING_SERVER",
            classification, ctx)) {/*from   w  w w .  j  a  va 2 s . co  m*/
        data.write("<h2>Access for " + GetDisplayName() + " was denied for non-global admin users</h2>");
        return;
    }
    double d = range.getEnd().getTimeInMillis() - range.getStart().getTimeInMillis();
    double day = d / 86400000;
    double hours = d / 3600000;
    double min = d / 60000;
    double sec = d / 1000;
    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;
        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append(GetHtmlFormattedHelp() + "<br />");
        data.append(
                "<table  class=\"table table-hover\"><tr><th>Service Hostname</th><th>Invocations</th><th>Msg/Day</th><th>Msg/Hour</th><th>Msg/Min</th><th>Msg/Sec</th></tr>");

        // for (int i = 0; i < urls.size(); i++) {
        List<String> hosts = new ArrayList<String>();
        try {
            cmd = con.prepareStatement(
                    "select hostingsource from rawdata where (UTCdatetime > ?) and (UTCdatetime < ?) group by hostingsource ;");
            cmd.setLong(1, range.getStart().getTimeInMillis());
            cmd.setLong(2, range.getEnd().getTimeInMillis());
            rs = cmd.executeQuery();
            while (rs.next()) {
                String s = rs.getString(1);
                if (!Utility.stringIsNullOrEmpty(s)) {
                    s = s.trim();
                }
                if (!Utility.stringIsNullOrEmpty(s)) {
                    hosts.add(s);
                }
            }
        } catch (Exception ex) {
        } finally {
            DBUtils.safeClose(rs);
            DBUtils.safeClose(cmd);
        }

        for (int i = 0; i < hosts.size(); i++) {
            long count = 0;
            try {
                cmd = con.prepareStatement("select count(*) from RawData where hostingsource=? and "
                        + "(UTCdatetime > ?) and (UTCdatetime < ?) ;");
                cmd.setString(1, hosts.get(i));
                cmd.setLong(2, range.getStart().getTimeInMillis());
                cmd.setLong(3, range.getEnd().getTimeInMillis());
                rs = cmd.executeQuery();

                try {
                    if (rs.next()) {
                        count = rs.getLong(1);
                    }
                } catch (Exception ex) {
                    log.log(Level.DEBUG, null, ex);
                }
            } catch (Exception ex) {
                log.log(Level.ERROR, "Error opening or querying the database.", ex);
            } finally {
                DBUtils.safeClose(rs);
                DBUtils.safeClose(cmd);
            }
            data.append("<tr><td>").append(Utility.encodeHTML(hosts.get(i))).append("</td><td>");
            data.append(count + "");
            data.append("</td><td>").append(format.format((double) ((double) count / day))).append("</td><td>")
                    .append(format.format((double) ((double) count / hours))).append("</td><td>")
                    .append(format.format((double) ((double) count / min))).append("</td><td>")
                    .append(format.format((double) ((double) count / sec))).append("</td></tr>");
            if (count > 0) {
                set.addValue((double) ((double) count / day), hosts.get(i), hosts.get(i));
            }
        }

        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Service URL", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, pixelHeightCalc(hosts.size()));
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }

        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.AverageMessageSizeByServiceByMethod.java

/**
 * {@inheritDoc}/*from   w  ww  . jav  a  2  s. c o  m*/
 */
@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {
    int itemcount = 0;
    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;
        data.append("<hr /><h2>");
        data.append(GetDisplayName());
        data.append("</h2>");
        data.append(GetHtmlFormattedHelp() + "<br />");
        //add description
        data.append(
                "<table class=\"table table-hover\"><tr><th>URL</th><th>Action</th><th>Average Message Size (bytes)</th></tr>");
        itemcount = 0;
        for (int i = 0; i < urls.size(); i++) {
            if (!isPolicyTypeOf(urls.get(i), PolicyType.TRANSACTIONAL)) {
                continue;
            }
            //https://github.com/mil-oss/fgsms/issues/112
            if (!UserIdentityUtil.hasReadAccess(currentuser, "getReport", urls.get(i), classification, ctx)) {
                continue;
            }
            String url = Utility.encodeHTML(BaseReportGenerator.getPolicyDisplayName(urls.get(i)));
            try {
                List<String> actions = getSoapActions(urls.get(i), con);

                for (int k = 0; k < actions.size(); k++) {
                    itemcount++;
                    long count = -1;
                    try {
                        cmd = con.prepareStatement(
                                "select AVG(responseSize + requestSize) as messagesSize from RawData where URI=? and "
                                        + "(UTCdatetime > ?) and (UTCdatetime < ?) and soapaction=?");
                        cmd.setString(1, urls.get(i));
                        cmd.setLong(2, range.getStart().getTimeInMillis());
                        cmd.setLong(3, range.getEnd().getTimeInMillis());
                        cmd.setString(4, actions.get(k));

                        rs = cmd.executeQuery();

                        try {
                            if (rs.next()) {
                                count = rs.getLong(1);
                            }
                        } catch (Exception ex) {
                            log.log(Level.DEBUG,
                                    " error querying database for average message size url:" + urls.get(i), ex);
                        }
                    } catch (Exception ex) {
                        log.log(Level.ERROR,
                                "Error opening or querying the database." + this.getClass().getSimpleName(),
                                ex);
                    } finally {
                        DBUtils.safeClose(rs);
                        DBUtils.safeClose(cmd);
                    }

                    data.append("<tr><td>").append(url).append("</td><td>");
                    data.append(Utility.encodeHTML(actions.get(k)));
                    data.append("</td><td>");
                    if (count >= 0) {
                        data.append(count + " bytes");
                    } else {
                        data.append("N/A");
                    }
                    data.append("</td></tr>");
                    if (count >= 0) {
                        set.addValue(count, actions.get(k), url);
                    }

                }
            } catch (Exception ex) {
                data.append("0 bytes</td></tr>");
                log.log(Level.ERROR,
                        "Error opening or querying the database." + this.getClass().getSimpleName(), ex);
            }
        }
        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Service URL", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            int height = pixelHeightCalc(itemcount);
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, height);
        } catch (Exception ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }

        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.ThroughputByServiceByMethod.java

/**
 * {@inheritDoc}//  w w  w .j  a va  2  s  .c o  m
 */
@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {

    double d = range.getEnd().getTimeInMillis() - range.getStart().getTimeInMillis();
    double day = d / 86400000;
    double hours = d / 3600000;
    double min = d / 60000;
    double sec = d / 1000;

    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;

        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append(GetHtmlFormattedHelp() + "<br />");
        data.append(
                "<table class=\"table table-hover\"><tr><th>URL</th><th>Method</th><th>Invocations</th><th>Msg/Day</th><th>Msg/Hour</th><th>Msg/Min</th><th>Msg/Sec</th></tr>");
        for (int i = 0; i < urls.size(); i++) {
            if (!isPolicyTypeOf(urls.get(i), PolicyType.TRANSACTIONAL)) {
                continue;
            }
            //https://github.com/mil-oss/fgsms/issues/112
            if (!UserIdentityUtil.hasReadAccess(currentuser, "getReport", urls.get(i), classification, ctx)) {
                continue;
            }
            String url = Utility.encodeHTML(BaseReportGenerator.getPolicyDisplayName(urls.get(i)));
            try {
                List<String> actions = getSoapActions(urls.get(i), con);

                for (int k = 0; k < actions.size(); k++) {
                    long count = 0;

                    try {
                        cmd = con.prepareStatement(
                                "select count(*) from RawData where URI=? and soapaction=? and "
                                        + "(UTCdatetime > ?) and (UTCdatetime < ?) ;");
                        cmd.setString(1, urls.get(i));
                        cmd.setString(2, actions.get(k));
                        cmd.setLong(3, range.getStart().getTimeInMillis());
                        cmd.setLong(4, range.getEnd().getTimeInMillis());
                        rs = cmd.executeQuery();

                        try {
                            if (rs.next()) {
                                count = rs.getLong(1);
                            }
                        } catch (Exception ex) {
                            log.log(Level.DEBUG, null, ex);
                        }
                    } catch (Exception ex) {
                        log.log(Level.WARN, null, ex);
                    } finally {
                        DBUtils.safeClose(rs);
                        DBUtils.safeClose(cmd);
                    }

                    data.append("<tr><td>").append(url).append("</td><td>")
                            .append(Utility.encodeHTML(actions.get(k))).append("</td><td>");
                    data.append(count + "");
                    data.append("</td><td>").append(format.format((double) ((double) count / day)))
                            .append("</td><td>").append(format.format((double) ((double) count / hours)))
                            .append("</td><td>").append(format.format((double) ((double) count / min)))
                            .append("</td><td>").append(format.format((double) ((double) count / sec)))
                            .append("</td></tr>");
                    if (count > 0) {
                        set.addValue((double) ((double) count / day), actions.get(k), url);
                    }
                }

            } catch (Exception ex) {

                log.log(Level.ERROR, "Error opening or querying the database.", ex);
            }
        }
        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Service URL", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, pixelHeightCalc(set.getRowCount()));
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }

        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.InvocationsByConsumer.java

/**
 * {@inheritDoc}//ww w.  j av a 2 s.  c  om
 */
@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {
    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        PreparedStatement userQuery = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;
        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append("This represents service usage by consumer for the provided services.<br />");
        //add description
        data.append("<table class=\"table table-hover\"><tr><th>User</th><th>Invocations</th></tr>");

        ResultSet actionRS = null;
        Set<String> users = new HashSet<String>();
        try {

            userQuery = con.prepareStatement("select consumeridentity from rawdata group by consumeridentity");
            actionRS = userQuery.executeQuery();
            users.add("unspecified");
            while (actionRS.next()) {
                try {
                    String s = actionRS.getString(1);
                    s = s.trim();
                    if (!Utility.stringIsNullOrEmpty(s)) {
                        String[] items = s.split(";");
                        for (int x = 0; x < items.length; x++) {
                            if (!Utility.stringIsNullOrEmpty(items[x])) {
                                users.add(items[x].trim());
                            }
                        }
                    }

                } catch (Exception ex) {
                    log.log(Level.WARN, " error querying database", ex);
                }
            }
        } catch (Exception ex) {
            log.log(Level.WARN, " error querying database", ex);
        } finally {
            DBUtils.safeClose(actionRS);
            DBUtils.safeClose(userQuery);
        }

        try {
            Iterator<String> iterator = users.iterator();
            while (iterator.hasNext()) {
                String u = iterator.next();
                try {
                    if (u.equalsIgnoreCase("unspecified")) {
                        cmd = con.prepareStatement(
                                "select count(*)  from RawData where consumeridentity is null and "
                                        + "(UTCdatetime > ?) and (UTCdatetime < ?)");

                        cmd.setLong(1, range.getStart().getTimeInMillis());
                        cmd.setLong(2, range.getEnd().getTimeInMillis());
                    } else {
                        cmd = con.prepareStatement("select count(*)  from RawData where consumeridentity=? and "
                                + "(UTCdatetime > ?) and (UTCdatetime < ?)");
                        cmd.setString(1, u);
                        cmd.setLong(2, range.getStart().getTimeInMillis());
                        cmd.setLong(3, range.getEnd().getTimeInMillis());
                    }
                    rs = cmd.executeQuery();
                    long count = 0;
                    try {
                        if (rs.next()) {
                            count = rs.getLong(1);
                        }
                    } catch (Exception ex) {
                        log.log(Level.DEBUG, " error querying database", ex);
                    }

                    data.append("<tr><td>").append(Utility.encodeHTML(u)).append("</td><td>");
                    data.append(count + "").append("</td></tr>");
                    if (count > 0) {
                        set.addValue(count, u, u);
                    }
                } catch (Exception ex) {
                    log.log(Level.WARN, " error querying database", ex);
                } finally {
                    DBUtils.safeClose(rs);
                    DBUtils.safeClose(cmd);
                }
            }
        } catch (Exception ex) {
            data.append("0</td></tr>");
            log.log(Level.ERROR, "Error opening or querying the database.", ex);
        } finally {
            DBUtils.safeClose(cmd);

        }

        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Consumers", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, pixelHeightCalc(set.getRowCount()));
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }

        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:org.miloss.fgsms.services.rs.impl.reports.ws.SuccessFailureCountByServiceByMethod.java

/**
 * {@inheritDoc}/*w w w  .j  a  v  a 2 s .c  o m*/
 */
@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {

    Connection con = Utility.getPerformanceDBConnection();
    try {
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;

        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append("This represents the success and failure counts by Service by Method<br />");
        //add description
        data.append(
                "<table class=\"table table-hover\"><tr><th>URL</th><th>Method</th><th>Success</th><th>Failure</th></tr>");

        for (int i = 0; i < urls.size(); i++) {
            if (!isPolicyTypeOf(urls.get(i), PolicyType.TRANSACTIONAL)) {
                continue;
            }
            //https://github.com/mil-oss/fgsms/issues/112
            if (!UserIdentityUtil.hasReadAccess(currentuser, "getReport", urls.get(i), classification, ctx)) {
                continue;
            }
            String url = Utility.encodeHTML(BaseReportGenerator.getPolicyDisplayName(urls.get(i)));
            try {
                List<String> actions = getSoapActions(urls.get(i), con);

                for (int k = 0; k < actions.size(); k++) {

                    long success = 0;
                    long failures = 0;
                    try {
                        cmd = con.prepareStatement("select count(*)  from RawData where URI=? and "
                                + "(UTCdatetime > ?) and (UTCdatetime < ?) and soapaction=? and success=true;");
                        cmd.setString(1, urls.get(i));
                        cmd.setLong(2, range.getStart().getTimeInMillis());
                        cmd.setLong(3, range.getEnd().getTimeInMillis());
                        cmd.setString(4, actions.get(k));
                        rs = cmd.executeQuery();
                        try {
                            if (rs.next()) {
                                success = rs.getLong(1);
                            }
                        } catch (Exception ex) {
                            log.log(Level.DEBUG, null, ex);
                        }
                    } catch (Exception ex) {
                        log.log(Level.WARN, null, ex);
                    } finally {
                        DBUtils.safeClose(rs);
                        DBUtils.safeClose(cmd);
                    }
                    try {
                        cmd = con.prepareStatement("select count(*)  from RawData where URI=? and "
                                + "(UTCdatetime > ?) and (UTCdatetime < ?) and soapaction=? and success=false;");
                        cmd.setString(1, urls.get(i));
                        cmd.setLong(2, range.getStart().getTimeInMillis());
                        cmd.setLong(3, range.getEnd().getTimeInMillis());
                        cmd.setString(4, actions.get(k));

                        rs = cmd.executeQuery();

                        try {
                            if (rs.next()) {
                                failures = rs.getLong(1);
                            }
                        } catch (Exception ex) {
                            log.log(Level.DEBUG, null, ex);
                        }
                    } catch (Exception ex) {
                        log.log(Level.WARN, null, ex);
                    } finally {
                        DBUtils.safeClose(rs);
                        DBUtils.safeClose(cmd);
                    }
                    data.append("<tr><td>").append(url).append("</td><td>");
                    data.append(Utility.encodeHTML(actions.get(k))).append("</td><td>").append(success + "")
                            .append("</td><td>").append(failures + "").append("</td></tr>");

                    if (success > 0) {
                        set.addValue(success, url + " Success", url + " Success");
                    }
                    if (failures > 0) {
                        set.addValue(failures, url + " Failures", url + " Failures");
                    }
                }
            } catch (Exception ex) {
                data.append("0 bytes</td></tr>");
                log.log(Level.ERROR, "Error opening or querying the database.", ex);
            }
        }

        chart = org.jfree.chart.ChartFactory.createBarChart(GetDisplayName(), "Service URL", "", set,
                PlotOrientation.HORIZONTAL, true, false, false);
        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, pixelHeightCalc(set.getRowCount()));
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }
        data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
        files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:com.devoteam.srit.xmlloader.core.report.derived.DerivedCounter.java

protected void writeChartToFile(String path, JFreeChart chart) {
    try {/*from   w  ww. ja  v a  2s  .c  o m*/
        ChartUtilities.saveChartAsPNG(new File(path), chart, 800, 400);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.miloss.fgsms.services.rs.impl.reports.broker.ConsumersByQueueOrTopic.java

@Override
public void generateReport(OutputStreamWriter data, List<String> urls, String path, List<String> files,
        TimeRange range, String currentuser, SecurityWrapper classification, WebServiceContext ctx)
        throws IOException {

    Connection con = Utility.getPerformanceDBConnection();
    try {/*from ww w  . j  av a  2s . c  o m*/
        PreparedStatement cmd = null;
        ResultSet rs = null;
        DefaultCategoryDataset set = new DefaultCategoryDataset();
        JFreeChart chart = null;

        data.append("<hr /><h2>").append(GetDisplayName()).append("</h2>");
        data.append(
                "This represents the average number of consumers for all channels (topics/queues/etc) on a specific message broker.<br />");
        data.append(
                "<table class=\"table table-hover\"><tr><th>URI</th><th>Channel</th><th>Average Consumer Count</th></tr>");

        TimeSeriesCollection col = new TimeSeriesCollection();
        for (int i = 0; i < urls.size(); i++) {
            if (!isPolicyTypeOf(urls.get(i), PolicyType.STATISTICAL)) {
                continue;
            }
            //https://github.com/mil-oss/fgsms/issues/112
            if (!UserIdentityUtil.hasReadAccess(currentuser, "getReport", urls.get(i), classification, ctx)) {
                continue;
            }
            String url = Utility.encodeHTML(BaseReportGenerator.getPolicyDisplayName(urls.get(i)));
            try {
                data.append("<tr><td>").append(Utility.encodeHTML(urls.get(i))).append("</td>");
                double average = 0;
                try {
                    cmd = con.prepareStatement(
                            "select avg(activeconsumercount), host, canonicalname from brokerhistory where host=? and utcdatetime > ? and utcdatetime < ? group by canonicalname, host;");
                    cmd.setString(1, urls.get(i));
                    cmd.setLong(2, range.getStart().getTimeInMillis());
                    cmd.setLong(3, range.getEnd().getTimeInMillis());
                    rs = cmd.executeQuery();

                    if (rs.next()) {
                        average = rs.getDouble(1);

                    }
                } catch (Exception ex) {
                    log.log(Level.WARN, null, ex);
                } finally {
                    DBUtils.safeClose(rs);
                    DBUtils.safeClose(cmd);
                }

                data.append("<td>").append(average + "").append("</td>");

                try {
                    //ok now get the raw data....
                    cmd = con.prepareStatement(
                            "select utcdatetime,activeconsumercount, canonicalname from brokerhistory where host=? and utcdatetime > ? and utcdatetime < ?;");
                    cmd.setString(1, urls.get(i));
                    cmd.setLong(2, range.getStart().getTimeInMillis());
                    cmd.setLong(3, range.getEnd().getTimeInMillis());
                    rs = cmd.executeQuery();
                    TimeSeries ts = new TimeSeries(urls.get(i), Millisecond.class);
                    while (rs.next()) {

                        //set.addValue(rs.getLong(1), urls.get(i), rs.getString("canonicalname"));
                        GregorianCalendar gcal = new GregorianCalendar();
                        gcal.setTimeInMillis(rs.getLong(1));
                        Millisecond m = new Millisecond(gcal.getTime());

                        ts.addOrUpdate(m, rs.getLong(2));

                    }
                    col.addSeries(ts);
                } catch (Exception ex) {
                    log.log(Level.WARN, null, ex);
                } finally {
                    DBUtils.safeClose(rs);
                    DBUtils.safeClose(cmd);
                }

            } catch (Exception ex) {
                log.log(Level.ERROR, "Error opening or querying the database.", ex);
            }

        }
        chart = org.jfree.chart.ChartFactory.createTimeSeriesChart(GetDisplayName(), "Timestamp", "Count", col,
                true, false, false);

        data.append("</table>");
        try {
            ChartUtilities.saveChartAsPNG(new File(
                    path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png"), chart,
                    1500, 400);
            data.append("<img src=\"image_").append(this.getClass().getSimpleName()).append(".png\">");
            files.add(path + getFilePathDelimitor() + "image_" + this.getClass().getSimpleName() + ".png");
        } catch (IOException ex) {
            log.log(Level.ERROR, "Error saving chart image for request", ex);
        }
    } catch (Exception ex) {
        log.log(Level.ERROR, null, ex);
    } finally {
        DBUtils.safeClose(con);
    }
}

From source file:com.xpn.xwiki.plugin.charts.ChartingPlugin.java

private Chart generatePngChart(JFreeChart jfchart, ChartParams params, XWikiContext context)
        throws IOException, GenerateException {

    File file = getTempFile(params.hashCode(), "png");

    ChartUtilities.saveChartAsPNG(file, jfchart, params.getInteger(ChartParams.WIDTH).intValue(),
            params.getInteger(ChartParams.HEIGHT).intValue());

    String imageURL = context.getDoc().getAttachmentURL(file.getName(), "charting", context);
    String pageURL = imageURL;/*w ww  .jav  a  2s  . c  o  m*/
    return new ChartImpl(params, imageURL, pageURL);
}