Skip to content

Commit

Permalink
Add linter for healthcare/datasets
Browse files Browse the repository at this point in the history
Ref: #836
  • Loading branch information
fhinkel committed Nov 9, 2018
1 parent cb3ba4d commit b5ce298
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 113 deletions.
17 changes: 17 additions & 0 deletions healthcare/datasets/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
extends:
- 'eslint:recommended'
- 'plugin:node/recommended'
- prettier
plugins:
- node
- prettier
rules:
prettier/prettier: error
block-scoped-var: error
eqeqeq: error
no-warning-comments: warn
no-var: error
prefer-const: error
no-console: off
node/no-unpublished-require: off
8 changes: 8 additions & 0 deletions healthcare/datasets/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
bracketSpacing: false
printWidth: 80
semi: true
singleQuote: true
tabWidth: 2
trailingComma: es5
useTabs: false
188 changes: 107 additions & 81 deletions healthcare/datasets/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const {google} = require('googleapis');

// [START healthcare_create_dataset]
function createDataset (client, projectId, cloudRegion, datasetId) {
function createDataset(client, projectId, cloudRegion, datasetId) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
Expand All @@ -28,7 +28,8 @@ function createDataset (client, projectId, cloudRegion, datasetId) {

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

client.projects.locations.datasets.create(request)
client.projects.locations.datasets
.create(request)
.then(() => {
console.log(`Created dataset: ${datasetId}`);
})
Expand All @@ -39,18 +40,18 @@ function createDataset (client, projectId, cloudRegion, datasetId) {
// [END healthcare_create_dataset]

// [START healthcare_delete_dataset]
function deleteDataset (client, projectId, cloudRegion, datasetId, cb) {
function deleteDataset(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 datasetName = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`;

const request = {name: datasetName};

client.projects.locations.datasets.delete(request)
client.projects.locations.datasets
.delete(request)
.then(() => {
console.log(`Deleted dataset: ${datasetId}`);
})
Expand All @@ -61,18 +62,18 @@ function deleteDataset (client, projectId, cloudRegion, datasetId, cb) {
// [END healthcare_delete_dataset]

// [START healthcare_get_dataset]
function getDataset (client, projectId, cloudRegion, datasetId) {
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 datasetName = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`;

const request = {name: datasetName};

client.projects.locations.datasets.get(request)
client.projects.locations.datasets
.get(request)
.then(results => {
console.log('Got dataset:\n', results.data);
})
Expand All @@ -83,7 +84,7 @@ function getDataset (client, projectId, cloudRegion, datasetId) {
// [END healthcare_get_dataset]

// [START healthcare_list_datasets]
function listDatasets (client, projectId, cloudRegion) {
function listDatasets(client, projectId, cloudRegion) {
// Client retrieved in callback
// getClient(serviceAccountJson, function(client) {...});
// const cloudRegion = 'us-central1';
Expand All @@ -92,7 +93,8 @@ function listDatasets (client, projectId, cloudRegion) {

const request = {parent: parentName};

client.projects.locations.datasets.list(request)
client.projects.locations.datasets
.list(request)
.then(results => {
console.log('Datasets:', results.data);
})
Expand All @@ -103,27 +105,27 @@ function listDatasets (client, projectId, cloudRegion) {
// [END healthcare_list_datasets]

// [START healthcare_patch_dataset]
function patchDataset (client, projectId, cloudRegion, datasetId, timeZone) {
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 datasetName = `projects/${projectId}/locations/${cloudRegion}/datasets/${datasetId}`;

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

client.projects.locations.datasets.patch(request)
client.projects.locations.datasets
.patch(request)
.then(results => {
console.log(
`Dataset ${datasetId} patched with time zone ${
results.data.timeZone}`);
`Dataset ${datasetId} patched with time zone ${results.data.timeZone}`
);
})
.catch(err => {
console.error(err);
Expand All @@ -132,29 +134,33 @@ function patchDataset (client, projectId, cloudRegion, datasetId, timeZone) {
// [END healthcare_patch_dataset]

// [START healthcare_deidentify_dataset]
function deidentifyDataset (
client, projectId, cloudRegion, sourceDatasetId, destinationDatasetId,
whitelistTags) {
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 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}}}
resource: {config: {dicom: {whitelistTags: whitelistTags}}},
};

client.projects.locations.datasets.deidentify(request)
.then(results => {
client.projects.locations.datasets
.deidentify(request)
.then(() => {
console.log(`De-identified data written from dataset
${sourceDatasetId} to dataset ${destinationDatasetId}`);
})
Expand All @@ -167,23 +173,23 @@ function deidentifyDataset (
// [START healthcare_get_client]
// Returns an authorized API client by discovering the Healthcare API with
// the provided API key.
function getClient (apiKey, serviceAccountJson, cb) {
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}`;
const discoveryUrl = `${DISCOVERY_API}?labels=CHC_ALPHA&version=${API_VERSION}&key=${apiKey}`;

google.options({auth: authClient});

google.discoverAPI(discoveryUrl)
.then((client) => {
google
.discoverAPI(discoveryUrl)
.then(client => {
cb(client);
})
.catch((err) => {
.catch(err => {
console.log(`Error during API discovery: ${err}`);
});
});
Expand All @@ -196,101 +202,121 @@ require(`yargs`) // eslint-disable-line
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.',
description:
'The API key used for discovering the API. ' +
'Defaults to the value of the API_KEY environment variable.',
requiresArg: true,
type: 'string'
type: 'string',
},
cloudRegion: {
alias: 'c',
default: 'us-central1',
requiresArg: true,
type: 'string'
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.',
'The Project ID to use. Defaults to the value of the ' +
'GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT environment variables.',
requiresArg: true,
type: 'string'
type: 'string',
},
serviceAccount: {
alias: 's',
default: process.env.GOOGLE_APPLICATION_CREDENTIALS,
description: 'The path to your service credentials JSON.',
requiresArg: true,
type: 'string'
}
type: 'string',
},
})
.command(
`createDataset <datasetId>`, `Creates a new health dataset.`, {},
(opts) => {
const cb = function (client) {
createDataset(
client, opts.projectId, opts.cloudRegion, opts.datasetId);
'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>`,
'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);
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);
'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',
'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 <datasetId> <timeZone>',
'Updates dataset metadata.',
{},
opts => {
const cb = function(client) {
patchDataset(
client, opts.projectId, opts.cloudRegion, opts.datasetId,
opts.timeZone);
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) {
opts => {
const cb = function(client) {
deidentifyDataset(
client, opts.projectId, opts.cloudRegion, opts.sourceDatasetId,
opts.destinationDatasetId, opts.whitelistTags);
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`)
'For more information, see https://cloud.google.com/healthcare/docs'
)
.help()
.strict()
.argv;
.strict().argv;
Loading

0 comments on commit b5ce298

Please sign in to comment.