-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move csv_v2 and csv_searchsource to export type class (#160041)
## Summary This is a part of #158092 This PR encompasses both csv_v2 and csv_searchsource as the CsvExportType and CsvSearchsourceExportType. Based on the reporting plugin readme, CsvSearchsourceExportType is currently used but CsvV2ExportType is refactored for when it can reach feature parity to csv searchsource. ### 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 <[email protected]> Co-authored-by: Timothy Sullivan <[email protected]>
- Loading branch information
1 parent
f3e2ed5
commit 65e94c6
Showing
30 changed files
with
436 additions
and
362 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
16 changes: 0 additions & 16 deletions
16
x-pack/plugins/reporting/server/export_types/csv_searchsource/create_job.ts
This file was deleted.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
x-pack/plugins/reporting/server/export_types/csv_searchsource/csv_searchsource.test.ts
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,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
jest.mock('@kbn/generate-csv', () => ({ | ||
CsvGenerator: class CsvGeneratorMock { | ||
generateData() { | ||
return { | ||
size: 123, | ||
content_type: 'text/csv', | ||
}; | ||
} | ||
}, | ||
})); | ||
|
||
import nodeCrypto from '@elastic/node-crypto'; | ||
import { coreMock, elasticsearchServiceMock, loggingSystemMock } from '@kbn/core/server/mocks'; | ||
import { Writable } from 'stream'; | ||
import { ReportingCore } from '../..'; | ||
import { CancellationToken } from '@kbn/reporting-common'; | ||
import { createMockConfigSchema, createMockReportingCore } from '../../test_helpers'; | ||
import { CsvSearchSourceExportType } from './csv_searchsource'; | ||
import { discoverPluginMock } from '@kbn/discover-plugin/server/mocks'; | ||
import { dataPluginMock } from '@kbn/data-plugin/server/mocks'; | ||
import { createMockScreenshottingStart } from '@kbn/screenshotting-plugin/server/mock'; | ||
|
||
const mockLogger = loggingSystemMock.createLogger(); | ||
const encryptionKey = 'tetkey'; | ||
const headers = { sid: 'cooltestheaders' }; | ||
let encryptedHeaders: string; | ||
let mockReportingCore: ReportingCore; | ||
let stream: jest.Mocked<Writable>; | ||
let mockCsvSearchSourceExportType: CsvSearchSourceExportType; | ||
|
||
beforeAll(async () => { | ||
const crypto = nodeCrypto({ encryptionKey }); | ||
|
||
encryptedHeaders = await crypto.encrypt(headers); | ||
const configType = createMockConfigSchema({ | ||
encryptionKey, | ||
csv: { | ||
checkForFormulas: true, | ||
escapeFormulaValues: true, | ||
maxSizeBytes: 180000, | ||
scroll: { size: 500, duration: '30s' }, | ||
}, | ||
}); | ||
const mockCoreSetup = coreMock.createSetup(); | ||
const mockCoreStart = coreMock.createStart(); | ||
const context = coreMock.createPluginInitializerContext(configType); | ||
|
||
mockReportingCore = await createMockReportingCore(configType); | ||
|
||
mockCsvSearchSourceExportType = new CsvSearchSourceExportType( | ||
mockCoreSetup, | ||
configType, | ||
mockLogger, | ||
context | ||
); | ||
|
||
mockCsvSearchSourceExportType.setup({ | ||
basePath: { set: jest.fn() }, | ||
}); | ||
|
||
mockCsvSearchSourceExportType.start({ | ||
esClient: elasticsearchServiceMock.createClusterClient(), | ||
savedObjects: mockCoreStart.savedObjects, | ||
uiSettings: mockCoreStart.uiSettings, | ||
discover: discoverPluginMock.createStartContract(), | ||
data: dataPluginMock.createStartContract(), | ||
screenshotting: createMockScreenshottingStart(), | ||
reporting: mockReportingCore.getContract(), | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
stream = {} as typeof stream; | ||
}); | ||
|
||
test('gets the csv content from job parameters', async () => { | ||
const payload = await mockCsvSearchSourceExportType.runTask( | ||
'cool-job-id', | ||
{ | ||
headers: encryptedHeaders, | ||
browserTimezone: 'US/Alaska', | ||
searchSource: {}, | ||
objectType: 'search', | ||
title: 'Test Search', | ||
version: '7.13.0', | ||
}, | ||
new CancellationToken(), | ||
stream | ||
); | ||
|
||
expect(payload).toMatchInlineSnapshot(` | ||
Object { | ||
"content_type": "text/csv", | ||
"size": 123, | ||
} | ||
`); | ||
}); |
104 changes: 104 additions & 0 deletions
104
x-pack/plugins/reporting/server/export_types/csv_searchsource/csv_searchsource.ts
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,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { DataPluginStart } from '@kbn/data-plugin/server/plugin'; | ||
import { DiscoverServerPluginStart } from '@kbn/discover-plugin/server'; | ||
import { CsvGenerator } from '@kbn/generate-csv'; | ||
import { CancellationToken } from '@kbn/reporting-common'; | ||
import { Writable } from 'stream'; | ||
import { | ||
CSV_JOB_TYPE, | ||
LICENSE_TYPE_BASIC, | ||
LICENSE_TYPE_CLOUD_STANDARD, | ||
LICENSE_TYPE_ENTERPRISE, | ||
LICENSE_TYPE_GOLD, | ||
LICENSE_TYPE_PLATINUM, | ||
LICENSE_TYPE_TRIAL, | ||
} from '../../../common/constants'; | ||
import { getFieldFormats } from '../../services'; | ||
import { ExportType, BaseExportTypeSetupDeps, BaseExportTypeStartDeps } from '../common'; | ||
import { decryptJobHeaders } from '../common/decrypt_job_headers'; | ||
import { JobParamsCSV, TaskPayloadCSV } from './types'; | ||
|
||
/* | ||
* @TODO move to be within @kbn/reporitng-export-types | ||
*/ | ||
|
||
type CsvSearchSourceExportTypeSetupDeps = BaseExportTypeSetupDeps; | ||
interface CsvSearchSourceExportTypeStartDeps extends BaseExportTypeStartDeps { | ||
discover: DiscoverServerPluginStart; | ||
data: DataPluginStart; | ||
} | ||
|
||
export class CsvSearchSourceExportType extends ExportType< | ||
JobParamsCSV, | ||
TaskPayloadCSV, | ||
CsvSearchSourceExportTypeSetupDeps, | ||
CsvSearchSourceExportTypeStartDeps | ||
> { | ||
id = 'csv_searchsource'; | ||
name = CSV_JOB_TYPE; | ||
jobType = CSV_JOB_TYPE; | ||
jobContentEncoding = 'base64' as const; | ||
jobContentExtension = 'csv' as const; | ||
validLicenses = [ | ||
LICENSE_TYPE_TRIAL, | ||
LICENSE_TYPE_BASIC, | ||
LICENSE_TYPE_CLOUD_STANDARD, | ||
LICENSE_TYPE_GOLD, | ||
LICENSE_TYPE_PLATINUM, | ||
LICENSE_TYPE_ENTERPRISE, | ||
]; | ||
constructor(...args: ConstructorParameters<typeof ExportType>) { | ||
super(...args); | ||
const logger = args[2]; | ||
this.logger = logger.get('csv-searchsource-export'); | ||
} | ||
|
||
public createJob = async (jobParams: JobParamsCSV) => { | ||
return { ...jobParams, isDeprecated: false }; | ||
}; | ||
|
||
public runTask = async ( | ||
jobId: string, | ||
job: TaskPayloadCSV, | ||
cancellationToken: CancellationToken, | ||
stream: Writable | ||
) => { | ||
const { encryptionKey, csv: csvConfig } = this.config; | ||
const logger = this.logger.get(`execute-job:${jobId}`); | ||
const headers = await decryptJobHeaders(encryptionKey, job.headers, logger); | ||
const fakeRequest = this.getFakeRequest(headers, job.spaceId, logger); | ||
const uiSettings = await this.getUiSettingsClient(fakeRequest, logger); | ||
const dataPluginStart = this.startDeps.data; | ||
const fieldFormatsRegistry = await getFieldFormats().fieldFormatServiceFactory(uiSettings); | ||
|
||
const es = this.startDeps.esClient.asScoped(fakeRequest); | ||
const searchSourceStart = await dataPluginStart.search.searchSource.asScoped(fakeRequest); | ||
|
||
const clients = { | ||
uiSettings, | ||
data: dataPluginStart.search.asScoped(fakeRequest), | ||
es, | ||
}; | ||
const dependencies = { | ||
searchSourceStart, | ||
fieldFormatsRegistry, | ||
}; | ||
|
||
const csv = new CsvGenerator( | ||
job, | ||
csvConfig, | ||
clients, | ||
dependencies, | ||
cancellationToken, | ||
logger, | ||
stream | ||
); | ||
return await csv.generateData(); | ||
}; | ||
} |
Oops, something went wrong.