1 package org.apache.archiva.metadata.repository.stats;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.MetadataRepository;
24 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
25 import org.apache.archiva.metadata.repository.MetadataResolutionException;
26 import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import java.text.ParseException;
31 import java.text.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Date;
36 import java.util.List;
37 import java.util.TimeZone;
40 * @plexus.component role="org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager" role-hint="default"
42 public class DefaultRepositoryStatisticsManager
43 implements RepositoryStatisticsManager
45 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryStatisticsManager.class );
47 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
49 public RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
50 throws MetadataRepositoryException
52 // TODO: consider a more efficient implementation that directly gets the last one from the content repository
53 List<String> scans = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
54 Collections.sort( scans );
55 if ( !scans.isEmpty() )
57 String name = scans.get( scans.size() - 1 );
58 return (RepositoryStatistics) metadataRepository.getMetadataFacet( repositoryId,
59 RepositoryStatistics.FACET_ID, name );
67 private void walkRepository( MetadataRepository metadataRepository, RepositoryStatistics stats, String repositoryId,
69 throws MetadataResolutionException
71 for ( String namespace : metadataRepository.getNamespaces( repositoryId, ns ) )
73 walkRepository( metadataRepository, stats, repositoryId, ns + "." + namespace );
76 Collection<String> projects = metadataRepository.getProjects( repositoryId, ns );
77 if ( !projects.isEmpty() )
79 stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
80 stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
82 for ( String project : projects )
84 for ( String version : metadataRepository.getProjectVersions( repositoryId, ns, project ) )
86 for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositoryId, ns, project,
89 stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
90 stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
92 MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet(
93 MavenArtifactFacet.FACET_ID );
96 String type = facet.getType();
97 stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );
105 public void addStatisticsAfterScan( MetadataRepository metadataRepository, String repositoryId, Date startTime,
106 Date endTime, long totalFiles, long newFiles )
107 throws MetadataRepositoryException
109 RepositoryStatistics repositoryStatistics = new RepositoryStatistics();
110 repositoryStatistics.setScanStartTime( startTime );
111 repositoryStatistics.setScanEndTime( endTime );
112 repositoryStatistics.setTotalFileCount( totalFiles );
113 repositoryStatistics.setNewFileCount( newFiles );
115 // In the future, instead of being tied to a scan we might want to record information in the fly based on
116 // events that are occurring. Even without these totals we could query much of the information on demand based
117 // on information from the metadata content repository. In the mean time, we lock information in at scan time.
118 // Note that if new types are later discoverable due to a code change or new plugin, historical stats will not
119 // be updated and the repository will need to be rescanned.
121 long startWalk = System.currentTimeMillis();
122 // TODO: we can probably get a more efficient implementation directly from the metadata repository, but for now
123 // we just walk it. Alternatively, we could build an index, or store the aggregate information and update
127 for ( String ns : metadataRepository.getRootNamespaces( repositoryId ) )
129 walkRepository( metadataRepository, repositoryStatistics, repositoryId, ns );
132 catch ( MetadataResolutionException e )
134 throw new MetadataRepositoryException( e.getMessage(), e );
136 log.info( "Repository walk for statistics executed in " + ( System.currentTimeMillis() - startWalk ) + "ms" );
138 metadataRepository.addMetadataFacet( repositoryId, repositoryStatistics );
141 public void deleteStatistics( MetadataRepository metadataRepository, String repositoryId )
142 throws MetadataRepositoryException
144 metadataRepository.removeMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
147 public List<RepositoryStatistics> getStatisticsInRange( MetadataRepository metadataRepository, String repositoryId,
148 Date startTime, Date endTime )
149 throws MetadataRepositoryException
151 List<RepositoryStatistics> results = new ArrayList<RepositoryStatistics>();
152 List<String> list = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
153 Collections.sort( list, Collections.reverseOrder() );
154 for ( String name : list )
158 Date date = createNameFormat().parse( name );
159 if ( ( startTime == null || !date.before( startTime ) ) && ( endTime == null || !date.after(
162 RepositoryStatistics stats = (RepositoryStatistics) metadataRepository.getMetadataFacet(
163 repositoryId, RepositoryStatistics.FACET_ID, name );
164 results.add( stats );
167 catch ( ParseException e )
169 log.error( "Invalid scan result found in the metadata repository: " + e.getMessage() );
170 // continue and ignore this one
176 private static SimpleDateFormat createNameFormat()
178 SimpleDateFormat fmt = new SimpleDateFormat( RepositoryStatistics.SCAN_TIMESTAMP_FORMAT );
179 fmt.setTimeZone( UTC_TIME_ZONE );