Skip to content

Commit

Permalink
Made review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rishitha-ravi committed Oct 16, 2024
1 parent 9b99627 commit 78f9437
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/strandls/esmodule/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private ApiConstants() {
public static final String GEOHASH_AGGREGATION = "/geohash-aggregation";
public static final String TERMS_AGGREGATION = "/terms-aggregation";
public static final String AGGREGATION = "/aggregation";
public static final String TEMPORAL_AGGREGATION = "/temporal-aggregation";
public static final String MONTH_AGGREGATION = "/month-aggregation";
public static final String BOUNDS = "/bounds";
public static final String SEARCH = "/search";
Expand Down
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 @@ -23,5 +23,5 @@ private Constants() {
public static final String GROUP_BY_DAY = "group_by_day";
public static final String GROUP_BY_OBSERVED = "group_by_observed";
public static final String GROUP_BY_TRAITS = "group_by_traits";
public static final String AGG = "agg";
public static final String TEMPORAL_AGG = "temporal_agg";
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.strandls.esmodule.indexes.pojo.UserScore;
import com.strandls.esmodule.models.AggregationResponse;
import com.strandls.esmodule.models.AuthorUploadedObservationInfo;
import com.strandls.esmodule.models.DayAggregation;
import com.strandls.esmodule.models.FilterPanelData;
import com.strandls.esmodule.models.GeoHashAggregationData;
import com.strandls.esmodule.models.IdentifiersInfo;
Expand All @@ -42,6 +43,7 @@
import com.strandls.esmodule.models.MapResponse;
import com.strandls.esmodule.models.MapSearchParams;
import com.strandls.esmodule.models.MapSortType;
import com.strandls.esmodule.models.MonthAggregation;
import com.strandls.esmodule.models.ObservationInfo;
import com.strandls.esmodule.models.ObservationLatLon;
import com.strandls.esmodule.models.ObservationNearBy;
Expand Down Expand Up @@ -390,7 +392,7 @@ public MapDocument termsAggregation(@PathParam("index") String index, @PathParam
}

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

@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)
Expand All @@ -399,7 +401,7 @@ public MapDocument termsAggregation(@PathParam("index") String index, @PathParam

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

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

try {
response = elasticSearchService.aggregationByDay(index, user);
Expand All @@ -411,16 +413,16 @@ public Response getAggregationPerDay(@PathParam("index") String index, @PathPara
}

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

@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) })
@ApiResponse(code = 500, message = "ERROR", response = String.class) })

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

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

try {
response = elasticSearchService.aggregationByMonth(index, user);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/strandls/esmodule/models/DayAggregation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
*
*/
package com.strandls.esmodule.models;

/**
* @author Rishitha Ravi
*
*
*/
public class DayAggregation {

private String date;
private Long value;

public DayAggregation() {
super();
}

public DayAggregation(String date, Long value) {
super();
this.date = date;
this.value = value;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public Long getValue() {
return value;
}

public void setValue(Long value) {
this.value = value;
}

}
52 changes: 52 additions & 0 deletions src/main/java/com/strandls/esmodule/models/MonthAggregation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
*
*/
package com.strandls.esmodule.models;

/**
* @author Rishitha Ravi
*
*
*/
public class MonthAggregation {

private String month;
private String year;
private Long value;

public MonthAggregation() {
super();
}

public MonthAggregation(String month, String year, Long value) {
super();
this.month = month;
this.year = year;
this.value = value;
}

public String getMonth() {
return month;
}

public void setMonth(String month) {
this.month = month;
}

public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}

public Long getValue() {
return value;
}

public void setValue(Long value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import com.strandls.esmodule.indexes.pojo.ExtendedTaxonDefinition;
import com.strandls.esmodule.models.AggregationResponse;
import com.strandls.esmodule.models.AuthorUploadedObservationInfo;
import com.strandls.esmodule.models.DayAggregation;
import com.strandls.esmodule.models.FilterPanelData;
import com.strandls.esmodule.models.GeoHashAggregationData;
import com.strandls.esmodule.models.IdentifiersInfo;
import com.strandls.esmodule.models.MapDocument;
import com.strandls.esmodule.models.MapQueryResponse;
import com.strandls.esmodule.models.MapResponse;
import com.strandls.esmodule.models.MapSearchParams;
import com.strandls.esmodule.models.MonthAggregation;
import com.strandls.esmodule.models.ObservationInfo;
import com.strandls.esmodule.models.ObservationLatLon;
import com.strandls.esmodule.models.ObservationNearBy;
Expand Down Expand Up @@ -216,15 +218,15 @@ MapDocument termsAggregation(String index, String type, String field, String sub
* @param user
* @return {@link Map}
*/
Map<String, List<Map<String, Object>>> aggregationByDay(String index, String user) throws IOException;
Map<String, List<DayAggregation>> aggregationByDay(String index, String user) throws IOException;

/**
*
* @param index
* @param user
* @return {@link Map}
*/
Map<String, List<Map<String, Object>>> aggregationByMonth(String index, String user) throws IOException;
Map<String, List<MonthAggregation>> aggregationByMonth(String index, String user) throws IOException;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.DocValueFormat.DateTime;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
Expand Down Expand Up @@ -101,6 +100,7 @@
import com.strandls.esmodule.models.AuthorUploadedObservationInfo;
import com.strandls.esmodule.models.CustomFieldValues;
import com.strandls.esmodule.models.CustomFields;
import com.strandls.esmodule.models.DayAggregation;
import com.strandls.esmodule.models.FilterPanelData;
import com.strandls.esmodule.models.GeoHashAggregationData;
import com.strandls.esmodule.models.IdentifiersInfo;
Expand All @@ -113,6 +113,7 @@
import com.strandls.esmodule.models.MapSortType;
import com.strandls.esmodule.models.MaxVotedReco;
import com.strandls.esmodule.models.MaxVotedRecoFreq;
import com.strandls.esmodule.models.MonthAggregation;
import com.strandls.esmodule.models.ObservationInfo;
import com.strandls.esmodule.models.ObservationLatLon;
import com.strandls.esmodule.models.ObservationMapInfo;
Expand Down Expand Up @@ -148,6 +149,9 @@ public class ElasticSearchServiceImpl extends ElasticSearchQueryUtil implements

private static final String USERGROUP = "userGroup";

List<String> months = Arrays.asList("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec");

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -551,15 +555,14 @@ public MapResponse rangeSearch(String index, String type, List<MapRangeQuery> qu
}

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

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

Expand All @@ -571,21 +574,19 @@ public Map<String, List<Map<String, Object>>> aggregationByDay(String index, Str
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>>>();
Histogram dateHistogram = response.getAggregations().get("agg");
Map<String, List<DayAggregation>> groupbyday = new LinkedHashMap<>();
Histogram dateHistogram = response.getAggregations().get(Constants.TEMPORAL_AGG);

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
String year = entry.getKeyAsString().substring(0, 4);
List<Map<String, Object>> yeardata;
String year = entry.getKeyAsString().substring(0, 4); // Extract the year
List<DayAggregation> yeardata;
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());
DayAggregation data = new DayAggregation(entry.getKeyAsString(), entry.getDocCount());
yeardata.add(data);
groupbyday.put(year, yeardata);
}
Expand All @@ -594,15 +595,14 @@ public Map<String, List<Map<String, Object>>> aggregationByDay(String index, Str
}

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

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

Expand All @@ -614,28 +614,27 @@ public Map<String, List<Map<String, Object>>> aggregationByMonth(String index, S
SearchRequest request = new SearchRequest(index);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Histogram dateHistogram = response.getAggregations().get("agg");
Histogram dateHistogram = response.getAggregations().get(Constants.TEMPORAL_AGG);
Histogram.Bucket lastBucket = dateHistogram.getBuckets().get(dateHistogram.getBuckets().size() - 1);
Map<String, List<Map<String, Object>>> groupByMonth = new LinkedHashMap<>();
Map<String, List<MonthAggregation>> groupByMonth = new LinkedHashMap<>();
Integer yearInterval = 50;
String currentYear = lastBucket.getKeyAsString().substring(0, 4);

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
String year = entry.getKeyAsString().substring(0, 4);
String year = entry.getKeyAsString().substring(0, 4); // Extract the year
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;
Integer intervalId = intervaldiff / yearInterval;
String intervalKey = String.format("%04d", // %04d formats the number as a 4-digit year (e.g., 0050)
Math.max(Integer.parseInt(currentYear) - ((intervalId + 1) * yearInterval), 0)) + "-"
+ String.format("%04d", Integer.parseInt(currentYear) - (intervalId * yearInterval));
List<MonthAggregation> 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());
String month = entry.getKeyAsString().substring(5, 8); // Extract the month
MonthAggregation data = new MonthAggregation(month, year, entry.getDocCount());
intervaldata.add(data);
groupByMonth.put(intervalKey, intervaldata);
}
Expand Down Expand Up @@ -671,16 +670,17 @@ 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(Constants.AGG).field("created_on")
aggregation = AggregationBuilders.dateHistogram(Constants.TEMPORAL_AGG).field("created_on")
.calendarInterval(DateHistogramInterval.days(1)).format("yyyy-MM-dd");
} else if (filter.equals(Constants.GROUP_BY_OBSERVED)) {
aggregation = AggregationBuilders.dateHistogram(Constants.AGG).field("from_date")
aggregation = AggregationBuilders.dateHistogram(Constants.TEMPORAL_AGG).field("from_date")
.calendarInterval(DateHistogramInterval.MONTH).format("yyyy-MMM");
} else if (filter.equals(Constants.GROUP_BY_TRAITS)) {
TermsAggregationBuilder termsAgg = AggregationBuilders.terms("traits_agg")
.field("facts.trait_value.trait_aggregation.raw").size(1000);
aggregation = termsAgg.subAggregation(AggregationBuilders.dateHistogram(Constants.AGG).field("from_date")
.calendarInterval(DateHistogramInterval.MONTH).format("yyyy-MMM").minDocCount(1));
aggregation = termsAgg
.subAggregation(AggregationBuilders.dateHistogram(Constants.TEMPORAL_AGG).field("from_date")
.calendarInterval(DateHistogramInterval.MONTH).format("yyyy-MMM").minDocCount(1));
} else {
aggregation = AggregationBuilders.terms(filter).field(filter).size(1000);
}
Expand Down Expand Up @@ -971,26 +971,24 @@ private AggregationResponse groupAggregation(String index, AggregationBuilder ag
groupMonth.put(entry.getKey(), entry.getDocCount());
}
} else if (filter.equals(Constants.GROUP_BY_DAY)) {
Histogram dateHistogram = response.getAggregations().get(Constants.AGG);
Histogram dateHistogram = response.getAggregations().get(Constants.TEMPORAL_AGG);

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
groupMonth.put(entry.getKeyAsString(), entry.getDocCount());
}
} else if (filter.equals(Constants.GROUP_BY_OBSERVED)) {
Histogram dateHistogram = response.getAggregations().get(Constants.AGG);
Histogram dateHistogram = response.getAggregations().get(Constants.TEMPORAL_AGG);

for (Histogram.Bucket entry : dateHistogram.getBuckets()) {
groupMonth.put(entry.getKeyAsString(), entry.getDocCount());
}
} else if (filter.equals(Constants.GROUP_BY_TRAITS)) {
Terms termsHistogram = response.getAggregations().get("traits_agg");
List<String> months = Arrays.asList("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec");
for (Terms.Bucket entry : termsHistogram.getBuckets()) {
Histogram dateHistogram = entry.getAggregations().get("agg");
Histogram dateHistogram = entry.getAggregations().get(Constants.TEMPORAL_AGG);
Map<String, Long> monthSumDays = new LinkedHashMap<>();
for (Histogram.Bucket dateEntry : dateHistogram.getBuckets()) {
String monthName = dateEntry.getKeyAsString().substring(5, 8);
String monthName = dateEntry.getKeyAsString().substring(5, 8); // Extract the month
monthSumDays.put(monthName,
monthSumDays.getOrDefault(monthName, (long) 0) + dateEntry.getDocCount());
}
Expand Down

0 comments on commit 78f9437

Please sign in to comment.