Skip to content

Cleanup DLP Risk Analysis snippets #2069

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

Merged
merged 9 commits into from
Feb 19, 2020
Merged

Cleanup DLP Risk Analysis snippets #2069

merged 9 commits into from
Feb 19, 2020

Conversation

shubha-rajan
Copy link
Contributor

Working towards #1479
This ended up being a larger PR than expected. I can split it up if needed.

@shubha-rajan shubha-rajan requested review from kurtisvg and a team February 13, 2020 22:13
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 13, 2020
@lesv
Copy link
Contributor

lesv commented Feb 13, 2020

Please fix compile problems

@shubha-rajan
Copy link
Contributor Author

shubha-rajan commented Feb 14, 2020

Please fix compile problems

build failures are a result of broken tests that were removed in #2062 and #2065. Once those are merged in, everything should pass.

ProjectSubscriptionName.of(projectId, subscriptionId);

MessageReceiver handleMessage =
(PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably too long for a lamba - can you surface it out into a full function? I think within the class is still fine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure of how to do this when the lambda used variables outside of the scope of the function, like dlpJob and done. handleMessage needs to be callable so I need to return a function. I'm not sure if there's a way to build functions using closures in Java? I ended up nesting the lambda within another function, and passing the DlpJob and Future to that function in in c634313, but that seems more complicated and less readable to me? Thoughts?

// Retrieve completed job status
DlpJob completedJob =
dlpServiceClient.getDlpJob(
GetDlpJobRequest.newBuilder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pull the request build into a separate var/line? I think that will actually make this shorter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 8546690

// Get the result and parse through and process the information
CategoricalStatsResult result =
completedJob.getRiskDetails().getCategoricalStatsResult();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

CategoricalStatsResult result =
completedJob.getRiskDetails().getCategoricalStatsResult();

for (CategoricalStatsHistogramBucket bucket :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would pull result.getValueFrequencyHistogramBucketsList() into it's own line too (still 2 lines, but easier to read)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same for a couple of statements below, but up to you)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in e6d4f91

String tableId = "your-bigquery-table-id";
String topicId = "pub-sub-topic";
String subscriptionId = "pub-sub-subscription";
List<String> quasiIdColumns = Arrays.asList("name", "age", "zip_code", "...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be inline - not required for testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in 8546690

ProjectSubscriptionName.of(projectId, subscriptionId);

MessageReceiver handleMessage =
(PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long lamba again

KMapEstimationResult kmapResult = completedJob.getRiskDetails().getKMapEstimationResult();

for (KMapEstimationHistogramBucket result : kmapResult.getKMapEstimationHistogramList()) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 92ea643

.getQuasiIdsValuesList()
.stream()
.map(
v -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment on this function would be helpful.

nit: Does there have to be a new line before v -> { (might be a java format thing, just looks weird to me)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed the weird formatting thing, and also rewrote this part to be a bit clearer and more consistent with other samples in e6d4f91. I can also go in and comment on it if that's still necessary.

String topicId,
String subscriptionId,
String sensitiveAttribute,
List<String> quasiIds) throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sensitiveAttribute, quasiIds inlined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 82c1f07

ProjectSubscriptionName.of(projectId, subscriptionId);

MessageReceiver handleMessage =
(PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long lambda

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my previous comment about the lambda. Also, this lambda is also used in the Inspect snippets, so if we change it here, we should probably change it there as well.

@googlebot googlebot added cla: no This human has *not* signed the Contributor License Agreement. and removed cla: yes This human has signed the Contributor License Agreement. labels Feb 15, 2020
@shubha-rajan shubha-rajan force-pushed the dlp-snippets-refactor-4 branch from 9d2733f to b8e00a5 Compare February 15, 2020 05:23
@googlebot googlebot added cla: yes This human has signed the Contributor License Agreement. and removed cla: no This human has *not* signed the Contributor License Agreement. labels Feb 15, 2020
@shubha-rajan shubha-rajan force-pushed the dlp-snippets-refactor-4 branch from b8e00a5 to 0df96fc Compare February 15, 2020 05:26
@GoogleCloudPlatform GoogleCloudPlatform deleted a comment from googlebot Feb 15, 2020
@GoogleCloudPlatform GoogleCloudPlatform deleted a comment from googlebot Feb 15, 2020
@shubha-rajan shubha-rajan force-pushed the dlp-snippets-refactor-4 branch from 0df96fc to c634313 Compare February 15, 2020 06:19
@shubha-rajan
Copy link
Contributor Author

Presubmit tests are going to fail because of linting errors in other files outside of this PR. Those fixes are handled in #2083, since we probably don't want all of those changes in this PR

@shubha-rajan shubha-rajan force-pushed the dlp-snippets-refactor-4 branch from 92ea643 to 673fec4 Compare February 18, 2020 22:40
throw new IllegalArgumentException("The numbers of quasi-IDs and infoTypes must be equal!");
}

ArrayList<TaggedField> taggedFields =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Does this need to be an ArrayList or can it be just a List?

}

ArrayList<TaggedField> taggedFields =
IntStream.range(0, quasiIds.size())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This might just be a case where it is easier to read as a for loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced with a for loop in 0e72a97

@kurtisvg
Copy link
Contributor

forcing merge - (broken tests tracking in #1479)

@kurtisvg kurtisvg merged commit c8ef8a5 into master Feb 19, 2020
@kurtisvg kurtisvg deleted the dlp-snippets-refactor-4 branch February 19, 2020 21:16
Sita04 pushed a commit that referenced this pull request Feb 7, 2023
* refactored Risk analysis samples

* updated inspect tests and readme

* moved parameters to inline, cleaned up files, reformatted for consistency

* replaced iterators with intstream in RiskAnalysisKMap

* moved message receiver lambda to its own method

* ran code formatter

* refactored long messageReciever lambda

* rewrote lambda for pubsub message receiver

* replaced int stream with for loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants