Skip to content

Commit

Permalink
Proof of concept convert ES response to a neutral data object to spit…
Browse files Browse the repository at this point in the history
… out JSON

Created a class called DataTermsFacet, which holds list of term:count pairs.
  • Loading branch information
peterdietz committed Sep 16, 2012
1 parent 9cc7844 commit 2bada68
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dspace-stats/pom.xml
Expand Up @@ -204,6 +204,13 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<reporting>
Expand Down
@@ -0,0 +1,64 @@
package org.dspace.statistics;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
* A neutral data object to hold data for statistics.
*
*/
public class DataTermsFacet {
private List<TermsFacet> terms;

public DataTermsFacet() {
terms = new ArrayList<TermsFacet>();
}
public void addTermFacet(TermsFacet termsFacet ) {
terms.add(termsFacet);
}

/**
* Render this data object into JSON format.
*
* An example of the output could be of the format:
* [{"term":"247166","count":10},{"term":"247168","count":6}]
* @return
*/
public String toJson() {
Gson gson = new Gson();
return gson.toJson(terms);
}




public static class TermsFacet {
private String term;
private Integer count;

public TermsFacet(String term, Integer count) {
setTerm(term);
setCount(count);
}

public String getTerm() {
return term;
}

public void setTerm(String term) {
this.term = term;
}

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}


}
}
Expand Up @@ -12,6 +12,7 @@
import org.dspace.content.*;
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.statistics.DataTermsFacet;
import org.dspace.statistics.ElasticSearchLogger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
Expand Down Expand Up @@ -261,6 +262,15 @@ public void showAllReports() throws WingException, SQLException{
// Top Downloads to Owning Object
TermsFacet bitstreamsFacet = resp.getFacets().facet(TermsFacet.class, "top_bitstreams_lastmonth");
addTermFacetToTable(bitstreamsFacet, division, "Bitstream", "Top Downloads for " + getLastMonthString());

// Convert Elastic Search data to a common DataTermsFacet object, and stuff in DRI/HTML of page.
TermsFacet topBitstreamsFacet = resp.getFacets().facet(TermsFacet.class, "top_bitstreams_lastmonth");
List<? extends TermsFacet.Entry> termsFacetEntries = topBitstreamsFacet.getEntries();
DataTermsFacet termsFacet = new DataTermsFacet();
for(TermsFacet.Entry entry : termsFacetEntries) {
termsFacet.addTermFacet(new DataTermsFacet.TermsFacet(entry.getTerm(), entry.getCount()));
}
division.addHidden("jsonTopDownloads").setValue(termsFacet.toJson());
}

public AbstractFacetBuilder facetTopBitstreamsLastMonth() {
Expand Down

0 comments on commit 2bada68

Please sign in to comment.