This project has retired. For details please refer to its
Attic page.
DefaultRepositoryStatisticsManager xref
1 package org.apache.archiva.metadata.repository.stats;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26 import org.apache.archiva.metadata.repository.MetadataResolutionException;
27 import org.apache.commons.lang.time.StopWatch;
28 import org.apache.jackrabbit.commons.JcrUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
32
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.TimeZone;
43 import javax.jcr.Node;
44 import javax.jcr.RepositoryException;
45 import javax.jcr.Session;
46 import javax.jcr.query.Query;
47 import javax.jcr.query.QueryManager;
48 import javax.jcr.query.QueryResult;
49 import javax.jcr.query.Row;
50
51
52
53
54 @Service("repositoryStatisticsManager#default")
55 public class DefaultRepositoryStatisticsManager
56 implements RepositoryStatisticsManager
57 {
58 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryStatisticsManager.class );
59
60 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
61
62 @Override
63 public boolean hasStatistics( MetadataRepository metadataRepository, String repositoryId )
64 throws MetadataRepositoryException
65 {
66 return metadataRepository.hasMetadataFacet( repositoryId, RepositoryStatistics.FACET_ID );
67 }
68
69 @Override
70 public RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
71 throws MetadataRepositoryException
72 {
73 StopWatch stopWatch = new StopWatch();
74 stopWatch.start();
75
76 List<String> scans = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
77 if ( scans == null )
78 {
79 return null;
80 }
81 Collections.sort( scans );
82 if ( !scans.isEmpty() )
83 {
84 String name = scans.get( scans.size() - 1 );
85 RepositoryStatistics repositoryStatistics =
86 RepositoryStatistics.class.cast( metadataRepository.getMetadataFacet( repositoryId, RepositoryStatistics.FACET_ID,
87 name ));
88 stopWatch.stop();
89 log.debug( "time to find last RepositoryStatistics: {} ms", stopWatch.getTime() );
90 return repositoryStatistics;
91 }
92 else
93 {
94 return null;
95 }
96 }
97
98 private void walkRepository( MetadataRepository metadataRepository, RepositoryStatistics stats, String repositoryId,
99 String ns )
100 throws MetadataResolutionException
101 {
102 for ( String namespace : metadataRepository.getNamespaces( repositoryId, ns ) )
103 {
104 walkRepository( metadataRepository, stats, repositoryId, ns + "." + namespace );
105 }
106
107 Collection<String> projects = metadataRepository.getProjects( repositoryId, ns );
108 if ( !projects.isEmpty() )
109 {
110 stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
111 stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
112
113 for ( String project : projects )
114 {
115 for ( String version : metadataRepository.getProjectVersions( repositoryId, ns, project ) )
116 {
117 for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositoryId, ns, project,
118 version ) )
119 {
120 stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
121 stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
122
123 MavenArtifactFacet facet =
124 (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
125 if ( facet != null )
126 {
127 String type = facet.getType();
128 stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );
129 }
130 }
131 }
132 }
133 }
134 }
135
136 @Override
137 public void addStatisticsAfterScan( MetadataRepository metadataRepository, String repositoryId, Date startTime,
138 Date endTime, long totalFiles, long newFiles )
139 throws MetadataRepositoryException
140 {
141 RepositoryStatistics repositoryStatistics = new RepositoryStatistics();
142 repositoryStatistics.setRepositoryId( repositoryId );
143 repositoryStatistics.setScanStartTime( startTime );
144 repositoryStatistics.setScanEndTime( endTime );
145 repositoryStatistics.setTotalFileCount( totalFiles );
146 repositoryStatistics.setNewFileCount( newFiles );
147
148
149
150
151
152
153
154
155 long startGather = System.currentTimeMillis();
156
157
158
159 if ( metadataRepository.canObtainAccess( Session.class ) )
160 {
161
162
163 populateStatisticsFromJcr( (Session) metadataRepository.obtainAccess( Session.class ), repositoryId,
164 repositoryStatistics );
165 }
166 else
167 {
168
169
170
171
172
173 populateStatisticsFromRepositoryWalk( metadataRepository, repositoryId, repositoryStatistics );
174 }
175
176 log.info( "Gathering statistics executed in {} ms", ( System.currentTimeMillis() - startGather ) );
177
178 metadataRepository.addMetadataFacet( repositoryId, repositoryStatistics );
179 }
180
181 private void populateStatisticsFromJcr( Session session, String repositoryId,
182 RepositoryStatistics repositoryStatistics )
183 throws MetadataRepositoryException
184 {
185
186
187 try
188 {
189 QueryManager queryManager = session.getWorkspace().getQueryManager();
190
191
192
193
194
195
196
197 String whereClause = "WHERE jcr:path LIKE '/repositories/" + repositoryId + "/content/%'";
198 Query query = queryManager.createQuery( "SELECT size FROM archiva:artifact " + whereClause, Query.SQL );
199
200 QueryResult queryResult = query.execute();
201
202 Map<String, Integer> totalByType = new HashMap<>();
203 long totalSize = 0, totalArtifacts = 0;
204 for ( Row row : JcrUtils.getRows( queryResult ) )
205 {
206 Node n = row.getNode();
207 totalSize += row.getValue( "size" ).getLong();
208
209 String type;
210 if ( n.hasNode( MavenArtifactFacet.FACET_ID ) )
211 {
212 Node facetNode = n.getNode( MavenArtifactFacet.FACET_ID );
213 type = facetNode.getProperty( "type" ).getString();
214 }
215 else
216 {
217 type = "Other";
218 }
219 Integer prev = totalByType.get( type );
220 totalByType.put( type, prev != null ? prev + 1 : 1 );
221
222 totalArtifacts++;
223 }
224
225 repositoryStatistics.setTotalArtifactCount( totalArtifacts );
226 repositoryStatistics.setTotalArtifactFileSize( totalSize );
227 for ( Map.Entry<String, Integer> entry : totalByType.entrySet() )
228 {
229 repositoryStatistics.setTotalCountForType( entry.getKey(), entry.getValue() );
230 }
231
232
233
234 query = queryManager.createQuery( "SELECT * FROM archiva:project " + whereClause + " ORDER BY jcr:score",
235 Query.SQL );
236 repositoryStatistics.setTotalProjectCount( query.execute().getRows().getSize() );
237
238
239
240 query = queryManager.createQuery(
241 "SELECT * FROM archiva:namespace " + whereClause + " AND namespace IS NOT NULL ORDER BY jcr:score",
242 Query.SQL );
243 repositoryStatistics.setTotalGroupCount( query.execute().getRows().getSize() );
244 }
245 catch ( RepositoryException e )
246 {
247 throw new MetadataRepositoryException( e.getMessage(), e );
248 }
249 }
250
251 private void populateStatisticsFromRepositoryWalk( MetadataRepository metadataRepository, String repositoryId,
252 RepositoryStatistics repositoryStatistics )
253 throws MetadataRepositoryException
254 {
255 try
256 {
257 for ( String ns : metadataRepository.getRootNamespaces( repositoryId ) )
258 {
259 walkRepository( metadataRepository, repositoryStatistics, repositoryId, ns );
260 }
261 }
262 catch ( MetadataResolutionException e )
263 {
264 throw new MetadataRepositoryException( e.getMessage(), e );
265 }
266 }
267
268 @Override
269 public void deleteStatistics( MetadataRepository metadataRepository, String repositoryId )
270 throws MetadataRepositoryException
271 {
272 metadataRepository.removeMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
273 }
274
275 @Override
276 public List<RepositoryStatistics> getStatisticsInRange( MetadataRepository metadataRepository, String repositoryId,
277 Date startTime, Date endTime )
278 throws MetadataRepositoryException
279 {
280 List<RepositoryStatistics> results = new ArrayList<>();
281 List<String> list = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
282 Collections.sort( list, Collections.reverseOrder() );
283 for ( String name : list )
284 {
285 try
286 {
287 Date date = createNameFormat().parse( name );
288 if ( ( startTime == null || !date.before( startTime ) ) && ( endTime == null || !date.after(
289 endTime ) ) )
290 {
291 RepositoryStatistics stats =
292 (RepositoryStatistics) metadataRepository.getMetadataFacet( repositoryId,
293 RepositoryStatistics.FACET_ID,
294 name );
295 results.add( stats );
296 }
297 }
298 catch ( ParseException e )
299 {
300 log.error( "Invalid scan result found in the metadata repository: " + e.getMessage() );
301
302 }
303 }
304 return results;
305 }
306
307 private static SimpleDateFormat createNameFormat()
308 {
309 SimpleDateFormat fmt = new SimpleDateFormat( RepositoryStatistics.SCAN_TIMESTAMP_FORMAT );
310 fmt.setTimeZone( UTC_TIME_ZONE );
311 return fmt;
312 }
313 }