Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Added the ability to get the status of a compute job
Browse files Browse the repository at this point in the history
- Added tests
  • Loading branch information
r-marques committed Oct 7, 2020
1 parent 39b4503 commit 254842b
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/io/keyko/nevermined/api/AssetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.gateway.ComputeStatus;
import io.keyko.nevermined.models.metadata.SearchResult;
import io.keyko.nevermined.models.service.AuthConfig;
import io.keyko.nevermined.models.service.ProviderConfig;
Expand Down Expand Up @@ -369,4 +370,15 @@ public interface AssetsAPI {
List<ComputeLogs> getComputeLogs(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException;

/**
* Get the status for the compute job with executionId and serviceAgreementId
* @param serviceAgreementId The service agreement id for the compute service
* @param executionId The execution id of the compute job
* @param consumerAddress The address of the consumer that executed the compute job
* @return The current status of the compute job
* @throws ServiceException ServiceException
*/
ComputeStatus getComputeStatus(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException;

}
7 changes: 7 additions & 0 deletions src/main/java/io/keyko/nevermined/api/impl/AssetsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.gateway.ComputeStatus;
import io.keyko.nevermined.models.metadata.SearchResult;
import io.keyko.nevermined.models.service.AuthConfig;
import io.keyko.nevermined.models.service.ProviderConfig;
Expand Down Expand Up @@ -75,6 +76,12 @@ public List<ComputeLogs> getComputeLogs(String serviceAgreementId, String execut
return neverminedManager.getComputeLogs(serviceAgreementId, executionId, consumerAddress, providerConfig);
}

@Override
public ComputeStatus getComputeStatus(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException {
return neverminedManager.getComputeStatus(serviceAgreementId, executionId, consumerAddress, providerConfig);
}

@Override
public DDO resolve(DID did) throws DDOException {
return neverminedManager.resolveDID(did);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/keyko/nevermined/external/GatewayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,42 @@ public static List<ComputeLogs> getComputeLogs(String serviceEndpoint, String co
return logs;
}

/**
* Calls a gateway endpoint to get the compute status
*
* @param serviceEndpoint the gateway service endpoint
* @param consumerAddress the address of the consumer of the compute to the data job
* @param signature the signature of the executionId
* @return the current status of the compute job.
* @throws ServiceException ServiceException
*/
public static ComputeStatus getComputeStatus(String serviceEndpoint, String consumerAddress, String signature)
throws ServiceException {

HttpResponse response;
ComputeStatus computeStatus;
Map<String, String> headers = new HashMap<>();
headers.put(HEADER_CONSUMER_ADDRESS, consumerAddress);
headers.put(HEADER_SIGNATURE, signature);

try {
response = HttpHelper.httpClientGet(serviceEndpoint, headers);
} catch (HttpException e) {
log.error("Exception getting the compute status: " + e.getMessage());
throw new ServiceException("Exception getting the compute status", e);
}

if (response.getStatusCode() != 200 && response.getStatusCode() != 201) {
log.error("Unable to get the compute status: " + response.toString());
throw new ServiceException("Unable to get compute status: " + response.toString());
}

try {
computeStatus = ComputeStatus.fromJSON(new TypeReference<>() {}, response.getBody());
} catch (IOException e) {
log.error("Exception parsing the compute status: " + e.getMessage());
throw new ServiceException("Unable to parse status", e);
}
return computeStatus;
}
}
26 changes: 26 additions & 0 deletions src/main/java/io/keyko/nevermined/manager/NeverminedManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.gateway.ComputeStatus;
import io.keyko.nevermined.models.gateway.ExecuteService;
import io.keyko.nevermined.models.service.*;
import io.keyko.nevermined.models.service.types.*;
Expand Down Expand Up @@ -1137,6 +1138,31 @@ public List<ComputeLogs> getComputeLogs(String serviceAgreementId, String execut
return GatewayService.getComputeLogs(serviceEndpoint, consumerAddress, signature);
}

/**
* Generates the service endpoint and signature and calls the gateway to get the status of a compute job
*
* @param serviceAgreementId The agreement id for the compute to the data
* @param executionId The id of the compute job
* @param consumerAddress The address of the consumer of the compute to the data job
* @param providerConfig The configuration of the provider.
* @return The current status of the compute job.
* @throws ServiceException Service Exception
*/
public ComputeStatus getComputeStatus(String serviceAgreementId, String executionId, String consumerAddress,
ProviderConfig providerConfig) throws ServiceException {
String serviceEndpoint = providerConfig.getAccessEndpoint()
.replace("/access", "/compute/status/");
serviceEndpoint += serviceAgreementId + "/" + executionId;
String signature;
try {
signature = generateSignature(executionId);
} catch (IOException | CipherException e) {
log.error("Exception generating signature: ", e.getMessage());
throw new ServiceException("Unable to generate signature", e);
}
return GatewayService.getComputeStatus(serviceEndpoint, consumerAddress, signature);
}


// TODO: to be implemented
public Order getOrder(String orderId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.keyko.nevermined.models.gateway;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.keyko.nevermined.models.AbstractModel;
import io.keyko.nevermined.models.DID;
import io.keyko.nevermined.models.FromJsonToModel;

import java.util.List;
import java.util.Optional;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ComputeStatus extends AbstractModel implements FromJsonToModel {
@JsonProperty
public String startedAt;

@JsonProperty
public String finishedAt;

@JsonProperty
public String status;

@JsonProperty
public DID did;

@JsonProperty
public List<PodStatus> pods;
}
21 changes: 21 additions & 0 deletions src/main/java/io/keyko/nevermined/models/gateway/PodStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.keyko.nevermined.models.gateway;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.keyko.nevermined.models.AbstractModel;
import io.keyko.nevermined.models.FromJsonToModel;

@JsonIgnoreProperties(ignoreUnknown = true)
public class PodStatus extends AbstractModel implements FromJsonToModel {
@JsonProperty
public String startedAt;

@JsonProperty
public String finishedAt;

@JsonProperty
public String podName;

@JsonProperty
public String status;
}
12 changes: 12 additions & 0 deletions src/test/java/io/keyko/nevermined/api/AssetsApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.keyko.nevermined.models.asset.AssetMetadata;
import io.keyko.nevermined.models.asset.OrderResult;
import io.keyko.nevermined.models.gateway.ComputeLogs;
import io.keyko.nevermined.models.gateway.ComputeStatus;
import io.keyko.nevermined.models.service.ProviderConfig;
import io.keyko.nevermined.models.service.Service;
import io.keyko.nevermined.models.service.types.ComputingService;
Expand Down Expand Up @@ -303,6 +304,17 @@ public void testComputeLogs() throws Exception {
assertNotNull(computeLogs);
}

// Ignore test until e2e compute elements are automated
@Ignore
@Test
public void testComputeStatus() throws Exception {
ComputeStatus computeStatus = neverminedAPI.getAssetsAPI()
.getComputeStatus("1d587c0143ac4400b8776178c57946f301fc53f966f64a65911fa53c8b497391",
"nevermined-compute-8tfvn", neverminedAPI.getMainAccount().address,
providerConfig);
assertNotNull(computeStatus);
}

// TODO: Automate the Compute use cases e2e
// Ignoring test until the e2e compute components are automated
@Ignore
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/io/keyko/nevermined/models/ComputeStatusTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.keyko.nevermined.models;

import com.fasterxml.jackson.core.type.TypeReference;
import io.keyko.nevermined.models.gateway.ComputeStatus;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;

public class ComputeStatusTest {

private static String COMPUTE_STATUS_SAMPLE = "src/test/resources/examples/compute-status.json";
private static String COMPUTE_STATUS_CONTENT;

@BeforeClass
public static void setUp() throws Exception {
COMPUTE_STATUS_CONTENT = new String(Files.readAllBytes(Paths.get(COMPUTE_STATUS_SAMPLE)));
}
@Test
public void fromJsonTest() throws Exception {
ComputeStatus computeStatus = ComputeStatus.fromJSON(new TypeReference<>() {}, COMPUTE_STATUS_CONTENT);

assertEquals("did:nv:7ce18efb179b65a1ca4b1598ad4d1fb4107c4fe51336e2871d3f7ae208873fd4",
computeStatus.did.getDid());
assertEquals("2020-09-18T12:24:33+00:00Z", computeStatus.startedAt);
assertEquals("2020-09-18T12:24:50+00:00Z", computeStatus.finishedAt);
assertEquals("Succeeded", computeStatus.status);
assertEquals(3, computeStatus.pods.size());

assertEquals("2020-09-18T12:24:49+00:00Z", computeStatus.pods.get(0).finishedAt);
assertEquals("2020-09-18T12:24:44+00:00Z", computeStatus.pods.get(0).startedAt);
assertEquals("publishing", computeStatus.pods.get(0).podName);
assertEquals("Succeeded", computeStatus.pods.get(0).status);
}
}
26 changes: 26 additions & 0 deletions src/test/resources/examples/compute-status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"did": "did:nv:7ce18efb179b65a1ca4b1598ad4d1fb4107c4fe51336e2871d3f7ae208873fd4",
"finishedAt": "2020-09-18T12:24:50+00:00Z",
"pods": [
{
"finishedAt": "2020-09-18T12:24:49+00:00Z",
"podName": "publishing",
"startedAt": "2020-09-18T12:24:44+00:00Z",
"status": "Succeeded"
},
{
"finishedAt": "2020-09-18T12:24:42+00:00Z",
"podName": "transformation",
"startedAt": "2020-09-18T12:24:39+00:00Z",
"status": "Succeeded"
},
{
"finishedAt": "2020-09-18T12:24:38+00:00Z",
"podName": "configurator",
"startedAt": "2020-09-18T12:24:33+00:00Z",
"status": "Succeeded"
}
],
"startedAt": "2020-09-18T12:24:33+00:00Z",
"status": "Succeeded"
}

0 comments on commit 254842b

Please sign in to comment.