Skip to content

Commit f85d558

Browse files
bug 10774: Introduce percentage in list commands and cluster id in alert table
1 parent b336a8b commit f85d558

6 files changed

Lines changed: 69 additions & 14 deletions

File tree

‎core/src/com/cloud/alert/AlertVO.java‎

100644100755
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ public class AlertVO implements Alert{
4040
private long id;
4141

4242
@Column(name="type")
43-
private short type;
44-
43+
private short type;
44+
45+
@Column(name="cluster_id")
46+
private Long clusterId = null;
47+
4548
@Column(name="pod_id")
4649
private Long podId = null;
4750

@@ -92,7 +95,13 @@ public void setSubject(String subject) {
9295
this.subject = subject;
9396
}
9497

95-
@Override
98+
public Long getClusterId() {
99+
return clusterId;
100+
}
101+
public void setClusterId(Long clusterId) {
102+
this.clusterId = clusterId;
103+
}
104+
@Override
96105
public Long getPodId() {
97106
return podId;
98107
}

‎server/src/com/cloud/alert/AlertManagerImpl.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public void sendAlert(short alertType, long dataCenterId, Long podId, String sub
261261
// shouldn't we have a type/severity as part of the API so that severe errors get sent right away?
262262
try {
263263
if (_emailAlert != null) {
264-
_emailAlert.sendAlert(alertType, dataCenterId, podId, subject, body);
264+
_emailAlert.sendAlert(alertType, dataCenterId, podId, null, subject, body);
265265
}
266266
} catch (Exception ex) {
267267
s_logger.error("Problem sending email alert", ex);
@@ -503,6 +503,8 @@ private void generateEmailAlert(DataCenterVO dc, HostPodVO pod, ClusterVO cluste
503503
String usedStr;
504504
String pctStr = formatPercent(usedCapacity/totalCapacity);
505505
short alertType = -1;
506+
Long podId = pod == null ? null : pod.getId();
507+
Long clusterId = cluster == null ? null : cluster.getId();
506508

507509
switch (capacityType) {
508510

@@ -584,7 +586,7 @@ private void generateEmailAlert(DataCenterVO dc, HostPodVO pod, ClusterVO cluste
584586
}
585587

586588
try {
587-
_emailAlert.sendAlert(alertType, dc.getId(), null, msgSubject, msgContent);
589+
_emailAlert.sendAlert(alertType, dc.getId(), podId, clusterId, msgSubject, msgContent);
588590
} catch (Exception ex) {
589591
s_logger.error("Exception in CapacityChecker", ex);
590592
}
@@ -683,22 +685,23 @@ protected PasswordAuthentication getPasswordAuthentication() {
683685
}
684686

685687
// TODO: make sure this handles SSL transport (useAuth is true) and regular
686-
public void sendAlert(short alertType, long dataCenterId, Long podId, String subject, String content) throws MessagingException, UnsupportedEncodingException {
688+
public void sendAlert(short alertType, long dataCenterId, Long podId, Long clusterId, String subject, String content) throws MessagingException, UnsupportedEncodingException {
687689
AlertVO alert = null;
688690
if ((alertType != AlertManager.ALERT_TYPE_HOST) &&
689691
(alertType != AlertManager.ALERT_TYPE_USERVM) &&
690692
(alertType != AlertManager.ALERT_TYPE_DOMAIN_ROUTER) &&
691693
(alertType != AlertManager.ALERT_TYPE_CONSOLE_PROXY) &&
692694
(alertType != AlertManager.ALERT_TYPE_STORAGE_MISC) &&
693695
(alertType != AlertManager.ALERT_TYPE_MANAGMENT_NODE)) {
694-
alert = _alertDao.getLastAlert(alertType, dataCenterId, podId);
696+
alert = _alertDao.getLastAlert(alertType, dataCenterId, podId, clusterId);
695697
}
696698

697699
if (alert == null) {
698700
// set up a new alert
699701
AlertVO newAlert = new AlertVO();
700702
newAlert.setType(alertType);
701-
newAlert.setSubject(subject);
703+
newAlert.setSubject(subject);
704+
newAlert.setClusterId(clusterId);
702705
newAlert.setPodId(podId);
703706
newAlert.setDataCenterId(dataCenterId);
704707
newAlert.setSentCount(1); // initialize sent count to 1 since we are now sending an alert
@@ -737,7 +740,7 @@ public void sendAlert(short alertType, long dataCenterId, Long podId, String sub
737740

738741
public void clearAlert(short alertType, long dataCenterId, Long podId) {
739742
if (alertType != -1) {
740-
AlertVO alert = _alertDao.getLastAlert(alertType, dataCenterId, podId);
743+
AlertVO alert = _alertDao.getLastAlert(alertType, dataCenterId, podId, null);
741744
if (alert != null) {
742745
AlertVO updatedAlert = _alertDao.createForUpdate();
743746
updatedAlert.setResolved(new Date());

‎server/src/com/cloud/alert/dao/AlertDao.java‎

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222
import com.cloud.utils.db.GenericDao;
2323

2424
public interface AlertDao extends GenericDao<AlertVO, Long> {
25+
AlertVO getLastAlert(short type, long dataCenterId, Long podId, Long clusterId);
26+
// This is for backward compatibility
2527
AlertVO getLastAlert(short type, long dataCenterId, Long podId);
2628
}

‎server/src/com/cloud/alert/dao/AlertDaoImpl.java‎

100644100755
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,41 @@
3030
@Local(value = { AlertDao.class })
3131
public class AlertDaoImpl extends GenericDaoBase<AlertVO, Long> implements AlertDao {
3232
@Override
33-
public AlertVO getLastAlert(short type, long dataCenterId, Long podId) {
33+
public AlertVO getLastAlert(short type, long dataCenterId, Long podId, Long clusterId) {
3434
Filter searchFilter = new Filter(AlertVO.class, "createdDate", Boolean.FALSE, Long.valueOf(1), Long.valueOf(1));
3535
SearchCriteria<AlertVO> sc = createSearchCriteria();
3636

3737
sc.addAnd("type", SearchCriteria.Op.EQ, Short.valueOf(type));
3838
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, Long.valueOf(dataCenterId));
3939
if (podId != null) {
4040
sc.addAnd("podId", SearchCriteria.Op.EQ, podId);
41+
}
42+
if (clusterId != null) {
43+
sc.addAnd("clusterId", SearchCriteria.Op.EQ, clusterId);
4144
}
4245

4346
List<AlertVO> alerts = listBy(sc, searchFilter);
4447
if ((alerts != null) && !alerts.isEmpty()) {
4548
return alerts.get(0);
4649
}
4750
return null;
51+
}
52+
53+
@Override
54+
public AlertVO getLastAlert(short type, long dataCenterId, Long podId) {
55+
Filter searchFilter = new Filter(AlertVO.class, "createdDate", Boolean.FALSE, Long.valueOf(1), Long.valueOf(1));
56+
SearchCriteria<AlertVO> sc = createSearchCriteria();
57+
58+
sc.addAnd("type", SearchCriteria.Op.EQ, Short.valueOf(type));
59+
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, Long.valueOf(dataCenterId));
60+
if (podId != null) {
61+
sc.addAnd("podId", SearchCriteria.Op.EQ, podId);
62+
}
63+
64+
List<AlertVO> alerts = listBy(sc, searchFilter);
65+
if ((alerts != null) && !alerts.isEmpty()) {
66+
return alerts.get(0);
67+
}
68+
return null;
4869
}
4970
}

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
public class ApiResponseHelper implements ResponseGenerator {
182182

183183
public final Logger s_logger = Logger.getLogger(ApiResponseHelper.class);
184-
184+
private static final DecimalFormat s_percentFormat = new DecimalFormat("##.##");
185185

186186
@Override
187187
public UserResponse createUserResponse(User user) {
@@ -760,7 +760,12 @@ public PodResponse createPodResponse(Pod pod, Boolean showCapacities) {
760760
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() - c.get(0).getUsedCapacity());
761761
}else{
762762
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
763-
}
763+
}
764+
if (capacityResponse.getCapacityTotal() != 0) {
765+
capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
766+
} else {
767+
capacityResponse.setPercentUsed(s_percentFormat.format(0L));
768+
}
764769
capacityResponses.add(capacityResponse);
765770
}
766771
// Do it for stats as well.
@@ -810,6 +815,11 @@ public ZoneResponse createZoneResponse(DataCenter dataCenter, Boolean showCapaci
810815
}else{
811816
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
812817
}
818+
if (capacityResponse.getCapacityTotal() != 0) {
819+
capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
820+
} else {
821+
capacityResponse.setPercentUsed(s_percentFormat.format(0L));
822+
}
813823
capacityResponses.add(capacityResponse);
814824
}
815825
// Do it for stats as well.
@@ -840,7 +850,12 @@ private List<CapacityResponse> getStatsCapacityresponse(Long poolId, Long cluste
840850
CapacityResponse capacityResponse = new CapacityResponse();
841851
capacityResponse.setCapacityType(capacity.getCapacityType());
842852
capacityResponse.setCapacityUsed(capacity.getUsedCapacity());
843-
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
853+
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
854+
if (capacityResponse.getCapacityTotal() != 0) {
855+
capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
856+
} else {
857+
capacityResponse.setPercentUsed(s_percentFormat.format(0L));
858+
}
844859
capacityResponses.add(capacityResponse);
845860
}
846861

@@ -1033,7 +1048,11 @@ public ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapaci
10331048
}else{
10341049
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
10351050
}
1036-
1051+
if (capacityResponse.getCapacityTotal() != 0) {
1052+
capacityResponse.setPercentUsed(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
1053+
} else {
1054+
capacityResponse.setPercentUsed(s_percentFormat.format(0L));
1055+
}
10371056
capacityResponses.add(capacityResponse);
10381057
}
10391058
// Do it for stats as well.

‎setup/db/create-schema.sql‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ CREATE TABLE `cloud`.`op_host_capacity` (
11101110
CREATE TABLE `cloud`.`alert` (
11111111
`id` bigint unsigned NOT NULL auto_increment,
11121112
`type` int(1) unsigned NOT NULL,
1113+
`cluster_id` bigint unsigned,
11131114
`pod_id` bigint unsigned,
11141115
`data_center_id` bigint unsigned NOT NULL,
11151116
`subject` varchar(999) COMMENT 'according to SMTP spec, max subject length is 1000 including the CRLF character, so allow enough space to fit long pod/zone/host names',

0 commit comments

Comments
 (0)