Search in sources :

Example 1 with MetricsEndpointException

use of ddf.metrics.reporting.internal.MetricsEndpointException in project ddf by codice.

the class MetricsEndpoint method getMetricsData.

/**
     * Retrieve data for the specified metric over the given time range. The URL to access this
     * method is of the form http://<host>:<port>/<metricName>.<outputFormat> So the desired metric
     * filename is specified in the URL, e.g., catalogQueryCount.png, where the filename extension
     * defines the desired output format returned for the metric's data. Currently supported formats
     * are png, csv, xls, ppt, xml, and json.
     * <p>
     * Note that the time range can be specified as either a start and end date (in RFC3339 format,
     * i.e., YYYY-MM-DD'T'hh:mm:ssZ), or as an offset in seconds from the current time. These 2 time
     * range mechanisms cannot be combined, e.g., you cannot specify an end date and an offset to be
     * applied from that end date.
     * <p>
     * By default, the metric's name will be used for the y-axis label on the PNG graph, and the
     * metric name and time range will be used for the graph's title. Both of these can be
     * optionally specified with the yAxisLabel and title parameters. These 2 parameters do not
     * apply for the other formats.
     *
     * @param metricName   Name of the metric being graphed, e.g., queryCount
     * @param outputFormat output format of the metric, e.g. csv
     * @param startDate    Specifies the start of the time range of the search on the metric's data (RFC-3339
     *                     - Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Cannot be used with dateOffset
     *                     parameter.
     * @param endDate      Specifies the end of the time range of the search on the metric's data (RFC-3339 -
     *                     Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Cannot be used with dateOffset
     *                     parameter.
     * @param dateOffset   Specifies an offset, backwards from the current time, to search on the modified
     *                     time field for entries. Defined in seconds. Cannot be used with startDate and
     *                     endDate parameters.
     * @param yAxisLabel   (optional) the label to apply to the graph's y-axis
     * @param title        (optional) the title to be applied to the graph
     * @param uriInfo
     * @return Response containing the metric's data in the specified outputFormat
     * @throws MetricsEndpointException
     */
@GET
@Path("/{metricName}.{outputFormat}")
public Response getMetricsData(@PathParam("metricName") String metricName, @PathParam("outputFormat") String outputFormat, @QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate, @QueryParam("dateOffset") String dateOffset, @QueryParam("yAxisLabel") String yAxisLabel, @QueryParam("title") String title, @Context UriInfo uriInfo) throws MetricsEndpointException {
    LOGGER.trace("ENTERING: getMetricsData  -  metricName = {},    outputFormat = {}", metricName, outputFormat);
    LOGGER.trace("request url: {}", uriInfo.getRequestUri());
    LOGGER.trace("startDate = {},     endDate = {}", startDate, endDate);
    LOGGER.trace("dateOffset = {}", dateOffset);
    Response response = null;
    // Client must specify *either* startDate and/or endDate *OR* dateOffset
    if (!StringUtils.isBlank(dateOffset) && (!StringUtils.isBlank(startDate) || !StringUtils.isBlank(endDate))) {
        throw new MetricsEndpointException("Cannot specify dateOffset and startDate or endDate, must specify either dateOffset only or startDate and/or endDate", Response.Status.BAD_REQUEST);
    }
    long endTime;
    if (!StringUtils.isBlank(endDate)) {
        endTime = parseDate(endDate);
        LOGGER.trace("Parsed endTime = {}", endTime);
    } else {
        // Default end time for metrics graphing to now (in seconds)
        Calendar now = getCalendar();
        endTime = now.getTimeInMillis() / MILLISECONDS_PER_SECOND;
        LOGGER.trace("Defaulted endTime to {}", endTime);
        // Set endDate to new calculated endTime (so that endDate is displayed properly
        // in graph's title)
        endDate = dateFormatter.format(now.getTime());
    }
    long startTime;
    if (!StringUtils.isBlank(startDate)) {
        startTime = parseDate(startDate);
        LOGGER.trace("Parsed startTime = {}", startTime);
    } else if (!StringUtils.isBlank(dateOffset)) {
        startTime = endTime - Long.parseLong(dateOffset);
        LOGGER.trace("Offset-computed startTime = {}", startTime);
        // Set startDate to new calculated startTime (so that startDate is displayed properly
        // in graph's title)
        Calendar cal = getCalendar();
        cal.setTimeInMillis(startTime * MILLISECONDS_PER_SECOND);
        startDate = dateFormatter.format(cal.getTime());
    } else {
        // Default start time for metrics graphing to end time last 24 hours (in seconds)
        startTime = endTime - ONE_DAY_IN_SECONDS;
        LOGGER.trace("Defaulted startTime to {}", startTime);
        // Set startDate to new calculated startTime (so that startDate is displayed properly
        // in graph's title)
        Calendar cal = getCalendar();
        cal.setTimeInMillis(startTime * MILLISECONDS_PER_SECOND);
        startDate = dateFormatter.format(cal.getTime());
    }
    LOGGER.trace("startDate = {},   endDate = {}", startDate, endDate);
    if (StringUtils.isBlank(yAxisLabel)) {
        yAxisLabel = RrdMetricsRetriever.convertCamelCase(metricName);
    }
    if (StringUtils.isBlank(title)) {
        title = RrdMetricsRetriever.convertCamelCase(metricName) + " for " + startDate + " to " + endDate;
    }
    // Convert metric filename to rrd filename (because RRD file required by MetricRetriever to
    // generate graph)
    String rrdFilename = metricsDir + metricName + RRD_FILE_EXTENSION;
    if (outputFormat.equalsIgnoreCase(PNG_FORMAT)) {
        LOGGER.trace("Retrieving PNG-formatted data for metric {}", metricName);
        try {
            byte[] metricsGraphBytes = metricsRetriever.createGraph(metricName, rrdFilename, startTime, endTime, yAxisLabel, title);
            ByteArrayInputStream bis = new ByteArrayInputStream(metricsGraphBytes);
            response = Response.ok(bis, PNG_MIME_TYPE).build();
        } catch (IOException | MetricsGraphException e) {
            LOGGER.info("Could not create graph for metric {}", metricName);
            throw new MetricsEndpointException("Cannot create metrics graph for specified metric.", Response.Status.BAD_REQUEST);
        }
    } else if (outputFormat.equalsIgnoreCase("csv")) {
        try {
            String csv = metricsRetriever.createCsvData(rrdFilename, startTime, endTime);
            ResponseBuilder responseBuilder = Response.ok(csv);
            responseBuilder.type("text/csv");
            response = responseBuilder.build();
        } catch (IOException | MetricsGraphException e) {
            LOGGER.info("Could not create CSV data for metric {}", metricName);
            throw new MetricsEndpointException("Cannot create CSV data for specified metric.", Response.Status.BAD_REQUEST);
        }
    } else if (outputFormat.equalsIgnoreCase("xls")) {
        LOGGER.trace("Retrieving XLS-formatted data for metric {}", metricName);
        try {
            OutputStream os = metricsRetriever.createXlsData(metricName, rrdFilename, startTime, endTime);
            InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
            ResponseBuilder responseBuilder = Response.ok(is);
            responseBuilder.type("application/vnd.ms-excel");
            response = responseBuilder.build();
        } catch (IOException | MetricsGraphException e) {
            LOGGER.info("Could not create XLS data for metric {}", metricName);
            throw new MetricsEndpointException("Cannot create XLS data for specified metric.", Response.Status.BAD_REQUEST);
        }
    } else if (outputFormat.equalsIgnoreCase("ppt")) {
        LOGGER.trace("Retrieving PPT-formatted data for metric {}", metricName);
        try {
            OutputStream os = metricsRetriever.createPptData(metricName, rrdFilename, startTime, endTime);
            InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
            ResponseBuilder responseBuilder = Response.ok(is);
            responseBuilder.type("application/vnd.ms-powerpoint");
            response = responseBuilder.build();
        } catch (IOException | MetricsGraphException e) {
            LOGGER.info("Could not create PPT data for metric {}", metricName);
            throw new MetricsEndpointException("Cannot create PPT data for metric for specified metric.", Response.Status.BAD_REQUEST);
        }
    } else if (outputFormat.equalsIgnoreCase("xml")) {
        LOGGER.trace("Retrieving XML-formatted data for metric {}", metricName);
        try {
            String xmlData = metricsRetriever.createXmlData(metricName, rrdFilename, startTime, endTime);
            ResponseBuilder responseBuilder = Response.ok(xmlData);
            responseBuilder.type("text/xml");
            response = responseBuilder.build();
        } catch (IOException | MetricsGraphException e) {
            LOGGER.info("Could not create XML data for metric {}", metricName);
            throw new MetricsEndpointException("Cannot create XML data for specified metric.", Response.Status.BAD_REQUEST);
        }
    } else if (outputFormat.equalsIgnoreCase("json")) {
        LOGGER.trace("Retrieving JSON-formatted data for metric {}", metricName);
        try {
            String jsonData = metricsRetriever.createJsonData(metricName, rrdFilename, startTime, endTime);
            ResponseBuilder responseBuilder = Response.ok(jsonData);
            responseBuilder.type("application/json");
            response = responseBuilder.build();
        } catch (IOException | MetricsGraphException e) {
            LOGGER.info("Could not create JSON data for metric {}", metricName);
            throw new MetricsEndpointException("Cannot create JSON data for specified metric.", Response.Status.BAD_REQUEST);
        }
    }
    LOGGER.trace("EXITING: getMetricsData");
    return response;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MetricsEndpointException(ddf.metrics.reporting.internal.MetricsEndpointException) Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) MetricsGraphException(ddf.metrics.reporting.internal.MetricsGraphException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with MetricsEndpointException

use of ddf.metrics.reporting.internal.MetricsEndpointException in project ddf by codice.

the class MetricsEndpoint method getMetricsReport.

/**
     * Retrieve data for the all metrics over the given time range. The URL to access this method is
     * of the form http://<host>:<port>/report.<outputFormat> The filename extension defines the
     * desired output format returned for the report's data. Currently supported formats are xls and
     * ppt.
     * <p>
     * The XLS-formatted report will be one spreadsheet (workbook) with a worksheet per metric. The
     * PPT-formatted report will be one PowerPoint slide deck with a slide per metric. Each slide
     * will contain the metric's PNG graph.
     * <p>
     * If a summary interval is requested, the XSL report will instead contain a single table, with
     * the summarized values for each interval and metric. Cannot be used with PPT format.
     * <p>
     * Note that the time range can be specified as either a start and end date (in RFC3339 format,
     * i.e., YYYY-MM-DD'T'hh:mm:ssZ), or as an offset in seconds from the current time. These 2 time
     * range mechanisms cannot be combined, e.g., you cannot specify an end date and an offset to be
     * applied from that end date.
     * <p>
     * By default, the metric's name will be used for the y-axis label, and the metric name and time
     * range will be used for the graph's title for the report in PPT format.
     *
     * @param startDate       Specifies the start of the time range of the search on the metric's data (RFC-3339
     *                        - Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Cannot be used with dateOffset
     *                        parameter.
     * @param endDate         Specifies the end of the time range of the search on the metric's data (RFC-3339 -
     *                        Date and Time format, i.e. YYYY-MM-DDTHH:mm:ssZ). Cannot be used with dateOffset
     *                        parameter.
     * @param dateOffset      Specifies an offset, backwards from the current time, to search on the modified
     *                        time field for entries. Defined in seconds. Cannot be used with startDate or
     *                        endDate parameters.
     * @param summaryInterval One of {@link ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever.SUMMARY_INTERVALS}
     * @param uriInfo
     * @return Response containing the report as a stream in either XLS or PPT format
     * @throws MetricsEndpointException
     */
@GET
@Path("/report.{outputFormat}")
public Response getMetricsReport(@PathParam("outputFormat") String outputFormat, @QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate, @QueryParam("dateOffset") String dateOffset, @QueryParam("summaryInterval") String summaryInterval, @Context UriInfo uriInfo) throws MetricsEndpointException {
    LOGGER.debug("ENTERING: getMetricsReport  -  outputFormat = {}", outputFormat);
    LOGGER.debug("request url: {}", uriInfo.getRequestUri());
    LOGGER.debug("startDate = {},     endDate = {}", startDate, endDate);
    LOGGER.debug("dateOffset = {}", dateOffset);
    Response response = null;
    // Client must specify *either* startDate and/or endDate *OR* dateOffset
    if (!StringUtils.isBlank(dateOffset) && (!StringUtils.isBlank(startDate) || !StringUtils.isBlank(endDate))) {
        throw new MetricsEndpointException("Cannot specify dateOffset and startDate or endDate, must specify either dateOffset only or startDate and/or endDate", Response.Status.BAD_REQUEST);
    }
    long endTime;
    if (!StringUtils.isBlank(endDate)) {
        endTime = parseDate(endDate);
        LOGGER.debug("Parsed endTime = {}", endTime);
    } else {
        // Default end time for metrics graphing to now (in seconds)
        Calendar now = getCalendar();
        endTime = now.getTimeInMillis() / MILLISECONDS_PER_SECOND;
        LOGGER.debug("Defaulted endTime to {}", endTime);
        // Set endDate to new calculated endTime (so that endDate is displayed properly
        // in graph's title)
        endDate = dateFormatter.format(now.getTime());
    }
    long startTime;
    if (!StringUtils.isBlank(startDate)) {
        startTime = parseDate(startDate);
        LOGGER.debug("Parsed startTime = {}", startTime);
    } else if (!StringUtils.isBlank(dateOffset)) {
        startTime = endTime - Long.parseLong(dateOffset);
        LOGGER.debug("Offset-computed startTime = {}", startTime);
        // Set startDate to new calculated startTime (so that startDate is displayed properly
        // in graph's title)
        Calendar cal = getCalendar();
        cal.setTimeInMillis(startTime * MILLISECONDS_PER_SECOND);
        startDate = dateFormatter.format(cal.getTime());
    } else {
        // Default start time for metrics graphing to end time last 24 hours (in seconds)
        startTime = endTime - ONE_DAY_IN_SECONDS;
        LOGGER.debug("Defaulted startTime to {}", startTime);
        // Set startDate to new calculated startTime (so that startDate is displayed properly
        // in graph's title)
        Calendar cal = getCalendar();
        cal.setTimeInMillis(startTime * MILLISECONDS_PER_SECOND);
        startDate = dateFormatter.format(cal.getTime());
    }
    LOGGER.debug("startDate = {},   endDate = {}", startDate, endDate);
    List<String> metricNames = getMetricsNames();
    // Generated name for metrics file (<DDF Sitename>_<Startdate>_<EndDate>.outputFormat)
    String dispositionString = "attachment; filename=" + SystemInfo.getSiteName() + "_" + startDate.substring(0, 10) + "_" + endDate.substring(0, 10) + "." + outputFormat;
    try {
        if (outputFormat.equalsIgnoreCase("xls")) {
            OutputStream os = metricsRetriever.createXlsReport(metricNames, metricsDir, startTime, endTime, summaryInterval);
            InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
            ResponseBuilder responseBuilder = Response.ok(is);
            responseBuilder.type("application/vnd.ms-excel");
            responseBuilder.header("Content-Disposition", dispositionString);
            response = responseBuilder.build();
        } else if (outputFormat.equalsIgnoreCase("ppt")) {
            if (StringUtils.isNotEmpty(summaryInterval)) {
                throw new MetricsEndpointException("Summary interval not allowed for ppt format", Response.Status.BAD_REQUEST);
            }
            OutputStream os = metricsRetriever.createPptReport(metricNames, metricsDir, startTime, endTime);
            InputStream is = new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray());
            ResponseBuilder responseBuilder = Response.ok(is);
            responseBuilder.type("application/vnd.ms-powerpoint");
            responseBuilder.header("Content-Disposition", dispositionString);
            response = responseBuilder.build();
        }
    } catch (IOException | MetricsGraphException e) {
        LOGGER.debug("Could not create {} report", outputFormat, e);
        throw new MetricsEndpointException("Could not create report in specified output format.", Response.Status.BAD_REQUEST);
    }
    LOGGER.debug("EXITING: getMetricsReport");
    return response;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MetricsEndpointException(ddf.metrics.reporting.internal.MetricsEndpointException) Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) MetricsGraphException(ddf.metrics.reporting.internal.MetricsGraphException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with MetricsEndpointException

use of ddf.metrics.reporting.internal.MetricsEndpointException in project ddf by codice.

the class MetricsEndpointTest method testSummaryFormat.

@Test
public void testSummaryFormat() throws URISyntaxException {
    boolean pass = false;
    try {
        MetricsEndpoint endpoint = getEndpoint();
        endpoint.setMetricsDir(TEST_DIR);
        Response response = endpoint.getMetricsReport("ppt", null, null, "3600", "minuteā€°", createUriInfo());
    } catch (MetricsEndpointException e) {
        pass = true;
    }
    assertThat("Format not rejected", pass, is(true));
}
Also used : Response(javax.ws.rs.core.Response) MetricsEndpointException(ddf.metrics.reporting.internal.MetricsEndpointException) Test(org.junit.Test)

Example 4 with MetricsEndpointException

use of ddf.metrics.reporting.internal.MetricsEndpointException in project ddf by codice.

the class MetricsEndpointTest method testGetMetricsGraphWithMetricsGraphException.

// NOTE: "expected" annotation does not work when test case extends XMLTestCase,
// hence the usage of the try/catch/fail approach for the expected exception
@Test
public // (expected = MetricsEndpointException.class)
void testGetMetricsGraphWithMetricsGraphException() throws Exception {
    UriInfo uriInfo = createUriInfo();
    RrdMetricsRetriever metricsRetriever = mock(RrdMetricsRetriever.class);
    when(metricsRetriever.createGraph(anyString(), anyString(), anyLong(), anyLong(), anyString(), anyString())).thenThrow(MetricsGraphException.class);
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    endpoint.setMetricsRetriever(metricsRetriever);
    try {
        endpoint.getMetricsData("uptime", "png", "2013-03-25T06:00:00-07:00", "2013-03-25T07:10:00-07:00", null, "my label", "my title", uriInfo);
        fail();
    } catch (MetricsEndpointException e) {
    }
}
Also used : RrdMetricsRetriever(ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever) MetricsEndpointException(ddf.metrics.reporting.internal.MetricsEndpointException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 5 with MetricsEndpointException

use of ddf.metrics.reporting.internal.MetricsEndpointException in project ddf by codice.

the class MetricsEndpointTest method verifyException.

private void verifyException(String format, Class exceptionClass) throws Exception {
    UriInfo uriInfo = createUriInfo();
    RrdMetricsRetriever metricsRetriever = mock(RrdMetricsRetriever.class);
    when(metricsRetriever.createJsonData(anyString(), anyString(), anyLong(), anyLong())).thenThrow(exceptionClass);
    when(metricsRetriever.createCsvData(anyString(), anyLong(), anyLong())).thenThrow(exceptionClass);
    when(metricsRetriever.createXlsData(anyString(), anyString(), anyLong(), anyLong())).thenThrow(exceptionClass);
    when(metricsRetriever.createXmlData(anyString(), anyString(), anyLong(), anyLong())).thenThrow(exceptionClass);
    // Get the metrics data from the endpoint
    MetricsEndpoint endpoint = getEndpoint();
    endpoint.setMetricsDir(TEST_DIR);
    endpoint.setMetricsRetriever(metricsRetriever);
    try {
        Response response = endpoint.getMetricsData("uptime", format, null, null, "900", "my label", "my title", uriInfo);
        fail();
    } catch (MetricsEndpointException e) {
    }
}
Also used : Response(javax.ws.rs.core.Response) RrdMetricsRetriever(ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever) MetricsEndpointException(ddf.metrics.reporting.internal.MetricsEndpointException) UriInfo(javax.ws.rs.core.UriInfo)

Aggregations

MetricsEndpointException (ddf.metrics.reporting.internal.MetricsEndpointException)7 Response (javax.ws.rs.core.Response)4 UriInfo (javax.ws.rs.core.UriInfo)4 Test (org.junit.Test)4 RrdMetricsRetriever (ddf.metrics.reporting.internal.rrd4j.RrdMetricsRetriever)3 MetricsGraphException (ddf.metrics.reporting.internal.MetricsGraphException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Calendar (java.util.Calendar)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2