mirror of
https://github.com/HangarMC/Hangar.git
synced 2025-02-17 15:01:42 +08:00
fix: version stats endpoint (closes #1140)
This commit is contained in:
parent
683110338d
commit
914f548386
@ -195,17 +195,22 @@ public interface VersionsApiDAO {
|
||||
|
||||
@KeyColumn("date")
|
||||
@RegisterConstructorMapper(value = VersionStats.class, prefix = "vs")
|
||||
@RegisterColumnMapper(VersionStatsMapper.class)
|
||||
@SqlQuery("""
|
||||
SELECT cast(dates.day AS date) date,
|
||||
coalesce(pvd.downloads, 0) vs_totalDownloads
|
||||
FROM projects p,
|
||||
project_versions pv,
|
||||
(SELECT generate_series(:fromDate::date, :toDate::date, INTERVAL '1 DAY') AS day) dates
|
||||
LEFT JOIN project_versions_downloads pvd ON dates.day = pvd.day
|
||||
WHERE lower(p.owner_name) = lower(:author)
|
||||
AND lower(p.slug) = lower(:slug)
|
||||
AND pv.version_string = :versionString
|
||||
AND (pvd IS NULL OR (pvd.project_id = p.id AND pvd.version_id = pv.id));
|
||||
""")
|
||||
SELECT date, sum(platform_downloads) vs_totalDownloads, array_agg((ARRAY[subquery.platform, subquery.platform_downloads])) AS vs_platformDownloads
|
||||
FROM (SELECT cast(dates.day AS date) date,
|
||||
pvd.platform platform,
|
||||
coalesce(sum(pvd.downloads), 0) AS platform_downloads
|
||||
FROM projects p,
|
||||
project_versions pv,
|
||||
(SELECT generate_series(:fromDate::date, :toDate::date, INTERVAL '1 DAY') AS day) dates
|
||||
LEFT JOIN project_versions_downloads pvd ON dates.day = pvd.day
|
||||
WHERE lower(p.owner_name) = lower(:author)
|
||||
AND lower(p.slug) = lower(:slug)
|
||||
AND pv.version_string = :versionString
|
||||
AND (pvd IS NULL OR (pvd.project_id = p.id AND pvd.version_id = pv.id))
|
||||
GROUP BY date, platform) subquery
|
||||
GROUP BY date;
|
||||
""")
|
||||
Map<String, VersionStats> getVersionStats(String author, String slug, String versionString, OffsetDateTime fromDate, OffsetDateTime toDate);
|
||||
}
|
||||
|
@ -22,16 +22,23 @@ public class VersionStatsMapper implements ColumnMapper<Map<Platform, Long>> {
|
||||
|
||||
final Object[] array = (Object[]) arr.getArray();
|
||||
for (final Object entry : array) {
|
||||
final PGobject pgObject = (PGobject) entry;
|
||||
if (pgObject.getValue() == null) {
|
||||
continue;
|
||||
}
|
||||
if (entry instanceof final PGobject pgObject) {
|
||||
if (pgObject.getValue() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final String val = pgObject.getValue().substring(1, pgObject.getValue().length() - 1);
|
||||
final String[] split = val.split(",");
|
||||
final int platformIndex = Integer.parseInt(split[0]);
|
||||
final long downloads = Long.parseLong(split[1]);
|
||||
result.put(Platform.getValues()[platformIndex], downloads);
|
||||
final String val = pgObject.getValue().substring(1, pgObject.getValue().length() - 1);
|
||||
final String[] split = val.split(",");
|
||||
final int platformIndex = Integer.parseInt(split[0]);
|
||||
final long downloads = Long.parseLong(split[1]);
|
||||
result.put(Platform.getValues()[platformIndex], downloads);
|
||||
} else if (entry instanceof final Long[] longArr){
|
||||
if (longArr.length < 2 || longArr[0] == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.put(Platform.getValues()[longArr[0].intValue()], longArr[1]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user