Modify Get data according to Subscribed or Unsubscribed statuses

#11204
Nipuni Kavindya 6 months ago
parent b3c2c8a524
commit 0742d467db

@ -1710,12 +1710,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
groupDetailMap.put("DeviceCount", groupDetailWithDevices.get("DeviceCount"));
// Fetch device subscriptions for each device ID in the group
List<Map<String, Object>> deviceDetails = new ArrayList<>();
List<Map<String, Object>> pendingDevices = new ArrayList<>();
List<Map<String, Object>> installedDevices = new ArrayList<>();
List<Map<String, Object>> errorDevices = new ArrayList<>();
List<Map<String, Object>> newDevices = new ArrayList<>();
List<Integer> deviceIds = (List<Integer>) groupDetailWithDevices.get("DeviceIds");
Map<String, Integer> statusCounts = new HashMap<>();
statusCounts.put("PENDING", 0);
statusCounts.put("IN_PROGRESS", 0);
statusCounts.put("COMPLETED", 0);
statusCounts.put("ERROR", 0);
statusCounts.put("NEW", 0);
@ -1728,15 +1730,34 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (subscription.getDeviceId() == deviceId) {
Map<String, Object> deviceDetailMap = new HashMap<>();
deviceDetailMap.put("DeviceID", subscription.getDeviceId());
deviceDetailMap.put("Status", subscription.getStatus());
deviceDetails.add(deviceDetailMap);
// deviceDetailMap.put("Status", subscription.getStatus()); // if need to get the real status
String status = subscription.getStatus();
statusCounts.put(status, statusCounts.get(status) + 1);
switch (status) {
case "COMPLETED":
installedDevices.add(deviceDetailMap);
statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
break;
case "ERROR":
case "INVALID":
case "UNAUTHORIZED":
errorDevices.add(deviceDetailMap);
statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
break;
case "IN_PROGRESS":
case "PENDING":
case "REPEATED":
pendingDevices.add(deviceDetailMap);
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
break;
}
isNewDevice = false;
}
}
if (isNewDevice) {
Map<String, Object> newDeviceDetailMap = new HashMap<>();
newDeviceDetailMap.put("DeviceID", deviceId);
newDevices.add(newDeviceDetailMap);
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
}
}
@ -1748,7 +1769,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusPercentages.put(entry.getKey(), percentage);
}
groupDetailMap.put("Devices", deviceDetails);
Map<String, Object> devicesMap = new HashMap<>();
devicesMap.put("PendingDevices", pendingDevices);
devicesMap.put("InstalledDevices", installedDevices);
devicesMap.put("ErrorDevices", errorDevices);
devicesMap.put("NewDevices", newDevices);
groupDetailMap.put("Devices", devicesMap);
groupDetailMap.put("StatusPercentages", statusPercentages);
groupDetailsWithDevices.add(groupDetailMap);
@ -1781,11 +1808,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<Map<String, Object>> userSubscriptions = subscriptionDAO.getUserSubscriptionsByUUID(uuid, unsubscribe, tenantId);
List<Map<String, Object>> userSubscriptionsWithDevices = new ArrayList<>();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
.getDeviceManagementProviderService();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
for (Map<String, Object> userSubscription : userSubscriptions) {
String userName = (String) userSubscription.get("UserName");
// Retrieve owner details and device IDs for the user using the service layer
@ -1803,12 +1828,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
userSubscriptionMap.put("DeviceCount", ownerDetailsWithDevices.get("DeviceCount"));
// Fetch device subscriptions for each device ID associated with the user
List<Map<String, Object>> deviceDetails = new ArrayList<>();
List<Map<String, Object>> pendingDevices = new ArrayList<>();
List<Map<String, Object>> installedDevices = new ArrayList<>();
List<Map<String, Object>> errorDevices = new ArrayList<>();
List<Map<String, Object>> newDevices = new ArrayList<>();
List<Integer> deviceIds = (List<Integer>) ownerDetailsWithDevices.get("DeviceIds");
Map<String, Integer> statusCounts = new HashMap<>();
statusCounts.put("PENDING", 0);
statusCounts.put("IN_PROGRESS", 0);
statusCounts.put("COMPLETED", 0);
statusCounts.put("ERROR", 0);
statusCounts.put("NEW", 0);
@ -1821,15 +1848,34 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (subscription.getDeviceId() == deviceId) {
Map<String, Object> deviceDetailMap = new HashMap<>();
deviceDetailMap.put("DeviceID", subscription.getDeviceId());
deviceDetailMap.put("Status", subscription.getStatus());
deviceDetails.add(deviceDetailMap);
// deviceDetailMap.put("Status", subscription.getStatus());
String status = subscription.getStatus();
statusCounts.put(status, statusCounts.get(status) + 1);
switch (status) {
case "COMPLETED":
installedDevices.add(deviceDetailMap);
statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
break;
case "ERROR":
case "INVALID":
case "UNAUTHORIZED":
errorDevices.add(deviceDetailMap);
statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
break;
case "IN_PROGRESS":
case "PENDING":
case "REPEATED":
pendingDevices.add(deviceDetailMap);
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
break;
}
isNewDevice = false;
}
}
if (isNewDevice) {
Map<String, Object> newDeviceDetailMap = new HashMap<>();
newDeviceDetailMap.put("DeviceID", deviceId);
newDevices.add(newDeviceDetailMap);
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
}
}
@ -1841,7 +1887,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusPercentages.put(entry.getKey(), percentage);
}
userSubscriptionMap.put("Devices", deviceDetails);
Map<String, Object> devicesMap = new HashMap<>();
devicesMap.put("PendingDevices", pendingDevices);
devicesMap.put("InstalledDevices", installedDevices);
devicesMap.put("ErrorDevices", errorDevices);
devicesMap.put("NewDevices", newDevices);
userSubscriptionMap.put("Devices", devicesMap);
userSubscriptionMap.put("StatusPercentages", statusPercentages);
userSubscriptionsWithDevices.add(userSubscriptionMap);
@ -1876,7 +1928,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
for (Map<String, Object> roleSubscription : roleSubscriptions) {
String roleName = (String) roleSubscription.get("RoleName");
Map<String, Object> roleSubscriptionMap = new HashMap<>();
roleSubscriptionMap.put("RoleName", roleName);
@ -1887,10 +1938,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
roleSubscriptionMap.put("UnsubscribedTimestamp", roleSubscription.get("UnsubscribedTimestamp"));
roleSubscriptionMap.put("AppReleaseId", roleSubscription.get("AppReleaseId"));
List<Map<String, Object>> deviceDetails = new ArrayList<>();
List<Map<String, Object>> pendingDevices = new ArrayList<>();
List<Map<String, Object>> installedDevices = new ArrayList<>();
List<Map<String, Object>> errorDevices = new ArrayList<>();
List<Map<String, Object>> newDevices = new ArrayList<>();
Map<String, Integer> statusCounts = new HashMap<>();
statusCounts.put("PENDING", 0);
statusCounts.put("IN_PROGRESS", 0);
statusCounts.put("COMPLETED", 0);
statusCounts.put("ERROR", 0);
statusCounts.put("NEW", 0);
@ -1922,29 +1976,55 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (deviceSubscription.getDeviceId() == deviceId) {
Map<String, Object> deviceDetailMap = new HashMap<>();
deviceDetailMap.put("DeviceID", deviceSubscription.getDeviceId());
deviceDetailMap.put("Status", deviceSubscription.getStatus());
deviceDetails.add(deviceDetailMap);
// deviceDetailMap.put("Status", deviceSubscription.getStatus());
String status = deviceSubscription.getStatus();
statusCounts.put(status, statusCounts.get(status) + 1);
switch (status) {
case "COMPLETED":
installedDevices.add(deviceDetailMap);
statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
break;
case "ERROR":
case "INVALID":
case "UNAUTHORIZED":
errorDevices.add(deviceDetailMap);
statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
break;
case "IN_PROGRESS":
case "PENDING":
case "REPEATED":
pendingDevices.add(deviceDetailMap);
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
break;
}
isNewDevice = false;
}
}
if (isNewDevice) {
Map<String, Object> newDeviceDetailMap = new HashMap<>();
newDeviceDetailMap.put("DeviceID", deviceId);
newDevices.add(newDeviceDetailMap);
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
}
}
}
int totalDevices = deviceDetails.size();
int totalDevices = pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size();
Map<String, Double> statusPercentages = new HashMap<>();
for (Map.Entry<String, Integer> entry : statusCounts.entrySet()) {
double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100;
statusPercentages.put(entry.getKey(), percentage);
}
roleSubscriptionMap.put("Devices", deviceDetails);
Map<String, Object> devicesMap = new HashMap<>();
devicesMap.put("PendingDevices", pendingDevices);
devicesMap.put("InstalledDevices", installedDevices);
devicesMap.put("ErrorDevices", errorDevices);
devicesMap.put("NewDevices", newDevices);
roleSubscriptionMap.put("Devices", devicesMap);
roleSubscriptionMap.put("StatusPercentages", statusPercentages);
roleSubscriptionMap.put("DeviceCount", totalDevices);
roleSubscriptionsWithDevices.add(roleSubscriptionMap);
}
@ -1967,6 +2047,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
}
// Get user list for each role
public List<String> getUsersForRole(String roleName) throws UserStoreException {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
int tenantId = ctx.getTenantId();
@ -1991,6 +2072,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
Map<String, Object> operationDetails = deviceManagementProviderService.getOperationDetailsById(operationId);
if (operationDetails != null) {
deviceSubscription.put("OperationCode", operationDetails.get("OperationCode"));
deviceSubscription.put("OperationDetails", operationDetails.get("OperationDetails"));
deviceSubscription.put("OperationProperties", operationDetails.get("OperationProperties"));
}
}
}

Loading…
Cancel
Save