-
Notifications
You must be signed in to change notification settings - Fork 16
chore(samples): Retail Tutorials. Events (write, rejoin, purge) #303
chore(samples): Retail Tutorials. Events (write, rejoin, purge) #303
Conversation
Warning: This pull request is touching the following templated files:
|
Here is the summary of changes. You are about to add 3 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only reviewed the first sample. but similar changes should probably be made throughout
env_vars: { | ||
key: "JOB_TYPE" | ||
value: "tutorials-samples" | ||
} | ||
|
||
# TODO: remove this after we've migrated all tests and scripts | ||
env_vars: { | ||
key: "PROJECT_NUMBER" | ||
value: "779844219229" | ||
} | ||
|
||
env_vars: { | ||
key: "PROJECT_ID" | ||
value: "java-docs-samples-testing" | ||
} | ||
|
||
env_vars: { | ||
key: "GOOGLE_APPLICATION_CREDENTIALS" | ||
value: "secret_manager/java-docs-samples-service-account" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general we usually try to avoid putting secrets here and instead put them in secret-manager.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue is still open.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in secret manager. This just configures the env var for ADC
samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java
Outdated
Show resolved
Hide resolved
public static void callPurgeUserEvents() | ||
throws IOException, ExecutionException, InterruptedException { | ||
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation = | ||
UserEventServiceClient.create().purgeUserEventsAsync(getPurgeUserEventRequest()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clients should be re-used and show off a try-with: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md#client-initialization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
|
||
System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest); | ||
|
||
return purgeUserEventsRequest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to return the operation name to assert it in the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the style guide:
Snippet methods should specify a return type of void and avoid returning any value wherever possible. Instead, show the user how to interact with a returned object programmatically by printing some example attributes to the console.
Assert the behavior is correct from the output to console, since that is what the user will use to verify the output is correct.
// [START retail_purge_user_event] | ||
|
||
/* | ||
* Import user events into a catalog from inline source using Retail API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment does not look correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
// [START retail_rejoin_user_event] | ||
|
||
/* | ||
* Import user events into a catalog from inline source using Retail API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment not correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
// [START retail_write_user_event] | ||
|
||
/* | ||
* Import user events into a catalog from inline source using Retail API |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
|
||
System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest); | ||
|
||
return purgeUserEventsRequest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the style guide:
Snippet methods should specify a return type of void and avoid returning any value wherever possible. Instead, show the user how to interact with a returned object programmatically by printing some example attributes to the console.
Assert the behavior is correct from the output to console, since that is what the user will use to verify the output is correct.
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); | ||
String visitorId = "test_visitor_id"; | ||
|
||
writeUserEvent(visitorId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this included here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is something that is used for testing? It doesn't need to be included in main, the test should set whatever resources it needs on the testing side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the visitor Id is a required value for the writeUserEvent method and is a filter value for the purgeUserEvent method using filtering
So it sud be here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed constant visitorId value to random generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's some misunderstand about the Method Structure:
The "main" method should just be a collection of inputs the user would need to provide to run the sample. It should use examples that the user can edit and replace with their own data to quickly and easily run the sample. We shouldn't need to rely on it executing for the testing - we should rely directly on the snippet method instead.
It should essentially look like this:
public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String defaultCatalog =
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
// This should be a comment describing where the visitor id is or a link to a different sample if it's created in that sample
String visitorId = "my-visitor-id";
callPurgeUserEvents(visitorId, defaultCatalog);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean projectId should have placeholder value? E.g my-project-id ?
If I do so, the build will fail, and in github CI checks fails also.
visitorId can have any String value, that's okay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should have a placeholder value - it's failing because we're using the main
for testing instead of calling callPurgeUserEvents
directly for testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
private static PurgeUserEventsRequest getPurgeUserEventRequest( | ||
String visitorId, String defaultCatalog) { | ||
PurgeUserEventsRequest purgeUserEventsRequest = | ||
PurgeUserEventsRequest.newBuilder() | ||
// TO CHECK ERROR HANDLING SET INVALID FILTER HERE: | ||
.setFilter(String.format("visitorId=\"%s\"", visitorId)) | ||
.setParent(defaultCatalog) | ||
.setForce(true) | ||
.build(); | ||
|
||
System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest); | ||
|
||
return purgeUserEventsRequest; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this needs to be in a separate function - it's only used once and should just be declared inline before it's used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, inlined it.
env_vars: { | ||
key: "JOB_TYPE" | ||
value: "tutorials-samples" | ||
} | ||
|
||
# TODO: remove this after we've migrated all tests and scripts | ||
env_vars: { | ||
key: "PROJECT_NUMBER" | ||
value: "779844219229" | ||
} | ||
|
||
env_vars: { | ||
key: "PROJECT_ID" | ||
value: "java-docs-samples-testing" | ||
} | ||
|
||
env_vars: { | ||
key: "GOOGLE_APPLICATION_CREDENTIALS" | ||
value: "secret_manager/java-docs-samples-service-account" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This issue is still open.
@Before | ||
public void setUp() throws IOException, InterruptedException, ExecutionException { | ||
Process exec = | ||
Runtime.getRuntime().exec("mvn compile exec:java -Dexec.mainClass=events.PurgeUserEvent"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we running this with Runtime.getRuntime()? Can't we just call the snippet function directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To check the response, we should get output from the console and assert then.
This approach is working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to avoid exec and just capture the stdout directly. Please see this example for the standard way we do it: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/storage/cloud-client/src/test/java/com/example/storage/QuickstartSampleIT.java#L47-L67
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the command the user will use to ru the code sample, isn't it better to repeat the user actions here in the test, to simulate the command running and the checking the output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically, main is just a stub to make it easier for the user to provide input. That input should be virtually identical to args needed to run the method directly. In general we try to avoid using environment variables to configure snippets, because it's often easier for users to just edit the sample directly and run in whichever way is most comfortable for them (especially for java, it's not always from a terminal).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still open.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed test logic.
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation = | ||
userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest); | ||
|
||
System.out.printf("The purge operation was started: %s%n", purgeOperation.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: are we sure we only want to show the user starting this operation, and not waiting for the results?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This operation is running from several hours up to 3 Days, so we will only show the operation Id as a proof, that it was started
@Before | ||
public void setUp() throws IOException, InterruptedException, ExecutionException { | ||
Process exec = | ||
Runtime.getRuntime().exec("mvn compile exec:java -Dexec.mainClass=events.PurgeUserEvent"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still open.
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); | ||
String visitorId = "test_visitor_id"; | ||
|
||
writeUserEvent(visitorId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should have a placeholder value - it's failing because we're using the main
for testing instead of calling callPurgeUserEvents
directly for testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
marking approved, but please note this comment chain about putting placeholders for the user in "main": #303 (comment)
This is generally a preferred approach because it doesn't encourage users to set environment variables, which can be tricky in some environments.
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> ☕️