Merge branch 'kernel-4.6.2' into 'kernel-4.6.x'

commented braking changes related to analytics

See merge request entgra/carbon-device-mgt!779
kernel-4.6.x
Charitha Goonetilleke 3 years ago
commit 1d494347a4

@ -363,11 +363,11 @@
<version>${carbon.identity.framework.version}</version> <version>${carbon.identity.framework.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <!--<dependency>-->
<groupId>org.wso2.carbon.analytics</groupId> <!--<groupId>org.wso2.carbon.analytics</groupId>-->
<artifactId>org.wso2.carbon.analytics.dataservice.commons</artifactId> <!--<artifactId>org.wso2.carbon.analytics.dataservice.commons</artifactId>-->
<scope>provided</scope> <!--<scope>provided</scope>-->
</dependency> <!--</dependency>-->
<dependency> <dependency>
<groupId>org.wso2.carbon.analytics-common</groupId> <groupId>org.wso2.carbon.analytics-common</groupId>
<artifactId>org.wso2.carbon.event.receiver.stub</artifactId> <artifactId>org.wso2.carbon.event.receiver.stub</artifactId>

@ -27,12 +27,12 @@ import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.wso2.carbon.analytics.api.AnalyticsDataAPI; import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil; import org.wso2.carbon.analytics.api.AnalyticsDataAPIUtil;
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse; //import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry; //import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry;
import org.wso2.carbon.analytics.dataservice.commons.SortByField; //import org.wso2.carbon.analytics.dataservice.commons.SortByField;
import org.wso2.carbon.analytics.dataservice.commons.SortType; //import org.wso2.carbon.analytics.dataservice.commons.SortType;
import org.wso2.carbon.analytics.datasource.commons.Record; //import org.wso2.carbon.analytics.datasource.commons.Record;
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; //import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
@ -120,40 +120,40 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
if (from != 0 || to != 0) { if (from != 0 || to != 0) {
query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]"; query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]";
} }
try { // try {
if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized( // if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized(
new DeviceIdentifier(deviceId, deviceType), // new DeviceIdentifier(deviceId, deviceType),
DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) { // DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); // return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
} // }
List<SortByField> sortByFields = new ArrayList<>(); // List<SortByField> sortByFields = new ArrayList<>();
SortByField sortByField = new SortByField("timeStamp", SortType.ASC); // SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
sortByFields.add(sortByField); // sortByFields.add(sortByField);
// this is the user who initiates the request // this is the user who initiates the request
String authorizedUser = MultitenantUtils.getTenantAwareUsername( // String authorizedUser = MultitenantUtils.getTenantAwareUsername(
CarbonContext.getThreadLocalCarbonContext().getUsername()); // CarbonContext.getThreadLocalCarbonContext().getUsername());
try { // try {
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); // String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); // int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI(); AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query, // List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
0, // 0,
100, // 100,
sortByFields); // sortByFields);
List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(), // List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
searchResults); // searchResults);
return Response.ok().entity(events).build(); // return Response.ok().entity(events).build();
} catch (AnalyticsException | UserStoreException e) { // } catch (AnalyticsException | UserStoreException e) {
log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e); // log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
throw DeviceMgtUtil.buildBadRequestException( // throw DeviceMgtUtil.buildBadRequestException(
Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT); // Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
} // }
} catch (DeviceAccessAuthorizationException e) { // } catch (DeviceAccessAuthorizationException e) {
log.error(e.getErrorMessage()); // log.error(e.getErrorMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
} // }
} }
@Path("stats/device-locations") @Path("stats/device-locations")
@ -559,40 +559,40 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
if (from != 0 || to != 0) { if (from != 0 || to != 0) {
query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]"; query += " AND timeStamp : [" + fromDate + " TO " + toDate + "]";
} }
try { // try {
if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized( // if (!DeviceMgtAPIUtils.getDeviceAccessAuthorizationService().isUserAuthorized(
new DeviceIdentifier(deviceId, deviceType), // new DeviceIdentifier(deviceId, deviceType),
DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) { // DeviceGroupConstants.Permissions.DEFAULT_STATS_MONITOR_PERMISSIONS)) {
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); // return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
} // }
List<SortByField> sortByFields = new ArrayList<>(); // List<SortByField> sortByFields = new ArrayList<>();
SortByField sortByField = new SortByField("timeStamp", SortType.ASC); // SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
sortByFields.add(sortByField); // sortByFields.add(sortByField);
// this is the user who initiates the request // this is the user who initiates the request
String authorizedUser = MultitenantUtils.getTenantAwareUsername( // String authorizedUser = MultitenantUtils.getTenantAwareUsername(
CarbonContext.getThreadLocalCarbonContext().getUsername()); // CarbonContext.getThreadLocalCarbonContext().getUsername());
try { // try {
String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser); // String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser);
int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); // int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI(); // AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query, // List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
0, // 0,
100, // 100,
sortByFields); // sortByFields);
List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(), // List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
searchResults); // searchResults);
return Response.ok().entity(events).build(); // return Response.ok().entity(events).build();
} catch (AnalyticsException | UserStoreException e) { // } catch (AnalyticsException | UserStoreException e) {
log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e); // log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
throw DeviceMgtUtil.buildBadRequestException( // throw DeviceMgtUtil.buildBadRequestException(
Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT); // Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
} // }
} catch (DeviceAccessAuthorizationException e) { // } catch (DeviceAccessAuthorizationException e) {
log.error(e.getErrorMessage()); // log.error(e.getErrorMessage());
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
} // }
} }
@Path("alerts/history") @Path("alerts/history")
@ -607,80 +607,80 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService {
if (from != 0 || to != 0) { if (from != 0 || to != 0) {
query = "timeStamp : [" + fromDate + " TO " + toDate + "]"; query = "timeStamp : [" + fromDate + " TO " + toDate + "]";
} }
try { // try {
List<SortByField> sortByFields = new ArrayList<>(); // List<SortByField> sortByFields = new ArrayList<>();
SortByField sortByField = new SortByField("timeStamp", SortType.ASC); // SortByField sortByField = new SortByField("timeStamp", SortType.ASC);
sortByFields.add(sortByField); // sortByFields.add(sortByField);
//
// this is the user who initiates the request // // this is the user who initiates the request
String authorizedUser = MultitenantUtils.getTenantAwareUsername( // String authorizedUser = MultitenantUtils.getTenantAwareUsername(
CarbonContext.getThreadLocalCarbonContext().getUsername()); // CarbonContext.getThreadLocalCarbonContext().getUsername());
//
String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser); // String tenantDomain = MultitenantUtils.getTenantDomain(authorizedUser);
int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain); // int tenantId = DeviceMgtAPIUtils.getRealmService().getTenantManager().getTenantId(tenantDomain);
AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI(); // AnalyticsDataAPI analyticsDataAPI = DeviceMgtAPIUtils.getAnalyticsDataAPI();
List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query, // List<SearchResultEntry> searchResults = analyticsDataAPI.search(tenantId, tableName, query,
0, // 0,
100, // 100,
sortByFields); // sortByFields);
List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(), // List<Event> events = getEventBeans(analyticsDataAPI, tenantId, tableName, new ArrayList<String>(),
searchResults); // searchResults);
return Response.ok().entity(events).build(); // return Response.ok().entity(events).build();
//
} catch (AnalyticsException | UserStoreException e) { // } catch (AnalyticsException | UserStoreException e) {
log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e); // log.error("Failed to perform search on table: " + tableName + " : " + e.getMessage(), e);
throw DeviceMgtUtil.buildBadRequestException( throw DeviceMgtUtil.buildBadRequestException(
Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT); Constants.ErrorMessages.STATUS_BAD_REQUEST_MESSAGE_DEFAULT);
} // }
}
private List<Event> getEventBeans(AnalyticsDataAPI analyticsDataAPI, int tenantId, String tableName,
List<String> columns,
List<SearchResultEntry> searchResults) throws AnalyticsException {
List<String> ids = getIds(searchResults);
List<String> requiredColumns = (columns == null || columns.isEmpty()) ? null : columns;
AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, requiredColumns, ids);
List<Record> records = AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response);
Map<String, Event> eventBeanMap = getEventBeanKeyedWithIds(records);
return getSortedEventBeans(eventBeanMap, searchResults);
}
private List<Event> getSortedEventBeans(Map<String, Event> eventBeanMap,
List<SearchResultEntry> searchResults) {
List<Event> sortedRecords = new ArrayList<>();
for (SearchResultEntry entry : searchResults) {
sortedRecords.add(eventBeanMap.get(entry.getId()));
}
return sortedRecords;
} }
private Map<String, Event> getEventBeanKeyedWithIds(List<Record> records) { // private List<Event> getEventBeans(AnalyticsDataAPI analyticsDataAPI, int tenantId, String tableName,
Map<String, Event> eventBeanMap = new HashMap<>(); // List<String> columns,
for (Record record : records) { // List<SearchResultEntry> searchResults) throws AnalyticsException {
Event event = getEventBean(record); // List<String> ids = getIds(searchResults);
eventBeanMap.put(event.getId(), event); // List<String> requiredColumns = (columns == null || columns.isEmpty()) ? null : columns;
} // AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, requiredColumns, ids);
return eventBeanMap; // List<Record> records = AnalyticsDataAPIUtil.listRecords(analyticsDataAPI, response);
} // Map<String, Event> eventBeanMap = getEventBeanKeyedWithIds(records);
// return getSortedEventBeans(eventBeanMap, searchResults);
private List<String> getIds(List<SearchResultEntry> searchResults) { // }
List<String> ids = new ArrayList<>(); //
if (searchResults != null) { // private List<Event> getSortedEventBeans(Map<String, Event> eventBeanMap,
for (SearchResultEntry resultEntry : searchResults) { // List<SearchResultEntry> searchResults) {
ids.add(resultEntry.getId()); // List<Event> sortedRecords = new ArrayList<>();
} // for (SearchResultEntry entry : searchResults) {
} // sortedRecords.add(eventBeanMap.get(entry.getId()));
return ids; // }
} // return sortedRecords;
// }
private static Event getEventBean(Record record) { //
Event eventBean = new Event(); // private Map<String, Event> getEventBeanKeyedWithIds(List<Record> records) {
eventBean.setId(record.getId()); // Map<String, Event> eventBeanMap = new HashMap<>();
eventBean.setTableName(record.getTableName()); // for (Record record : records) {
eventBean.setTimestamp(record.getTimestamp()); // Event event = getEventBean(record);
eventBean.setValues(record.getValues()); // eventBeanMap.put(event.getId(), event);
return eventBean; // }
} // return eventBeanMap;
// }
//
// private List<String> getIds(List<SearchResultEntry> searchResults) {
// List<String> ids = new ArrayList<>();
// if (searchResults != null) {
// for (SearchResultEntry resultEntry : searchResults) {
// ids.add(resultEntry.getId());
// }
// }
// return ids;
// }
//
// private static Event getEventBean(Record record) {
// Event eventBean = new Event();
// eventBean.setId(record.getId());
// eventBean.setTableName(record.getTableName());
// eventBean.setTimestamp(record.getTimestamp());
// eventBean.setValues(record.getValues());
// return eventBean;
// }
@Path("/geo-fence") @Path("/geo-fence")
@POST @POST

Loading…
Cancel
Save