mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
fixing line endings in usage
This commit is contained in:
parent
91fadc4a0d
commit
65d5d4d549
@ -10,258 +10,258 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.URLName;
|
||||
import javax.mail.Message.RecipientType;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.alert.AlertManager;
|
||||
import com.cloud.alert.AlertVO;
|
||||
import com.cloud.alert.dao.AlertDao;
|
||||
import com.cloud.configuration.dao.ConfigurationDao;
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
import com.sun.mail.smtp.SMTPSSLTransport;
|
||||
import com.sun.mail.smtp.SMTPTransport;
|
||||
|
||||
@Local(value={AlertManager.class})
|
||||
public class UsageAlertManagerImpl implements AlertManager {
|
||||
private static final Logger s_logger = Logger.getLogger(UsageAlertManagerImpl.class.getName());
|
||||
|
||||
private String _name = null;
|
||||
private EmailAlert _emailAlert;
|
||||
private AlertDao _alertDao;
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
_name = name;
|
||||
|
||||
ComponentLocator locator = ComponentLocator.getCurrentLocator();
|
||||
ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
|
||||
if (configDao == null) {
|
||||
s_logger.error("Unable to get the configuration dao.");
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<String, String> configs = configDao.getConfiguration("management-server", params);
|
||||
|
||||
// set up the email system for alerts
|
||||
String emailAddressList = configs.get("alert.email.addresses");
|
||||
String[] emailAddresses = null;
|
||||
if (emailAddressList != null) {
|
||||
emailAddresses = emailAddressList.split(",");
|
||||
}
|
||||
|
||||
String smtpHost = configs.get("alert.smtp.host");
|
||||
int smtpPort = NumbersUtil.parseInt(configs.get("alert.smtp.port"), 25);
|
||||
String useAuthStr = configs.get("alert.smtp.useAuth");
|
||||
boolean useAuth = ((useAuthStr == null) ? false : Boolean.parseBoolean(useAuthStr));
|
||||
String smtpUsername = configs.get("alert.smtp.username");
|
||||
String smtpPassword = configs.get("alert.smtp.password");
|
||||
String emailSender = configs.get("alert.email.sender");
|
||||
String smtpDebugStr = configs.get("alert.smtp.debug");
|
||||
boolean smtpDebug = false;
|
||||
if (smtpDebugStr != null) {
|
||||
smtpDebug = Boolean.parseBoolean(smtpDebugStr);
|
||||
}
|
||||
|
||||
_emailAlert = new EmailAlert(emailAddresses, smtpHost, smtpPort, useAuth, smtpUsername, smtpPassword, emailSender, smtpDebug);
|
||||
|
||||
_alertDao = locator.getDao(AlertDao.class);
|
||||
if (_alertDao == null) {
|
||||
s_logger.error("Unable to get the alert dao.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAlert(short alertType, long dataCenterId, long podId) {
|
||||
try {
|
||||
if (_emailAlert != null) {
|
||||
_emailAlert.clearAlert(alertType, dataCenterId, podId);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Problem clearing email alert", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendAlert(short alertType, long dataCenterId, Long podId, String subject, String body) {
|
||||
// TODO: queue up these messages and send them as one set of issues once a certain number of issues is reached? If that's the case,
|
||||
// shouldn't we have a type/severity as part of the API so that severe errors get sent right away?
|
||||
try {
|
||||
if (_emailAlert != null) {
|
||||
_emailAlert.sendAlert(alertType, dataCenterId, podId, subject, body);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Problem sending email alert", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class EmailAlert {
|
||||
private Session _smtpSession;
|
||||
private InternetAddress[] _recipientList;
|
||||
private final String _smtpHost;
|
||||
private int _smtpPort = -1;
|
||||
private boolean _smtpUseAuth = false;
|
||||
private final String _smtpUsername;
|
||||
private final String _smtpPassword;
|
||||
private final String _emailSender;
|
||||
|
||||
public EmailAlert(String[] recipientList, String smtpHost, int smtpPort, boolean smtpUseAuth, final String smtpUsername, final String smtpPassword, String emailSender, boolean smtpDebug) {
|
||||
if (recipientList != null) {
|
||||
_recipientList = new InternetAddress[recipientList.length];
|
||||
for (int i = 0; i < recipientList.length; i++) {
|
||||
try {
|
||||
_recipientList[i] = new InternetAddress(recipientList[i], recipientList[i]);
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Exception creating address for: " + recipientList[i], ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_smtpHost = smtpHost;
|
||||
_smtpPort = smtpPort;
|
||||
_smtpUseAuth = smtpUseAuth;
|
||||
_smtpUsername = smtpUsername;
|
||||
_smtpPassword = smtpPassword;
|
||||
_emailSender = emailSender;
|
||||
|
||||
if (_smtpHost != null) {
|
||||
Properties smtpProps = new Properties();
|
||||
smtpProps.put("mail.smtp.host", smtpHost);
|
||||
smtpProps.put("mail.smtp.port", smtpPort);
|
||||
smtpProps.put("mail.smtp.auth", ""+smtpUseAuth);
|
||||
if (smtpUsername != null) {
|
||||
smtpProps.put("mail.smtp.user", smtpUsername);
|
||||
}
|
||||
|
||||
smtpProps.put("mail.smtps.host", smtpHost);
|
||||
smtpProps.put("mail.smtps.port", smtpPort);
|
||||
smtpProps.put("mail.smtps.auth", ""+smtpUseAuth);
|
||||
if (smtpUsername != null) {
|
||||
smtpProps.put("mail.smtps.user", smtpUsername);
|
||||
}
|
||||
|
||||
if ((smtpUsername != null) && (smtpPassword != null)) {
|
||||
_smtpSession = Session.getInstance(smtpProps, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(smtpUsername, smtpPassword);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
_smtpSession = Session.getInstance(smtpProps);
|
||||
}
|
||||
_smtpSession.setDebug(smtpDebug);
|
||||
} else {
|
||||
_smtpSession = null;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: make sure this handles SSL transport (useAuth is true) and regular
|
||||
public void sendAlert(short alertType, long dataCenterId, Long podId, String subject, String content) throws MessagingException, UnsupportedEncodingException {
|
||||
AlertVO alert = null;
|
||||
|
||||
if ((alertType != AlertManager.ALERT_TYPE_HOST) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_USERVM) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_DOMAIN_ROUTER) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_CONSOLE_PROXY) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_SSVM) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_STORAGE_MISC) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_MANAGMENT_NODE)) {
|
||||
alert = _alertDao.getLastAlert(alertType, dataCenterId, podId);
|
||||
}
|
||||
|
||||
if (alert == null) {
|
||||
// set up a new alert
|
||||
AlertVO newAlert = new AlertVO();
|
||||
newAlert.setType(alertType);
|
||||
newAlert.setSubject(subject);
|
||||
newAlert.setPodId(podId);
|
||||
newAlert.setDataCenterId(dataCenterId);
|
||||
newAlert.setSentCount(1); // initialize sent count to 1 since we are now sending an alert
|
||||
newAlert.setLastSent(new Date());
|
||||
_alertDao.persist(newAlert);
|
||||
} else {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Have already sent: " + alert.getSentCount() + " emails for alert type '" + alertType + "' -- skipping send email");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (_smtpSession != null) {
|
||||
SMTPMessage msg = new SMTPMessage(_smtpSession);
|
||||
msg.setSender(new InternetAddress(_emailSender, _emailSender));
|
||||
msg.setFrom(new InternetAddress(_emailSender, _emailSender));
|
||||
for (InternetAddress address : _recipientList) {
|
||||
msg.addRecipient(RecipientType.TO, address);
|
||||
}
|
||||
msg.setSubject(subject);
|
||||
msg.setSentDate(new Date());
|
||||
msg.setContent(content, "text/plain");
|
||||
msg.saveChanges();
|
||||
|
||||
SMTPTransport smtpTrans = null;
|
||||
if (_smtpUseAuth) {
|
||||
smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
|
||||
} else {
|
||||
smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
|
||||
}
|
||||
smtpTrans.connect();
|
||||
smtpTrans.sendMessage(msg, msg.getAllRecipients());
|
||||
smtpTrans.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearAlert(short alertType, long dataCenterId, Long podId) {
|
||||
if (alertType != -1) {
|
||||
AlertVO alert = _alertDao.getLastAlert(alertType, dataCenterId, podId);
|
||||
if (alert != null) {
|
||||
AlertVO updatedAlert = _alertDao.createForUpdate();
|
||||
updatedAlert.setResolved(new Date());
|
||||
_alertDao.update(alert.getId(), updatedAlert);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateCapacity() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
package com.cloud.usage;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.URLName;
|
||||
import javax.mail.Message.RecipientType;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.alert.AlertManager;
|
||||
import com.cloud.alert.AlertVO;
|
||||
import com.cloud.alert.dao.AlertDao;
|
||||
import com.cloud.configuration.dao.ConfigurationDao;
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
import com.sun.mail.smtp.SMTPSSLTransport;
|
||||
import com.sun.mail.smtp.SMTPTransport;
|
||||
|
||||
@Local(value={AlertManager.class})
|
||||
public class UsageAlertManagerImpl implements AlertManager {
|
||||
private static final Logger s_logger = Logger.getLogger(UsageAlertManagerImpl.class.getName());
|
||||
|
||||
private String _name = null;
|
||||
private EmailAlert _emailAlert;
|
||||
private AlertDao _alertDao;
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
_name = name;
|
||||
|
||||
ComponentLocator locator = ComponentLocator.getCurrentLocator();
|
||||
ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
|
||||
if (configDao == null) {
|
||||
s_logger.error("Unable to get the configuration dao.");
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<String, String> configs = configDao.getConfiguration("management-server", params);
|
||||
|
||||
// set up the email system for alerts
|
||||
String emailAddressList = configs.get("alert.email.addresses");
|
||||
String[] emailAddresses = null;
|
||||
if (emailAddressList != null) {
|
||||
emailAddresses = emailAddressList.split(",");
|
||||
}
|
||||
|
||||
String smtpHost = configs.get("alert.smtp.host");
|
||||
int smtpPort = NumbersUtil.parseInt(configs.get("alert.smtp.port"), 25);
|
||||
String useAuthStr = configs.get("alert.smtp.useAuth");
|
||||
boolean useAuth = ((useAuthStr == null) ? false : Boolean.parseBoolean(useAuthStr));
|
||||
String smtpUsername = configs.get("alert.smtp.username");
|
||||
String smtpPassword = configs.get("alert.smtp.password");
|
||||
String emailSender = configs.get("alert.email.sender");
|
||||
String smtpDebugStr = configs.get("alert.smtp.debug");
|
||||
boolean smtpDebug = false;
|
||||
if (smtpDebugStr != null) {
|
||||
smtpDebug = Boolean.parseBoolean(smtpDebugStr);
|
||||
}
|
||||
|
||||
_emailAlert = new EmailAlert(emailAddresses, smtpHost, smtpPort, useAuth, smtpUsername, smtpPassword, emailSender, smtpDebug);
|
||||
|
||||
_alertDao = locator.getDao(AlertDao.class);
|
||||
if (_alertDao == null) {
|
||||
s_logger.error("Unable to get the alert dao.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return _name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean stop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAlert(short alertType, long dataCenterId, long podId) {
|
||||
try {
|
||||
if (_emailAlert != null) {
|
||||
_emailAlert.clearAlert(alertType, dataCenterId, podId);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Problem clearing email alert", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendAlert(short alertType, long dataCenterId, Long podId, String subject, String body) {
|
||||
// TODO: queue up these messages and send them as one set of issues once a certain number of issues is reached? If that's the case,
|
||||
// shouldn't we have a type/severity as part of the API so that severe errors get sent right away?
|
||||
try {
|
||||
if (_emailAlert != null) {
|
||||
_emailAlert.sendAlert(alertType, dataCenterId, podId, subject, body);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Problem sending email alert", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class EmailAlert {
|
||||
private Session _smtpSession;
|
||||
private InternetAddress[] _recipientList;
|
||||
private final String _smtpHost;
|
||||
private int _smtpPort = -1;
|
||||
private boolean _smtpUseAuth = false;
|
||||
private final String _smtpUsername;
|
||||
private final String _smtpPassword;
|
||||
private final String _emailSender;
|
||||
|
||||
public EmailAlert(String[] recipientList, String smtpHost, int smtpPort, boolean smtpUseAuth, final String smtpUsername, final String smtpPassword, String emailSender, boolean smtpDebug) {
|
||||
if (recipientList != null) {
|
||||
_recipientList = new InternetAddress[recipientList.length];
|
||||
for (int i = 0; i < recipientList.length; i++) {
|
||||
try {
|
||||
_recipientList[i] = new InternetAddress(recipientList[i], recipientList[i]);
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Exception creating address for: " + recipientList[i], ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_smtpHost = smtpHost;
|
||||
_smtpPort = smtpPort;
|
||||
_smtpUseAuth = smtpUseAuth;
|
||||
_smtpUsername = smtpUsername;
|
||||
_smtpPassword = smtpPassword;
|
||||
_emailSender = emailSender;
|
||||
|
||||
if (_smtpHost != null) {
|
||||
Properties smtpProps = new Properties();
|
||||
smtpProps.put("mail.smtp.host", smtpHost);
|
||||
smtpProps.put("mail.smtp.port", smtpPort);
|
||||
smtpProps.put("mail.smtp.auth", ""+smtpUseAuth);
|
||||
if (smtpUsername != null) {
|
||||
smtpProps.put("mail.smtp.user", smtpUsername);
|
||||
}
|
||||
|
||||
smtpProps.put("mail.smtps.host", smtpHost);
|
||||
smtpProps.put("mail.smtps.port", smtpPort);
|
||||
smtpProps.put("mail.smtps.auth", ""+smtpUseAuth);
|
||||
if (smtpUsername != null) {
|
||||
smtpProps.put("mail.smtps.user", smtpUsername);
|
||||
}
|
||||
|
||||
if ((smtpUsername != null) && (smtpPassword != null)) {
|
||||
_smtpSession = Session.getInstance(smtpProps, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(smtpUsername, smtpPassword);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
_smtpSession = Session.getInstance(smtpProps);
|
||||
}
|
||||
_smtpSession.setDebug(smtpDebug);
|
||||
} else {
|
||||
_smtpSession = null;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: make sure this handles SSL transport (useAuth is true) and regular
|
||||
public void sendAlert(short alertType, long dataCenterId, Long podId, String subject, String content) throws MessagingException, UnsupportedEncodingException {
|
||||
AlertVO alert = null;
|
||||
|
||||
if ((alertType != AlertManager.ALERT_TYPE_HOST) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_USERVM) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_DOMAIN_ROUTER) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_CONSOLE_PROXY) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_SSVM) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_STORAGE_MISC) &&
|
||||
(alertType != AlertManager.ALERT_TYPE_MANAGMENT_NODE)) {
|
||||
alert = _alertDao.getLastAlert(alertType, dataCenterId, podId);
|
||||
}
|
||||
|
||||
if (alert == null) {
|
||||
// set up a new alert
|
||||
AlertVO newAlert = new AlertVO();
|
||||
newAlert.setType(alertType);
|
||||
newAlert.setSubject(subject);
|
||||
newAlert.setPodId(podId);
|
||||
newAlert.setDataCenterId(dataCenterId);
|
||||
newAlert.setSentCount(1); // initialize sent count to 1 since we are now sending an alert
|
||||
newAlert.setLastSent(new Date());
|
||||
_alertDao.persist(newAlert);
|
||||
} else {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Have already sent: " + alert.getSentCount() + " emails for alert type '" + alertType + "' -- skipping send email");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (_smtpSession != null) {
|
||||
SMTPMessage msg = new SMTPMessage(_smtpSession);
|
||||
msg.setSender(new InternetAddress(_emailSender, _emailSender));
|
||||
msg.setFrom(new InternetAddress(_emailSender, _emailSender));
|
||||
for (InternetAddress address : _recipientList) {
|
||||
msg.addRecipient(RecipientType.TO, address);
|
||||
}
|
||||
msg.setSubject(subject);
|
||||
msg.setSentDate(new Date());
|
||||
msg.setContent(content, "text/plain");
|
||||
msg.saveChanges();
|
||||
|
||||
SMTPTransport smtpTrans = null;
|
||||
if (_smtpUseAuth) {
|
||||
smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
|
||||
} else {
|
||||
smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
|
||||
}
|
||||
smtpTrans.connect();
|
||||
smtpTrans.sendMessage(msg, msg.getAllRecipients());
|
||||
smtpTrans.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearAlert(short alertType, long dataCenterId, Long podId) {
|
||||
if (alertType != -1) {
|
||||
AlertVO alert = _alertDao.getLastAlert(alertType, dataCenterId, podId);
|
||||
if (alert != null) {
|
||||
AlertVO updatedAlert = _alertDao.createForUpdate();
|
||||
updatedAlert.setResolved(new Date());
|
||||
_alertDao.update(alert.getId(), updatedAlert);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculateCapacity() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,12 +10,12 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage;
|
||||
|
||||
import com.cloud.usage.UsageJobVO;
|
||||
import com.cloud.utils.component.Manager;
|
||||
|
||||
public interface UsageManager extends Manager {
|
||||
public void scheduleParse();
|
||||
public void parse(UsageJobVO job, long startDateMillis, long endDateMillis);
|
||||
}
|
||||
package com.cloud.usage;
|
||||
|
||||
import com.cloud.usage.UsageJobVO;
|
||||
import com.cloud.utils.component.Manager;
|
||||
|
||||
public interface UsageManager extends Manager {
|
||||
public void scheduleParse();
|
||||
public void parse(UsageJobVO job, long startDateMillis, long endDateMillis);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -10,27 +10,27 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class UsageServer {
|
||||
private static final Logger s_logger = Logger.getLogger(UsageServer.class.getName());
|
||||
public static final String Name = "usage-server";
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// TODO: do we need to communicate with mgmt server?
|
||||
final ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
UsageManager mgr = _locator.getManager(UsageManager.class);
|
||||
if (mgr != null) {
|
||||
if (s_logger.isInfoEnabled()) {
|
||||
s_logger.info("UsageServer ready...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.cloud.usage;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class UsageServer {
|
||||
private static final Logger s_logger = Logger.getLogger(UsageServer.class.getName());
|
||||
public static final String Name = "usage-server";
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
// TODO: do we need to communicate with mgmt server?
|
||||
final ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
UsageManager mgr = _locator.getManager(UsageManager.class);
|
||||
if (mgr != null) {
|
||||
if (s_logger.isInfoEnabled()) {
|
||||
s_logger.info("UsageServer ready...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,163 +10,163 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageIPAddressVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageIPAddressDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class IPAddressUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(IPAddressUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageIPAddressDao m_usageIPAddressDao = _locator.getDao(UsageIPAddressDao.class);
|
||||
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing IP Address usage for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_ip_address table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageIPAddressVO> usageIPAddress = m_usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
|
||||
|
||||
if(usageIPAddress.isEmpty()){
|
||||
s_logger.debug("No IP Address usage for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
|
||||
Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>();
|
||||
|
||||
// loop through all the usage IPs, create a usage record for each
|
||||
for (UsageIPAddressVO usageIp : usageIPAddress) {
|
||||
long IpId = usageIp.getId();
|
||||
|
||||
String key = ""+IpId;
|
||||
|
||||
// store the info in the IP map
|
||||
IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat(), usageIp.isSystem()));
|
||||
|
||||
Date IpAssignDate = usageIp.getAssigned();
|
||||
Date IpReleaseDeleteDate = usageIp.getReleased();
|
||||
|
||||
if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) {
|
||||
IpReleaseDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (IpAssignDate.before(startDate)) {
|
||||
IpAssignDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (IpReleaseDeleteDate.getTime() - IpAssignDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String ipIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey);
|
||||
long useTime = ipTimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
IpInfo info = IPMap.get(ipIdKey);
|
||||
createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat(), info.isSystem);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) {
|
||||
Pair<Long, Long> ipUsageInfo = usageDataMap.get(key);
|
||||
if (ipUsageInfo == null) {
|
||||
ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = ipUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
ipUsageInfo = new Pair<Long, Long>(ipUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, ipUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total usage time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating IP usage record with id: " + IpId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
String usageDesc = "IPAddress: "+IPAddress;
|
||||
|
||||
// Create the usage record
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), IpId,
|
||||
(isSystem?1:0), (isSourceNat?"SourceNat":""), startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class IpInfo {
|
||||
private long zoneId;
|
||||
private long IpId;
|
||||
private String IPAddress;
|
||||
private boolean isSourceNat;
|
||||
private boolean isSystem;
|
||||
|
||||
public IpInfo(long zoneId,long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) {
|
||||
this.zoneId = zoneId;
|
||||
this.IpId = IpId;
|
||||
this.IPAddress = IPAddress;
|
||||
this.isSourceNat = isSourceNat;
|
||||
this.isSystem = isSystem;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
public long getIpId() {
|
||||
return IpId;
|
||||
}
|
||||
|
||||
public String getIPAddress() {
|
||||
return IPAddress;
|
||||
}
|
||||
|
||||
public boolean isSourceNat() {
|
||||
return isSourceNat;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageIPAddressVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageIPAddressDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class IPAddressUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(IPAddressUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageIPAddressDao m_usageIPAddressDao = _locator.getDao(UsageIPAddressDao.class);
|
||||
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing IP Address usage for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_ip_address table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageIPAddressVO> usageIPAddress = m_usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
|
||||
|
||||
if(usageIPAddress.isEmpty()){
|
||||
s_logger.debug("No IP Address usage for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
|
||||
Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>();
|
||||
|
||||
// loop through all the usage IPs, create a usage record for each
|
||||
for (UsageIPAddressVO usageIp : usageIPAddress) {
|
||||
long IpId = usageIp.getId();
|
||||
|
||||
String key = ""+IpId;
|
||||
|
||||
// store the info in the IP map
|
||||
IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat(), usageIp.isSystem()));
|
||||
|
||||
Date IpAssignDate = usageIp.getAssigned();
|
||||
Date IpReleaseDeleteDate = usageIp.getReleased();
|
||||
|
||||
if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) {
|
||||
IpReleaseDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (IpAssignDate.before(startDate)) {
|
||||
IpAssignDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (IpReleaseDeleteDate.getTime() - IpAssignDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String ipIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey);
|
||||
long useTime = ipTimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
IpInfo info = IPMap.get(ipIdKey);
|
||||
createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat(), info.isSystem);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) {
|
||||
Pair<Long, Long> ipUsageInfo = usageDataMap.get(key);
|
||||
if (ipUsageInfo == null) {
|
||||
ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = ipUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
ipUsageInfo = new Pair<Long, Long>(ipUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, ipUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total usage time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating IP usage record with id: " + IpId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
String usageDesc = "IPAddress: "+IPAddress;
|
||||
|
||||
// Create the usage record
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), IpId,
|
||||
(isSystem?1:0), (isSourceNat?"SourceNat":""), startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class IpInfo {
|
||||
private long zoneId;
|
||||
private long IpId;
|
||||
private String IPAddress;
|
||||
private boolean isSourceNat;
|
||||
private boolean isSystem;
|
||||
|
||||
public IpInfo(long zoneId,long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) {
|
||||
this.zoneId = zoneId;
|
||||
this.IpId = IpId;
|
||||
this.IPAddress = IPAddress;
|
||||
this.isSourceNat = isSourceNat;
|
||||
this.isSystem = isSystem;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
public long getIpId() {
|
||||
return IpId;
|
||||
}
|
||||
|
||||
public String getIPAddress() {
|
||||
return IPAddress;
|
||||
}
|
||||
|
||||
public boolean isSourceNat() {
|
||||
return isSourceNat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,146 +10,146 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageLoadBalancerPolicyVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageLoadBalancerPolicyDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class LoadBalancerUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(LoadBalancerUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageLoadBalancerPolicyDao m_usageLoadBalancerPolicyDao = _locator.getDao(UsageLoadBalancerPolicyDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all LoadBalancerPolicy usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageLoadBalancerPolicyVO> usageLBs = m_usageLoadBalancerPolicyDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageLBs.isEmpty()){
|
||||
s_logger.debug("No load balancer usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, LBInfo> lbMap = new HashMap<String, LBInfo>();
|
||||
|
||||
// loop through all the load balancer policies, create a usage record for each
|
||||
for (UsageLoadBalancerPolicyVO usageLB : usageLBs) {
|
||||
long lbId = usageLB.getId();
|
||||
String key = ""+lbId;
|
||||
|
||||
lbMap.put(key, new LBInfo(lbId, usageLB.getZoneId()));
|
||||
|
||||
Date lbCreateDate = usageLB.getCreated();
|
||||
Date lbDeleteDate = usageLB.getDeleted();
|
||||
|
||||
if ((lbDeleteDate == null) || lbDeleteDate.after(endDate)) {
|
||||
lbDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (lbCreateDate.before(startDate)) {
|
||||
lbCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (lbDeleteDate.getTime() - lbCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateLBUsageData(usageMap, key, usageLB.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String lbIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> sgtimeInfo = usageMap.get(lbIdKey);
|
||||
long useTime = sgtimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
LBInfo info = lbMap.get(lbIdKey);
|
||||
createUsageRecord(UsageTypes.LOAD_BALANCER_POLICY, useTime, startDate, endDate, account, info.getId(), info.getZoneId() );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateLBUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long lbId, long duration) {
|
||||
Pair<Long, Long> lbUsageInfo = usageDataMap.get(key);
|
||||
if (lbUsageInfo == null) {
|
||||
lbUsageInfo = new Pair<Long, Long>(new Long(lbId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = lbUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
lbUsageInfo = new Pair<Long, Long>(lbUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, lbUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long lbId, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating Volume usage record for load balancer: " + lbId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Load Balancing Policy: "+lbId+" usage time";
|
||||
|
||||
//ToDo: get zone id
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, null, null, lbId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class LBInfo {
|
||||
private long id;
|
||||
private long zoneId;
|
||||
|
||||
public LBInfo(long id, long zoneId) {
|
||||
this.id = id;
|
||||
this.zoneId = zoneId;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageLoadBalancerPolicyVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageLoadBalancerPolicyDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class LoadBalancerUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(LoadBalancerUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageLoadBalancerPolicyDao m_usageLoadBalancerPolicyDao = _locator.getDao(UsageLoadBalancerPolicyDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all LoadBalancerPolicy usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageLoadBalancerPolicyVO> usageLBs = m_usageLoadBalancerPolicyDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageLBs.isEmpty()){
|
||||
s_logger.debug("No load balancer usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, LBInfo> lbMap = new HashMap<String, LBInfo>();
|
||||
|
||||
// loop through all the load balancer policies, create a usage record for each
|
||||
for (UsageLoadBalancerPolicyVO usageLB : usageLBs) {
|
||||
long lbId = usageLB.getId();
|
||||
String key = ""+lbId;
|
||||
|
||||
lbMap.put(key, new LBInfo(lbId, usageLB.getZoneId()));
|
||||
|
||||
Date lbCreateDate = usageLB.getCreated();
|
||||
Date lbDeleteDate = usageLB.getDeleted();
|
||||
|
||||
if ((lbDeleteDate == null) || lbDeleteDate.after(endDate)) {
|
||||
lbDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (lbCreateDate.before(startDate)) {
|
||||
lbCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (lbDeleteDate.getTime() - lbCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateLBUsageData(usageMap, key, usageLB.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String lbIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> sgtimeInfo = usageMap.get(lbIdKey);
|
||||
long useTime = sgtimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
LBInfo info = lbMap.get(lbIdKey);
|
||||
createUsageRecord(UsageTypes.LOAD_BALANCER_POLICY, useTime, startDate, endDate, account, info.getId(), info.getZoneId() );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateLBUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long lbId, long duration) {
|
||||
Pair<Long, Long> lbUsageInfo = usageDataMap.get(key);
|
||||
if (lbUsageInfo == null) {
|
||||
lbUsageInfo = new Pair<Long, Long>(new Long(lbId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = lbUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
lbUsageInfo = new Pair<Long, Long>(lbUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, lbUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long lbId, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating Volume usage record for load balancer: " + lbId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Load Balancing Policy: "+lbId+" usage time";
|
||||
|
||||
//ToDo: get zone id
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, null, null, lbId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class LBInfo {
|
||||
private long id;
|
||||
private long zoneId;
|
||||
|
||||
public LBInfo(long id, long zoneId) {
|
||||
this.id = id;
|
||||
this.zoneId = zoneId;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,158 +10,158 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageNetworkOfferingVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageNetworkOfferingDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class NetworkOfferingUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(NetworkOfferingUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageNetworkOfferingDao m_usageNetworkOfferingDao = _locator.getDao(UsageNetworkOfferingDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all NetworkOffering usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageNetworkOfferingVO> usageNOs = m_usageNetworkOfferingDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageNOs.isEmpty()){
|
||||
s_logger.debug("No NetworkOffering usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, NOInfo> noMap = new HashMap<String, NOInfo>();
|
||||
|
||||
// loop through all the network offerings, create a usage record for each
|
||||
for (UsageNetworkOfferingVO usageNO : usageNOs) {
|
||||
long vmId = usageNO.getVmInstanceId();
|
||||
long noId = usageNO.getNetworkOfferingId();
|
||||
String key = ""+vmId+"NO"+noId;
|
||||
|
||||
noMap.put(key, new NOInfo(vmId, usageNO.getZoneId(), noId, usageNO.isDefault()));
|
||||
|
||||
Date noCreateDate = usageNO.getCreated();
|
||||
Date noDeleteDate = usageNO.getDeleted();
|
||||
|
||||
if ((noDeleteDate == null) || noDeleteDate.after(endDate)) {
|
||||
noDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (noCreateDate.before(startDate)) {
|
||||
noCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (noDeleteDate.getTime() - noCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateNOUsageData(usageMap, key, usageNO.getVmInstanceId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String noIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> notimeInfo = usageMap.get(noIdKey);
|
||||
long useTime = notimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
NOInfo info = noMap.get(noIdKey);
|
||||
createUsageRecord(UsageTypes.NETWORK_OFFERING, useTime, startDate, endDate, account, info.getVmId(), info.getNOId(), info.getZoneId(), info.isDefault());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateNOUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
|
||||
Pair<Long, Long> noUsageInfo = usageDataMap.get(key);
|
||||
if (noUsageInfo == null) {
|
||||
noUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = noUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
noUsageInfo = new Pair<Long, Long>(noUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, noUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long noId, long zoneId, boolean isDefault) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating network offering:" + noId + " usage record for Vm : " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Network offering:" + noId + " for Vm : " + vmId + " usage time";
|
||||
|
||||
long defaultNic = (isDefault) ? 1 : 0;
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), vmId, null, noId, null, defaultNic, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class NOInfo {
|
||||
private long vmId;
|
||||
private long zoneId;
|
||||
private long noId;
|
||||
private boolean isDefault;
|
||||
|
||||
public NOInfo(long vmId, long zoneId, long noId, boolean isDefault) {
|
||||
this.vmId = vmId;
|
||||
this.zoneId = zoneId;
|
||||
this.noId = noId;
|
||||
this.isDefault = isDefault;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVmId() {
|
||||
return vmId;
|
||||
}
|
||||
public long getNOId() {
|
||||
return noId;
|
||||
}
|
||||
|
||||
public boolean isDefault(){
|
||||
return isDefault;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageNetworkOfferingVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageNetworkOfferingDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class NetworkOfferingUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(NetworkOfferingUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageNetworkOfferingDao m_usageNetworkOfferingDao = _locator.getDao(UsageNetworkOfferingDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all NetworkOffering usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageNetworkOfferingVO> usageNOs = m_usageNetworkOfferingDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageNOs.isEmpty()){
|
||||
s_logger.debug("No NetworkOffering usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, NOInfo> noMap = new HashMap<String, NOInfo>();
|
||||
|
||||
// loop through all the network offerings, create a usage record for each
|
||||
for (UsageNetworkOfferingVO usageNO : usageNOs) {
|
||||
long vmId = usageNO.getVmInstanceId();
|
||||
long noId = usageNO.getNetworkOfferingId();
|
||||
String key = ""+vmId+"NO"+noId;
|
||||
|
||||
noMap.put(key, new NOInfo(vmId, usageNO.getZoneId(), noId, usageNO.isDefault()));
|
||||
|
||||
Date noCreateDate = usageNO.getCreated();
|
||||
Date noDeleteDate = usageNO.getDeleted();
|
||||
|
||||
if ((noDeleteDate == null) || noDeleteDate.after(endDate)) {
|
||||
noDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (noCreateDate.before(startDate)) {
|
||||
noCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (noDeleteDate.getTime() - noCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateNOUsageData(usageMap, key, usageNO.getVmInstanceId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String noIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> notimeInfo = usageMap.get(noIdKey);
|
||||
long useTime = notimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
NOInfo info = noMap.get(noIdKey);
|
||||
createUsageRecord(UsageTypes.NETWORK_OFFERING, useTime, startDate, endDate, account, info.getVmId(), info.getNOId(), info.getZoneId(), info.isDefault());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateNOUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
|
||||
Pair<Long, Long> noUsageInfo = usageDataMap.get(key);
|
||||
if (noUsageInfo == null) {
|
||||
noUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = noUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
noUsageInfo = new Pair<Long, Long>(noUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, noUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long noId, long zoneId, boolean isDefault) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating network offering:" + noId + " usage record for Vm : " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Network offering:" + noId + " for Vm : " + vmId + " usage time";
|
||||
|
||||
long defaultNic = (isDefault) ? 1 : 0;
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), vmId, null, noId, null, defaultNic, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class NOInfo {
|
||||
private long vmId;
|
||||
private long zoneId;
|
||||
private long noId;
|
||||
private boolean isDefault;
|
||||
|
||||
public NOInfo(long vmId, long zoneId, long noId, boolean isDefault) {
|
||||
this.vmId = vmId;
|
||||
this.zoneId = zoneId;
|
||||
this.noId = noId;
|
||||
this.isDefault = isDefault;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVmId() {
|
||||
return vmId;
|
||||
}
|
||||
public long getNOId() {
|
||||
return noId;
|
||||
}
|
||||
|
||||
public boolean isDefault(){
|
||||
return isDefault;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,152 +10,152 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageNetworkVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageNetworkDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
|
||||
public class NetworkUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(NetworkUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageNetworkDao m_usageNetworkDao = _locator.getDao(UsageNetworkDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all Network usage events for account: " + account.getId());
|
||||
}
|
||||
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_network table for all entries for userId with
|
||||
// event_date in the given range
|
||||
SearchCriteria<UsageNetworkVO> sc = m_usageNetworkDao.createSearchCriteria();
|
||||
sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
|
||||
sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
|
||||
List<UsageNetworkVO> usageNetworkVOs = m_usageNetworkDao.search(sc, null);
|
||||
|
||||
Map<String, NetworkInfo> networkUsageByZone = new HashMap<String, NetworkInfo>();
|
||||
|
||||
// Calculate the total bytes since last parsing
|
||||
for (UsageNetworkVO usageNetwork : usageNetworkVOs) {
|
||||
long zoneId = usageNetwork.getZoneId();
|
||||
String key = ""+zoneId;
|
||||
if(usageNetwork.getHostId() != 0){
|
||||
key += "-Host"+usageNetwork.getHostId();
|
||||
}
|
||||
NetworkInfo networkInfo = networkUsageByZone.get(key);
|
||||
|
||||
long bytesSent = usageNetwork.getBytesSent();
|
||||
long bytesReceived = usageNetwork.getBytesReceived();
|
||||
if (networkInfo != null) {
|
||||
bytesSent += networkInfo.getBytesSent();
|
||||
bytesReceived += networkInfo.getBytesRcvd();
|
||||
}
|
||||
|
||||
networkUsageByZone.put(key, new NetworkInfo(zoneId, usageNetwork.getHostId(), usageNetwork.getHostType(), usageNetwork.getNetworkId(), bytesSent, bytesReceived));
|
||||
}
|
||||
|
||||
for (String key : networkUsageByZone.keySet()) {
|
||||
NetworkInfo networkInfo = networkUsageByZone.get(key);
|
||||
long totalBytesSent = networkInfo.getBytesSent();
|
||||
long totalBytesReceived = networkInfo.getBytesRcvd();
|
||||
|
||||
if ((totalBytesSent > 0L) || (totalBytesReceived > 0L)) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating usage record, total bytes sent:" + totalBytesSent + ", total bytes received: " + totalBytesReceived + " for account: "
|
||||
+ account.getId() + " in availability zone " + networkInfo.getZoneId() + ", start: " + startDate + ", end: " + endDate);
|
||||
}
|
||||
|
||||
Long hostId = null;
|
||||
|
||||
// Create the usage record for bytes sent
|
||||
String usageDesc = "network bytes sent";
|
||||
if(networkInfo.getHostId() != 0){
|
||||
hostId = networkInfo.getHostId();
|
||||
usageDesc += " for Host: "+networkInfo.getHostId();
|
||||
}
|
||||
UsageVO usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesSent + " bytes sent",
|
||||
UsageTypes.NETWORK_BYTES_SENT, new Double(totalBytesSent), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
|
||||
// Create the usage record for bytes received
|
||||
usageDesc = "network bytes received";
|
||||
if(networkInfo.getHostId() != 0){
|
||||
usageDesc += " for Host: "+networkInfo.getHostId();
|
||||
}
|
||||
usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesReceived + " bytes received",
|
||||
UsageTypes.NETWORK_BYTES_RECEIVED, new Double(totalBytesReceived), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
} else {
|
||||
// Don't charge anything if there were zero bytes processed
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("No usage record (0 bytes used) generated for account: " + account.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static class NetworkInfo {
|
||||
private long zoneId;
|
||||
private long hostId;
|
||||
private String hostType;
|
||||
private Long networkId;
|
||||
private long bytesSent;
|
||||
private long bytesRcvd;
|
||||
|
||||
public NetworkInfo(long zoneId, long hostId, String hostType, Long networkId, long bytesSent, long bytesRcvd) {
|
||||
this.zoneId = zoneId;
|
||||
this.hostId = hostId;
|
||||
this.hostType = hostType;
|
||||
this.networkId = networkId;
|
||||
this.bytesSent = bytesSent;
|
||||
this.bytesRcvd = bytesRcvd;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
public long getHostId() {
|
||||
return hostId;
|
||||
}
|
||||
|
||||
public Long getNetworkId() {
|
||||
return networkId;
|
||||
}
|
||||
|
||||
public long getBytesSent() {
|
||||
return bytesSent;
|
||||
}
|
||||
|
||||
public long getBytesRcvd() {
|
||||
return bytesRcvd;
|
||||
}
|
||||
|
||||
public String getHostType(){
|
||||
return hostType;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageNetworkVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageNetworkDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
|
||||
public class NetworkUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(NetworkUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageNetworkDao m_usageNetworkDao = _locator.getDao(UsageNetworkDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all Network usage events for account: " + account.getId());
|
||||
}
|
||||
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_network table for all entries for userId with
|
||||
// event_date in the given range
|
||||
SearchCriteria<UsageNetworkVO> sc = m_usageNetworkDao.createSearchCriteria();
|
||||
sc.addAnd("accountId", SearchCriteria.Op.EQ, account.getId());
|
||||
sc.addAnd("eventTimeMillis", SearchCriteria.Op.BETWEEN, startDate.getTime(), endDate.getTime());
|
||||
List<UsageNetworkVO> usageNetworkVOs = m_usageNetworkDao.search(sc, null);
|
||||
|
||||
Map<String, NetworkInfo> networkUsageByZone = new HashMap<String, NetworkInfo>();
|
||||
|
||||
// Calculate the total bytes since last parsing
|
||||
for (UsageNetworkVO usageNetwork : usageNetworkVOs) {
|
||||
long zoneId = usageNetwork.getZoneId();
|
||||
String key = ""+zoneId;
|
||||
if(usageNetwork.getHostId() != 0){
|
||||
key += "-Host"+usageNetwork.getHostId();
|
||||
}
|
||||
NetworkInfo networkInfo = networkUsageByZone.get(key);
|
||||
|
||||
long bytesSent = usageNetwork.getBytesSent();
|
||||
long bytesReceived = usageNetwork.getBytesReceived();
|
||||
if (networkInfo != null) {
|
||||
bytesSent += networkInfo.getBytesSent();
|
||||
bytesReceived += networkInfo.getBytesRcvd();
|
||||
}
|
||||
|
||||
networkUsageByZone.put(key, new NetworkInfo(zoneId, usageNetwork.getHostId(), usageNetwork.getHostType(), usageNetwork.getNetworkId(), bytesSent, bytesReceived));
|
||||
}
|
||||
|
||||
for (String key : networkUsageByZone.keySet()) {
|
||||
NetworkInfo networkInfo = networkUsageByZone.get(key);
|
||||
long totalBytesSent = networkInfo.getBytesSent();
|
||||
long totalBytesReceived = networkInfo.getBytesRcvd();
|
||||
|
||||
if ((totalBytesSent > 0L) || (totalBytesReceived > 0L)) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating usage record, total bytes sent:" + totalBytesSent + ", total bytes received: " + totalBytesReceived + " for account: "
|
||||
+ account.getId() + " in availability zone " + networkInfo.getZoneId() + ", start: " + startDate + ", end: " + endDate);
|
||||
}
|
||||
|
||||
Long hostId = null;
|
||||
|
||||
// Create the usage record for bytes sent
|
||||
String usageDesc = "network bytes sent";
|
||||
if(networkInfo.getHostId() != 0){
|
||||
hostId = networkInfo.getHostId();
|
||||
usageDesc += " for Host: "+networkInfo.getHostId();
|
||||
}
|
||||
UsageVO usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesSent + " bytes sent",
|
||||
UsageTypes.NETWORK_BYTES_SENT, new Double(totalBytesSent), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
|
||||
// Create the usage record for bytes received
|
||||
usageDesc = "network bytes received";
|
||||
if(networkInfo.getHostId() != 0){
|
||||
usageDesc += " for Host: "+networkInfo.getHostId();
|
||||
}
|
||||
usageRecord = new UsageVO(networkInfo.getZoneId(), account.getId(), account.getDomainId(), usageDesc, totalBytesReceived + " bytes received",
|
||||
UsageTypes.NETWORK_BYTES_RECEIVED, new Double(totalBytesReceived), hostId, networkInfo.getHostType(), networkInfo.getNetworkId(), startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
} else {
|
||||
// Don't charge anything if there were zero bytes processed
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("No usage record (0 bytes used) generated for account: " + account.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static class NetworkInfo {
|
||||
private long zoneId;
|
||||
private long hostId;
|
||||
private String hostType;
|
||||
private Long networkId;
|
||||
private long bytesSent;
|
||||
private long bytesRcvd;
|
||||
|
||||
public NetworkInfo(long zoneId, long hostId, String hostType, Long networkId, long bytesSent, long bytesRcvd) {
|
||||
this.zoneId = zoneId;
|
||||
this.hostId = hostId;
|
||||
this.hostType = hostType;
|
||||
this.networkId = networkId;
|
||||
this.bytesSent = bytesSent;
|
||||
this.bytesRcvd = bytesRcvd;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
public long getHostId() {
|
||||
return hostId;
|
||||
}
|
||||
|
||||
public Long getNetworkId() {
|
||||
return networkId;
|
||||
}
|
||||
|
||||
public long getBytesSent() {
|
||||
return bytesSent;
|
||||
}
|
||||
|
||||
public long getBytesRcvd() {
|
||||
return bytesRcvd;
|
||||
}
|
||||
|
||||
public String getHostType(){
|
||||
return hostType;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,146 +10,146 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsagePortForwardingRuleVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsagePortForwardingRuleDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class PortForwardingUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(PortForwardingUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsagePortForwardingRuleDao m_usagePFRuleDao = _locator.getDao(UsagePortForwardingRuleDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all PortForwardingRule usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsagePortForwardingRuleVO> usagePFs = m_usagePFRuleDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usagePFs.isEmpty()){
|
||||
s_logger.debug("No port forwarding usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, PFInfo> pfMap = new HashMap<String, PFInfo>();
|
||||
|
||||
// loop through all the port forwarding rule, create a usage record for each
|
||||
for (UsagePortForwardingRuleVO usagePF : usagePFs) {
|
||||
long pfId = usagePF.getId();
|
||||
String key = ""+pfId;
|
||||
|
||||
pfMap.put(key, new PFInfo(pfId, usagePF.getZoneId()));
|
||||
|
||||
Date pfCreateDate = usagePF.getCreated();
|
||||
Date pfDeleteDate = usagePF.getDeleted();
|
||||
|
||||
if ((pfDeleteDate == null) || pfDeleteDate.after(endDate)) {
|
||||
pfDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (pfCreateDate.before(startDate)) {
|
||||
pfCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (pfDeleteDate.getTime() - pfCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updatePFUsageData(usageMap, key, usagePF.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String pfIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> sgtimeInfo = usageMap.get(pfIdKey);
|
||||
long useTime = sgtimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
PFInfo info = pfMap.get(pfIdKey);
|
||||
createUsageRecord(UsageTypes.PORT_FORWARDING_RULE, useTime, startDate, endDate, account, info.getId(), info.getZoneId() );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updatePFUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long pfId, long duration) {
|
||||
Pair<Long, Long> pfUsageInfo = usageDataMap.get(key);
|
||||
if (pfUsageInfo == null) {
|
||||
pfUsageInfo = new Pair<Long, Long>(new Long(pfId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = pfUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
pfUsageInfo = new Pair<Long, Long>(pfUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, pfUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long pfId, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating usage record for port forwarding rule: " + pfId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Port Forwarding Rule: "+pfId+" usage time";
|
||||
|
||||
//ToDo: get zone id
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, null, null, pfId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class PFInfo {
|
||||
private long id;
|
||||
private long zoneId;
|
||||
|
||||
public PFInfo(long id, long zoneId) {
|
||||
this.id = id;
|
||||
this.zoneId = zoneId;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsagePortForwardingRuleVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsagePortForwardingRuleDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class PortForwardingUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(PortForwardingUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsagePortForwardingRuleDao m_usagePFRuleDao = _locator.getDao(UsagePortForwardingRuleDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all PortForwardingRule usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsagePortForwardingRuleVO> usagePFs = m_usagePFRuleDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usagePFs.isEmpty()){
|
||||
s_logger.debug("No port forwarding usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, PFInfo> pfMap = new HashMap<String, PFInfo>();
|
||||
|
||||
// loop through all the port forwarding rule, create a usage record for each
|
||||
for (UsagePortForwardingRuleVO usagePF : usagePFs) {
|
||||
long pfId = usagePF.getId();
|
||||
String key = ""+pfId;
|
||||
|
||||
pfMap.put(key, new PFInfo(pfId, usagePF.getZoneId()));
|
||||
|
||||
Date pfCreateDate = usagePF.getCreated();
|
||||
Date pfDeleteDate = usagePF.getDeleted();
|
||||
|
||||
if ((pfDeleteDate == null) || pfDeleteDate.after(endDate)) {
|
||||
pfDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (pfCreateDate.before(startDate)) {
|
||||
pfCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (pfDeleteDate.getTime() - pfCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updatePFUsageData(usageMap, key, usagePF.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String pfIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> sgtimeInfo = usageMap.get(pfIdKey);
|
||||
long useTime = sgtimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
PFInfo info = pfMap.get(pfIdKey);
|
||||
createUsageRecord(UsageTypes.PORT_FORWARDING_RULE, useTime, startDate, endDate, account, info.getId(), info.getZoneId() );
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updatePFUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long pfId, long duration) {
|
||||
Pair<Long, Long> pfUsageInfo = usageDataMap.get(key);
|
||||
if (pfUsageInfo == null) {
|
||||
pfUsageInfo = new Pair<Long, Long>(new Long(pfId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = pfUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
pfUsageInfo = new Pair<Long, Long>(pfUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, pfUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long pfId, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating usage record for port forwarding rule: " + pfId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Port Forwarding Rule: "+pfId+" usage time";
|
||||
|
||||
//ToDo: get zone id
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, null, null, pfId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class PFInfo {
|
||||
private long id;
|
||||
private long zoneId;
|
||||
|
||||
public PFInfo(long id, long zoneId) {
|
||||
this.id = id;
|
||||
this.zoneId = zoneId;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,151 +10,151 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageSecurityGroupVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageSecurityGroupDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class SecurityGroupUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(SecurityGroupUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageSecurityGroupDao m_usageSecurityGroupDao = _locator.getDao(UsageSecurityGroupDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all SecurityGroup usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageSecurityGroupVO> usageSGs = m_usageSecurityGroupDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageSGs.isEmpty()){
|
||||
s_logger.debug("No SecurityGroup usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, SGInfo> sgMap = new HashMap<String, SGInfo>();
|
||||
|
||||
// loop through all the security groups, create a usage record for each
|
||||
for (UsageSecurityGroupVO usageSG : usageSGs) {
|
||||
long vmId = usageSG.getVmInstanceId();
|
||||
long sgId = usageSG.getSecurityGroupId();
|
||||
String key = ""+vmId+"SG"+sgId;
|
||||
|
||||
sgMap.put(key, new SGInfo(vmId, usageSG.getZoneId(), sgId));
|
||||
|
||||
Date sgCreateDate = usageSG.getCreated();
|
||||
Date sgDeleteDate = usageSG.getDeleted();
|
||||
|
||||
if ((sgDeleteDate == null) || sgDeleteDate.after(endDate)) {
|
||||
sgDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (sgCreateDate.before(startDate)) {
|
||||
sgCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (sgDeleteDate.getTime() - sgCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateSGUsageData(usageMap, key, usageSG.getVmInstanceId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String sgIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> sgtimeInfo = usageMap.get(sgIdKey);
|
||||
long useTime = sgtimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
SGInfo info = sgMap.get(sgIdKey);
|
||||
createUsageRecord(UsageTypes.SECURITY_GROUP, useTime, startDate, endDate, account, info.getVmId(), info.getSGId(), info.getZoneId());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateSGUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
|
||||
Pair<Long, Long> sgUsageInfo = usageDataMap.get(key);
|
||||
if (sgUsageInfo == null) {
|
||||
sgUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = sgUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
sgUsageInfo = new Pair<Long, Long>(sgUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, sgUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long sgId, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating security group:" + sgId + " usage record for Vm : " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Security Group: " + sgId + " for Vm : " + vmId + " usage time";
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), vmId, null, null, null, sgId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class SGInfo {
|
||||
private long vmId;
|
||||
private long zoneId;
|
||||
private long sgId;
|
||||
|
||||
public SGInfo(long vmId, long zoneId, long sgId) {
|
||||
this.vmId = vmId;
|
||||
this.zoneId = zoneId;
|
||||
this.sgId = sgId;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVmId() {
|
||||
return vmId;
|
||||
}
|
||||
public long getSGId() {
|
||||
return sgId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageSecurityGroupVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageSecurityGroupDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class SecurityGroupUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(SecurityGroupUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageSecurityGroupDao m_usageSecurityGroupDao = _locator.getDao(UsageSecurityGroupDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all SecurityGroup usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageSecurityGroupVO> usageSGs = m_usageSecurityGroupDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageSGs.isEmpty()){
|
||||
s_logger.debug("No SecurityGroup usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, SGInfo> sgMap = new HashMap<String, SGInfo>();
|
||||
|
||||
// loop through all the security groups, create a usage record for each
|
||||
for (UsageSecurityGroupVO usageSG : usageSGs) {
|
||||
long vmId = usageSG.getVmInstanceId();
|
||||
long sgId = usageSG.getSecurityGroupId();
|
||||
String key = ""+vmId+"SG"+sgId;
|
||||
|
||||
sgMap.put(key, new SGInfo(vmId, usageSG.getZoneId(), sgId));
|
||||
|
||||
Date sgCreateDate = usageSG.getCreated();
|
||||
Date sgDeleteDate = usageSG.getDeleted();
|
||||
|
||||
if ((sgDeleteDate == null) || sgDeleteDate.after(endDate)) {
|
||||
sgDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (sgCreateDate.before(startDate)) {
|
||||
sgCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (sgDeleteDate.getTime() - sgCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateSGUsageData(usageMap, key, usageSG.getVmInstanceId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String sgIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> sgtimeInfo = usageMap.get(sgIdKey);
|
||||
long useTime = sgtimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
SGInfo info = sgMap.get(sgIdKey);
|
||||
createUsageRecord(UsageTypes.SECURITY_GROUP, useTime, startDate, endDate, account, info.getVmId(), info.getSGId(), info.getZoneId());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateSGUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long vmId, long duration) {
|
||||
Pair<Long, Long> sgUsageInfo = usageDataMap.get(key);
|
||||
if (sgUsageInfo == null) {
|
||||
sgUsageInfo = new Pair<Long, Long>(new Long(vmId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = sgUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
sgUsageInfo = new Pair<Long, Long>(sgUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, sgUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, long sgId, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating security group:" + sgId + " usage record for Vm : " + vmId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Security Group: " + sgId + " for Vm : " + vmId + " usage time";
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), vmId, null, null, null, sgId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class SGInfo {
|
||||
private long vmId;
|
||||
private long zoneId;
|
||||
private long sgId;
|
||||
|
||||
public SGInfo(long vmId, long zoneId, long sgId) {
|
||||
this.vmId = vmId;
|
||||
this.zoneId = zoneId;
|
||||
this.sgId = sgId;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVmId() {
|
||||
return vmId;
|
||||
}
|
||||
public long getSGId() {
|
||||
return sgId;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,192 +10,192 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.StorageTypes;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageStorageVO;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageStorageDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class StorageUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(StorageUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageStorageDao m_usageStorageDao = _locator.getDao(UsageStorageDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all Storage usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageStorageVO> usageUsageStorages = m_usageStorageDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageUsageStorages.isEmpty()){
|
||||
s_logger.debug("No Storage usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
|
||||
Map<String, StorageInfo> storageMap = new HashMap<String, StorageInfo>();
|
||||
|
||||
// loop through all the usage volumes, create a usage record for each
|
||||
for (UsageStorageVO usageStorage : usageUsageStorages) {
|
||||
long storageId = usageStorage.getId();
|
||||
int storage_type = usageStorage.getStorageType();
|
||||
long size = usageStorage.getSize();
|
||||
long zoneId = usageStorage.getZoneId();
|
||||
Long sourceId = usageStorage.getSourceId();
|
||||
|
||||
String key = ""+storageId+"Z"+zoneId+"T"+storage_type;
|
||||
|
||||
// store the info in the storage map
|
||||
storageMap.put(key, new StorageInfo(zoneId, storageId, storage_type, sourceId, size));
|
||||
|
||||
Date storageCreateDate = usageStorage.getCreated();
|
||||
Date storageDeleteDate = usageStorage.getDeleted();
|
||||
|
||||
if ((storageDeleteDate == null) || storageDeleteDate.after(endDate)) {
|
||||
storageDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (storageCreateDate.before(startDate)) {
|
||||
storageCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (storageDeleteDate.getTime() - storageCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
updateStorageUsageData(usageMap, key, usageStorage.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String storageIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> storagetimeInfo = usageMap.get(storageIdKey);
|
||||
long useTime = storagetimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
StorageInfo info = storageMap.get(storageIdKey);
|
||||
createUsageRecord(info.getZoneId(), info.getStorageType(), useTime, startDate, endDate, account, info.getStorageId(), info.getSourceId(), info.getSize());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateStorageUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long storageId, long duration) {
|
||||
Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
|
||||
if (volUsageInfo == null) {
|
||||
volUsageInfo = new Pair<Long, Long>(new Long(storageId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = volUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
volUsageInfo = new Pair<Long, Long>(volUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, volUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(long zoneId, int type, long runningTime, Date startDate, Date endDate, AccountVO account, long storageId, Long sourceId, long size) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating Storage usage record for type: "+ type + " with id: " + storageId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
String usageDesc = "";
|
||||
Long tmplSourceId = null;
|
||||
|
||||
int usage_type = 0;
|
||||
switch(type){
|
||||
case StorageTypes.TEMPLATE:
|
||||
usage_type = UsageTypes.TEMPLATE;
|
||||
usageDesc += "Template ";
|
||||
tmplSourceId = sourceId;
|
||||
break;
|
||||
case StorageTypes.ISO:
|
||||
usage_type = UsageTypes.ISO;
|
||||
usageDesc += "ISO ";
|
||||
break;
|
||||
case StorageTypes.SNAPSHOT:
|
||||
usage_type = UsageTypes.SNAPSHOT;
|
||||
usageDesc += "Snapshot ";
|
||||
break;
|
||||
}
|
||||
// Create the usage record
|
||||
usageDesc += "Id:"+storageId+" Size:"+size;
|
||||
|
||||
//ToDo: get zone id
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usage_type,
|
||||
new Double(usage), null, null, null, tmplSourceId, storageId, size, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class StorageInfo {
|
||||
private long zoneId;
|
||||
private long storageId;
|
||||
private int storageType;
|
||||
private Long sourceId;
|
||||
private long size;
|
||||
|
||||
public StorageInfo(long zoneId, long storageId, int storageType, Long sourceId, long size) {
|
||||
this.zoneId = zoneId;
|
||||
this.storageId = storageId;
|
||||
this.storageType = storageType;
|
||||
this.sourceId = sourceId;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
public long getStorageId() {
|
||||
return storageId;
|
||||
}
|
||||
|
||||
public int getStorageType() {
|
||||
return storageType;
|
||||
}
|
||||
|
||||
public long getSourceId() {
|
||||
return sourceId;
|
||||
}
|
||||
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.StorageTypes;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageStorageVO;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageStorageDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class StorageUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(StorageUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageStorageDao m_usageStorageDao = _locator.getDao(UsageStorageDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all Storage usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageStorageVO> usageUsageStorages = m_usageStorageDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageUsageStorages.isEmpty()){
|
||||
s_logger.debug("No Storage usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
|
||||
Map<String, StorageInfo> storageMap = new HashMap<String, StorageInfo>();
|
||||
|
||||
// loop through all the usage volumes, create a usage record for each
|
||||
for (UsageStorageVO usageStorage : usageUsageStorages) {
|
||||
long storageId = usageStorage.getId();
|
||||
int storage_type = usageStorage.getStorageType();
|
||||
long size = usageStorage.getSize();
|
||||
long zoneId = usageStorage.getZoneId();
|
||||
Long sourceId = usageStorage.getSourceId();
|
||||
|
||||
String key = ""+storageId+"Z"+zoneId+"T"+storage_type;
|
||||
|
||||
// store the info in the storage map
|
||||
storageMap.put(key, new StorageInfo(zoneId, storageId, storage_type, sourceId, size));
|
||||
|
||||
Date storageCreateDate = usageStorage.getCreated();
|
||||
Date storageDeleteDate = usageStorage.getDeleted();
|
||||
|
||||
if ((storageDeleteDate == null) || storageDeleteDate.after(endDate)) {
|
||||
storageDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (storageCreateDate.before(startDate)) {
|
||||
storageCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (storageDeleteDate.getTime() - storageCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
updateStorageUsageData(usageMap, key, usageStorage.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String storageIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> storagetimeInfo = usageMap.get(storageIdKey);
|
||||
long useTime = storagetimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
StorageInfo info = storageMap.get(storageIdKey);
|
||||
createUsageRecord(info.getZoneId(), info.getStorageType(), useTime, startDate, endDate, account, info.getStorageId(), info.getSourceId(), info.getSize());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateStorageUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long storageId, long duration) {
|
||||
Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
|
||||
if (volUsageInfo == null) {
|
||||
volUsageInfo = new Pair<Long, Long>(new Long(storageId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = volUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
volUsageInfo = new Pair<Long, Long>(volUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, volUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(long zoneId, int type, long runningTime, Date startDate, Date endDate, AccountVO account, long storageId, Long sourceId, long size) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating Storage usage record for type: "+ type + " with id: " + storageId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
String usageDesc = "";
|
||||
Long tmplSourceId = null;
|
||||
|
||||
int usage_type = 0;
|
||||
switch(type){
|
||||
case StorageTypes.TEMPLATE:
|
||||
usage_type = UsageTypes.TEMPLATE;
|
||||
usageDesc += "Template ";
|
||||
tmplSourceId = sourceId;
|
||||
break;
|
||||
case StorageTypes.ISO:
|
||||
usage_type = UsageTypes.ISO;
|
||||
usageDesc += "ISO ";
|
||||
break;
|
||||
case StorageTypes.SNAPSHOT:
|
||||
usage_type = UsageTypes.SNAPSHOT;
|
||||
usageDesc += "Snapshot ";
|
||||
break;
|
||||
}
|
||||
// Create the usage record
|
||||
usageDesc += "Id:"+storageId+" Size:"+size;
|
||||
|
||||
//ToDo: get zone id
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", usage_type,
|
||||
new Double(usage), null, null, null, tmplSourceId, storageId, size, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class StorageInfo {
|
||||
private long zoneId;
|
||||
private long storageId;
|
||||
private int storageType;
|
||||
private Long sourceId;
|
||||
private long size;
|
||||
|
||||
public StorageInfo(long zoneId, long storageId, int storageType, Long sourceId, long size) {
|
||||
this.zoneId = zoneId;
|
||||
this.storageId = storageId;
|
||||
this.storageType = storageType;
|
||||
this.sourceId = sourceId;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
|
||||
public long getStorageId() {
|
||||
return storageId;
|
||||
}
|
||||
|
||||
public int getStorageType() {
|
||||
return storageType;
|
||||
}
|
||||
|
||||
public long getSourceId() {
|
||||
return sourceId;
|
||||
}
|
||||
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,22 +10,22 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public abstract class UsageParser implements Runnable {
|
||||
public static final Logger s_logger = Logger.getLogger(UsageParser.class.getName());
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
parse(null);
|
||||
} catch (Exception e) {
|
||||
s_logger.warn("Error while parsing usage events", e);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void parse(Date endDate);
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public abstract class UsageParser implements Runnable {
|
||||
public static final Logger s_logger = Logger.getLogger(UsageParser.class.getName());
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
parse(null);
|
||||
} catch (Exception e) {
|
||||
s_logger.warn("Error while parsing usage events", e);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void parse(Date endDate);
|
||||
}
|
||||
|
||||
@ -10,187 +10,187 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVMInstanceVO;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageVMInstanceDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class VMInstanceUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(VMInstanceUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageVMInstanceDao m_usageInstanceDao = _locator.getDao(UsageVMInstanceDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all VMInstance usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_vm_instance table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageVMInstanceVO> usageInstances = m_usageInstanceDao.getUsageRecords(account.getId(), startDate, endDate);
|
||||
//ToDo: Add domainID for getting usage records
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<String, Long>> usageVMUptimeMap = new HashMap<String, Pair<String, Long>>();
|
||||
Map<String, Pair<String, Long>> allocatedVMMap = new HashMap<String, Pair<String, Long>>();
|
||||
|
||||
Map<String, VMInfo> vmServiceOfferingMap = new HashMap<String, VMInfo>();
|
||||
|
||||
// loop through all the usage instances, create a usage record for each
|
||||
for (UsageVMInstanceVO usageInstance : usageInstances) {
|
||||
long vmId = usageInstance.getVmInstanceId();
|
||||
long soId = usageInstance.getSerivceOfferingId();
|
||||
long zoneId = usageInstance.getZoneId();
|
||||
long tId = usageInstance.getTemplateId();
|
||||
int usageType = usageInstance.getUsageType();
|
||||
String key = vmId + "-" + soId + "-" + usageType;
|
||||
|
||||
// store the info in the service offering map
|
||||
vmServiceOfferingMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType()));
|
||||
|
||||
Date vmStartDate = usageInstance.getStartDate();
|
||||
Date vmEndDate = usageInstance.getEndDate();
|
||||
|
||||
if ((vmEndDate == null) || vmEndDate.after(endDate)) {
|
||||
vmEndDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (vmStartDate.before(startDate)) {
|
||||
vmStartDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (vmEndDate.getTime() - vmStartDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
switch (usageType) {
|
||||
case UsageTypes.ALLOCATED_VM:
|
||||
updateVmUsageData(allocatedVMMap, key, usageInstance.getVmName(), currentDuration);
|
||||
break;
|
||||
case UsageTypes.RUNNING_VM:
|
||||
updateVmUsageData(usageVMUptimeMap, key, usageInstance.getVmName(), currentDuration);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (String vmIdKey : usageVMUptimeMap.keySet()) {
|
||||
Pair<String, Long> vmUptimeInfo = usageVMUptimeMap.get(vmIdKey);
|
||||
long runningTime = vmUptimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (runningTime > 0L) {
|
||||
VMInfo info = vmServiceOfferingMap.get(vmIdKey);
|
||||
createUsageRecord(UsageTypes.RUNNING_VM, runningTime, startDate, endDate, account, info.getVirtualMachineId(), vmUptimeInfo.first(), info.getZoneId(),
|
||||
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
|
||||
}
|
||||
}
|
||||
|
||||
for (String vmIdKey : allocatedVMMap.keySet()) {
|
||||
Pair<String, Long> vmAllocInfo = allocatedVMMap.get(vmIdKey);
|
||||
long allocatedTime = vmAllocInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (allocatedTime > 0L) {
|
||||
VMInfo info = vmServiceOfferingMap.get(vmIdKey);
|
||||
createUsageRecord(UsageTypes.ALLOCATED_VM, allocatedTime, startDate, endDate, account, info.getVirtualMachineId(), vmAllocInfo.first(), info.getZoneId(),
|
||||
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateVmUsageData(Map<String, Pair<String, Long>> usageDataMap, String key, String vmName, long duration) {
|
||||
Pair<String, Long> vmUsageInfo = usageDataMap.get(key);
|
||||
if (vmUsageInfo == null) {
|
||||
vmUsageInfo = new Pair<String, Long>(vmName, new Long(duration));
|
||||
} else {
|
||||
Long runningTime = vmUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
vmUsageInfo = new Pair<String, Long>(vmUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, vmUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId, long serviceOfferingId, long templateId, String hypervisorType) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating VM usage record for vm: " + vmName + ", type: " + type + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = vmName;
|
||||
if (type == UsageTypes.ALLOCATED_VM) {
|
||||
usageDesc += " allocated";
|
||||
} else {
|
||||
usageDesc += " running time";
|
||||
}
|
||||
usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")";
|
||||
UsageVO usageRecord = new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), Long.valueOf(vmId), vmName, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class VMInfo {
|
||||
private long virtualMachineId;
|
||||
private long zoneId;
|
||||
private long serviceOfferingId;
|
||||
private long templateId;
|
||||
private String hypervisorType;
|
||||
|
||||
public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType) {
|
||||
virtualMachineId = vmId;
|
||||
zoneId = zId;
|
||||
serviceOfferingId = soId;
|
||||
templateId = tId;
|
||||
this.hypervisorType = hypervisorType;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVirtualMachineId() {
|
||||
return virtualMachineId;
|
||||
}
|
||||
public long getServiceOfferingId() {
|
||||
return serviceOfferingId;
|
||||
}
|
||||
public long getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
private String getHypervisorType(){
|
||||
return hypervisorType;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVMInstanceVO;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageVMInstanceDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class VMInstanceUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(VMInstanceUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageVMInstanceDao m_usageInstanceDao = _locator.getDao(UsageVMInstanceDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all VMInstance usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_vm_instance table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageVMInstanceVO> usageInstances = m_usageInstanceDao.getUsageRecords(account.getId(), startDate, endDate);
|
||||
//ToDo: Add domainID for getting usage records
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<String, Long>> usageVMUptimeMap = new HashMap<String, Pair<String, Long>>();
|
||||
Map<String, Pair<String, Long>> allocatedVMMap = new HashMap<String, Pair<String, Long>>();
|
||||
|
||||
Map<String, VMInfo> vmServiceOfferingMap = new HashMap<String, VMInfo>();
|
||||
|
||||
// loop through all the usage instances, create a usage record for each
|
||||
for (UsageVMInstanceVO usageInstance : usageInstances) {
|
||||
long vmId = usageInstance.getVmInstanceId();
|
||||
long soId = usageInstance.getSerivceOfferingId();
|
||||
long zoneId = usageInstance.getZoneId();
|
||||
long tId = usageInstance.getTemplateId();
|
||||
int usageType = usageInstance.getUsageType();
|
||||
String key = vmId + "-" + soId + "-" + usageType;
|
||||
|
||||
// store the info in the service offering map
|
||||
vmServiceOfferingMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType()));
|
||||
|
||||
Date vmStartDate = usageInstance.getStartDate();
|
||||
Date vmEndDate = usageInstance.getEndDate();
|
||||
|
||||
if ((vmEndDate == null) || vmEndDate.after(endDate)) {
|
||||
vmEndDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (vmStartDate.before(startDate)) {
|
||||
vmStartDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (vmEndDate.getTime() - vmStartDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
switch (usageType) {
|
||||
case UsageTypes.ALLOCATED_VM:
|
||||
updateVmUsageData(allocatedVMMap, key, usageInstance.getVmName(), currentDuration);
|
||||
break;
|
||||
case UsageTypes.RUNNING_VM:
|
||||
updateVmUsageData(usageVMUptimeMap, key, usageInstance.getVmName(), currentDuration);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (String vmIdKey : usageVMUptimeMap.keySet()) {
|
||||
Pair<String, Long> vmUptimeInfo = usageVMUptimeMap.get(vmIdKey);
|
||||
long runningTime = vmUptimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (runningTime > 0L) {
|
||||
VMInfo info = vmServiceOfferingMap.get(vmIdKey);
|
||||
createUsageRecord(UsageTypes.RUNNING_VM, runningTime, startDate, endDate, account, info.getVirtualMachineId(), vmUptimeInfo.first(), info.getZoneId(),
|
||||
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
|
||||
}
|
||||
}
|
||||
|
||||
for (String vmIdKey : allocatedVMMap.keySet()) {
|
||||
Pair<String, Long> vmAllocInfo = allocatedVMMap.get(vmIdKey);
|
||||
long allocatedTime = vmAllocInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (allocatedTime > 0L) {
|
||||
VMInfo info = vmServiceOfferingMap.get(vmIdKey);
|
||||
createUsageRecord(UsageTypes.ALLOCATED_VM, allocatedTime, startDate, endDate, account, info.getVirtualMachineId(), vmAllocInfo.first(), info.getZoneId(),
|
||||
info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateVmUsageData(Map<String, Pair<String, Long>> usageDataMap, String key, String vmName, long duration) {
|
||||
Pair<String, Long> vmUsageInfo = usageDataMap.get(key);
|
||||
if (vmUsageInfo == null) {
|
||||
vmUsageInfo = new Pair<String, Long>(vmName, new Long(duration));
|
||||
} else {
|
||||
Long runningTime = vmUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
vmUsageInfo = new Pair<String, Long>(vmUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, vmUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId, long serviceOfferingId, long templateId, String hypervisorType) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating VM usage record for vm: " + vmName + ", type: " + type + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = vmName;
|
||||
if (type == UsageTypes.ALLOCATED_VM) {
|
||||
usageDesc += " allocated";
|
||||
} else {
|
||||
usageDesc += " running time";
|
||||
}
|
||||
usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")";
|
||||
UsageVO usageRecord = new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), Long.valueOf(vmId), vmName, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class VMInfo {
|
||||
private long virtualMachineId;
|
||||
private long zoneId;
|
||||
private long serviceOfferingId;
|
||||
private long templateId;
|
||||
private String hypervisorType;
|
||||
|
||||
public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType) {
|
||||
virtualMachineId = vmId;
|
||||
zoneId = zId;
|
||||
serviceOfferingId = soId;
|
||||
templateId = tId;
|
||||
this.hypervisorType = hypervisorType;
|
||||
}
|
||||
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVirtualMachineId() {
|
||||
return virtualMachineId;
|
||||
}
|
||||
public long getServiceOfferingId() {
|
||||
return serviceOfferingId;
|
||||
}
|
||||
public long getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
private String getHypervisorType(){
|
||||
return hypervisorType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,146 +10,146 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageVPNUserVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageVPNUserDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class VPNUserUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(VPNUserUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageVPNUserDao m_usageVPNUserDao = _locator.getDao(UsageVPNUserDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all VPN user usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
List<UsageVPNUserVO> usageVUs = m_usageVPNUserDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageVUs.isEmpty()){
|
||||
s_logger.debug("No VPN user usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, VUInfo> vuMap = new HashMap<String, VUInfo>();
|
||||
|
||||
// loop through all the VPN user usage, create a usage record for each
|
||||
for (UsageVPNUserVO usageVU : usageVUs) {
|
||||
long userId = usageVU.getUserId();
|
||||
String userName = usageVU.getUsername();
|
||||
String key = ""+userId+"VU"+userName;
|
||||
|
||||
vuMap.put(key, new VUInfo(userId, usageVU.getZoneId(), userName));
|
||||
|
||||
Date vuCreateDate = usageVU.getCreated();
|
||||
Date vuDeleteDate = usageVU.getDeleted();
|
||||
|
||||
if ((vuDeleteDate == null) || vuDeleteDate.after(endDate)) {
|
||||
vuDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (vuCreateDate.before(startDate)) {
|
||||
vuCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (vuDeleteDate.getTime() - vuCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateVUUsageData(usageMap, key, usageVU.getUserId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String vuIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> vutimeInfo = usageMap.get(vuIdKey);
|
||||
long useTime = vutimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
VUInfo info = vuMap.get(vuIdKey);
|
||||
createUsageRecord(UsageTypes.VPN_USERS, useTime, startDate, endDate, account, info.getUserId(), info.getUserName(), info.getZoneId());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateVUUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long userId, long duration) {
|
||||
Pair<Long, Long> vuUsageInfo = usageDataMap.get(key);
|
||||
if (vuUsageInfo == null) {
|
||||
vuUsageInfo = new Pair<Long, Long>(new Long(userId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = vuUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
vuUsageInfo = new Pair<Long, Long>(vuUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, vuUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long userId, String userName, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating VPN user:" + userId + " usage record, usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "VPN User: " + userName + ", Id: "+ userId + " usage time";
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, null, null, userId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class VUInfo {
|
||||
private long userId;
|
||||
private long zoneId;
|
||||
private String userName;
|
||||
|
||||
public VUInfo(long userId, long zoneId, String userName) {
|
||||
this.userId = userId;
|
||||
this.zoneId = zoneId;
|
||||
this.userName = userName;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageVPNUserVO;
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageVPNUserDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class VPNUserUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(VPNUserUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageVPNUserDao m_usageVPNUserDao = _locator.getDao(UsageVPNUserDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all VPN user usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
List<UsageVPNUserVO> usageVUs = m_usageVPNUserDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageVUs.isEmpty()){
|
||||
s_logger.debug("No VPN user usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
Map<String, VUInfo> vuMap = new HashMap<String, VUInfo>();
|
||||
|
||||
// loop through all the VPN user usage, create a usage record for each
|
||||
for (UsageVPNUserVO usageVU : usageVUs) {
|
||||
long userId = usageVU.getUserId();
|
||||
String userName = usageVU.getUsername();
|
||||
String key = ""+userId+"VU"+userName;
|
||||
|
||||
vuMap.put(key, new VUInfo(userId, usageVU.getZoneId(), userName));
|
||||
|
||||
Date vuCreateDate = usageVU.getCreated();
|
||||
Date vuDeleteDate = usageVU.getDeleted();
|
||||
|
||||
if ((vuDeleteDate == null) || vuDeleteDate.after(endDate)) {
|
||||
vuDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (vuCreateDate.before(startDate)) {
|
||||
vuCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (vuDeleteDate.getTime() - vuCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateVUUsageData(usageMap, key, usageVU.getUserId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String vuIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> vutimeInfo = usageMap.get(vuIdKey);
|
||||
long useTime = vutimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
VUInfo info = vuMap.get(vuIdKey);
|
||||
createUsageRecord(UsageTypes.VPN_USERS, useTime, startDate, endDate, account, info.getUserId(), info.getUserName(), info.getZoneId());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateVUUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long userId, long duration) {
|
||||
Pair<Long, Long> vuUsageInfo = usageDataMap.get(key);
|
||||
if (vuUsageInfo == null) {
|
||||
vuUsageInfo = new Pair<Long, Long>(new Long(userId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = vuUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
vuUsageInfo = new Pair<Long, Long>(vuUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, vuUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long userId, String userName, long zoneId) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating VPN user:" + userId + " usage record, usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "VPN User: " + userName + ", Id: "+ userId + " usage time";
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, null, null, userId, null, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class VUInfo {
|
||||
private long userId;
|
||||
private long zoneId;
|
||||
private String userName;
|
||||
|
||||
public VUInfo(long userId, long zoneId, String userName) {
|
||||
this.userId = userId;
|
||||
this.zoneId = zoneId;
|
||||
this.userName = userName;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -10,170 +10,170 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
// Automatically generated by addcopyright.py at 04/03/2012
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.UsageVolumeVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageVolumeDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class VolumeUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(VolumeUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageVolumeDao m_usageVolumeDao = _locator.getDao(UsageVolumeDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all Volume usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageVolumeVO> usageUsageVols = m_usageVolumeDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageUsageVols.isEmpty()){
|
||||
s_logger.debug("No volume usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
|
||||
Map<String, VolInfo> diskOfferingMap = new HashMap<String, VolInfo>();
|
||||
|
||||
// loop through all the usage volumes, create a usage record for each
|
||||
for (UsageVolumeVO usageVol : usageUsageVols) {
|
||||
long volId = usageVol.getId();
|
||||
Long doId = usageVol.getDiskOfferingId();
|
||||
long zoneId = usageVol.getZoneId();
|
||||
Long templateId = usageVol.getTemplateId();
|
||||
long size = usageVol.getSize();
|
||||
String key = ""+volId;
|
||||
|
||||
diskOfferingMap.put(key, new VolInfo(volId, zoneId, doId, templateId, size));
|
||||
|
||||
Date volCreateDate = usageVol.getCreated();
|
||||
Date volDeleteDate = usageVol.getDeleted();
|
||||
|
||||
if ((volDeleteDate == null) || volDeleteDate.after(endDate)) {
|
||||
volDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (volCreateDate.before(startDate)) {
|
||||
volCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (volDeleteDate.getTime() - volCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateVolUsageData(usageMap, key, usageVol.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String volIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> voltimeInfo = usageMap.get(volIdKey);
|
||||
long useTime = voltimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
VolInfo info = diskOfferingMap.get(volIdKey);
|
||||
createUsageRecord(UsageTypes.VOLUME, useTime, startDate, endDate, account, info.getVolumeId(), info.getZoneId(), info.getDiskOfferingId(), info.getTemplateId(), info.getSize());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateVolUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long volId, long duration) {
|
||||
Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
|
||||
if (volUsageInfo == null) {
|
||||
volUsageInfo = new Pair<Long, Long>(new Long(volId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = volUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
volUsageInfo = new Pair<Long, Long>(volUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, volUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId, Long templateId, long size) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating Volume usage record for vol: " + volId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Volume Id: "+volId+" usage time";
|
||||
|
||||
if(templateId != null){
|
||||
usageDesc += " (Template: " +templateId+ ")";
|
||||
} else if(doId != null){
|
||||
usageDesc += " (DiskOffering: " +doId+ ")";
|
||||
}
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, doId, templateId, volId, size, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class VolInfo {
|
||||
private long volId;
|
||||
private long zoneId;
|
||||
private Long diskOfferingId;
|
||||
private Long templateId;
|
||||
private long size;
|
||||
|
||||
public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, long size) {
|
||||
this.volId = volId;
|
||||
this.zoneId = zoneId;
|
||||
this.diskOfferingId = diskOfferingId;
|
||||
this.templateId = templateId;
|
||||
this.size = size;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVolumeId() {
|
||||
return volId;
|
||||
}
|
||||
public Long getDiskOfferingId() {
|
||||
return diskOfferingId;
|
||||
}
|
||||
public Long getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.cloud.usage.parser;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.usage.UsageServer;
|
||||
import com.cloud.usage.UsageTypes;
|
||||
import com.cloud.usage.UsageVO;
|
||||
import com.cloud.usage.UsageVolumeVO;
|
||||
import com.cloud.usage.dao.UsageDao;
|
||||
import com.cloud.usage.dao.UsageVolumeDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
|
||||
public class VolumeUsageParser {
|
||||
public static final Logger s_logger = Logger.getLogger(VolumeUsageParser.class.getName());
|
||||
|
||||
private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage");
|
||||
private static UsageDao m_usageDao = _locator.getDao(UsageDao.class);
|
||||
private static UsageVolumeDao m_usageVolumeDao = _locator.getDao(UsageVolumeDao.class);
|
||||
|
||||
public static boolean parse(AccountVO account, Date startDate, Date endDate) {
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Parsing all Volume usage events for account: " + account.getId());
|
||||
}
|
||||
if ((endDate == null) || endDate.after(new Date())) {
|
||||
endDate = new Date();
|
||||
}
|
||||
|
||||
// - query usage_volume table with the following criteria:
|
||||
// - look for an entry for accountId with start 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 start date before given range *and* end date after given range
|
||||
List<UsageVolumeVO> usageUsageVols = m_usageVolumeDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
|
||||
|
||||
if(usageUsageVols.isEmpty()){
|
||||
s_logger.debug("No volume usage events for this period");
|
||||
return true;
|
||||
}
|
||||
|
||||
// This map has both the running time *and* the usage amount.
|
||||
Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
|
||||
|
||||
Map<String, VolInfo> diskOfferingMap = new HashMap<String, VolInfo>();
|
||||
|
||||
// loop through all the usage volumes, create a usage record for each
|
||||
for (UsageVolumeVO usageVol : usageUsageVols) {
|
||||
long volId = usageVol.getId();
|
||||
Long doId = usageVol.getDiskOfferingId();
|
||||
long zoneId = usageVol.getZoneId();
|
||||
Long templateId = usageVol.getTemplateId();
|
||||
long size = usageVol.getSize();
|
||||
String key = ""+volId;
|
||||
|
||||
diskOfferingMap.put(key, new VolInfo(volId, zoneId, doId, templateId, size));
|
||||
|
||||
Date volCreateDate = usageVol.getCreated();
|
||||
Date volDeleteDate = usageVol.getDeleted();
|
||||
|
||||
if ((volDeleteDate == null) || volDeleteDate.after(endDate)) {
|
||||
volDeleteDate = endDate;
|
||||
}
|
||||
|
||||
// clip the start date to the beginning of our aggregation range if the vm has been running for a while
|
||||
if (volCreateDate.before(startDate)) {
|
||||
volCreateDate = startDate;
|
||||
}
|
||||
|
||||
long currentDuration = (volDeleteDate.getTime() - volCreateDate.getTime()) + 1; // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
|
||||
|
||||
|
||||
updateVolUsageData(usageMap, key, usageVol.getId(), currentDuration);
|
||||
}
|
||||
|
||||
for (String volIdKey : usageMap.keySet()) {
|
||||
Pair<Long, Long> voltimeInfo = usageMap.get(volIdKey);
|
||||
long useTime = voltimeInfo.second().longValue();
|
||||
|
||||
// Only create a usage record if we have a runningTime of bigger than zero.
|
||||
if (useTime > 0L) {
|
||||
VolInfo info = diskOfferingMap.get(volIdKey);
|
||||
createUsageRecord(UsageTypes.VOLUME, useTime, startDate, endDate, account, info.getVolumeId(), info.getZoneId(), info.getDiskOfferingId(), info.getTemplateId(), info.getSize());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void updateVolUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long volId, long duration) {
|
||||
Pair<Long, Long> volUsageInfo = usageDataMap.get(key);
|
||||
if (volUsageInfo == null) {
|
||||
volUsageInfo = new Pair<Long, Long>(new Long(volId), new Long(duration));
|
||||
} else {
|
||||
Long runningTime = volUsageInfo.second();
|
||||
runningTime = new Long(runningTime.longValue() + duration);
|
||||
volUsageInfo = new Pair<Long, Long>(volUsageInfo.first(), runningTime);
|
||||
}
|
||||
usageDataMap.put(key, volUsageInfo);
|
||||
}
|
||||
|
||||
private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long volId, long zoneId, Long doId, Long templateId, long size) {
|
||||
// Our smallest increment is hourly for now
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Total running time " + runningTime + "ms");
|
||||
}
|
||||
|
||||
float usage = runningTime / 1000f / 60f / 60f;
|
||||
|
||||
DecimalFormat dFormat = new DecimalFormat("#.######");
|
||||
String usageDisplay = dFormat.format(usage);
|
||||
|
||||
if (s_logger.isDebugEnabled()) {
|
||||
s_logger.debug("Creating Volume usage record for vol: " + volId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId());
|
||||
}
|
||||
|
||||
// Create the usage record
|
||||
String usageDesc = "Volume Id: "+volId+" usage time";
|
||||
|
||||
if(templateId != null){
|
||||
usageDesc += " (Template: " +templateId+ ")";
|
||||
} else if(doId != null){
|
||||
usageDesc += " (DiskOffering: " +doId+ ")";
|
||||
}
|
||||
|
||||
UsageVO usageRecord = new UsageVO(zoneId, account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type,
|
||||
new Double(usage), null, null, doId, templateId, volId, size, startDate, endDate);
|
||||
m_usageDao.persist(usageRecord);
|
||||
}
|
||||
|
||||
private static class VolInfo {
|
||||
private long volId;
|
||||
private long zoneId;
|
||||
private Long diskOfferingId;
|
||||
private Long templateId;
|
||||
private long size;
|
||||
|
||||
public VolInfo(long volId, long zoneId, Long diskOfferingId, Long templateId, long size) {
|
||||
this.volId = volId;
|
||||
this.zoneId = zoneId;
|
||||
this.diskOfferingId = diskOfferingId;
|
||||
this.templateId = templateId;
|
||||
this.size = size;
|
||||
}
|
||||
public long getZoneId() {
|
||||
return zoneId;
|
||||
}
|
||||
public long getVolumeId() {
|
||||
return volId;
|
||||
}
|
||||
public Long getDiskOfferingId() {
|
||||
return diskOfferingId;
|
||||
}
|
||||
public Long getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user