Usage parsers refactoring (#11097)

This commit is contained in:
Erik Böck 2025-07-16 03:38:47 -03:00 committed by GitHub
parent 3220eb442a
commit bb75abcffa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 255 additions and 617 deletions

View File

@ -34,10 +34,9 @@ import java.util.concurrent.TimeUnit;
import com.cloud.network.Network;
import com.cloud.usage.dao.UsageNetworksDao;
import com.cloud.usage.parser.NetworksUsageParser;
import com.cloud.usage.parser.UsageParser;
import com.cloud.network.vpc.Vpc;
import com.cloud.usage.dao.UsageVpcDao;
import com.cloud.usage.parser.VpcUsageParser;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import javax.persistence.EntityExistsException;
@ -75,21 +74,6 @@ import com.cloud.usage.dao.UsageVMSnapshotOnPrimaryDao;
import com.cloud.usage.dao.UsageVPNUserDao;
import com.cloud.usage.dao.UsageVmDiskDao;
import com.cloud.usage.dao.UsageVolumeDao;
import com.cloud.usage.parser.BackupUsageParser;
import com.cloud.usage.parser.BucketUsageParser;
import com.cloud.usage.parser.IPAddressUsageParser;
import com.cloud.usage.parser.LoadBalancerUsageParser;
import com.cloud.usage.parser.NetworkOfferingUsageParser;
import com.cloud.usage.parser.NetworkUsageParser;
import com.cloud.usage.parser.PortForwardingUsageParser;
import com.cloud.usage.parser.SecurityGroupUsageParser;
import com.cloud.usage.parser.StorageUsageParser;
import com.cloud.usage.parser.VMInstanceUsageParser;
import com.cloud.usage.parser.VMSnapshotOnPrimaryParser;
import com.cloud.usage.parser.VMSnapshotUsageParser;
import com.cloud.usage.parser.VPNUserUsageParser;
import com.cloud.usage.parser.VmDiskUsageParser;
import com.cloud.usage.parser.VolumeUsageParser;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.UserStatisticsVO;
@ -180,6 +164,9 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
@Inject
private UsageVpcDao usageVpcDao;
@Inject
private List<UsageParser> usageParsers;
private String _version = null;
private final Calendar _jobExecTime = Calendar.getInstance();
private int _aggregationDuration = 0;
@ -198,6 +185,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
private Future _heartbeat = null;
private Future _sanity = null;
private boolean usageSnapshotSelection = false;
private static TimeZone usageAggregationTimeZone = TimeZone.getTimeZone("GMT");
public UsageManagerImpl() {
@ -954,114 +942,12 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
private boolean parseHelperTables(AccountVO account, Date currentStartDate, Date currentEndDate) {
boolean parsed = false;
parsed = VMInstanceUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("vm usage instances successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
for (UsageParser parser : usageParsers) {
parsed = parser.doParsing(account, currentStartDate, currentEndDate);
logger.debug("{} usage was {} parsed for [{}].", parser.getParserName(), parsed ? "successfully" : "not successfully", account);
}
parsed = NetworkUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("network usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = VmDiskUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("vm disk usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = VolumeUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("volume usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = StorageUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("storage usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = SecurityGroupUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("Security Group usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = LoadBalancerUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("load balancer usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = PortForwardingUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("port forwarding usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = NetworkOfferingUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("network offering usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = IPAddressUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("IPAddress usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = VPNUserUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("VPN user usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = VMSnapshotUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("VM Snapshot usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = VMSnapshotOnPrimaryParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("VM Snapshot on primary usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = BackupUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("VM Backup usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = BucketUsageParser.parse(account, currentStartDate, currentEndDate);
if (logger.isDebugEnabled()) {
if (!parsed) {
logger.debug("Bucket usage successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")");
}
}
parsed = NetworksUsageParser.parse(account, currentStartDate, currentEndDate);
if (!parsed) {
logger.debug("Networks usage not parsed for account [{}}].", account);
}
parsed = VpcUsageParser.parse(account, currentStartDate, currentEndDate);
if (!parsed) {
logger.debug(String.format("VPC usage failed to parse for account [%s].", account));
}
return parsed;
}

View File

@ -21,49 +21,35 @@ import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.cloudstack.usage.UsageTypes;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import com.cloud.usage.UsageBackupVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageBackupDao;
import com.cloud.user.AccountVO;
@Component
public class BackupUsageParser {
protected static Logger LOGGER = LogManager.getLogger(BackupUsageParser.class);
private static UsageDao s_usageDao;
private static UsageBackupDao s_usageBackupDao;
@Inject
private UsageDao usageDao;
public class BackupUsageParser extends UsageParser {
@Inject
private UsageBackupDao usageBackupDao;
@PostConstruct
void init() {
s_usageDao = usageDao;
s_usageBackupDao = usageBackupDao;
@Override
public String getParserName() {
return "VM Backup";
}
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());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
final List<UsageBackupVO> usageBackups = s_usageBackupDao.getUsageRecords(account.getId(), startDate, endDate);
final List<UsageBackupVO> usageBackups = usageBackupDao.getUsageRecords(account.getId(), startDate, endDate);
if (usageBackups == null || usageBackups.isEmpty()) {
LOGGER.debug("No VM Backup usage for this period");
logger.debug("No VM Backup usage for this period");
return true;
}
@ -91,7 +77,7 @@ public class BackupUsageParser {
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);
usageDao.persist(usageRecord);
}
return true;

View File

@ -19,47 +19,32 @@ package com.cloud.usage.parser;
import com.cloud.usage.BucketStatisticsVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.BucketStatisticsDao;
import com.cloud.usage.dao.UsageDao;
import com.cloud.user.AccountVO;
import org.apache.cloudstack.usage.UsageTypes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Component
public class BucketUsageParser {
public static final Logger LOGGER = LogManager.getLogger(BucketUsageParser.class);
private static UsageDao s_usageDao;
private static BucketStatisticsDao s_bucketStatisticsDao;
public class BucketUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private BucketStatisticsDao _bucketStatisticsDao;
private BucketStatisticsDao bucketStatisticsDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_bucketStatisticsDao = _bucketStatisticsDao;
@Override
public String getParserName() {
return "Bucket";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all Bucket usage events for account {}", account);
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
List<BucketStatisticsVO> BucketStatisticsVOs = s_bucketStatisticsDao.listBy(account.getId());
List<BucketStatisticsVO> BucketStatisticsVOs = bucketStatisticsDao.listBy(account.getId());
List<UsageVO> usageRecords = new ArrayList<>();
for (BucketStatisticsVO bucketStatistics : BucketStatisticsVOs) {
@ -72,7 +57,7 @@ public class BucketUsageParser {
}
}
s_usageDao.saveUsageRecords(usageRecords);
usageDao.saveUsageRecords(usageRecords);
return true;
}

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageIPAddressVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageIPAddressDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class IPAddressUsageParser {
protected static Logger LOGGER = LogManager.getLogger(IPAddressUsageParser.class);
private static UsageDao s_usageDao;
private static UsageIPAddressDao s_usageIPAddressDao;
public class IPAddressUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageIPAddressDao _usageIPAddressDao;
private UsageIPAddressDao usageIPAddressDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageIPAddressDao = _usageIPAddressDao;
@Override
public String getParserName() {
return "IP Address";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing IP Address usage for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,10 +57,10 @@ public class IPAddressUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageIPAddressVO> usageIPAddress = s_usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
List<UsageIPAddressVO> usageIPAddress = usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
if (usageIPAddress.isEmpty()) {
LOGGER.debug("No IP Address usage for this period");
logger.debug("No IP Address usage for this period");
return true;
}
@ -128,7 +114,7 @@ public class IPAddressUsageParser {
return true;
}
private static void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) {
private void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) {
Pair<Long, Long> ipUsageInfo = usageDataMap.get(key);
if (ipUsageInfo == null) {
ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration));
@ -140,18 +126,17 @@ public class IPAddressUsageParser {
usageDataMap.put(key, ipUsageInfo);
}
private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long ipId, String ipAddress,
private void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long ipId, String ipAddress,
boolean isSourceNat, boolean isSystem, boolean isHidden) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total usage time " + runningTime + "ms");
}
logger.debug("Total usage time {} ms" , runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating IP usage record with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating IP usage record with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
ipId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -162,7 +147,7 @@ public class IPAddressUsageParser {
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);
usageDao.persist(usageRecord);
}
private static class IpInfo {

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageLoadBalancerPolicyVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageLoadBalancerPolicyDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class LoadBalancerUsageParser {
protected static Logger LOGGER = LogManager.getLogger(LoadBalancerUsageParser.class);
private static UsageDao s_usageDao;
private static UsageLoadBalancerPolicyDao s_usageLoadBalancerPolicyDao;
public class LoadBalancerUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageLoadBalancerPolicyDao _usageLoadBalancerPolicyDao;
private UsageLoadBalancerPolicyDao usageLoadBalancerPolicyDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageLoadBalancerPolicyDao = _usageLoadBalancerPolicyDao;
@Override
public String getParserName() {
return "Load Balancer Policy";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all LoadBalancerPolicy usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,10 +57,10 @@ public class LoadBalancerUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageLoadBalancerPolicyVO> usageLBs = s_usageLoadBalancerPolicyDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsageLoadBalancerPolicyVO> usageLBs = usageLoadBalancerPolicyDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageLBs.isEmpty()) {
LOGGER.debug("No load balancer usage events for this period");
logger.debug("No Load Balancer usage events for this period");
return true;
}
@ -125,7 +111,7 @@ public class LoadBalancerUsageParser {
return true;
}
private static void updateLBUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long lbId, long duration) {
private void updateLBUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long lbId, long duration) {
Pair<Long, Long> lbUsageInfo = usageDataMap.get(key);
if (lbUsageInfo == null) {
lbUsageInfo = new Pair<Long, Long>(new Long(lbId), new Long(duration));
@ -137,18 +123,18 @@ public class LoadBalancerUsageParser {
usageDataMap.put(key, lbUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long lbId, long zoneId) {
private void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long lbId, long zoneId) {
// Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating usage record for load balancer with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating usage record for load balancer with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
lbId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -159,7 +145,7 @@ public class LoadBalancerUsageParser {
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);
usageDao.persist(usageRecord);
}
private static class LBInfo {

View File

@ -22,46 +22,33 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageNetworkOfferingVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageNetworkOfferingDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class NetworkOfferingUsageParser {
protected static Logger LOGGER = LogManager.getLogger(NetworkOfferingUsageParser.class);
private static UsageDao s_usageDao;
private static UsageNetworkOfferingDao s_usageNetworkOfferingDao;
public class NetworkOfferingUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageNetworkOfferingDao _usageNetworkOfferingDao;
private UsageNetworkOfferingDao usageNetworkOfferingDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageNetworkOfferingDao = _usageNetworkOfferingDao;
@Override
public String getParserName() {
return "Network Offering";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all NetworkOffering usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,10 +58,10 @@ public class NetworkOfferingUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageNetworkOfferingVO> usageNOs = s_usageNetworkOfferingDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsageNetworkOfferingVO> usageNOs = usageNetworkOfferingDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageNOs.isEmpty()) {
LOGGER.debug("No NetworkOffering usage events for this period");
logger.debug("No NetworkOffering usage events for this period");
return true;
}
@ -126,7 +113,7 @@ public class NetworkOfferingUsageParser {
return true;
}
private static void updateNOUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
private void updateNOUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
Pair<Long, Long> noUsageInfo = usageDataMap.get(key);
if (noUsageInfo == null) {
noUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
@ -138,19 +125,18 @@ public class NetworkOfferingUsageParser {
usageDataMap.put(key, noUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long noId, long zoneId,
private 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 (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating network offering usage record for id [{}], vm [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating network offering usage record for id [{}], vm [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
noId, vmId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -161,7 +147,7 @@ public class NetworkOfferingUsageParser {
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);
usageDao.persist(usageRecord);
}
private static class NOInfo {

View File

@ -22,20 +22,16 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageNetworkVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageNetworkDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.db.SearchCriteria;
@ -43,38 +39,27 @@ import com.cloud.utils.db.SearchCriteria;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component
public class NetworkUsageParser {
protected static Logger LOGGER = LogManager.getLogger(NetworkUsageParser.class);
private static UsageDao s_usageDao;
private static UsageNetworkDao s_usageNetworkDao;
public class NetworkUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageNetworkDao _usageNetworkDao;
private UsageNetworkDao usageNetworkDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageNetworkDao = _usageNetworkDao;
@Override
public String getParserName() {
return "Network";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all Network usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
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();
SearchCriteria<UsageNetworkVO> sc = 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);
List<UsageNetworkVO> usageNetworkVOs = usageNetworkDao.search(sc, null);
Map<String, NetworkInfo> networkUsageByZone = new HashMap<String, NetworkInfo>();
@ -105,7 +90,7 @@ public class NetworkUsageParser {
long totalBytesReceived = networkInfo.getBytesRcvd();
if ((totalBytesSent > 0L) || (totalBytesReceived > 0L)) {
LOGGER.debug("Creating usage record, total bytes sent [{}], total bytes received [{}], startDate [{}], and endDate [{}], for account [{}] in " +
logger.debug("Creating usage record, total bytes sent [{}], total bytes received [{}], startDate [{}], and endDate [{}], for account [{}] in " +
"availability zone [{}].", toHumanReadableSize(totalBytesSent), toHumanReadableSize(totalBytesReceived),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId(), networkInfo.getZoneId());
@ -135,13 +120,11 @@ public class NetworkUsageParser {
usageRecords.add(usageRecord);
} else {
// Don't charge anything if there were zero bytes processed
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("No usage record (0 bytes used) generated for account: " + account.getId());
}
logger.debug("No usage record (0 bytes used) generated for account: [{}]", account.getUuid());
}
}
s_usageDao.saveUsageRecords(usageRecords);
usageDao.saveUsageRecords(usageRecords);
return true;
}

View File

@ -18,16 +18,12 @@ package com.cloud.usage.parser;
import com.cloud.usage.UsageNetworksVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageNetworksDao;
import com.cloud.user.AccountVO;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.cloudstack.usage.UsageTypes;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import java.text.DecimalFormat;
@ -35,32 +31,24 @@ import java.util.Date;
import java.util.List;
@Component
public class NetworksUsageParser {
private static final Logger LOGGER = LogManager.getLogger(NetworksUsageParser.class.getName());
public class NetworksUsageParser extends UsageParser {
@Inject
private UsageNetworksDao networksDao;
@Inject
private UsageDao usageDao;
private static UsageDao staticUsageDao;
private static UsageNetworksDao staticNetworksDao;
@PostConstruct
void init() {
staticUsageDao = usageDao;
staticNetworksDao = networksDao;
@Override
public String getParserName() {
return "Networks";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all networks usage events for account {}", account);
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
final List<UsageNetworksVO> usageNetworksVO = staticNetworksDao.getUsageRecords(account.getId(), startDate, endDate);
final List<UsageNetworksVO> usageNetworksVO = networksDao.getUsageRecords(account.getId(), startDate, endDate);
if (CollectionUtils.isEmpty(usageNetworksVO)) {
LOGGER.debug(String.format("Cannot find any VPC usage for account [%s] in period between [%s] and [%s].", account, startDate, endDate));
logger.debug("Cannot find any Networks usage for account [{}] in period between [{}] and [{}].", account, startDate, endDate);
return true;
}
@ -83,8 +71,8 @@ public class NetworksUsageParser {
long networkId = usageNetwork.getNetworkId();
long networkOfferingId = usageNetwork.getNetworkOfferingId();
LOGGER.debug(String.format("Creating network usage record with id [%s], network offering [%s], usage [%s], startDate [%s], and endDate [%s], for account [%s].",
networkId, networkOfferingId, usageDisplay, startDate, endDate, account));
logger.debug("Creating network usage record with id [{}], network offering [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
networkId, networkOfferingId, usageDisplay, startDate, endDate, account.getId());
String description = String.format("Network usage for network ID: %d, network offering: %d", usageNetwork.getNetworkId(), usageNetwork.getNetworkOfferingId());
UsageVO usageRecord =
@ -92,7 +80,7 @@ public class NetworksUsageParser {
UsageTypes.NETWORK, (double) usage, null, null, usageNetwork.getNetworkOfferingId(), null, usageNetwork.getNetworkId(),
(long)0, null, startDate, endDate);
usageRecord.setState(usageNetwork.getState());
staticUsageDao.persist(usageRecord);
usageDao.persist(usageRecord);
}
return true;

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsagePortForwardingRuleVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsagePortForwardingRuleDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class PortForwardingUsageParser {
protected static Logger LOGGER = LogManager.getLogger(PortForwardingUsageParser.class);
private static UsageDao s_usageDao;
private static UsagePortForwardingRuleDao s_usagePFRuleDao;
public class PortForwardingUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsagePortForwardingRuleDao _usagePFRuleDao;
private UsagePortForwardingRuleDao usagePFRuleDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usagePFRuleDao = _usagePFRuleDao;
@Override
public String getParserName() {
return "Port Forwarding Rule";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all PortForwardingRule usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,10 +57,10 @@ public class PortForwardingUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsagePortForwardingRuleVO> usagePFs = s_usagePFRuleDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsagePortForwardingRuleVO> usagePFs = usagePFRuleDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usagePFs.isEmpty()) {
LOGGER.debug("No port forwarding usage events for this period");
logger.debug("No port forwarding usage events for this period");
return true;
}
@ -125,7 +111,7 @@ public class PortForwardingUsageParser {
return true;
}
private static void updatePFUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long pfId, long duration) {
private void updatePFUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long pfId, long duration) {
Pair<Long, Long> pfUsageInfo = usageDataMap.get(key);
if (pfUsageInfo == null) {
pfUsageInfo = new Pair<Long, Long>(new Long(pfId), new Long(duration));
@ -137,18 +123,16 @@ public class PortForwardingUsageParser {
usageDataMap.put(key, pfUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long pfId, long zoneId) {
private void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long pfId, long zoneId) {
// Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating usage record for port forwarding rule [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating usage record for port forwarding rule [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
pfId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -159,7 +143,7 @@ public class PortForwardingUsageParser {
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);
usageDao.persist(usageRecord);
}
private static class PFInfo {

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageSecurityGroupVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageSecurityGroupDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class SecurityGroupUsageParser {
protected static Logger LOGGER = LogManager.getLogger(SecurityGroupUsageParser.class);
private static UsageDao s_usageDao;
private static UsageSecurityGroupDao s_usageSecurityGroupDao;
public class SecurityGroupUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageSecurityGroupDao _usageSecurityGroupDao;
private UsageSecurityGroupDao usageSecurityGroupDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageSecurityGroupDao = _usageSecurityGroupDao;
@Override
public String getParserName() {
return "Security Group";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all SecurityGroup usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,10 +57,10 @@ public class SecurityGroupUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageSecurityGroupVO> usageSGs = s_usageSecurityGroupDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsageSecurityGroupVO> usageSGs = usageSecurityGroupDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageSGs.isEmpty()) {
LOGGER.debug("No SecurityGroup usage events for this period");
logger.debug("No SecurityGroup usage events for this period");
return true;
}
@ -126,7 +112,7 @@ public class SecurityGroupUsageParser {
return true;
}
private static void updateSGUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
private void updateSGUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
Pair<Long, Long> sgUsageInfo = usageDataMap.get(key);
if (sgUsageInfo == null) {
sgUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
@ -138,18 +124,16 @@ public class SecurityGroupUsageParser {
usageDataMap.put(key, sgUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long sgId, long zoneId) {
private 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 (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating security group usage record for id [{}], vm [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating security group usage record for id [{}], vm [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
sgId, vmId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -159,7 +143,7 @@ public class SecurityGroupUsageParser {
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);
usageDao.persist(usageRecord);
}
private static class SGInfo {

View File

@ -22,13 +22,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
@ -36,7 +33,6 @@ import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.StorageTypes;
import com.cloud.usage.UsageStorageVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageStorageDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@ -44,27 +40,17 @@ import com.cloud.utils.Pair;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component
public class StorageUsageParser {
protected static Logger LOGGER = LogManager.getLogger(StorageUsageParser.class);
private static UsageDao s_usageDao;
private static UsageStorageDao s_usageStorageDao;
public class StorageUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageStorageDao _usageStorageDao;
private UsageStorageDao usageStorageDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageStorageDao = _usageStorageDao;
@Override
public String getParserName() {
return "Storage";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all Storage usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -74,10 +60,10 @@ public class StorageUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageStorageVO> usageUsageStorages = s_usageStorageDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsageStorageVO> usageUsageStorages = usageStorageDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageUsageStorages.isEmpty()) {
LOGGER.debug("No Storage usage events for this period");
logger.debug("No Storage usage events for this period");
return true;
}
@ -137,7 +123,7 @@ public class StorageUsageParser {
return true;
}
private static void updateStorageUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long storageId, long duration) {
private void updateStorageUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long storageId, long duration) {
Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
if (volUsageInfo == null) {
volUsageInfo = new Pair<Long, Long>(new Long(storageId), new Long(duration));
@ -149,19 +135,17 @@ public class StorageUsageParser {
usageDataMap.put(key, volUsageInfo);
}
private static void createUsageRecord(long zoneId, int type, long runningTime, Date startDate, Date endDate, AccountVO account, long storageId, Long sourceId,
private 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 (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating Storage usage record for type [{}], with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating Storage usage record for type [{}], with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
type, storageId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -199,7 +183,7 @@ public class StorageUsageParser {
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);
usageDao.persist(usageRecord);
}
private static class StorageInfo {

View File

@ -16,21 +16,30 @@
// under the License.
package com.cloud.usage.parser;
import com.cloud.usage.dao.UsageDao;
import com.cloud.user.AccountVO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.inject.Inject;
import java.util.Date;
public abstract class UsageParser {
Logger logger = LogManager.getLogger(getClass());
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
@Inject
UsageDao usageDao;
public abstract class UsageParser extends ManagedContextRunnable {
@Override
protected void runInContext() {
try {
parse(null);
} catch (Exception e) {
logger.warn("Error while parsing usage events", e);
}
private void beforeParse(AccountVO account) {
logger.debug("Parsing all {} usage events for account: [{}]", getParserName(), account);
}
public abstract void parse(Date endDate);
public abstract String getParserName();
protected abstract boolean parse(AccountVO account, Date startDate, Date endDate);
public boolean doParsing(AccountVO account, Date startDate, Date endDate) {
beforeParse(account);
return parse(account, startDate, endDate);
}
}

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import org.apache.commons.lang3.StringUtils;
import com.cloud.usage.UsageVMInstanceVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMInstanceDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class VMInstanceUsageParser {
protected static Logger LOGGER = LogManager.getLogger(VMInstanceUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVMInstanceDao s_usageInstanceDao;
public class VMInstanceUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;;
@Inject
private UsageVMInstanceDao _usageInstanceDao;
private UsageVMInstanceDao usageInstanceDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageInstanceDao = _usageInstanceDao;
@Override
public String getParserName() {
return "VM Instance";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all VMInstance usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,7 +57,7 @@ public class VMInstanceUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageVMInstanceVO> usageInstances = s_usageInstanceDao.getUsageRecords(account.getId(), startDate, endDate);
List<UsageVMInstanceVO> usageInstances = usageInstanceDao.getUsageRecords(account.getId(), startDate, endDate);
//ToDo: Add domainID for getting usage records
// This map has both the running time *and* the usage amount.
@ -151,7 +137,7 @@ public class VMInstanceUsageParser {
return true;
}
private static void updateVmUsageData(Map<String, Pair<String, Long>> usageDataMap, String key, String vmName, long duration) {
private void updateVmUsageData(Map<String, Pair<String, Long>> usageDataMap, String key, String vmName, long duration) {
Pair<String, Long> vmUsageInfo = usageDataMap.get(key);
if (vmUsageInfo == null) {
vmUsageInfo = new Pair<String, Long>(vmName, new Long(duration));
@ -163,19 +149,17 @@ public class VMInstanceUsageParser {
usageDataMap.put(key, vmUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId,
private void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId,
long serviceOfferingId, long templateId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
// Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating VM usage record for vm [{}], type [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating VM usage record for vm [{}], type [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
vmName, type, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -190,7 +174,7 @@ public class VMInstanceUsageParser {
UsageVO usageRecord =
new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId),
vmName, cpuCores, cpuSpeed, memory, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
s_usageDao.persist(usageRecord);
usageDao.persist(usageRecord);
}
private static class VMInfo {

View File

@ -22,65 +22,51 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageSnapshotOnPrimaryVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMSnapshotOnPrimaryDao;
import com.cloud.user.AccountVO;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component
public class VMSnapshotOnPrimaryParser {
protected static Logger LOGGER = LogManager.getLogger(VMSnapshotOnPrimaryParser.class);
private static UsageDao s_usageDao;
private static UsageVMSnapshotOnPrimaryDao s_usageSnapshotOnPrimaryDao;
public class VMSnapshotOnPrimaryParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageVMSnapshotOnPrimaryDao _usageSnapshotOnPrimaryDao;
private UsageVMSnapshotOnPrimaryDao usageSnapshotOnPrimaryDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageSnapshotOnPrimaryDao = _usageSnapshotOnPrimaryDao;
@Override
public String getParserName() {
return "VM Snapshot on Primary";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all VmSnapshot on primary usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
List<UsageSnapshotOnPrimaryVO> usageUsageVMSnapshots = s_usageSnapshotOnPrimaryDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
List<UsageSnapshotOnPrimaryVO> usageUsageVMSnapshots = usageSnapshotOnPrimaryDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
if (usageUsageVMSnapshots.isEmpty()) {
LOGGER.debug("No VM snapshot on primary usage events for this period");
logger.debug("No VM snapshot on primary usage events for this period");
return true;
}
Map<String, UsageSnapshotOnPrimaryVO> unprocessedUsage = new HashMap<String, UsageSnapshotOnPrimaryVO>();
for (UsageSnapshotOnPrimaryVO usageRec : usageUsageVMSnapshots) {
LOGGER.debug("usageRec for VMsnap on primary " + usageRec.toString());
logger.debug("usageRec for VMsnap on primary [{}]", usageRec.toString());
String key = usageRec.getName();
if (usageRec.getPhysicalSize() == 0) {
usageRec.setDeleted(new Date());
s_usageSnapshotOnPrimaryDao.updateDeleted(usageRec);
usageSnapshotOnPrimaryDao.updateDeleted(usageRec);
} else {
unprocessedUsage.put(key, usageRec);
}
@ -95,7 +81,7 @@ public class VMSnapshotOnPrimaryParser {
Date endDateEffective = endDate;
if (usageRec.getDeleted() != null && usageRec.getDeleted().before(endDate)){
endDateEffective = usageRec.getDeleted();
LOGGER.debug("Remoevd vm snapshot found endDateEffective " + endDateEffective + " period end data " + endDate);
logger.debug("Removed vm snapshot found endDateEffective [{}] period end data [{}]", endDateEffective, endDate);
}
long duration = (endDateEffective.getTime() - created.getTime()) + 1;
createUsageRecord(UsageTypes.VM_SNAPSHOT_ON_PRIMARY, duration, created, endDateEffective, account, usageRec.getVolumeId(), usageRec.getName(), usageRec.getZoneId(),
@ -105,19 +91,17 @@ public class VMSnapshotOnPrimaryParser {
return true;
}
private static void createUsageRecord(int usageType, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String name, long zoneId, long virtualSize,
private 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 (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating usage record for VMSnapshot with id [{}] in primary, vm [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating usage record for VMSnapshot with id [{}] in primary, vm [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
vmSnapshotId, vmId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -127,7 +111,7 @@ public class VMSnapshotOnPrimaryParser {
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);
usageDao.persist(usageRecord);
}
}

View File

@ -22,55 +22,41 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVMSnapshotVO;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMSnapshotDao;
import com.cloud.user.AccountVO;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component
public class VMSnapshotUsageParser {
protected static Logger LOGGER = LogManager.getLogger(VMSnapshotUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVMSnapshotDao s_usageVMSnapshotDao;
public class VMSnapshotUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageVMSnapshotDao _usageVMSnapshotDao;
private UsageVMSnapshotDao usageVMSnapshotDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageVMSnapshotDao = _usageVMSnapshotDao;
@Override
public String getParserName() {
return "VM Snapshot";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all VmSnapshot volume usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
List<UsageVMSnapshotVO> usageUsageVMSnapshots = s_usageVMSnapshotDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
List<UsageVMSnapshotVO> usageUsageVMSnapshots = usageVMSnapshotDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
if (usageUsageVMSnapshots.isEmpty()) {
LOGGER.debug("No VM snapshot usage events for this period");
logger.debug("No VM snapshot usage events for this period");
return true;
}
@ -84,7 +70,7 @@ public class VMSnapshotUsageParser {
unprocessedUsage.put(key, usageRec);
continue;
}
UsageVMSnapshotVO previousEvent = s_usageVMSnapshotDao.getPreviousUsageRecord(usageRec);
UsageVMSnapshotVO previousEvent = usageVMSnapshotDao.getPreviousUsageRecord(usageRec);
if (previousEvent == null || previousEvent.getSize() == 0) {
unprocessedUsage.put(key, usageRec);
continue;
@ -101,11 +87,11 @@ public class VMSnapshotUsageParser {
createUsageRecord(UsageTypes.VM_SNAPSHOT, duration, previousCreated, createDate, account, volId, zoneId, previousEvent.getDiskOfferingId(), vmId,
previousEvent.getSize(), usageRec.getVmSnapshotId());
previousEvent.setProcessed(new Date());
s_usageVMSnapshotDao.update(previousEvent);
usageVMSnapshotDao.update(previousEvent);
if (usageRec.getSize() == 0) {
usageRec.setProcessed(new Date());
s_usageVMSnapshotDao.update(usageRec);
usageVMSnapshotDao.update(usageRec);
} else
unprocessedUsage.put(key, usageRec);
}
@ -124,19 +110,17 @@ public class VMSnapshotUsageParser {
return true;
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId, Long vmId,
private 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 (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating usage record for VMSnapshot with id [{}], vol [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating usage record for VMSnapshot with id [{}], vol [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
vmSnapshotId, volId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -152,7 +136,7 @@ public class VMSnapshotUsageParser {
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);
usageDao.persist(usageRecord);
}
}

View File

@ -22,54 +22,40 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVO;
import com.cloud.usage.UsageVPNUserVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVPNUserDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class VPNUserUsageParser {
protected static Logger LOGGER = LogManager.getLogger(VPNUserUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVPNUserDao s_usageVPNUserDao;
public class VPNUserUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageVPNUserDao _usageVPNUserDao;
private UsageVPNUserDao usageVPNUserDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageVPNUserDao = _usageVPNUserDao;
@Override
public String getParserName() {
return "VPN User";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all VPN user usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
List<UsageVPNUserVO> usageVUs = s_usageVPNUserDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsageVPNUserVO> usageVUs = usageVPNUserDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageVUs.isEmpty()) {
LOGGER.debug("No VPN user usage events for this period");
logger.debug("No VPN user usage events for this period");
return true;
}
@ -121,7 +107,7 @@ public class VPNUserUsageParser {
return true;
}
private static void updateVUUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long userId, long duration) {
private void updateVUUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long userId, long duration) {
Pair<Long, Long> vuUsageInfo = usageDataMap.get(key);
if (vuUsageInfo == null) {
vuUsageInfo = new Pair<Long, Long>(new Long(userId), new Long(duration));
@ -133,18 +119,16 @@ public class VPNUserUsageParser {
usageDataMap.put(key, vuUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long userId, String userName, long zoneId) {
private void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long userId, String userName, long zoneId) {
// Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating usage record for VPN user [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating usage record for VPN user [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
userId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -154,7 +138,7 @@ public class VPNUserUsageParser {
UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, userId, null,
startDate, endDate);
s_usageDao.persist(usageRecord);
usageDao.persist(usageRecord);
}
private static class VUInfo {

View File

@ -22,20 +22,16 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVO;
import com.cloud.usage.UsageVmDiskVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVmDiskDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.db.SearchCriteria;
@ -43,38 +39,27 @@ import com.cloud.utils.db.SearchCriteria;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component
public class VmDiskUsageParser {
protected static Logger LOGGER = LogManager.getLogger(VmDiskUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVmDiskDao s_usageVmDiskDao;
public class VmDiskUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageVmDiskDao _usageVmDiskDao;
private UsageVmDiskDao usageVmDiskDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageVmDiskDao = _usageVmDiskDao;
@Override
public String getParserName() {
return "VM Disk";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all Vm Disk usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
// - query usage_disk table for all entries for userId with
// event_date in the given range
SearchCriteria<UsageVmDiskVO> sc = s_usageVmDiskDao.createSearchCriteria();
SearchCriteria<UsageVmDiskVO> sc = usageVmDiskDao.createSearchCriteria();
sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
List<UsageVmDiskVO> usageVmDiskVOs = s_usageVmDiskDao.search(sc, null);
List<UsageVmDiskVO> usageVmDiskVOs = usageVmDiskDao.search(sc, null);
Map<String, VmDiskInfo> vmDiskUsageByZone = new HashMap<String, VmDiskInfo>();
@ -110,7 +95,7 @@ public class VmDiskUsageParser {
long bytesWrite = vmDiskInfo.getBytesWrite();
if ((ioRead > 0L) || (ioWrite > 0L) || (bytesRead > 0L) || (bytesWrite > 0L)) {
LOGGER.debug("Creating vm disk usage record, io read [{}], io write [{}], bytes read [{}], bytes write [{}], startDate [{}], and endDate [{}], " +
logger.debug("Creating vm disk usage record, io read [{}], io write [{}], bytes read [{}], bytes write [{}], startDate [{}], and endDate [{}], " +
"for account [{}] in availability zone [{}].", toHumanReadableSize(ioRead), toHumanReadableSize(ioWrite), toHumanReadableSize(bytesRead),
toHumanReadableSize(bytesWrite), DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId(), vmDiskInfo.getZoneId());
@ -162,13 +147,11 @@ public class VmDiskUsageParser {
} else {
// Don't charge anything if there were zero bytes processed
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("No vm disk usage record (0 bytes used) generated for account: " + account.getId());
}
logger.debug("No vm disk usage record (0 bytes used) generated for account: [{}]", account.getId());
}
}
s_usageDao.saveUsageRecords(usageRecords);
usageDao.saveUsageRecords(usageRecords);
return true;
}

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVO;
import com.cloud.usage.UsageVolumeVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVolumeDao;
import com.cloud.user.AccountVO;
import com.cloud.utils.Pair;
@Component
public class VolumeUsageParser {
protected static Logger LOGGER = LogManager.getLogger(VolumeUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVolumeDao s_usageVolumeDao;
public class VolumeUsageParser extends UsageParser {
@Inject
private UsageDao _usageDao;
@Inject
private UsageVolumeDao _usageVolumeDao;
private UsageVolumeDao usageVolumeDao;
@PostConstruct
void init() {
s_usageDao = _usageDao;
s_usageVolumeDao = _usageVolumeDao;
@Override
public String getParserName() {
return "Volume";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Parsing all Volume usage events for account: " + account.getId());
}
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
@ -71,10 +57,10 @@ public class VolumeUsageParser {
// - look for an entry for accountId with end date in the given range
// - look for an entry for accountId with end date null (currently running vm or owned IP)
// - look for an entry for accountId with start date before given range *and* end date after given range
List<UsageVolumeVO> usageUsageVols = s_usageVolumeDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
List<UsageVolumeVO> usageUsageVols = usageVolumeDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
if (usageUsageVols.isEmpty()) {
LOGGER.debug("No volume usage events for this period");
logger.debug("No volume usage events for this period");
return true;
}
@ -131,7 +117,7 @@ public class VolumeUsageParser {
return true;
}
private static void updateVolUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long volId, long duration) {
private void updateVolUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long volId, long duration) {
Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
if (volUsageInfo == null) {
volUsageInfo = new Pair<Long, Long>(new Long(volId), new Long(duration));
@ -143,19 +129,17 @@ public class VolumeUsageParser {
usageDataMap.put(key, volUsageInfo);
}
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId,
private void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId,
Long templateId, long size) {
// Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Total running time " + runningTime + "ms");
}
logger.debug("Total running time {} ms", runningTime);
float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage);
LOGGER.debug("Creating Volume usage record for vol [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
logger.debug("Creating Volume usage record for vol [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
volId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -170,7 +154,7 @@ public class VolumeUsageParser {
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, doId, templateId, volId,
size, startDate, endDate);
s_usageDao.persist(usageRecord);
usageDao.persist(usageRecord);
}
private static class VolInfo {

View File

@ -17,15 +17,11 @@
package com.cloud.usage.parser;
import com.cloud.usage.UsageVpcVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageVpcDao;
import com.cloud.user.AccountVO;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.cloudstack.usage.UsageTypes;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component;
import java.text.DecimalFormat;
@ -33,31 +29,24 @@ import java.util.Date;
import java.util.List;
@Component
public class VpcUsageParser {
private static final Logger LOGGER = LogManager.getLogger(VpcUsageParser.class.getName());
public class VpcUsageParser extends UsageParser {
@Inject
private UsageVpcDao vpcDao;
@Inject
private UsageDao usageDao;
private static UsageDao s_usageDao;
private static UsageVpcDao s_usageVpcDao;
@PostConstruct
void init() {
s_usageDao = usageDao;
s_usageVpcDao = vpcDao;
@Override
public String getParserName() {
return "VPC";
}
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all VPC usage events for account {}", account);
@Override
protected boolean parse(AccountVO account, Date startDate, Date endDate) {
if ((endDate == null) || endDate.after(new Date())) {
endDate = new Date();
}
final List<UsageVpcVO> usageVPCs = s_usageVpcDao.getUsageRecords(account.getId(), startDate, endDate);
final List<UsageVpcVO> usageVPCs = vpcDao.getUsageRecords(account.getId(), startDate, endDate);
if (usageVPCs == null || usageVPCs.isEmpty()) {
LOGGER.debug(String.format("Cannot find any VPC usage for account [%s] in period between [%s] and [%s].", account, startDate, endDate));
logger.debug("Cannot find any VPC usage for account [{}] in period between [{}] and [{}].", account, startDate, endDate);
return true;
}
@ -79,15 +68,15 @@ public class VpcUsageParser {
String usageDisplay = dFormat.format(usage);
long vpcId = usageVPC.getVpcId();
LOGGER.debug("Creating VPC usage record with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
vpcId, usageDisplay, startDate, endDate, account);
logger.debug("Creating VPC usage record with id [{}], usage [{}], startDate [{}], and endDate [{}], for account [{}].",
vpcId, usageDisplay, startDate, endDate, account.getId());
String description = String.format("VPC usage for VPC ID: %d", usageVPC.getVpcId());
UsageVO usageRecord =
new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs",
UsageTypes.VPC, (double) usage, null, null, null, null, usageVPC.getVpcId(),
(long)0, null, startDate, endDate);
s_usageDao.persist(usageRecord);
usageDao.persist(usageRecord);
}
return true;