Skip to content

Commit 7a7e449

Browse files
usage: fix backup usage (apache#5259)
when creating usage record for backup usages in a period, we need to check if vm offering is changed. if vm offering is changed, create a new cloud usage record, otherwise update existing usage record. This PR fixes apache#4982
1 parent a5e7e08 commit 7a7e449

File tree

1 file changed

+17
-51
lines changed

1 file changed

+17
-51
lines changed

usage/src/main/java/com/cloud/usage/parser/BackupUsageParser.java

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
package com.cloud.usage.parser;
1919

20+
import java.text.DecimalFormat;
2021
import java.util.Date;
21-
import java.util.HashMap;
2222
import java.util.List;
23-
import java.util.Map;
2423

2524
import javax.annotation.PostConstruct;
2625
import javax.inject.Inject;
2726

28-
import org.apache.cloudstack.backup.Backup;
2927
import org.apache.cloudstack.usage.UsageTypes;
3028
import org.apache.log4j.Logger;
3129
import org.springframework.stereotype.Component;
@@ -68,65 +66,33 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
6866
return true;
6967
}
7068

71-
final Map<Long, BackupInfo> vmUsageMap = new HashMap<>();
7269
for (final UsageBackupVO usageBackup : usageBackups) {
7370
final Long vmId = usageBackup.getVmId();
7471
final Long zoneId = usageBackup.getZoneId();
7572
final Long offeringId = usageBackup.getBackupOfferingId();
76-
if (vmUsageMap.get(vmId) == null) {
77-
vmUsageMap.put(vmId, new BackupUsageParser.BackupInfo(new Backup.Metric(0L, 0L), zoneId, vmId, offeringId));
73+
Date createdDate = usageBackup.getCreated();
74+
Date removedDate = usageBackup.getRemoved();
75+
if (createdDate.before(startDate)) {
76+
createdDate = startDate;
7877
}
79-
final Backup.Metric metric = vmUsageMap.get(vmId).getMetric();
80-
metric.setBackupSize(metric.getBackupSize() + usageBackup.getSize());
81-
metric.setDataSize(metric.getDataSize() + usageBackup.getProtectedSize());
82-
}
78+
if (removedDate == null || removedDate.after(endDate)) {
79+
removedDate = endDate;
80+
}
81+
final long duration = (removedDate.getTime() - createdDate.getTime()) + 1;
82+
final float usage = duration / 1000f / 60f / 60f;
83+
DecimalFormat dFormat = new DecimalFormat("#.######");
84+
String usageDisplay = dFormat.format(usage);
8385

84-
for (final BackupInfo backupInfo : vmUsageMap.values()) {
85-
final Long vmId = backupInfo.getVmId();
86-
final Long zoneId = backupInfo.getZoneId();
87-
final Long offeringId = backupInfo.getOfferingId();
88-
final Double rawUsage = (double) backupInfo.getMetric().getBackupSize();
89-
final Double sizeGib = rawUsage / (1024.0 * 1024.0 * 1024.0);
90-
final String description = String.format("Backup usage VM ID: %d", vmId);
91-
final String usageDisplay = String.format("%.4f GiB", sizeGib);
86+
final Double rawUsage = (double) usageBackup.getSize();
87+
final String description = String.format("Backup usage VM ID: %d, backup offering: %d", vmId, offeringId);
9288

9389
final UsageVO usageRecord =
94-
new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay,
95-
UsageTypes.BACKUP, rawUsage, vmId, null, offeringId, null, vmId,
96-
backupInfo.getMetric().getBackupSize(), backupInfo.getMetric().getDataSize(), startDate, endDate);
90+
new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs",
91+
UsageTypes.BACKUP, new Double(usage), vmId, null, offeringId, null, vmId,
92+
usageBackup.getSize(), usageBackup.getProtectedSize(), startDate, endDate);
9793
s_usageDao.persist(usageRecord);
9894
}
9995

10096
return true;
10197
}
102-
103-
static class BackupInfo {
104-
Backup.Metric metric;
105-
Long zoneId;
106-
Long vmId;
107-
Long offeringId;
108-
109-
public BackupInfo(Backup.Metric metric, Long zoneId, Long vmId, Long offeringId) {
110-
this.metric = metric;
111-
this.zoneId = zoneId;
112-
this.vmId = vmId;
113-
this.offeringId = offeringId;
114-
}
115-
116-
public Backup.Metric getMetric() {
117-
return metric;
118-
}
119-
120-
public Long getZoneId() {
121-
return zoneId;
122-
}
123-
124-
public Long getVmId() {
125-
return vmId;
126-
}
127-
128-
public Long getOfferingId() {
129-
return offeringId;
130-
}
131-
}
13298
}

0 commit comments

Comments
 (0)