Skip to content

Commit

Permalink
Merge branch 'statisticsOp' of github.com:kelseyishmael/GeoGit into k…
Browse files Browse the repository at this point in the history
…elseyishmael-statisticsOp

Conflicts:
	src/web/api/src/main/java/org/geogit/web/api/CommandBuilder.java
	src/web/api/src/main/java/org/geogit/web/api/ResponseWriter.java
  • Loading branch information
groldan committed May 20, 2014
2 parents 955d754 + 0e951fe commit fd20f33
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/web/api/src/main/java/org/geogit/web/api/CommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.geogit.web.api.commands.RemoveWebOp;
import org.geogit.web.api.commands.ResolveConflict;
import org.geogit.web.api.commands.RevertFeatureWebOp;
import org.geogit.web.api.commands.StatisticsWebOp;
import org.geogit.web.api.commands.Status;
import org.geogit.web.api.commands.TagWebOp;
import org.geogit.web.api.commands.UpdateRefWeb;
Expand Down Expand Up @@ -106,6 +107,8 @@ public static WebAPICommand build(String commandName, ParameterSet options)
command = buildVersion(options);
} else if ("cat".equalsIgnoreCase(commandName)) {
command = buildCat(options);
} else if ("statistics".equalsIgnoreCase(commandName)) {
command = buildStatistics(options);
} else {
throw new CommandSpecException("'" + commandName + "' is not a geogit command");
}
Expand Down Expand Up @@ -500,4 +503,12 @@ static CatWebOp buildCat(ParameterSet options) {
}
return command;
}

static StatisticsWebOp buildStatistics(ParameterSet options) {
StatisticsWebOp command = new StatisticsWebOp();
command.setPath(options.getFirstValue("path", null));
command.setSince(options.getFirstValue("since", null));
command.setUntil(options.getFirstValue("branch", null));
return command;
}
}
66 changes: 64 additions & 2 deletions src/web/api/src/main/java/org/geogit/web/api/ResponseWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.geogit.web.api.commands.LsTree;
import org.geogit.web.api.commands.RefParseWeb;
import org.geogit.web.api.commands.RemoteWebOp;
import org.geogit.web.api.commands.StatisticsWebOp;
import org.geogit.web.api.commands.TagWebOp;
import org.geogit.web.api.commands.UpdateRefWeb;
import org.geotools.metadata.iso.citation.Citations;
Expand Down Expand Up @@ -793,8 +794,8 @@ public void writeFeatureDiffResponse(Map<PropertyDescriptor, AttributeDiff> diff
* @param diff - a DiffEntry iterator to build the response from
* @throws XMLStreamException
*/
public void writeGeometryChanges(final Context geogit, Iterator<DiffEntry> diff,
int page, int elementsPerPage) throws XMLStreamException {
public void writeGeometryChanges(final Context geogit, Iterator<DiffEntry> diff, int page,
int elementsPerPage) throws XMLStreamException {

Iterators.advance(diff, page * elementsPerPage);
int counter = 0;
Expand Down Expand Up @@ -1147,6 +1148,67 @@ public void writeBlameReport(BlameReport report) throws XMLStreamException {
out.writeEndElement();
}

public void writeStatistics(List<StatisticsWebOp.featureTypeStats> stats,
RevCommit firstCommit, RevCommit lastCommit, int totalCommits, List<RevPerson> authors,
int totalAdded, int totalModified, int totalRemoved) throws XMLStreamException {
out.writeStartElement("Statistics");
int numFeatureTypes = 0;
int totalNumFeatures = 0;
if (!stats.isEmpty()) {
out.writeStartElement("FeatureTypes");
for (StatisticsWebOp.featureTypeStats stat : stats) {
numFeatureTypes++;
out.writeStartElement("FeatureType");
writeElement("name", stat.getName());
writeElement("numFeatures", Long.toString(stat.getNumFeatures()));
totalNumFeatures += stat.getNumFeatures();
out.writeEndElement();
}
if (numFeatureTypes > 1) {
writeElement("totalFeatureTypes", Integer.toString(numFeatureTypes));
writeElement("totalFeatures", Integer.toString(totalNumFeatures));
}
out.writeEndElement();
}
if (lastCommit != null) {
writeCommit(lastCommit, "latestCommit", null, null, null);
}
if (firstCommit != null) {
writeCommit(firstCommit, "firstCommit", null, null, null);
}
if (totalCommits > 0) {
writeElement("totalCommits", Integer.toString(totalCommits));
}
if (totalAdded > 0) {
writeElement("totalAdded", Integer.toString(totalAdded));
}
if (totalRemoved > 0) {
writeElement("totalRemoved", Integer.toString(totalRemoved));
}
if (totalModified > 0) {
writeElement("totalModified", Integer.toString(totalModified));
}
if (!authors.isEmpty()) {
out.writeStartElement("Authors");

for (RevPerson author : authors) {
out.writeStartElement("Author");
if (author.getName().isPresent()) {
writeElement("name", author.getName().get());
}
if (author.getEmail().isPresent()) {
writeElement("email", author.getEmail().get());
}
out.writeEndElement();
}

writeElement("totalAuthors", Integer.toString(authors.size()));
out.writeEndElement();
}
out.writeEndElement();

}

private class GeometryChange {
private GeogitSimpleFeature feature;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* Copyright (c) 2013 OpenPlans. All rights reserved.
* This code is licensed under the BSD New License, available at the root
* application directory.
*/
package org.geogit.web.api.commands;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.geogit.api.Context;
import org.geogit.api.NodeRef;
import org.geogit.api.ObjectId;
import org.geogit.api.RevCommit;
import org.geogit.api.RevPerson;
import org.geogit.api.plumbing.LsTreeOp;
import org.geogit.api.plumbing.ParseTimestamp;
import org.geogit.api.plumbing.RevParse;
import org.geogit.api.plumbing.diff.DiffEntry;
import org.geogit.api.porcelain.DiffOp;
import org.geogit.api.porcelain.LogOp;
import org.geogit.web.api.AbstractWebAPICommand;
import org.geogit.web.api.CommandContext;
import org.geogit.web.api.CommandResponse;
import org.geogit.web.api.ResponseWriter;
import org.geotools.util.Range;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

/**
* List certain statistics of repository.
*/

public class StatisticsWebOp extends AbstractWebAPICommand {

String path;

String since;

String until;

public void setPath(String path) {
this.path = path;
}

public void setSince(String since) {
this.since = since;
}

public void setUntil(String until) {
this.until = until;
}

/**
* Runs the command and builds the appropriate response
*
* @param context - the context to use for this command
*/
@Override
public void run(CommandContext context) {
final Context geogit = this.getCommandLocator(context);
final List<featureTypeStats> stats = Lists.newArrayList();
LogOp logOp = geogit.command(LogOp.class).setFirstParentOnly(true);
final Iterator<RevCommit> log;
if (since != null && !since.trim().isEmpty()) {
Date untilTime = new Date();
Date sinceTime = new Date(geogit.command(ParseTimestamp.class).setString(since).call());
logOp.setTimeRange(new Range<Date>(Date.class, sinceTime, untilTime));
}
if (this.until != null) {
Optional<ObjectId> until;
until = geogit.command(RevParse.class).setRefSpec(this.until).call();
Preconditions.checkArgument(until.isPresent(), "Object not found '%s'", this.until);
logOp.setUntil(until.get());
}

LsTreeOp lsTreeOp = geogit.command(LsTreeOp.class)
.setStrategy(LsTreeOp.Strategy.TREES_ONLY);
if (path != null && !path.trim().isEmpty()) {
lsTreeOp.setReference(path);
logOp.addPath(path);
}
final Iterator<NodeRef> treeIter = lsTreeOp.call();

while (treeIter.hasNext()) {
NodeRef node = treeIter.next();
stats.add(new featureTypeStats(node.path(), context.getGeoGIT().getRepository()
.getTree(node.objectId()).size()));
}
log = logOp.call();

RevCommit firstCommit = null;
RevCommit lastCommit = null;
int totalCommits = 0;
final List<RevPerson> authors = Lists.newArrayList();

if (log.hasNext()) {
lastCommit = log.next();
totalCommits++;
}
while (log.hasNext()) {
firstCommit = log.next();
RevPerson newAuthor = firstCommit.getAuthor();
boolean authorFound = false;
for (RevPerson author : authors) {
if (newAuthor.getName().equals(author.getName())
&& newAuthor.getEmail().equals(author.getEmail())) {
authorFound = true;
break;
}
}
if (!authorFound) {
authors.add(newAuthor);
}
totalCommits++;
}
int addedFeatures = 0;
int modifiedFeatures = 0;
int removedFeatures = 0;
if (since != null && !since.trim().isEmpty() && firstCommit != null && lastCommit != null) {
final Iterator<DiffEntry> diff = geogit.command(DiffOp.class)
.setOldVersion(firstCommit.getId()).setNewVersion(lastCommit.getId())
.setFilter(path).call();
while (diff.hasNext()) {
DiffEntry entry = diff.next();
if (entry.changeType() == DiffEntry.ChangeType.ADDED) {
addedFeatures++;
} else if (entry.changeType() == DiffEntry.ChangeType.MODIFIED) {
modifiedFeatures++;
} else {
removedFeatures++;
}
}
}

final RevCommit first = firstCommit;
final RevCommit last = lastCommit;
final int total = totalCommits;
final int added = addedFeatures;
final int modified = modifiedFeatures;
final int removed = removedFeatures;
context.setResponseContent(new CommandResponse() {
@Override
public void write(ResponseWriter out) throws Exception {
out.start(true);
out.writeStatistics(stats, first, last, total, authors, added, modified, removed);
out.finish();
}
});
}

public class featureTypeStats {
long numFeatures;

String featureTypeName;

public featureTypeStats(String name, long numFeatures) {
this.numFeatures = numFeatures;
this.featureTypeName = name;
}

public long getNumFeatures() {
return numFeatures;
}

public String getName() {
return featureTypeName;
}
};
}

0 comments on commit fd20f33

Please sign in to comment.