/*
Copyright 2012 Christian Prause and Fraunhofer FIT
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 net.sf.collabreview.experimental;
import net.sf.collabreview.agents.MicroEditCastigator;
import net.sf.collabreview.core.Artifact;
import net.sf.collabreview.core.ArtifactIdentifier;
import net.sf.collabreview.core.CollabReview;
import net.sf.collabreview.core.Repository;
import net.sf.collabreview.core.configuration.ConfigurationData;
import net.sf.collabreview.web.ExtensionServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.TreeSet;
/**
* Counts the number of micro edits that occurred in certain time intervals.
* This is part of my scientific evaluation. Probably has no other purpose...
*
* @author Christian Prause (prause)
* @date 2012-04-26 17:32
*/
public class MicroEditCounter extends ExtensionServlet {
/**
* Apache commons logging logger for class MicroEditCounter.
*/
private static final Log logger = LogFactory.getLog(MicroEditCounter.class);
private CollabReview collabReview;
/**
* Tuesday, July 19th 2011, 16:00:00 (GMT) translates to 1311091200
*/
private long microEditCastigatorInstallTime = (long) 1311091200 * 1000;
/**
* About seven months of time.
*/
private long surveyPeriod = (long) 1000 * 3600 * 24 * 30 * 9;
private StringBuffer result;
@Override
protected void configure(CollabReview collabReview, ConfigurationData config) {
this.collabReview = collabReview;
}
@Override
protected void destroy() {
}
@SuppressWarnings({"deprecation"})
@Override
protected synchronized void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
if (result == null) {
result = new StringBuffer();
new Thread("MECCalc") {
@Override
public void run() {
int slices = 9;
int microEditCounts[] = new int[slices * 2];
int totalMicroEditsBefore = 0;
int totalMicroEditsAfter = 0;
int totalRevisionsBefore = 0;
int totalRevisionsAfter = 0;
result.append("<table border=1>\n");
Repository repository = collabReview.getRepository();
TreeSet<String> articles = new TreeSet<String>();
for (ArtifactIdentifier artifactIdentifier : repository.listAllArtifacts()) {
articles.add(artifactIdentifier.getName());
/*articles.add("GreenIT");
articles.add("Hydra_JF");
articles.add("Reputationforge");
articles.add("Issues");
articles.add("Enocean");
articles.add("Projekt_BRIDGE");
articles.add("Zeitschriften-Wunschliste");*/
}
for (String article : articles) {
logger.debug("Computing micro edits for article " + article);
Artifact artifact = repository.getLatestForName(article, "");
int beforeCount = MicroEditCastigator.countMicroEdits(repository, artifact, 3, 300 * 1000, microEditCastigatorInstallTime, surveyPeriod);
int afterCount = MicroEditCastigator.countMicroEdits(repository, artifact, 3, 300 * 1000, microEditCastigatorInstallTime + surveyPeriod, surveyPeriod);
totalMicroEditsBefore += beforeCount;
totalMicroEditsAfter += afterCount;
result.append(String.format("<tr><td>%s</td><td>%d</td><td>%d</td>", article, beforeCount, afterCount));
for (int i = 0; i < 2 * slices; i++) {
int microEditCount = MicroEditCastigator.countMicroEdits(repository, artifact, 3, 300 * 1000, microEditCastigatorInstallTime + surveyPeriod - i * surveyPeriod / slices, surveyPeriod / slices);
microEditCounts[i] += microEditCount;
result.append("<td>").append(microEditCount).append("</td>");
}
result.append("</tr>\n");
repository.commit();
}
result.append(String.format("<tr><td>Total</td><td>%d</td><td>%d</td>", totalMicroEditsBefore, totalMicroEditsAfter));
for (int i = 0; i < 2 * slices; i++) {
result.append("<td>").append(microEditCounts[i]).append("</td>");
}
result.append("</tr>\n");
logger.debug("Counting revisions...");
for (ArtifactIdentifier artifactIdentifier : repository.listAllArtifacts()) {
long time = repository.getArtifact(artifactIdentifier).getDate().getTime();
if (time > microEditCastigatorInstallTime - surveyPeriod && time < microEditCastigatorInstallTime) {
totalRevisionsBefore++;
}
if (time > microEditCastigatorInstallTime && time < microEditCastigatorInstallTime + surveyPeriod) {
totalRevisionsAfter++;
}
repository.commit();
}
result.append(String.format("<tr><td>Total revisions</td><td>%d</td><td>%d</td></tr>\n", totalRevisionsBefore, totalRevisionsAfter));
result.append("</table>\n");
logger.debug("done");
}
}.start();
}
writer.println("<html><head><title>MicroEditCounter</title></head><body>");
writer.println(result);
writer.println("</body></html>");
writer.flush();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
doGet(request, response);
}
}