-
Notifications
You must be signed in to change notification settings - Fork 235
fix(import-export): Use query document count in export COMPASS-4537, COMPASS-4906 #2307
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/* eslint-disable valid-jsdoc */ | ||
import fs from 'fs'; | ||
import stream from 'stream'; | ||
import { promisify } from 'util'; | ||
|
||
import PROCESS_STATUS from '../constants/process-status'; | ||
import EXPORT_STEP from '../constants/export-step'; | ||
|
@@ -322,16 +323,51 @@ export const changeExportStep = (status) => ({ | |
status: status | ||
}); | ||
|
||
const fetchDocumentCount = async(dataService, ns, query) => { | ||
// When there is no filter/limit/skip try to use the estimated count. | ||
if ( | ||
(!query.filter || Object.keys(query.filter).length < 1) | ||
&& !query.limit | ||
&& !query.skip | ||
) { | ||
try { | ||
const runEstimatedDocumentCount = promisify(dataService.estimatedCount.bind(dataService)); | ||
const count = await runEstimatedDocumentCount(ns, {}); | ||
|
||
return count; | ||
} catch (estimatedCountErr) { | ||
// `estimatedDocumentCount` is currently unsupported for | ||
// views and time-series collections, so we can fallback to a full | ||
// count in these cases and ignore this error. | ||
} | ||
} | ||
|
||
const runCount = promisify(dataService.count.bind(dataService)); | ||
|
||
const count = await runCount( | ||
ns, | ||
query.filter || {}, | ||
{ | ||
...(query.limit ? { limit: query.limit } : {} ), | ||
...(query.skip ? { skip: query.skip } : {} ) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably need to handle a failure + add a |
||
); | ||
return count; | ||
}; | ||
|
||
/** | ||
* Open the export modal. | ||
* | ||
* @param {number} [count] - optional pre supplied count to shortcut and | ||
* avoid a possibly expensive re-count. | ||
* | ||
* Counts the documents to be exported given the current query on modal open to | ||
* provide user with accurate export data | ||
* | ||
* @api public | ||
*/ | ||
export const openExport = () => { | ||
return (dispatch, getState) => { | ||
export const openExport = (count) => { | ||
return async(dispatch, getState) => { | ||
const { | ||
ns, | ||
exportData, | ||
|
@@ -340,12 +376,16 @@ export const openExport = () => { | |
|
||
const spec = exportData.query; | ||
|
||
dataService.estimatedCount(ns, {query: spec.filter}, function(countErr, count) { | ||
if (countErr) { | ||
return onError(countErr); | ||
} | ||
dispatch(onModalOpen(count, spec)); | ||
}); | ||
if (count) { | ||
return dispatch(onModalOpen(count, spec)); | ||
} | ||
|
||
try { | ||
const docCount = await fetchDocumentCount(dataService, ns, spec); | ||
dispatch(onModalOpen(docCount, spec)); | ||
} catch (e) { | ||
dispatch(onError(e)); | ||
} | ||
}; | ||
}; | ||
|
||
|
@@ -389,7 +429,7 @@ export const sampleFields = () => { | |
* @api public | ||
*/ | ||
export const startExport = () => { | ||
return (dispatch, getState) => { | ||
return async(dispatch, getState) => { | ||
const { | ||
ns, | ||
exportData, | ||
|
@@ -400,87 +440,85 @@ export const startExport = () => { | |
? { filter: {} } | ||
: exportData.query; | ||
|
||
const numDocsToExport = exportData.isFullCollection | ||
? await fetchDocumentCount(dataService, ns, spec) | ||
: exportData.count; | ||
|
||
// filter out only the fields we want to include in our export data | ||
const projection = Object.fromEntries( | ||
Object.entries(exportData.fields) | ||
.filter((keyAndValue) => keyAndValue[1] === 1)); | ||
|
||
dataService.estimatedCount(ns, {query: spec.filter}, function(countErr, numDocsToExport) { | ||
if (countErr) { | ||
return onError(countErr); | ||
} | ||
debug('count says to expect %d docs in export', numDocsToExport); | ||
const source = createReadableCollectionStream(dataService, ns, spec, projection); | ||
|
||
debug('count says to expect %d docs in export', numDocsToExport); | ||
const source = createReadableCollectionStream(dataService, ns, spec, projection); | ||
const progress = createProgressStream({ | ||
objectMode: true, | ||
length: numDocsToExport, | ||
time: 250 /* ms */ | ||
}); | ||
|
||
const progress = createProgressStream({ | ||
objectMode: true, | ||
length: numDocsToExport, | ||
time: 250 /* ms */ | ||
}); | ||
progress.on('progress', function(info) { | ||
dispatch(onProgress(info.percentage, info.transferred)); | ||
}); | ||
|
||
progress.on('progress', function(info) { | ||
dispatch(onProgress(info.percentage, info.transferred)); | ||
}); | ||
// Pick the columns that are going to be matched by the projection, | ||
// where some prefix the field (e.g. ['a', 'a.b', 'a.b.c'] for 'a.b.c') | ||
// has an entry in the projection object. | ||
const columns = Object.keys(exportData.allFields) | ||
.filter(field => field.split('.').some( | ||
(_part, index, parts) => projection[parts.slice(0, index + 1).join('.')])); | ||
let formatter; | ||
if (exportData.fileType === 'csv') { | ||
formatter = createCSVFormatter({ columns }); | ||
} else { | ||
formatter = createJSONFormatter(); | ||
} | ||
|
||
// Pick the columns that are going to be matched by the projection, | ||
// where some prefix the field (e.g. ['a', 'a.b', 'a.b.c'] for 'a.b.c') | ||
// has an entry in the projection object. | ||
const columns = Object.keys(exportData.allFields) | ||
.filter(field => field.split('.').some( | ||
(_part, index, parts) => projection[parts.slice(0, index + 1).join('.')])); | ||
let formatter; | ||
if (exportData.fileType === 'csv') { | ||
formatter = createCSVFormatter({ columns }); | ||
} else { | ||
formatter = createJSONFormatter(); | ||
} | ||
const dest = fs.createWriteStream(exportData.fileName); | ||
|
||
const dest = fs.createWriteStream(exportData.fileName); | ||
debug('executing pipeline'); | ||
dispatch(onStarted(source, dest, numDocsToExport)); | ||
stream.pipeline(source, progress, formatter, dest, function(err) { | ||
if (err) { | ||
debug('error running export pipeline', err); | ||
return dispatch(onError(err)); | ||
} | ||
debug( | ||
'done. %d docs exported to %s', | ||
numDocsToExport, | ||
exportData.fileName | ||
); | ||
dispatch(onFinished(numDocsToExport)); | ||
dispatch( | ||
appRegistryEmit( | ||
'export-finished', | ||
numDocsToExport, | ||
exportData.fileType | ||
) | ||
); | ||
|
||
debug('executing pipeline'); | ||
dispatch(onStarted(source, dest, numDocsToExport)); | ||
stream.pipeline(source, progress, formatter, dest, function(err) { | ||
if (err) { | ||
debug('error running export pipeline', err); | ||
return dispatch(onError(err)); | ||
} | ||
debug( | ||
'done. %d docs exported to %s', | ||
/** | ||
* TODO: lucas: For metrics: | ||
* | ||
* "resource": "Export", | ||
* "action": "completed", | ||
* "file_type": "<csv|json_array>", | ||
* "num_docs": "<how many docs exported>", | ||
* "full_collection": true|false | ||
* "filter": true|false, | ||
* "projection": true|false, | ||
* "skip": true|false, | ||
* "limit": true|false, | ||
* "fields_selected": true|false | ||
*/ | ||
dispatch( | ||
globalAppRegistryEmit( | ||
'export-finished', | ||
numDocsToExport, | ||
exportData.fileName | ||
); | ||
dispatch(onFinished(numDocsToExport)); | ||
dispatch( | ||
appRegistryEmit( | ||
'export-finished', | ||
numDocsToExport, | ||
exportData.fileType | ||
) | ||
); | ||
|
||
/** | ||
* TODO: lucas: For metrics: | ||
* | ||
* "resource": "Export", | ||
* "action": "completed", | ||
* "file_type": "<csv|json_array>", | ||
* "num_docs": "<how many docs exported>", | ||
* "full_collection": true|false | ||
* "filter": true|false, | ||
* "projection": true|false, | ||
* "skip": true|false, | ||
* "limit": true|false, | ||
* "fields_selected": true|false | ||
*/ | ||
dispatch( | ||
globalAppRegistryEmit( | ||
'export-finished', | ||
numDocsToExport, | ||
exportData.fileType | ||
) | ||
); | ||
}); | ||
exportData.fileType | ||
) | ||
); | ||
}); | ||
}; | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea! Maybe we could add a debug call for when it fails.