Search in sources :

Example 1 with ServersListQuery

use of org.opencastproject.index.service.resources.list.query.ServersListQuery in project opencast by opencast.

the class ServersListProviderTest method testHostnameList.

@Test
public void testHostnameList() throws ListProviderException {
    ResourceListQuery query = new ServersListQuery();
    Map<String, String> list = serverListProvider.getList(ServersListProvider.LIST_HOSTNAME, query, null);
    assertEquals(HOST1, list.get(HOST1));
    assertEquals(HOST2, list.get(HOST2));
    assertEquals(HOST3, list.get(HOST3));
    assertEquals(HOST4, list.get(HOST4));
}
Also used : ResourceListQuery(org.opencastproject.index.service.resources.list.api.ResourceListQuery) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) Test(org.junit.Test)

Example 2 with ServersListQuery

use of org.opencastproject.index.service.resources.list.query.ServersListQuery in project opencast by opencast.

the class ServersListProviderTest method testListNames.

@Test
public void testListNames() throws ListProviderException {
    ResourceListQuery query = new ServersListQuery();
    assertEquals(4, serverListProvider.getList("non-existing-name", query, null).size());
    assertEquals(4, serverListProvider.getList(ServersListProvider.LIST_HOSTNAME, query, null).size());
    assertEquals(3, serverListProvider.getList(ServersListProvider.LIST_STATUS, query, null).size());
}
Also used : ResourceListQuery(org.opencastproject.index.service.resources.list.api.ResourceListQuery) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) Test(org.junit.Test)

Example 3 with ServersListQuery

use of org.opencastproject.index.service.resources.list.query.ServersListQuery in project opencast by opencast.

the class ServersListProviderTest method testStatusList.

@Test
public void testStatusList() throws ListProviderException {
    ResourceListQuery query = new ServersListQuery();
    Map<String, String> list = serverListProvider.getList(ServersListProvider.LIST_STATUS, query, null);
    assertEquals(ServersListProvider.SERVER_STATUS_LABEL_ONLINE, list.get(ServersListProvider.SERVER_STATUS_ONLINE));
    assertEquals(ServersListProvider.SERVER_STATUS_LABEL_OFFLINE, list.get(ServersListProvider.SERVER_STATUS_OFFLINE));
    assertEquals(ServersListProvider.SERVER_STATUS_LABEL_MAINTENANCE, list.get(ServersListProvider.SERVER_STATUS_MAINTENANCE));
}
Also used : ResourceListQuery(org.opencastproject.index.service.resources.list.api.ResourceListQuery) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) Test(org.junit.Test)

Example 4 with ServersListQuery

use of org.opencastproject.index.service.resources.list.query.ServersListQuery in project opencast by opencast.

the class ServerEndpoint method getServers.

@GET
@Path("servers.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(description = "Returns the list of servers", name = "servers", restParameters = { @RestParameter(name = "limit", description = "The maximum number of items to return per page", isRequired = false, type = INTEGER), @RestParameter(name = "offset", description = "The offset", isRequired = false, type = INTEGER), @RestParameter(name = "filter", description = "Filter results by hostname, status or free text query", isRequired = false, type = STRING), @RestParameter(name = "sort", description = "The sort order.  May include any " + "of the following: COMPLETED (jobs), CORES, HOSTNAME, MAINTENANCE, MEANQUEUETIME (mean for jobs), " + "MEANRUNTIME (mean for jobs), ONLINE, QUEUED (jobs), RUNNING (jobs)." + "The suffix must be :ASC for ascending or :DESC for descending sort order (e.g. HOSTNAME:DESC).", isRequired = false, type = STRING) }, reponses = { @RestResponse(description = "Returns the list of jobs from Opencast", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The list of servers")
public Response getServers(@QueryParam("limit") final int limit, @QueryParam("offset") final int offset, @QueryParam("filter") String filter, @QueryParam("sort") String sort) throws Exception {
    ServersListQuery query = new ServersListQuery();
    EndpointUtil.addRequestFiltersToQuery(filter, query);
    query.setLimit(limit);
    query.setOffset(offset);
    List<JSONObject> servers = new ArrayList<>();
    // Get service statistics for all hosts and services
    List<ServiceStatistics> servicesStatistics = serviceRegistry.getServiceStatistics();
    for (HostRegistration server : serviceRegistry.getHostRegistrations()) {
        // Calculate statistics per server
        long jobsCompleted = 0;
        int jobsRunning = 0;
        int jobsQueued = 0;
        long sumMeanRuntime = 0;
        long sumMeanQueueTime = 0;
        int totalServiceOnHost = 0;
        int offlineJobProducerServices = 0;
        int totalJobProducerServices = 0;
        Set<String> serviceTypes = new HashSet<>();
        for (ServiceStatistics serviceStat : servicesStatistics) {
            if (server.getBaseUrl().equals(serviceStat.getServiceRegistration().getHost())) {
                totalServiceOnHost++;
                jobsCompleted += serviceStat.getFinishedJobs();
                jobsRunning += serviceStat.getRunningJobs();
                jobsQueued += serviceStat.getQueuedJobs();
                // mean time values are given in milliseconds,
                // we should convert them to seconds,
                // because the adminNG UI expect it in this format
                sumMeanRuntime += TimeUnit.MILLISECONDS.toSeconds(serviceStat.getMeanRunTime());
                sumMeanQueueTime += TimeUnit.MILLISECONDS.toSeconds(serviceStat.getMeanQueueTime());
                if (!serviceStat.getServiceRegistration().isOnline() && serviceStat.getServiceRegistration().isJobProducer()) {
                    offlineJobProducerServices++;
                    totalJobProducerServices++;
                } else if (serviceStat.getServiceRegistration().isJobProducer()) {
                    totalJobProducerServices++;
                }
                serviceTypes.add(serviceStat.getServiceRegistration().getServiceType());
            }
        }
        long meanRuntime = totalServiceOnHost > 0 ? Math.round((double) sumMeanRuntime / totalServiceOnHost) : 0L;
        long meanQueueTime = totalServiceOnHost > 0 ? Math.round((double) sumMeanQueueTime / totalServiceOnHost) : 0L;
        boolean vOnline = server.isOnline();
        boolean vMaintenance = server.isMaintenanceMode();
        String vHostname = server.getBaseUrl();
        int vCores = server.getCores();
        if (query.getHostname().isSome() && !StringUtils.equalsIgnoreCase(vHostname, query.getHostname().get()))
            continue;
        if (query.getStatus().isSome()) {
            if (StringUtils.equalsIgnoreCase(ServersListProvider.SERVER_STATUS_ONLINE, query.getStatus().get()) && !vOnline)
                continue;
            if (StringUtils.equalsIgnoreCase(ServersListProvider.SERVER_STATUS_OFFLINE, query.getStatus().get()) && vOnline)
                continue;
            if (StringUtils.equalsIgnoreCase(ServersListProvider.SERVER_STATUS_MAINTENANCE, query.getStatus().get()) && !vMaintenance)
                continue;
        }
        if (query.getFreeText().isSome() && !StringUtils.containsIgnoreCase(vHostname, query.getFreeText().get()) && !StringUtils.containsIgnoreCase(server.getIpAddress(), query.getFreeText().get()))
            continue;
        JSONObject jsonServer = new JSONObject();
        jsonServer.put(KEY_ONLINE, vOnline && offlineJobProducerServices <= totalJobProducerServices / 2);
        jsonServer.put(KEY_MAINTENANCE, vMaintenance);
        jsonServer.put(KEY_HOSTNAME, vHostname);
        jsonServer.put(KEY_CORES, vCores);
        jsonServer.put(KEY_RUNNING, jobsRunning);
        jsonServer.put(KEY_QUEUED, jobsQueued);
        jsonServer.put(KEY_COMPLETED, jobsCompleted);
        jsonServer.put(KEY_MEAN_RUN_TIME, meanRuntime);
        jsonServer.put(KEY_MEAN_QUEUE_TIME, meanQueueTime);
        servers.add(jsonServer);
    }
    // Sorting
    Sort sortKey = Sort.HOSTNAME;
    Boolean ascending = true;
    if (StringUtils.isNotBlank(sort)) {
        try {
            SortCriterion sortCriterion = RestUtils.parseSortQueryParameter(sort).iterator().next();
            sortKey = Sort.valueOf(sortCriterion.getFieldName().toUpperCase());
            ascending = SearchQuery.Order.Ascending == sortCriterion.getOrder() || SearchQuery.Order.None == sortCriterion.getOrder();
        } catch (WebApplicationException ex) {
            logger.warn("Failed to parse sort criterion \"{}\", invalid format.", sort);
        } catch (IllegalArgumentException ex) {
            logger.warn("Can not apply sort criterion \"{}\", no field with this name.", sort);
        }
    }
    JSONArray jsonList = new JSONArray();
    if (!servers.isEmpty()) {
        Collections.sort(servers, new ServerComparator(sortKey, ascending));
        jsonList.addAll(new SmartIterator(query.getLimit().getOrElse(0), query.getOffset().getOrElse(0)).applyLimitAndOffset(servers));
    }
    return RestUtils.okJsonList(getServersListAsJson(jsonList), query.getOffset().getOrElse(0), query.getLimit().getOrElse(0), servers.size());
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SmartIterator(org.opencastproject.util.SmartIterator) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) ServiceStatistics(org.opencastproject.serviceregistry.api.ServiceStatistics) JSONObject(org.json.simple.JSONObject) SortCriterion(org.opencastproject.matterhorn.search.SortCriterion) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 5 with ServersListQuery

use of org.opencastproject.index.service.resources.list.query.ServersListQuery in project opencast by opencast.

the class ServersListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    Map<String, String> list = new HashMap<String, String>();
    if (StringUtils.equalsIgnoreCase(LIST_STATUS, listName)) {
        list.put(SERVER_STATUS_ONLINE, SERVER_STATUS_LABEL_ONLINE);
        list.put(SERVER_STATUS_OFFLINE, SERVER_STATUS_LABEL_OFFLINE);
        list.put(SERVER_STATUS_MAINTENANCE, SERVER_STATUS_LABEL_MAINTENANCE);
        return list;
    }
    ServersListQuery serversQuery;
    try {
        serversQuery = (ServersListQuery) query;
    } catch (ClassCastException ex) {
        serversQuery = new ServersListQuery(query);
    }
    Option<String> fHostname = serversQuery.getHostname();
    Option<String> fStatus = serversQuery.getStatus();
    List<HostRegistration> allServers;
    try {
        allServers = serviceRegistry.getHostRegistrations();
    } catch (ServiceRegistryException e) {
        throw new ListProviderException("Not able to get the list of the hosts from the services registry");
    }
    for (HostRegistration server : allServers) {
        boolean vOnline = server.isOnline();
        boolean vMaintenance = server.isMaintenanceMode();
        String vHostname = server.getBaseUrl();
        if (fHostname.isSome() && !StringUtils.equalsIgnoreCase(StringUtils.trimToEmpty(fHostname.get()), vHostname))
            continue;
        if (fStatus.isSome()) {
            switch(StringUtils.trimToEmpty(fStatus.get())) {
                case SERVER_STATUS_ONLINE:
                    if (!vOnline)
                        continue;
                    break;
                case SERVER_STATUS_OFFLINE:
                    if (vOnline)
                        continue;
                    break;
                case SERVER_STATUS_MAINTENANCE:
                    if (!vMaintenance)
                        continue;
                    break;
                default:
                    break;
            }
        }
        switch(listName) {
            case LIST_HOSTNAME:
            default:
                list.put(vHostname, vHostname);
                break;
        }
    }
    return list;
}
Also used : HashMap(java.util.HashMap) HostRegistration(org.opencastproject.serviceregistry.api.HostRegistration) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) ServersListQuery(org.opencastproject.index.service.resources.list.query.ServersListQuery) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Aggregations

ServersListQuery (org.opencastproject.index.service.resources.list.query.ServersListQuery)5 Test (org.junit.Test)3 ResourceListQuery (org.opencastproject.index.service.resources.list.api.ResourceListQuery)3 HostRegistration (org.opencastproject.serviceregistry.api.HostRegistration)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 ListProviderException (org.opencastproject.index.service.exception.ListProviderException)1 SortCriterion (org.opencastproject.matterhorn.search.SortCriterion)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 ServiceStatistics (org.opencastproject.serviceregistry.api.ServiceStatistics)1 SmartIterator (org.opencastproject.util.SmartIterator)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1