Skip to content

Commit 588b40a

Browse files
committed
addding new method that provides stats about the number records download by datasets published from a country
1 parent 807bd2e commit 588b40a

File tree

6 files changed

+64
-20
lines changed

6 files changed

+64
-20
lines changed

registry-ws-client/src/main/java/org/gbif/registry/ws/client/GenericTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class GenericTypes {
9595
public static final GenericType<Map<UUID, String>> TITLES_MAP_TYPE = new GenericType<Map<UUID, String>>() {
9696
};
9797

98-
public static final GenericType<Map<Integer, Map<Integer,Long>>> DOWNLOADS_MONTHLY_STATS_TYPE = new GenericType<Map<Integer, Map<Integer,Long>>>() {
98+
public static final GenericType<Map<Integer, Map<Integer,Long>>> DOWNLOADS_STATS_TYPE = new GenericType<Map<Integer, Map<Integer,Long>>>() {
9999
};
100100

101101
private GenericTypes() {

registry-ws-client/src/main/java/org/gbif/registry/ws/client/OccurrenceDownloadWsClient.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.gbif.api.model.common.paging.Pageable;
44
import org.gbif.api.model.common.paging.PagingResponse;
5-
import org.gbif.api.model.common.search.Facet;
65
import org.gbif.api.model.occurrence.Download;
76
import org.gbif.api.model.registry.DatasetOccurrenceDownloadUsage;
87
import org.gbif.api.service.registry.OccurrenceDownloadService;
@@ -80,10 +79,19 @@ public PagingResponse<DatasetOccurrenceDownloadUsage> listDatasetUsages(
8079

8180
@Override
8281
public Map<Integer,Map<Integer,Long>> getMonthlyStats(@Nullable Date fromDate, @Nullable Date toDate, @Nullable Country country) {
82+
return statsServiceCall(fromDate, toDate, country, "monthlyStats");
83+
}
84+
85+
@Override
86+
public Map<Integer, Map<Integer, Long>> getDownloadRecordsHostedByCountry(@Nullable Date fromDate, @Nullable Date toDate, @Nullable Country country) {
87+
return statsServiceCall(fromDate, toDate, country, "downloadedRecords");
88+
}
89+
90+
private Map<Integer,Map<Integer,Long>> statsServiceCall(@Nullable Date fromDate, @Nullable Date toDate, @Nullable Country country, String path) {
8391
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
84-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
92+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
8593
Optional.ofNullable(fromDate).ifPresent( d -> params.add("fromDate", simpleDateFormat.format(d)));
8694
Optional.ofNullable(toDate).ifPresent( d -> params.add("toDate", simpleDateFormat.format(d)));
87-
return get(GenericTypes.DOWNLOADS_MONTHLY_STATS_TYPE, null, params ,null,"montlystats");
95+
return get(GenericTypes.DOWNLOADS_STATS_TYPE, null, params ,null,path);
8896
}
8997
}

registry-ws/src/main/java/org/gbif/registry/persistence/mapper/OccurrenceDownloadMapper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ public interface OccurrenceDownloadMapper {
5151
int countByUser(@Param("creator") String creator, @Param("status") Set<Download.Status> status);
5252

5353

54-
List<Facet.Count> getMonthlyStats(@Nullable @Param("fromDate") Date fromDate, @Nullable @Param("toDate") Date toDate,
55-
@Nullable @Param("country") String country);
54+
List<Facet.Count> getMonthlyStats(@Nullable @Param("fromDate") Date fromDate,
55+
@Nullable @Param("toDate") Date toDate,
56+
@Nullable @Param("country") String country);
5657

58+
List<Facet.Count> getDownloadedRecordsByCountry(@Nullable @Param("fromDate") Date fromDate,
59+
@Nullable @Param("toDate") Date toDate,
60+
@Nullable @Param("country") String country);
5761

5862
}

registry-ws/src/main/java/org/gbif/registry/ws/provider/PartialDateProvider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Arrays;
2020
import java.util.Calendar;
2121
import java.util.Date;
22-
import java.util.Objects;
2322

2423
/**
2524
* This annotation is used to mark Date parameter that can accept partial dates as input.

registry-ws/src/main/java/org/gbif/registry/ws/resources/OccurrenceDownloadResource.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public PagingResponse<DatasetOccurrenceDownloadUsage> listDatasetUsages(@PathPar
173173
}
174174

175175
@GET
176-
@Path("monthlystats")
176+
@Path("stats")
177177
@Override
178178
@NullToNotFound
179179
public Map<Integer,Map<Integer,Long>> getMonthlyStats(@Nullable @QueryParam("fromDate") @PartialDate Date fromDate,
@@ -182,6 +182,16 @@ public Map<Integer,Map<Integer,Long>> getMonthlyStats(@Nullable @QueryParam("fro
182182
return groupByYear(occurrenceDownloadMapper.getMonthlyStats(fromDate, toDate, Optional.ofNullable(country).map(Country::getIso2LetterCode).orElse(null)));
183183
}
184184

185+
@GET
186+
@Path("stats/downloadedRecords")
187+
@Override
188+
@NullToNotFound
189+
public Map<Integer, Map<Integer, Long>> getDownloadRecordsHostedByCountry(@Nullable @QueryParam("fromDate") @PartialDate Date fromDate,
190+
@Nullable @QueryParam("toDate") @PartialDate Date toDate,
191+
@Context Country country) {
192+
return groupByYear(occurrenceDownloadMapper.getDownloadedRecordsByCountry(fromDate, toDate, Optional.ofNullable(country).map(Country::getIso2LetterCode).orElse(null)));
193+
}
194+
185195
/**
186196
* Aggregates the download statistics in tree structure of month grouped by year.
187197
*/

registry-ws/src/main/resources/org/gbif/registry/persistence/mapper/OccurrenceDownloadMapper.xml

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,41 @@
163163
JOIN "user" u ON oc.created_by = u.username AND u.settings -> 'country' = #{country,jdbcType=OTHER}
164164
]]>
165165
</if>
166-
<where>
167-
<if test="fromDate != null" >
168-
<![CDATA[
169-
AND oc.created >= #{fromDate,jdbcType=TIMESTAMP}
170-
]]>
171-
</if>
172-
<if test="toDate != null" >
173-
<![CDATA[
174-
AND oc.created <= #{toDate,jdbcType=TIMESTAMP}
175-
]]>
176-
</if>
177-
</where>
166+
WHERE oc.status = 'SUCCEEDED'
167+
<if test="fromDate != null" >
168+
<![CDATA[
169+
AND oc.created >= #{fromDate,jdbcType=TIMESTAMP}
170+
]]>
171+
</if>
172+
<if test="toDate != null" >
173+
<![CDATA[
174+
AND oc.created <= #{toDate,jdbcType=TIMESTAMP}
175+
]]>
176+
</if>
177+
GROUP BY year_month
178+
ORDER BY year_month DESC;
179+
</select>
180+
181+
182+
<select id="getDownloadedRecordsByCountry" resultType="Count" resultMap="DOWNLOADS_STATS_MAP" parameterType="map">
183+
SELECT to_char(oc.created,'YYYY-MM') AS year_month, SUM(dod.number_records) AS count
184+
FROM dataset_occurrence_download dod
185+
JOIN occurrence_download oc ON oc.key = dod.download_key
186+
<if test="country != null">
187+
JOIN dataset d ON d.key = dod.dataset_key
188+
JOIN organization o ON d.publishing_organization_key = o.key AND o.country = #{country,jdbcType=OTHER}
189+
</if>
190+
WHERE oc.status = 'SUCCEEDED'
191+
<if test="fromDate != null" >
192+
<![CDATA[
193+
AND oc.created >= #{fromDate,jdbcType=TIMESTAMP}
194+
]]>
195+
</if>
196+
<if test="toDate != null" >
197+
<![CDATA[
198+
AND oc.created <= #{toDate,jdbcType=TIMESTAMP}
199+
]]>
200+
</if>
178201
GROUP BY year_month
179202
ORDER BY year_month DESC;
180203
</select>

0 commit comments

Comments
 (0)