Skip to content

Commit

Permalink
Adding a service that returns downloads counts and adding publisherOr…
Browse files Browse the repository at this point in the history
…gKey to the list of parameters

#117
  • Loading branch information
fmendezh committed May 18, 2021
1 parent 4765134 commit d1c610a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<springdoc-openapi-ui.version>1.5.8</springdoc-openapi-ui.version>

<!-- GBIF -->
<gbif-api.version>0.147</gbif-api.version>
<gbif-api.version>0.150-SNAPSHOT</gbif-api.version>
<gbif-common.version>0.50</gbif-common.version>
<gbif-common-mybatis.version>1.1</gbif-common-mybatis.version>
<gbif-common-ws.version>1.15</gbif-common-ws.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.UUID;

import javax.annotation.Nullable;
import javax.xml.crypto.Data;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.ResultActions;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.ImmutableMap;

import static org.gbif.registry.ws.it.fixtures.TestConstants.IT_APP_KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import javax.xml.bind.JAXBElement;
import javax.xml.transform.stream.StreamSource;

import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
Expand All @@ -44,6 +43,7 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.gbif.registry.ws.it.fixtures.TestConstants.IT_APP_KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
* Helper class tha generates a Citation String from {@link Dataset} and {@link Organization}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,13 @@ List<Facet.Count> getDownloadedRecordsByDataset(
@Nullable @Param("fromDate") Date fromDate,
@Nullable @Param("toDate") Date toDate,
@Nullable @Param("publishingCountry") String publishingCountry,
@Nullable @Param("datasetKey") UUID datasetKey);
@Nullable @Param("datasetKey") UUID datasetKey,
@Nullable @Param("publishingOrgKey") UUID publishingOrgKey);

List<Facet.Count> getDownloadsByDataset(
@Nullable @Param("fromDate") Date fromDate,
@Nullable @Param("toDate") Date toDate,
@Nullable @Param("publishingCountry") String publishingCountry,
@Nullable @Param("datasetKey") UUID datasetKey,
@Nullable @Param("publishingOrgKey") UUID publishingOrgKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
</select>

<select id="getDownloadsByUserCountry" resultType="org.gbif.api.model.common.search.Facet$Count" resultMap="DOWNLOADS_STATISTICS_COUNT_MAP" parameterType="map">
SELECT to_char(year_month AT TIME ZONE 'UTC','YYYY-MM') AS year_month, SUM(total_records) AS total_records, SUM(number_downloads) AS number_downloads
SELECT to_char(year_month AT TIME ZONE 'UTC','YYYY-MM') AS year_month, SUM(number_downloads) AS number_downloads
FROM download_user_statistics
<where>
<if test="userCountry != null">AND user_country = #{userCountry,jdbcType=OTHER}</if>
Expand All @@ -229,8 +229,27 @@
</select>

<select id="getDownloadedRecordsByDataset" resultType="org.gbif.api.model.common.search.Facet$Count" resultMap="DOWNLOADS_STATISTICS_TOTAL_RECORDS_MAP" parameterType="map">
SELECT to_char(year_month AT TIME ZONE 'UTC','YYYY-MM') AS year_month, SUM(total_records) AS total_records, SUM(number_downloads) AS number_downloads
SELECT to_char(year_month AT TIME ZONE 'UTC','YYYY-MM') AS year_month, SUM(total_records) AS total_records
FROM download_statistics
<if test="publishingOrgKey != null">
JOIN dataset d ON d.key = dataset_key AND d.publishing_organization_key = #{publishingOrgKey,jdbcType=OTHER}
</if>
<where>
<if test="publishingCountry != null">AND publishing_organization_country = #{publishingCountry,jdbcType=OTHER}</if>
<if test="datasetKey != null">AND dataset_key = #{datasetKey,jdbcType=OTHER}</if>
<if test="fromDate != null"><![CDATA[AND year_month >= #{fromDate,jdbcType=TIMESTAMP}]]></if>
<if test="toDate != null"><![CDATA[AND year_month < #{toDate,jdbcType=TIMESTAMP}]]></if>
</where>
GROUP BY year_month
ORDER BY year_month DESC;
</select>

<select id="getDownloadsByDataset" resultType="org.gbif.api.model.common.search.Facet$Count" resultMap="DOWNLOADS_STATISTICS_COUNT_MAP" parameterType="map">
SELECT to_char(year_month AT TIME ZONE 'UTC','YYYY-MM') AS year_month, SUM(number_downloads) AS number_downloads
FROM download_statistics
<if test="publishingOrgKey != null">
JOIN dataset d ON d.key = dataset_key AND d.publishing_organization_key = #{publishingOrgKey,jdbcType=OTHER}
</if>
<where>
<if test="publishingCountry != null">AND publishing_organization_country = #{publishingCountry,jdbcType=OTHER}</if>
<if test="datasetKey != null">AND dataset_key = #{datasetKey,jdbcType=OTHER}</if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,21 @@ Map<Integer, Map<Integer, Long>> getDownloadedRecordsByDataset(
@PartialDate("fromDate") Date fromDate,
@PartialDate("toDate") Date toDate,
@RequestParam(value = "publishingCountry", required = false) Country publishingCountry,
@RequestParam(value = "datasetKey", required = false) UUID datasetKey);
@RequestParam(value = "datasetKey", required = false) UUID datasetKey,
@RequestParam(value = "publishingOrgKey", required = false) UUID publishingOrgKey);

@RequestMapping(
method = RequestMethod.GET,
value = "statistics/downloadsByDataset",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@Override
Map<Integer, Map<Integer, Long>> getDownloadsByDataset(
@PartialDate("fromDate") Date fromDate,
@PartialDate("toDate") Date toDate,
@RequestParam(value = "publishingCountry", required = false) Country publishingCountry,
@RequestParam(value = "datasetKey", required = false) UUID datasetKey,
@RequestParam(value = "publishingOrgKey", required = false) UUID publishingOrgKey);

@RequestMapping(
method = RequestMethod.POST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,33 @@ public Map<Integer, Map<Integer, Long>> getDownloadedRecordsByDataset(
@PartialDate Date fromDate,
@PartialDate Date toDate,
Country publishingCountry,
@RequestParam(value = "datasetKey", required = false) UUID datasetKey) {
@RequestParam(value = "datasetKey", required = false) UUID datasetKey,
@RequestParam(value = "publishingOrgKey", required = false) UUID publishingOrgKey) {
return groupByYear(
occurrenceDownloadMapper.getDownloadedRecordsByDataset(
occurrenceDownloadMapper.getDownloadsByDataset(
fromDate,
toDate,
Optional.ofNullable(publishingCountry).map(Country::getIso2LetterCode).orElse(null),
datasetKey));
datasetKey,
publishingOrgKey));
}

@GetMapping("statistics/downloadsByDataset")
@Override
public Map<Integer, Map<Integer, Long>> getDownloadsByDataset(
@PartialDate Date fromDate,
@PartialDate Date toDate,
Country publishingCountry,
@RequestParam(value = "datasetKey", required = false) UUID datasetKey,
@RequestParam(value = "publishingOrgKey", required = false) UUID publishingOrgKey
) {
return groupByYear(
occurrenceDownloadMapper.getDownloadsByDataset(
fromDate,
toDate,
Optional.ofNullable(publishingCountry).map(Country::getIso2LetterCode).orElse(null),
datasetKey,
publishingOrgKey));
}

/** Aggregates the download statistics in tree structure of month grouped by year. */
Expand Down

0 comments on commit d1c610a

Please sign in to comment.