Skip to content

Commit

Permalink
Changed code to divide into 50 years observedOn data
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitha-ravi committed Oct 7, 2024
1 parent 3b92dfb commit 70eb99d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,14 @@ public Response getAggregationPerDay(@PathParam("filter") String filter){
@Path(ApiConstants.MONTH_AGGREGATION + "/{user}")
@Produces(MediaType.APPLICATION_JSON)

@ApiOperation(value = "Aggregation for List Page", notes = "Returns Aggregated values", response = List.class)
@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) })

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

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

try {
response=elasticSearchService.aggregationByMonth(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ MapDocument termsAggregation(String index, String type, String field, String sub
* @param user the index in which to search
* @return {@link Map}
*/
List<Map<String, Object>> aggregationByMonth(String user) throws IOException;
Map<String, List<Map<String, Object>>> aggregationByMonth(String user) throws IOException;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public Map<String, List<Map<String, Object>>> aggregationByDay(String filter) th
}

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

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
TermQueryBuilder authorFilter = QueryBuilders.termQuery("author_id", user);
Expand All @@ -611,18 +611,31 @@ public List<Map<String, Object>> aggregationByMonth(String user) throws IOExcept
SearchRequest request = new SearchRequest("extended_observation");
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
List<Map<String, Object>> groupbymonth= new ArrayList();
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);

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));
List<Map<String, Object>> intervaldata;
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());
groupbymonth.add(data);
intervaldata.add(data);
groupByMonth.put(intervalKey, intervaldata);
}

return groupbymonth;
return groupByMonth;
}

@Override
Expand Down

0 comments on commit 70eb99d

Please sign in to comment.