-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to delete annotations (#7069)
* add script * add package.json and script to delete annotations * amend help * fix issues * added explicit runs * add design document and index creation to script * update tests to wait for url to change * i think we can remove this deprecated function now
- Loading branch information
Showing
8 changed files
with
311 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "openmct-couch-plugin", | ||
"version": "1.0.0", | ||
"description": "CouchDB persistence plugin for Open MCT", | ||
"dependencies": { | ||
"@cloudant/couchbackup": "2.9.9" | ||
}, | ||
"scripts": { | ||
"backup:openmct": "npx couchbackup -u http://admin:[email protected]:5984/ -d openmct -o openmct-couch-backup.txt", | ||
"restore:openmct": "cat openmct-couch-backup.txt | npx couchrestore -u http://admin:[email protected]:5984/ -d openmct", | ||
"deleteAnnotations:openmct": "node scripts/deleteAnnotations.js $*", | ||
"deleteAnnotations:openmct:NOTEBOOK": "node scripts/deleteAnnotations.js -- --annotationType NOTEBOOK", | ||
"deleteAnnotations:openmct:GEOSPATIAL": "node scripts/deleteAnnotations.js -- --annotationType GEOSPATIAL", | ||
"deleteAnnotations:openmct:PIXEL_SPATIAL": "node scripts/deleteAnnotations.js -- --annotationType PIXEL_SPATIAL", | ||
"deleteAnnotations:openmct:TEMPORAL": "node scripts/deleteAnnotations.js -- --annotationType TEMPORAL", | ||
"deleteAnnotations:openmct:PLOT_SPATIAL": "node scripts/deleteAnnotations.js -- --annotationType PLOT_SPATIAL" | ||
} | ||
} |
190 changes: 190 additions & 0 deletions
190
src/plugins/persistence/couch/scripts/deleteAnnotations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
#!/usr/bin/env node | ||
|
||
/***************************************************************************** | ||
* Open MCT, Copyright (c) 2014-2023, United States Government | ||
* as represented by the Administrator of the National Aeronautics and Space | ||
* Administration. All rights reserved. | ||
* | ||
* Open MCT is 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. | ||
* | ||
* Open MCT includes source code licensed under additional open source | ||
* licenses. See the Open Source Licenses file (LICENSES.md) included with | ||
* this source code distribution or the Licensing information page available | ||
* at runtime from the About dialog for additional information. | ||
*****************************************************************************/ | ||
const process = require('process'); | ||
|
||
async function main() { | ||
try { | ||
const { annotationType, serverUrl, databaseName, helpRequested, username, password } = | ||
processArguments(); | ||
if (helpRequested) { | ||
return; | ||
} | ||
const docsToDelete = await gatherDocumentsForDeletion({ | ||
serverUrl, | ||
databaseName, | ||
annotationType, | ||
username, | ||
password | ||
}); | ||
const deletedDocumentCount = await performBulkDelete({ | ||
docsToDelete, | ||
serverUrl, | ||
databaseName, | ||
username, | ||
password | ||
}); | ||
console.log( | ||
`Deleted ${deletedDocumentCount} document${deletedDocumentCount === 1 ? '' : 's'}.` | ||
); | ||
} catch (error) { | ||
console.error(`Error: ${error.message}`); | ||
} | ||
} | ||
|
||
const ANNOTATION_TYPES = Object.freeze({ | ||
NOTEBOOK: 'NOTEBOOK', | ||
GEOSPATIAL: 'GEOSPATIAL', | ||
PIXEL_SPATIAL: 'PIXEL_SPATIAL', | ||
TEMPORAL: 'TEMPORAL', | ||
PLOT_SPATIAL: 'PLOT_SPATIAL' | ||
}); | ||
|
||
function processArguments() { | ||
const args = process.argv.slice(2); | ||
let annotationType; | ||
let databaseName = 'openmct'; // default db name to "openmct" | ||
let serverUrl = new URL('http://127.0.0.1:5984'); // default db name to "openmct" | ||
let helpRequested = false; | ||
|
||
args.forEach((val, index) => { | ||
switch (val) { | ||
case '--help': | ||
console.log( | ||
'Usage: deleteAnnotations.js [--annotationType type] [--dbName name] <CouchDB URL> \nFor authentication, set the environment variables COUCHDB_USERNAME and COUCHDB_PASSWORD. \n' | ||
); | ||
console.log('Annotation types: ', Object.keys(ANNOTATION_TYPES).join(', ')); | ||
helpRequested = true; | ||
break; | ||
case '--annotationType': | ||
annotationType = args[index + 1]; | ||
if (!Object.values(ANNOTATION_TYPES).includes(annotationType)) { | ||
throw new Error(`Invalid annotation type: ${annotationType}`); | ||
} | ||
break; | ||
case '--dbName': | ||
databaseName = args[index + 1]; | ||
break; | ||
case '--serverUrl': | ||
serverUrl = new URL(args[index + 1]); | ||
break; | ||
} | ||
}); | ||
|
||
let username = process.env.COUCHDB_USERNAME || ''; | ||
let password = process.env.COUCHDB_PASSWORD || ''; | ||
|
||
return { | ||
annotationType, | ||
serverUrl, | ||
databaseName, | ||
helpRequested, | ||
username, | ||
password | ||
}; | ||
} | ||
|
||
async function gatherDocumentsForDeletion({ | ||
serverUrl, | ||
databaseName, | ||
annotationType, | ||
username, | ||
password | ||
}) { | ||
const baseUrl = `${serverUrl.href}${databaseName}/_find`; | ||
let bookmark = null; | ||
let docsToDelete = []; | ||
let hasMoreDocs = true; | ||
|
||
const body = { | ||
selector: { | ||
_id: { $gt: null }, | ||
'model.type': 'annotation' | ||
}, | ||
fields: ['_id', '_rev'], | ||
limit: 1000 | ||
}; | ||
|
||
if (annotationType !== undefined) { | ||
body.selector['model.annotationType'] = annotationType; | ||
} | ||
|
||
const findOptions = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(body) | ||
}; | ||
|
||
if (username && password) { | ||
findOptions.headers.Authorization = `Basic ${btoa(`${username}:${password}`)}`; | ||
} | ||
|
||
while (hasMoreDocs) { | ||
if (bookmark) { | ||
body.bookmark = bookmark; | ||
} | ||
|
||
const res = await fetch(baseUrl, findOptions); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Server responded with status: ${res.status}`); | ||
} | ||
|
||
const findResult = await res.json(); | ||
|
||
bookmark = findResult.bookmark; | ||
docsToDelete = [...docsToDelete, ...findResult.docs]; | ||
|
||
// check if we got less than limit, set hasMoreDocs to false | ||
hasMoreDocs = findResult.docs.length === body.limit; | ||
} | ||
|
||
return docsToDelete; | ||
} | ||
|
||
async function performBulkDelete({ docsToDelete, serverUrl, databaseName, username, password }) { | ||
docsToDelete.forEach((doc) => (doc._deleted = true)); | ||
|
||
const deleteOptions = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ docs: docsToDelete }) | ||
}; | ||
|
||
if (username && password) { | ||
deleteOptions.headers.Authorization = `Basic ${btoa(`${username}:${password}`)}`; | ||
} | ||
|
||
const response = await fetch(`${serverUrl.href}${databaseName}/_bulk_docs`, deleteOptions); | ||
if (!response.ok) { | ||
throw new Error('Failed with status code: ' + response.status); | ||
} | ||
|
||
return docsToDelete.length; | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters