Skip to content

Commit

Permalink
Optimized the code for user report generation (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthik-tarento authored Dec 23, 2022
1 parent df944f4 commit 09a1b11
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 191 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ public class CbExtServerProperties {

@Value("${course.url}")
private String courseLinkUrl;

@Value("${es.user.report.include.fields}")
private String esUserReportIncludeFields;

public String getUserAssessmentSubmissionDuration() {
return userAssessmentSubmissionDuration;
Expand Down Expand Up @@ -1573,4 +1576,12 @@ public String getCourseLinkUrl() {
public void setCourseLinkUrl(String courseLinkUrl) {
this.courseLinkUrl = courseLinkUrl;
}

public String[] getEsUserReportIncludeFields() {
return esUserReportIncludeFields.split(",", -1);
}

public void setEsUserReportIncludeFields(String esUserReportIncludeFields) {
this.esUserReportIncludeFields = esUserReportIncludeFields;
}
}
237 changes: 128 additions & 109 deletions src/main/java/org/sunbird/common/util/IndexerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,113 +31,117 @@
@Service
public class IndexerService {

private Logger logger = LoggerFactory.getLogger(IndexerService.class);

@Autowired
@Qualifier("esClient")
private RestHighLevelClient esClient;

@Autowired
@Qualifier("sbEsClient")
private RestHighLevelClient sbEsClient;

/**
* @param index name of index
* @param indexType index type
* @param entityId entity Id
* @param indexDocument index Document
* @return status
*/
public RestStatus addEntity(String index, String indexType, String entityId, Map<String, Object> indexDocument) {
logger.info("addEntity starts with index {} and entityId {}", index, entityId);
IndexResponse response = null;
try {
if(!StringUtils.isEmpty(entityId)){
response = esClient.index(new IndexRequest(index, indexType, entityId).source(indexDocument), RequestOptions.DEFAULT);
}else{
response = esClient.index(new IndexRequest(index, indexType).source(indexDocument), RequestOptions.DEFAULT);
}
} catch (IOException e) {
logger.error("Exception in adding record to ElasticSearch", e);
}
if (null == response)
return null;
return response.status();
}

/**
* @param index name of index
* @param indexType index type
* @param entityId entity Id
* @param indexDocument index Document
* @return status
*/
public RestStatus updateEntity(String index, String indexType, String entityId, Map<String, ?> indexDocument) {
logger.info("updateEntity starts with index {} and entityId {}", index, entityId);
UpdateResponse response = null;
try {
response = esClient.update(new UpdateRequest(index.toLowerCase(), indexType, entityId).doc(indexDocument), RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception in updating a record to ElasticSearch", e);
}
if (null == response)
return null;
return response.status();
}

/**
* @param index name of index
* @param indexType index type
* @param entityId entity Id
* @return status
*/
public Map<String, Object> readEntity(String index, String indexType, String entityId){
logger.info("readEntity starts with index {} and entityId {}", index, entityId);
GetResponse response = null;
try {
response = esClient.get(new GetRequest(index, indexType, entityId), RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception in getting the record from ElasticSearch", e);
}
if(null == response)
return null;
return response.getSourceAsMap();
}

/**
* Search the document in es based on provided information
*
* @param indexName es index name
* @param type index type
* @param searchSourceBuilder source builder
* @return es search response
* @throws IOException
*/
public SearchResponse getEsResult(String indexName, String type, SearchSourceBuilder searchSourceBuilder, boolean isSunbirdES) throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(indexName);
if (!StringUtils.isEmpty(type))
searchRequest.types(type);
searchRequest.source(searchSourceBuilder);
return getEsResult(searchRequest, isSunbirdES);
}

public RestStatus BulkInsert(List<IndexRequest> indexRequestList) {
BulkResponse restStatus = null;
if (!CollectionUtils.isEmpty(indexRequestList)) {
BulkRequest bulkRequest = new BulkRequest();
indexRequestList.forEach(bulkRequest::add);
try {
restStatus = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception while doing the bulk operation in ElasticSearch", e);
}
}
if(null == restStatus)
return null;
return restStatus.status();
}

private Logger logger = LoggerFactory.getLogger(IndexerService.class);

@Autowired
@Qualifier("esClient")
private RestHighLevelClient esClient;

@Autowired
@Qualifier("sbEsClient")
private RestHighLevelClient sbEsClient;

/**
* @param index name of index
* @param indexType index type
* @param entityId entity Id
* @param indexDocument index Document
* @return status
*/
public RestStatus addEntity(String index, String indexType, String entityId, Map<String, Object> indexDocument) {
logger.info("addEntity starts with index {} and entityId {}", index, entityId);
IndexResponse response = null;
try {
if (!StringUtils.isEmpty(entityId)) {
response = esClient.index(new IndexRequest(index, indexType, entityId).source(indexDocument),
RequestOptions.DEFAULT);
} else {
response = esClient.index(new IndexRequest(index, indexType).source(indexDocument),
RequestOptions.DEFAULT);
}
} catch (IOException e) {
logger.error("Exception in adding record to ElasticSearch", e);
}
if (null == response)
return null;
return response.status();
}

/**
* @param index name of index
* @param indexType index type
* @param entityId entity Id
* @param indexDocument index Document
* @return status
*/
public RestStatus updateEntity(String index, String indexType, String entityId, Map<String, ?> indexDocument) {
logger.info("updateEntity starts with index {} and entityId {}", index, entityId);
UpdateResponse response = null;
try {
response = esClient.update(new UpdateRequest(index.toLowerCase(), indexType, entityId).doc(indexDocument),
RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception in updating a record to ElasticSearch", e);
}
if (null == response)
return null;
return response.status();
}

/**
* @param index name of index
* @param indexType index type
* @param entityId entity Id
* @return status
*/
public Map<String, Object> readEntity(String index, String indexType, String entityId) {
logger.info("readEntity starts with index {} and entityId {}", index, entityId);
GetResponse response = null;
try {
response = esClient.get(new GetRequest(index, indexType, entityId), RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception in getting the record from ElasticSearch", e);
}
if (null == response)
return null;
return response.getSourceAsMap();
}

/**
* Search the document in es based on provided information
*
* @param indexName es index name
* @param type index type
* @param searchSourceBuilder source builder
* @return es search response
* @throws IOException
*/
public SearchResponse getEsResult(String indexName, String type, SearchSourceBuilder searchSourceBuilder,
boolean isSunbirdES) throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(indexName);
if (!StringUtils.isEmpty(type))
searchRequest.types(type);
searchRequest.source(searchSourceBuilder);
return getEsResult(searchRequest, isSunbirdES);
}

public RestStatus BulkInsert(List<IndexRequest> indexRequestList) {
BulkResponse restStatus = null;
if (!CollectionUtils.isEmpty(indexRequestList)) {
BulkRequest bulkRequest = new BulkRequest();
indexRequestList.forEach(bulkRequest::add);
try {
restStatus = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception while doing the bulk operation in ElasticSearch", e);
}
}
if (null == restStatus)
return null;
return restStatus.status();
}

public long getDocumentCount(String index, SearchSourceBuilder searchSourceBuilder) {
try {
CountRequest countRequest = new CountRequest().indices(index);
Expand All @@ -149,9 +153,24 @@ public long getDocumentCount(String index, SearchSourceBuilder searchSourceBuild
return 0l;
}
}


public long getDocumentCount(String index, boolean isSunbirdES) {
try {
CountRequest countRequest = new CountRequest().indices(index);
if (isSunbirdES) {
return sbEsClient.count(countRequest, RequestOptions.DEFAULT).getCount();
} else {
return esClient.count(countRequest, RequestOptions.DEFAULT).getCount();
}

} catch (Exception e) {

}
return 0l;
}

private SearchResponse getEsResult(SearchRequest searchRequest, boolean isSbES) throws IOException {
if(isSbES) {
if (isSbES) {
return sbEsClient.search(searchRequest, RequestOptions.DEFAULT);
} else {
return esClient.search(searchRequest, RequestOptions.DEFAULT);
Expand Down
Loading

0 comments on commit 09a1b11

Please sign in to comment.