Search in sources :

Example 1 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

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);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 2 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

the class NetworkUsageParser method parse.

public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Parsing all Network usage events for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
        endDate = new Date();
    }
    // - query usage_network table for all entries for userId with
    // event_date in the given range
    SearchCriteria<UsageNetworkVO> sc = m_usageNetworkDao.createSearchCriteria();
    sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
    sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
    List<UsageNetworkVO> usageNetworkVOs = m_usageNetworkDao.search(sc, null);
    Map<String, NetworkInfo> networkUsageByZone = new HashMap<String, NetworkInfo>();
    // Calculate the total bytes since last parsing
    for (UsageNetworkVO usageNetwork : usageNetworkVOs) {
        long zoneId = usageNetwork.getZoneId();
        String key = "" + zoneId;
        if (usageNetwork.getHostId() != 0) {
            key += "-Host" + usageNetwork.getHostId();
        }
        NetworkInfo networkInfo = networkUsageByZone.get(key);
        long bytesSent = usageNetwork.getBytesSent();
        long bytesReceived = usageNetwork.getBytesReceived();
        if (networkInfo != null) {
            bytesSent += networkInfo.getBytesSent();
            bytesReceived += networkInfo.getBytesRcvd();
        }
        networkUsageByZone.put(key, new NetworkInfo(zoneId, usageNetwork.getHostId(), usageNetwork.getHostType(), usageNetwork.getNetworkId(), bytesSent, bytesReceived));
    }
    for (String key : networkUsageByZone.keySet()) {
        NetworkInfo networkInfo = networkUsageByZone.get(key);
        long totalBytesSent = networkInfo.getBytesSent();
        long totalBytesReceived = networkInfo.getBytesRcvd();
        if ((totalBytesSent > 0L) || (totalBytesReceived > 0L)) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Creating usage record, total bytes sent:" + totalBytesSent + ", total bytes received: " + totalBytesReceived + " for account: " + account.getId() + " in availability zone " + networkInfo.getZoneId() + ", start: " + startDate + ", end: " + endDate);
            }
            Long hostId = null;
            // Create the usage record for bytes sent
            String usageDesc = "network bytes sent";
            if (networkInfo.getHostId() != 0) {
                hostId = networkInfo.getHostId();
                usageDesc += " for Host: " + networkInfo.getHostId();
            }
            UsageVO usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesSent + " bytes sent", UsageTypes.NETWORK_BYTES_SENT, new Double(totalBytesSent), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
            m_usageDao.persist(usageRecord);
            // Create the usage record for bytes received
            usageDesc = "network bytes received";
            if (networkInfo.getHostId() != 0) {
                usageDesc += " for Host: " + networkInfo.getHostId();
            }
            usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesReceived + " bytes received", UsageTypes.NETWORK_BYTES_RECEIVED, new Double(totalBytesReceived), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
            m_usageDao.persist(usageRecord);
        } else {
            // Don't charge anything if there were zero bytes processed
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("No usage record (0 bytes used) generated for account: " + account.getId());
            }
        }
    }
    return true;
}
Also used : UsageNetworkVO(com.cloud.usage.UsageNetworkVO) HashMap(java.util.HashMap) UsageVO(com.cloud.usage.UsageVO) Date(java.util.Date)

Example 3 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

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);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 4 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

the class SecurityGroupUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long sgId, 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 security group:" + sgId + " usage record for Vm : " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "Security Group: " + sgId + " for Vm : " + vmId + " usage time";
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, null, null, sgId, null, startDate, endDate);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 5 with UsageVO

use of com.cloud.usage.UsageVO in project CloudStack-archive by CloudStack-extras.

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) {
    // 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 ";
            break;
        case StorageTypes.SNAPSHOT:
            usage_type = UsageTypes.SNAPSHOT;
            usageDesc += "Snapshot ";
            break;
    }
    // Create the usage record
    usageDesc += "Id:" + storageId + " Size:" + size;
    //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, startDate, endDate);
    m_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

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