Skip to content

Commit

Permalink
fixed organizations and country metrics for occurrence downloads usages
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Jul 21, 2023
1 parent c67f7cb commit 35be3c5
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,28 @@ public void testDownloadUsages(ServiceType serviceType) {
occurrenceDownload.getKey(), null, null, null, new PagingRequest());
assertEquals(2, organizationUsages.getResults().size());
assertEquals(2, organizationUsages.getCount());

organizationUsages =
service.listOrganizationUsages(
occurrenceDownload.getKey(),
null,
OrganizationUsageSortField.COUNTRY_CODE,
null,
new PagingRequest(0, 1));
assertEquals(1, organizationUsages.getResults().size());
assertEquals(2, organizationUsages.getCount());

organizationUsages =
service.listOrganizationUsages(
occurrenceDownload.getKey(),
null,
OrganizationUsageSortField.COUNTRY_CODE,
SortOrder.DESC,
new PagingRequest(1, 1));
assertEquals(1, organizationUsages.getResults().size());
assertEquals(2, organizationUsages.getCount());
assertEquals(o.getKey(), organizationUsages.getResults().get(0).getOrganizationKey());

organizationUsages =
service.listOrganizationUsages(
occurrenceDownload.getKey(),
Expand Down Expand Up @@ -579,6 +601,18 @@ public void testDownloadUsages(ServiceType serviceType) {
assertEquals(2, countryUsages.getResults().size());
assertEquals(2, countryUsages.getCount());

countryUsages =
service.listCountryUsages(
occurrenceDownload.getKey(),
CountryUsageSortField.COUNTRY_CODE,
SortOrder.DESC,
new PagingRequest(1, 1));
assertEquals(1, countryUsages.getResults().size());
assertEquals(2, countryUsages.getCount());
assertEquals(
Country.DENMARK.getIso2LetterCode(),
countryUsages.getResults().get(0).getPublishingCountryCode());

countryUsages =
service.listCountryUsages(
occurrenceDownload.getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@
import java.net.URI;
import java.util.UUID;

import org.gbif.registry.persistence.mapper.params.OrganizationOccurrenceDownloadDto;

import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -128,10 +126,6 @@ ConfigurationCustomizer mybatisConfigCustomizer() {
configuration.getTypeAliasRegistry().registerAlias("Metadata", Metadata.class);
configuration.getTypeAliasRegistry().registerAlias("Download", Download.class);
configuration.getTypeAliasRegistry().registerAlias("DownloadRequest", DownloadRequest.class);
configuration
.getTypeAliasRegistry()
.registerAlias(
"OrganizationOccurrenceDownloadDto", OrganizationOccurrenceDownloadDto.class);
configuration
.getTypeAliasRegistry()
.registerAlias("PredicateDownloadRequest", PredicateDownloadRequest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.model.occurrence.DownloadType;
import org.gbif.api.model.registry.CountryOccurrenceDownloadUsage;
import org.gbif.api.model.registry.DatasetOccurrenceDownloadUsage;
import org.gbif.api.model.registry.OrganizationOccurrenceDownloadUsage;
import org.gbif.api.vocabulary.CountryUsageSortField;
import org.gbif.api.vocabulary.DatasetUsageSortField;
import org.gbif.api.vocabulary.OrganizationUsageSortField;
import org.gbif.api.vocabulary.SortOrder;
import org.gbif.registry.persistence.mapper.params.OrganizationOccurrenceDownloadDto;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,16 +58,22 @@ void createOrUpdateUsages(
@Param("downloadKey") String downloadKey,
@Param("citationMap") Map<UUID, Long> downloadDataset);

List<OrganizationOccurrenceDownloadDto> listOrganizationsByDownload(
List<OrganizationOccurrenceDownloadUsage> listOrganizationsByDownload(
@Param("downloadKey") String downloadKey,
@Param("organizationTitle") String organizationTitle,
@Param("sortBy") OrganizationUsageSortField sortBy,
@Param("sortOrder") SortOrder sortOrder,
@Nullable @Param("page") Pageable page);

int countOrganizationsByDownload(
List<CountryOccurrenceDownloadUsage> listCountriesByDownload(
@Param("downloadKey") String downloadKey,
@Param("organizationTitle") String organizationTitle);
@Param("sortBy") CountryUsageSortField sortBy,
@Param("sortOrder") SortOrder sortOrder,
@Nullable @Param("page") Pageable page);

int countCountriesByDownload(
int countOrganizationsByDownload(
@Param("downloadKey") String downloadKey,
@Param("organizationTitle") String organizationTitle);

int countCountriesByDownload(@Param("downloadKey") String downloadKey);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,24 @@
<!-- The download is null -->
</resultMap>

<resultMap id="ORGANIZATION_OCCURRENCE_MAP" type="OrganizationOccurrenceDownloadDto" autoMapping="false">
<resultMap id="ORGANIZATION_OCCURRENCE_DOWNLOAD_MAP" type="org.gbif.api.model.registry.OrganizationOccurrenceDownloadUsage" autoMapping="false">
<id property="downloadKey" column="download_key"/>
<id property="datasetKey" column="dataset_key"/>
<result property="organizationKey" column="orgKey"/>
<result property="organizationTitle" column="orgTitle"/>
<result property="numberRecords" column="number_records"/>
<result property="publishingCountryCode" column="country"/>
<id property="organizationKey" column="organizationKey"/>
<result property="organizationTitle" column="organizationTitle"/>
<result property="numberRecords" column="numberRecords"/>
<result property="publishingCountryCode" column="publishingCountryCode"/>
</resultMap>

<resultMap id="COUNTRY_OCCURRENCE_DOWNLOAD_MAP" type="org.gbif.api.model.registry.CountryOccurrenceDownloadUsage" autoMapping="false">
<id property="downloadKey" column="download_key"/>
<id property="publishingCountryCode" column="publishingCountryCode"/>
<result property="numberRecords" column="numberRecords"/>
</resultMap>

<sql id="DATASET_OCCURRENCE_DOWNLOAD_FIELDS">
dod.download_key,dod.dataset_key,dod.dataset_title,dod.dataset_doi,dod.dataset_citation,dod.number_records
</sql>

<sql id="ORGANIZATION_OCCURRENCE_DOWNLOAD_FIELDS">
dod.download_key,dod.dataset_key,dod.number_records,o.key AS orgKey,o.title AS orgTitle,o.country
</sql>

<select id="listByDataset" resultMap="DATASET_OCCURRENCE_DOWNLOAD_MAP" parameterType="map">
SELECT <include refid="DATASET_OCCURRENCE_DOWNLOAD_FIELDS"/>,<include refid="org.gbif.registry.persistence.mapper.OccurrenceDownloadMapper.OCCURRENCE_DOWNLOAD_FIELDS"/>
FROM dataset_occurrence_download dod
Expand Down Expand Up @@ -97,8 +98,10 @@
</if>
</select>

<select id="listOrganizationsByDownload" resultMap="ORGANIZATION_OCCURRENCE_MAP" parameterType="org.gbif.api.model.common.paging.Pageable">
SELECT <include refid="ORGANIZATION_OCCURRENCE_DOWNLOAD_FIELDS"/>
<select id="listOrganizationsByDownload" resultMap="ORGANIZATION_OCCURRENCE_DOWNLOAD_MAP"
parameterType="org.gbif.api.model.common.paging.Pageable">
SELECT o.key AS organizationKey, o.title AS organizationTitle, o.country AS publishingCountryCode, dod.download_key,
sum(dod.number_records) AS numberRecords
FROM dataset_occurrence_download dod
INNER JOIN occurrence_download od ON dod.download_key = od.key
INNER JOIN dataset d on d.key = dod.dataset_key
Expand All @@ -107,7 +110,75 @@
<if test="organizationTitle != null" >
AND o.title = #{organizationTitle,jdbcType=VARCHAR}
</if>
ORDER BY od.created DESC, od.key
GROUP BY o.key, o.title, o.country, dod.download_key, od.created, od.key
ORDER BY
<if test="sortBy != null">
<choose>
<when test="sortBy.name.equals('ORGANIZATION_TITLE')">
o.title
</when>
<when test="sortBy.name.equals('COUNTRY_CODE')">
o.country
</when>
<when test="sortBy.name.equals('RECORD_COUNT')">
numberRecords
</when>
<otherwise/>
</choose>
<if test="sortOrder != null">
<choose>
<when test="sortOrder.name.equals('DESC')">
DESC
</when>
<otherwise>
ASC
</otherwise>
</choose>
</if>
,
</if>
od.created DESC, od.key, o.key
<if test="page != null" >
LIMIT #{page.limit} OFFSET #{page.offset}
</if>
</select>

<select id="listCountriesByDownload" resultMap="COUNTRY_OCCURRENCE_DOWNLOAD_MAP"
parameterType="org.gbif.api.model.common.paging.Pageable">
SELECT o.country AS publishingCountryCode, dod.download_key, sum(dod.number_records) AS numberRecords
FROM dataset_occurrence_download dod
INNER JOIN occurrence_download od ON dod.download_key = od.key
INNER JOIN dataset d on d.key = dod.dataset_key
INNER JOIN organization o ON o.key = d.publishing_organization_key
WHERE dod.download_key = #{downloadKey,jdbcType=OTHER}
GROUP BY o.country,dod.download_key,od.created,od.key
ORDER BY
<if test="sortBy != null">
<choose>
<when test="sortBy.name.equals('COUNTRY_CODE')">
o.country
</when>
<when test="sortBy.name.equals('RECORD_COUNT')">
numberRecords
</when>
<otherwise/>
</choose>
<if test="sortOrder != null">
<choose>
<when test="sortOrder.name.equals('DESC')">
DESC
</when>
<otherwise>
ASC
</otherwise>
</choose>
</if>
,
</if>
od.created DESC, od.key
<if test="sortBy != null and sortBy.name.equals('RECORD_COUNT')">
,o.country
</if>
<if test="page != null" >
LIMIT #{page.limit} OFFSET #{page.offset}
</if>
Expand All @@ -132,9 +203,6 @@
INNER JOIN dataset d on d.key = dod.dataset_key
INNER JOIN organization o ON o.key = d.publishing_organization_key
WHERE dod.download_key = #{downloadKey,jdbcType=OTHER}
<if test="organizationTitle != null" >
AND o.title = #{organizationTitle,jdbcType=VARCHAR}
</if>
</select>

<select id="countByDataset" resultType="Integer" parameterType="map">
Expand Down
Loading

0 comments on commit 35be3c5

Please sign in to comment.