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.network.Network;
import com.cloud.usage.dao.UsageNetworksDao; 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.network.vpc.Vpc;
import com.cloud.usage.dao.UsageVpcDao; import com.cloud.usage.dao.UsageVpcDao;
import com.cloud.usage.parser.VpcUsageParser;
import javax.inject.Inject; import javax.inject.Inject;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import javax.persistence.EntityExistsException; 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.UsageVPNUserDao;
import com.cloud.usage.dao.UsageVmDiskDao; import com.cloud.usage.dao.UsageVmDiskDao;
import com.cloud.usage.dao.UsageVolumeDao; 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.Account;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.user.UserStatisticsVO; import com.cloud.user.UserStatisticsVO;
@ -180,6 +164,9 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
@Inject @Inject
private UsageVpcDao usageVpcDao; private UsageVpcDao usageVpcDao;
@Inject
private List<UsageParser> usageParsers;
private String _version = null; private String _version = null;
private final Calendar _jobExecTime = Calendar.getInstance(); private final Calendar _jobExecTime = Calendar.getInstance();
private int _aggregationDuration = 0; private int _aggregationDuration = 0;
@ -198,6 +185,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
private Future _heartbeat = null; private Future _heartbeat = null;
private Future _sanity = null; private Future _sanity = null;
private boolean usageSnapshotSelection = false; private boolean usageSnapshotSelection = false;
private static TimeZone usageAggregationTimeZone = TimeZone.getTimeZone("GMT"); private static TimeZone usageAggregationTimeZone = TimeZone.getTimeZone("GMT");
public UsageManagerImpl() { public UsageManagerImpl() {
@ -954,114 +942,12 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
private boolean parseHelperTables(AccountVO account, Date currentStartDate, Date currentEndDate) { private boolean parseHelperTables(AccountVO account, Date currentStartDate, Date currentEndDate) {
boolean parsed = false; boolean parsed = false;
parsed = VMInstanceUsageParser.parse(account, currentStartDate, currentEndDate); for (UsageParser parser : usageParsers) {
if (logger.isDebugEnabled()) { parsed = parser.doParsing(account, currentStartDate, currentEndDate);
if (!parsed) {
logger.debug("vm usage instances successfully parsed? " + parsed + " (for account: " + account.getAccountName() + ", id: " + account.getId() + ")"); 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; return parsed;
} }

View File

@ -21,49 +21,35 @@ import java.text.DecimalFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.cloud.usage.UsageBackupVO; import com.cloud.usage.UsageBackupVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageBackupDao; import com.cloud.usage.dao.UsageBackupDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
@Component @Component
public class BackupUsageParser { public class BackupUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(BackupUsageParser.class);
private static UsageDao s_usageDao;
private static UsageBackupDao s_usageBackupDao;
@Inject
private UsageDao usageDao;
@Inject @Inject
private UsageBackupDao usageBackupDao; private UsageBackupDao usageBackupDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = usageDao; return "VM Backup";
s_usageBackupDao = usageBackupDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all VM Backup usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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()) { 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; return true;
} }
@ -91,7 +77,7 @@ public class BackupUsageParser {
new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs", new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs",
UsageTypes.BACKUP, new Double(usage), vmId, null, offeringId, null, vmId, UsageTypes.BACKUP, new Double(usage), vmId, null, offeringId, null, vmId,
usageBackup.getSize(), usageBackup.getProtectedSize(), startDate, endDate); usageBackup.getSize(), usageBackup.getProtectedSize(), startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
return true; return true;

View File

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

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageIPAddressVO; import com.cloud.usage.UsageIPAddressVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageIPAddressDao; import com.cloud.usage.dao.UsageIPAddressDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class IPAddressUsageParser { public class IPAddressUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(IPAddressUsageParser.class);
private static UsageDao s_usageDao;
private static UsageIPAddressDao s_usageIPAddressDao;
@Inject @Inject
private UsageDao _usageDao; private UsageIPAddressDao usageIPAddressDao;
@Inject
private UsageIPAddressDao _usageIPAddressDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "IP Address";
s_usageIPAddressDao = _usageIPAddressDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing IP Address usage for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { if (usageIPAddress.isEmpty()) {
LOGGER.debug("No IP Address usage for this period"); logger.debug("No IP Address usage for this period");
return true; return true;
} }
@ -128,7 +114,7 @@ public class IPAddressUsageParser {
return true; 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); Pair<Long, Long> ipUsageInfo = usageDataMap.get(key);
if (ipUsageInfo == null) { if (ipUsageInfo == null) {
ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration)); ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration));
@ -140,18 +126,17 @@ public class IPAddressUsageParser {
usageDataMap.put(key, ipUsageInfo); 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) { 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; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), ipId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -162,7 +147,7 @@ public class IPAddressUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), ipId, 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); (isSystem ? 1 : 0), (isSourceNat ? "SourceNat" : ""), startDate, endDate, isHidden);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class IpInfo { private static class IpInfo {

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageLoadBalancerPolicyVO; import com.cloud.usage.UsageLoadBalancerPolicyVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageLoadBalancerPolicyDao; import com.cloud.usage.dao.UsageLoadBalancerPolicyDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class LoadBalancerUsageParser { public class LoadBalancerUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(LoadBalancerUsageParser.class);
private static UsageDao s_usageDao;
private static UsageLoadBalancerPolicyDao s_usageLoadBalancerPolicyDao;
@Inject @Inject
private UsageDao _usageDao; private UsageLoadBalancerPolicyDao usageLoadBalancerPolicyDao;
@Inject
private UsageLoadBalancerPolicyDao _usageLoadBalancerPolicyDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "Load Balancer Policy";
s_usageLoadBalancerPolicyDao = _usageLoadBalancerPolicyDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all LoadBalancerPolicy usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { 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; return true;
} }
@ -125,7 +111,7 @@ public class LoadBalancerUsageParser {
return true; 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); Pair<Long, Long> lbUsageInfo = usageDataMap.get(key);
if (lbUsageInfo == null) { if (lbUsageInfo == null) {
lbUsageInfo = new Pair<Long, Long>(new Long(lbId), new Long(duration)); lbUsageInfo = new Pair<Long, Long>(new Long(lbId), new Long(duration));
@ -137,18 +123,18 @@ public class LoadBalancerUsageParser {
usageDataMap.put(key, lbUsageInfo); 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 // 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; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), lbId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -159,7 +145,7 @@ public class LoadBalancerUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, lbId, null, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, lbId, null,
startDate, endDate); startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class LBInfo { private static class LBInfo {

View File

@ -22,46 +22,33 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageNetworkOfferingVO; import com.cloud.usage.UsageNetworkOfferingVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageNetworkOfferingDao; import com.cloud.usage.dao.UsageNetworkOfferingDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class NetworkOfferingUsageParser { public class NetworkOfferingUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(NetworkOfferingUsageParser.class);
private static UsageDao s_usageDao;
private static UsageNetworkOfferingDao s_usageNetworkOfferingDao;
@Inject @Inject
private UsageDao _usageDao; private UsageNetworkOfferingDao usageNetworkOfferingDao;
@Inject
private UsageNetworkOfferingDao _usageNetworkOfferingDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "Network Offering";
s_usageNetworkOfferingDao = _usageNetworkOfferingDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all NetworkOffering usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { if (usageNOs.isEmpty()) {
LOGGER.debug("No NetworkOffering usage events for this period"); logger.debug("No NetworkOffering usage events for this period");
return true; return true;
} }
@ -126,7 +113,7 @@ public class NetworkOfferingUsageParser {
return true; 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); Pair<Long, Long> noUsageInfo = usageDataMap.get(key);
if (noUsageInfo == null) { if (noUsageInfo == null) {
noUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration)); noUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
@ -138,19 +125,18 @@ public class NetworkOfferingUsageParser {
usageDataMap.put(key, noUsageInfo); 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) { boolean isDefault) {
// Our smallest increment is hourly for now // 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; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), noId, vmId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -161,7 +147,7 @@ public class NetworkOfferingUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, noId, null, defaultNic, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, noId, null, defaultNic,
null, startDate, endDate); null, startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class NOInfo { private static class NOInfo {

View File

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

View File

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

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsagePortForwardingRuleVO; import com.cloud.usage.UsagePortForwardingRuleVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsagePortForwardingRuleDao; import com.cloud.usage.dao.UsagePortForwardingRuleDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class PortForwardingUsageParser { public class PortForwardingUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(PortForwardingUsageParser.class);
private static UsageDao s_usageDao;
private static UsagePortForwardingRuleDao s_usagePFRuleDao;
@Inject @Inject
private UsageDao _usageDao; private UsagePortForwardingRuleDao usagePFRuleDao;
@Inject
private UsagePortForwardingRuleDao _usagePFRuleDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "Port Forwarding Rule";
s_usagePFRuleDao = _usagePFRuleDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all PortForwardingRule usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { 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; return true;
} }
@ -125,7 +111,7 @@ public class PortForwardingUsageParser {
return true; 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); Pair<Long, Long> pfUsageInfo = usageDataMap.get(key);
if (pfUsageInfo == null) { if (pfUsageInfo == null) {
pfUsageInfo = new Pair<Long, Long>(new Long(pfId), new Long(duration)); pfUsageInfo = new Pair<Long, Long>(new Long(pfId), new Long(duration));
@ -137,18 +123,16 @@ public class PortForwardingUsageParser {
usageDataMap.put(key, pfUsageInfo); 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 // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), pfId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -159,7 +143,7 @@ public class PortForwardingUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, pfId, null, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, pfId, null,
startDate, endDate); startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class PFInfo { private static class PFInfo {

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageSecurityGroupVO; import com.cloud.usage.UsageSecurityGroupVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageSecurityGroupDao; import com.cloud.usage.dao.UsageSecurityGroupDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class SecurityGroupUsageParser { public class SecurityGroupUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(SecurityGroupUsageParser.class);
private static UsageDao s_usageDao;
private static UsageSecurityGroupDao s_usageSecurityGroupDao;
@Inject @Inject
private UsageDao _usageDao; private UsageSecurityGroupDao usageSecurityGroupDao;
@Inject
private UsageSecurityGroupDao _usageSecurityGroupDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "Security Group";
s_usageSecurityGroupDao = _usageSecurityGroupDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all SecurityGroup usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { if (usageSGs.isEmpty()) {
LOGGER.debug("No SecurityGroup usage events for this period"); logger.debug("No SecurityGroup usage events for this period");
return true; return true;
} }
@ -126,7 +112,7 @@ public class SecurityGroupUsageParser {
return true; 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); Pair<Long, Long> sgUsageInfo = usageDataMap.get(key);
if (sgUsageInfo == null) { if (sgUsageInfo == null) {
sgUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration)); sgUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
@ -138,18 +124,16 @@ public class SecurityGroupUsageParser {
usageDataMap.put(key, sgUsageInfo); 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 // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), sgId, vmId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -159,7 +143,7 @@ public class SecurityGroupUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, null, null, sgId, null, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, null, null, sgId, null,
startDate, endDate); startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class SGInfo { private static class SGInfo {

View File

@ -22,13 +22,10 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; 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.StorageTypes;
import com.cloud.usage.UsageStorageVO; import com.cloud.usage.UsageStorageVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageStorageDao; import com.cloud.usage.dao.UsageStorageDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@ -44,27 +40,17 @@ import com.cloud.utils.Pair;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize; import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component @Component
public class StorageUsageParser { public class StorageUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(StorageUsageParser.class);
private static UsageDao s_usageDao;
private static UsageStorageDao s_usageStorageDao;
@Inject @Inject
private UsageDao _usageDao; private UsageStorageDao usageStorageDao;
@Inject
private UsageStorageDao _usageStorageDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "Storage";
s_usageStorageDao = _usageStorageDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all Storage usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { if (usageUsageStorages.isEmpty()) {
LOGGER.debug("No Storage usage events for this period"); logger.debug("No Storage usage events for this period");
return true; return true;
} }
@ -137,7 +123,7 @@ public class StorageUsageParser {
return true; 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); Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
if (volUsageInfo == null) { if (volUsageInfo == null) {
volUsageInfo = new Pair<Long, Long>(new Long(storageId), new Long(duration)); volUsageInfo = new Pair<Long, Long>(new Long(storageId), new Long(duration));
@ -149,19 +135,17 @@ public class StorageUsageParser {
usageDataMap.put(key, volUsageInfo); 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) { long size, Long virtualSize) {
// Our smallest increment is hourly for now // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), type, storageId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -199,7 +183,7 @@ public class StorageUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usage_type, new Double(usage), null, null, null, tmplSourceId, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usage_type, new Double(usage), null, null, null, tmplSourceId,
storageId, size, virtualSize, startDate, endDate); storageId, size, virtualSize, startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class StorageInfo { private static class StorageInfo {

View File

@ -16,21 +16,30 @@
// under the License. // under the License.
package com.cloud.usage.parser; 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; 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 { private void beforeParse(AccountVO account) {
logger.debug("Parsing all {} usage events for account: [{}]", getParserName(), account);
@Override
protected void runInContext() {
try {
parse(null);
} catch (Exception e) {
logger.warn("Error while parsing usage events", e);
}
} }
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.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.cloud.usage.UsageVMInstanceVO; import com.cloud.usage.UsageVMInstanceVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMInstanceDao; import com.cloud.usage.dao.UsageVMInstanceDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class VMInstanceUsageParser { public class VMInstanceUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(VMInstanceUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVMInstanceDao s_usageInstanceDao;
@Inject @Inject
private UsageDao _usageDao;; private UsageVMInstanceDao usageInstanceDao;
@Inject
private UsageVMInstanceDao _usageInstanceDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "VM Instance";
s_usageInstanceDao = _usageInstanceDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all VMInstance usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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 //ToDo: Add domainID for getting usage records
// This map has both the running time *and* the usage amount. // This map has both the running time *and* the usage amount.
@ -151,7 +137,7 @@ public class VMInstanceUsageParser {
return true; 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); Pair<String, Long> vmUsageInfo = usageDataMap.get(key);
if (vmUsageInfo == null) { if (vmUsageInfo == null) {
vmUsageInfo = new Pair<String, Long>(vmName, new Long(duration)); vmUsageInfo = new Pair<String, Long>(vmName, new Long(duration));
@ -163,19 +149,17 @@ public class VMInstanceUsageParser {
usageDataMap.put(key, vmUsageInfo); 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) { long serviceOfferingId, long templateId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
// Our smallest increment is hourly for now // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), vmName, type, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -190,7 +174,7 @@ public class VMInstanceUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId), 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); 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 { private static class VMInfo {

View File

@ -22,65 +22,51 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageSnapshotOnPrimaryVO; import com.cloud.usage.UsageSnapshotOnPrimaryVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMSnapshotOnPrimaryDao; import com.cloud.usage.dao.UsageVMSnapshotOnPrimaryDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize; import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component @Component
public class VMSnapshotOnPrimaryParser { public class VMSnapshotOnPrimaryParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(VMSnapshotOnPrimaryParser.class);
private static UsageDao s_usageDao;
private static UsageVMSnapshotOnPrimaryDao s_usageSnapshotOnPrimaryDao;
@Inject @Inject
private UsageDao _usageDao; private UsageVMSnapshotOnPrimaryDao usageSnapshotOnPrimaryDao;
@Inject
private UsageVMSnapshotOnPrimaryDao _usageSnapshotOnPrimaryDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "VM Snapshot on Primary";
s_usageSnapshotOnPrimaryDao = _usageSnapshotOnPrimaryDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all VmSnapshot on primary usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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()) { 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; return true;
} }
Map<String, UsageSnapshotOnPrimaryVO> unprocessedUsage = new HashMap<String, UsageSnapshotOnPrimaryVO>(); Map<String, UsageSnapshotOnPrimaryVO> unprocessedUsage = new HashMap<String, UsageSnapshotOnPrimaryVO>();
for (UsageSnapshotOnPrimaryVO usageRec : usageUsageVMSnapshots) { 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(); String key = usageRec.getName();
if (usageRec.getPhysicalSize() == 0) { if (usageRec.getPhysicalSize() == 0) {
usageRec.setDeleted(new Date()); usageRec.setDeleted(new Date());
s_usageSnapshotOnPrimaryDao.updateDeleted(usageRec); usageSnapshotOnPrimaryDao.updateDeleted(usageRec);
} else { } else {
unprocessedUsage.put(key, usageRec); unprocessedUsage.put(key, usageRec);
} }
@ -95,7 +81,7 @@ public class VMSnapshotOnPrimaryParser {
Date endDateEffective = endDate; Date endDateEffective = endDate;
if (usageRec.getDeleted() != null && usageRec.getDeleted().before(endDate)){ if (usageRec.getDeleted() != null && usageRec.getDeleted().before(endDate)){
endDateEffective = usageRec.getDeleted(); 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; long duration = (endDateEffective.getTime() - created.getTime()) + 1;
createUsageRecord(UsageTypes.VM_SNAPSHOT_ON_PRIMARY, duration, created, endDateEffective, account, usageRec.getVolumeId(), usageRec.getName(), usageRec.getZoneId(), 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; 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) { long physicalSize, Long vmSnapshotId) {
// Our smallest increment is hourly for now // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), vmSnapshotId, vmId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); 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, 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); 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.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVMSnapshotVO; import com.cloud.usage.UsageVMSnapshotVO;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVMSnapshotDao; import com.cloud.usage.dao.UsageVMSnapshotDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import static com.cloud.utils.NumbersUtil.toHumanReadableSize; import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
@Component @Component
public class VMSnapshotUsageParser { public class VMSnapshotUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(VMSnapshotUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVMSnapshotDao s_usageVMSnapshotDao;
@Inject @Inject
private UsageDao _usageDao; private UsageVMSnapshotDao usageVMSnapshotDao;
@Inject
private UsageVMSnapshotDao _usageVMSnapshotDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "VM Snapshot";
s_usageVMSnapshotDao = _usageVMSnapshotDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all VmSnapshot volume usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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()) { 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; return true;
} }
@ -84,7 +70,7 @@ public class VMSnapshotUsageParser {
unprocessedUsage.put(key, usageRec); unprocessedUsage.put(key, usageRec);
continue; continue;
} }
UsageVMSnapshotVO previousEvent = s_usageVMSnapshotDao.getPreviousUsageRecord(usageRec); UsageVMSnapshotVO previousEvent = usageVMSnapshotDao.getPreviousUsageRecord(usageRec);
if (previousEvent == null || previousEvent.getSize() == 0) { if (previousEvent == null || previousEvent.getSize() == 0) {
unprocessedUsage.put(key, usageRec); unprocessedUsage.put(key, usageRec);
continue; continue;
@ -101,11 +87,11 @@ public class VMSnapshotUsageParser {
createUsageRecord(UsageTypes.VM_SNAPSHOT, duration, previousCreated, createDate, account, volId, zoneId, previousEvent.getDiskOfferingId(), vmId, createUsageRecord(UsageTypes.VM_SNAPSHOT, duration, previousCreated, createDate, account, volId, zoneId, previousEvent.getDiskOfferingId(), vmId,
previousEvent.getSize(), usageRec.getVmSnapshotId()); previousEvent.getSize(), usageRec.getVmSnapshotId());
previousEvent.setProcessed(new Date()); previousEvent.setProcessed(new Date());
s_usageVMSnapshotDao.update(previousEvent); usageVMSnapshotDao.update(previousEvent);
if (usageRec.getSize() == 0) { if (usageRec.getSize() == 0) {
usageRec.setProcessed(new Date()); usageRec.setProcessed(new Date());
s_usageVMSnapshotDao.update(usageRec); usageVMSnapshotDao.update(usageRec);
} else } else
unprocessedUsage.put(key, usageRec); unprocessedUsage.put(key, usageRec);
} }
@ -124,19 +110,17 @@ public class VMSnapshotUsageParser {
return true; 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) { long size, Long vmSnapshotId) {
// Our smallest increment is hourly for now // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), vmSnapshotId, volId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -152,7 +136,7 @@ public class VMSnapshotUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, doId, null, vmSnapshotId, size, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), vmId, null, doId, null, vmSnapshotId, size,
startDate, endDate); 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.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.UsageVPNUserVO; import com.cloud.usage.UsageVPNUserVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVPNUserDao; import com.cloud.usage.dao.UsageVPNUserDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class VPNUserUsageParser { public class VPNUserUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(VPNUserUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVPNUserDao s_usageVPNUserDao;
@Inject @Inject
private UsageDao _usageDao; private UsageVPNUserDao usageVPNUserDao;
@Inject
private UsageVPNUserDao _usageVPNUserDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "VPN User";
s_usageVPNUserDao = _usageVPNUserDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all VPN user usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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()) { 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; return true;
} }
@ -121,7 +107,7 @@ public class VPNUserUsageParser {
return true; 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); Pair<Long, Long> vuUsageInfo = usageDataMap.get(key);
if (vuUsageInfo == null) { if (vuUsageInfo == null) {
vuUsageInfo = new Pair<Long, Long>(new Long(userId), new Long(duration)); vuUsageInfo = new Pair<Long, Long>(new Long(userId), new Long(duration));
@ -133,18 +119,16 @@ public class VPNUserUsageParser {
usageDataMap.put(key, vuUsageInfo); 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 // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), userId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId());
@ -154,7 +138,7 @@ public class VPNUserUsageParser {
UsageVO usageRecord = UsageVO usageRecord =
new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, userId, null, new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, null, null, userId, null,
startDate, endDate); startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class VUInfo { private static class VUInfo {

View File

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

View File

@ -22,46 +22,32 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import com.cloud.usage.UsageManagerImpl; import com.cloud.usage.UsageManagerImpl;
import com.cloud.utils.DateUtil; import com.cloud.utils.DateUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.apache.cloudstack.usage.UsageTypes; import org.apache.cloudstack.usage.UsageTypes;
import com.cloud.usage.UsageVO; import com.cloud.usage.UsageVO;
import com.cloud.usage.UsageVolumeVO; import com.cloud.usage.UsageVolumeVO;
import com.cloud.usage.dao.UsageDao;
import com.cloud.usage.dao.UsageVolumeDao; import com.cloud.usage.dao.UsageVolumeDao;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.utils.Pair; import com.cloud.utils.Pair;
@Component @Component
public class VolumeUsageParser { public class VolumeUsageParser extends UsageParser {
protected static Logger LOGGER = LogManager.getLogger(VolumeUsageParser.class);
private static UsageDao s_usageDao;
private static UsageVolumeDao s_usageVolumeDao;
@Inject @Inject
private UsageDao _usageDao; private UsageVolumeDao usageVolumeDao;
@Inject
private UsageVolumeDao _usageVolumeDao;
@PostConstruct @Override
void init() { public String getParserName() {
s_usageDao = _usageDao; return "Volume";
s_usageVolumeDao = _usageVolumeDao;
} }
public static boolean parse(AccountVO account, Date startDate, Date endDate) { @Override
if (LOGGER.isDebugEnabled()) { protected boolean parse(AccountVO account, Date startDate, Date endDate) {
LOGGER.debug("Parsing all Volume usage events for account: " + account.getId());
}
if ((endDate == null) || endDate.after(new Date())) { if ((endDate == null) || endDate.after(new Date())) {
endDate = 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 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 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 // - 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()) { if (usageUsageVols.isEmpty()) {
LOGGER.debug("No volume usage events for this period"); logger.debug("No volume usage events for this period");
return true; return true;
} }
@ -131,7 +117,7 @@ public class VolumeUsageParser {
return true; 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); Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
if (volUsageInfo == null) { if (volUsageInfo == null) {
volUsageInfo = new Pair<Long, Long>(new Long(volId), new Long(duration)); volUsageInfo = new Pair<Long, Long>(new Long(volId), new Long(duration));
@ -143,19 +129,17 @@ public class VolumeUsageParser {
usageDataMap.put(key, volUsageInfo); 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) { Long templateId, long size) {
// Our smallest increment is hourly for now // Our smallest increment is hourly for now
if (LOGGER.isDebugEnabled()) { logger.debug("Total running time {} ms", runningTime);
LOGGER.debug("Total running time " + runningTime + "ms");
}
float usage = runningTime / 1000f / 60f / 60f; float usage = runningTime / 1000f / 60f / 60f;
DecimalFormat dFormat = new DecimalFormat("#.######"); DecimalFormat dFormat = new DecimalFormat("#.######");
String usageDisplay = dFormat.format(usage); 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), volId, usageDisplay, DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), startDate),
DateUtil.displayDateInTimezone(UsageManagerImpl.getUsageAggregationTimeZone(), endDate), account.getId()); 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, UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), null, null, doId, templateId, volId,
size, startDate, endDate); size, startDate, endDate);
s_usageDao.persist(usageRecord); usageDao.persist(usageRecord);
} }
private static class VolInfo { private static class VolInfo {

View File

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