Skip to content

Commit

Permalink
[CSV] Include custom field label in CSV reports (elastic#181565)
Browse files Browse the repository at this point in the history
- Closes elastic#100374

## Summary

This PR adds `field.customLabel` to CSV reports (column names) if
available.

Before:
<img width="800" alt="Screenshot 2024-04-24 at 15 16 45"
src="https://github.com/elastic/kibana/assets/1415710/870c9146-dd3a-4cc6-a12f-f67fbd7196f5">

After:
<img width="800" alt="Screenshot 2024-04-24 at 15 17 04"
src="https://github.com/elastic/kibana/assets/1415710/7f870899-71a2-42e7-ade4-d92acca9b96a">


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
jughosta and kibanamachine committed Apr 26, 2024
1 parent 41fd643 commit 4331175
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.

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

34 changes: 24 additions & 10 deletions packages/kbn-generate-csv/src/generate_csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import {
savedObjectsClientMock,
uiSettingsServiceMock,
} from '@kbn/core/server/mocks';
import { createStubDataView } from '@kbn/data-views-plugin/common/data_views/data_view.stub';
import { stubLogstashFieldSpecMap } from '@kbn/data-views-plugin/common/field.stub';
import { ISearchClient, ISearchStartSearchSource } from '@kbn/data-plugin/common';
import { searchSourceInstanceMock } from '@kbn/data-plugin/common/search/search_source/mocks';
import type { IScopedSearchClient } from '@kbn/data-plugin/server';
Expand Down Expand Up @@ -132,20 +134,31 @@ describe('CsvGenerator', () => {

mockConfig = getMockConfig();

const dataView = createStubDataView({
spec: {
id: 'test',
title: 'logstash-*',
fields: {
...stubLogstashFieldSpecMap,
bytes: {
...stubLogstashFieldSpecMap.bytes,
customLabel: 'bytes_custom_label',
},
},
},
opts: {
metaFields: ['_id', '_index', '_type', '_score'],
},
});

dataView.getFormatterForField = jest.fn();

searchSourceMock.getField = jest.fn((key: string) => {
switch (key) {
case 'pit':
return { id: mockCursorId };
case 'index':
return {
fields: {
getByName: jest.fn(() => []),
getByType: jest.fn(() => []),
},
metaFields: ['_id', '_index', '_type', '_score'],
getFormatterForField: jest.fn(),
getIndexPattern: () => 'logstash-*',
};
return dataView;
}
});

Expand Down Expand Up @@ -184,13 +197,14 @@ describe('CsvGenerator', () => {
date: `["2020-12-31T00:14:28.000Z"]`,
ip: `["110.135.176.89"]`,
message: `["This is a great message!"]`,
bytes: `[100]`,
},
} as unknown as estypes.SearchHit,
]),
})
);
const generateCsv = new CsvGenerator(
createMockJob({ columns: ['date', 'ip', 'message'] }),
createMockJob({ columns: ['date', 'ip', 'message', 'bytes'] }),
mockConfig,
mockTaskInstanceFields,
{
Expand Down
18 changes: 14 additions & 4 deletions packages/kbn-generate-csv/src/generate_csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Writable } from 'stream';

import { errors as esErrors, estypes } from '@elastic/elasticsearch';
import type { IScopedClusterClient, IUiSettingsClient, Logger } from '@kbn/core/server';
import type { ISearchClient, ISearchStartSearchSource } from '@kbn/data-plugin/common';
import type { DataView, ISearchClient, ISearchStartSearchSource } from '@kbn/data-plugin/common';
import { cellHasFormulas, tabifyDocs } from '@kbn/data-plugin/common';
import type { Datatable } from '@kbn/expressions-plugin/server';
import type {
Expand Down Expand Up @@ -146,11 +146,21 @@ export class CsvGenerator {
private generateHeader(
columns: Set<string>,
builder: MaxSizeStringBuilder,
settings: CsvExportSettings
settings: CsvExportSettings,
dataView: DataView
) {
this.logger.debug(`Building CSV header row`);
const header =
Array.from(columns).map(this.escapeValues(settings)).join(settings.separator) + '\n';
Array.from(columns)
.map((column) => {
const field = dataView?.fields.getByName(column);
if (field && field.customLabel && field.customLabel !== column) {
return `${field.customLabel} (${column})`;
}
return column;
})
.map(this.escapeValues(settings))
.join(settings.separator) + '\n';

if (!builder.tryAppend(header)) {
return {
Expand Down Expand Up @@ -364,7 +374,7 @@ export class CsvGenerator {

if (first) {
first = false;
this.generateHeader(columns, builder, settings);
this.generateHeader(columns, builder, settings, index);
}

if (table.rows.length < 1) {
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-generate-csv/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"@kbn/es-query",
"@kbn/es-types",
"@kbn/esql-utils",
"@kbn/data-views-plugin",
]
}

0 comments on commit 4331175

Please sign in to comment.