Commit 8343274b authored by Feng Xue's avatar Feng Xue

Add ETag support to correlation and regression

Add the support to correlation and regression

Fixes #2062
parent 9ce7cd95
package com.nesstar.rest.common;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.MissingDeletionType;
import java.util.List;
public class CorrelationClassVariables {
private Study study;
private Server server;
private List<Variable> correlationVariables;
private List<Variable> weightVariables;
private String caseSubsetExpression;
private MissingDeletionType missingDeletionType;
public Study getStudy() {
return study;
}
public void setStudy(Study study) {
this.study = study;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public List<Variable> getCorrelationVariables() {
return correlationVariables;
}
public void setCorrelationVariables(List<Variable> correlationVariables) {
this.correlationVariables = correlationVariables;
}
public List<Variable> getWeightVariables() {
return weightVariables;
}
public void setWeightVariables(List<Variable> weightVariables) {
this.weightVariables = weightVariables;
}
public String getCaseSubsetExpression() {
return caseSubsetExpression;
}
public void setCaseSubsetExpression(String caseSubsetExpression) {
this.caseSubsetExpression = caseSubsetExpression;
}
public MissingDeletionType getMissingDeletionType() {
return missingDeletionType;
}
public void setMissingDeletionType(MissingDeletionType missingDeletionType) {
this.missingDeletionType = missingDeletionType;
}
}
......@@ -75,7 +75,7 @@ public final class ETag {
}
data.add(classVariables.getCaseSubset());
return ETag.createETag(new ETagGenerator(data));
return createETag(new ETagGenerator(data));
}
public static ETag generateEtagForCube(final Cube cube, final CubeRequest cubeRequest, final HttpServletRequest request) {
......@@ -84,4 +84,44 @@ public final class ETag {
data.add(url).add(cube).add(cubeRequest);
return createETag(new ETagGenerator(data));
}
public static ETag generateEtagForCorrelation(final CorrelationClassVariables classVariables, final HttpServletRequest request) {
ETagData data = new ETagData();
String url = request.getRequestURL().toString();
data.add(url).add(classVariables.getStudy());
for (Variable variable: classVariables.getCorrelationVariables()) {
data.add(variable);
}
for (Variable variable: classVariables.getWeightVariables()) {
data.add(variable);
}
data.add(classVariables.getCaseSubsetExpression());
data.add(classVariables.getMissingDeletionType());
return createETag(new ETagGenerator(data));
}
public static ETag generateEtagForRegression(final RegressionClassVariables classVariables, final HttpServletRequest request) {
ETagData data = new ETagData();
String url = request.getRequestURL().toString();
data.add(url).add(classVariables.getStudy());
for (Variable variable: classVariables.getIndependentVariables()) {
data.add(variable);
}
for (Variable variable: classVariables.getWeightVariables()) {
data.add(variable);
}
data.add(classVariables.getDependentVariable());
data.add(classVariables.getCaseSubsetExpression());
data.add(classVariables.getMissingDeletionType());
return createETag(new ETagGenerator(data));
}
}
package com.nesstar.rest.common;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.MissingDeletionType;
import java.util.List;
public class RegressionClassVariables {
private Study study;
private Server server;
private Variable dependentVariable;
private List<Variable> independentVariables;
private List<Variable> weightVariables;
private String caseSubsetExpression;
private MissingDeletionType missingDeletionType;
public Study getStudy() {
return study;
}
public void setStudy(Study study) {
this.study = study;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Variable getDependentVariable() {
return dependentVariable;
}
public void setDependentVariable(Variable dependentVariable) {
this.dependentVariable = dependentVariable;
}
public List<Variable> getIndependentVariables() {
return independentVariables;
}
public void setIndependentVariables(List<Variable> independentVariables) {
this.independentVariables = independentVariables;
}
public List<Variable> getWeightVariables() {
return weightVariables;
}
public void setWeightVariables(List<Variable> weightVariables) {
this.weightVariables = weightVariables;
}
public String getCaseSubsetExpression() {
return caseSubsetExpression;
}
public void setCaseSubsetExpression(String caseSubsetExpression) {
this.caseSubsetExpression = caseSubsetExpression;
}
public MissingDeletionType getMissingDeletionType() {
return missingDeletionType;
}
public void setMissingDeletionType(MissingDeletionType missingDeletionType) {
this.missingDeletionType = missingDeletionType;
}
}
package com.nesstar.rest.resources;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.CorrelationBuilder;
import com.nesstar.api.analysis.CorrelationResult;
import com.nesstar.api.analysis.MissingDeletionType;
import com.nesstar.api.subset.CaseSubset;
import com.nesstar.api.subset.SubsetSyntaxException;
import com.nesstar.rest.common.ServerHandler;
import com.nesstar.rest.common.Operation;
import com.nesstar.rest.common.OperationRunner;
import com.nesstar.rest.common.ResourceExtractor;
import com.nesstar.rest.common.ServerHandler;
import com.nesstar.rest.common.ETag;
import com.nesstar.rest.common.CorrelationClassVariables;
import com.nesstar.rest.result_object_makers.MetadataMaker;
import com.yammer.metrics.annotation.Timed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
......@@ -38,14 +38,6 @@ public final class CorrelationResource extends AbstractResource {
super(serverHandler);
}
private static class ClassVariables {
private Study study;
private Server server;
private List<Variable> correlationVariables;
private List<Variable> weightVariables;
private String caseSubsetExpression;
private MissingDeletionType missingDeletionType;
}
@GET
@Timed
......@@ -54,53 +46,57 @@ public final class CorrelationResource extends AbstractResource {
@PathParam("studyId") final String studyId,
@QueryParam("caseSubset") final String caseSubset,
@QueryParam("missDeletion") final String missingDeletion,
@Context final HttpServletRequest request) {
@Context final HttpServletRequest request,
@Context final HttpServletResponse response) {
final ClassVariables classVariables = new ClassVariables();
final CorrelationClassVariables classVariables = new CorrelationClassVariables();
return OperationRunner.runWithExceptionHandling(new Operation() {
@Override
public Object run() throws Exception {
MissingDeletionType missingDeletionType = MissingDeletionType.valueOf(missingDeletion);
classVariables.server = serverHandler.getServer();
classVariables.study = ResourceExtractor.getStudy(studyId, classVariables.server);
classVariables.correlationVariables = ResourceExtractor.getVariables(correlationVariables, classVariables.server);
classVariables.weightVariables = ResourceExtractor.getVariables(weightVariables, classVariables.server);
classVariables.caseSubsetExpression = caseSubset;
if (null != missingDeletionType) {
classVariables.missingDeletionType = missingDeletionType;
classVariables.setServer(serverHandler.getServer());
classVariables.setStudy(ResourceExtractor.getStudy(studyId, classVariables.getServer()));
classVariables.setCorrelationVariables(ResourceExtractor.getVariables(correlationVariables, classVariables.getServer()));
classVariables.setWeightVariables(ResourceExtractor.getVariables(weightVariables, classVariables.getServer()));
classVariables.setCaseSubsetExpression(caseSubset);
try {
classVariables.setMissingDeletionType(classVariables.getMissingDeletionType());
} catch (NullPointerException e) {
//ignore
}
CorrelationResult correlationResult = performCorrelation(classVariables);
ETag etag = ETag.generateEtagForCorrelation(classVariables, request);
setETagHeader(etag, response);
return getCorrelationObject(correlationResult, classVariables);
}
}, serverHandler);
}
private CorrelationResult performCorrelation(ClassVariables classVariables) throws IOException, NotAuthorizedException, SubsetSyntaxException {
private CorrelationResult performCorrelation(CorrelationClassVariables classVariables) throws IOException, NotAuthorizedException, SubsetSyntaxException {
CorrelationBuilder correlationBuilder = new CorrelationBuilder();
for (Variable variable : classVariables.correlationVariables) {
for (Variable variable : classVariables.getCorrelationVariables()) {
correlationBuilder.addCorrelationVariable(variable);
}
for (Variable weightVariable : classVariables.weightVariables) {
for (Variable weightVariable : classVariables.getWeightVariables()) {
correlationBuilder.addWeightVariable(weightVariable);
}
if (classVariables.caseSubsetExpression != null) {
correlationBuilder.setCaseSubset(CaseSubset.compile(classVariables.caseSubsetExpression, classVariables.server));
if (classVariables.getCaseSubsetExpression() != null) {
correlationBuilder.setCaseSubset(CaseSubset.compile(classVariables.getCaseSubsetExpression(), classVariables.getServer()));
}
if (classVariables.missingDeletionType != null) {
correlationBuilder.setMissingDeletionType(classVariables.missingDeletionType);
if (classVariables.getMissingDeletionType() != null) {
correlationBuilder.setMissingDeletionType(classVariables.getMissingDeletionType());
}
return classVariables.study.correlate(correlationBuilder);
return classVariables.getStudy().correlate(correlationBuilder);
}
private Object getCorrelationObject(CorrelationResult correlationResult, ClassVariables classVariables) throws IOException, NotAuthorizedException {
private Object getCorrelationObject(CorrelationResult correlationResult, CorrelationClassVariables classVariables) throws IOException, NotAuthorizedException {
HashMap<String, Object> resultValuesMap = new LinkedHashMap<String, Object>();
for (int i = 0; i < classVariables.correlationVariables.size(); i++) {
for (int j = i + 1; j < classVariables.correlationVariables.size(); j++) {
Variable var1 = classVariables.correlationVariables.get(i);
Variable var2 = classVariables.correlationVariables.get(j);
List<Variable> correlationVariables = classVariables.getCorrelationVariables();
for (int i = 0; i < correlationVariables.size(); i++) {
for (int j = i + 1; j < correlationVariables.size(); j++) {
Variable var1 = correlationVariables.get(i);
Variable var2 = correlationVariables.get(j);
String key = var1.getId() + "|" + var2.getId();
resultValuesMap.put(key, getCorrelationCoefficients(var1, var2, correlationResult));
}
......
......@@ -2,21 +2,23 @@ package com.nesstar.rest.resources;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.LinearRegressionBuilder;
import com.nesstar.api.analysis.LinearRegressionResult;
import com.nesstar.api.analysis.MissingDeletionType;
import com.nesstar.api.subset.CaseSubset;
import com.nesstar.rest.common.ServerHandler;
import com.nesstar.rest.common.Operation;
import com.nesstar.rest.common.OperationRunner;
import com.nesstar.rest.result_object_makers.ErrorMaker;
import com.nesstar.rest.common.ResourceExtractor;
import com.nesstar.rest.common.ServerHandler;
import com.nesstar.rest.common.ETag;
import com.nesstar.rest.common.RegressionClassVariables;
import com.nesstar.rest.result_object_makers.ErrorMaker;
import com.nesstar.rest.result_object_makers.MetadataMaker;
import com.yammer.metrics.annotation.Timed;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.Produces;
......@@ -39,16 +41,6 @@ public final class RegressionResource extends AbstractResource {
super(serverHandler);
}
private static class ClassVariables {
private Study study;
private Server server;
private Variable dependentVariable;
private List<Variable> independentVariables;
private List<Variable> weightVariables;
private String caseSubsetExpression;
private MissingDeletionType missingDeletionType;
}
@GET
@Timed
public Object regress(@QueryParam("depVar") final String dependentVariable,
......@@ -57,9 +49,10 @@ public final class RegressionResource extends AbstractResource {
@QueryParam("caseSubset") final String caseSubset,
@QueryParam("missDeletion") final String missingDeletion,
@PathParam("studyId") final String studyId,
@Context final HttpServletRequest request) {
@Context final HttpServletRequest request,
@Context final HttpServletResponse response) {
final ClassVariables classVariables = new ClassVariables();
final RegressionClassVariables classVariables = new RegressionClassVariables();
if (dependentVariable == null || independentVariables.isEmpty()) {
return ErrorMaker.getBadRequestError("The request must specify a dependent variable and at least 1 independent variables.");
......@@ -68,47 +61,52 @@ public final class RegressionResource extends AbstractResource {
return OperationRunner.runWithExceptionHandling(new Operation() {
@Override
public Object run() throws Exception {
MissingDeletionType missingDeletionType = MissingDeletionType.valueOf(missingDeletion);
classVariables.server = serverHandler.getServer();
classVariables.study = ResourceExtractor.getStudy(studyId, classVariables.server);
classVariables.dependentVariable = ResourceExtractor.getVariable(dependentVariable, classVariables.server);
classVariables.independentVariables = ResourceExtractor.getVariables(independentVariables, classVariables.server);
classVariables.weightVariables = ResourceExtractor.getVariables(weightVariables, classVariables.server);
classVariables.caseSubsetExpression = caseSubset;
if (null != missingDeletionType) {
classVariables.missingDeletionType = missingDeletionType;
classVariables.setServer(serverHandler.getServer());
Server server = classVariables.getServer();
classVariables.setStudy(ResourceExtractor.getStudy(studyId, server));
classVariables.setDependentVariable(ResourceExtractor.getVariable(dependentVariable, server));
classVariables.setIndependentVariables(ResourceExtractor.getVariables(independentVariables, server));
classVariables.setWeightVariables(ResourceExtractor.getVariables(weightVariables, server));
classVariables.setCaseSubsetExpression(caseSubset);
try {
classVariables.setMissingDeletionType(classVariables.getMissingDeletionType());
} catch (NullPointerException e) {
//ignore
}
LinearRegressionResult linearRegressionResult = performRegression(classVariables);
ETag etag = ETag.generateEtagForRegression(classVariables, request);
setETagHeader(etag, response);
return getRegressionObject(linearRegressionResult, classVariables);
}
}, serverHandler);
}
private LinearRegressionResult performRegression(ClassVariables classVariables) throws Exception {
private LinearRegressionResult performRegression(RegressionClassVariables classVariables) throws Exception {
LinearRegressionBuilder builder = new LinearRegressionBuilder();
builder.setDependentVariable(classVariables.dependentVariable);
for (Variable independentVariable : classVariables.independentVariables) {
builder.setDependentVariable(classVariables.getDependentVariable());
for (Variable independentVariable : classVariables.getIndependentVariables()) {
builder.addIndependentVariable(independentVariable);
}
for (Variable weightVariable : classVariables.weightVariables) {
for (Variable weightVariable : classVariables.getWeightVariables()) {
builder.addWeightVariable(weightVariable);
}
if (classVariables.caseSubsetExpression != null) {
builder.setCaseSubset(CaseSubset.compile(classVariables.caseSubsetExpression, classVariables.server));
if (classVariables.getCaseSubsetExpression() != null) {
builder.setCaseSubset(CaseSubset.compile(classVariables.getCaseSubsetExpression(), classVariables.getServer()));
}
if (classVariables.missingDeletionType != null) {
builder.setMissingDeletionType(classVariables.missingDeletionType);
MissingDeletionType missingDeletionType = classVariables.getMissingDeletionType();
if (missingDeletionType != null) {
builder.setMissingDeletionType(missingDeletionType);
}
return (LinearRegressionResult) classVariables.study.regress(builder);
return (LinearRegressionResult) classVariables.getStudy().regress(builder);
}
private Object getRegressionObject(LinearRegressionResult linearRegressionResult, ClassVariables classVariables) throws IOException, NotAuthorizedException {
private Object getRegressionObject(LinearRegressionResult linearRegressionResult, RegressionClassVariables classVariables) throws IOException, NotAuthorizedException {
HashMap<String, Object> regression = new LinkedHashMap<String, Object>();
regression.put("dependentVariableId", classVariables.dependentVariable.getId());
regression.put("dependentVariableId", classVariables.getDependentVariable().getId());
regression.put("caseCount", linearRegressionResult.getCaseCount());
regression.put("intercept", linearRegressionResult.getIntercept());
......@@ -121,7 +119,7 @@ public final class RegressionResource extends AbstractResource {
@SuppressWarnings("rawtypes")
List<Map> independentVariablesData = new ArrayList<Map>();
for (Variable independentVariable : classVariables.independentVariables) {
for (Variable independentVariable : classVariables.getIndependentVariables()) {
independentVariablesData.add(getIndependentVariableData(independentVariable, linearRegressionResult));
}
regression.put("independentVariablesData", independentVariablesData);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment