Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
74dde50
GPII-3138: Added CouchDB views for retrieving snapset data.
cindyli Jun 27, 2018
ee30bb2
GPII-3138: Update snapsets in the data base
klown Jul 6, 2018
1c0f38a
GPII-3138: Update snapsets in the data base
klown Jul 6, 2018
b5dd8c0
GPII-3138: Update snapsets in the data base
klown Jul 6, 2018
f7068c0
GPII-3138: Update snapsets in the data base
klown Jul 9, 2018
e2f4e63
GPII-3138: Update snapsets in the data base
klown Jul 9, 2018
554ae82
GPII-3138: Update snapsets in the data base
klown Jul 9, 2018
f145f66
GPII-3138: Delete and reload snapsets in the data base
klown Jul 10, 2018
8f54a33
GPII-3138: Merged upstream master GPII branch into GPII-3138
klown Jul 16, 2018
67f7bd6
GPII-3138: Add a couchDB view to return all GPII keys.
cindyli Jul 18, 2018
c48d7ae
Merge pull request #1 from cindyli/GPII-3138
klown Jul 18, 2018
f029df2
GPII-3138: Merged Cindy's pull request with new database view
klown Jul 18, 2018
5f2f1b5
GPII-3138: Delete and (re)load snapsets into the database
klown Jul 20, 2018
750cdf3
GPII-3138: Delete and reload snapsets into the database
klown Jul 23, 2018
0e41600
GPII-3138: Delete and reload snapsets into the database
klown Jul 24, 2018
22d6605
GPII-3138: Delete and load snapsets Prefs Safes.
klown Jul 25, 2018
574236f
GPII-3138: Delete and load snapsets Prefs Safes into the database.
klown Jul 25, 2018
d6547d3
GPII-3138: Delete and load snapsets Prefs Safes into the database.
klown Jul 25, 2018
8520379
GPII-3138: Delete and load snapsets into the database
klown Jul 25, 2018
ef1e721
GPII-3138: Merged upstream GPII master branch.
klown Jul 26, 2018
0181c42
GPII-3138: Delete and load snapsets into the database
klown Jul 27, 2018
9bb64af
GPII-3138: Update snapsets into the database
klown Jul 30, 2018
a4541ba
GPII-3138: Update snapsets into the database
klown Jul 30, 2018
e026465
GPII-3138: Merged changes from upstreasm master GPII branch.
klown Jul 31, 2018
f4c41a6
GPII-3138: Update snapsets into the database
klown Jul 31, 2018
6723b6e
GPII-3138: Update snapsets into the database
klown Jul 31, 2018
2e0bb55
GPII-3138: Update snapsets into the database
klown Jul 31, 2018
fae02da
GPII-3138: Use UUIDs for demo user keys as well as creating 20 empty …
cindyli Aug 1, 2018
831a762
Merge pull request #2 from cindyli/GPII-3138
klown Aug 1, 2018
582b3d7
GPII-3138: Update snapsets in the database
klown Aug 5, 2018
c401c97
GPII-3138: Update snapsets in the database
klown Aug 7, 2018
54defe7
GPII-3138: Update snapsets in the database
klown Aug 7, 2018
58fdde7
GPII-3138: Update snapsets in the database
klown Aug 9, 2018
0862af6
GPII-3138: Merged changes from upstream master GPII branch.
klown Aug 10, 2018
79abd7d
GPII-3138: Merged changes from upstream master GPII branch
klown Aug 27, 2018
2b8cbeb
GPII-3138: Update snapsets in the database
klown Aug 28, 2018
59f03a8
GPII-3138: Update snapsets in the database
klown Aug 30, 2018
6ee716e
GPII-3138: Update snapsets in the database
klown Sep 10, 2018
9a090a1
GPII-3138: Update snapsets in the database
klown Sep 11, 2018
a9eec11
GPII-3138: Update snapsets in the database
klown Sep 11, 2018
a4cf452
GPII-3138: Update snapsets in the database
klown Sep 12, 2018
63c1a11
GPII-3138: Update snapsets in the database
klown Sep 13, 2018
7ce1b84
GPII-3138: Update snapsets in the database
klown Sep 18, 2018
afc6888
GPII-3138: Update snapsets in the database
klown Sep 19, 2018
75456ba
GPII-3138: Update snapsets in the database
klown Sep 19, 2018
230c8f0
GPII-3138: Update snapsets in the database
klown Sep 19, 2018
23b27e4
GPII-3138 Merged upstream master GPII branch
klown Oct 9, 2018
33f1741
GPII-3138: Update snapsets in the database.
klown Oct 9, 2018
2c48ca5
GPII-3138: Update snapsets in the database
klown Oct 11, 2018
d9ac25e
GPII-3138: Update snapsets in the database
klown Oct 15, 2018
ebde69d
GPII-3138: Update snapsets in the database
klown Oct 17, 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
4 changes: 2 additions & 2 deletions scripts/convertPrefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ https://github.com/GPII/universal/blob/master/LICENSE.txt
*/

// This script reads files from an input directory that contains preferences JSON5 files and convert them to JSON files of GPII keys and
// preferences safes suitable for direct loading into CouchDB or PouchDB, which comply with the new GPII data model:
// preferences safes suitable for direct loading into CouchDB or PouchDB, which comply with the new GPII data model:
// https://wiki.gpii.net/w/Keys,_KeyTokens,_and_Preferences in the target directory
// Usage: node scripts/convertPrefs.js {input_path} {target_path}
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the usage too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

//
Expand Down Expand Up @@ -61,7 +61,7 @@ rimraf(targetDir, function () {
"_id": prefsSafeId,
"type": "prefsSafe",
"schemaVersion": "0.1",
"prefsSafeType": "user",
"prefsSafeType": "snapset",
"name": gpiiKey,
"password": null,
"email": null,
Expand Down
243 changes: 243 additions & 0 deletions scripts/deleteSnapsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*!
Copyright 2018 OCAD University
Licensed under the New BSD license. You may not use this file except in
compliance with this License.
You may obtain a copy of the License at
https://github.com/GPII/universal/blob/master/LICENSE.txt
*/

// This script modifies the preferences data base:
// 1. Finds all the Prefs Safes of type "snapset" (prefsSafesType = "snapset"),
// 2. Finds all the GPII Keys associated with each snapset Prefs Safe
// 3. Deletes the found Prefs Safes and associated GPII Keys
//
// A sample command that runs this script:
// node deleteSnapsets.js $COUCHDBURL

"use strict";

var http = require("http"),
url = require("url"),
fluid = require("infusion");

var gpii = fluid.registerNamespace("gpii");
fluid.registerNamespace("gpii.dataLoader");
fluid.setLogging(fluid.logLevel.INFO)

var dbLoader = gpii.dataLoader;
dbLoader.couchDbUrl = process.argv[2];
if (!fluid.isValue(dbLoader.couchDbUrl)) {
fluid.log("COUCHDB_URL environment variable must be defined");
fluid.log("Usage: node deleteSnapsets.js $COUCHDB_URL");
process.exit(1);
}
dbLoader.prefsSafesViewUrl = dbLoader.couchDbUrl + "/_design/views/_view/findSnapsetPrefsSafes";
dbLoader.gpiiKeysViewUrl = dbLoader.couchDbUrl + "/_design/views/_view/findAllGpiiKeys";
dbLoader.parsedCouchDbUrl = url.parse(dbLoader.couchDbUrl);
dbLoader.snapsetPrefsSafes = [];
dbLoader.gpiiKeys = [];

fluid.log("COUCHDB_URL: '" +
dbLoader.parsedCouchDbUrl.protocol + "//" +
dbLoader.parsedCouchDbUrl.hostname + ":" +
dbLoader.parsedCouchDbUrl.port +
dbLoader.parsedCouchDbUrl.pathname +
"'");

/**
* Find the Prefs Safes of type "snapset", mark them to be deleted and add
* them to an array of records to remove.
* @param {String} responseString - The response from the database query for
* retrieving the snapset PrefsSafes records
* @return {Array} - Array of snapset Prefs Safes each with their "deleted"
* field set to true.
*/
dbLoader.processSnapsets = function (responseString) {
fluid.log("Processing the snapset Prefs Safes records...");
dbLoader.snapSets = JSON.parse(responseString);
fluid.each(dbLoader.snapSets.rows, function (aSnapset) {
aSnapset.value._deleted = true;
dbLoader.snapsetPrefsSafes.push(aSnapset.value);
});
fluid.log("\tSnapset Prefs Safes marked for deletion.");
return dbLoader.snapsetPrefsSafes;
Copy link
Contributor

Choose a reason for hiding this comment

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

This line seems unnecessary because dbLoader.snapsetPrefsSafes is a global variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

... and nothing actually uses the return value. Removed.

};

/**
* Find the GPII Key records that are associated with a snapset PrefsSafe, mark
* them for deletion, and add them to array of records to delete.
* @param {String} responseString - The response from the database query for
* retrieving all the GPII Keys.
* @return {Array} - Array of snapset PrefsSafes' GPII Keys with the "deleted"
* field set to true.
*/
dbLoader.processGpiiKeys = function (responseString) {
fluid.log("Processing the GPII Keys...");
var gpiiKeyRecords = JSON.parse(responseString);
dbLoader.gpiiKeys = dbLoader.findAndDeletePrefsSafesGpiiKeys(
gpiiKeyRecords, dbLoader.snapsetPrefsSafes
);
fluid.log("\tGPII Keys associated with snapset Prefs Safes marked for deletion.");
return dbLoader.gpiiKeys;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right.

};

/**
* Given all the GPII Keys records in the database, find the ones that reference
* a snapset PrefsSafe. As each GPII Key is found it is marked for
* deletion.
* @param {Array} gpiiKeyRecords - Array of GPII Key records from the database.
* @return {Array} - the values from the gpiiKeyRecords that are snapset GPII Keys.
*/
dbLoader.findAndDeletePrefsSafesGpiiKeys = function (gpiiKeyRecords, snapSets) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function marks "_delete" flags on GPII keys but doesn't perform deletion. Could you improve the function name? Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've renamed it markPrefsSafesGpiiKeysForDeletion()

var gpiiKeysToDelete = [];
fluid.each(gpiiKeyRecords.rows, function (gpiiKeyRecord) {
var gpiiKey = fluid.find(snapSets, function (aSnapSet) {
if (gpiiKeyRecord.value.prefsSafeId === aSnapSet._id) {
return gpiiKeyRecord.value;
}
}, null);
if (gpiiKey !== null) {
gpiiKey._deleted = true;
gpiiKeysToDelete.push(gpiiKey);
}
});
return gpiiKeysToDelete;
};

/**
* Log that the batch deletion of snapset Prefs Safes and their GPII Keys has
* been completed.
*/
dbLoader.procesBatchDelete = function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: proces -> process

Copy link
Member Author

Choose a reason for hiding this comment

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

I've already removed the function and replaced it with an anonymous one since it's just a log message.

fluid.log("Bulk deletion of snapset Prefs Safes and their GPII Keys...");
};

/**
* Make a bulk request of the database to delete the snapset Prefs Safes and
* their associated GPII Keys in one go. Note that this only sets up the request
* and returns a wrapper (function) to execute the request.
* @responseHandler {Object} - http response handler for the request.
* @return {Function} - A function that wraps an http request to execute the
* batch deletion.
*/
dbLoader.doBatchDelete = function (responseHandler) {
return function() {
var docsToRemove = dbLoader.snapsetPrefsSafes.concat(dbLoader.gpiiKeys);

var batchDeleteOptions = {
hostname: dbLoader.parsedCouchDbUrl.hostname,
port: dbLoader.parsedCouchDbUrl.port,
path: "/gpii/_bulk_docs",
method: "POST",
headers: {
"Accept": "application/json",
"Content-Length": 0, // filled in below
"Content-Type": "application/json"
}
};
var batchPostData = JSON.stringify({"docs": docsToRemove});
batchDeleteOptions.headers["Content-Length"] = Buffer.byteLength(batchPostData);
var batchDeleteRequest = http.request(batchDeleteOptions, responseHandler);
batchDeleteRequest.write(batchPostData);
batchDeleteRequest.end();
return batchDeleteRequest;
};
};

/**
* Log that the snapsets (Prefs Safes and GPII Keys) have been deleted.
*/
dbLoader.bulkDeletionComplete = function() {
fluid.log ("\tBulk deletion completed.");
};

/**
* Generate a response handler, setting up the given promise to resolve/reject
* at the correct time.
* @handleEnd {Function} - Function to call that deals with the response data
* when the response receives an "end" event.
* @promise {Promise} - Promose to resolve/reject on a response "end" or "error"
* event.
* @errorMsg {String} - Optional error message to prepend to the error received
* from a response "error" event.
* @return {Function} - Function that acts as a reponse callback for an http
* request
*/
dbLoader.createResponseHandler = function (handleEnd, promise, errorMsg) {
return function (response) {
var responseString = "";

response.setEncoding("utf8");
response.on("data", function (chunk) {
responseString += chunk;
});
response.on("end", function () {
debugger;
var value = handleEnd(responseString)
promise.resolve(value);
debugger;
});
response.on("error", function (e) {
fluid.log(errorMsg + e.message);
promise.reject(e);
});
};
};

/**
* General mechanism to create a database request, set up an error handler and
* return. It is up to the caller to trigger the request by calling its end()
* function.
* @param {String} databaseURL - URL to query the database with.
* @param {Function} handleResponse - callback that processes the response from
* the request.
* @param {String} errorMsg - optional error message for request errors.
* @return {http.ClientRequest} - The http request object.
*/
dbLoader.queryDatabase = function (databaseURL, handleResponse, errorMsg) {
var aRequest = http.request(databaseURL, handleResponse);
aRequest.on("error", function (e) {
fluid.log(errorMsg + e.message);
});
return aRequest;
};

// Get the snapsets Prefs Safes.
var snapsetsPromise = fluid.promise();
var getSnapSetsResponse = dbLoader.createResponseHandler(
dbLoader.processSnapsets,
snapsetsPromise,
"Error retrieving snapsets Prefs Safes: "
);
var snapSetsRequest = dbLoader.queryDatabase(
dbLoader.prefsSafesViewUrl,
getSnapSetsResponse,
"Error requesting snapsets Prefs Safes: "
);
snapSetsRequest.end();

// Get the associated GPII Keys.
var gpiiKeysPromise = fluid.promise();
var getGpiiKeysResponse = dbLoader.createResponseHandler(
dbLoader.processGpiiKeys,
gpiiKeysPromise,
"Error finding snapset Prefs Safes associated GPII Keys: "
);
var getGpiiKeysRequest = dbLoader.queryDatabase(
dbLoader.gpiiKeysViewUrl,
getGpiiKeysResponse,
"Error requesting GPII Keys: "
);
snapsetsPromise.then(function () { getGpiiKeysRequest.end(); });

// Batch delete snapset Prefs Safes and their GPII Keys.
var batchDeletePromise = fluid.promise();
var batchDeleteResponse = dbLoader.createResponseHandler(dbLoader.procesBatchDelete, batchDeletePromise);
var execBatchDelete = dbLoader.doBatchDelete(batchDeleteResponse);
gpiiKeysPromise.then(execBatchDelete);

// Done.
batchDeletePromise.then(dbLoader.bulkDeletionComplete);

12 changes: 9 additions & 3 deletions scripts/vagrantCloudBasedContainers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ COUCHDB_IMAGE=couchdb
COUCHDB_PORT=5984
COUCHDB_HEALTHCHECK_DELAY=2
COUCHDB_HEALTHCHECK_TIMEOUT=30
if [ "$NO_REBUILD" == "true" ] ; then
CLEAR_INDEX=
else
CLEAR_INDEX=1
fi

STATIC_DATA_DIR="/home/vagrant/sync/universal/testData/dbData"
BUILD_DATA_DIR="/home/vagrant/sync/universal/build/dbData"
UNIVERSAL_DIR="/home/vagrant/sync/universal"
STATIC_DATA_DIR="$UNIVERSAL_DIR/testData/dbData"
BUILD_DATA_DIR="$UNIVERSAL_DIR/build/dbData"

DATALOADER_IMAGE="gpii/gpii-dataloader"
DATALOADER_COUCHDB_URL="http://couchdb:${COUCHDB_PORT}/gpii"
Expand Down Expand Up @@ -77,7 +83,7 @@ docker run -d -p $COUCHDB_PORT:$COUCHDB_PORT --name couchdb $COUCHDB_IMAGE
wget -O /dev/null --retry-connrefused --waitretry=$COUCHDB_HEALTHCHECK_DELAY --read-timeout=20 --timeout=1 --tries=$COUCHDB_HEALTHCHECK_TIMEOUT http://localhost:$COUCHDB_PORT

# Load the CouchDB data
docker run --rm --link couchdb -v $STATIC_DATA_DIR:/static_data -e STATIC_DATA_DIR=/static_data -v $BUILD_DATA_DIR:/build_data -e BUILD_DATA_DIR=/build_data -e COUCHDB_URL=$DATALOADER_COUCHDB_URL -e CLEAR_INDEX=1 $DATALOADER_IMAGE
docker run --rm --link couchdb -v $STATIC_DATA_DIR:/static_data -e STATIC_DATA_DIR=/static_data -v $BUILD_DATA_DIR:/build_data -e BUILD_DATA_DIR=/build_data -e COUCHDB_URL=$DATALOADER_COUCHDB_URL -e CLEAR_INDEX=$CLEAR_INDEX $DATALOADER_IMAGE

# Wait for the CouchDB views become accessible. Accessing the view URL forced the view index to build which take time.
# The URL returns 500 when the index is not ready, so use "--retry-on-http-error" option to continue retries at 500 response code.
Expand Down
9 changes: 9 additions & 0 deletions testData/dbData/views.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
},
"findAuthorizationByAccessToken": {
"map": "function(doc) {if (doc.type === 'gpiiAppInstallationAuthorization' && doc.revoked === false) {emit(doc.accessToken, {'_id': doc.clientId, 'authorization': doc})}}"
},
"findSnapsetPrefsSafes": {
"map": "function(doc) {if (doc.type === 'prefsSafe' && doc.prefsSafeType === 'snapset') {emit(doc._id, doc)}}"
},
"findGpiiKeysByPrefsSafeId": {
Copy link
Contributor

Choose a reason for hiding this comment

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

This view is no longer used and can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, deleted.

"map": "function(doc) {if (doc.type === 'gpiiKey') {emit(doc.prefsSafeId, doc)}}"
},
"findAllGpiiKeys": {
"map": "function(doc) {if (doc.type === 'gpiiKey') {emit(doc._id, doc)}}"
}
}
}
Expand Down