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

[Reporting] Fix performance of CSV generation #120309

Merged
merged 5 commits into from
Dec 3, 2021
Merged
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,35 @@ export class CsvGenerator {
/*
* Format a Datatable into rows of CSV content
*/
private generateRows(
private async generateRows(
columns: string[],
table: Datatable,
builder: MaxSizeStringBuilder,
formatters: Record<string, FieldFormat>,
settings: CsvExportSettings
) {
this.logger.debug(`Building ${table.rows.length} CSV data rows...`);

const asyncGenerateRow = async (dataTableRow: Record<string, any>): Promise<string> => {
return new Promise((resolve) => {
setImmediate(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add a comment explaining why we are using setImmediate here? Perhaps a link to the to nodejs documentation 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.

resolve(
columns
.map((f) => ({ column: f, data: dataTableRow[f] }))
.map(this.formatCellValues(formatters))
.map(this.escapeValues(settings))
.join(settings.separator) + '\n'
);
});
tsullivan marked this conversation as resolved.
Show resolved Hide resolved
});
};

for (const dataTableRow of table.rows) {
if (this.cancellationToken.isCancelled()) {
break;
}

const row =
columns
.map((f) => ({ column: f, data: dataTableRow[f] }))
.map(this.formatCellValues(formatters))
.map(this.escapeValues(settings))
.join(settings.separator) + '\n';

const row = await asyncGenerateRow(dataTableRow);
tsullivan marked this conversation as resolved.
Show resolved Hide resolved
if (!builder.tryAppend(row)) {
this.logger.warn(`Max Size Reached after ${this.csvRowCount} rows.`);
this.maxSizeReached = true;
Expand Down Expand Up @@ -377,7 +386,7 @@ export class CsvGenerator {
}

const formatters = this.getFormatters(table);
this.generateRows(columns, table, builder, formatters, settings);
await this.generateRows(columns, table, builder, formatters, settings);

// update iterator
currentRecord += table.rows.length;
Expand Down