Skip to content

Commit a0333e2

Browse files
authored
Merge pull request #76 from comet-ml/CM-2347-registry-model-count
[CM-2347]: Implement equivalent of API.get_registry_model_count
2 parents e964d77 + bb1a56c commit a0333e2

File tree

9 files changed

+97
-0
lines changed

9 files changed

+97
-0
lines changed

comet-examples/src/main/java/ml/comet/examples/RegistryModelExample.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ record = api.registerModel(updatedModel, experiment.getExperimentKey());
203203
newComment, newStages);
204204
System.out.printf("Model version details was successfully updated for: %s\n", SOME_MODEL_VERSION_UP);
205205

206+
// read the number of registered models
207+
//
208+
Optional<Integer> modelsCount = api.getRegistryModelsCount(experiment.getWorkspaceName());
209+
if (modelsCount.isPresent()) {
210+
System.out.printf("The number of registered models under workspace '%s' is: %d\n",
211+
experiment.getWorkspaceName(), modelsCount.get());
212+
} else {
213+
System.out.printf("Failed to get registered models count. The workspace '%s' doesn't exists.\n",
214+
experiment.getWorkspaceName());
215+
}
216+
206217
// delete registry model version
207218
//
208219
System.out.printf("Deleting registry model version '%s/%s:%s'.\n",

comet-java-client/src/main/java/ml/comet/experiment/CometApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ ModelDownloadInfo downloadRegistryModel(Path outputPath, String registryName, St
129129
*/
130130
List<String> getRegistryModelVersions(String registryName, String workspace);
131131

132+
/**
133+
* Returns the number of models registered in the specified workspace that is available to the current user.
134+
*
135+
* @param workspace the name of the workspace.
136+
* @return the {@code Optional} number of models registered in the specified workspace.
137+
* If workspace doesn't exist the empty {@code Optional} returned.
138+
*/
139+
Optional<Integer> getRegistryModelsCount(String workspace);
140+
132141
/**
133142
* Adds or updates notes associated with the registry model.
134143
*

comet-java-client/src/main/java/ml/comet/experiment/impl/CometApiImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import ml.comet.experiment.impl.http.ConnectionInitializer;
1515
import ml.comet.experiment.impl.rest.ExperimentModelListResponse;
1616
import ml.comet.experiment.impl.rest.ExperimentModelResponse;
17+
import ml.comet.experiment.impl.rest.RegistryModelCountResponse;
1718
import ml.comet.experiment.impl.rest.RegistryModelCreateRequest;
1819
import ml.comet.experiment.impl.rest.RegistryModelDetailsResponse;
1920
import ml.comet.experiment.impl.rest.RegistryModelItemCreateRequest;
@@ -74,6 +75,7 @@
7475
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DELETE_REGISTRY_MODEL_VERSION;
7576
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DOWNLOAD_REGISTRY_MODEL;
7677
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_FIND_EXPERIMENT_MODEL_BY_NAME;
78+
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_GET_REGISTRY_MODELS_COUNT;
7779
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_GET_REGISTRY_MODEL_DETAILS;
7880
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_GET_REGISTRY_MODEL_NOTES;
7981
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_GET_REGISTRY_MODEL_VERSIONS;
@@ -395,6 +397,22 @@ public List<String> getRegistryModelVersions(@NonNull String registryName, @NonN
395397
ArrayList::addAll);
396398
}
397399

400+
@Override
401+
public Optional<Integer> getRegistryModelsCount(@NonNull String workspace) {
402+
try {
403+
RegistryModelCountResponse countResponse = this.restApiClient
404+
.getRegistryModelsCount(workspace)
405+
.blockingGet();
406+
return Optional.of(countResponse.getRegistryModelCount());
407+
} catch (CometApiException ex) {
408+
this.logger.error(getString(FAILED_TO_GET_REGISTRY_MODELS_COUNT, workspace), ex);
409+
if (ex.getStatusCode() == 400) {
410+
return Optional.empty();
411+
}
412+
throw ex;
413+
}
414+
}
415+
398416
@Override
399417
public void updateRegistryModelNotes(
400418
@NonNull String notes, @NonNull String registryName, @NonNull String workspace) {

comet-java-client/src/main/java/ml/comet/experiment/impl/RestApiClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import ml.comet.experiment.impl.rest.MinMaxResponse;
4242
import ml.comet.experiment.impl.rest.OutputUpdate;
4343
import ml.comet.experiment.impl.rest.ParameterRest;
44+
import ml.comet.experiment.impl.rest.RegistryModelCountResponse;
4445
import ml.comet.experiment.impl.rest.RegistryModelCreateRequest;
4546
import ml.comet.experiment.impl.rest.RegistryModelCreateResponse;
4647
import ml.comet.experiment.impl.rest.RegistryModelDetailsResponse;
@@ -94,6 +95,7 @@
9495
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_METRICS;
9596
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_OUTPUT;
9697
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_PARAMETERS;
98+
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_REGISTRY_MODELS_COUNT;
9799
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_REGISTRY_MODEL_DETAILS;
98100
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_REGISTRY_MODEL_LIST;
99101
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_REGISTRY_MODEL_NOTES;
@@ -375,6 +377,12 @@ Single<RegistryModelDetailsResponse> getRegistryModelDetails(String modelName, S
375377
RegistryModelDetailsResponse.class);
376378
}
377379

380+
Single<RegistryModelCountResponse> getRegistryModelsCount(String workspaceName) {
381+
return this.singleFromSyncGetWithRetries(GET_REGISTRY_MODELS_COUNT,
382+
Collections.singletonMap(WORKSPACE_NAME, workspaceName), true,
383+
RegistryModelCountResponse.class);
384+
}
385+
378386
Single<RestApiResponse> downloadRegistryModel(
379387
final OutputStream output, String workspace, String registryName, final DownloadModelOptions options) {
380388
Map<QueryParamName, String> queryParams = downloadModelParams(workspace, registryName, options);

comet-java-client/src/main/java/ml/comet/experiment/impl/constants/ApiEndpoints.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@ public final class ApiEndpoints {
5353
public static final String DOWNLOAD_REGISTRY_MODEL = READ_API_URL + "/registry-model/item/download";
5454
public static final String GET_REGISTRY_MODEL_DETAILS = READ_API_URL + "/registry-model/details";
5555
public static final String GET_REGISTRY_MODEL_NOTES = READ_API_URL + "/registry-model/notes";
56+
57+
public static final String GET_REGISTRY_MODELS_COUNT = READ_API_URL + "/registry-model/count";
5658
}

comet-java-client/src/main/java/ml/comet/experiment/impl/resources/LogMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public class LogMessages {
9595
public static final String FAILED_TO_GET_REGISTRY_MODEL_DETAILS = "FAILED_TO_GET_REGISTRY_MODEL_DETAILS";
9696
public static final String FAILED_TO_GET_REGISTRY_MODEL_VERSIONS = "FAILED_TO_GET_REGISTRY_MODEL_VERSIONS";
9797
public static final String FAILED_TO_GET_REGISTRY_MODEL_NOTES = "FAILED_TO_GET_REGISTRY_MODEL_NOTES";
98+
public static final String FAILED_TO_GET_REGISTRY_MODELS_COUNT = "FAILED_TO_GET_REGISTRY_MODELS_COUNT";
9899
public static final String FAILED_TO_UPDATE_REGISTRY_MODEL_NOTES = "FAILED_TO_UPDATE_REGISTRY_MODEL_NOTES";
99100
public static final String FAILED_TO_UPDATE_REGISTRY_MODEL = "FAILED_TO_UPDATE_REGISTRY_MODEL";
100101
public static final String FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION = "FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ml.comet.experiment.impl.rest;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
@Data
14+
public class RegistryModelCountResponse {
15+
private int registryModelCount;
16+
}

comet-java-client/src/main/resources/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ FAILED_TO_DOWNLOAD_REGISTRY_MODEL=Failed to download registry model, reason: '%s
8080
FAILED_TO_GET_REGISTRY_MODEL_DETAILS=Failed to get details of the registry model '%s/%s', reason: '%s', sdk error code: '%s'.
8181
FAILED_TO_GET_REGISTRY_MODEL_VERSIONS=Failed to get versions of the registry model '%s/%s'.
8282
FAILED_TO_GET_REGISTRY_MODEL_NOTES=Failed to get notes of the registry model '%s/%s', reason: '%s', sdk error code: '%s'.
83+
FAILED_TO_GET_REGISTRY_MODELS_COUNT=Failed to get number of registry models under workspace '%s'.
8384
FAILED_TO_UPDATE_REGISTRY_MODEL_NOTES=Failed to update notes of the registry model '%s/%s'.
8485
FAILED_TO_UPDATE_REGISTRY_MODEL=Failed to update registry model '%s/%s' with data: '%s'.
8586
FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION=Failed to update registry model's version '%s/%s:%s' with data: '%s'.

comet-java-client/src/test/java/ml/comet/experiment/impl/CometApiTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,37 @@ public void testUpdateRegistryModelNotes_model_doesnt_exists() {
477477
COMET_API.updateRegistryModelNotes(SOME_NOTES, modelName, SHARED_EXPERIMENT.getWorkspaceName()));
478478
}
479479

480+
@Test
481+
public void testGetRegistryModelsCount() {
482+
String modelName = String.format("%s-%d", SOME_MODEL_NAME, System.currentTimeMillis());
483+
484+
// register model with defaults
485+
//
486+
registerModelWithDefaults(modelName);
487+
488+
// wait for registry model to be processed by backend
489+
//
490+
Awaitility.await("failed to get registry model")
491+
.pollInterval(1, TimeUnit.SECONDS)
492+
.atMost(60, TimeUnit.SECONDS)
493+
.until(() -> COMET_API.getRegistryModelDetails(modelName,
494+
SHARED_EXPERIMENT.getWorkspaceName()).isPresent());
495+
496+
// get models count and do assert
497+
//
498+
Optional<Integer> modelsCount = COMET_API.getRegistryModelsCount(SHARED_EXPERIMENT.getWorkspaceName());
499+
assertTrue(modelsCount.isPresent());
500+
// there could be more than one model registered from other tests
501+
assertTrue(modelsCount.get() > 1);
502+
}
503+
504+
@Test
505+
public void testGetRegistryModelsCount_not_existing_workspace() {
506+
String notExistingWorkspace = "not existing workspace";
507+
Optional<Integer> modelsCount = COMET_API.getRegistryModelsCount(notExistingWorkspace);
508+
assertFalse(modelsCount.isPresent());
509+
}
510+
480511
@Test
481512
public void testUpdateRegistryModelNotes() {
482513
String modelName = String.format("%s-%d", SOME_MODEL_NAME, System.currentTimeMillis());

0 commit comments

Comments
 (0)