Search in sources :

Example 1 with EntityStatistics

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;
}
Also used : EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics)

Example 2 with EntityStatistics

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;
}
Also used : EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics)

Example 3 with EntityStatistics

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();
    }
}
Also used : EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics)

Example 4 with EntityStatistics

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();
    }
}
Also used : com.haulmont.cuba.core(com.haulmont.cuba.core) DbmsSpecificFactory(com.haulmont.cuba.core.sys.persistence.DbmsSpecificFactory) InheritanceType(javax.persistence.InheritanceType) JoinTable(javax.persistence.JoinTable) java.util(java.util) Logger(org.slf4j.Logger) Connection(java.sql.Connection) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Stores(com.haulmont.cuba.core.global.Stores) DatabaseMetaData(java.sql.DatabaseMetaData) MetaClass(com.haulmont.chile.core.model.MetaClass) Metadata(com.haulmont.cuba.core.global.Metadata) DbmsType(com.haulmont.cuba.core.sys.persistence.DbmsType) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) SQLException(java.sql.SQLException) Table(javax.persistence.Table) EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics) Inheritance(javax.persistence.Inheritance) Configuration(com.haulmont.cuba.core.global.Configuration) ResultSet(java.sql.ResultSet) DataSource(javax.sql.DataSource) MetaClass(com.haulmont.chile.core.model.MetaClass) EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics)

Example 5 with EntityStatistics

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);
    }
}
Also used : EntityStatistics(com.haulmont.cuba.core.entity.EntityStatistics) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException)

Aggregations

EntityStatistics (com.haulmont.cuba.core.entity.EntityStatistics)6 DbInitializationException (com.haulmont.cuba.core.sys.DbInitializationException)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 com.haulmont.cuba.core (com.haulmont.cuba.core)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 Stores (com.haulmont.cuba.core.global.Stores)1 DbmsSpecificFactory (com.haulmont.cuba.core.sys.persistence.DbmsSpecificFactory)1 DbmsType (com.haulmont.cuba.core.sys.persistence.DbmsType)1 Authenticated (com.haulmont.cuba.security.app.Authenticated)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Inject (javax.inject.Inject)1 Inheritance (javax.persistence.Inheritance)1 InheritanceType (javax.persistence.InheritanceType)1