Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge Products #1569

Merged
merged 13 commits into from
Sep 6, 2019
4 changes: 2 additions & 2 deletions vision/product-search/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.52.0</version>
<version>1.88.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.52.0</version>
<version>1.88.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@

package com.example.vision;

import com.google.api.gax.longrunning.OperationFuture;

import com.google.cloud.vision.v1.BatchOperationMetadata;
import com.google.cloud.vision.v1.LocationName;
import com.google.cloud.vision.v1.Product;
import com.google.cloud.vision.v1.ProductName;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.cloud.vision.v1.ProductSetPurgeConfig;
import com.google.cloud.vision.v1.PurgeProductsRequest;

import com.google.protobuf.Empty;

import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
Expand Down Expand Up @@ -135,6 +144,50 @@ public static void removeProductFromProductSet(
}
// [END vision_product_search_remove_product_from_product_set]

// [START vision_product_search_purge_products_in_product_set]
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
/**
* Delete all products in a product set.
*
* @param projectId - Id of the project.
* @param location - Region name.
* @param productSetId - Id of the product set.
* @param force - Perform the purge only when force is set to True.
* @throws Exception - any error.
*/
public static void purgeProductsInProductSet(
String projectId, String location, String productSetId, boolean force)
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
throws Exception {
try (ProductSearchClient client = ProductSearchClient.create()) {

String parent = LocationName.format(projectId, location);
ProductSetPurgeConfig productSetPurgeConfig = ProductSetPurgeConfig
.newBuilder()
.setProductSetId(productSetId)
.build();

PurgeProductsRequest req = PurgeProductsRequest
.newBuilder()
.setParent(parent)
.setProductSetPurgeConfig(productSetPurgeConfig)
// The operation is irreversible and removes multiple products.
// The user is required to pass in force=True to actually perform the
// purge.
// If force is not set to True, the service raises an exception.
.setForce(force)
.build();

//TODO: once its supported in all regions, will change it to 60 sec.
// testing method with region asia-east1 seems bit slower than normal.
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved

OperationFuture<Empty, BatchOperationMetadata> response = client.purgeProductsAsync(req);
response.getPollingFuture().get(90, TimeUnit.MINUTES);
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved

System.out.println("Products removed from product set.");
}

}
// [END vision_product_search_purge_products_in_product_set]

public static void main(String[] args) throws Exception {
ProductInProductSetManagement productInProductSetManagement =
new ProductInProductSetManagement();
Expand All @@ -157,6 +210,11 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
removeProductFromProductSetParser.addArgument("productId");
removeProductFromProductSetParser.addArgument("productSetId");

Subparser purgeProductsInProductSetParser =
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
subparsers.addParser("purge_products_in_product_set");
purgeProductsInProductSetParser.addArgument("productSetId");
purgeProductsInProductSetParser.addArgument("force");

String projectId = System.getenv("PROJECT_ID");
String computeRegion = System.getenv("REGION_NAME");

Expand All @@ -172,7 +230,13 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
}
if (ns.get("command").equals("remove_product_from_product_set")) {
removeProductFromProductSet(
projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId"));
projectId, computeRegion, ns.getString("productId"), ns.getString("productSetId"));
}
System.out.println(ns.getAttrs());
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
if (ns.get("command").equals("purge_products_in_product_set")) {
purgeProductsInProductSet(
projectId, computeRegion, ns.getString("productSetId"),
Boolean.parseBoolean(ns.getString("force")));
}

} catch (ArgumentParserException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package com.example.vision;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.vision.v1.LocationName;
import com.google.cloud.vision.v1.Product;
import com.google.cloud.vision.v1.Product.KeyValue;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.cloud.vision.v1.PurgeProductsRequest;
import com.google.protobuf.FieldMask;

import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
Expand Down Expand Up @@ -213,6 +217,40 @@ public static void deleteProduct(String projectId, String computeRegion, String
}
// [END vision_product_search_delete_product]

// [START vision_product_search_purge_orphan_products]
/**
* Delete the product and all its reference images.
*
* @param projectId - Id of the project.
* @param computeRegion - A compute region name.
* @param force - Perform the purge only when force is set to True.
* @throws Exception - on I/O and API errors.
*/
public static void purgeOrphanProducts(String projectId, String computeRegion, boolean force)
throws Exception {
try (ProductSearchClient client = ProductSearchClient.create()) {

String parent = LocationName.format(projectId, computeRegion);

// The purge operation is async.
PurgeProductsRequest req = PurgeProductsRequest
.newBuilder()
.setDeleteOrphanProducts(true)
.setForce(force)
.setParent(parent)
.build();

OperationFuture response = client.purgeProductsAsync(req);

//TODO: once its supported in all regions, will change it to 60 sec.
// testing method with region asia-east1 seems bit slower than normal.
response.getPollingFuture().get(90, TimeUnit.SECONDS);

System.out.println("Orphan products deleted.");
}
}
// [END vision_product_search_purge_orphan_products]

public static void main(String[] args) throws Exception {
ProductManagement productManagement = new ProductManagement();
productManagement.argsHelper(args, System.out);
Expand Down Expand Up @@ -241,6 +279,9 @@ public void argsHelper(String[] args, PrintStream out) throws Exception {
Subparser deleteProductParser = subparsers.addParser("delete_product");
deleteProductParser.addArgument("productId");

Subparser purgeOrphanProductsParser = subparsers.addParser("purge_orphan_products");
munkhuushmgl marked this conversation as resolved.
Show resolved Hide resolved
purgeOrphanProductsParser.addArgument("force");

String projectId = System.getenv("PROJECT_ID");
String computeRegion = System.getenv("REGION_NAME");

Expand Down Expand Up @@ -268,7 +309,9 @@ public void argsHelper(String[] args, PrintStream out) throws Exception {
if (ns.get("command").equals("delete_product")) {
deleteProduct(projectId, computeRegion, ns.getString("productId"));
}

if (ns.get("command").equals("purge_orphan_products")) {
purgeOrphanProducts(projectId, computeRegion, Boolean.parseBoolean(ns.getString("force")));
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.example.vision;

import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageName;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.cloud.vision.v1.ReferenceImage;

Expand Down Expand Up @@ -123,7 +125,7 @@ public static void getReferenceImage(

// Get the full path of the reference image.
String formattedName =
ProductSearchClient.formatImageName(
ImageName.format(
projectId, computeRegion, productId, referenceImageId);
// Get complete detail of the reference image.
ReferenceImage image = client.getReferenceImage(formattedName);
Expand Down Expand Up @@ -158,7 +160,7 @@ public static void deleteReferenceImage(

// Get the full path of the reference image.
String formattedName =
ProductSearchClient.formatImageName(
ImageName.format(
projectId, computeRegion, productId, referenceImageId);
// Delete the reference image.
client.deleteReferenceImage(formattedName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,28 @@ public void testRemoveProductFromProductSet() throws Exception {
got = bout.toString();
assertThat(got).doesNotContain(PRODUCT_ID);
}

@Test
public void testPurgeProductsInProductSet() throws Exception {
// Act
ProductInProductSetManagement.addProductToProductSet(
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_SET_ID);
ProductManagement.listProducts(
PROJECT_ID, COMPUTE_REGION);

// Assert
String got = bout.toString();
assertThat(got).contains(PRODUCT_ID);

bout.reset();
ProductInProductSetManagement.purgeProductsInProductSet(
PROJECT_ID, COMPUTE_REGION, PRODUCT_SET_ID, true);

ProductManagement.listProducts(
PROJECT_ID, COMPUTE_REGION);

// Assert
got = bout.toString();
assertThat(got).doesNotContain(PRODUCT_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,26 @@ public void testUpdateProductLabels() throws Exception {
assertThat(got).contains(KEY);
assertThat(got).contains(VALUE);
}

@Test
public void testPurgeOrphanProducts() throws Exception {
// Act
ProductManagement.createProduct(
PROJECT_ID, COMPUTE_REGION, PRODUCT_ID, PRODUCT_DISPLAY_NAME, PRODUCT_CATEGORY);
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);

// Assert
String got = bout.toString();
assertThat(got).contains(PRODUCT_ID);

bout.reset();

// Act
ProductManagement.purgeOrphanProducts(PROJECT_ID, COMPUTE_REGION, true);

// Assert
got = bout.toString();
ProductManagement.listProducts(PROJECT_ID, COMPUTE_REGION);
assertThat(got).doesNotContain(PRODUCT_ID);
}
}