Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into apm-error-tab…
Browse files Browse the repository at this point in the history
…le-api
  • Loading branch information
cauemarcondes committed Feb 15, 2021
2 parents ac5f9a5 + 42e11e6 commit d8f19a0
Show file tree
Hide file tree
Showing 154 changed files with 3,972 additions and 1,352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ core: {
savedObjects: {
client: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
exporter: ISavedObjectsExporter;
importer: ISavedObjectsImporter;
getClient: (options?: SavedObjectsClientProviderOptions) => SavedObjectsClientContract;
getExporter: (client: SavedObjectsClientContract) => ISavedObjectsExporter;
getImporter: (client: SavedObjectsClientContract) => ISavedObjectsImporter;
};
elasticsearch: {
client: IScopedClusterClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export interface RequestHandlerContext

| Property | Type | Description |
| --- | --- | --- |
| [core](./kibana-plugin-core-server.requesthandlercontext.core.md) | <code>{</code><br/><code> savedObjects: {</code><br/><code> client: SavedObjectsClientContract;</code><br/><code> typeRegistry: ISavedObjectTypeRegistry;</code><br/><code> exporter: ISavedObjectsExporter;</code><br/><code> importer: ISavedObjectsImporter;</code><br/><code> };</code><br/><code> elasticsearch: {</code><br/><code> client: IScopedClusterClient;</code><br/><code> legacy: {</code><br/><code> client: ILegacyScopedClusterClient;</code><br/><code> };</code><br/><code> };</code><br/><code> uiSettings: {</code><br/><code> client: IUiSettingsClient;</code><br/><code> };</code><br/><code> }</code> | |
| [core](./kibana-plugin-core-server.requesthandlercontext.core.md) | <code>{</code><br/><code> savedObjects: {</code><br/><code> client: SavedObjectsClientContract;</code><br/><code> typeRegistry: ISavedObjectTypeRegistry;</code><br/><code> getClient: (options?: SavedObjectsClientProviderOptions) =&gt; SavedObjectsClientContract;</code><br/><code> getExporter: (client: SavedObjectsClientContract) =&gt; ISavedObjectsExporter;</code><br/><code> getImporter: (client: SavedObjectsClientContract) =&gt; ISavedObjectsImporter;</code><br/><code> };</code><br/><code> elasticsearch: {</code><br/><code> client: IScopedClusterClient;</code><br/><code> legacy: {</code><br/><code> client: ILegacyScopedClusterClient;</code><br/><code> };</code><br/><code> };</code><br/><code> uiSettings: {</code><br/><code> client: IUiSettingsClient;</code><br/><code> };</code><br/><code> }</code> | |

28 changes: 12 additions & 16 deletions src/core/server/core_route_handler_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import { SavedObjectsClientContract } from './saved_objects/types';
import {
InternalSavedObjectsServiceStart,
ISavedObjectTypeRegistry,
ISavedObjectsExporter,
ISavedObjectsImporter,
SavedObjectsClientProviderOptions,
} from './saved_objects';
import {
InternalElasticsearchServiceStart,
Expand Down Expand Up @@ -58,8 +57,6 @@ class CoreSavedObjectsRouteHandlerContext {
) {}
#scopedSavedObjectsClient?: SavedObjectsClientContract;
#typeRegistry?: ISavedObjectTypeRegistry;
#exporter?: ISavedObjectsExporter;
#importer?: ISavedObjectsImporter;

public get client() {
if (this.#scopedSavedObjectsClient == null) {
Expand All @@ -75,19 +72,18 @@ class CoreSavedObjectsRouteHandlerContext {
return this.#typeRegistry;
}

public get exporter() {
if (this.#exporter == null) {
this.#exporter = this.savedObjectsStart.createExporter(this.client);
}
return this.#exporter;
}
public getClient = (options?: SavedObjectsClientProviderOptions) => {
if (!options) return this.client;
return this.savedObjectsStart.getScopedClient(this.request, options);
};

public get importer() {
if (this.#importer == null) {
this.#importer = this.savedObjectsStart.createImporter(this.client);
}
return this.#importer;
}
public getExporter = (client: SavedObjectsClientContract) => {
return this.savedObjectsStart.createExporter(client);
};

public getImporter = (client: SavedObjectsClientContract) => {
return this.savedObjectsStart.createImporter(client);
};
}

class CoreUiSettingsRouteHandlerContext {
Expand Down
6 changes: 4 additions & 2 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
SavedObjectsServiceStart,
ISavedObjectsExporter,
ISavedObjectsImporter,
SavedObjectsClientProviderOptions,
} from './saved_objects';
import { CapabilitiesSetup, CapabilitiesStart } from './capabilities';
import { MetricsServiceSetup, MetricsServiceStart } from './metrics';
Expand Down Expand Up @@ -415,8 +416,9 @@ export interface RequestHandlerContext {
savedObjects: {
client: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
exporter: ISavedObjectsExporter;
importer: ISavedObjectsImporter;
getClient: (options?: SavedObjectsClientProviderOptions) => SavedObjectsClientContract;
getExporter: (client: SavedObjectsClientContract) => ISavedObjectsExporter;
getImporter: (client: SavedObjectsClientContract) => ISavedObjectsImporter;
};
elasticsearch: {
client: IScopedClusterClient;
Expand Down
5 changes: 3 additions & 2 deletions src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ function createCoreRequestHandlerContextMock() {
savedObjects: {
client: savedObjectsClientMock.create(),
typeRegistry: savedObjectsTypeRegistryMock.create(),
exporter: savedObjectsServiceMock.createExporter(),
importer: savedObjectsServiceMock.createImporter(),
getClient: savedObjectsClientMock.create,
getExporter: savedObjectsServiceMock.createExporter,
getImporter: savedObjectsServiceMock.createImporter,
},
elasticsearch: {
client: elasticsearchServiceMock.createScopedClusterClient(),
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/saved_objects/routes/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export const registerDeleteRoute = (router: IRouter, { coreUsageData }: RouteDep
catchAndReturnBoomErrors(async (context, req, res) => {
const { type, id } = req.params;
const { force } = req.query;
const { getClient } = context.core.savedObjects;

const usageStatsClient = coreUsageData.getClient();
usageStatsClient.incrementSavedObjectsDelete({ request: req }).catch(() => {});

const result = await context.core.savedObjects.client.delete(type, id, { force });
const client = getClient();
const result = await client.delete(type, id, { force });
return res.ok({ body: result });
})
);
Expand Down
13 changes: 9 additions & 4 deletions src/core/server/saved_objects/routes/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ export const registerExportRoute = (
},
catchAndReturnBoomErrors(async (context, req, res) => {
const cleaned = cleanOptions(req.body);
const supportedTypes = context.core.savedObjects.typeRegistry
.getImportableAndExportableTypes()
.map((t) => t.name);
const { typeRegistry, getExporter, getClient } = context.core.savedObjects;
const supportedTypes = typeRegistry.getImportableAndExportableTypes().map((t) => t.name);

let options: EitherExportOptions;
try {
options = validateOptions(cleaned, {
Expand All @@ -181,7 +181,12 @@ export const registerExportRoute = (
});
}

const exporter = context.core.savedObjects.exporter;
const includedHiddenTypes = supportedTypes.filter((supportedType) =>
typeRegistry.isHidden(supportedType)
);

const client = getClient({ includedHiddenTypes });
const exporter = getExporter(client);

const usageStatsClient = coreUsageData.getClient();
usageStatsClient
Expand Down
11 changes: 10 additions & 1 deletion src/core/server/saved_objects/routes/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const registerImportRoute = (
},
catchAndReturnBoomErrors(async (context, req, res) => {
const { overwrite, createNewCopies } = req.query;
const { getClient, getImporter, typeRegistry } = context.core.savedObjects;

const usageStatsClient = coreUsageData.getClient();
usageStatsClient
Expand All @@ -84,7 +85,15 @@ export const registerImportRoute = (
});
}

const { importer } = context.core.savedObjects;
const supportedTypes = typeRegistry.getImportableAndExportableTypes().map((t) => t.name);

const includedHiddenTypes = supportedTypes.filter((supportedType) =>
typeRegistry.isHidden(supportedType)
);

const client = getClient({ includedHiddenTypes });
const importer = getImporter(client);

try {
const result = await importer.import({
readStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('DELETE /api/saved_objects/{type}/{id}', () => {

beforeEach(async () => {
({ server, httpSetup, handlerContext } = await setupServer());
savedObjectsClient = handlerContext.savedObjects.client;
savedObjectsClient = handlerContext.savedObjects.getClient();
handlerContext.savedObjects.getClient = jest.fn().mockImplementation(() => savedObjectsClient);

const router = httpSetup.createRouter('/api/saved_objects/');
coreUsageStatsClient = coreUsageStatsClientMock.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ describe('POST /api/saved_objects/_export', () => {
handlerContext.savedObjects.typeRegistry.getImportableAndExportableTypes.mockReturnValue(
allowedTypes.map(createExportableType)
);
exporter = handlerContext.savedObjects.exporter;
exporter = handlerContext.savedObjects.getExporter();

const router = httpSetup.createRouter('/api/saved_objects/');
handlerContext.savedObjects.getExporter = jest
.fn()
.mockImplementation(() => exporter as ReturnType<typeof savedObjectsExporterMock.create>);

coreUsageStatsClient = coreUsageStatsClientMock.create();
coreUsageStatsClient.incrementSavedObjectsExport.mockRejectedValue(new Error('Oh no!')); // intentionally throw this error, which is swallowed, so we can assert that the operation does not fail
const coreUsageData = coreUsageDataServiceMock.createSetupContract(coreUsageStatsClient);
Expand Down Expand Up @@ -77,6 +81,7 @@ describe('POST /api/saved_objects/_export', () => {
],
},
];

exporter.exportByTypes.mockResolvedValueOnce(createListStream(sortedObjects));

const result = await supertest(httpSetup.server.listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ describe(`POST ${URL}`, () => {
typeRegistry: handlerContext.savedObjects.typeRegistry,
importSizeLimit: 10000,
});
handlerContext.savedObjects.importer.import.mockImplementation((options) =>
importer.import(options)
);
handlerContext.savedObjects.getImporter = jest
.fn()
.mockImplementation(() => importer as jest.Mocked<SavedObjectsImporter>);

const router = httpSetup.createRouter('/internal/saved_objects/');
coreUsageStatsClient = coreUsageStatsClientMock.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ describe(`POST ${URL}`, () => {
} as any)
);

savedObjectsClient = handlerContext.savedObjects.client;
savedObjectsClient = handlerContext.savedObjects.getClient();
savedObjectsClient.checkConflicts.mockResolvedValue({ errors: [] });

const importer = new SavedObjectsImporter({
savedObjectsClient,
typeRegistry: handlerContext.savedObjects.typeRegistry,
importSizeLimit: 10000,
});
handlerContext.savedObjects.importer.resolveImportErrors.mockImplementation((options) =>
importer.resolveImportErrors(options)
);

handlerContext.savedObjects.getImporter = jest
.fn()
.mockImplementation(() => importer as jest.Mocked<SavedObjectsImporter>);

const router = httpSetup.createRouter('/api/saved_objects/');
coreUsageStatsClient = coreUsageStatsClientMock.create();
Expand Down
14 changes: 13 additions & 1 deletion src/core/server/saved_objects/routes/resolve_import_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { extname } from 'path';
import { Readable } from 'stream';
import { schema } from '@kbn/config-schema';
import { chain } from 'lodash';
import { IRouter } from '../../http';
import { CoreUsageDataSetup } from '../../core_usage_data';
import { SavedObjectConfig } from '../saved_objects_config';
Expand Down Expand Up @@ -91,7 +92,18 @@ export const registerResolveImportErrorsRoute = (
});
}

const { importer } = context.core.savedObjects;
const { getClient, getImporter, typeRegistry } = context.core.savedObjects;

const includedHiddenTypes = chain(req.body.retries)
.map('type')
.uniq()
.filter(
(type) => typeRegistry.isHidden(type) && typeRegistry.isImportableAndExportable(type)
)
.value();

const client = getClient({ includedHiddenTypes });
const importer = getImporter(client);

try {
const result = await importer.resolveImportErrors({
Expand Down
5 changes: 3 additions & 2 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1924,8 +1924,9 @@ export interface RequestHandlerContext {
savedObjects: {
client: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
exporter: ISavedObjectsExporter;
importer: ISavedObjectsImporter;
getClient: (options?: SavedObjectsClientProviderOptions) => SavedObjectsClientContract;
getExporter: (client: SavedObjectsClientContract) => ISavedObjectsExporter;
getImporter: (client: SavedObjectsClientContract) => ISavedObjectsImporter;
};
elasticsearch: {
client: IScopedClusterClient;
Expand Down
22 changes: 13 additions & 9 deletions src/dev/typescript/build_ts_refs_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,45 @@ export async function runBuildRefsCli() {
async ({ log, flags }) => {
const outDirs = getOutputsDeep(REF_CONFIG_PATHS);

if (flags.clean) {
const cacheEnabled = process.env.BUILD_TS_REFS_CACHE_ENABLE === 'true' || !!flags.cache;
const doCapture = process.env.BUILD_TS_REFS_CACHE_CAPTURE === 'true';
const doClean = !!flags.clean || doCapture;
const doInitCache = cacheEnabled && !doClean;

if (doClean) {
log.info('deleting', outDirs.length, 'ts output directories');
await concurrentMap(100, outDirs, (outDir) => del(outDir));
}

let outputCache;
if (flags.cache) {
if (cacheEnabled) {
outputCache = await RefOutputCache.create({
log,
outDirs,
repoRoot: REPO_ROOT,
workingDir: CACHE_WORKING_DIR,
upstreamUrl: 'https://github.com/elastic/kibana.git',
});
}

if (outputCache && doInitCache) {
await outputCache.initCaches();
}

await buildAllTsRefs(log);

if (outputCache) {
if (process.env.BUILD_TS_REFS_CACHE_CAPTURE === 'true') {
await outputCache.captureCache(Path.resolve(REPO_ROOT, 'target/ts_refs_cache'));
}
if (outputCache && doCapture) {
await outputCache.captureCache(Path.resolve(REPO_ROOT, 'target/ts_refs_cache'));
}

if (outputCache) {
await outputCache.cleanup();
}
},
{
description: 'Build TypeScript projects',
flags: {
boolean: ['clean', 'cache'],
default: {
cache: process.env.BUILD_TS_REFS_CACHE_ENABLE === 'true' ? true : false,
},
},
log: {
defaultLevel: 'debug',
Expand Down
2 changes: 1 addition & 1 deletion src/dev/typescript/ref_output_cache/repo_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class RepoInfo {

this.log.info('determining merge base with upstream');

const mergeBase = this.git(['merge-base', ref, 'FETCH_HEAD']);
const mergeBase = await this.git(['merge-base', ref, 'FETCH_HEAD']);
this.log.info('merge base with', upstreamBranch, 'is', mergeBase);

return mergeBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import moment from 'moment-timezone';
import { i18n } from '@kbn/i18n';

import { KBN_FIELD_TYPES, TimeRange, TimeRangeBounds, UI_SETTINGS } from '../../../../common';
import { IFieldType } from '../../../index_patterns';

import { intervalOptions, autoInterval, isAutoInterval } from './_interval_options';
import { createFilterDateHistogram } from './create_filter/date_histogram';
Expand Down Expand Up @@ -58,7 +59,7 @@ export function isDateHistogramBucketAggConfig(agg: any): agg is IBucketDateHist
}

export interface AggParamsDateHistogram extends BaseAggParams {
field?: string;
field?: IFieldType | string;
timeRange?: TimeRange;
useNormalizedEsInterval?: boolean;
scaleMetricValues?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jest.mock('moment', () => {
return moment;
});

import { IndexPattern } from '../../../index_patterns';
import { IndexPattern, IndexPatternField } from '../../../index_patterns';
import { AggParamsDateHistogram } from '../buckets';
import { inferTimeZone } from './infer_time_zone';

Expand Down Expand Up @@ -51,6 +51,31 @@ describe('inferTimeZone', () => {
).toEqual('UTC');
});

it('reads time zone from index pattern type meta if available when the field is not a string', () => {
expect(
inferTimeZone(
{
field: {
name: 'mydatefield',
} as IndexPatternField,
},
({
typeMeta: {
aggs: {
date_histogram: {
mydatefield: {
time_zone: 'UTC',
},
},
},
},
} as unknown) as IndexPattern,
() => false,
jest.fn()
)
).toEqual('UTC');
});

it('reads time zone from moment if set to default', () => {
expect(inferTimeZone({}, {} as IndexPattern, () => true, jest.fn())).toEqual('CET');
});
Expand Down
Loading

0 comments on commit d8f19a0

Please sign in to comment.