|
10 | 10 | // limitations under the License. |
11 | 11 | // |
12 | 12 | // Automatically generated by addcopyright.py at 04/03/2012 |
13 | | -package com.cloud.usage.parser; |
14 | | - |
15 | | -import java.text.DecimalFormat; |
16 | | -import java.util.Date; |
17 | | -import java.util.HashMap; |
18 | | -import java.util.List; |
19 | | -import java.util.Map; |
20 | | - |
21 | | -import org.apache.log4j.Logger; |
22 | | - |
23 | | -import com.cloud.usage.UsageIPAddressVO; |
24 | | -import com.cloud.usage.UsageServer; |
25 | | -import com.cloud.usage.UsageTypes; |
26 | | -import com.cloud.usage.UsageVO; |
27 | | -import com.cloud.usage.dao.UsageDao; |
28 | | -import com.cloud.usage.dao.UsageIPAddressDao; |
29 | | -import com.cloud.user.AccountVO; |
30 | | -import com.cloud.utils.Pair; |
31 | | -import com.cloud.utils.component.ComponentLocator; |
32 | | - |
33 | | -public class IPAddressUsageParser { |
34 | | - public static final Logger s_logger = Logger.getLogger(IPAddressUsageParser.class.getName()); |
35 | | - |
36 | | - private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage"); |
37 | | - private static UsageDao m_usageDao = _locator.getDao(UsageDao.class); |
38 | | - private static UsageIPAddressDao m_usageIPAddressDao = _locator.getDao(UsageIPAddressDao.class); |
39 | | - |
40 | | - |
41 | | - public static boolean parse(AccountVO account, Date startDate, Date endDate) { |
42 | | - if (s_logger.isDebugEnabled()) { |
43 | | - s_logger.debug("Parsing IP Address usage for account: " + account.getId()); |
44 | | - } |
45 | | - if ((endDate == null) || endDate.after(new Date())) { |
46 | | - endDate = new Date(); |
47 | | - } |
48 | | - |
49 | | - // - query usage_ip_address table with the following criteria: |
50 | | - // - look for an entry for accountId with start date in the given range |
51 | | - // - look for an entry for accountId with end date in the given range |
52 | | - // - look for an entry for accountId with end date null (currently running vm or owned IP) |
53 | | - // - look for an entry for accountId with start date before given range *and* end date after given range |
54 | | - List<UsageIPAddressVO> usageIPAddress = m_usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate); |
55 | | - |
56 | | - if(usageIPAddress.isEmpty()){ |
57 | | - s_logger.debug("No IP Address usage for this period"); |
58 | | - return true; |
59 | | - } |
60 | | - |
61 | | - // This map has both the running time *and* the usage amount. |
62 | | - Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>(); |
63 | | - |
64 | | - Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>(); |
65 | | - |
66 | | - // loop through all the usage IPs, create a usage record for each |
67 | | - for (UsageIPAddressVO usageIp : usageIPAddress) { |
68 | | - long IpId = usageIp.getId(); |
69 | | - |
70 | | - String key = ""+IpId; |
71 | | - |
72 | | - // store the info in the IP map |
73 | | - IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat(), usageIp.isSystem())); |
74 | | - |
75 | | - Date IpAssignDate = usageIp.getAssigned(); |
76 | | - Date IpReleaseDeleteDate = usageIp.getReleased(); |
77 | | - |
78 | | - if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) { |
79 | | - IpReleaseDeleteDate = endDate; |
80 | | - } |
81 | | - |
82 | | - // clip the start date to the beginning of our aggregation range if the vm has been running for a while |
83 | | - if (IpAssignDate.before(startDate)) { |
84 | | - IpAssignDate = startDate; |
85 | | - } |
86 | | - |
87 | | - 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) |
88 | | - |
89 | | - updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration); |
90 | | - } |
91 | | - |
92 | | - for (String ipIdKey : usageMap.keySet()) { |
93 | | - Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey); |
94 | | - long useTime = ipTimeInfo.second().longValue(); |
95 | | - |
96 | | - // Only create a usage record if we have a runningTime of bigger than zero. |
97 | | - if (useTime > 0L) { |
98 | | - IpInfo info = IPMap.get(ipIdKey); |
99 | | - createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat(), info.isSystem); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - return true; |
104 | | - } |
105 | | - |
106 | | - private static void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) { |
107 | | - Pair<Long, Long> ipUsageInfo = usageDataMap.get(key); |
108 | | - if (ipUsageInfo == null) { |
109 | | - ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration)); |
110 | | - } else { |
111 | | - Long runningTime = ipUsageInfo.second(); |
112 | | - runningTime = new Long(runningTime.longValue() + duration); |
113 | | - ipUsageInfo = new Pair<Long, Long>(ipUsageInfo.first(), runningTime); |
114 | | - } |
115 | | - usageDataMap.put(key, ipUsageInfo); |
116 | | - } |
117 | | - |
118 | | - private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) { |
119 | | - if (s_logger.isDebugEnabled()) { |
120 | | - s_logger.debug("Total usage time " + runningTime + "ms"); |
121 | | - } |
122 | | - |
123 | | - float usage = runningTime / 1000f / 60f / 60f; |
124 | | - |
125 | | - DecimalFormat dFormat = new DecimalFormat("#.######"); |
126 | | - String usageDisplay = dFormat.format(usage); |
127 | | - |
128 | | - if (s_logger.isDebugEnabled()) { |
129 | | - s_logger.debug("Creating IP usage record with id: " + IpId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId()); |
130 | | - } |
131 | | - |
132 | | - String usageDesc = "IPAddress: "+IPAddress; |
133 | | - |
134 | | - // Create the usage record |
135 | | - |
136 | | - UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), IpId, |
137 | | - (isSystem?1:0), (isSourceNat?"SourceNat":""), startDate, endDate); |
138 | | - m_usageDao.persist(usageRecord); |
139 | | - } |
140 | | - |
141 | | - private static class IpInfo { |
142 | | - private long zoneId; |
143 | | - private long IpId; |
144 | | - private String IPAddress; |
145 | | - private boolean isSourceNat; |
146 | | - private boolean isSystem; |
147 | | - |
148 | | - public IpInfo(long zoneId,long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) { |
149 | | - this.zoneId = zoneId; |
150 | | - this.IpId = IpId; |
151 | | - this.IPAddress = IPAddress; |
152 | | - this.isSourceNat = isSourceNat; |
153 | | - this.isSystem = isSystem; |
154 | | - } |
155 | | - |
156 | | - public long getZoneId() { |
157 | | - return zoneId; |
158 | | - } |
159 | | - |
160 | | - public long getIpId() { |
161 | | - return IpId; |
162 | | - } |
163 | | - |
164 | | - public String getIPAddress() { |
165 | | - return IPAddress; |
166 | | - } |
167 | | - |
168 | | - public boolean isSourceNat() { |
169 | | - return isSourceNat; |
170 | | - } |
171 | | - } |
172 | | -} |
| 13 | +package com.cloud.usage.parser; |
| 14 | + |
| 15 | +import java.text.DecimalFormat; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.apache.log4j.Logger; |
| 22 | + |
| 23 | +import com.cloud.usage.UsageIPAddressVO; |
| 24 | +import com.cloud.usage.UsageServer; |
| 25 | +import com.cloud.usage.UsageTypes; |
| 26 | +import com.cloud.usage.UsageVO; |
| 27 | +import com.cloud.usage.dao.UsageDao; |
| 28 | +import com.cloud.usage.dao.UsageIPAddressDao; |
| 29 | +import com.cloud.user.AccountVO; |
| 30 | +import com.cloud.utils.Pair; |
| 31 | +import com.cloud.utils.component.ComponentLocator; |
| 32 | + |
| 33 | +public class IPAddressUsageParser { |
| 34 | + public static final Logger s_logger = Logger.getLogger(IPAddressUsageParser.class.getName()); |
| 35 | + |
| 36 | + private static ComponentLocator _locator = ComponentLocator.getLocator(UsageServer.Name, "usage-components.xml", "log4j-cloud_usage"); |
| 37 | + private static UsageDao m_usageDao = _locator.getDao(UsageDao.class); |
| 38 | + private static UsageIPAddressDao m_usageIPAddressDao = _locator.getDao(UsageIPAddressDao.class); |
| 39 | + |
| 40 | + |
| 41 | + public static boolean parse(AccountVO account, Date startDate, Date endDate) { |
| 42 | + if (s_logger.isDebugEnabled()) { |
| 43 | + s_logger.debug("Parsing IP Address usage for account: " + account.getId()); |
| 44 | + } |
| 45 | + if ((endDate == null) || endDate.after(new Date())) { |
| 46 | + endDate = new Date(); |
| 47 | + } |
| 48 | + |
| 49 | + // - query usage_ip_address table with the following criteria: |
| 50 | + // - look for an entry for accountId with start date in the given range |
| 51 | + // - look for an entry for accountId with end date in the given range |
| 52 | + // - look for an entry for accountId with end date null (currently running vm or owned IP) |
| 53 | + // - look for an entry for accountId with start date before given range *and* end date after given range |
| 54 | + List<UsageIPAddressVO> usageIPAddress = m_usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate); |
| 55 | + |
| 56 | + if(usageIPAddress.isEmpty()){ |
| 57 | + s_logger.debug("No IP Address usage for this period"); |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + // This map has both the running time *and* the usage amount. |
| 62 | + Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>(); |
| 63 | + |
| 64 | + Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>(); |
| 65 | + |
| 66 | + // loop through all the usage IPs, create a usage record for each |
| 67 | + for (UsageIPAddressVO usageIp : usageIPAddress) { |
| 68 | + long IpId = usageIp.getId(); |
| 69 | + |
| 70 | + String key = ""+IpId; |
| 71 | + |
| 72 | + // store the info in the IP map |
| 73 | + IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat(), usageIp.isSystem())); |
| 74 | + |
| 75 | + Date IpAssignDate = usageIp.getAssigned(); |
| 76 | + Date IpReleaseDeleteDate = usageIp.getReleased(); |
| 77 | + |
| 78 | + if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) { |
| 79 | + IpReleaseDeleteDate = endDate; |
| 80 | + } |
| 81 | + |
| 82 | + // clip the start date to the beginning of our aggregation range if the vm has been running for a while |
| 83 | + if (IpAssignDate.before(startDate)) { |
| 84 | + IpAssignDate = startDate; |
| 85 | + } |
| 86 | + |
| 87 | + 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) |
| 88 | + |
| 89 | + updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration); |
| 90 | + } |
| 91 | + |
| 92 | + for (String ipIdKey : usageMap.keySet()) { |
| 93 | + Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey); |
| 94 | + long useTime = ipTimeInfo.second().longValue(); |
| 95 | + |
| 96 | + // Only create a usage record if we have a runningTime of bigger than zero. |
| 97 | + if (useTime > 0L) { |
| 98 | + IpInfo info = IPMap.get(ipIdKey); |
| 99 | + createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat(), info.isSystem); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + private static void updateIpUsageData(Map<String, Pair<Long, Long>> usageDataMap, String key, long ipId, long duration) { |
| 107 | + Pair<Long, Long> ipUsageInfo = usageDataMap.get(key); |
| 108 | + if (ipUsageInfo == null) { |
| 109 | + ipUsageInfo = new Pair<Long, Long>(new Long(ipId), new Long(duration)); |
| 110 | + } else { |
| 111 | + Long runningTime = ipUsageInfo.second(); |
| 112 | + runningTime = new Long(runningTime.longValue() + duration); |
| 113 | + ipUsageInfo = new Pair<Long, Long>(ipUsageInfo.first(), runningTime); |
| 114 | + } |
| 115 | + usageDataMap.put(key, ipUsageInfo); |
| 116 | + } |
| 117 | + |
| 118 | + private static void createUsageRecord(long zoneId, long runningTime, Date startDate, Date endDate, AccountVO account, long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) { |
| 119 | + if (s_logger.isDebugEnabled()) { |
| 120 | + s_logger.debug("Total usage time " + runningTime + "ms"); |
| 121 | + } |
| 122 | + |
| 123 | + float usage = runningTime / 1000f / 60f / 60f; |
| 124 | + |
| 125 | + DecimalFormat dFormat = new DecimalFormat("#.######"); |
| 126 | + String usageDisplay = dFormat.format(usage); |
| 127 | + |
| 128 | + if (s_logger.isDebugEnabled()) { |
| 129 | + s_logger.debug("Creating IP usage record with id: " + IpId + ", usage: " + usageDisplay + ", startDate: " + startDate + ", endDate: " + endDate + ", for account: " + account.getId()); |
| 130 | + } |
| 131 | + |
| 132 | + String usageDesc = "IPAddress: "+IPAddress; |
| 133 | + |
| 134 | + // Create the usage record |
| 135 | + |
| 136 | + UsageVO usageRecord = new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", UsageTypes.IP_ADDRESS, new Double(usage), IpId, |
| 137 | + (isSystem?1:0), (isSourceNat?"SourceNat":""), startDate, endDate); |
| 138 | + m_usageDao.persist(usageRecord); |
| 139 | + } |
| 140 | + |
| 141 | + private static class IpInfo { |
| 142 | + private long zoneId; |
| 143 | + private long IpId; |
| 144 | + private String IPAddress; |
| 145 | + private boolean isSourceNat; |
| 146 | + private boolean isSystem; |
| 147 | + |
| 148 | + public IpInfo(long zoneId,long IpId, String IPAddress, boolean isSourceNat, boolean isSystem) { |
| 149 | + this.zoneId = zoneId; |
| 150 | + this.IpId = IpId; |
| 151 | + this.IPAddress = IPAddress; |
| 152 | + this.isSourceNat = isSourceNat; |
| 153 | + this.isSystem = isSystem; |
| 154 | + } |
| 155 | + |
| 156 | + public long getZoneId() { |
| 157 | + return zoneId; |
| 158 | + } |
| 159 | + |
| 160 | + public long getIpId() { |
| 161 | + return IpId; |
| 162 | + } |
| 163 | + |
| 164 | + public String getIPAddress() { |
| 165 | + return IPAddress; |
| 166 | + } |
| 167 | + |
| 168 | + public boolean isSourceNat() { |
| 169 | + return isSourceNat; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments