Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publisher statistics #1070

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions server/src/main/java/org/eclipse/openvsx/UserAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jakarta.servlet.http.HttpServletRequest;
import org.eclipse.openvsx.eclipse.EclipseService;
import org.eclipse.openvsx.entities.NamespaceMembership;
import org.eclipse.openvsx.entities.PublisherStatistics;
import org.eclipse.openvsx.entities.UserData;
import org.eclipse.openvsx.json.*;
import org.eclipse.openvsx.repositories.RepositoryService;
Expand Down Expand Up @@ -366,4 +367,18 @@ public ResponseEntity<UserJson> signPublisherAgreement() {
}
}

@GetMapping(
path = "/user/statistics",
produces = MediaType.APPLICATION_JSON_VALUE
)
public List<PublisherStatisticsJson> getPublisherStatistics() {
var user = users.findLoggedInUser();
if (user == null) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN);
}

return repositories.findPublisherStatisticsByUser(user).stream()
.map(PublisherStatistics::toJson)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.openvsx.migration.HandlerJobRequest;
import org.eclipse.openvsx.repositories.RepositoryService;
import org.eclipse.openvsx.search.SearchUtilService;
import org.eclipse.openvsx.statistics.MonthlyStatisticsJobRequestHandler;
import org.eclipse.openvsx.storage.StorageUtilService;
import org.eclipse.openvsx.util.*;
import org.jobrunr.scheduling.JobRequestScheduler;
Expand Down Expand Up @@ -78,7 +79,7 @@ public AdminService(

@EventListener
public void applicationStarted(ApplicationStartedEvent event) {
var jobRequest = new HandlerJobRequest<>(MonthlyAdminStatisticsJobRequestHandler.class);
var jobRequest = new HandlerJobRequest<>(MonthlyStatisticsJobRequestHandler.class);
scheduler.scheduleRecurrently("MonthlyAdminStatistics", Cron.monthly(1, 0, 3), ZoneId.of("UTC"), jobRequest);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/** ******************************************************************************
* Copyright (c) 2024 Precies. Software OU and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* ****************************************************************************** */
package org.eclipse.openvsx.entities;

import jakarta.persistence.*;
import org.eclipse.openvsx.json.PublisherStatisticsJson;

import java.util.*;

@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "year", "month"})})
public class PublisherStatistics {

@Id
@GeneratedValue(generator = "publisherStatisticsSeq")
@SequenceGenerator(name = "publisherStatisticsSeq", sequenceName = "publisher_statistics_seq")
private long id;

@ManyToOne
@JoinColumn(name = "user_data")
private UserData user;

private int year;

private int month;

@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "extension_identifier")
@Column(name = "downloads")
private Map<String, Long> extensionDownloads;

@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "extension_identifier")
@Column(name = "downloads")
private Map<String, Long> extensionTotalDownloads;

public PublisherStatisticsJson toJson() {
var json = new PublisherStatisticsJson();
json.setYear(year);
json.setMonth(month);
json.setExtensionDownloads(extensionDownloads);
json.setExtensionTotalDownloads(extensionTotalDownloads);
return json;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public UserData getUser() {
return user;
}

public void setUser(UserData user) {
this.user = user;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public Map<String, Long> getExtensionDownloads() {
return extensionDownloads;
}

public void setExtensionDownloads(Map<String, Long> extensionDownloads) {
this.extensionDownloads = extensionDownloads;
}

public Map<String, Long> getExtensionTotalDownloads() {
return extensionTotalDownloads;
}

public void setExtensionTotalDownloads(Map<String, Long> extensionTotalDownloads) {
this.extensionTotalDownloads = extensionTotalDownloads;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/** ******************************************************************************
* Copyright (c) 2024 Precies. Software OU and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* ****************************************************************************** */
package org.eclipse.openvsx.json;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class PublisherStatisticsJson extends ResultJson {

public static PublisherStatisticsJson error(String message) {
var result = new PublisherStatisticsJson();
result.setError(message);
return result;
}

private int year;

private int month;

private Map<String, Long> extensionDownloads;

private Map<String, Long> extensionTotalDownloads;

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public Map<String, Long> getExtensionDownloads() {
return extensionDownloads;
}

public void setExtensionDownloads(Map<String, Long> extensionDownlaods) {
this.extensionDownloads = extensionDownlaods;
}

public Map<String, Long> getExtensionTotalDownloads() {
return extensionTotalDownloads;
}

public void setExtensionTotalDownloads(Map<String, Long> extensionTotalDownlaods) {
this.extensionTotalDownloads = extensionTotalDownlaods;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.eclipse.openvsx.entities.Extension;
import org.eclipse.openvsx.entities.Namespace;
import org.eclipse.openvsx.statistics.MembershipDownloadCount;
import org.eclipse.openvsx.util.ExtensionId;
import org.eclipse.openvsx.web.SitemapRow;
import org.jooq.Record;
Expand All @@ -24,8 +25,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import static org.eclipse.openvsx.jooq.Tables.EXTENSION;
import static org.eclipse.openvsx.jooq.Tables.NAMESPACE;
import static org.eclipse.openvsx.jooq.Tables.*;

@Component
public class ExtensionJooqRepository {
Expand Down Expand Up @@ -270,4 +270,40 @@ public boolean hasExtension(String namespace, String extension) {
.and(EXTENSION.NAME.equalIgnoreCase(extension))
);
}

public List<MembershipDownloadCount> findMembershipDownloads(int offset, int limit) {
return dsl.select(USER_DATA.ID, NAMESPACE.NAME, EXTENSION.NAME, EXTENSION.DOWNLOAD_COUNT)
.from(USER_DATA)
.join(NAMESPACE_MEMBERSHIP).on(NAMESPACE_MEMBERSHIP.USER_DATA.eq(USER_DATA.ID))
.join(NAMESPACE).on(NAMESPACE.ID.eq(NAMESPACE_MEMBERSHIP.NAMESPACE))
.join(EXTENSION).on(EXTENSION.NAMESPACE_ID.eq(NAMESPACE.ID))
.where(USER_DATA.PROVIDER.eq("github"))
.and(EXTENSION.ACTIVE.eq(true))
.orderBy(USER_DATA.ID, NAMESPACE.NAME, EXTENSION.NAME)
.offset(offset)
.limit(limit)
.fetch(row -> new MembershipDownloadCount(
row.get(USER_DATA.ID),
row.get(NAMESPACE.NAME),
row.get(EXTENSION.NAME),
row.get(EXTENSION.DOWNLOAD_COUNT))
);
}

public List<MembershipDownloadCount> findMembershipDownloads(String loginName) {
return dsl.select(USER_DATA.ID, NAMESPACE.NAME, EXTENSION.NAME, EXTENSION.DOWNLOAD_COUNT)
.from(USER_DATA)
.join(NAMESPACE_MEMBERSHIP).on(NAMESPACE_MEMBERSHIP.USER_DATA.eq(USER_DATA.ID))
.join(NAMESPACE).on(NAMESPACE.ID.eq(NAMESPACE_MEMBERSHIP.NAMESPACE))
.join(EXTENSION).on(EXTENSION.NAMESPACE_ID.eq(NAMESPACE.ID))
.where(USER_DATA.PROVIDER.eq("github"))
.and(USER_DATA.LOGIN_NAME.eq(loginName))
.and(EXTENSION.ACTIVE.eq(true))
.fetch(row -> new MembershipDownloadCount(
row.get(USER_DATA.ID),
row.get(NAMESPACE.NAME),
row.get(EXTENSION.NAME),
row.get(EXTENSION.DOWNLOAD_COUNT))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** ******************************************************************************
* Copyright (c) 2024 Precies. Software OU and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
* ****************************************************************************** */
package org.eclipse.openvsx.repositories;

import org.eclipse.openvsx.entities.PublisherStatistics;
import org.eclipse.openvsx.entities.UserData;
import org.springframework.data.repository.Repository;
import org.springframework.data.util.Streamable;

public interface PublisherStatisticsRepository extends Repository<PublisherStatistics, Long> {

PublisherStatistics findByYearAndMonthAndUserId(int year, int month, long userId);

Streamable<PublisherStatistics> findByUser(UserData user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.json.QueryRequest;
import org.eclipse.openvsx.json.VersionTargetPlatformsJson;
import org.eclipse.openvsx.statistics.MembershipDownloadCount;
import org.eclipse.openvsx.util.ExtensionId;
import org.eclipse.openvsx.util.NamingUtil;
import org.eclipse.openvsx.web.SitemapRow;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class RepositoryService {
private final MigrationItemRepository migrationItemRepo;
private final SignatureKeyPairRepository signatureKeyPairRepo;
private final SignatureKeyPairJooqRepository signatureKeyPairJooqRepo;
private final PublisherStatisticsRepository publisherStatisticsRepo;

public RepositoryService(
NamespaceRepository namespaceRepo,
Expand All @@ -81,7 +83,8 @@ public RepositoryService(
AdminStatisticCalculationsRepository adminStatisticCalculationsRepo,
MigrationItemRepository migrationItemRepo,
SignatureKeyPairRepository signatureKeyPairRepo,
SignatureKeyPairJooqRepository signatureKeyPairJooqRepo
SignatureKeyPairJooqRepository signatureKeyPairJooqRepo,
PublisherStatisticsRepository publisherStatisticsRepo
) {
this.namespaceRepo = namespaceRepo;
this.namespaceJooqRepo = namespaceJooqRepo;
Expand All @@ -105,6 +108,7 @@ public RepositoryService(
this.migrationItemRepo = migrationItemRepo;
this.signatureKeyPairRepo = signatureKeyPairRepo;
this.signatureKeyPairJooqRepo = signatureKeyPairJooqRepo;
this.publisherStatisticsRepo = publisherStatisticsRepo;
}

public Namespace findNamespace(String name) {
Expand Down Expand Up @@ -667,4 +671,20 @@ public boolean hasExtension(String namespace, String extension) {
public Streamable<Extension> findDeprecatedExtensions(Extension replacement) {
return extensionRepo.findByReplacement(replacement);
}

public PublisherStatistics findPublisherStatisticsByYearAndMonthAndUserId(int year, int month, long userId) {
return publisherStatisticsRepo.findByYearAndMonthAndUserId(year, month, userId);
}

public Streamable<PublisherStatistics> findPublisherStatisticsByUser(UserData user) {
return publisherStatisticsRepo.findByUser(user);
}

public List<MembershipDownloadCount> findMembershipDownloads(int offset, int limit) {
return extensionJooqRepo.findMembershipDownloads(offset, limit);
}

public List<MembershipDownloadCount> findMembershipDownloads(String loginName) {
return extensionJooqRepo.findMembershipDownloads(loginName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.data.util.Streamable;

public interface UserDataRepository extends Repository<UserData, Long> {

Expand All @@ -22,4 +23,5 @@ public interface UserDataRepository extends Repository<UserData, Long> {

long count();

Streamable<UserData> findByProvider(String provider);
}
Loading