use of org.opencastproject.util.SmartIterator in project opencast by opencast.
the class ServicesListProvider method getList.
@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
ServicesListQuery servicesQuery;
try {
servicesQuery = (ServicesListQuery) query;
} catch (ClassCastException ex) {
servicesQuery = new ServicesListQuery(query);
}
Map<String, String> result = new HashMap<String, String>();
if (LIST_STATUS.equals(listName)) {
for (ServiceState s : ServiceState.values()) {
result.put(s.toString(), SERVICE_STATUS_FILTER_PREFIX + s.toString());
}
return result;
}
List<ServiceRegistration> serviceRegistrations;
try {
serviceRegistrations = serviceRegistry.getServiceRegistrations();
} catch (ServiceRegistryException ex) {
throw new ListProviderException("Failed to get service registrations.", ex);
}
for (ServiceRegistration serviceRegistration : serviceRegistrations) {
if (servicesQuery.getHostname().isSome() && !StringUtils.equals(servicesQuery.getHostname().get(), serviceRegistration.getHost()))
continue;
if (servicesQuery.getActions().isSome() && servicesQuery.getActions().get() && serviceRegistration.getServiceState() == ServiceState.NORMAL)
continue;
result.put(serviceRegistration.getServiceType(), serviceRegistration.getServiceType());
}
if (servicesQuery.getLimit().isSome() || servicesQuery.getLimit().isSome()) {
int limit = servicesQuery.getLimit().getOrElse(0);
int offset = servicesQuery.getOffset().getOrElse(0);
result = new SmartIterator(limit, offset).applyLimitAndOffset(result);
}
return result;
}
use of org.opencastproject.util.SmartIterator in project opencast by opencast.
the class CaptureAgentsEndpoint method getAgents.
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("agents.json")
@RestQuery(name = "getAgents", description = "Return all of the known capture agents on the system", restParameters = { @RestParameter(name = "filter", isRequired = false, description = "The filter used for the query. They should be formated like that: 'filter1:value1,filter2:value2'", type = STRING), @RestParameter(defaultValue = "100", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "false", description = "Define if the inputs should or not returned with the capture agent.", isRequired = false, name = "inputs", type = RestParameter.Type.BOOLEAN), @RestParameter(name = "sort", isRequired = false, description = "The sort order. May include any of the following: STATUS, NAME OR LAST_UPDATED. Add '_DESC' to reverse the sort order (e.g. STATUS_DESC).", type = STRING) }, reponses = { @RestResponse(description = "An XML representation of the agent capabilities", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "")
public Response getAgents(@QueryParam("limit") int limit, @QueryParam("offset") int offset, @QueryParam("inputs") boolean inputs, @QueryParam("filter") String filter, @QueryParam("sort") String sort) {
Option<String> filterName = Option.none();
Option<String> filterStatus = Option.none();
Option<Long> filterLastUpdated = Option.none();
Option<String> filterText = Option.none();
Option<String> optSort = Option.option(trimToNull(sort));
Map<String, String> filters = RestUtils.parseFilter(filter);
for (String name : filters.keySet()) {
if (AgentsListQuery.FILTER_NAME_NAME.equals(name))
filterName = Option.some(filters.get(name));
if (AgentsListQuery.FILTER_STATUS_NAME.equals(name))
filterStatus = Option.some(filters.get(name));
if (AgentsListQuery.FILTER_LAST_UPDATED.equals(name)) {
try {
filterLastUpdated = Option.some(Long.parseLong(filters.get(name)));
} catch (NumberFormatException e) {
logger.info("Unable to parse long {}", filters.get(name));
return Response.status(Status.BAD_REQUEST).build();
}
}
if (AgentsListQuery.FILTER_TEXT_NAME.equals(name) && StringUtils.isNotBlank(filters.get(name)))
filterText = Option.some(filters.get(name));
}
// Filter agents by filter criteria
List<Agent> filteredAgents = new ArrayList<>();
for (Entry<String, Agent> entry : service.getKnownAgents().entrySet()) {
Agent agent = entry.getValue();
// Filter list
if ((filterName.isSome() && !filterName.get().equals(agent.getName())) || (filterStatus.isSome() && !filterStatus.get().equals(agent.getState())) || (filterLastUpdated.isSome() && filterLastUpdated.get() != agent.getLastHeardFrom()) || (filterText.isSome() && !TextFilter.match(filterText.get(), agent.getName(), agent.getState())))
continue;
filteredAgents.add(agent);
}
int total = filteredAgents.size();
// Sort by status, name or last updated date
if (optSort.isSome()) {
final Set<SortCriterion> sortCriteria = RestUtils.parseSortQueryParameter(optSort.get());
Collections.sort(filteredAgents, new Comparator<Agent>() {
@Override
public int compare(Agent agent1, Agent agent2) {
for (SortCriterion criterion : sortCriteria) {
Order order = criterion.getOrder();
switch(criterion.getFieldName()) {
case "status":
if (order.equals(Order.Descending))
return agent2.getState().compareTo(agent1.getState());
return agent1.getState().compareTo(agent2.getState());
case "name":
if (order.equals(Order.Descending))
return agent2.getName().compareTo(agent1.getName());
return agent1.getName().compareTo(agent2.getName());
case "updated":
if (order.equals(Order.Descending))
return agent2.getLastHeardFrom().compareTo(agent1.getLastHeardFrom());
return agent1.getLastHeardFrom().compareTo(agent2.getLastHeardFrom());
default:
logger.info("Unknown sort type: {}", criterion.getFieldName());
return 0;
}
}
return 0;
}
});
}
// Apply Limit and offset
filteredAgents = new SmartIterator<Agent>(limit, offset).applyLimitAndOffset(filteredAgents);
// Run through and build a map of updates (rather than states)
List<JValue> agentsJSON = new ArrayList<>();
for (Agent agent : filteredAgents) {
agentsJSON.add(generateJsonAgent(agent, /* Option.option(room), blacklist, */
inputs, false));
}
return okJsonList(agentsJSON, offset, limit, total);
}
use of org.opencastproject.util.SmartIterator in project opencast by opencast.
the class UsersEndpoint method getUsers.
@GET
@Path("users.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(name = "allusers", description = "Returns a list of users", returnDescription = "Returns a JSON representation of the list of user accounts", restParameters = { @RestParameter(name = "filter", isRequired = false, description = "The filter used for the query. They should be formated like that: 'filter1:value1,filter2:value2'", type = STRING), @RestParameter(name = "sort", isRequired = false, description = "The sort order. May include any of the following: STATUS, NAME OR LAST_UPDATED. Add '_DESC' to reverse the sort order (e.g. STATUS_DESC).", type = STRING), @RestParameter(defaultValue = "100", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.STRING), @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "The user accounts.") })
public Response getUsers(@QueryParam("filter") String filter, @QueryParam("sort") String sort, @QueryParam("limit") int limit, @QueryParam("offset") int offset) throws IOException {
if (limit < 1)
limit = 100;
Option<String> optSort = Option.option(trimToNull(sort));
Option<String> filterName = Option.none();
Option<String> filterRole = Option.none();
Option<String> filterProvider = Option.none();
Option<String> filterText = Option.none();
Map<String, String> filters = RestUtils.parseFilter(filter);
for (String name : filters.keySet()) {
String value = filters.get(name);
if (UsersListQuery.FILTER_NAME_NAME.equals(name)) {
filterName = Option.some(value);
} else if (UsersListQuery.FILTER_ROLE_NAME.equals(name)) {
filterRole = Option.some(value);
} else if (UsersListQuery.FILTER_PROVIDER_NAME.equals(name)) {
filterProvider = Option.some(value);
} else if ((UsersListQuery.FILTER_TEXT_NAME.equals(name)) && (StringUtils.isNotBlank(value))) {
filterText = Option.some(value);
}
}
// Filter users by filter criteria
List<User> filteredUsers = new ArrayList<>();
for (Iterator<User> i = userDirectoryService.getUsers(); i.hasNext(); ) {
User user = i.next();
// Filter list
if (filterName.isSome() && !filterName.get().equals(user.getName()) || (filterRole.isSome() && !Stream.$(user.getRoles()).map(getRoleName).toSet().contains(filterRole.get())) || (filterProvider.isSome() && !filterProvider.get().equals(user.getProvider())) || (filterText.isSome() && !TextFilter.match(filterText.get(), user.getUsername(), user.getName(), user.getEmail(), user.getProvider()) && !TextFilter.match(filterText.get(), Stream.$(user.getRoles()).map(getRoleName).mkString(" ")))) {
continue;
}
filteredUsers.add(user);
}
int total = filteredUsers.size();
// Sort by name, description or role
if (optSort.isSome()) {
final Set<SortCriterion> sortCriteria = RestUtils.parseSortQueryParameter(optSort.get());
Collections.sort(filteredUsers, new Comparator<User>() {
@Override
public int compare(User user1, User user2) {
for (SortCriterion criterion : sortCriteria) {
Order order = criterion.getOrder();
switch(criterion.getFieldName()) {
case "name":
if (order.equals(Order.Descending))
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user2.getName()), trimToEmpty(user1.getName()));
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user1.getName()), trimToEmpty(user2.getName()));
case "username":
if (order.equals(Order.Descending))
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user2.getUsername()), trimToEmpty(user1.getUsername()));
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user1.getUsername()), trimToEmpty(user2.getUsername()));
case "email":
if (order.equals(Order.Descending))
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user2.getEmail()), trimToEmpty(user1.getEmail()));
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user1.getEmail()), trimToEmpty(user2.getEmail()));
case "roles":
String roles1 = Stream.$(user1.getRoles()).map(getRoleName).sort(sortByName).mkString(",");
String roles2 = Stream.$(user2.getRoles()).map(getRoleName).sort(sortByName).mkString(",");
if (order.equals(Order.Descending))
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(roles2), trimToEmpty(roles1));
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(roles1), trimToEmpty(roles2));
case "provider":
if (order.equals(Order.Descending))
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user2.getProvider()), trimToEmpty(user1.getProvider()));
return CASE_INSENSITIVE_ORDER.compare(trimToEmpty(user1.getProvider()), trimToEmpty(user2.getProvider()));
default:
logger.info("Unkown sort type: {}", criterion.getFieldName());
return 0;
}
}
return 0;
}
});
}
// Apply Limit and offset
filteredUsers = new SmartIterator<User>(limit, offset).applyLimitAndOffset(filteredUsers);
List<JValue> usersJSON = new ArrayList<>();
for (User user : filteredUsers) {
usersJSON.add(generateJsonUser(user));
}
return okJsonList(usersJSON, offset, limit, total);
}
use of org.opencastproject.util.SmartIterator in project opencast by opencast.
the class JobEndpoint method getJobs.
@GET
@Path("jobs.json")
@Produces(MediaType.APPLICATION_JSON)
@RestQuery(description = "Returns the list of active jobs", name = "jobs", restParameters = { @RestParameter(name = "limit", description = "The maximum number of items to return per page", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "offset", description = "The offset", isRequired = false, type = RestParameter.Type.INTEGER), @RestParameter(name = "filter", description = "Filter results by hostname, status or free text query", isRequired = false, type = RestParameter.Type.STRING), @RestParameter(name = "sort", description = "The sort order. May include any of the following: CREATOR, OPERATION, PROCESSINGHOST, STATUS, STARTED, SUBMITTED or TYPE. " + "The suffix must be :ASC for ascending or :DESC for descending sort order (e.g. OPERATION:DESC)", isRequired = false, type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "Returns the list of active jobs from Opencast", responseCode = HttpServletResponse.SC_OK) }, returnDescription = "The list of jobs as JSON")
public Response getJobs(@QueryParam("limit") final int limit, @QueryParam("offset") final int offset, @QueryParam("filter") final String filter, @QueryParam("sort") final String sort) {
JobsListQuery query = new JobsListQuery();
EndpointUtil.addRequestFiltersToQuery(filter, query);
query.setLimit(limit);
query.setOffset(offset);
String fHostname = null;
if (query.getHostname().isSome())
fHostname = StringUtils.trimToNull(query.getHostname().get());
String fStatus = null;
if (query.getStatus().isSome())
fStatus = StringUtils.trimToNull(query.getStatus().get());
String fFreeText = null;
if (query.getFreeText().isSome())
fFreeText = StringUtils.trimToNull(query.getFreeText().get());
List<Job> jobs = new ArrayList<>();
try {
for (Job job : serviceRegistry.getActiveJobs()) {
// filter workflow jobs
if (StringUtils.equals(WorkflowService.JOB_TYPE, job.getJobType()) && StringUtils.equals("START_WORKFLOW", job.getOperation()))
continue;
// filter by hostname
if (fHostname != null && !StringUtils.equalsIgnoreCase(job.getProcessingHost(), fHostname))
continue;
// filter by status
if (fStatus != null && !StringUtils.equalsIgnoreCase(job.getStatus().toString(), fStatus))
continue;
// fitler by user free text
if (fFreeText != null && !StringUtils.equalsIgnoreCase(job.getProcessingHost(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getJobType(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getOperation(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getCreator(), fFreeText) && !StringUtils.equalsIgnoreCase(job.getStatus().toString(), fFreeText) && !StringUtils.equalsIgnoreCase(Long.toString(job.getId()), fFreeText) && (job.getRootJobId() != null && !StringUtils.equalsIgnoreCase(Long.toString(job.getRootJobId()), fFreeText)))
continue;
jobs.add(job);
}
} catch (ServiceRegistryException ex) {
logger.error("Failed to retrieve jobs list from service registry.", ex);
return RestUtil.R.serverError();
}
JobSort sortKey = JobSort.SUBMITTED;
boolean ascending = true;
if (StringUtils.isNotBlank(sort)) {
try {
SortCriterion sortCriterion = RestUtils.parseSortQueryParameter(sort).iterator().next();
sortKey = JobSort.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);
}
}
JobComparator comparator = new JobComparator(sortKey, ascending);
Collections.sort(jobs, comparator);
List<JValue> json = getJobsAsJSON(new SmartIterator(query.getLimit().getOrElse(0), query.getOffset().getOrElse(0)).applyLimitAndOffset(jobs));
return RestUtils.okJsonList(json, offset, limit, jobs.size());
}
use of org.opencastproject.util.SmartIterator 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());
}
Aggregations