Search in sources :

Example 11 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class IPAddressUsageParser method createUsageRecord.

private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long ipId, String ipAddress, boolean isSourceNat, boolean isSystem, boolean isHidden) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total usage time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating IP usage record with id: " + ipId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    String usageDesc = "IPAddress: " + ipAddress;
    // Create the usage record
    UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), ipId, (isSystem ? 1 : 0), (isSourceNat ? "SourceNat" : ""), startDate, endDate, isHidden);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 12 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class NetworkOfferingUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long noId, long zoneId, boolean isDefault) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating network offering:" + noId + " usage record for Vm : " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "Network offering:" + noId + " for Vm : " + vmId + " usage time";
    long defaultNic = (isDefault) ? 1 : 0;
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, noId, null, defaultNic, null, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 13 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class PortForwardingUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long pfId, long zoneId) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating usage record for port forwarding rule: " + pfId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "Port Forwarding Rule: " + pfId + " usage time";
    // ToDo: get zone id
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, pfId, null, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 14 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class StorageUsageParser method createUsageRecord.

private static void createUsageRecord(long zoneId, int type, long runningTime, Date startDate, Date endDate, AccountVO account, long storageId, Long sourceId, long size, Long virtualSize) {
    // Our smallest increment is hourly for now
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Total running time " + runningTime + "ms");
    }
    float usage = runningTime / 1000f / 60f / 60f;
    DecimalFormat dFormat = new DecimalFormat("#.######");
    String usageDisplay = dFormat.format(usage);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Creating Storage usage record for type: " + type + " with id: " + storageId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    String usageDesc = "";
    Long tmplSourceId = null;
    int usage_type = 0;
    switch(type) {
        case StorageTypes.TEMPLATE:
            usage_type = UsageTypes.TEMPLATE;
            usageDesc += "Template ";
            tmplSourceId = sourceId;
            break;
        case StorageTypes.ISO:
            usage_type = UsageTypes.ISO;
            usageDesc += "ISO ";
            virtualSize = size;
            break;
        case StorageTypes.SNAPSHOT:
            usage_type = UsageTypes.SNAPSHOT;
            usageDesc += "Snapshot ";
            break;
        case StorageTypes.VOLUME:
            usage_type = UsageTypes.VOLUME_SECONDARY;
            usageDesc += "Volume ";
            break;
    }
    // Create the usage record
    usageDesc += "Id:" + storageId + " Size:" + toHumanReadableSize(size);
    if (type != StorageTypes.SNAPSHOT) {
        usageDesc += " VirtualSize: " + toHumanReadableSize(virtualSize);
    }
    // ToDo: get zone id
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usage_type, new Double(usage), null, null, null, tmplSourceId, storageId, size, virtualSize, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 15 with UsageVO

use of com.cloud.usage.UsageVO in project cloudstack by apache.

the class BackupUsageParser method parse.

public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Parsing all VM Backup usage events for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
        endDate = new Date();
    }
    final List<UsageBackupVO> usageBackups = s_usageBackupDao.getUsageRecords(account.getId(), startDate, endDate);
    if (usageBackups == null || usageBackups.isEmpty()) {
        LOGGER.debug("No VM Backup usage for this period");
        return true;
    }
    for (final UsageBackupVO usageBackup : usageBackups) {
        final Long vmId = usageBackup.getVmId();
        final Long zoneId = usageBackup.getZoneId();
        final Long offeringId = usageBackup.getBackupOfferingId();
        Date createdDate = usageBackup.getCreated();
        Date removedDate = usageBackup.getRemoved();
        if (createdDate.before(startDate)) {
            createdDate = startDate;
        }
        if (removedDate == null || removedDate.after(endDate)) {
            removedDate = endDate;
        }
        final long duration = (removedDate.getTime() - createdDate.getTime()) + 1;
        final float usage = duration / 1000f / 60f / 60f;
        DecimalFormat dFormat = new DecimalFormat("#.######");
        String usageDisplay = dFormat.format(usage);
        final Double rawUsage = (double) usageBackup.getSize();
        final String description = String.format("Backup usage VM ID: %d, backup offering: %d", vmId, offeringId);
        final UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs", UsageTypes.BACKUP, new Double(usage), vmId, null, offeringId, null, vmId, usageBackup.getSize(), usageBackup.getProtectedSize(), startDate, endDate);
        s_usageDao.persist(usageRecord);
    }
    return true;
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageBackupVO(com.cloud.usage.UsageBackupVO) UsageVO(com.cloud.usage.UsageVO) Date(java.util.Date)

Aggregations

UsageVO (com.cloud.usage.UsageVO)31 DecimalFormat (java.text.DecimalFormat)21 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 QuotaUsageVO (org.apache.cloudstack.quota.vo.QuotaUsageVO)5 AccountVO (com.cloud.user.AccountVO)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 List (java.util.List)3 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)3 UsageNetworkVO (com.cloud.usage.UsageNetworkVO)2 Pair (com.cloud.utils.Pair)2 DomainVO (com.cloud.domain.DomainVO)1 UsageBackupVO (com.cloud.usage.UsageBackupVO)1 UsageVmDiskVO (com.cloud.usage.UsageVmDiskVO)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 UsageRecordResponse (org.apache.cloudstack.api.response.UsageRecordResponse)1