This project has retired. For details please refer to its
Attic page.
DefaultReportRepositoriesService xref
1 package org.apache.archiva.rest.services;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
22 import org.apache.archiva.metadata.repository.MetadataRepository;
23 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
27 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
28 import org.apache.archiva.rest.api.services.ReportRepositoriesService;
29 import org.apache.commons.lang.StringUtils;
30 import org.springframework.stereotype.Service;
31
32 import javax.inject.Inject;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.List;
37
38
39
40
41
42
43
44 @Service( "reportRepositoriesService#rest" )
45 public class DefaultReportRepositoriesService
46 extends AbstractRestService
47 implements ReportRepositoriesService
48 {
49
50 private static final String ALL_REPOSITORIES = "all";
51
52 @Inject
53 private RepositoryStatisticsManager repositoryStatisticsManager;
54
55 @Override
56 public List<RepositoryStatistics> getStatisticsReport( List<String> repositoriesId, int rowCount, Date startDate,
57 Date endDate )
58 throws ArchivaRestServiceException
59 {
60 switch ( repositoriesId.size() )
61 {
62 case 0:
63 throw new ArchivaRestServiceException( "report.statistics.report.missing-repositories", null );
64 case 1:
65 return getUniqueRepositoryReport( repositoriesId.get( 0 ), rowCount, startDate, endDate );
66 default:
67 return getMultipleRepositoriesReport( repositoriesId, rowCount );
68 }
69 }
70
71 private List<RepositoryStatistics> getMultipleRepositoriesReport( List<String> repositoriesId, int rowCount )
72 {
73 RepositorySession repositorySession = repositorySessionFactory.createSession();
74 try
75 {
76 MetadataRepository metadataRepository = repositorySession.getRepository();
77 List<RepositoryStatistics> stats = new ArrayList<>();
78 for ( String repo : repositoriesId )
79 {
80 try
81 {
82 stats.add( repositoryStatisticsManager.getLastStatistics( metadataRepository, repo ) );
83 }
84 catch ( MetadataRepositoryException e )
85 {
86 log.warn( "Unable to retrieve stats, assuming is empty: {}", e.getMessage(), e );
87 }
88 }
89
90 return stats.subList( 0, stats.size() > rowCount ? rowCount : stats.size() );
91 }
92 finally
93 {
94 repositorySession.close();
95 }
96 }
97
98 private List<RepositoryStatistics> getUniqueRepositoryReport( String repositoryId, int rowCount, Date startDate,
99 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 }