Skip to content

Commit

Permalink
[Reporting] Fix CSV generator to prevent columns from being shifted (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dokmic authored Sep 23, 2022
1 parent f3a3243 commit 54c9c2d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,73 @@ it('uses the scrollId to page all the data', async () => {
});
});

it('keeps order of the columns during the scroll', async () => {
mockDataClient.search = jest.fn().mockImplementation(() =>
Rx.of({
rawResponse: {
_scroll_id: 'awesome-scroll-hero',
hits: {
hits: [
{
fields: {
a: ['a1'],
b: ['b1'],
},
},
],
total: 3,
},
},
})
);

mockEsClient.asCurrentUser.scroll = jest
.fn()
.mockResolvedValueOnce({
hits: {
hits: [
{
fields: {
b: ['b2'],
},
},
],
},
})
.mockResolvedValueOnce({
hits: {
hits: [
{
fields: {
a: ['a3'],
c: ['c3'],
},
},
],
},
});

const generateCsv = new CsvGenerator(
createMockJob({ searchSource: {}, columns: [] }),
mockConfig,
{
es: mockEsClient,
data: mockDataClient,
uiSettings: uiSettingsClient,
},
{
searchSourceStart: mockSearchSourceService,
fieldFormatsRegistry: mockFieldFormatsRegistry,
},
new CancellationToken(),
mockLogger,
stream
);
await generateCsv.generateData();

expect(content).toMatchSnapshot();
});

describe('fields from job.searchSource.getFields() (7.12 generated)', () => {
it('cells can be multi-value', async () => {
mockDataClient.search = jest.fn().mockImplementation(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ export class CsvGenerator {
* Use the list of columns to generate the header row
*/
private generateHeader(
columns: string[],
columns: Set<string>,
builder: MaxSizeStringBuilder,
settings: CsvExportSettings
) {
this.logger.debug(`Building CSV header row...`);
const header = columns.map(this.escapeValues(settings)).join(settings.separator) + '\n';
const header =
Array.from(columns).map(this.escapeValues(settings)).join(settings.separator) + '\n';

if (!builder.tryAppend(header)) {
return {
Expand All @@ -213,7 +214,7 @@ export class CsvGenerator {
* Format a Datatable into rows of CSV content
*/
private async generateRows(
columns: string[],
columns: Set<string>,
table: Datatable,
builder: MaxSizeStringBuilder,
formatters: Record<string, FieldFormat>,
Expand Down Expand Up @@ -315,6 +316,7 @@ export class CsvGenerator {
this.logger.error(err);
}

const columns = new Set<string>(this.job.columns ?? []);
try {
do {
if (this.cancellationToken.isCancelled()) {
Expand Down Expand Up @@ -366,11 +368,8 @@ export class CsvGenerator {
break;
}

let columns: string[];
if (this.job.columns && this.job.columns.length > 0) {
columns = this.job.columns;
} else {
columns = this.getColumnsFromTabify(table);
if (!this.job.columns?.length) {
this.getColumnsFromTabify(table).forEach((column) => columns.add(column));
}

if (first) {
Expand Down

0 comments on commit 54c9c2d

Please sign in to comment.