use of org.opencastproject.serviceregistry.api.JaxbServiceStatistics in project opencast by opencast.
the class ServiceRegistryJpaImpl method getServiceStatistics.
/**
* Gets performance and runtime statistics for each known service registration.
* For the statistics, only jobs created within the time interval [startDate, endDate] are being considered
*
* @param startDate
* Only jobs created after this data are considered for statistics
* @param endDate
* Only jobs created before this data are considered for statistics
* @return the service statistics
* @throws ServiceRegistryException
* if there is a problem accessing the service registry
*/
private List<ServiceStatistics> getServiceStatistics(Date startDate, Date endDate) throws ServiceRegistryException {
EntityManager em = null;
try {
em = emf.createEntityManager();
Map<Long, JaxbServiceStatistics> statsMap = new HashMap<Long, JaxbServiceStatistics>();
// Make sure we also include the services that have no processing history so far
List<ServiceRegistrationJpaImpl> services = em.createNamedQuery("ServiceRegistration.getAll").getResultList();
for (ServiceRegistrationJpaImpl s : services) {
statsMap.put(s.getId(), new JaxbServiceStatistics(s));
}
Query query = em.createNamedQuery("ServiceRegistration.statistics");
query.setParameter("minDateCreated", startDate, TemporalType.TIMESTAMP);
query.setParameter("maxDateCreated", endDate, TemporalType.TIMESTAMP);
List queryResults = query.getResultList();
for (Object result : queryResults) {
Object[] oa = (Object[]) result;
Number serviceRegistrationId = ((Number) oa[0]);
if (serviceRegistrationId == null || serviceRegistrationId.longValue() == 0)
continue;
Status status = Status.values()[((Number) oa[1]).intValue()];
Number count = (Number) oa[2];
Number meanQueueTime = (Number) oa[3];
Number meanRunTime = (Number) oa[4];
// The statistics query returns a cartesian product, so we need to iterate over them to build up the objects
JaxbServiceStatistics stats = statsMap.get(serviceRegistrationId.longValue());
if (stats == null)
continue;
// the status will be null if there are no jobs at all associated with this service registration
if (status != null) {
switch(status) {
case RUNNING:
stats.setRunningJobs(count.intValue());
break;
case QUEUED:
case DISPATCHING:
stats.setQueuedJobs(count.intValue());
break;
case FINISHED:
stats.setMeanRunTime(meanRunTime.longValue());
stats.setMeanQueueTime(meanQueueTime.longValue());
stats.setFinishedJobs(count.intValue());
break;
default:
break;
}
}
}
List<ServiceStatistics> stats = new ArrayList<ServiceStatistics>(statsMap.values());
Collections.sort(stats, new Comparator<ServiceStatistics>() {
@Override
public int compare(ServiceStatistics o1, ServiceStatistics o2) {
ServiceRegistration reg1 = o1.getServiceRegistration();
ServiceRegistration reg2 = o2.getServiceRegistration();
int typeComparison = reg1.getServiceType().compareTo(reg2.getServiceType());
return typeComparison == 0 ? reg1.getHost().compareTo(reg2.getHost()) : typeComparison;
}
});
return stats;
} catch (Exception e) {
throw new ServiceRegistryException(e);
} finally {
if (em != null)
em.close();
}
}
Aggregations