Search in sources :

Example 1 with DescribeSolutionResult

use of com.amazonaws.services.personalize.model.DescribeSolutionResult in project knime-cloud by knime.

the class AmazonPersonalizeCreateSolutionVersionNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    final CloudConnectionInformation cxnInfo = ((AmazonConnectionInformationPortObject) inObjects[0]).getConnectionInformation();
    try (final AmazonPersonalizeConnection personalizeConnection = new AmazonPersonalizeConnection(cxnInfo)) {
        final AmazonPersonalize personalizeClient = personalizeConnection.getClient();
        // Create solution configuration or use existing one
        final String solutionArn;
        if (m_settings.isCreateNewSolution()) {
            solutionArn = createSolution(personalizeClient);
        } else {
            solutionArn = m_settings.getExistingSolution().getARN();
        }
        // Wait until the solution is active
        final DescribeSolutionRequest describeSolutionRequest = new DescribeSolutionRequest().withSolutionArn(solutionArn);
        AmazonPersonalizeUtils.waitUntilActive(() -> {
            DescribeSolutionResult describeSolution = personalizeClient.describeSolution(describeSolutionRequest);
            final String status = describeSolution.getSolution().getStatus();
            exec.setMessage("Creating solution configuration (Status: " + status + ")");
            return status.equals(Status.ACTIVE.getStatus());
        }, 100);
        exec.setProgress(0.5);
        // Create solution version
        final String solutionVersionArn = personalizeClient.createSolutionVersion(new CreateSolutionVersionRequest().withSolutionArn(solutionArn)).getSolutionVersionArn();
        // Wait until solution version is active (or failed)
        final DescribeSolutionVersionRequest describeSolutionVersionRequest = new DescribeSolutionVersionRequest().withSolutionVersionArn(solutionVersionArn);
        AmazonPersonalizeUtils.waitUntilActive(() -> {
            final DescribeSolutionVersionResult solutionVersionDescription = personalizeClient.describeSolutionVersion(describeSolutionVersionRequest);
            final String status = solutionVersionDescription.getSolutionVersion().getStatus();
            exec.setMessage("Creating solution version (Status: " + status + ")");
            if (status.equals(Status.CREATED_FAILED.getStatus())) {
                throw new IllegalStateException("No solution version has been created. Reason: " + solutionVersionDescription.getSolutionVersion().getFailureReason());
            }
            return status.equals(Status.ACTIVE.getStatus());
        }, 2000);
        // Retrieve the recipe type to put it into the output
        final DescribeSolutionVersionResult solutionVersionDescription = personalizeClient.describeSolutionVersion(describeSolutionVersionRequest);
        final String recipeType = personalizeClient.describeRecipe(new DescribeRecipeRequest().withRecipeArn(solutionVersionDescription.getSolutionVersion().getRecipeArn())).getRecipe().getRecipeType();
        // final String recipeType = personalizeClient.describeRecipe(new DescribeRecipeRequest().withRecipeArn(
        // personalizeClient.describeSolution(new DescribeSolutionRequest().withSolutionArn(solutionArn))
        // .getSolution().getRecipeArn()))
        // .getRecipe().getRecipeType();
        // Create output
        final Map<String, Double> metrics = personalizeClient.getSolutionMetrics(new GetSolutionMetricsRequest().withSolutionVersionArn(solutionVersionArn)).getMetrics();
        if (m_settings.isOutputSolutionVersionArnAsVar()) {
            pushFlowVariableString("solution-version-ARN", solutionVersionArn);
        }
        return new PortObject[] { createOutput(exec, solutionVersionArn, recipeType, metrics) };
    }
}
Also used : AmazonPersonalizeConnection(org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection) DescribeRecipeRequest(com.amazonaws.services.personalize.model.DescribeRecipeRequest) GetSolutionMetricsRequest(com.amazonaws.services.personalize.model.GetSolutionMetricsRequest) DescribeSolutionResult(com.amazonaws.services.personalize.model.DescribeSolutionResult) AmazonPersonalize(com.amazonaws.services.personalize.AmazonPersonalize) DescribeSolutionVersionResult(com.amazonaws.services.personalize.model.DescribeSolutionVersionResult) CreateSolutionVersionRequest(com.amazonaws.services.personalize.model.CreateSolutionVersionRequest) DescribeSolutionRequest(com.amazonaws.services.personalize.model.DescribeSolutionRequest) AmazonConnectionInformationPortObject(org.knime.cloud.aws.util.AmazonConnectionInformationPortObject) DescribeSolutionVersionRequest(com.amazonaws.services.personalize.model.DescribeSolutionVersionRequest) CloudConnectionInformation(org.knime.cloud.core.util.port.CloudConnectionInformation) AmazonConnectionInformationPortObject(org.knime.cloud.aws.util.AmazonConnectionInformationPortObject) PortObject(org.knime.core.node.port.PortObject)

Aggregations

AmazonPersonalize (com.amazonaws.services.personalize.AmazonPersonalize)1 CreateSolutionVersionRequest (com.amazonaws.services.personalize.model.CreateSolutionVersionRequest)1 DescribeRecipeRequest (com.amazonaws.services.personalize.model.DescribeRecipeRequest)1 DescribeSolutionRequest (com.amazonaws.services.personalize.model.DescribeSolutionRequest)1 DescribeSolutionResult (com.amazonaws.services.personalize.model.DescribeSolutionResult)1 DescribeSolutionVersionRequest (com.amazonaws.services.personalize.model.DescribeSolutionVersionRequest)1 DescribeSolutionVersionResult (com.amazonaws.services.personalize.model.DescribeSolutionVersionResult)1 GetSolutionMetricsRequest (com.amazonaws.services.personalize.model.GetSolutionMetricsRequest)1 AmazonPersonalizeConnection (org.knime.cloud.aws.mlservices.nodes.personalize.AmazonPersonalizeConnection)1 AmazonConnectionInformationPortObject (org.knime.cloud.aws.util.AmazonConnectionInformationPortObject)1 CloudConnectionInformation (org.knime.cloud.core.util.port.CloudConnectionInformation)1 PortObject (org.knime.core.node.port.PortObject)1