Search in sources :

Example 1 with UsageVPNUserVO

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

the class VPNUserUsageParser method parse.

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;
        }
        // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
        long currentDuration = (vuDeleteDate.getTime() - vuCreateDate.getTime()) + 1;
        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;
}
Also used : HashMap(java.util.HashMap) UsageVPNUserVO(com.cloud.usage.UsageVPNUserVO) Date(java.util.Date) Pair(com.cloud.utils.Pair)

Example 2 with UsageVPNUserVO

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

the class VPNUserUsageParser method parse.

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 = s_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;
        }
        if (vuCreateDate.after(endDate)) {
            //Ignore records created after endDate
            continue;
        }
        // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
        long currentDuration = (vuDeleteDate.getTime() - vuCreateDate.getTime()) + 1;
        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;
}
Also used : HashMap(java.util.HashMap) UsageVPNUserVO(com.cloud.usage.UsageVPNUserVO) Date(java.util.Date) Pair(com.cloud.utils.Pair)

Example 3 with UsageVPNUserVO

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

the class UsageVPNUserDaoImpl method getUsageRecords.

@Override
public List<UsageVPNUserVO> getUsageRecords(Long accountId, Long domainId, Date startDate, Date endDate, boolean limit, int page) {
    List<UsageVPNUserVO> usageRecords = new ArrayList<UsageVPNUserVO>();
    Long param1 = null;
    String sql = null;
    if (accountId != null) {
        sql = GET_USAGE_RECORDS_BY_ACCOUNT;
        param1 = accountId;
    } else if (domainId != null) {
        sql = GET_USAGE_RECORDS_BY_DOMAIN;
        param1 = domainId;
    } else {
        sql = GET_ALL_USAGE_RECORDS;
    }
    if (limit) {
        int startIndex = 0;
        if (page > 0) {
            startIndex = 500 * (page - 1);
        }
        sql += " LIMIT " + startIndex + ",500";
    }
    TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
    PreparedStatement pstmt = null;
    try {
        int i = 1;
        pstmt = txn.prepareAutoCloseStatement(sql);
        if (param1 != null) {
            pstmt.setLong(i++, param1);
        }
        pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
        pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
        pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
        pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
        pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate));
        pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate));
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            //zoneId, account_id, domain_id, user_id, user_name, created, deleted
            Long zoneId = Long.valueOf(rs.getLong(1));
            Long acctId = Long.valueOf(rs.getLong(2));
            Long dId = Long.valueOf(rs.getLong(3));
            long userId = Long.valueOf(rs.getLong(4));
            String userName = rs.getString(5);
            Date createdDate = null;
            Date deletedDate = null;
            String createdTS = rs.getString(6);
            String deletedTS = rs.getString(7);
            if (createdTS != null) {
                createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS);
            }
            if (deletedTS != null) {
                deletedDate = DateUtil.parseDateString(s_gmtTimeZone, deletedTS);
            }
            usageRecords.add(new UsageVPNUserVO(zoneId, acctId, dId, userId, userName, createdDate, deletedDate));
        }
    } catch (Exception e) {
        txn.rollback();
        s_logger.warn("Error getting usage records", e);
    } finally {
        txn.close();
    }
    return usageRecords;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) UsageVPNUserVO(com.cloud.usage.UsageVPNUserVO) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) SQLException(java.sql.SQLException) CloudException(com.cloud.exception.CloudException)

Aggregations

UsageVPNUserVO (com.cloud.usage.UsageVPNUserVO)3 Date (java.util.Date)3 Pair (com.cloud.utils.Pair)2 HashMap (java.util.HashMap)2 CloudException (com.cloud.exception.CloudException)1 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1