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.model.RepositoryStatistics; 026import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager; 027import org.apache.archiva.rest.api.services.ArchivaRestServiceException; 028import org.apache.archiva.rest.api.services.ReportRepositoriesService; 029import org.apache.commons.lang3.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 = null; 074 try 075 { 076 repositorySession = repositorySessionFactory.createSession(); 077 } 078 catch ( MetadataRepositoryException e ) 079 { 080 e.printStackTrace( ); 081 } 082 try 083 { 084 MetadataRepository metadataRepository = repositorySession.getRepository(); 085 List<RepositoryStatistics> stats = new ArrayList<>(); 086 for ( String repo : repositoriesId ) 087 { 088 try 089 { 090 stats.add( repositoryStatisticsManager.getLastStatistics( repo ) ); 091 } 092 catch ( MetadataRepositoryException e ) 093 { 094 log.warn( "Unable to retrieve stats, assuming is empty: {}", e.getMessage(), e ); 095 } 096 } 097 098 return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() ); 099 } 100 finally 101 { 102 repositorySession.close(); 103 } 104 } 105 106 private List<RepositoryStatistics> getUniqueRepositoryReport( String repositoryId, int rowCount, Date startDate, 107 Date endDate ) 108 { 109 RepositorySession repositorySession = null; 110 try 111 { 112 repositorySession = repositorySessionFactory.createSession(); 113 } 114 catch ( MetadataRepositoryException e ) 115 { 116 e.printStackTrace( ); 117 } 118 try 119 { 120 MetadataRepository metadataRepository = repositorySession.getRepository(); 121 List<RepositoryStatistics> stats = null; 122 try 123 { 124 stats = repositoryStatisticsManager.getStatisticsInRange( repositoryId, startDate, 125 endDate ); 126 } 127 catch ( MetadataRepositoryException e ) 128 { 129 log.warn( "Unable to retrieve stats, assuming is empty: {}", e.getMessage(), e ); 130 } 131 if ( stats == null || stats.isEmpty() ) 132 { 133 return Collections.<RepositoryStatistics>emptyList(); 134 } 135 136 return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() ); 137 } 138 finally 139 { 140 repositorySession.close(); 141 } 142 } 143 144 @Override 145 public List<RepositoryProblemFacet> getHealthReport( String repository, String groupId, int rowCount ) 146 throws ArchivaRestServiceException 147 { 148 RepositorySession repositorySession = null; 149 try 150 { 151 repositorySession = repositorySessionFactory.createSession(); 152 } 153 catch ( MetadataRepositoryException e ) 154 { 155 e.printStackTrace( ); 156 } 157 try 158 { 159 List<String> observableRepositories = getObservableRepos(); 160 if ( !ALL_REPOSITORIES.equals( repository ) && !observableRepositories.contains( repository ) ) 161 { 162 throw new ArchivaRestServiceException( 163 "${$.i18n.prop('report.repository.illegal-access', " + repository + ")}", "repositoryId", 164 new IllegalAccessException() ); 165 } 166 167 if ( !ALL_REPOSITORIES.equals( repository ) ) 168 { 169 observableRepositories = Collections.singletonList( repository ); 170 } 171 172 List<RepositoryProblemFacet> problemArtifacts = new ArrayList<>(); 173 MetadataRepository metadataRepository = repositorySession.getRepository(); 174 for ( String repoId : observableRepositories ) 175 { 176 for ( String name : metadataRepository.getMetadataFacets(repositorySession , repoId, RepositoryProblemFacet.FACET_ID ) ) 177 { 178 RepositoryProblemFacet metadataFacet = 179 (RepositoryProblemFacet) metadataRepository.getMetadataFacet(repositorySession , 180 repoId, 181 RepositoryProblemFacet.FACET_ID, name ); 182 if ( StringUtils.isEmpty( groupId ) || groupId.equals( metadataFacet.getNamespace() ) ) 183 { 184 problemArtifacts.add( metadataFacet ); 185 } 186 } 187 } 188 189 return problemArtifacts; 190 } 191 catch ( MetadataRepositoryException e ) 192 { 193 throw new ArchivaRestServiceException( e.getMessage(), e ); 194 } 195 finally 196 { 197 repositorySession.close(); 198 } 199 } 200}