001package org.apache.archiva.rest.services; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet; 022import org.apache.archiva.metadata.repository.MetadataRepository; 023import org.apache.archiva.metadata.repository.MetadataRepositoryException; 024import org.apache.archiva.metadata.repository.RepositorySession; 025import org.apache.archiva.metadata.repository.stats.RepositoryStatistics; 026import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager; 027import org.apache.archiva.rest.api.services.ArchivaRestServiceException; 028import org.apache.archiva.rest.api.services.ReportRepositoriesService; 029import org.apache.commons.lang.StringUtils; 030import org.springframework.stereotype.Service; 031 032import javax.inject.Inject; 033import java.util.ArrayList; 034import java.util.Collections; 035import java.util.Date; 036import java.util.List; 037 038/** 039 * DefaultReportRepositoriesService 040 * 041 * @author Adrien Lecharpentier <adrien.lecharpentier@zenika.com> 042 * @since 1.4-M3 043 */ 044@Service( "reportRepositoriesService#rest" ) 045public class DefaultReportRepositoriesService 046 extends AbstractRestService 047 implements ReportRepositoriesService 048{ 049 050 private static final String ALL_REPOSITORIES = "all"; 051 052 @Inject 053 private RepositoryStatisticsManager repositoryStatisticsManager; 054 055 @Override 056 public List<RepositoryStatistics> getStatisticsReport( List<String> repositoriesId, int rowCount, Date startDate, 057 Date endDate ) 058 throws ArchivaRestServiceException 059 { 060 switch ( repositoriesId.size() ) 061 { 062 case 0: 063 throw new ArchivaRestServiceException( "report.statistics.report.missing-repositories", null ); 064 case 1: 065 return getUniqueRepositoryReport( repositoriesId.get( 0 ), rowCount, startDate, endDate ); 066 default: 067 return getMultipleRepositoriesReport( repositoriesId, rowCount ); 068 } 069 } 070 071 private List<RepositoryStatistics> getMultipleRepositoriesReport( List<String> repositoriesId, int rowCount ) 072 { 073 RepositorySession repositorySession = repositorySessionFactory.createSession(); 074 try 075 { 076 MetadataRepository metadataRepository = repositorySession.getRepository(); 077 List<RepositoryStatistics> stats = new ArrayList<>(); 078 for ( String repo : repositoriesId ) 079 { 080 try 081 { 082 stats.add( repositoryStatisticsManager.getLastStatistics( metadataRepository, repo ) ); 083 } 084 catch ( MetadataRepositoryException e ) 085 { 086 log.warn( "Unable to retrieve stats, assuming is empty: {}", e.getMessage(), e ); 087 } 088 } 089 090 return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() ); 091 } 092 finally 093 { 094 repositorySession.close(); 095 } 096 } 097 098 private List<RepositoryStatistics> getUniqueRepositoryReport( String repositoryId, int rowCount, Date startDate, 099 Date endDate ) 100 { 101 RepositorySession repositorySession = repositorySessionFactory.createSession(); 102 try 103 { 104 MetadataRepository metadataRepository = repositorySession.getRepository(); 105 List<RepositoryStatistics> stats = null; 106 try 107 { 108 stats = repositoryStatisticsManager.getStatisticsInRange( metadataRepository, repositoryId, startDate, 109 endDate ); 110 } 111 catch ( MetadataRepositoryException e ) 112 { 113 log.warn( "Unable to retrieve stats, assuming is empty: {}", e.getMessage(), e ); 114 } 115 if ( stats == null || stats.isEmpty() ) 116 { 117 return Collections.<RepositoryStatistics>emptyList(); 118 } 119 120 return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() ); 121 } 122 finally 123 { 124 repositorySession.close(); 125 } 126 } 127 128 @Override 129 public List<RepositoryProblemFacet> getHealthReport( String repository, String groupId, int rowCount ) 130 throws ArchivaRestServiceException 131 { 132 RepositorySession repositorySession = repositorySessionFactory.createSession(); 133 try 134 { 135 List<String> observableRepositories = getObservableRepos(); 136 if ( !ALL_REPOSITORIES.equals( repository ) && !observableRepositories.contains( repository ) ) 137 { 138 throw new ArchivaRestServiceException( 139 "${$.i18n.prop('report.repository.illegal-access', " + repository + ")}", "repositoryId", 140 new IllegalAccessException() ); 141 } 142 143 if ( !ALL_REPOSITORIES.equals( repository ) ) 144 { 145 observableRepositories = Collections.singletonList( repository ); 146 } 147 148 List<RepositoryProblemFacet> problemArtifacts = new ArrayList<>(); 149 MetadataRepository metadataRepository = repositorySession.getRepository(); 150 for ( String repoId : observableRepositories ) 151 { 152 for ( String name : metadataRepository.getMetadataFacets( repoId, RepositoryProblemFacet.FACET_ID ) ) 153 { 154 RepositoryProblemFacet metadataFacet = 155 (RepositoryProblemFacet) metadataRepository.getMetadataFacet( repoId, 156 RepositoryProblemFacet.FACET_ID, 157 name ); 158 if ( StringUtils.isEmpty( groupId ) || groupId.equals( metadataFacet.getNamespace() ) ) 159 { 160 problemArtifacts.add( metadataFacet ); 161 } 162 } 163 } 164 165 return problemArtifacts; 166 } 167 catch ( MetadataRepositoryException e ) 168 { 169 throw new ArchivaRestServiceException( e.getMessage(), e ); 170 } 171 finally 172 { 173 repositorySession.close(); 174 } 175 } 176}