Search in sources :

Example 1 with RelativeTimePeriod

use of org.opennms.web.svclayer.model.RelativeTimePeriod in project opennms by OpenNMS.

the class DefaultDistributedStatusService method createHistoryModel.

/** {@inheritDoc} */
@Override
public DistributedStatusHistoryModel createHistoryModel(String locationName, String monitorId, String applicationName, String timeSpan, String previousLocationName) {
    List<String> errors = new LinkedList<String>();
    List<OnmsMonitoringLocation> locationDefinitions = m_monitoringLocationDao.findAll();
    List<RelativeTimePeriod> periods = Arrays.asList(RelativeTimePeriod.getDefaultPeriods());
    Collection<OnmsApplication> applications = m_applicationDao.findAll();
    List<OnmsApplication> sortedApplications = new ArrayList<OnmsApplication>(applications);
    Collections.sort(sortedApplications);
    OnmsMonitoringLocation location = new OnmsMonitoringLocation();
    if (locationName == null) {
        if (!locationDefinitions.isEmpty()) {
            location = locationDefinitions.get(0);
        }
    } else {
        location = m_monitoringLocationDao.get(locationName);
        if (location == null) {
            errors.add("Could not find location definition '" + locationName + "'");
            if (!locationDefinitions.isEmpty()) {
                location = locationDefinitions.get(0);
            }
        }
    }
    OnmsApplication application = new OnmsApplication();
    if (applicationName == null) {
        if (!sortedApplications.isEmpty()) {
            application = sortedApplications.get(0);
        }
    } else {
        application = m_applicationDao.findByName(applicationName);
        if (application == null) {
            errors.add("Could not find application '" + applicationName + "'");
            if (!sortedApplications.isEmpty()) {
                application = sortedApplications.get(0);
            }
        }
    }
    Collection<OnmsLocationMonitor> monitors = m_locationMonitorDao.findByLocationDefinition(location);
    List<OnmsLocationMonitor> sortedMonitors = new LinkedList<OnmsLocationMonitor>(monitors);
    Collections.sort(sortedMonitors);
    OnmsLocationMonitor monitor = null;
    if (monitorId != null && !"".equals(monitorId.trim()) && location.getLocationName().equals(previousLocationName)) {
        for (OnmsLocationMonitor m : sortedMonitors) {
            if (m.getId().equals(monitorId)) {
                monitor = m;
                break;
            }
        }
    }
    if (monitor == null && !sortedMonitors.isEmpty()) {
        monitor = sortedMonitors.get(0);
    }
    RelativeTimePeriod period = RelativeTimePeriod.getPeriodByIdOrDefault(timeSpan);
    /*
         * Initialize the hierarchy under the service so that we don't get
         * a LazyInitializationException later when the JSP page is pulling
         * data out of the model object.
         */
    Collection<OnmsMonitoredService> memberServices = m_monitoredServiceDao.findByApplication(application);
    for (OnmsMonitoredService service : memberServices) {
        m_locationMonitorDao.initialize(service.getIpInterface());
        m_locationMonitorDao.initialize(service.getIpInterface().getNode());
    }
    Collection<OnmsMonitoredService> applicationMemberServices = m_monitoredServiceDao.findByApplication(application);
    if (applicationMemberServices.isEmpty()) {
        errors.add("There are no services in the application '" + applicationName + "'");
    }
    DistributedStatusHistoryModel model = new DistributedStatusHistoryModel(locationDefinitions, sortedApplications, sortedMonitors, periods, location, application, applicationMemberServices, monitor, period, errors);
    initializeGraphUrls(model);
    return model;
}
Also used : ArrayList(java.util.ArrayList) OnmsApplication(org.opennms.netmgt.model.OnmsApplication) LinkedList(java.util.LinkedList) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) DistributedStatusHistoryModel(org.opennms.web.svclayer.model.DistributedStatusHistoryModel) RelativeTimePeriod(org.opennms.web.svclayer.model.RelativeTimePeriod) OnmsLocationMonitor(org.opennms.netmgt.model.OnmsLocationMonitor) OnmsMonitoringLocation(org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation)

Example 2 with RelativeTimePeriod

use of org.opennms.web.svclayer.model.RelativeTimePeriod 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)

Aggregations

RelativeTimePeriod (org.opennms.web.svclayer.model.RelativeTimePeriod)2 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 LinkedList (java.util.LinkedList)1 RrdException (org.jrobin.core.RrdException)1 TimeParser (org.jrobin.core.timespec.TimeParser)1 TimeSpec (org.jrobin.core.timespec.TimeSpec)1 OnmsApplication (org.opennms.netmgt.model.OnmsApplication)1 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)1 OnmsMonitoredService (org.opennms.netmgt.model.OnmsMonitoredService)1 ResourceId (org.opennms.netmgt.model.ResourceId)1 OnmsMonitoringLocation (org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation)1 MissingParameterException (org.opennms.web.servlet.MissingParameterException)1 DistributedStatusHistoryModel (org.opennms.web.svclayer.model.DistributedStatusHistoryModel)1 GraphResults (org.opennms.web.svclayer.model.GraphResults)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1