Skip to content

Commit

Permalink
Merging in main
Browse files Browse the repository at this point in the history
  • Loading branch information
ymao1 committed Feb 22, 2023
2 parents b9ac0f4 + 81a4b39 commit 84d867b
Show file tree
Hide file tree
Showing 37 changed files with 1,992 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function SloSummary({ slo, historicalSummary = [], historicalSummaryLoadi

return (
<EuiFlexGroup direction="row" justifyContent="spaceBetween" gutterSize="xl">
<EuiFlexItem grow={false} style={{ width: 210 }}>
<EuiFlexItem grow={false} style={{ width: 200 }}>
<EuiFlexGroup direction="row" responsive={false} gutterSize="xs" alignItems="center">
<EuiFlexItem grow={false} style={{ width: 120 }}>
<EuiStat
Expand Down Expand Up @@ -64,9 +64,9 @@ export function SloSummary({ slo, historicalSummary = [], historicalSummaryLoadi
</EuiFlexGroup>
</EuiFlexItem>

<EuiFlexItem grow={false} style={{ width: 240 }}>
<EuiFlexItem grow={false} style={{ width: 260 }}>
<EuiFlexGroup direction="row" responsive={false} gutterSize="xs" alignItems="center">
<EuiFlexItem grow={false} style={{ width: 160 }}>
<EuiFlexItem grow={false} style={{ width: 180 }}>
<EuiStat
description={i18n.translate('xpack.observability.slos.slo.stats.budgetRemaining', {
defaultMessage: 'Budget remaining',
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/reporting/common/constants/report_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* 2.0.
*/

// Export Type Definitions
export const CSV_REPORT_TYPE = 'CSV';
export const CSV_REPORT_TYPE_V2 = 'csv_v2';

export const PDF_REPORT_TYPE = 'printablePdf';
export const PDF_REPORT_TYPE_V2 = 'printablePdfV2';
Expand Down
23 changes: 23 additions & 0 deletions x-pack/plugins/reporting/common/types/export_types/csv_v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 type { BaseParamsV2, BasePayloadV2 } from '../base';

interface CsvFromSavedObjectBase {
objectType: 'search';
}

/**
* Makes title optional, as it can be derived from the saved search object
*/
export type JobParamsCsvFromSavedObject = CsvFromSavedObjectBase &
Omit<BaseParamsV2, 'title'> & { title?: string };

/**
*
*/
export type TaskPayloadCsvFromSavedObject = CsvFromSavedObjectBase & BasePayloadV2;
4 changes: 4 additions & 0 deletions x-pack/plugins/reporting/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import type { PdfScreenshotResult, PngScreenshotResult } from '@kbn/screenshotting-plugin/server';
import type { BaseParams, BaseParamsV2, BasePayload, BasePayloadV2, JobId } from './base';

export type {
JobParamsCsvFromSavedObject,
TaskPayloadCsvFromSavedObject,
} from './export_types/csv_v2';
export type { JobParamsPNGDeprecated } from './export_types/png';
export type { JobParamsPNGV2 } from './export_types/png_v2';
export type { JobAppParamsPDF, JobParamsPDFDeprecated } from './export_types/printable_pdf';
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/reporting/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"requiredPlugins": [
"data",
"discover",
"fieldFormats",
"esUiShared",
"home",
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@

import type {
DocLinksServiceSetup,
FakeRawRequest,
Headers,
IBasePath,
IClusterClient,
KibanaRequest,
Logger,
PackageInfo,
PluginInitializerContext,
SavedObjectsClientContract,
SavedObjectsServiceStart,
StatusServiceSetup,
UiSettingsServiceStart,
KibanaRequest,
FakeRawRequest,
Headers,
} from '@kbn/core/server';
import { CoreKibanaRequest, ServiceStatusLevels } from '@kbn/core/server';
import type { PluginStart as DataPluginStart } from '@kbn/data-plugin/server';
import type { DiscoverServerPluginStart } from '@kbn/discover-plugin/server';
import type { PluginSetupContract as FeaturesPluginSetup } from '@kbn/features-plugin/server';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/server';
import type { LicensingPluginStart } from '@kbn/licensing-plugin/server';
Expand Down Expand Up @@ -69,6 +70,7 @@ export interface ReportingInternalStart {
uiSettings: UiSettingsServiceStart;
esClient: IClusterClient;
data: DataPluginStart;
discover: DiscoverServerPluginStart;
fieldFormats: FieldFormatsStart;
licensing: LicensingPluginStart;
logger: Logger;
Expand Down
42 changes: 42 additions & 0 deletions x-pack/plugins/reporting/server/export_types/csv_v2/create_job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 Boom from '@hapi/boom';
import { JobParamsCsvFromSavedObject, TaskPayloadCsvFromSavedObject } from '../../../common/types';
import { CreateJobFn, CreateJobFnFactory } from '../../types';

type CreateJobFnType = CreateJobFn<JobParamsCsvFromSavedObject, TaskPayloadCsvFromSavedObject>;

export const createJobFnFactory: CreateJobFnFactory<CreateJobFnType> = function createJobFactoryFn(
reporting
) {
return async function createJob(jobParams, _context, req) {
// 1. Validation of locatorParams
const { locatorParams } = jobParams;
const { id, params } = locatorParams[0];
if (
!locatorParams ||
!Array.isArray(locatorParams) ||
locatorParams.length !== 1 ||
id !== 'DISCOVER_APP_LOCATOR' ||
!params
) {
throw Boom.badRequest('Invalid Job params: must contain a single Discover App locator');
}

if (!params || !params.savedSearchId || typeof params.savedSearchId !== 'string') {
throw Boom.badRequest('Invalid Discover App locator: must contain a savedSearchId');
}

// use Discover contract to get the title of the report from job params
const { discover: discoverPluginStart } = await reporting.getPluginStartDeps();
const locatorClient = await discoverPluginStart.locator.asScopedClient(req);
const title = await locatorClient.titleFromLocator(params);

return { ...jobParams, title };
};
};
63 changes: 63 additions & 0 deletions x-pack/plugins/reporting/server/export_types/csv_v2/execute_job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 type { TaskPayloadCsvFromSavedObject } from '../../../common/types';
import { getFieldFormats } from '../../services';
import type { RunTaskFn, RunTaskFnFactory } from '../../types';
import { decryptJobHeaders } from '../common';
import { CsvGenerator } from '../csv_searchsource/generate_csv';

type RunTaskFnType = RunTaskFn<TaskPayloadCsvFromSavedObject>;

export const runTaskFnFactory: RunTaskFnFactory<RunTaskFnType> = (reporting, _logger) => {
const config = reporting.getConfig();
const encryptionKey = config.get('encryptionKey');
const csvConfig = config.get('csv');

return async function runTask(jobId, job, cancellationToken, stream) {
const logger = _logger.get(`execute:${jobId}`);

const headers = await decryptJobHeaders(encryptionKey, job.headers, logger);
const fakeRequest = reporting.getFakeRequest(headers, job.spaceId, logger);
const uiSettings = await reporting.getUiSettingsClient(fakeRequest, logger);
const fieldFormatsRegistry = await getFieldFormats().fieldFormatServiceFactory(uiSettings);
const { data: dataPluginStart, discover: discoverPluginStart } =
await reporting.getPluginStartDeps();
const data = dataPluginStart.search.asScoped(fakeRequest);

const { locatorParams } = job;
const { params } = locatorParams[0];

// use Discover contract to convert the job params into inputs for CsvGenerator
const locatorClient = await discoverPluginStart.locator.asScopedClient(fakeRequest);
const columns = await locatorClient.columnsFromLocator(params);
const searchSource = await locatorClient.searchSourceFromLocator(params);

const [es, searchSourceStart] = await Promise.all([
(await reporting.getEsClient()).asScoped(fakeRequest),
await dataPluginStart.search.searchSource.asScoped(fakeRequest),
]);

const clients = { uiSettings, data, es };
const dependencies = { searchSourceStart, fieldFormatsRegistry };

const csv = new CsvGenerator(
{
columns,
searchSource: searchSource.getSerializedFields(true),
...job,
},
csvConfig,
clients,
dependencies,
cancellationToken,
logger,
stream
);
return await csv.generateData();
};
};
40 changes: 40 additions & 0 deletions x-pack/plugins/reporting/server/export_types/csv_v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 {
CSV_REPORT_TYPE_V2 as 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 { JobParamsCsvFromSavedObject, TaskPayloadCsvFromSavedObject } from '../../../common/types';
import { CreateJobFn, ExportTypeDefinition, RunTaskFn } from '../../types';
import { createJobFnFactory } from './create_job';
import { runTaskFnFactory } from './execute_job';

export const getExportType = (): ExportTypeDefinition<
CreateJobFn<JobParamsCsvFromSavedObject>,
RunTaskFn<TaskPayloadCsvFromSavedObject>
> => ({
id: CSV_JOB_TYPE,
name: CSV_JOB_TYPE,
jobType: CSV_JOB_TYPE,
jobContentExtension: 'csv',
createJobFnFactory,
runTaskFnFactory,
validLicenses: [
LICENSE_TYPE_TRIAL,
LICENSE_TYPE_BASIC,
LICENSE_TYPE_CLOUD_STANDARD,
LICENSE_TYPE_GOLD,
LICENSE_TYPE_PLATINUM,
LICENSE_TYPE_ENTERPRISE,
],
});
4 changes: 3 additions & 1 deletion x-pack/plugins/reporting/server/lib/export_types_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { isString } from 'lodash';
import { getExportType as getTypeCsvFromSavedObject } from '../export_types/csv_searchsource_immediate';
import { getExportType as getTypeCsvFromSavedObject } from '../export_types/csv_v2';
import { getExportType as getTypeCsvFromSavedObjectImmediate } from '../export_types/csv_searchsource_immediate';
import { getExportType as getTypeCsv } from '../export_types/csv_searchsource';
import { getExportType as getTypePng } from '../export_types/png';
import { getExportType as getTypePngV2 } from '../export_types/png_v2';
Expand Down Expand Up @@ -88,6 +89,7 @@ export function getExportTypesRegistry(): ExportTypesRegistry {
const getTypeFns: Array<() => ExportTypeDefinition<CreateFnType | null, RunFnType>> = [
getTypeCsv,
getTypeCsvFromSavedObject,
getTypeCsvFromSavedObjectImmediate,
getTypePng,
getTypePngV2,
getTypePrintablePdf,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/reporting/server/routes/lib/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ export class RequestHandler {
throw new Error(`Export type ${exportTypeId} is not an async job type!`);
}

// 1. ensure the incoming params have a version field
// 1. ensure the incoming params have a version field (should be set by the UI)
jobParams.version = checkParamsVersion(jobParams, logger);

// 2. encrypt request headers for the running report job to authenticate itself with Kibana
// 3. call the export type's createJobFn to create the job payload
const [headers, job] = await Promise.all([
this.encryptHeaders(),
createJob(jobParams, context),
createJob(jobParams, context, this.req),
]);

const payload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
statusServiceMock,
} from '@kbn/core/server/mocks';
import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import { discoverPluginMock } from '@kbn/discover-plugin/server/mocks';
import { FieldFormatsRegistry } from '@kbn/field-formats-plugin/common';
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';
import { DeepPartial } from 'utility-types';
Expand Down Expand Up @@ -66,6 +67,7 @@ export const createMockPluginStart = async (
esClient: elasticsearchServiceMock.createClusterClient(),
savedObjects: { getScopedClient: jest.fn() },
uiSettings: { asScopedToClient: () => ({ get: jest.fn() }) },
discover: discoverPluginMock.createStartContract(),
data: dataPluginMock.createStartContract(),
fieldFormats: () => Promise.resolve(fieldFormatsMock),
store: await createMockReportingStore(config),
Expand Down
17 changes: 10 additions & 7 deletions x-pack/plugins/reporting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
* 2.0.
*/

import type { IRouter, Logger, CustomRequestHandlerContext } from '@kbn/core/server';
import type { CustomRequestHandlerContext, IRouter, KibanaRequest, Logger } from '@kbn/core/server';
import type { DataPluginStart } from '@kbn/data-plugin/server/plugin';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/server';
import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import type { Writable } from 'stream';
import { DiscoverServerPluginStart } from '@kbn/discover-plugin/server';
import type { PluginSetupContract as FeaturesPluginSetup } from '@kbn/features-plugin/server';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/server';
import type { LicensingPluginStart } from '@kbn/licensing-plugin/server';
import type { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/server';
import type {
PngScreenshotOptions as BasePngScreenshotOptions,
PdfScreenshotOptions as BasePdfScreenshotOptions,
PngScreenshotOptions as BasePngScreenshotOptions,
ScreenshottingStart,
} from '@kbn/screenshotting-plugin/server';
import type {
Expand All @@ -28,6 +27,8 @@ import type {
TaskManagerSetupContract,
TaskManagerStartContract,
} from '@kbn/task-manager-plugin/server';
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import type { Writable } from 'stream';
import type { CancellationToken } from '../common/cancellation_token';
import type { BaseParams, BasePayload, TaskRunResult, UrlOrUrlLocatorTuple } from '../common/types';
import type { ReportingConfigType } from './config';
Expand Down Expand Up @@ -59,7 +60,8 @@ export type ScrollConfig = ReportingConfigType['csv']['scroll'];
// default fn type for CreateJobFnFactory
export type CreateJobFn<JobParamsType = BaseParams, JobPayloadType = BasePayload> = (
jobParams: JobParamsType,
context: ReportingRequestHandlerContext
context: ReportingRequestHandlerContext,
req: KibanaRequest
) => Promise<Omit<JobPayloadType, 'headers' | 'spaceId'>>;

// default fn type for RunTaskFnFactory
Expand Down Expand Up @@ -105,6 +107,7 @@ export interface ReportingSetupDeps {

export interface ReportingStartDeps {
data: DataPluginStart;
discover: DiscoverServerPluginStart;
fieldFormats: FieldFormatsStart;
licensing: LicensingPluginStart;
screenshotting: ScreenshottingStart;
Expand Down
Loading

0 comments on commit 84d867b

Please sign in to comment.