use of com.haulmont.cuba.core.entity.EntityStatistics in project cuba by cuba-platform.
the class PersistenceManager method getEntityStatisticsInstance.
protected EntityStatistics getEntityStatisticsInstance(String name, EntityManager em) {
TypedQuery<EntityStatistics> q = em.createQuery("select s from sys$EntityStatistics s where s.name = ?1", EntityStatistics.class);
q.setParameter(1, name);
List<EntityStatistics> list = q.getResultList();
EntityStatistics es;
if (list.isEmpty()) {
es = metadata.create(EntityStatistics.class);
es.setName(name);
em.persist(es);
} else {
es = list.get(0);
}
return es;
}
use of com.haulmont.cuba.core.entity.EntityStatistics in project cuba by cuba-platform.
the class PersistenceManager method enterStatistics.
@Override
public synchronized EntityStatistics enterStatistics(String name, Long instanceCount, Integer fetchUI, Integer maxFetchUI, Integer lazyCollectionThreshold, Integer lookupScreenThreshold) {
Transaction tx = persistence.createTransaction();
EntityStatistics es;
try {
EntityManager em = persistence.getEntityManager();
es = getEntityStatisticsInstance(getOriginalOrThisEntityName(name), em);
if (instanceCount != null) {
es.setInstanceCount(instanceCount);
}
if (fetchUI != null) {
es.setFetchUI(fetchUI);
}
if (maxFetchUI != null) {
es.setMaxFetchUI(maxFetchUI);
}
if (lazyCollectionThreshold != null) {
es.setLazyCollectionThreshold(lazyCollectionThreshold);
}
if (lookupScreenThreshold != null) {
es.setLookupScreenThreshold(lookupScreenThreshold);
}
tx.commit();
} finally {
tx.end();
}
flushStatisticsCache();
return es;
}
use of com.haulmont.cuba.core.entity.EntityStatistics in project cuba by cuba-platform.
the class PersistenceManager method internalLoadStatisticsCache.
protected void internalLoadStatisticsCache() {
log.info("Loading statistics cache");
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
TypedQuery<EntityStatistics> q = em.createQuery("select s from sys$EntityStatistics s", EntityStatistics.class);
List<EntityStatistics> list = q.getResultList();
for (EntityStatistics es : list) {
statisticsCache.put(es.getName(), es);
}
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.EntityStatistics in project cuba by cuba-platform.
the class PersistenceManager method refreshStatisticsForEntity.
@Override
public void refreshStatisticsForEntity(String name) {
log.debug("Refreshing statistics for entity " + name);
MetaClass metaClass = metadata.getExtendedEntities().getOriginalOrThisMetaClass(metadata.getClassNN(name));
String storeName = metadata.getTools().getStoreName(metaClass);
if (storeName == null) {
log.debug("Entity " + name + " is not persistent, ignoring it");
return;
}
Transaction tx = persistence.createTransaction(storeName);
try {
EntityManager em = persistence.getEntityManager(storeName);
Query q = em.createQuery("select count(e) from " + name + " e");
Long count = (Long) q.getSingleResult();
EntityStatistics entityStatistics;
if (Stores.isMain(storeName)) {
entityStatistics = getEntityStatisticsInstance(name, em);
entityStatistics.setInstanceCount(count);
} else {
entityStatistics = persistence.callInTransaction(mainDsEm -> {
EntityStatistics es = getEntityStatisticsInstance(name, mainDsEm);
es.setInstanceCount(count);
return es;
});
}
getStatisticsCache().put(name, entityStatistics);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.EntityStatistics in project cuba by cuba-platform.
the class PersistenceManager method showStatistics.
@Override
public String showStatistics(String entityName) {
try {
Map<String, EntityStatistics> statistics = persistenceManager.getEntityStatistics();
if (StringUtils.isBlank(entityName)) {
StringBuilder sb = new StringBuilder();
sb.append("Displaying statistics for all entities.\n");
sb.append("To show a particular entity only, pass its name in the method parameter.\n\n");
for (EntityStatistics stat : statistics.values()) {
sb.append(stat).append("\n");
}
return sb.toString();
} else {
EntityStatistics es = statistics.get(entityName);
return es == null ? "No such entity" : es.toString();
}
} catch (Exception e) {
log.error("showStatistics error", e);
return ExceptionUtils.getStackTrace(e);
}
}
Aggregations