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 );
50 private MetadataRepository metadataRepository;
52 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
54 public RepositoryStatistics getLastStatistics( String repositoryId )
55 throws MetadataRepositoryException
57 // TODO: consider a more efficient implementation that directly gets the last one from the content repository
58 List<String> scans = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
59 Collections.sort( scans );
60 if ( !scans.isEmpty() )
62 String name = scans.get( scans.size() - 1 );
63 return (RepositoryStatistics) metadataRepository.getMetadataFacet( repositoryId,
64 RepositoryStatistics.FACET_ID, name );
72 private void walkRepository( RepositoryStatistics stats, String repositoryId, String ns )
73 throws MetadataResolutionException
75 for ( String namespace : metadataRepository.getNamespaces( repositoryId, ns ) )
77 walkRepository( stats, repositoryId, ns + "." + namespace );
80 Collection<String> projects = metadataRepository.getProjects( repositoryId, ns );
81 if ( !projects.isEmpty() )
83 stats.setTotalGroupCount( stats.getTotalGroupCount() + 1 );
84 stats.setTotalProjectCount( stats.getTotalProjectCount() + projects.size() );
86 for ( String project : projects )
88 for ( String version : metadataRepository.getProjectVersions( repositoryId, ns, project ) )
90 for ( ArtifactMetadata artifact : metadataRepository.getArtifacts( repositoryId, ns, project,
93 stats.setTotalArtifactCount( stats.getTotalArtifactCount() + 1 );
94 stats.setTotalArtifactFileSize( stats.getTotalArtifactFileSize() + artifact.getSize() );
96 MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet(
97 MavenArtifactFacet.FACET_ID );
100 String type = facet.getType();
101 stats.setTotalCountForType( type, stats.getTotalCountForType( type ) + 1 );
110 public void addStatisticsAfterScan( String repositoryId, Date startTime, Date endTime, long totalFiles,
112 throws MetadataRepositoryException
114 RepositoryStatistics repositoryStatistics = new RepositoryStatistics();
115 repositoryStatistics.setScanStartTime( startTime );
116 repositoryStatistics.setScanEndTime( endTime );
117 repositoryStatistics.setTotalFileCount( totalFiles );
118 repositoryStatistics.setNewFileCount( newFiles );
120 // In the future, instead of being tied to a scan we might want to record information in the fly based on
121 // events that are occurring. Even without these totals we could query much of the information on demand based
122 // on information from the metadata content repository. In the mean time, we lock information in at scan time.
123 // Note that if new types are later discoverable due to a code change or new plugin, historical stats will not
124 // be updated and the repository will need to be rescanned.
126 long startWalk = System.currentTimeMillis();
127 // TODO: we can probably get a more efficient implementation directly from the metadata repository, but for now
128 // we just walk it. Alternatively, we could build an index, or store the aggregate information and update
132 for ( String ns : metadataRepository.getRootNamespaces( repositoryId ) )
134 walkRepository( repositoryStatistics, repositoryId, ns );
137 catch ( MetadataResolutionException e )
139 throw new MetadataRepositoryException( e.getMessage(), e );
141 log.info( "Repository walk for statistics executed in " + ( System.currentTimeMillis() - startWalk ) + "ms" );
143 metadataRepository.addMetadataFacet( repositoryId, repositoryStatistics );
146 public void deleteStatistics( String repositoryId )
147 throws MetadataRepositoryException
149 metadataRepository.removeMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
152 public List<RepositoryStatistics> getStatisticsInRange( String repositoryId, Date startTime, Date endTime )
153 throws MetadataRepositoryException
155 List<RepositoryStatistics> results = new ArrayList<RepositoryStatistics>();
156 List<String> list = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
157 Collections.sort( list, Collections.reverseOrder() );
158 for ( String name : list )
162 Date date = createNameFormat().parse( name );
163 if ( ( startTime == null || !date.before( startTime ) ) && ( endTime == null || !date.after(
166 RepositoryStatistics stats = (RepositoryStatistics) metadataRepository.getMetadataFacet(
167 repositoryId, RepositoryStatistics.FACET_ID, name );
168 results.add( stats );
171 catch ( ParseException e )
173 log.error( "Invalid scan result found in the metadata repository: " + e.getMessage() );
174 // continue and ignore this one
180 private static SimpleDateFormat createNameFormat()
182 SimpleDateFormat fmt = new SimpleDateFormat( RepositoryStatistics.SCAN_TIMESTAMP_FORMAT );
183 fmt.setTimeZone( UTC_TIME_ZONE );
187 public void setMetadataRepository( MetadataRepository metadataRepository )
189 this.metadataRepository = metadataRepository;