]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1574] add rest method to get repositoty statistics
authorOlivier Lamy <olamy@apache.org>
Thu, 19 Jan 2012 12:18:19 +0000 (12:18 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 19 Jan 2012 12:18:19 +0000 (12:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1233310 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-api/src/main/java/org/apache/archiva/admin/model/RepositoryCommonValidator.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArchivaRepositoryStatistics.java [new file with mode: 0644]
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ManagedRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ManagedRepositoriesServiceTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/SearchServiceTest.java

diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArchivaRepositoryStatistics.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ArchivaRepositoryStatistics.java
new file mode 100644 (file)
index 0000000..7b55e64
--- /dev/null
@@ -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();
+    }
+}
index 934274e7335199bd8f62fa318387656dfd8b384c..98d4dbef9a8bd18ebf09da7ff0edeadf8c742105 100644 (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;
+
 
 }
index 0b02b472e2e54001d1fe664eea5758e17f91d467..87f589179f62698c1f33fbfeaaaeb47a93a3b73a 100644 (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;
+    }
 }
index 7b78165276e3890d0e7b7c6b33a1827507017bcb..e43b521baa9eac4a79f5b7f09fe2e79240f28553 100644 (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 );
+        }
+
+    }
 }
index 47a896eee855ef6b6b3aa5dc1209ac45d750ad63..edffe0762bbcbefe4c7695d9916026320bab12d8 100644 (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 );
+    }
 }
index 43ac0b14d5ebed259c62bcefbaed09c4ff4aedd4..89ac79b0d8e7f9e5a2ac9d9a5094fc0031642c15 100644 (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 );
-        }
-
-    }
-
-
 }