Skip to content

Commit

Permalink
Made code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitha-ravi committed Oct 11, 2024
1 parent 70eb99d commit 6c54f79
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/strandls/esmodule/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ private Constants() {
public static final String IDENTIFIER_ID = "all_reco_vote.authors_voted.id";
public static final String GROUP_BY_DAY = "group_by_day";
public static final String GROUP_BY_OBSERVED = "group_by_observed";

public static final String AGG = "agg";
}
31 changes: 14 additions & 17 deletions src/main/java/com/strandls/esmodule/controllers/ESController.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,20 +390,19 @@ public MapDocument termsAggregation(@PathParam("index") String index, @PathParam
}

@GET
@Path(ApiConstants.AGGREGATION + "/{filter}")
@Path(ApiConstants.AGGREGATION + "/{index}/{user}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Aggregation for List Page", notes = "Returns Aggregated values", response = Map.class)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Location field not specified for bounds", response = String.class),
@ApiOperation(value = "Aggregation for temporal distribution-date created in user page", notes = "Return observations created on data group by year, filtered by userId", response = Map.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Exception", response = String.class),
@ApiResponse(code = 500, message = "ERROR", response = String.class) })

public Response getAggregationPerDay(@PathParam("filter") String filter){
public Response getAggregationPerDay(@PathParam("index") String index, @PathParam("user") String user) {

Map<String, List<Map<String, Object>>> response = null;

try {
response=elasticSearchService.aggregationByDay(filter);
response = elasticSearchService.aggregationByDay(index, user);
return Response.status(Status.OK).entity(response).build();
} catch (Exception e) {
throw new WebApplicationException(
Expand All @@ -412,20 +411,19 @@ public Response getAggregationPerDay(@PathParam("filter") String filter){
}

@GET
@Path(ApiConstants.MONTH_AGGREGATION + "/{user}")
@Path(ApiConstants.MONTH_AGGREGATION + "{index}/{user}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Aggregation for List Page", notes = "Returns Aggregated values", response = Map.class)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Location field not specified for bounds", response = String.class),
@ApiResponse(code = 500, message = "ERROR", response = String.class) })
@ApiOperation(value = "Aggregation for temporal distribution-month observed in user page", notes = "Return observed on data grouped by month into intervals of 50 years, filtered by userId", response = Map.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Exception", response = String.class),
@ApiResponse(code = 500, message = "ERROR", response = String.class) })

public Response getAggregationPerMonth(@PathParam("user") String user){
public Response getAggregationPerMonth(@PathParam("index") String index, @PathParam("user") String user) {

Map<String, List<Map<String, Object>>> response = null;

try {
response=elasticSearchService.aggregationByMonth(user);
response = elasticSearchService.aggregationByMonth(index, user);
return Response.status(Status.OK).entity(response).build();
} catch (Exception e) {
throw new WebApplicationException(
Expand Down Expand Up @@ -784,8 +782,7 @@ public Response getListPageFilterValue(@PathParam("index") String index, @PathPa
@ApiOperation(value = "Auto complete username", notes = "Returns List of userIbp", response = MapResponse.class)
@ApiResponses(value = { @ApiResponse(code = 500, message = "ERROR", response = String.class) })
public Response autocompleteUserIBP(@PathParam("index") String index, @PathParam("type") String type,
@QueryParam("userGroupId") String userGroupId, @QueryParam("name") String name)
throws IOException {
@QueryParam("userGroupId") String userGroupId, @QueryParam("name") String name) throws IOException {
MapResponse results = elasticSearchService.autocompleteUserIBP(index, type, userGroupId, name);
return Response.status(Status.OK).entity(results).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,19 @@ MapDocument termsAggregation(String index, String type, String field, String sub

/**
*
* @param filter the index in which to search
* @param index
* @param user
* @return {@link Map}
*/
Map<String, List<Map<String, Object>>> aggregationByDay(String filter) throws IOException;
Map<String, List<Map<String, Object>>> aggregationByDay(String index, String user) throws IOException;

/**
*
* @param user the index in which to search
* @param index
* @param user
* @return {@link Map}
*/
Map<String, List<Map<String, Object>>> aggregationByMonth(String user) throws IOException;
Map<String, List<Map<String, Object>>> aggregationByMonth(String index, String user) throws IOException;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,89 +550,93 @@ public MapResponse rangeSearch(String index, String type, List<MapRangeQuery> qu
}

@Override
public Map<String, List<Map<String, Object>>> aggregationByDay(String filter) throws IOException {
public Map<String, List<Map<String, Object>>> aggregationByDay(String index, String user) throws IOException {

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
TermQueryBuilder authorFilter = QueryBuilders.termQuery("author_id", filter);
boolQuery.filter(authorFilter);
TermQueryBuilder authorFilter = QueryBuilders.termQuery("author_id", user);
boolQuery.filter(authorFilter);
AggregationBuilder aggregation = null;
aggregation = AggregationBuilders.dateHistogram("agg").field("created_on").dateHistogramInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
aggregation = AggregationBuilders.dateHistogram(Constants.AGG).field("created_on")
.calendarInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
AggregationResponse aggregationResponse = new AggregationResponse();
if (aggregation == null)
return null;
return null;

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (boolQuery != null)
sourceBuilder.query(boolQuery);
sourceBuilder.aggregation(aggregation);

SearchRequest request = new SearchRequest("extended_observation");
SearchRequest request = new SearchRequest(index);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Map<String,List<Map<String, Object>>> groupbyday= new LinkedHashMap <String,List<Map<String, Object>>>();
Map<String, List<Map<String, Object>>> groupbyday = new LinkedHashMap<String, List<Map<String, Object>>>();
Histogram dateHistogram = response.getAggregations().get("agg");

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
String year = entry.getKeyAsString().substring(0,4);
String year = entry.getKeyAsString().substring(0, 4);
List<Map<String, Object>> yeardata;
if(groupbyday.containsKey(year)) {
if (groupbyday.containsKey(year)) {
yeardata = groupbyday.get(year);
} else {
yeardata = new ArrayList<>();
}

Map<String, Object> data = new HashMap<>();
data.put("date", entry.getKeyAsString());
data.put("value", entry.getDocCount());
yeardata.add(data);
data.put("date", entry.getKeyAsString());
data.put("value", entry.getDocCount());
yeardata.add(data);
groupbyday.put(year, yeardata);
}

return groupbyday;
}

@Override
public Map<String, List<Map<String, Object>>> aggregationByMonth(String user) throws IOException {
public Map<String, List<Map<String, Object>>> aggregationByMonth(String index, String user) throws IOException {

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
TermQueryBuilder authorFilter = QueryBuilders.termQuery("author_id", user);
boolQuery.filter(authorFilter);
boolQuery.filter(authorFilter);
AggregationBuilder aggregation = null;
aggregation = AggregationBuilders.dateHistogram("agg").field("from_date").dateHistogramInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
aggregation = AggregationBuilders.dateHistogram(Constants.AGG).field("from_date")
.calendarInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
AggregationResponse aggregationResponse = new AggregationResponse();
if (aggregation == null)
return null;
return null;

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (boolQuery != null)
sourceBuilder.query(boolQuery);
sourceBuilder.aggregation(aggregation);

SearchRequest request = new SearchRequest("extended_observation");
SearchRequest request = new SearchRequest(index);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Histogram dateHistogram = response.getAggregations().get("agg");
Histogram.Bucket lastBucket = dateHistogram.getBuckets().get(dateHistogram.getBuckets().size() - 1);
Map<String, List<Map<String, Object>>> groupByMonth= new LinkedHashMap<>();
String currentYear = lastBucket.getKeyAsString().substring(0,4);
Map<String, List<Map<String, Object>>> groupByMonth = new LinkedHashMap<>();
String currentYear = lastBucket.getKeyAsString().substring(0, 4);

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
String year = entry.getKeyAsString().substring(0,4);
Integer intervaldiff= Integer.parseInt(currentYear)-Integer.parseInt(year);
Integer intervalId = intervaldiff/50;
String intervalKey = String.format("%04d",Math.max( Integer.parseInt(currentYear)-((intervalId+1)*50),0)) + "-" + String.format("%04d",Integer.parseInt(currentYear)-(intervalId*50));
String year = entry.getKeyAsString().substring(0, 4);
Integer intervaldiff = Integer.parseInt(currentYear) - Integer.parseInt(year);
Integer intervalId = intervaldiff / 50;
String intervalKey = String.format("%04d",
Math.max(Integer.parseInt(currentYear) - ((intervalId + 1) * 50), 0)) + "-"
+ String.format("%04d", Integer.parseInt(currentYear) - (intervalId * 50));
List<Map<String, Object>> intervaldata;
if(groupByMonth.containsKey(intervalKey)) {
if (groupByMonth.containsKey(intervalKey)) {
intervaldata = groupByMonth.get(intervalKey);
} else {
intervaldata = new ArrayList<>();
}
Map<String, Object> data = new HashMap<>();
data.put("month", entry.getKeyAsString().substring(5,8));
data.put("year", entry.getKeyAsString().substring(0,4));
data.put("value", entry.getDocCount());
intervaldata.add(data);
groupByMonth.put(intervalKey, intervaldata);
data.put("month", entry.getKeyAsString().substring(5, 8));
data.put("year", entry.getKeyAsString().substring(0, 4));
data.put("value", entry.getDocCount());
intervaldata.add(data);
groupByMonth.put(intervalKey, intervaldata);
}

return groupByMonth;
Expand Down Expand Up @@ -666,11 +670,12 @@ public AggregationResponse aggregation(String index, String type, MapSearchQuery
aggregation = AggregationBuilders.nested(nestedFiled, nestedFiled)
.subAggregation(AggregationBuilders.terms(nestedFilter).field(nestedFilter).size(1000));
} else if (filter.equals(Constants.GROUP_BY_DAY)) {
aggregation = AggregationBuilders.dateHistogram("agg").field("created_on").dateHistogramInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
aggregation = AggregationBuilders.dateHistogram(Constants.AGG).field("created_on")
.calendarInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
} else if (filter.equals(Constants.GROUP_BY_OBSERVED)) {
aggregation = AggregationBuilders.dateHistogram("agg").field("from_date").dateHistogramInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
}
else {
aggregation = AggregationBuilders.dateHistogram(Constants.AGG).field("from_date")
.calendarInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
} else {
aggregation = AggregationBuilders.terms(filter).field(filter).size(1000);
}

Expand Down Expand Up @@ -935,7 +940,8 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
Map<Object, Long> groupMonth;

if (filter.equals(Constants.MVR_SCIENTIFIC_NAME) || filter.equals(Constants.AUTHOR_ID)
|| filter.equals(Constants.IDENTIFIER_ID) || filter.equals(Constants.GROUP_BY_DAY) || filter.equals(Constants.GROUP_BY_OBSERVED)) {
|| filter.equals(Constants.IDENTIFIER_ID) || filter.equals(Constants.GROUP_BY_DAY)
|| filter.equals(Constants.GROUP_BY_OBSERVED)) {
groupMonth = new LinkedHashMap<Object, Long>();
} else {
groupMonth = new HashMap<Object, Long>();
Expand Down Expand Up @@ -970,7 +976,7 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
groupMonth.put(entry.getKeyAsString(), entry.getDocCount());
}
}else {
} else {
Terms frommonth = response.getAggregations().get(filter);

for (Terms.Bucket entry : frommonth.getBuckets()) {
Expand Down

0 comments on commit 6c54f79

Please sign in to comment.