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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
108 changes: 108 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 @@ -340,4 +341,111 @@ public ResponseEntity<UserJson> signPublisherAgreement() {
}
}

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

// TODO mock response to develop dashboard
var octYaml = new PublisherStatisticsJson.ExtensionDownloads();
octYaml.setExtensionIdentifier("amvanbaren.yaml");
octYaml.setDownloads(123);

var octYamlTotal = new PublisherStatisticsJson.ExtensionDownloads();
octYamlTotal.setExtensionIdentifier("amvanbaren.yaml");
octYamlTotal.setDownloads(8943);

var octJava = new PublisherStatisticsJson.ExtensionDownloads();
octJava.setExtensionIdentifier("amvanbaren.java");
octJava.setDownloads(837);

var octJavaTotal = new PublisherStatisticsJson.ExtensionDownloads();
octJavaTotal.setExtensionIdentifier("amvanbaren.java");
octJavaTotal.setDownloads(2453);

var octTheme = new PublisherStatisticsJson.ExtensionDownloads();
octTheme.setExtensionIdentifier("amvanbaren.theme");
octTheme.setDownloads(345);

var octThemeTotal = new PublisherStatisticsJson.ExtensionDownloads();
octThemeTotal.setExtensionIdentifier("amvanbaren.theme");
octThemeTotal.setDownloads(9485);

var oct = new PublisherStatisticsJson();
oct.setMonth(10);
oct.setYear(2024);
oct.setExtensionDownloads(List.of(octYaml, octJava, octTheme));
oct.setExtensionTotalDownloads(List.of(octYamlTotal, octJavaTotal, octThemeTotal));

var novYaml = new PublisherStatisticsJson.ExtensionDownloads();
novYaml.setExtensionIdentifier("amvanbaren.yaml");
novYaml.setDownloads(3375);

var novYamlTotal = new PublisherStatisticsJson.ExtensionDownloads();
novYamlTotal.setExtensionIdentifier("amvanbaren.yaml");
novYamlTotal.setDownloads(12318);

var novJava = new PublisherStatisticsJson.ExtensionDownloads();
novJava.setExtensionIdentifier("amvanbaren.java");
novJava.setDownloads(1022);

var novJavaTotal = new PublisherStatisticsJson.ExtensionDownloads();
novJavaTotal.setExtensionIdentifier("amvanbaren.java");
novJavaTotal.setDownloads(3475);

var novTheme = new PublisherStatisticsJson.ExtensionDownloads();
novTheme.setExtensionIdentifier("amvanbaren.theme");
novTheme.setDownloads(10329);

var novThemeTotal = new PublisherStatisticsJson.ExtensionDownloads();
novThemeTotal.setExtensionIdentifier("amvanbaren.theme");
novThemeTotal.setDownloads(19814);

var nov = new PublisherStatisticsJson();
nov.setMonth(11);
nov.setYear(2024);
nov.setExtensionDownloads(List.of(novYaml, novJava, novTheme));
nov.setExtensionTotalDownloads(List.of(novYamlTotal, novJavaTotal, novThemeTotal));

var decYaml = new PublisherStatisticsJson.ExtensionDownloads();
decYaml.setExtensionIdentifier("amvanbaren.yaml");
decYaml.setDownloads(1971);

var decYamlTotal = new PublisherStatisticsJson.ExtensionDownloads();
decYamlTotal.setExtensionIdentifier("amvanbaren.yaml");
decYamlTotal.setDownloads(14289);

var decJava = new PublisherStatisticsJson.ExtensionDownloads();
decJava.setExtensionIdentifier("amvanbaren.java");
decJava.setDownloads(1788);

var decJavaTotal = new PublisherStatisticsJson.ExtensionDownloads();
decJavaTotal.setExtensionIdentifier("amvanbaren.java");
decJavaTotal.setDownloads(5263);

var decTheme = new PublisherStatisticsJson.ExtensionDownloads();
decTheme.setExtensionIdentifier("amvanbaren.theme");
decTheme.setDownloads(3384);

var decThemeTotal = new PublisherStatisticsJson.ExtensionDownloads();
decThemeTotal.setExtensionIdentifier("amvanbaren.theme");
decThemeTotal.setDownloads(23198);

var dec = new PublisherStatisticsJson();
dec.setMonth(12);
dec.setYear(2024);
dec.setExtensionDownloads(List.of(decYaml, decJava, decTheme));
dec.setExtensionTotalDownloads(List.of(decYamlTotal, decJavaTotal, decThemeTotal));

return List.of(oct, nov, dec);

// 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,114 @@
/** ******************************************************************************
* 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.*;
import java.util.stream.Collectors;

@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> extensionDownlaods;

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

public PublisherStatisticsJson toJson() {
var json = new PublisherStatisticsJson();
json.setYear(year);
json.setMonth(month);
json.setExtensionDownloads(toExtensionDownloads(extensionDownlaods));
json.setExtensionTotalDownloads(toExtensionDownloads(extensionTotalDownlaods));

return json;
}

private List<PublisherStatisticsJson.ExtensionDownloads> toExtensionDownloads(Map<String, Long> downloads) {
return downloads.entrySet().stream()
.map(entry -> {
var mapping = new PublisherStatisticsJson.ExtensionDownloads();
mapping.setExtensionIdentifier(entry.getKey());
mapping.setDownloads(entry.getValue());
return mapping;
})
.sorted(Comparator.comparingLong(PublisherStatisticsJson.ExtensionDownloads::getDownloads).reversed())
.collect(Collectors.toList());
}

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> getExtensionDownlaods() {
return extensionDownlaods;
}

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

public Map<String, Long> getExtensionTotalDownlaods() {
return extensionTotalDownlaods;
}

public void setExtensionTotalDownlaods(Map<String, Long> extensionTotalDownlaods) {
this.extensionTotalDownlaods = extensionTotalDownlaods;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/** ******************************************************************************
* 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.List;

@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 List<ExtensionDownloads> extensionDownloads;

private List<ExtensionDownloads> 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 List<ExtensionDownloads> getExtensionDownloads() {
return extensionDownloads;
}

public void setExtensionDownloads(List<ExtensionDownloads> extensionDownloads) {
this.extensionDownloads = extensionDownloads;
}

public List<ExtensionDownloads> getExtensionTotalDownloads() {
return extensionTotalDownloads;
}

public void setExtensionTotalDownloads(List<ExtensionDownloads> extensionTotalDownloads) {
this.extensionTotalDownloads = extensionTotalDownloads;
}

public static class ExtensionDownloads {
private String extensionIdentifier;

private long downloads;

public String getExtensionIdentifier() {
return extensionIdentifier;
}

public void setExtensionIdentifier(String extensionIdentifier) {
this.extensionIdentifier = extensionIdentifier;
}

public long getDownloads() {
return downloads;
}

public void setDownloads(long downloads) {
this.downloads = downloads;
}
}
}
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 findByYearAndMonthAndUser(int year, int month, UserData user);

Streamable<PublisherStatistics> findByUser(UserData user);
}
Loading
Loading