Add support to retrieve repository statistics for a project via REST

Statistics for a repository can now be retrieved by GET on
'/projects/*/statistics.git'.

To get the statistics for a repository the global capability
'Run Garbage Collection' is required. Normal users should not be
interested in the repository statistics which is why we keep the access
restricted for now.

Change-Id: If5a2def2f840e1b30206a19a923d02e813db4049
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2013-03-20 13:20:26 +01:00 committed by Shawn Pearce
parent e026a7d7af
commit 19ea9b9733
4 changed files with 149 additions and 0 deletions

View File

@ -374,6 +374,40 @@ As response the new ref to which `HEAD` points is returned.
"refs/heads/stable"
----
[[get-repository-statistics]]
Get Repository Statistics
~~~~~~~~~~~~~~~~~~~~~~~~~
[verse]
'GET /projects/link:#project-name[\{project-name\}]/statistics.git'
Return statistics for the repository of a project.
.Request
----
GET /projects/plugins%2Freplication/statistics.git HTTP/1.0
----
The repository statistics are returned as a
link:#repository-statistics-info[RepositoryStatisticsInfo] entity.
.Response
----
HTTP/1.1 200 OK
Content-Disposition: attachment
Content-Type: application/json;charset=UTF-8
)]}'
{
"number_of_loose_objects": 127,
"number_of_loose_refs": 15,
"number_of_pack_files": 15,
"number_of_packed_objects": 67,
"number_of_packed_refs": 0,
"size_of_loose_objects": 29466,
"size_of_packed_objects": 9646
}
----
[[dashboard-endpoints]]
Dashboard Endpoints
-------------------
@ -805,6 +839,24 @@ Message that should be used to commit the change of the project parent
in the `project.config` file to the `refs/meta/config` branch.
|=============================
[[repository-statistics-info]]
RepositoryStatisticsInfo
~~~~~~~~~~~~~~~~~~~~~~~~
The `RepositoryStatisticsInfo` entity contains information about
statistics of a Git repository.
[options="header",width="50%",cols="1,6"]
|======================================
|Field Name |Description
|`number_of_loose_objects` |Number of loose objects.
|`number_of_loose_refs` |Number of loose refs.
|`number_of_pack_files` |Number of pack files.
|`number_of_packed_objects`|Number of packed objects.
|`number_of_packed_refs` |Number of packed refs.
|`size_of_loose_objects` |Size of loose objects in bytes.
|`size_of_packed_objects` |Size of packed objects in bytes.
|======================================
GERRIT
------

View File

@ -0,0 +1,63 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed 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.
package com.google.gerrit.server.project;
import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import org.eclipse.jgit.api.GarbageCollectCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.lib.Repository;
import java.io.IOException;
import java.util.Properties;
@RequiresCapability(GlobalCapability.RUN_GC)
public class GetStatistics implements RestReadView<ProjectResource> {
private final GitRepositoryManager repoManager;
@Inject
GetStatistics(GitRepositoryManager repoManager) {
this.repoManager = repoManager;
}
@Override
public RepositoryStatistics apply(ProjectResource rsrc)
throws ResourceNotFoundException, ResourceConflictException {
try {
Repository repo = repoManager.openRepository(rsrc.getNameKey());
try {
GarbageCollectCommand gc = Git.wrap(repo).gc();
return new RepositoryStatistics(gc.getStatistics());
} catch (GitAPIException e) {
throw new ResourceConflictException(e.getMessage());
} catch (JGitInternalException e) {
throw new ResourceConflictException(e.getMessage());
} finally {
repo.close();
}
} catch (IOException e) {
throw new ResourceNotFoundException(rsrc.getName());
}
}
}

View File

@ -43,6 +43,8 @@ public class Module extends RestApiModule {
get(PROJECT_KIND, "HEAD").to(GetHead.class);
put(PROJECT_KIND, "HEAD").to(SetHead.class);
get(PROJECT_KIND, "statistics.git").to(GetStatistics.class);
child(PROJECT_KIND, "dashboards").to(DashboardsCollection.class);
get(DASHBOARD_KIND).to(GetDashboard.class);
put(DASHBOARD_KIND).to(SetDashboard.class);

View File

@ -0,0 +1,32 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed 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.
package com.google.gerrit.server.project;
import com.google.common.base.CaseFormat;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;
class RepositoryStatistics extends TreeMap<String, Object> {
private static final long serialVersionUID = 1L;
public RepositoryStatistics(Properties p) {
for (Entry<Object, Object> e : p.entrySet()) {
put(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,
e.getKey().toString()), e.getValue());
}
}
}