Search in sources :

Example 1 with QuotaUsagePerUser

use of org.ovirt.engine.core.common.businessentities.QuotaUsagePerUser in project ovirt-engine by oVirt.

the class QuotaManager method generatePerUserUsageReport.

/**
 * Return a list of QuotaUsagePerUser representing the status of all the quotas available for a specific user
 *
 * @param quotaIdsList
 *            - quotas available for user
 * @return - list of QuotaUsagePerUser
 */
public Map<Guid, QuotaUsagePerUser> generatePerUserUsageReport(List<Quota> quotaIdsList) {
    Map<Guid, QuotaUsagePerUser> quotaPerUserUsageEntityMap = new HashMap<>();
    List<Quota> needToCache = new ArrayList<>();
    if (quotaIdsList != null) {
        lock.readLock().lock();
        try {
            for (Quota quotaExternal : quotaIdsList) {
                // look for the quota in the cache
                Map<Guid, Quota> quotaMap = storagePoolQuotaMap.get(quotaExternal.getStoragePoolId());
                Quota quota = null;
                if (quotaMap != null) {
                    quota = quotaMap.get(quotaExternal.getId());
                }
                // if quota not in cache look for it in DB and add it to cache
                if (quota == null) {
                    needToCache.add(quotaExternal);
                } else {
                    QuotaUsagePerUser usagePerUser = addQuotaEntry(quota);
                    if (usagePerUser != null) {
                        quotaPerUserUsageEntityMap.put(quota.getId(), usagePerUser);
                    }
                }
            }
        } finally {
            lock.readLock().unlock();
        }
        if (!needToCache.isEmpty()) {
            lock.writeLock().lock();
            try {
                for (Quota quotaExternal : needToCache) {
                    // look for the quota in the cache again (it may have been added by now)
                    addStoragePoolToCache(quotaExternal.getStoragePoolId());
                    Quota quota = fetchQuotaFromCache(quotaExternal.getId(), quotaExternal.getStoragePoolId());
                    QuotaUsagePerUser usagePerUser = addQuotaEntry(quota);
                    if (usagePerUser != null) {
                        quotaPerUserUsageEntityMap.put(quota.getId(), usagePerUser);
                    }
                }
            } finally {
                lock.writeLock().unlock();
            }
        }
    }
    return quotaPerUserUsageEntityMap;
}
Also used : Quota(org.ovirt.engine.core.common.businessentities.Quota) HashMap(java.util.HashMap) QuotaUsagePerUser(org.ovirt.engine.core.common.businessentities.QuotaUsagePerUser) ArrayList(java.util.ArrayList) Guid(org.ovirt.engine.core.compat.Guid)

Example 2 with QuotaUsagePerUser

use of org.ovirt.engine.core.common.businessentities.QuotaUsagePerUser in project ovirt-engine by oVirt.

the class QuotaManager method addQuotaEntry.

private QuotaUsagePerUser addQuotaEntry(Quota quota) {
    // if quota is not null (found in cache or DB) - add entry to quotaPerUserUsageEntityMap
    if (quota != null) {
        long storageLimit = 0;
        double storageUsage = 0;
        int cpuLimit = 0;
        int cpuUsage = 0;
        long memLimit = 0;
        long memUsage = 0;
        // calc storage
        if (quota.getGlobalQuotaStorage() != null) {
            storageLimit = quota.getGlobalQuotaStorage().getStorageSizeGB();
            storageUsage = quota.getGlobalQuotaStorage().getStorageSizeGBUsage();
        } else {
            for (QuotaStorage quotaStorage : quota.getQuotaStorages()) {
                // once storage was set unlimited it will remain so
                if (QuotaStorage.UNLIMITED.equals(quotaStorage.getStorageSizeGB())) {
                    // Do not break because usage is still counting
                    storageLimit = QuotaStorage.UNLIMITED;
                }
                if (storageLimit != QuotaStorage.UNLIMITED) {
                    storageLimit += quotaStorage.getStorageSizeGB();
                }
                storageUsage += quotaStorage.getStorageSizeGBUsage();
            }
        }
        // calc cpu and mem
        if (quota.getGlobalQuotaCluster() != null) {
            memLimit = quota.getGlobalQuotaCluster().getMemSizeMB();
            memUsage = quota.getGlobalQuotaCluster().getMemSizeMBUsage();
            cpuLimit = quota.getGlobalQuotaCluster().getVirtualCpu();
            cpuUsage = quota.getGlobalQuotaCluster().getVirtualCpuUsage();
        } else {
            for (QuotaCluster quotaCluster : quota.getQuotaClusters()) {
                // once mem was set unlimited it will remain so
                if (QuotaCluster.UNLIMITED_MEM.equals(quotaCluster.getMemSizeMB())) {
                    // Do not break because usage is still counting
                    memLimit = QuotaCluster.UNLIMITED_MEM;
                }
                if (memLimit != QuotaCluster.UNLIMITED_MEM) {
                    memLimit += quotaCluster.getMemSizeMB();
                }
                // once cpu was set unlimited it will remain so
                if (QuotaCluster.UNLIMITED_VCPU.equals(quotaCluster.getVirtualCpu())) {
                    // Do not break because usage is still counting
                    cpuLimit = QuotaCluster.UNLIMITED_VCPU;
                }
                if (cpuLimit != QuotaCluster.UNLIMITED_VCPU) {
                    cpuLimit += quotaCluster.getVirtualCpu();
                }
                memUsage += quotaCluster.getMemSizeMBUsage();
                cpuUsage += quotaCluster.getVirtualCpuUsage();
            }
        }
        return new QuotaUsagePerUser(quota.getId(), quota.getQuotaName(), storageLimit, storageUsage, cpuLimit, cpuUsage, memLimit, memUsage);
    }
    return null;
}
Also used : QuotaStorage(org.ovirt.engine.core.common.businessentities.QuotaStorage) QuotaUsagePerUser(org.ovirt.engine.core.common.businessentities.QuotaUsagePerUser) QuotaCluster(org.ovirt.engine.core.common.businessentities.QuotaCluster)

Aggregations

QuotaUsagePerUser (org.ovirt.engine.core.common.businessentities.QuotaUsagePerUser)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Quota (org.ovirt.engine.core.common.businessentities.Quota)1 QuotaCluster (org.ovirt.engine.core.common.businessentities.QuotaCluster)1 QuotaStorage (org.ovirt.engine.core.common.businessentities.QuotaStorage)1 Guid (org.ovirt.engine.core.compat.Guid)1