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

Add Cloud Healthcare API alpha datasets samples and tests #801

Merged
merged 24 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
01d3c4e
Add Cloud Healthcare API alpha datasets samples and tests
noerog Oct 26, 2018
c12e2f8
Update README.md
noerog Oct 26, 2018
e79592c
Update README.md
noerog Oct 26, 2018
8178535
Merge branch 'master' into noero-chc-api-nodejs
noerog Oct 29, 2018
0c10bf5
Fix lint errors.
noerog Oct 30, 2018
8aeed2b
Fix lint errors.
noerog Oct 30, 2018
f2988d1
Fix lint errors.
noerog Oct 30, 2018
930894a
Remove yarn from package.json.
noerog Nov 1, 2018
baf32c3
Delete yarn.lock.
noerog Nov 1, 2018
9c20102
Add kokoro config file.
noerog Nov 1, 2018
3ed95b2
Merge branch 'noero-chc-api-nodejs' of https://github.com/noerog/node…
noerog Nov 1, 2018
268645f
"NodeJS" -> "Node.js"
noerog Nov 1, 2018
79bcf99
Remove trailing comma.
noerog Nov 1, 2018
288368f
Merge branch 'master' into noero-chc-api-nodejs
fhinkel Nov 5, 2018
9b7bb6f
Change date to 2018 and change "Google Inc." to "Google LLC"
noerog Nov 5, 2018
767e6ff
Replace console.log('ERROR:', err); with console.error(err)
noerog Nov 5, 2018
5c98cbf
Remove unused dependencies. Add "GCLOUD_PROJECT" required env var
noerog Nov 5, 2018
85378c7
Merge branch 'master' into noero-chc-api-nodejs
noerog Nov 5, 2018
456d1d4
Fix another instance of "NodeJS"
noerog Nov 5, 2018
f74a9ed
Merge branch 'master' into noero-chc-api-nodejs
noerog Nov 6, 2018
ab073a0
Add Keystore secret
noerog Nov 6, 2018
9c15d37
Merge branch 'master' into noero-chc-api-nodejs
fhinkel Nov 7, 2018
a1a2675
Merge branch 'master' into noero-chc-api-nodejs
noerog Nov 7, 2018
d7eaa63
Merge branch 'master' into noero-chc-api-nodejs
fhinkel Nov 8, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .kokoro/healthcare-datasets.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Format: //devtools/kokoro/config/proto/build.proto

# Set the folder in which the tests are run
env_vars: {
key: "PROJECT"
value: "healthcare/datasets"
}

# Tell the trampoline which build file to use.
env_vars: {
key: "TRAMPOLINE_BUILD_FILE"
value: "github/nodejs-docs-samples/.kokoro/build.sh"
}

before_action {
fetch_keystore {
keystore_resource {
keystore_config_id: 71386
keyname: "healthcare-api-nodejs-samples-kokoro-api-key"
}
}
}
37 changes: 37 additions & 0 deletions healthcare/datasets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# Cloud Healthcare API Node.js Dataset Management example

This sample app demonstrates dataset management for the Cloud Healthcare API.

# Setup

Run the following command to install the library dependencies for Node.js:

npm install

# Running the sample

Commands:
datasets.js createDataset <datasetId> Creates a new health dataset.
datasets.js deleteDataset <datasetId> Deletes the specified health dataset and all data
contained in the dataset.
datasets.js getDataset <datasetId> Gets any metadata associated with a dataset.
datasets.js listDatasets Lists the datasets in the given GCP project.
datasets.js patchDataset <datasetId> <timeZone> Updates dataset metadata.
datasets.js deidentifyDataset <sourceDatasetId> Creates a new dataset containing de-identified data from
<destinationDatasetId> <whitelistTags> the source dataset.

Options:
--version Show version number [boolean]
--apiKey, -a The API key used for discovering the API. Defaults to
the value of API_KEY environment variable.
[string]
--cloudRegion, -c [string] [default: "us-central1"]
--projectId, -p The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT
environment variables. [string]
--serviceAccount, -s The path to your service credentials JSON.
[string]
--help Show help [boolean]

For more information, see https://cloud.google.com/healthcare/docs
noerog marked this conversation as resolved.
Show resolved Hide resolved
296 changes: 296 additions & 0 deletions healthcare/datasets/datasets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,296 @@
/**
* Copyright 2018, Google, LLC
* Licensed 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.
*/

'use strict';

const {google} = require('googleapis');

// [START healthcare_create_dataset]
function createDataset (client, projectId, cloudRegion, datasetId) {
// Client retrieved in callback
Copy link
Contributor

Choose a reason for hiding this comment

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

You're not using callbacks in the code. Is there another way to get the client? Not clean to mix the two.

// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const datasetId = 'my-dataset';
const parentName = `projects/${projectId}/locations/${cloudRegion}`;

const request = {parent: parentName, datasetId: datasetId};

client.projects.locations.datasets.create(request)
.then(() => {
console.log(`Created dataset: ${datasetId}`);
})
.catch(err => {
console.error(err);
});
}
// [END healthcare_create_dataset]

// [START healthcare_delete_dataset]
function deleteDataset (client, projectId, cloudRegion, datasetId, cb) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use default parameters instead of these comments? We usually don't use adjective-noun-123 but PROJECT_ID with a TODO for the user to replace it..

// const projectId = 'adjective-noun-123';
// const datasetId = 'my-dataset';
const datasetName =
`projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`;

const request = {name: datasetName};

client.projects.locations.datasets.delete(request)
.then(() => {
console.log(`Deleted dataset: ${datasetId}`);
})
.catch(err => {
console.error(err);
});
}
// [END healthcare_delete_dataset]

// [START healthcare_get_dataset]
function getDataset (client, projectId, cloudRegion, datasetId) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const datasetId = 'my-dataset';
const datasetName =
`projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`;

const request = {name: datasetName};

client.projects.locations.datasets.get(request)
.then(results => {
console.log('Got dataset:\n', results.data);
})
.catch(err => {
console.error(err);
});
}
// [END healthcare_get_dataset]

// [START healthcare_list_datasets]
function listDatasets (client, projectId, cloudRegion) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
const parentName = `projects/${projectId}/locations/${cloudRegion}`;

const request = {parent: parentName};

client.projects.locations.datasets.list(request)
.then(results => {
console.log('Datasets:', results.data);
})
.catch(err => {
console.error(err);
});
}
// [END healthcare_list_datasets]

// [START healthcare_patch_dataset]
function patchDataset (client, projectId, cloudRegion, datasetId, timeZone) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const datasetId = 'my-dataset';
// const timeZone = 'GMT'
const datasetName =
`projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`;

const request = {
name: datasetName,
updateMask: 'timeZone',
resource: {timeZone: timeZone}
};

client.projects.locations.datasets.patch(request)
.then(results => {
console.log(
`Dataset ${datasetId} patched with time zone ${
results.data.timeZone}`);
})
.catch(err => {
console.error(err);
});
}
// [END healthcare_patch_dataset]

// [START healthcare_deidentify_dataset]
function deidentifyDataset (
client, projectId, cloudRegion, sourceDatasetId, destinationDatasetId,
whitelistTags) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const sourceDatasetId = 'my-dataset';
// const destinationDatasetId = 'my-destination-dataset';
// const whitelistTags = 'PatientID';
const sourceDatasetName = `projects/${projectId}/locations/${
cloudRegion}/datasets/${sourceDatasetId}`;
const destinationDatasetName = `projects/${projectId}/locations/${
cloudRegion}/datasets/${destinationDatasetId}`;

const request = {
sourceDataset: sourceDatasetName,
destinationDataset: destinationDatasetName,
resource: {config: {dicom: {whitelistTags: whitelistTags}}}
};

client.projects.locations.datasets.deidentify(request)
.then(results => {
console.log(`De-identified data written from dataset
${sourceDatasetId} to dataset ${destinationDatasetId}`);
})
.catch(err => {
console.error(err);
});
}
// [END healthcare_deidentify_dataset]

// [START healthcare_get_client]
// Returns an authorized API client by discovering the Healthcare API with
// the provided API key.
function getClient (apiKey, serviceAccountJson, cb) {
const API_VERSION = 'v1alpha';
const DISCOVERY_API = 'https://healthcare.googleapis.com/$discovery/rest';

google.auth
.getClient({scopes: ['https://www.googleapis.com/auth/cloud-platform']})
.then(authClient => {
const discoveryUrl = `${DISCOVERY_API}?labels=CHC_ALPHA&version=${
API_VERSION}&key=${apiKey}`;

google.options({auth: authClient});

google.discoverAPI(discoveryUrl)
.then((client) => {
cb(client);
})
.catch((err) => {
console.log(`Error during API discovery: ${err}`);
});
});
}
// [END healthcare_get_client]

require(`yargs`) // eslint-disable-line
.demand(1)
.options({
apiKey: {
alias: 'a',
default: process.env.API_KEY,
description: 'The API key used for discovering the API. ' +
'Defaults to the value of the API_KEY environment variable.',
requiresArg: true,
type: 'string'
},
cloudRegion: {
alias: 'c',
default: 'us-central1',
requiresArg: true,
type: 'string'
},
projectId: {
alias: 'p',
default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT,
description:
'The Project ID to use. Defaults to the value of the ' +
'GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.',
requiresArg: true,
type: 'string'
},
serviceAccount: {
alias: 's',
default: process.env.GOOGLE_APPLICATION_CREDENTIALS,
description: 'The path to your service credentials JSON.',
requiresArg: true,
type: 'string'
}
})
.command(
`createDataset <datasetId>`, `Creates a new health dataset.`, {},
(opts) => {
const cb = function (client) {
createDataset(
client, opts.projectId, opts.cloudRegion, opts.datasetId);
};
getClient(opts.apiKey, opts.serviceAccount, cb);
})
.command(
`deleteDataset <datasetId>`,
`Deletes the specified health dataset and all data contained
in the dataset.`,
{},
(opts) => {
const cb = function (client) {
deleteDataset(
client, opts.projectId, opts.cloudRegion, opts.datasetId);
};
getClient(opts.apiKey, opts.serviceAccount, cb);
})
.command(
`getDataset <datasetId>`,
`Gets any metadata associated with a dataset.`, {},
(opts) => {
const cb = function (client) {
getDataset(
client, opts.projectId, opts.cloudRegion, opts.datasetId);
};
getClient(opts.apiKey, opts.serviceAccount, cb);
})
.command(
`listDatasets`, `Lists the datasets in the given GCP project.`, {},
(opts) => {
const cb = function (client) {
listDatasets(client, opts.projectId, opts.cloudRegion);
};
getClient(opts.apiKey, opts.serviceAccount, cb);
})
.command(
`patchDataset <datasetId> <timeZone>`, `Updates dataset metadata.`, {},
(opts) => {
const cb = function (client) {
patchDataset(
client, opts.projectId, opts.cloudRegion, opts.datasetId,
opts.timeZone);
};
getClient(opts.apiKey, opts.serviceAccount, cb);
})
.command(
`deidentifyDataset <sourceDatasetId> <destinationDatasetId>
<whitelistTags>`,
`Creates a new dataset containing de-identified data from the
source dataset.`,
{},
(opts) => {
const cb = function (client) {
deidentifyDataset(
client, opts.projectId, opts.cloudRegion, opts.sourceDatasetId,
opts.destinationDatasetId, opts.whitelistTags);
};
getClient(opts.apiKey, opts.serviceAccount, cb);
})
.wrap(120)
.recommendCommands()
.epilogue(
`For more information, see https://cloud.google.com/healthcare/docs`)
.help()
.strict()
.argv;
35 changes: 35 additions & 0 deletions healthcare/datasets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "nodejs-docs-samples-healthcare",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google LLC",
"repository": "GoogleCloudPlatform/nodejs-docs-samples",
"engines": {
"node": ">=6.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

I would require Node 8 and rewrite it with async/await

},
"scripts": {
"test": "ava -T 1m --verbose system-test/*.test.js"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use mocha not ava. We're in the process of deleting ava.

},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.3.1",
"ava": "^0.25.0"
},
"dependencies": {
"googleapis": "^32.0.0",
"uuid": "^3.3.2",
"yargs": "^12.0.1"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
"requiresProjectId": true,
"test": {
"build": {
"requiredEnvVars": [
"API_KEY",
"GCLOUD_PROJECT"
]
}
}
}
}
Loading