Add fix for group subscription table

Co-authored-by: Oshani Silva <oshani@entgra.io>
Co-committed-by: Oshani Silva <oshani@entgra.io>
rm-11445
Oshani Silva 4 months ago committed by Lasantha Dharmakeerthi
parent e062bf79dc
commit 8b6eee0a97

@ -23,10 +23,15 @@ import java.util.List;
public class CategorizedSubscriptionResult { public class CategorizedSubscriptionResult {
private List<DeviceSubscriptionData> installedDevices; private List<DeviceSubscriptionData> installedDevices;
private int installedDevicesCount;
private List<DeviceSubscriptionData> pendingDevices; private List<DeviceSubscriptionData> pendingDevices;
private int pendingDevicesCount;
private List<DeviceSubscriptionData> errorDevices; private List<DeviceSubscriptionData> errorDevices;
private int errorDevicesCount;
private List<DeviceSubscriptionData> newDevices; private List<DeviceSubscriptionData> newDevices;
private int newDevicesCount;
private List<DeviceSubscriptionData> subscribedDevices; private List<DeviceSubscriptionData> subscribedDevices;
private int subscribedDevicesCount;
public CategorizedSubscriptionResult(List<DeviceSubscriptionData> installedDevices, public CategorizedSubscriptionResult(List<DeviceSubscriptionData> installedDevices,
List<DeviceSubscriptionData> pendingDevices, List<DeviceSubscriptionData> pendingDevices,
@ -61,6 +66,48 @@ public class CategorizedSubscriptionResult {
this.subscribedDevices = subscribedDevices; this.subscribedDevices = subscribedDevices;
} }
public CategorizedSubscriptionResult(List<DeviceSubscriptionData> installedDevices,
List<DeviceSubscriptionData> pendingDevices,
List<DeviceSubscriptionData> errorDevices,
List<DeviceSubscriptionData> newDevices,
int installedDevicesCount,
int pendingDevicesCount,
int errorDevicesCount,
int newDevicesCount
) {
this.installedDevices = installedDevices;
this.pendingDevices = pendingDevices;
this.errorDevices = errorDevices;
this.newDevices = newDevices;
this.subscribedDevices = null;
this.installedDevicesCount = installedDevicesCount;
this.pendingDevicesCount = pendingDevicesCount;
this.errorDevicesCount = errorDevicesCount;
this.newDevicesCount = newDevicesCount;
this.subscribedDevicesCount = 0;
}
public CategorizedSubscriptionResult(List<DeviceSubscriptionData> installedDevices,
List<DeviceSubscriptionData> pendingDevices,
List<DeviceSubscriptionData> errorDevices,
List<DeviceSubscriptionData> newDevices,
List<DeviceSubscriptionData> subscribedDevices, int installedDevicesCount,
int pendingDevicesCount,
int errorDevicesCount,
int newDevicesCount,
int subscribedDevicesCount) {
this.installedDevices = installedDevices;
this.pendingDevices = pendingDevices;
this.errorDevices = errorDevices;
this.newDevices = newDevices;
this.subscribedDevices = subscribedDevices;
this.installedDevicesCount = installedDevicesCount;
this.pendingDevicesCount = pendingDevicesCount;
this.errorDevicesCount = errorDevicesCount;
this.newDevicesCount = newDevicesCount;
this.subscribedDevicesCount = subscribedDevicesCount;
}
public CategorizedSubscriptionResult(List<DeviceSubscriptionData> devices, String tabActionStatus) { public CategorizedSubscriptionResult(List<DeviceSubscriptionData> devices, String tabActionStatus) {
switch (tabActionStatus) { switch (tabActionStatus) {
case "COMPLETED": case "COMPLETED":
@ -127,4 +174,44 @@ public class CategorizedSubscriptionResult {
public void setSubscribedDevices(List<DeviceSubscriptionData> subscribedDevices) { public void setSubscribedDevices(List<DeviceSubscriptionData> subscribedDevices) {
this.subscribedDevices = subscribedDevices; this.subscribedDevices = subscribedDevices;
} }
public int getInstalledDevicesCount() {
return installedDevicesCount;
}
public void setInstalledDevicesCount(int installedDevicesCount) {
this.installedDevicesCount = installedDevicesCount;
}
public int getPendingDevicesCount() {
return pendingDevicesCount;
}
public void setPendingDevicesCount(int pendingDevicesCount) {
this.pendingDevicesCount = pendingDevicesCount;
}
public int getErrorDevicesCount() {
return errorDevicesCount;
}
public void setErrorDevicesCount(int errorDevicesCount) {
this.errorDevicesCount = errorDevicesCount;
}
public int getNewDevicesCount() {
return newDevicesCount;
}
public void setNewDevicesCount(int newDevicesCount) {
this.newDevicesCount = newDevicesCount;
}
public int getSubscribedDevicesCount() {
return subscribedDevicesCount;
}
public void setSubscribedDevicesCount(int subscribedDevicesCount) {
this.subscribedDevicesCount = subscribedDevicesCount;
}
} }

@ -517,4 +517,16 @@ public interface SubscriptionDAO {
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/ */
int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException;
/**
* This method is used to get the counts of devices related to a UUID.
*
* @param appReleaseId the UUID of the application release.
* @param tenantId id of the current tenant.
* @param actionStatus categorized status.
* @param actionTriggeredFrom type of the action.
* @return {@link int} which contains the count of the subscription type
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
int countSubscriptionsByStatus(int appReleaseId, int tenantId, String actionStatus, String actionTriggeredFrom) throws ApplicationManagementDAOException;
} }

@ -1655,14 +1655,22 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
"GS.UNSUBSCRIBED_BY, GS.UNSUBSCRIBED_TIMESTAMP, GS.AP_APP_RELEASE_ID " + "GS.UNSUBSCRIBED_BY, GS.UNSUBSCRIBED_TIMESTAMP, GS.AP_APP_RELEASE_ID " +
"FROM AP_GROUP_SUBSCRIPTION GS " + "FROM AP_GROUP_SUBSCRIPTION GS " +
"WHERE GS.AP_APP_RELEASE_ID = ? AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + "WHERE GS.AP_APP_RELEASE_ID = ? AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " +
"ORDER BY " + subscriptionStatusTime + " DESC " + "ORDER BY " + subscriptionStatusTime + " DESC ";
"LIMIT ? OFFSET ?";
// Append limit and offset only if limit is not -1
if (limit != -1) {
sql = sql + " LIMIT ? OFFSET ?";
}
try (PreparedStatement ps = conn.prepareStatement(sql)) { try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId); ps.setInt(1, appReleaseId);
ps.setBoolean(2, unsubscribe); ps.setBoolean(2, unsubscribe);
ps.setInt(3, tenantId); ps.setInt(3, tenantId);
ps.setInt(4, limit);
ps.setInt(5, offset); // Set limit and offset parameters only if limit is not -1
if (limit != -1) {
ps.setInt(4, limit);
ps.setInt(5, offset);
}
try (ResultSet rs = ps.executeQuery()) { try (ResultSet rs = ps.executeQuery()) {
GroupSubscriptionDTO groupDetail; GroupSubscriptionDTO groupDetail;
@ -2487,4 +2495,34 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
throw new ApplicationManagementDAOException(msg, e); throw new ApplicationManagementDAOException(msg, e);
} }
} }
public int countSubscriptionsByStatus(int appReleaseId, int tenantId, String actionStatus, String actionTriggeredFrom) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to count device subscriptions by status and actionTriggeredFrom for the given AppReleaseID.");
}
try {
Connection conn = this.getDBConnection();
String sql = "SELECT COUNT(*) FROM AP_DEVICE_SUBSCRIPTION WHERE AP_APP_RELEASE_ID = ? AND TENANT_ID = ? AND STATUS = ? AND ACTION_TRIGGERED_FROM = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId);
ps.setInt(2, tenantId);
ps.setString(3, actionStatus);
ps.setString(4, actionTriggeredFrom);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
return rs.getInt(1);
}
}
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to count device subscriptions by status and action trigger.";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "SQL Error occurred while counting device subscriptions by status and action trigger.";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
return 0;
}
} }

@ -1725,7 +1725,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<SubscriptionsDTO> groupDetailsWithDevices = new ArrayList<>(); List<SubscriptionsDTO> groupDetailsWithDevices = new ArrayList<>();
List<GroupSubscriptionDTO> groupDetails = List<GroupSubscriptionDTO> groupDetails =
subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, -1);
if (groupDetails == null) { if (groupDetails == null) {
throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId); throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId);
} }
@ -1744,7 +1744,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
GroupDetailsDTO groupDetailWithDevices = GroupDetailsDTO groupDetailWithDevices =
groupManagementProviderService.getGroupDetailsWithDevices( groupManagementProviderService.getGroupDetailsWithDevices(
groupName, applicationDTO.getDeviceTypeId(), request.getOwner(), groupName, applicationDTO.getDeviceTypeId(), request.getOwner(),
request.getDeviceName(), request.getDeviceStatus(), offset, limit); request.getDeviceName(), request.getDeviceStatus(), offset, -1);
SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO();
groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); groupDetailDTO.setId(groupDetailWithDevices.getGroupId());
@ -1878,33 +1878,73 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
List<DeviceSubscriptionData> requestedDevices = new ArrayList<>(); List<DeviceSubscriptionData> requestedDevices = new ArrayList<>();
int installedCount;
int pendingCount;
int errorCount;
int newCount;
int subscribedCount;
int totalDeviceCount =
groupManagementProviderService.getDeviceCount(groupDetailWithDevices.getGroupId());
if (StringUtils.isNotBlank(request.getTabActionStatus())) { if (StringUtils.isNotBlank(request.getTabActionStatus())) {
switch (request.getTabActionStatus()) { switch (request.getTabActionStatus()) {
case "COMPLETED": case "COMPLETED":
requestedDevices = installedDevices; requestedDevices = installedDevices;
installedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType());
break; break;
case "PENDING": case "PENDING":
requestedDevices = pendingDevices; requestedDevices = pendingDevices;
pendingCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType());
break; break;
case "ERROR": case "ERROR":
requestedDevices = errorDevices; requestedDevices = errorDevices;
errorCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType());
break; break;
case "NEW": case "NEW":
requestedDevices = newDevices; requestedDevices = newDevices;
break; break;
case "SUBSCRIBED": case "SUBSCRIBED":
requestedDevices = subscribedDevices; requestedDevices = subscribedDevices;
subscribedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType());
break; break;
} }
groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus()));
} else { } else {
CategorizedSubscriptionResult categorizedSubscriptionResult; CategorizedSubscriptionResult categorizedSubscriptionResult;
installedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "COMPLETED", request.getActionType());
pendingCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "PENDING", request.getActionType());
errorCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "ERROR", request.getActionType());
subscribedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "SUBSCRIBED", request.getActionType());
newCount = totalDeviceCount - (installedCount + pendingCount + errorCount + subscribedCount);
List<DeviceSubscriptionData> paginatedInstalledDevices = installedDevices.stream()
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
List<DeviceSubscriptionData> paginatedPendingDevices = pendingDevices.stream()
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
List<DeviceSubscriptionData> paginatedErrorDevices = errorDevices.stream()
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
List<DeviceSubscriptionData> paginatedNewDevices = newDevices.stream()
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
List<DeviceSubscriptionData> paginatedSubscribedDevices = subscribedDevices.stream()
.skip(offset)
.limit(limit)
.collect(Collectors.toList());
if (subscribedDevices.isEmpty()) { if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult = categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); new CategorizedSubscriptionResult(paginatedInstalledDevices, paginatedPendingDevices, paginatedErrorDevices, paginatedNewDevices, installedCount, pendingCount, errorCount, newCount);
} else { } else {
categorizedSubscriptionResult = categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); new CategorizedSubscriptionResult(paginatedInstalledDevices, paginatedPendingDevices, paginatedErrorDevices, paginatedNewDevices, paginatedSubscribedDevices, installedCount, pendingCount, errorCount, newCount, subscribedCount);
} }
groupDetailDTO.setDevices(categorizedSubscriptionResult); groupDetailDTO.setDevices(categorizedSubscriptionResult);
} }

@ -1495,7 +1495,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
sql.append(" AND e.STATUS = ?"); sql.append(" AND e.STATUS = ?");
} }
sql.append(" LIMIT ? OFFSET ?"); // Append limit and offset only if limit is not -1
if (limit != -1) {
sql.append(" LIMIT ? OFFSET ?");
}
Connection conn = null; Connection conn = null;
try { try {
@ -1519,8 +1522,11 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(index++, deviceStatus); stmt.setString(index++, deviceStatus);
} }
stmt.setInt(index++, limit); // Set limit and offset parameters only if limit is not -1
stmt.setInt(index++, offset); if (limit != -1) {
stmt.setInt(index++, limit);
stmt.setInt(index++, offset);
}
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {

Loading…
Cancel
Save