From a2fad630c741d5fa1267dd4c0b05e7e14c6a429f Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Wed, 4 Mar 2020 13:10:45 +0530 Subject: [PATCH 1/4] Add hourly and daily usage to application bean --- .../mgt/common/app/mgt/Application.java | 20 +++++++++++++++++++ .../mgt/core/DeviceManagementConstants.java | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java index ecbafd974c2..38d7cf5d7b6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java @@ -53,6 +53,10 @@ public class Application implements Serializable { private int memoryUsage; @ApiModelProperty(name = "isActive", value = "Is the application actively running", required = true) private boolean isActive; + @ApiModelProperty(name = "hourlyUsage", value = "App hourly usage") + private long hourlyUsage; + @ApiModelProperty(name = "dailyUsage", value = "App daily usage") + private long dailyUsage; public String getType() { @@ -179,4 +183,20 @@ public class Application implements Serializable { public void setActive(boolean active) { isActive = active; } + + public long getHourlyUsage() { + return hourlyUsage; + } + + public void setHourlyUsage(long hourlyUsage) { + this.hourlyUsage = hourlyUsage; + } + + public long getDailyUsage() { + return dailyUsage; + } + + public void setDailyUsage(long dailyUsage) { + this.dailyUsage = dailyUsage; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index def109fd041..0a616820606 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -144,8 +144,9 @@ public final class DeviceManagementConstants { private Report() { throw new AssertionError(); } + public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host"; public static final String REPORTING_CONTEXT = "/event"; public static final String DEVICE_INFO_ENDPOINT = REPORTING_CONTEXT + "/device-info"; - public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host"; + public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; } } From 2e66e78d7dc76ee98add8aea8fc890f9d90051fb Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Wed, 4 Mar 2020 13:11:25 +0530 Subject: [PATCH 2/4] Add a method to retrieve the reporting host --- .../wso2/carbon/device/mgt/core/util/HttpReportingUtil.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/HttpReportingUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/HttpReportingUtil.java index 385f5866b44..2d034d74f9a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/HttpReportingUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/HttpReportingUtil.java @@ -25,11 +25,16 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HTTP; import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; +import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import java.io.IOException; public class HttpReportingUtil { + public static String getReportingHost() { + return System.getProperty(DeviceManagementConstants.Report.REPORTING_EVENT_HOST); + } + public static int invokeApi(String payload, String endpoint) throws EventPublishingException { try (CloseableHttpClient client = HttpClients.createDefault()) { HttpPost apiEndpoint = new HttpPost(endpoint); From 7599d97e68afa9a5f7042d3793b2ef9d7c28c672 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Wed, 4 Mar 2020 13:12:54 +0530 Subject: [PATCH 3/4] Call reporting app usage endpoint --- .../mgt/ApplicationManagerProviderServiceImpl.java | 13 +++++++++++++ .../mgt/impl/DeviceInformationManagerImpl.java | 1 + 2 files changed, 14 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index e9cc0583ff5..c22a8ff44d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; @@ -39,6 +40,7 @@ import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; import java.sql.SQLException; import java.util.ArrayList; @@ -277,6 +279,17 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem device.getEnrolmentInfo().getId(), tenantId); } DeviceManagementDAOFactory.commitTransaction(); + + String reportingHost = HttpReportingUtil.getReportingHost(); + if (!StringUtils.isBlank(reportingHost)) { + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setTenantId(tenantId); + deviceDetailsWrapper.setDevice(device); + deviceDetailsWrapper.setApplications(newApplications); + HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), + reportingHost + DeviceManagementConstants.Report.APP_USAGE_ENDPOINT); + } + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred saving application list of the device " + device.getDeviceIdentifier(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index f5b7d141dcd..aa11b9f68fe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; From 8b5e336986304ee137ce412f456816e3ca333cd3 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Wed, 4 Mar 2020 14:10:06 +0530 Subject: [PATCH 4/4] Use the method to retrieve the reporting host --- .../details/mgt/impl/DeviceInformationManagerImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index aa11b9f68fe..78b2119efb6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -176,9 +176,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } private void publishEvents(Device device, DeviceInfo deviceInfo) { - String reportingHost = System.getProperty(DeviceManagementConstants.Report - .REPORTING_EVENT_HOST); - if (reportingHost != null && !reportingHost.isEmpty() && isPublishingEnabledForTenant()) { + String reportingHost = HttpReportingUtil.getReportingHost(); + if (!StringUtils.isBlank(reportingHost) && isPublishingEnabledForTenant()) { try { DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); deviceDetailsWrapper.setDevice(device);