[MRM-1574] add rest method to get repositoty statistics

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1233310 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-01-19 12:18:19 +00:00
parent 0cb6235dfa
commit 4ada471449
7 changed files with 292 additions and 37 deletions

View File

@ -56,5 +56,4 @@ public interface RepositoryCommonValidator
*/
String removeExpressions( String directory );
}

View File

@ -0,0 +1,148 @@
package org.apache.archiva.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
/**
* @author Olivier Lamy
* @since 1.4-M3
*/
@XmlRootElement( name = "archivaRepositoryStatistics" )
public class ArchivaRepositoryStatistics
{
private Date scanEndTime;
private Date scanStartTime;
private long totalArtifactCount;
private long totalArtifactFileSize;
private long totalFileCount;
private long totalGroupCount;
private long totalProjectCount;
private long newFileCount;
public ArchivaRepositoryStatistics()
{
// no op
}
public Date getScanEndTime()
{
return scanEndTime;
}
public void setScanEndTime( Date scanEndTime )
{
this.scanEndTime = scanEndTime;
}
public Date getScanStartTime()
{
return scanStartTime;
}
public void setScanStartTime( Date scanStartTime )
{
this.scanStartTime = scanStartTime;
}
public long getTotalArtifactCount()
{
return totalArtifactCount;
}
public void setTotalArtifactCount( long totalArtifactCount )
{
this.totalArtifactCount = totalArtifactCount;
}
public long getTotalArtifactFileSize()
{
return totalArtifactFileSize;
}
public void setTotalArtifactFileSize( long totalArtifactFileSize )
{
this.totalArtifactFileSize = totalArtifactFileSize;
}
public long getTotalFileCount()
{
return totalFileCount;
}
public void setTotalFileCount( long totalFileCount )
{
this.totalFileCount = totalFileCount;
}
public long getTotalGroupCount()
{
return totalGroupCount;
}
public void setTotalGroupCount( long totalGroupCount )
{
this.totalGroupCount = totalGroupCount;
}
public long getTotalProjectCount()
{
return totalProjectCount;
}
public void setTotalProjectCount( long totalProjectCount )
{
this.totalProjectCount = totalProjectCount;
}
public long getNewFileCount()
{
return newFileCount;
}
public void setNewFileCount( long newFileCount )
{
this.newFileCount = newFileCount;
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append( "ArchivaRepositoryStatistics" );
sb.append( "{scanEndTime=" ).append( scanEndTime );
sb.append( ", scanStartTime=" ).append( scanStartTime );
sb.append( ", totalArtifactCount=" ).append( totalArtifactCount );
sb.append( ", totalArtifactFileSize=" ).append( totalArtifactFileSize );
sb.append( ", totalFileCount=" ).append( totalFileCount );
sb.append( ", totalGroupCount=" ).append( totalGroupCount );
sb.append( ", totalProjectCount=" ).append( totalProjectCount );
sb.append( ", newFileCount=" ).append( newFileCount );
sb.append( '}' );
return sb.toString();
}
}

View File

@ -20,6 +20,7 @@ package org.apache.archiva.rest.api.services;
*/
import org.apache.archiva.admin.model.beans.ManagedRepository;
import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.codehaus.plexus.redback.authorization.RedbackAuthorization;
@ -87,5 +88,12 @@ public interface ManagedRepositoriesService
Boolean fileLocationExists( @QueryParam( "fileLocation" ) String fileLocation )
throws ArchivaRestServiceException;
@Path( "getManagedRepositoryStatistics/{repositoryId}" )
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
ArchivaRepositoryStatistics getManagedRepositoryStatistics( @PathParam( "repositoryId" ) String repositoryId )
throws ArchivaRestServiceException;
}

View File

@ -18,17 +18,26 @@ package org.apache.archiva.rest.services;
* under the License.
*/
import net.sf.beanlib.provider.replicator.BeanReplicator;
import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.RepositoryCommonValidator;
import org.apache.archiva.admin.model.beans.ManagedRepository;
import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.archiva.metadata.repository.MetadataRepositoryException;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.metadata.repository.RepositorySessionFactory;
import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.File;
import java.util.Collections;
import java.util.List;
@ -52,6 +61,13 @@ public class DefaultManagedRepositoriesService
@Inject
private RepositoryCommonValidator repositoryCommonValidator;
@Inject
private RepositoryStatisticsManager repositoryStatisticsManager;
@Inject
@Named( value = "repositorySessionFactory" )
protected RepositorySessionFactory repositorySessionFactory;
public List<ManagedRepository> getManagedRepositories()
throws ArchivaRestServiceException
@ -143,4 +159,37 @@ public class DefaultManagedRepositoriesService
String location = repositoryCommonValidator.removeExpressions( fileLocation );
return new File( location ).exists();
}
public ArchivaRepositoryStatistics getManagedRepositoryStatistics( String repositoryId )
throws ArchivaRestServiceException
{
RepositorySession repositorySession = repositorySessionFactory.createSession();
try
{
MetadataRepository metadataRepository = repositorySession.getRepository();
RepositoryStatistics stats = null;
try
{
stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
}
catch ( MetadataRepositoryException e )
{
log.warn( "Error retrieving repository statistics: " + e.getMessage(), e );
}
if ( stats != null )
{
ArchivaRepositoryStatistics archivaRepositoryStatistics =
new BeanReplicator().replicateBean( stats, ArchivaRepositoryStatistics.class );
return archivaRepositoryStatistics;
}
}
finally
{
repositorySession.close();
}
return null;
}
}

View File

@ -30,14 +30,17 @@ import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
import org.apache.archiva.rest.api.services.RepositoriesService;
import org.apache.archiva.rest.api.services.RepositoryGroupService;
import org.apache.archiva.rest.api.services.SearchService;
import org.apache.archiva.security.common.ArchivaRoleConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.codehaus.redback.rest.services.AbstractRestServicesTest;
import org.junit.Before;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.util.Date;
/**
* @author Olivier Lamy
@ -57,6 +60,24 @@ public abstract class AbstractArchivaRestTest
// END SNIPPET: authz-header
@Override
@Before
public void startServer()
throws Exception
{
File appServerBase = new File( System.getProperty( "appserver.base" ) );
File jcrDirectory = new File( appServerBase, "jcr" );
if ( jcrDirectory.exists() )
{
FileUtils.deleteDirectory( jcrDirectory );
}
super.startServer();
}
@Override
protected String getSpringConfigLocation()
{
@ -264,4 +285,40 @@ public abstract class AbstractArchivaRestTest
}
}
protected void createAndIndexRepo( String testRepoId, String repoPath )
throws Exception
{
if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
{
getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
}
ManagedRepository managedRepository = new ManagedRepository();
managedRepository.setId( testRepoId );
managedRepository.setName( "test repo" );
managedRepository.setLocation( new File( repoPath ).getPath() );
managedRepository.setIndexDirectory(
System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
service.addManagedRepository( managedRepository );
getRoleManagementService( authorizationHeader ).assignTemplatedRole(
ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
}
protected void deleteTestRepo( String id )
throws Exception
{
if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
{
getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
}
}
}

View File

@ -20,7 +20,9 @@ package org.apache.archiva.rest.services;
*/
import org.apache.archiva.admin.model.beans.ManagedRepository;
import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
import org.apache.archiva.rest.api.services.RepositoriesService;
import org.junit.Test;
import java.io.File;
@ -96,5 +98,33 @@ public class ManagedRepositoriesServiceTest
}
@Test
public void getManagedRepositoryStatistics()
throws Exception
{
String testRepoId = "test-repo";
// force guest user creation if not exists
if ( getUserService( authorizationHeader ).getGuestUser() == null )
{
assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
}
createAndIndexRepo( testRepoId, "src/test/repo-with-osgi" );
ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
ArchivaRepositoryStatistics archivaRepositoryStatistics = service.getManagedRepositoryStatistics( testRepoId );
assertNotNull( archivaRepositoryStatistics );
log.info( "archivaRepositoryStatistics:" + archivaRepositoryStatistics.toString() );
assertEquals( 92, archivaRepositoryStatistics.getNewFileCount() );
assertEquals( 92, archivaRepositoryStatistics.getTotalFileCount() );
deleteTestRepo( testRepoId );
}
}

View File

@ -358,42 +358,6 @@ public class SearchServiceTest
deleteTestRepo( testRepoId );
}
private void createAndIndexRepo( String testRepoId, String repoPath )
throws Exception
{
if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
{
getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
}
ManagedRepository managedRepository = new ManagedRepository();
managedRepository.setId( testRepoId );
managedRepository.setName( "test repo" );
managedRepository.setLocation( new File( repoPath ).getPath() );
managedRepository.setIndexDirectory( "target/.index-" + Long.toString( new Date().getTime() ) );
ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
service.addManagedRepository( managedRepository );
getRoleManagementService( authorizationHeader ).assignTemplatedRole(
ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
}
private void deleteTestRepo( String id )
throws Exception
{
if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
{
getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
}
}
}