You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/07/11 06:24:55 UTC

[GitHub] [beam] youngoli commented on a diff in pull request #22152: Add fhirio.Deidentify transform

youngoli commented on code in PR #22152:
URL: https://github.com/apache/beam/pull/22152#discussion_r917210607


##########
sdks/go/pkg/beam/io/fhirio/common.go:
##########
@@ -91,18 +108,53 @@ func (c *fhirStoreClientImpl) search(storePath, resourceType string, queries map
 	}
 
 	searchRequest := &healthcare.SearchResourcesRequest{}
+	fhirService := c.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir
+
 	if resourceType == "" {
-		return c.fhirService.Search(storePath, searchRequest).Do(queryParams...)
+		return fhirService.Search(storePath, searchRequest).Do(queryParams...)
 	}
-	return c.fhirService.SearchType(storePath, resourceType, searchRequest).Do(queryParams...)
+	return fhirService.SearchType(storePath, resourceType, searchRequest).Do(queryParams...)
 }
 
-func newFhirStoreClient() *fhirStoreClientImpl {
-	healthcareService, err := healthcare.NewService(context.Background(), option.WithUserAgent(UserAgent))
+func (c *fhirStoreClientImpl) deidentify(srcStorePath, dstStorePath string, deidConfig *healthcare.DeidentifyConfig) (operationResults, error) {
+	deidRequest := &healthcare.DeidentifyFhirStoreRequest{
+		Config:           deidConfig,
+		DestinationStore: dstStorePath,
+	}
+	operation, err := c.healthcareService.Projects.Locations.Datasets.FhirStores.Deidentify(srcStorePath, deidRequest).Do()
 	if err != nil {
-		panic("Failed to initialize Google Cloud Healthcare Service. Reason: " + err.Error())
+		return operationResults{}, err
+	}
+	return c.pollTilCompleteAndCollectResults(operation)
+}
+
+func (c *fhirStoreClientImpl) pollTilCompleteAndCollectResults(operation *healthcare.Operation) (operationResults, error) {
+	var err error
+	for !operation.Done {
+		time.Sleep(15 * time.Second)
+		operation, err = c.healthcareService.Projects.Locations.Datasets.Operations.Get(operation.Name).Do()

Review Comment:
   Can you explain what the purpose is of getting the `operation` again here? I'm not quite sure what's happening. Is it updating the status so the loop will detect when it's done?



##########
sdks/go/pkg/beam/io/fhirio/common.go:
##########
@@ -91,18 +108,53 @@ func (c *fhirStoreClientImpl) search(storePath, resourceType string, queries map
 	}
 
 	searchRequest := &healthcare.SearchResourcesRequest{}
+	fhirService := c.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir

Review Comment:
   Style suggestion: Instead of having to call all this manually when a fhirService is needed, you could have a helper function, so it would look like this.
   ```suggestion
   	fhirService := c.fhirService()
   ```



##########
sdks/go/test/integration/io/fhirio/fhirio_test.go:
##########
@@ -178,102 +185,91 @@ func extractResourcePathFrom(resourceLocationURL string) (string, error) {
 	return resourceLocationURL[startIdx:endIdx], nil
 }
 
-// Useful to prevent flaky results.
-func runWithBackoffRetries(t *testing.T, p *beam.Pipeline) error {
+func readTestTask(t *testing.T, s beam.Scope, testStoreInfo fhirStoreInfo) func() {
 	t.Helper()
 
-	var err error
-	for attempt := 0; attempt < len(backoffDuration); attempt++ {
-		err = ptest.Run(p)
-		if err == nil {
-			break
-		}
-		t.Logf("backoff %v after failure", backoffDuration[attempt])
-		time.Sleep(backoffDuration[attempt])
-	}
-	return err
-}
-
-func TestFhirIO_Read(t *testing.T) {
-	integration.CheckFilters(t)
-	checkFlags(t)
-
-	_, testResourcePaths, teardownFhirStore := setupFhirStoreWithData(t)
-	defer teardownFhirStore()
-
-	p, s, resourcePaths := ptest.CreateList(testResourcePaths)
-	resources, failedReads := fhirio.Read(s, resourcePaths)
-	passert.Empty(s, failedReads)
-	passert.Count(s, resources, "", len(testResourcePaths))
-
-	ptest.RunAndValidate(t, p)
-}
-
-func TestFhirIO_InvalidRead(t *testing.T) {
-	integration.CheckFilters(t)
-	checkFlags(t)
-
-	fhirStorePath, _, teardownFhirStore := setupFhirStoreWithData(t)
-	defer teardownFhirStore()
-
-	invalidResourcePath := fhirStorePath + "/fhir/Patient/invalid"
-	p, s, resourcePaths := ptest.CreateList([]string{invalidResourcePath})
-	resources, failedReads := fhirio.Read(s, resourcePaths)
+	testResources := append(testStoreInfo.resourcesPaths, testStoreInfo.path+"/fhir/Patient/invalid")

Review Comment:
   Seeing as all these tests are now going to technically be part of the same pipeline, it may be helpful to call `s.Scope` on each of them to aid debugging.



##########
sdks/go/pkg/beam/io/fhirio/common_test.go:
##########
@@ -0,0 +1,76 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package fhirio
+
+import (
+	"google.golang.org/api/healthcare/v1"

Review Comment:
   Nit: Move non-standard packages to the bottom section.



##########
sdks/go/pkg/beam/io/fhirio/common.go:
##########
@@ -91,18 +108,53 @@ func (c *fhirStoreClientImpl) search(storePath, resourceType string, queries map
 	}
 
 	searchRequest := &healthcare.SearchResourcesRequest{}
+	fhirService := c.healthcareService.Projects.Locations.Datasets.FhirStores.Fhir
+
 	if resourceType == "" {
-		return c.fhirService.Search(storePath, searchRequest).Do(queryParams...)
+		return fhirService.Search(storePath, searchRequest).Do(queryParams...)
 	}
-	return c.fhirService.SearchType(storePath, resourceType, searchRequest).Do(queryParams...)
+	return fhirService.SearchType(storePath, resourceType, searchRequest).Do(queryParams...)
 }
 
-func newFhirStoreClient() *fhirStoreClientImpl {
-	healthcareService, err := healthcare.NewService(context.Background(), option.WithUserAgent(UserAgent))
+func (c *fhirStoreClientImpl) deidentify(srcStorePath, dstStorePath string, deidConfig *healthcare.DeidentifyConfig) (operationResults, error) {
+	deidRequest := &healthcare.DeidentifyFhirStoreRequest{
+		Config:           deidConfig,
+		DestinationStore: dstStorePath,
+	}
+	operation, err := c.healthcareService.Projects.Locations.Datasets.FhirStores.Deidentify(srcStorePath, deidRequest).Do()
 	if err != nil {
-		panic("Failed to initialize Google Cloud Healthcare Service. Reason: " + err.Error())
+		return operationResults{}, err
+	}
+	return c.pollTilCompleteAndCollectResults(operation)
+}
+
+func (c *fhirStoreClientImpl) pollTilCompleteAndCollectResults(operation *healthcare.Operation) (operationResults, error) {
+	var err error
+	for !operation.Done {
+		time.Sleep(15 * time.Second)

Review Comment:
   My first thought here is that some kind of falloff retry would be better, with increasing wait times instead of starting at a full 15 seconds. Although I suppose that depends on the typical wait time these operations can expect to take.



##########
sdks/go/pkg/beam/io/fhirio/deidentify.go:
##########
@@ -0,0 +1,93 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package fhirio provides an API for reading and writing resources to Google
+// Cloud Healthcare Fhir stores.
+// Experimental.
+package fhirio
+
+import (
+	"context"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	"google.golang.org/api/healthcare/v1"
+)
+
+func init() {
+	register.DoFn3x0[context.Context, string, func(string)]((*deidentifyFn)(nil))
+	register.Emitter1[string]()
+}
+
+type deidentifyFn struct {
+	fnCommonVariables
+	DestinationStorePath  string
+	DeidentifyConfig      *healthcare.DeidentifyConfig
+	operationErrorCount   beam.Counter
+	operationSuccessCount beam.Counter
+}
+
+func (fn deidentifyFn) String() string {
+	return "deidentifyFn"
+}
+
+func (fn *deidentifyFn) Setup() {
+	fn.fnCommonVariables.setup(fn.String())
+	fn.operationErrorCount = beam.NewCounter(fn.String(), operationErrorCounterName)
+	fn.operationSuccessCount = beam.NewCounter(fn.String(), operationSuccessCounterName)
+}
+
+func (fn *deidentifyFn) ProcessElement(ctx context.Context, srcStorePath string, emitDstStore func(string)) {
+	result, err := executeAndRecordLatency(ctx, &fn.latencyMs, func() (operationResults, error) {
+		return fn.client.deidentify(srcStorePath, fn.DestinationStorePath, fn.DeidentifyConfig)
+	})
+	if err != nil {
+		log.Warnf(ctx, "Deidentify operation failed. Reason: %v", err)
+		fn.operationErrorCount.Inc(ctx, 1)
+		return
+	}
+
+	fn.operationSuccessCount.Inc(ctx, 1)
+	fn.resourcesSuccessCount.Inc(ctx, result.Successes)
+	fn.resourcesErrorCount.Inc(ctx, result.Failures)
+	emitDstStore(fn.DestinationStorePath)
+}
+
+// Deidentify transform de-identifies sensitive data in resources located in a
+// Google Cloud FHIR store. It receives a source and destination store paths as
+// well as de-identification configuration (
+// https://cloud.google.com/healthcare-api/docs/reference/rest/v1/DeidentifyConfig#FhirConfig).
+// It performs de-identification on the source store using the provided
+// configuration and applies the result in the destination store. It outputs a
+// PCollection containing the destination store path if de-identification was
+// performed successfully, otherwise it returns an empty PCollection.
+// See: https://cloud.google.com/healthcare-api/docs/how-tos/fhir-deidentify
+func Deidentify(s beam.Scope, srcStore, dstStore string, config *healthcare.DeidentifyConfig) beam.PCollection {
+	s = s.Scope("fhirio.Deidentify")
+	return deidentify(s, srcStore, dstStore, config, nil)
+}
+
+func deidentify(s beam.Scope, srcStore, dstStore string, config *healthcare.DeidentifyConfig, client fhirStoreClient) beam.PCollection {
+	return beam.ParDo(
+		s,
+		&deidentifyFn{
+			fnCommonVariables:    fnCommonVariables{client: client},
+			DestinationStorePath: dstStore,
+			DeidentifyConfig:     config,
+		},
+		beam.Create(s, srcStore),

Review Comment:
   Nit: This implementation is functional, but if the intention is really to have srcStore and dstStore only set once per transform during pipeline construction, then I'd recommend making them both fields in the Fn, and using beam.Impulse to send an empty []byte as an input. This is the standard approach for writing Sources (i.e. transforms that take no input PCollections but produce outputs).



##########
sdks/go/pkg/beam/io/fhirio/deidentify.go:
##########
@@ -0,0 +1,93 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package fhirio provides an API for reading and writing resources to Google
+// Cloud Healthcare Fhir stores.
+// Experimental.
+package fhirio
+
+import (
+	"context"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	"google.golang.org/api/healthcare/v1"
+)
+
+func init() {
+	register.DoFn3x0[context.Context, string, func(string)]((*deidentifyFn)(nil))
+	register.Emitter1[string]()
+}
+
+type deidentifyFn struct {
+	fnCommonVariables
+	DestinationStorePath  string
+	DeidentifyConfig      *healthcare.DeidentifyConfig
+	operationErrorCount   beam.Counter
+	operationSuccessCount beam.Counter
+}
+
+func (fn deidentifyFn) String() string {
+	return "deidentifyFn"
+}
+
+func (fn *deidentifyFn) Setup() {
+	fn.fnCommonVariables.setup(fn.String())
+	fn.operationErrorCount = beam.NewCounter(fn.String(), operationErrorCounterName)
+	fn.operationSuccessCount = beam.NewCounter(fn.String(), operationSuccessCounterName)
+}
+
+func (fn *deidentifyFn) ProcessElement(ctx context.Context, srcStorePath string, emitDstStore func(string)) {
+	result, err := executeAndRecordLatency(ctx, &fn.latencyMs, func() (operationResults, error) {
+		return fn.client.deidentify(srcStorePath, fn.DestinationStorePath, fn.DeidentifyConfig)
+	})
+	if err != nil {
+		log.Warnf(ctx, "Deidentify operation failed. Reason: %v", err)
+		fn.operationErrorCount.Inc(ctx, 1)
+		return
+	}
+
+	fn.operationSuccessCount.Inc(ctx, 1)
+	fn.resourcesSuccessCount.Inc(ctx, result.Successes)
+	fn.resourcesErrorCount.Inc(ctx, result.Failures)
+	emitDstStore(fn.DestinationStorePath)
+}
+
+// Deidentify transform de-identifies sensitive data in resources located in a
+// Google Cloud FHIR store. It receives a source and destination store paths as
+// well as de-identification configuration (
+// https://cloud.google.com/healthcare-api/docs/reference/rest/v1/DeidentifyConfig#FhirConfig).
+// It performs de-identification on the source store using the provided
+// configuration and applies the result in the destination store. It outputs a
+// PCollection containing the destination store path if de-identification was
+// performed successfully, otherwise it returns an empty PCollection.
+// See: https://cloud.google.com/healthcare-api/docs/how-tos/fhir-deidentify
+func Deidentify(s beam.Scope, srcStore, dstStore string, config *healthcare.DeidentifyConfig) beam.PCollection {

Review Comment:
   My first thought with this approach to Deidentify is that it lacks the ability to configure this operation at runtime, for example taking a KV of srcStore and dstStore as an input. I'm not sure how likely a use-case that is for users though. Is there a reason to avoid that approach?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org