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 java.text.ParseException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
28 import org.apache.archiva.metadata.repository.MetadataRepository;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * @plexus.component role="org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager" role-hint="default"
35 public class DefaultRepositoryStatisticsManager
36 implements RepositoryStatisticsManager
38 private static final Logger log = LoggerFactory.getLogger( DefaultRepositoryStatisticsManager.class );
43 private MetadataRepository metadataRepository;
45 public RepositoryStatistics getLastStatistics( String repositoryId )
47 // TODO: consider a more efficient implementation that directly gets the last one from the content repository
48 List<String> scans = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
49 Collections.sort( scans );
50 if ( !scans.isEmpty() )
52 String name = scans.get( scans.size() - 1 );
53 return (RepositoryStatistics) metadataRepository.getMetadataFacet( repositoryId,
54 RepositoryStatistics.FACET_ID, name );
62 public void addStatisticsAfterScan( String repositoryId, RepositoryStatistics repositoryStatistics )
64 // In the future, instead of being tied to a scan we might want to record information in the fly based on
65 // events that are occurring. Even without these totals we could query much of the information on demand based
66 // on information from the metadata content repository. In the mean time, we lock information in at scan time.
67 // Note that if new types are later discoverable due to a code change or new plugin, historical stats will not
68 // be updated and the repository will need to be rescanned.
70 // TODO, populate these and also a count per artifact type
71 // populate total artifact count from content repository
72 // repositoryStatistics.setTotalArtifactCount( );
73 // populate total size from content repository
74 // repositoryStatistics.setTotalArtifactFileSize( );
75 // populate total group count from content repository
76 // repositoryStatistics.setTotalGroupCount( );
77 // populate total project count from content repository
78 // repositoryStatistics.setTotalProjectCount( );
80 metadataRepository.addMetadataFacet( repositoryId, repositoryStatistics );
83 public void deleteStatistics( String repositoryId )
85 metadataRepository.removeMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
88 public List<RepositoryStatistics> getStatisticsInRange( String repositoryId, Date startTime, Date endTime )
90 List<RepositoryStatistics> results = new ArrayList<RepositoryStatistics>();
91 List<String> list = metadataRepository.getMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
92 Collections.sort( list, Collections.reverseOrder() );
93 for ( String name : list )
97 Date date = RepositoryStatistics.SCAN_TIMESTAMP.parse( name );
98 if ( ( startTime == null || !date.before( startTime ) ) &&
99 ( endTime == null || !date.after( endTime ) ) )
101 RepositoryStatistics stats =
102 (RepositoryStatistics) metadataRepository.getMetadataFacet( repositoryId,
103 RepositoryStatistics.FACET_ID,
105 results.add( stats );
108 catch ( ParseException e )
110 log.error( "Invalid scan result found in the metadata repository: " + e.getMessage() );
111 // continue and ignore this one
117 public void setMetadataRepository( MetadataRepository metadataRepository )
119 this.metadataRepository = metadataRepository;