Search in sources :

Example 1 with TimeSpec

use of org.jrobin.core.timespec.TimeSpec in project opennms by OpenNMS.

the class GraphResultsController method handleRequestInternal.

/** {@inheritDoc} */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String[] requiredParameters = new String[] { "resourceId", "reports" };
    for (String requiredParameter : requiredParameters) {
        if (request.getParameter(requiredParameter) == null) {
            throw new MissingParameterException(requiredParameter, requiredParameters);
        }
    }
    ResourceId[] resourceIds = Arrays.stream(request.getParameterValues("resourceId")).map(ResourceId::fromString).toArray(ResourceId[]::new);
    String[] reports = request.getParameterValues("reports");
    // see if the start and end time were explicitly set as params
    String start = request.getParameter("start");
    String end = request.getParameter("end");
    String relativeTime = request.getParameter("relativetime");
    final String startMonth = request.getParameter("startMonth");
    final String startDate = request.getParameter("startDate");
    final String startYear = request.getParameter("startYear");
    final String startHour = request.getParameter("startHour");
    final String endMonth = request.getParameter("endMonth");
    final String endDate = request.getParameter("endDate");
    final String endYear = request.getParameter("endYear");
    final String endHour = request.getParameter("endHour");
    long startLong = 0;
    long endLong = 0;
    if (start != null || end != null) {
        String[] ourRequiredParameters = new String[] { "start", "end" };
        if (start == null) {
            throw new MissingParameterException("start", ourRequiredParameters);
        }
        if (end == null) {
            throw new MissingParameterException("end", ourRequiredParameters);
        }
        //The following is very similar to RrdGraphController.parseTimes, but modified for the local context a bit
        // There's merging possibilities, but I don't know how (common parent class seems wrong; service bean for a single
        // method isn't much better.  Ideas?
        //Try a simple 'long' parsing.  If either fails, do a full parse.  If one is a straight 'long' but the other isn't
        // that's fine, the TimeParser code will handle it fine (as long as we convert milliseconds to seconds)
        // Indeed, we *have* to use TimeParse for both to ensure any relative references (using "start" or "end") work correctly. 
        // NB: can't do a "safe" parsing using the WebSecurityUtils; if we did, it would filter out all the possible rrdfetch 
        // format text and always work :)
        boolean startIsInteger = false;
        boolean endIsInteger = false;
        // is expected for epoch times by TimeParser
        try {
            startLong = Long.valueOf(start);
            startIsInteger = true;
            start = "" + (startLong / 1000);
        } catch (NumberFormatException e) {
        }
        try {
            endLong = Long.valueOf(end);
            endIsInteger = true;
            end = "" + (endLong / 1000);
        } catch (NumberFormatException e) {
        }
        if (!endIsInteger || !startIsInteger) {
            //One or both of start/end aren't integers, so we need to do full parsing using TimeParser
            TimeParser startParser = new TimeParser(start);
            TimeParser endParser = new TimeParser(end);
            try {
                TimeSpec specStart = startParser.parse();
                TimeSpec specEnd = endParser.parse();
                long[] results = TimeSpec.getTimestamps(specStart, specEnd);
                //Multiply by 1000.  TimeSpec returns timestamps in Seconds, not Milliseconds.  
                startLong = results[0] * 1000;
                endLong = results[1] * 1000;
            } catch (RrdException e1) {
                throw new IllegalArgumentException("Could not parse start '" + start + "' and end '" + end + "' as valid time specifications", e1);
            }
        }
    } else if (startMonth != null || startDate != null || startYear != null || startHour != null || endMonth != null || endDate != null || endYear != null || endHour != null) {
        String[] ourRequiredParameters = new String[] { "startMonth", "startDate", "startYear", "startHour", "endMonth", "endDate", "endYear", "endHour" };
        for (String requiredParameter : ourRequiredParameters) {
            if (request.getParameter(requiredParameter) == null) {
                throw new MissingParameterException(requiredParameter, ourRequiredParameters);
            }
        }
        Calendar startCal = Calendar.getInstance();
        startCal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(startMonth));
        startCal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(startDate));
        startCal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(startYear));
        startCal.set(Calendar.HOUR_OF_DAY, WebSecurityUtils.safeParseInt(startHour));
        startCal.set(Calendar.MINUTE, 0);
        startCal.set(Calendar.SECOND, 0);
        startCal.set(Calendar.MILLISECOND, 0);
        Calendar endCal = Calendar.getInstance();
        endCal.set(Calendar.MONTH, WebSecurityUtils.safeParseInt(endMonth));
        endCal.set(Calendar.DATE, WebSecurityUtils.safeParseInt(endDate));
        endCal.set(Calendar.YEAR, WebSecurityUtils.safeParseInt(endYear));
        endCal.set(Calendar.HOUR_OF_DAY, WebSecurityUtils.safeParseInt(endHour));
        endCal.set(Calendar.MINUTE, 0);
        endCal.set(Calendar.SECOND, 0);
        endCal.set(Calendar.MILLISECOND, 0);
        startLong = startCal.getTime().getTime();
        endLong = endCal.getTime().getTime();
    } else {
        if (relativeTime == null) {
            relativeTime = s_periods[0].getId();
        }
        RelativeTimePeriod period = RelativeTimePeriod.getPeriodByIdOrDefault(s_periods, relativeTime, s_periods[0]);
        long[] times = period.getStartAndEndTimes();
        startLong = times[0];
        endLong = times[1];
    }
    // The 'matching' parameter is going to work only for one resource.
    String matching = request.getParameter("matching");
    if (matching != null) {
        reports = getSuggestedReports(resourceIds[0], matching);
    }
    ModelAndView modelAndView = null;
    try {
        GraphResults model = m_graphResultsService.findResults(resourceIds, reports, startLong, endLong, relativeTime);
        modelAndView = new ModelAndView("/graph/results", "results", model);
    } catch (Exception e) {
        LOG.warn("Can't get graph results", e);
        modelAndView = new ModelAndView("/graph/results-error");
    }
    modelAndView.addObject("loggedIn", request.getRemoteUser() != null);
    return modelAndView;
}
Also used : Calendar(java.util.Calendar) ModelAndView(org.springframework.web.servlet.ModelAndView) MissingParameterException(org.opennms.web.servlet.MissingParameterException) RrdException(org.jrobin.core.RrdException) TimeSpec(org.jrobin.core.timespec.TimeSpec) RelativeTimePeriod(org.opennms.web.svclayer.model.RelativeTimePeriod) ResourceId(org.opennms.netmgt.model.ResourceId) GraphResults(org.opennms.web.svclayer.model.GraphResults) MissingParameterException(org.opennms.web.servlet.MissingParameterException) RrdException(org.jrobin.core.RrdException) TimeParser(org.jrobin.core.timespec.TimeParser)

Example 2 with TimeSpec

use of org.jrobin.core.timespec.TimeSpec in project opennms by OpenNMS.

the class RrdGraphController method parseTimes.

public long[] parseTimes(HttpServletRequest request) {
    String startTime = request.getParameter("start");
    String endTime = request.getParameter("end");
    if (startTime == null || "".equals(startTime)) {
        startTime = "now - 1day";
    }
    if (endTime == null || "".equals(endTime)) {
        endTime = "now";
    }
    boolean startIsInteger = false;
    boolean endIsInteger = false;
    long start = 0, end = 0;
    try {
        start = Long.valueOf(startTime);
        startIsInteger = true;
    } catch (NumberFormatException e) {
    }
    try {
        end = Long.valueOf(endTime);
        endIsInteger = true;
    } catch (NumberFormatException e) {
    }
    if (endIsInteger && startIsInteger) {
        return new long[] { start, end };
    }
    // is expected for epoch times by TimeParser
    if (startIsInteger) {
        //Convert to seconds
        startTime = "" + (start / 1000);
    }
    if (endIsInteger) {
        endTime = "" + (end / 1000);
    }
    TimeParser startParser = new TimeParser(startTime);
    TimeParser endParser = new TimeParser(endTime);
    try {
        TimeSpec specStart = startParser.parse();
        TimeSpec specEnd = endParser.parse();
        long[] results = TimeSpec.getTimestamps(specStart, specEnd);
        //Multiply by 1000.  TimeSpec returns timestamps in Seconds, not Milliseconds.  Gah.  
        results[0] = results[0] * 1000;
        results[1] = results[1] * 1000;
        return results;
    } catch (RrdException e) {
        throw new IllegalArgumentException("Could not parse start '" + startTime + "' and end '" + endTime + "' as valid time specifications", e);
    }
}
Also used : RrdException(org.jrobin.core.RrdException) TimeParser(org.jrobin.core.timespec.TimeParser) TimeSpec(org.jrobin.core.timespec.TimeSpec)

Aggregations

RrdException (org.jrobin.core.RrdException)2 TimeParser (org.jrobin.core.timespec.TimeParser)2 TimeSpec (org.jrobin.core.timespec.TimeSpec)2 Calendar (java.util.Calendar)1 ResourceId (org.opennms.netmgt.model.ResourceId)1 MissingParameterException (org.opennms.web.servlet.MissingParameterException)1 GraphResults (org.opennms.web.svclayer.model.GraphResults)1 RelativeTimePeriod (org.opennms.web.svclayer.model.RelativeTimePeriod)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1