Search in sources :

Example 16 with UsageVO

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

the class LoadBalancerUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long lbId, 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 Volume usage record for load balancer: " + lbId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "Load Balancing Policy: " + lbId + " 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, lbId, null, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 17 with UsageVO

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

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 = s_usageNetworkDao.createSearchCriteria();
    sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
    sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
    List<UsageNetworkVO> usageNetworkVOs = s_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() + "-Network-" + usageNetwork.getNetworkId();
        }
        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));
    }
    List<UsageVO> usageRecords = new ArrayList<UsageVO>();
    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: " + toHumanReadableSize(totalBytesSent) + ", total bytes received: " + toHumanReadableSize(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);
            usageRecords.add(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);
            usageRecords.add(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());
            }
        }
    }
    s_usageDao.saveUsageRecords(usageRecords);
    return true;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UsageVO(com.cloud.usage.UsageVO) Date(java.util.Date) UsageNetworkVO(com.cloud.usage.UsageNetworkVO)

Example 18 with UsageVO

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

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

Example 19 with UsageVO

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

the class VMSnapshotOnPrimaryParser method createUsageRecord.

private static void createUsageRecord(int usageType, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String name, long zoneId, long virtualSize, long physicalSize, Long vmSnapshotId) {
    // 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 VMSnapshot Id: " + vmSnapshotId + " On Primary usage record for vm: " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "VMSnapshot Id: " + vmSnapshotId + " On Primary Usage: VM Id: " + vmId;
    usageDesc += " Size: " + toHumanReadableSize(virtualSize);
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usageType, new Double(usage), vmId, name, null, null, vmSnapshotId, physicalSize, virtualSize, startDate, endDate);
    s_usageDao.persist(usageRecord);
}
Also used : DecimalFormat(java.text.DecimalFormat) UsageVO(com.cloud.usage.UsageVO)

Example 20 with UsageVO

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

the class VMSnapshotUsageParser method createUsageRecord.

private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId, Long vmId, long size, Long vmSnapshotId) {
    // 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 VMSnapshot Id:" + vmSnapshotId + " Volume usage record for vol: " + volId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
    }
    // Create the usage record
    String usageDesc = "VMSnapshot Id: " + vmSnapshotId + " Usage: " + "VM Id: " + vmId + " Volume Id: " + volId + " ";
    if (doId != null) {
        usageDesc += " DiskOffering: " + doId;
    }
    usageDesc += " Size: " + toHumanReadableSize(size);
    UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, doId, null, vmSnapshotId, size, startDate, endDate);
    s_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