package eu.dnetlib.repo.manager.controllers; import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import eu.dnetlib.repo.manager.service.BrokerService; import eu.dnetlib.repo.manager.service.DashboardService; import eu.dnetlib.repo.manager.service.PiWikService; import eu.dnetlib.repo.manager.service.RepositoryService; import io.swagger.annotations.Api; import org.json.JSONException; import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping(value = "/dashboard") @Api(description = "Dashboard API", tags = {"dashboard"}) public class DashboardController { @Autowired private DashboardService dashboardService; @Autowired private RepositoryService repositoryService; @Autowired private BrokerService brokerService; @Autowired private PiWikService piWikService; @RequestMapping(value = "/getRepositoriesSummary/{page}/{size}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody @PreAuthorize("hasAuthority('REGISTERED_USER')") public List getRepositoriesSummaryInfo( @PathVariable("page") String page, @PathVariable("size") String size) throws JSONException { return dashboardService.getRepositoriesSummaryInfo(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(), page, size); } @RequestMapping(value = "/collectionMonitorSummary/{repoId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody @PreAuthorize("hasAuthority('REGISTERED_USER')") public CollectionMonitorSummary getCollectionMonitorSummary( @PathVariable("repoId") String repoId, @RequestParam(name = "size", required = false, defaultValue = "20") int size) throws JSONException { List aggregationDetails = repositoryService.getRepositoryAggregations(repoId, 0, size); CollectionMonitorSummary collectionMonitorSummary = new CollectionMonitorSummary(); collectionMonitorSummary.setAggregationDetails(aggregationDetails); size = 0; do { aggregationDetails = repositoryService.getRepositoryAggregations(repoId, size, size + 50); for (AggregationDetails aggregationDetail : aggregationDetails) { if (aggregationDetail.getIndexedVersion()) { collectionMonitorSummary.setLastIndexedVersion(aggregationDetail); break; } } size += 30; } while (aggregationDetails.size() != 0 && collectionMonitorSummary.getLastIndexedVersion() == null); return collectionMonitorSummary; } @RequestMapping(value = "/usageSummary/{repoId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody @PreAuthorize("hasAuthority('REGISTERED_USER')") public UsageSummary getUsageSummary( @PathVariable("repoId") String repoId ) throws RepositoryServiceException { return new UsageSummary(repositoryService.getMetricsInfoForRepository(repoId), piWikService.getPiwikSiteForRepo(repoId)); } @RequestMapping(value = "/brokerSummary/{ds_name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody @PreAuthorize("hasAuthority('REGISTERED_USER')") public BrokerSummary getBrokerSummary( @PathVariable("ds_name") String datasourceName) throws BrokerException { return new BrokerSummary(brokerService.getSimpleSubscriptionsOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail()), brokerService.getTopicsForDatasource(datasourceName)); } }