diff --git a/.eslintignore b/.eslintignore index 5513ad1320232..ea8ab55ad7726 100644 --- a/.eslintignore +++ b/.eslintignore @@ -22,7 +22,7 @@ snapshots.js /src/core/lib/kbn_internal_native_observable /src/legacy/plugin_discovery/plugin_pack/__tests__/fixtures/plugins/broken /src/plugins/data/common/es_query/kuery/ast/_generated_/** -/src/plugins/vis_type_timelion/public/_generated_/** +/src/plugins/vis_type_timelion/common/_generated_/** /x-pack/legacy/plugins/**/__tests__/fixtures/** /x-pack/plugins/apm/e2e/tmp/* /x-pack/plugins/canvas/canvas_plugin diff --git a/.stylelintrc b/.stylelintrc index 29c1f4b552b48..26431cfee6f1d 100644 --- a/.stylelintrc +++ b/.stylelintrc @@ -32,6 +32,7 @@ rules: - function - return - for + - at-root comment-no-empty: true no-duplicate-at-import-rules: true no-duplicate-selectors: true diff --git a/dev_docs/assets/saved_object_vs_data_indices.png b/dev_docs/assets/saved_object_vs_data_indices.png new file mode 100644 index 0000000000000..e79a5cd848db1 Binary files /dev/null and b/dev_docs/assets/saved_object_vs_data_indices.png differ diff --git a/dev_docs/key_concepts/saved_objects.mdx b/dev_docs/key_concepts/saved_objects.mdx new file mode 100644 index 0000000000000..d89342765c8f1 --- /dev/null +++ b/dev_docs/key_concepts/saved_objects.mdx @@ -0,0 +1,74 @@ +--- +id: kibDevDocsSavedObjectsIntro +slug: /kibana-dev-docs/saved-objects-intro +title: Saved Objects +summary: Saved Objects are a key concept to understand when building a Kibana plugin. +date: 2021-02-02 +tags: ['kibana','dev', 'contributor', 'api docs'] +--- + +"Saved Objects" are developer defined, persisted entities, stored in the Kibana system index (which is also sometimes referred to as the `.kibana` index). +The Saved Objects service allows Kibana plugins to use Elasticsearch like a primary database. Think of it as an Object Document Mapper for Elasticsearch. + Some examples of Saved Object types are dashboards, lens, canvas workpads, index patterns, cases, ml jobs, and advanced settings. Some Saved Object types are + exposed to the user in the [Saved Object management UI](https://www.elastic.co/guide/en/kibana/current/managing-saved-objects.html), but not all. + +Developers create and manage their Saved Objects using the SavedObjectClient, while other data in Elasticsearch should be accessed via the data plugin's search +services. + +![image](../assets/saved_object_vs_data_indices.png) + + + + +## References + +In order to support import and export, and space-sharing capabilities, Saved Objects need to explicitly list any references they contain to other Saved Objects. +The parent should have a reference to it's children, not the other way around. That way when a "parent" is exported (or shared to a space), + all the "children" will be automatically included. However, when a "child" is exported, it will not include all "parents". + + + +## Migrations and Backward compatibility + +As your plugin evolves, you may need to change your Saved Object type in a breaking way (for example, changing the type of an attribtue, or removing +an attribute). If that happens, you should write a migration to upgrade the Saved Objects that existed prior to the change. + +. + +## Security + +Saved Objects can be secured using Kibana's Privileges model, unlike data that comes from data indices, which is secured using Elasticsearch's Privileges model. + +### Space awareness + +Saved Objects are "space aware". They exist in the space they were created in, and any spaces they have been shared with. + +### Feature controls and RBAC + +Feature controls provide another level of isolation and shareability for Saved Objects. Admins can give users and roles read, write or none permissions for each Saved Object type. + +### Object level security (OLS) + +OLS is an oft-requested feature that is not implemented yet. When it is, it will provide users with even more sharing and privacy flexibility. Individual +objects can be private to the user, shared with a selection of others, or made public. Much like how sharing Google Docs works. + +## Scalability + +By default all saved object types go into a single index. If you expect your saved object type to have a lot of unique fields, or if you expect there +to be many of them, you can have your objects go in a separate index by using the `indexPattern` field. Reporting and task manager are two +examples of features that use this capability. + +## Searchability + +Because saved objects are stored in system indices, they cannot be searched like other data can. If you see the phrase “[X] as data” it is +referring to this searching limitation. Users will not be able to create custom dashboards using saved object data, like they would for data stored +in Elasticsearch data indices. + +## Saved Objects by value + +Sometimes Saved Objects end up persisted inside another Saved Object. We call these Saved Objects “by value”, as opposed to "by + reference". If an end user creates a visualization and adds it to a dashboard without saving it to the visualization + library, the data ends up nested inside the dashboard Saved Object. This helps keep the visualization library smaller. It also avoids + issues with edits propagating - since an entity can only exist in a single place. + Note that from the end user stand point, we don’t use these terms “by reference” and “by value”. + diff --git a/dev_docs/tutorials/saved_objects.mdx b/dev_docs/tutorials/saved_objects.mdx new file mode 100644 index 0000000000000..bd7d231218af1 --- /dev/null +++ b/dev_docs/tutorials/saved_objects.mdx @@ -0,0 +1,250 @@ +--- +id: kibDevTutorialSavedObject +slug: /kibana-dev-docs/tutorial/saved-objects +title: Register a new saved object type +summary: Learn how to register a new saved object type. +date: 2021-02-05 +tags: ['kibana','onboarding', 'dev', 'architecture', 'tutorials'] +--- + +Saved Object type definitions should be defined in their own `my_plugin/server/saved_objects` directory. + +The folder should contain a file per type, named after the snake_case name of the type, and an index.ts file exporting all the types. + +**src/plugins/my_plugin/server/saved_objects/dashboard_visualization.ts** + +```ts +import { SavedObjectsType } from 'src/core/server'; + +export const dashboardVisualization: SavedObjectsType = { + name: 'dashboard_visualization', [1] + hidden: false, + namespaceType: 'single', + mappings: { + dynamic: false, + properties: { + description: { + type: 'text', + }, + hits: { + type: 'integer', + }, + }, + }, + migrations: { + '1.0.0': migratedashboardVisualizationToV1, + '2.0.0': migratedashboardVisualizationToV2, + }, +}; +``` + +[1] Since the name of a Saved Object type forms part of the url path for the public Saved Objects HTTP API, +these should follow our API URL path convention and always be written as snake case. + +**src/plugins/my_plugin/server/saved_objects/index.ts** + +```ts +export { dashboardVisualization } from './dashboard_visualization'; +export { dashboard } from './dashboard'; +``` + +**src/plugins/my_plugin/server/plugin.ts** + +```ts +import { dashboard, dashboardVisualization } from './saved_objects'; + +export class MyPlugin implements Plugin { + setup({ savedObjects }) { + savedObjects.registerType(dashboard); + savedObjects.registerType(dashboardVisualization); + } +} +``` + +## Mappings + +Each Saved Object type can define its own Elasticsearch field mappings. Because multiple Saved Object +types can share the same index, mappings defined by a type will be nested under a top-level field that matches the type name. + +For example, the mappings defined by the dashboard_visualization Saved Object type: + +**src/plugins/my_plugin/server/saved_objects/dashboard_visualization.ts** + +```ts +import { SavedObjectsType } from 'src/core/server'; + +export const dashboardVisualization: SavedObjectsType = { + name: 'dashboard_visualization', + ... + mappings: { + properties: { + dynamic: false, + description: { + type: 'text', + }, + hits: { + type: 'integer', + }, + }, + }, + migrations: { ... }, +}; +``` + +Will result in the following mappings being applied to the .kibana index: + +```ts +{ + "mappings": { + "dynamic": "strict", + "properties": { + ... + "dashboard_vizualization": { + "dynamic": false, + "properties": { + "description": { + "type": "text", + }, + "hits": { + "type": "integer", + }, + }, + } + } + } +} +``` +Do not use field mappings like you would use data types for the columns of a SQL database. Instead, field mappings are analogous to a +SQL index. Only specify field mappings for the fields you wish to search on or query. By specifying `dynamic: false` + in any level of your mappings, Elasticsearch will accept and store any other fields even if they are not specified in your mappings. + +Since Elasticsearch has a default limit of 1000 fields per index, plugins should carefully consider the +fields they add to the mappings. Similarly, Saved Object types should never use `dynamic: true` as this can cause an arbitrary + amount of fields to be added to the .kibana index. + + ## References + +Declare by adding an id, type and name to the + `references` array. + +```ts +router.get( + { path: '/some-path', validate: false }, + async (context, req, res) => { + const object = await context.core.savedObjects.client.create( + 'dashboard', + { + title: 'my dashboard', + panels: [ + { visualization: 'vis1' }, [1] + ], + indexPattern: 'indexPattern1' + }, + { references: [ + { id: '...', type: 'visualization', name: 'vis1' }, + { id: '...', type: 'index_pattern', name: 'indexPattern1' }, + ] + } + ) + ... + } +); +``` +[1] Note how `dashboard.panels[0].visualization` stores the name property of the reference (not the id directly) to be able to uniquely +identify this reference. This guarantees that the id the reference points to always remains up to date. If a + visualization id was directly stored in `dashboard.panels[0].visualization` there is a risk that this id gets updated without + updating the reference in the references array. + +## Writing migrations + +Saved Objects support schema changes between Kibana versions, which we call migrations. Migrations are + applied when a Kibana installation is upgraded from one version to the next, when exports are imported via + the Saved Objects Management UI, or when a new object is created via the HTTP API. + +Each Saved Object type may define migrations for its schema. Migrations are specified by the Kibana version number, receive an input document, + and must return the fully migrated document to be persisted to Elasticsearch. + +Let’s say we want to define two migrations: - In version 1.1.0, we want to drop the subtitle field and append it to the title - In version + 1.4.0, we want to add a new id field to every panel with a newly generated UUID. + +First, the current mappings should always reflect the latest or "target" schema. Next, we should define a migration function for each step in the schema evolution: + +**src/plugins/my_plugin/server/saved_objects/dashboard_visualization.ts** + +```ts +import { SavedObjectsType, SavedObjectMigrationFn } from 'src/core/server'; +import uuid from 'uuid'; + +interface DashboardVisualizationPre110 { + title: string; + subtitle: string; + panels: Array<{}>; +} +interface DashboardVisualization110 { + title: string; + panels: Array<{}>; +} + +interface DashboardVisualization140 { + title: string; + panels: Array<{ id: string }>; +} + +const migrateDashboardVisualization110: SavedObjectMigrationFn< + DashboardVisualizationPre110, [1] + DashboardVisualization110 +> = (doc) => { + const { subtitle, ...attributesWithoutSubtitle } = doc.attributes; + return { + ...doc, [2] + attributes: { + ...attributesWithoutSubtitle, + title: `${doc.attributes.title} - ${doc.attributes.subtitle}`, + }, + }; +}; + +const migrateDashboardVisualization140: SavedObjectMigrationFn< + DashboardVisualization110, + DashboardVisualization140 +> = (doc) => { + const outPanels = doc.attributes.panels?.map((panel) => { + return { ...panel, id: uuid.v4() }; + }); + return { + ...doc, + attributes: { + ...doc.attributes, + panels: outPanels, + }, + }; +}; + +export const dashboardVisualization: SavedObjectsType = { + name: 'dashboard_visualization', [1] + /** ... */ + migrations: { + // Takes a pre 1.1.0 doc, and converts it to 1.1.0 + '1.1.0': migrateDashboardVisualization110, + + // Takes a 1.1.0 doc, and converts it to 1.4.0 + '1.4.0': migrateDashboardVisualization140, [3] + }, +}; +``` +[1] It is useful to define an interface for each version of the schema. This allows TypeScript to ensure that you are properly handling the input and output + types correctly as the schema evolves. + +[2] Returning a shallow copy is necessary to avoid type errors when using different types for the input and output shape. + +[3] Migrations do not have to be defined for every version. The version number of a migration must always be the earliest Kibana version + in which this migration was released. So if you are creating a migration which will + be part of the v7.10.0 release, but will also be backported and released as v7.9.3, the migration version should be: 7.9.3. + + Migrations should be written defensively, an exception in a migration function will prevent a Kibana upgrade from succeeding and will cause downtime for our users. + Having said that, if a + document is encountered that is not in the expected shape, migrations are encouraged to throw an exception to abort the upgrade. In most scenarios, it is better to + fail an upgrade than to silently ignore a corrupt document which can cause unexpected behaviour at some future point in time. + +It is critical that you have extensive tests to ensure that migrations behave as expected with all possible input documents. Given how simple it is to test all the branch +conditions in a migration function and the high impact of a bug in this code, there’s really no reason not to aim for 100% test code coverage. diff --git a/docs/dev-tools/searchprofiler/getting-started.asciidoc b/docs/dev-tools/searchprofiler/getting-started.asciidoc index 7cd54db5562b7..ad73d03bcbfd8 100644 --- a/docs/dev-tools/searchprofiler/getting-started.asciidoc +++ b/docs/dev-tools/searchprofiler/getting-started.asciidoc @@ -2,7 +2,7 @@ [[profiler-getting-started]] === Getting Started -The {searchprofiler} is automatically enabled in {kib}. Open the main menu, click *Dev Tools*, then click *Search Profiler* +The {searchprofiler} is automatically enabled in {kib}. Open the main menu, click *Dev Tools*, then click *{searchprofiler}* to get started. {searchprofiler} displays the names of the indices searched, the shards in each index, diff --git a/docs/developer/plugin-list.asciidoc b/docs/developer/plugin-list.asciidoc index 90d9c42c8aef3..fc565491b4f63 100644 --- a/docs/developer/plugin-list.asciidoc +++ b/docs/developer/plugin-list.asciidoc @@ -297,6 +297,10 @@ which will load the visualization's editor. |To access an elasticsearch instance that has live data you have two options: +|{kib-repo}blob/{branch}/x-pack/plugins/banners/README.md[banners] +|Allow to add a header banner that will be displayed on every page of the Kibana application + + |{kib-repo}blob/{branch}/x-pack/plugins/beats_management/readme.md[beatsManagement] |Notes: Failure to have auth enabled in Kibana will make for a broken UI. UI-based errors not yet in place diff --git a/docs/development/core/public/kibana-plugin-core-public.chromestart.md b/docs/development/core/public/kibana-plugin-core-public.chromestart.md index 663b326193de5..2d465745c436b 100644 --- a/docs/development/core/public/kibana-plugin-core-public.chromestart.md +++ b/docs/development/core/public/kibana-plugin-core-public.chromestart.md @@ -67,6 +67,7 @@ core.chrome.setHelpExtension(elem => { | [setBreadcrumbs(newBreadcrumbs)](./kibana-plugin-core-public.chromestart.setbreadcrumbs.md) | Override the current set of breadcrumbs | | [setBreadcrumbsAppendExtension(breadcrumbsAppendExtension)](./kibana-plugin-core-public.chromestart.setbreadcrumbsappendextension.md) | Mount an element next to the last breadcrumb | | [setCustomNavLink(newCustomNavLink)](./kibana-plugin-core-public.chromestart.setcustomnavlink.md) | Override the current set of custom nav link | +| [setHeaderBanner(headerBanner)](./kibana-plugin-core-public.chromestart.setheaderbanner.md) | Set the banner that will appear on top of the chrome header. | | [setHelpExtension(helpExtension)](./kibana-plugin-core-public.chromestart.sethelpextension.md) | Override the current set of custom help content | | [setHelpSupportUrl(url)](./kibana-plugin-core-public.chromestart.sethelpsupporturl.md) | Override the default support URL shown in the help menu | | [setIsVisible(isVisible)](./kibana-plugin-core-public.chromestart.setisvisible.md) | Set the temporary visibility for the chrome. This does nothing if the chrome is hidden by default and should be used to hide the chrome for things like full-screen modes with an exit button. | diff --git a/docs/development/core/public/kibana-plugin-core-public.chromestart.setheaderbanner.md b/docs/development/core/public/kibana-plugin-core-public.chromestart.setheaderbanner.md new file mode 100644 index 0000000000000..02a2fa65ed478 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.chromestart.setheaderbanner.md @@ -0,0 +1,28 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [ChromeStart](./kibana-plugin-core-public.chromestart.md) > [setHeaderBanner](./kibana-plugin-core-public.chromestart.setheaderbanner.md) + +## ChromeStart.setHeaderBanner() method + +Set the banner that will appear on top of the chrome header. + +Signature: + +```typescript +setHeaderBanner(headerBanner?: ChromeUserBanner): void; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| headerBanner | ChromeUserBanner | | + +Returns: + +`void` + +## Remarks + +Using `undefined` when invoking this API will remove the banner. + diff --git a/docs/development/core/public/kibana-plugin-core-public.chromeuserbanner.content.md b/docs/development/core/public/kibana-plugin-core-public.chromeuserbanner.content.md new file mode 100644 index 0000000000000..7a77fdc6223de --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.chromeuserbanner.content.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [ChromeUserBanner](./kibana-plugin-core-public.chromeuserbanner.md) > [content](./kibana-plugin-core-public.chromeuserbanner.content.md) + +## ChromeUserBanner.content property + +Signature: + +```typescript +content: MountPoint; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.chromeuserbanner.md b/docs/development/core/public/kibana-plugin-core-public.chromeuserbanner.md new file mode 100644 index 0000000000000..8617c5c4d2b12 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.chromeuserbanner.md @@ -0,0 +1,19 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [ChromeUserBanner](./kibana-plugin-core-public.chromeuserbanner.md) + +## ChromeUserBanner interface + + +Signature: + +```typescript +export interface ChromeUserBanner +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [content](./kibana-plugin-core-public.chromeuserbanner.content.md) | MountPoint<HTMLDivElement> | | + diff --git a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md index f4bce8b51ebb1..5be8f8ce7e8c7 100644 --- a/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md +++ b/docs/development/core/public/kibana-plugin-core-public.doclinksstart.md @@ -17,5 +17,5 @@ export interface DocLinksStart | --- | --- | --- | | [DOC\_LINK\_VERSION](./kibana-plugin-core-public.doclinksstart.doc_link_version.md) | string | | | [ELASTIC\_WEBSITE\_URL](./kibana-plugin-core-public.doclinksstart.elastic_website_url.md) | string | | -| [links](./kibana-plugin-core-public.doclinksstart.links.md) | {
readonly dashboard: {
readonly guide: string;
readonly drilldowns: string;
readonly drilldownsTriggerPicker: string;
readonly urlDrilldownTemplateSyntax: string;
readonly urlDrilldownVariables: string;
};
readonly discover: Record<string, string>;
readonly filebeat: {
readonly base: string;
readonly installation: string;
readonly configuration: string;
readonly elasticsearchOutput: string;
readonly startup: string;
readonly exportedFields: string;
};
readonly auditbeat: {
readonly base: string;
};
readonly metricbeat: {
readonly base: string;
};
readonly enterpriseSearch: {
readonly base: string;
readonly appSearchBase: string;
readonly workplaceSearchBase: string;
};
readonly heartbeat: {
readonly base: string;
};
readonly logstash: {
readonly base: string;
};
readonly functionbeat: {
readonly base: string;
};
readonly winlogbeat: {
readonly base: string;
};
readonly aggs: {
readonly date_histogram: string;
readonly date_range: string;
readonly date_format_pattern: string;
readonly filter: string;
readonly filters: string;
readonly geohash_grid: string;
readonly histogram: string;
readonly ip_range: string;
readonly range: string;
readonly significant_terms: string;
readonly terms: string;
readonly avg: string;
readonly avg_bucket: string;
readonly max_bucket: string;
readonly min_bucket: string;
readonly sum_bucket: string;
readonly cardinality: string;
readonly count: string;
readonly cumulative_sum: string;
readonly derivative: string;
readonly geo_bounds: string;
readonly geo_centroid: string;
readonly max: string;
readonly median: string;
readonly min: string;
readonly moving_avg: string;
readonly percentile_ranks: string;
readonly serial_diff: string;
readonly std_dev: string;
readonly sum: string;
readonly top_hits: string;
};
readonly runtimeFields: string;
readonly scriptedFields: {
readonly scriptFields: string;
readonly scriptAggs: string;
readonly painless: string;
readonly painlessApi: string;
readonly painlessSyntax: string;
readonly luceneExpressions: string;
};
readonly indexPatterns: {
readonly loadingData: string;
readonly introduction: string;
};
readonly addData: string;
readonly kibana: string;
readonly elasticsearch: Record<string, string>;
readonly siem: {
readonly guide: string;
readonly gettingStarted: string;
};
readonly query: {
readonly eql: string;
readonly luceneQuerySyntax: string;
readonly queryDsl: string;
readonly kueryQuerySyntax: string;
};
readonly date: {
readonly dateMath: string;
};
readonly management: Record<string, string>;
readonly ml: Record<string, string>;
readonly transforms: Record<string, string>;
readonly visualize: Record<string, string>;
readonly apis: Readonly<{
createIndex: string;
createSnapshotLifecyclePolicy: string;
createRoleMapping: string;
createRoleMappingTemplates: string;
createApiKey: string;
createPipeline: string;
createTransformRequest: string;
executeWatchActionModes: string;
openIndex: string;
putComponentTemplate: string;
painlessExecute: string;
putComponentTemplateMetadata: string;
putWatch: string;
updateTransform: string;
}>;
readonly observability: Record<string, string>;
readonly alerting: Record<string, string>;
readonly maps: Record<string, string>;
readonly monitoring: Record<string, string>;
readonly security: Readonly<{
apiKeyServiceSettings: string;
clusterPrivileges: string;
elasticsearchSettings: string;
elasticsearchEnableSecurity: string;
indicesPrivileges: string;
kibanaTLS: string;
kibanaPrivileges: string;
mappingRoles: string;
mappingRolesFieldRules: string;
runAsPrivilege: string;
}>;
readonly watcher: Record<string, string>;
readonly ccs: Record<string, string>;
} | | +| [links](./kibana-plugin-core-public.doclinksstart.links.md) | {
readonly dashboard: {
readonly guide: string;
readonly drilldowns: string;
readonly drilldownsTriggerPicker: string;
readonly urlDrilldownTemplateSyntax: string;
readonly urlDrilldownVariables: string;
};
readonly discover: Record<string, string>;
readonly filebeat: {
readonly base: string;
readonly installation: string;
readonly configuration: string;
readonly elasticsearchOutput: string;
readonly startup: string;
readonly exportedFields: string;
};
readonly auditbeat: {
readonly base: string;
};
readonly metricbeat: {
readonly base: string;
};
readonly enterpriseSearch: {
readonly base: string;
readonly appSearchBase: string;
readonly workplaceSearchBase: string;
};
readonly heartbeat: {
readonly base: string;
};
readonly logstash: {
readonly base: string;
};
readonly functionbeat: {
readonly base: string;
};
readonly winlogbeat: {
readonly base: string;
};
readonly aggs: {
readonly date_histogram: string;
readonly date_range: string;
readonly date_format_pattern: string;
readonly filter: string;
readonly filters: string;
readonly geohash_grid: string;
readonly histogram: string;
readonly ip_range: string;
readonly range: string;
readonly significant_terms: string;
readonly terms: string;
readonly avg: string;
readonly avg_bucket: string;
readonly max_bucket: string;
readonly min_bucket: string;
readonly sum_bucket: string;
readonly cardinality: string;
readonly count: string;
readonly cumulative_sum: string;
readonly derivative: string;
readonly geo_bounds: string;
readonly geo_centroid: string;
readonly max: string;
readonly median: string;
readonly min: string;
readonly moving_avg: string;
readonly percentile_ranks: string;
readonly serial_diff: string;
readonly std_dev: string;
readonly sum: string;
readonly top_hits: string;
};
readonly runtimeFields: string;
readonly scriptedFields: {
readonly scriptFields: string;
readonly scriptAggs: string;
readonly painless: string;
readonly painlessApi: string;
readonly painlessSyntax: string;
readonly luceneExpressions: string;
};
readonly indexPatterns: {
readonly loadingData: string;
readonly introduction: string;
};
readonly addData: string;
readonly kibana: string;
readonly elasticsearch: Record<string, string>;
readonly siem: {
readonly guide: string;
readonly gettingStarted: string;
};
readonly query: {
readonly eql: string;
readonly luceneQuerySyntax: string;
readonly queryDsl: string;
readonly kueryQuerySyntax: string;
};
readonly date: {
readonly dateMath: string;
};
readonly management: Record<string, string>;
readonly ml: Record<string, string>;
readonly transforms: Record<string, string>;
readonly visualize: Record<string, string>;
readonly apis: Readonly<{
createIndex: string;
createSnapshotLifecyclePolicy: string;
createRoleMapping: string;
createRoleMappingTemplates: string;
createApiKey: string;
createPipeline: string;
createTransformRequest: string;
executeWatchActionModes: string;
indexExists: string;
openIndex: string;
putComponentTemplate: string;
painlessExecute: string;
putComponentTemplateMetadata: string;
putWatch: string;
updateTransform: string;
}>;
readonly observability: Record<string, string>;
readonly alerting: Record<string, string>;
readonly maps: Record<string, string>;
readonly monitoring: Record<string, string>;
readonly security: Readonly<{
apiKeyServiceSettings: string;
clusterPrivileges: string;
elasticsearchSettings: string;
elasticsearchEnableSecurity: string;
indicesPrivileges: string;
kibanaTLS: string;
kibanaPrivileges: string;
mappingRoles: string;
mappingRolesFieldRules: string;
runAsPrivilege: string;
}>;
readonly watcher: Record<string, string>;
readonly ccs: Record<string, string>;
} | | diff --git a/docs/development/core/public/kibana-plugin-core-public.md b/docs/development/core/public/kibana-plugin-core-public.md index e307b5c9971b0..5524cf328fbfe 100644 --- a/docs/development/core/public/kibana-plugin-core-public.md +++ b/docs/development/core/public/kibana-plugin-core-public.md @@ -56,6 +56,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [ChromeRecentlyAccessed](./kibana-plugin-core-public.chromerecentlyaccessed.md) | [APIs](./kibana-plugin-core-public.chromerecentlyaccessed.md) for recently accessed history. | | [ChromeRecentlyAccessedHistoryItem](./kibana-plugin-core-public.chromerecentlyaccessedhistoryitem.md) | | | [ChromeStart](./kibana-plugin-core-public.chromestart.md) | ChromeStart allows plugins to customize the global chrome header UI and enrich the UX with additional information about the current location of the browser. | +| [ChromeUserBanner](./kibana-plugin-core-public.chromeuserbanner.md) | | | [CoreSetup](./kibana-plugin-core-public.coresetup.md) | Core services exposed to the Plugin setup lifecycle | | [CoreStart](./kibana-plugin-core-public.corestart.md) | Core services exposed to the Plugin start lifecycle | | [DocLinksStart](./kibana-plugin-core-public.doclinksstart.md) | | diff --git a/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.maxwidth.md b/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.maxwidth.md new file mode 100644 index 0000000000000..0ae888f9cb361 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.maxwidth.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [OverlayModalOpenOptions](./kibana-plugin-core-public.overlaymodalopenoptions.md) > [maxWidth](./kibana-plugin-core-public.overlaymodalopenoptions.maxwidth.md) + +## OverlayModalOpenOptions.maxWidth property + +Signature: + +```typescript +maxWidth?: boolean | number | string; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.md b/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.md index 5c0ef8fb1ec86..5307a8357a814 100644 --- a/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.md +++ b/docs/development/core/public/kibana-plugin-core-public.overlaymodalopenoptions.md @@ -18,4 +18,5 @@ export interface OverlayModalOpenOptions | ["data-test-subj"](./kibana-plugin-core-public.overlaymodalopenoptions._data-test-subj_.md) | string | | | [className](./kibana-plugin-core-public.overlaymodalopenoptions.classname.md) | string | | | [closeButtonAriaLabel](./kibana-plugin-core-public.overlaymodalopenoptions.closebuttonarialabel.md) | string | | +| [maxWidth](./kibana-plugin-core-public.overlaymodalopenoptions.maxwidth.md) | boolean | number | string | | diff --git a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md index 8bd87c2f6ea35..69cfb818561e5 100644 --- a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md +++ b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md @@ -23,9 +23,11 @@ export interface SavedObjectsFindOptions | [namespaces](./kibana-plugin-core-public.savedobjectsfindoptions.namespaces.md) | string[] | | | [page](./kibana-plugin-core-public.savedobjectsfindoptions.page.md) | number | | | [perPage](./kibana-plugin-core-public.savedobjectsfindoptions.perpage.md) | number | | +| [pit](./kibana-plugin-core-public.savedobjectsfindoptions.pit.md) | SavedObjectsPitParams | Search against a specific Point In Time (PIT) that you've opened with . | | [preference](./kibana-plugin-core-public.savedobjectsfindoptions.preference.md) | string | An optional ES preference value to be used for the query \* | | [rootSearchFields](./kibana-plugin-core-public.savedobjectsfindoptions.rootsearchfields.md) | string[] | The fields to perform the parsed query against. Unlike the searchFields argument, these are expected to be root fields and will not be modified. If used in conjunction with searchFields, both are concatenated together. | | [search](./kibana-plugin-core-public.savedobjectsfindoptions.search.md) | string | Search documents using the Elasticsearch Simple Query String syntax. See Elasticsearch Simple Query String query argument for more information | +| [searchAfter](./kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md) | unknown[] | Use the sort values from the previous page to retrieve the next page of results. | | [searchFields](./kibana-plugin-core-public.savedobjectsfindoptions.searchfields.md) | string[] | The fields to perform the parsed query against. See Elasticsearch Simple Query String fields argument for more information | | [sortField](./kibana-plugin-core-public.savedobjectsfindoptions.sortfield.md) | string | | | [sortOrder](./kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md) | string | | diff --git a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.pit.md b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.pit.md new file mode 100644 index 0000000000000..2284a4d8d210d --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.pit.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [SavedObjectsFindOptions](./kibana-plugin-core-public.savedobjectsfindoptions.md) > [pit](./kibana-plugin-core-public.savedobjectsfindoptions.pit.md) + +## SavedObjectsFindOptions.pit property + +Search against a specific Point In Time (PIT) that you've opened with . + +Signature: + +```typescript +pit?: SavedObjectsPitParams; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md new file mode 100644 index 0000000000000..99ca2c34e77be --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [SavedObjectsFindOptions](./kibana-plugin-core-public.savedobjectsfindoptions.md) > [searchAfter](./kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md) + +## SavedObjectsFindOptions.searchAfter property + +Use the sort values from the previous page to retrieve the next page of results. + +Signature: + +```typescript +searchAfter?: unknown[]; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.md b/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.md index 0b7e6467667cb..6fcfae559dd33 100644 --- a/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.md +++ b/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.md @@ -23,6 +23,7 @@ export interface UiSettingsParams | [name](./kibana-plugin-core-public.uisettingsparams.name.md) | string | title in the UI | | [optionLabels](./kibana-plugin-core-public.uisettingsparams.optionlabels.md) | Record<string, string> | text labels for 'select' type UI element | | [options](./kibana-plugin-core-public.uisettingsparams.options.md) | string[] | array of permitted values for this setting | +| [order](./kibana-plugin-core-public.uisettingsparams.order.md) | number | index of the settings within its category (ascending order, smallest will be displayed first). Used for ordering in the UI. settings without order defined will be displayed last and ordered by name | | [readonly](./kibana-plugin-core-public.uisettingsparams.readonly.md) | boolean | a flag indicating that value cannot be changed | | [requiresPageReload](./kibana-plugin-core-public.uisettingsparams.requirespagereload.md) | boolean | a flag indicating whether new value applying requires page reloading | | [schema](./kibana-plugin-core-public.uisettingsparams.schema.md) | Type<T> | | diff --git a/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.order.md b/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.order.md new file mode 100644 index 0000000000000..d93aaeb904616 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.uisettingsparams.order.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [UiSettingsParams](./kibana-plugin-core-public.uisettingsparams.md) > [order](./kibana-plugin-core-public.uisettingsparams.order.md) + +## UiSettingsParams.order property + +index of the settings within its category (ascending order, smallest will be displayed first). Used for ordering in the UI. + + settings without order defined will be displayed last and ordered by name + +Signature: + +```typescript +order?: number; +``` diff --git a/docs/development/core/public/kibana-plugin-core-public.uisettingstype.md b/docs/development/core/public/kibana-plugin-core-public.uisettingstype.md index 5753704ccfe03..65e6264ea1e08 100644 --- a/docs/development/core/public/kibana-plugin-core-public.uisettingstype.md +++ b/docs/development/core/public/kibana-plugin-core-public.uisettingstype.md @@ -9,5 +9,5 @@ UI element type to represent the settings. Signature: ```typescript -export declare type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image'; +export declare type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image' | 'color'; ``` diff --git a/docs/development/core/server/kibana-plugin-core-server.md b/docs/development/core/server/kibana-plugin-core-server.md index 5fe5eda7a8172..1791335d58fef 100644 --- a/docs/development/core/server/kibana-plugin-core-server.md +++ b/docs/development/core/server/kibana-plugin-core-server.md @@ -155,6 +155,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsCheckConflictsResponse](./kibana-plugin-core-server.savedobjectscheckconflictsresponse.md) | | | [SavedObjectsClientProviderOptions](./kibana-plugin-core-server.savedobjectsclientprovideroptions.md) | Options to control the creation of the Saved Objects Client. | | [SavedObjectsClientWrapperOptions](./kibana-plugin-core-server.savedobjectsclientwrapperoptions.md) | Options passed to each SavedObjectsClientWrapperFactory to aid in creating the wrapper instance. | +| [SavedObjectsClosePointInTimeResponse](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md) | | | [SavedObjectsComplexFieldMapping](./kibana-plugin-core-server.savedobjectscomplexfieldmapping.md) | See [SavedObjectsFieldMapping](./kibana-plugin-core-server.savedobjectsfieldmapping.md) for documentation. | | [SavedObjectsCoreFieldMapping](./kibana-plugin-core-server.savedobjectscorefieldmapping.md) | See [SavedObjectsFieldMapping](./kibana-plugin-core-server.savedobjectsfieldmapping.md) for documentation. | | [SavedObjectsCreateOptions](./kibana-plugin-core-server.savedobjectscreateoptions.md) | | @@ -188,6 +189,9 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsMappingProperties](./kibana-plugin-core-server.savedobjectsmappingproperties.md) | Describe the fields of a [saved object type](./kibana-plugin-core-server.savedobjectstypemappingdefinition.md). | | [SavedObjectsMigrationLogger](./kibana-plugin-core-server.savedobjectsmigrationlogger.md) | | | [SavedObjectsMigrationVersion](./kibana-plugin-core-server.savedobjectsmigrationversion.md) | Information about the migrations that have been applied to this SavedObject. When Kibana starts up, KibanaMigrator detects outdated documents and migrates them based on this value. For each migration that has been applied, the plugin's name is used as a key and the latest migration version as the value. | +| [SavedObjectsOpenPointInTimeOptions](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md) | | +| [SavedObjectsOpenPointInTimeResponse](./kibana-plugin-core-server.savedobjectsopenpointintimeresponse.md) | | +| [SavedObjectsPitParams](./kibana-plugin-core-server.savedobjectspitparams.md) | | | [SavedObjectsRawDoc](./kibana-plugin-core-server.savedobjectsrawdoc.md) | A raw document as represented directly in the saved object index. | | [SavedObjectsRawDocParseOptions](./kibana-plugin-core-server.savedobjectsrawdocparseoptions.md) | Options that can be specified when using the saved objects serializer to parse a raw document. | | [SavedObjectsRemoveReferencesToOptions](./kibana-plugin-core-server.savedobjectsremovereferencestooptions.md) | | @@ -301,6 +305,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsClientFactory](./kibana-plugin-core-server.savedobjectsclientfactory.md) | Describes the factory used to create instances of the Saved Objects Client. | | [SavedObjectsClientFactoryProvider](./kibana-plugin-core-server.savedobjectsclientfactoryprovider.md) | Provider to invoke to retrieve a [SavedObjectsClientFactory](./kibana-plugin-core-server.savedobjectsclientfactory.md). | | [SavedObjectsClientWrapperFactory](./kibana-plugin-core-server.savedobjectsclientwrapperfactory.md) | Describes the factory used to create instances of Saved Objects Client Wrappers. | +| [SavedObjectsClosePointInTimeOptions](./kibana-plugin-core-server.savedobjectsclosepointintimeoptions.md) | | | [SavedObjectsExportTransform](./kibana-plugin-core-server.savedobjectsexporttransform.md) | Transformation function used to mutate the exported objects of the associated type.A type's export transform function will be executed once per user-initiated export, for all objects of that type. | | [SavedObjectsFieldMapping](./kibana-plugin-core-server.savedobjectsfieldmapping.md) | Describe a [saved object type mapping](./kibana-plugin-core-server.savedobjectstypemappingdefinition.md) field.Please refer to [elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) For the mapping documentation | | [SavedObjectsImportHook](./kibana-plugin-core-server.savedobjectsimporthook.md) | A hook associated with a specific saved object type, that will be invoked during the import process. The hook will have access to the objects of the registered type.Currently, the only supported feature for import hooks is to return warnings to be displayed in the UI when the import succeeds. The only interactions the hook can have with the import process is via the hook's response. Mutating the objects inside the hook's code will have no effect. | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.closepointintime.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.closepointintime.md new file mode 100644 index 0000000000000..dc765260a08ca --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.closepointintime.md @@ -0,0 +1,25 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsClient](./kibana-plugin-core-server.savedobjectsclient.md) > [closePointInTime](./kibana-plugin-core-server.savedobjectsclient.closepointintime.md) + +## SavedObjectsClient.closePointInTime() method + +Closes a Point In Time (PIT) by ID. This simply proxies the request to ES via the Elasticsearch client, and is included in the Saved Objects Client as a convenience for consumers who are using [SavedObjectsClient.openPointInTimeForType()](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md). + +Signature: + +```typescript +closePointInTime(id: string, options?: SavedObjectsClosePointInTimeOptions): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| id | string | | +| options | SavedObjectsClosePointInTimeOptions | | + +Returns: + +`Promise` + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.md index da1f4d029ea2b..887f7f7d93a87 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.md @@ -30,11 +30,13 @@ The constructor for this class is marked as internal. Third-party code should no | [bulkGet(objects, options)](./kibana-plugin-core-server.savedobjectsclient.bulkget.md) | | Returns an array of objects by id | | [bulkUpdate(objects, options)](./kibana-plugin-core-server.savedobjectsclient.bulkupdate.md) | | Bulk Updates multiple SavedObject at once | | [checkConflicts(objects, options)](./kibana-plugin-core-server.savedobjectsclient.checkconflicts.md) | | Check what conflicts will result when creating a given array of saved objects. This includes "unresolvable conflicts", which are multi-namespace objects that exist in a different namespace; such conflicts cannot be resolved/overwritten. | +| [closePointInTime(id, options)](./kibana-plugin-core-server.savedobjectsclient.closepointintime.md) | | Closes a Point In Time (PIT) by ID. This simply proxies the request to ES via the Elasticsearch client, and is included in the Saved Objects Client as a convenience for consumers who are using [SavedObjectsClient.openPointInTimeForType()](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md). | | [create(type, attributes, options)](./kibana-plugin-core-server.savedobjectsclient.create.md) | | Persists a SavedObject | | [delete(type, id, options)](./kibana-plugin-core-server.savedobjectsclient.delete.md) | | Deletes a SavedObject | | [deleteFromNamespaces(type, id, namespaces, options)](./kibana-plugin-core-server.savedobjectsclient.deletefromnamespaces.md) | | Removes namespaces from a SavedObject | | [find(options)](./kibana-plugin-core-server.savedobjectsclient.find.md) | | Find all SavedObjects matching the search query | | [get(type, id, options)](./kibana-plugin-core-server.savedobjectsclient.get.md) | | Retrieves a single object | +| [openPointInTimeForType(type, options)](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md) | | Opens a Point In Time (PIT) against the indices for the specified Saved Object types. The returned id can then be passed to [SavedObjectsClient.find()](./kibana-plugin-core-server.savedobjectsclient.find.md) to search against that PIT. | | [removeReferencesTo(type, id, options)](./kibana-plugin-core-server.savedobjectsclient.removereferencesto.md) | | Updates all objects containing a reference to the given {type, id} tuple to remove the said reference. | | [resolve(type, id, options)](./kibana-plugin-core-server.savedobjectsclient.resolve.md) | | Resolves a single object, using any legacy URL alias if it exists | | [update(type, id, attributes, options)](./kibana-plugin-core-server.savedobjectsclient.update.md) | | Updates an SavedObject | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md new file mode 100644 index 0000000000000..56c1d6d1ddc33 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md @@ -0,0 +1,25 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsClient](./kibana-plugin-core-server.savedobjectsclient.md) > [openPointInTimeForType](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md) + +## SavedObjectsClient.openPointInTimeForType() method + +Opens a Point In Time (PIT) against the indices for the specified Saved Object types. The returned `id` can then be passed to [SavedObjectsClient.find()](./kibana-plugin-core-server.savedobjectsclient.find.md) to search against that PIT. + +Signature: + +```typescript +openPointInTimeForType(type: string | string[], options?: SavedObjectsOpenPointInTimeOptions): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| type | string | string[] | | +| options | SavedObjectsOpenPointInTimeOptions | | + +Returns: + +`Promise` + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeoptions.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeoptions.md new file mode 100644 index 0000000000000..27432a8805b06 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeoptions.md @@ -0,0 +1,12 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsClosePointInTimeOptions](./kibana-plugin-core-server.savedobjectsclosepointintimeoptions.md) + +## SavedObjectsClosePointInTimeOptions type + + +Signature: + +```typescript +export declare type SavedObjectsClosePointInTimeOptions = SavedObjectsBaseOptions; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md new file mode 100644 index 0000000000000..43ecd1298d5d9 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsClosePointInTimeResponse](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md) + +## SavedObjectsClosePointInTimeResponse interface + + +Signature: + +```typescript +export interface SavedObjectsClosePointInTimeResponse +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [num\_freed](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.num_freed.md) | number | The number of search contexts that have been successfully closed. | +| [succeeded](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.succeeded.md) | boolean | If true, all search contexts associated with the PIT id are successfully closed. | + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.num_freed.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.num_freed.md new file mode 100644 index 0000000000000..b64932fcee8f6 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.num_freed.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsClosePointInTimeResponse](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md) > [num\_freed](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.num_freed.md) + +## SavedObjectsClosePointInTimeResponse.num\_freed property + +The number of search contexts that have been successfully closed. + +Signature: + +```typescript +num_freed: number; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.succeeded.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.succeeded.md new file mode 100644 index 0000000000000..225a549a4cf59 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsclosepointintimeresponse.succeeded.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsClosePointInTimeResponse](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md) > [succeeded](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.succeeded.md) + +## SavedObjectsClosePointInTimeResponse.succeeded property + +If true, all search contexts associated with the PIT id are successfully closed. + +Signature: + +```typescript +succeeded: boolean; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter._constructor_.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter._constructor_.md index 5e959bbee7beb..3f3d708c590ee 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter._constructor_.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter._constructor_.md @@ -9,10 +9,11 @@ Constructs a new instance of the `SavedObjectsExporter` class Signature: ```typescript -constructor({ savedObjectsClient, typeRegistry, exportSizeLimit, }: { +constructor({ savedObjectsClient, typeRegistry, exportSizeLimit, logger, }: { savedObjectsClient: SavedObjectsClientContract; typeRegistry: ISavedObjectTypeRegistry; exportSizeLimit: number; + logger: Logger; }); ``` @@ -20,5 +21,5 @@ constructor({ savedObjectsClient, typeRegistry, exportSizeLimit, }: { | Parameter | Type | Description | | --- | --- | --- | -| { savedObjectsClient, typeRegistry, exportSizeLimit, } | {
savedObjectsClient: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
exportSizeLimit: number;
} | | +| { savedObjectsClient, typeRegistry, exportSizeLimit, logger, } | {
savedObjectsClient: SavedObjectsClientContract;
typeRegistry: ISavedObjectTypeRegistry;
exportSizeLimit: number;
logger: Logger;
} | | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter.md index 727108b824c84..ce23e91633b07 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsexporter.md @@ -15,7 +15,7 @@ export declare class SavedObjectsExporter | Constructor | Modifiers | Description | | --- | --- | --- | -| [(constructor)({ savedObjectsClient, typeRegistry, exportSizeLimit, })](./kibana-plugin-core-server.savedobjectsexporter._constructor_.md) | | Constructs a new instance of the SavedObjectsExporter class | +| [(constructor)({ savedObjectsClient, typeRegistry, exportSizeLimit, logger, })](./kibana-plugin-core-server.savedobjectsexporter._constructor_.md) | | Constructs a new instance of the SavedObjectsExporter class | ## Properties diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md index d393d579dbdd2..6f7c05ea469bc 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md @@ -23,9 +23,11 @@ export interface SavedObjectsFindOptions | [namespaces](./kibana-plugin-core-server.savedobjectsfindoptions.namespaces.md) | string[] | | | [page](./kibana-plugin-core-server.savedobjectsfindoptions.page.md) | number | | | [perPage](./kibana-plugin-core-server.savedobjectsfindoptions.perpage.md) | number | | +| [pit](./kibana-plugin-core-server.savedobjectsfindoptions.pit.md) | SavedObjectsPitParams | Search against a specific Point In Time (PIT) that you've opened with [SavedObjectsClient.openPointInTimeForType()](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md). | | [preference](./kibana-plugin-core-server.savedobjectsfindoptions.preference.md) | string | An optional ES preference value to be used for the query \* | | [rootSearchFields](./kibana-plugin-core-server.savedobjectsfindoptions.rootsearchfields.md) | string[] | The fields to perform the parsed query against. Unlike the searchFields argument, these are expected to be root fields and will not be modified. If used in conjunction with searchFields, both are concatenated together. | | [search](./kibana-plugin-core-server.savedobjectsfindoptions.search.md) | string | Search documents using the Elasticsearch Simple Query String syntax. See Elasticsearch Simple Query String query argument for more information | +| [searchAfter](./kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md) | unknown[] | Use the sort values from the previous page to retrieve the next page of results. | | [searchFields](./kibana-plugin-core-server.savedobjectsfindoptions.searchfields.md) | string[] | The fields to perform the parsed query against. See Elasticsearch Simple Query String fields argument for more information | | [sortField](./kibana-plugin-core-server.savedobjectsfindoptions.sortfield.md) | string | | | [sortOrder](./kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md) | string | | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.pit.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.pit.md new file mode 100644 index 0000000000000..fac333227088c --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.pit.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsFindOptions](./kibana-plugin-core-server.savedobjectsfindoptions.md) > [pit](./kibana-plugin-core-server.savedobjectsfindoptions.pit.md) + +## SavedObjectsFindOptions.pit property + +Search against a specific Point In Time (PIT) that you've opened with [SavedObjectsClient.openPointInTimeForType()](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md). + +Signature: + +```typescript +pit?: SavedObjectsPitParams; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md new file mode 100644 index 0000000000000..6364370948976 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsFindOptions](./kibana-plugin-core-server.savedobjectsfindoptions.md) > [searchAfter](./kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md) + +## SavedObjectsFindOptions.searchAfter property + +Use the sort values from the previous page to retrieve the next page of results. + +Signature: + +```typescript +searchAfter?: unknown[]; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.md index 4ed069d1598fe..fd56e8ce40e24 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.md @@ -20,6 +20,7 @@ export interface SavedObjectsFindResponse | --- | --- | --- | | [page](./kibana-plugin-core-server.savedobjectsfindresponse.page.md) | number | | | [per\_page](./kibana-plugin-core-server.savedobjectsfindresponse.per_page.md) | number | | +| [pit\_id](./kibana-plugin-core-server.savedobjectsfindresponse.pit_id.md) | string | | | [saved\_objects](./kibana-plugin-core-server.savedobjectsfindresponse.saved_objects.md) | Array<SavedObjectsFindResult<T>> | | | [total](./kibana-plugin-core-server.savedobjectsfindresponse.total.md) | number | | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.pit_id.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.pit_id.md new file mode 100644 index 0000000000000..dc4f9b509d606 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresponse.pit_id.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsFindResponse](./kibana-plugin-core-server.savedobjectsfindresponse.md) > [pit\_id](./kibana-plugin-core-server.savedobjectsfindresponse.pit_id.md) + +## SavedObjectsFindResponse.pit\_id property + +Signature: + +```typescript +pit_id?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md index e455074a7d11b..0f8e9c59236bb 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md @@ -16,4 +16,5 @@ export interface SavedObjectsFindResult extends SavedObject | Property | Type | Description | | --- | --- | --- | | [score](./kibana-plugin-core-server.savedobjectsfindresult.score.md) | number | The Elasticsearch _score of this result. | +| [sort](./kibana-plugin-core-server.savedobjectsfindresult.sort.md) | unknown[] | The Elasticsearch sort value of this result. | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md new file mode 100644 index 0000000000000..3cc02c404c8d7 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md @@ -0,0 +1,41 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsFindResult](./kibana-plugin-core-server.savedobjectsfindresult.md) > [sort](./kibana-plugin-core-server.savedobjectsfindresult.sort.md) + +## SavedObjectsFindResult.sort property + +The Elasticsearch `sort` value of this result. + +Signature: + +```typescript +sort?: unknown[]; +``` + +## Remarks + +This can be passed directly to the `searchAfter` param in the [SavedObjectsFindOptions](./kibana-plugin-core-server.savedobjectsfindoptions.md) in order to page through large numbers of hits. It is recommended you use this alongside a Point In Time (PIT) that was opened with [SavedObjectsClient.openPointInTimeForType()](./kibana-plugin-core-server.savedobjectsclient.openpointintimefortype.md). + +## Example + + +```ts +const { id } = await savedObjectsClient.openPointInTimeForType('visualization'); +const page1 = await savedObjectsClient.find({ + type: 'visualization', + sortField: 'updated_at', + sortOrder: 'asc', + pit, +}); +const lastHit = page1.saved_objects[page1.saved_objects.length - 1]; +const page2 = await savedObjectsClient.find({ + type: 'visualization', + sortField: 'updated_at', + sortOrder: 'asc', + pit: { id: page1.pit_id }, + searchAfter: lastHit.sort, +}); +await savedObjectsClient.closePointInTime(page2.pit_id); + +``` + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.keepalive.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.keepalive.md new file mode 100644 index 0000000000000..57752318cb96a --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.keepalive.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsOpenPointInTimeOptions](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md) > [keepAlive](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.keepalive.md) + +## SavedObjectsOpenPointInTimeOptions.keepAlive property + +Optionally specify how long ES should keep the PIT alive until the next request. Defaults to `5m`. + +Signature: + +```typescript +keepAlive?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md new file mode 100644 index 0000000000000..46516be2329e9 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsOpenPointInTimeOptions](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md) + +## SavedObjectsOpenPointInTimeOptions interface + + +Signature: + +```typescript +export interface SavedObjectsOpenPointInTimeOptions extends SavedObjectsBaseOptions +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [keepAlive](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.keepalive.md) | string | Optionally specify how long ES should keep the PIT alive until the next request. Defaults to 5m. | +| [preference](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.preference.md) | string | An optional ES preference value to be used for the query. | + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.preference.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.preference.md new file mode 100644 index 0000000000000..7a9f3a49e8663 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeoptions.preference.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsOpenPointInTimeOptions](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.md) > [preference](./kibana-plugin-core-server.savedobjectsopenpointintimeoptions.preference.md) + +## SavedObjectsOpenPointInTimeOptions.preference property + +An optional ES preference value to be used for the query. + +Signature: + +```typescript +preference?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeresponse.id.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeresponse.id.md new file mode 100644 index 0000000000000..66387e5b3b89f --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeresponse.id.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsOpenPointInTimeResponse](./kibana-plugin-core-server.savedobjectsopenpointintimeresponse.md) > [id](./kibana-plugin-core-server.savedobjectsopenpointintimeresponse.id.md) + +## SavedObjectsOpenPointInTimeResponse.id property + +PIT ID returned from ES. + +Signature: + +```typescript +id: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeresponse.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeresponse.md new file mode 100644 index 0000000000000..c4be2692763a5 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsopenpointintimeresponse.md @@ -0,0 +1,19 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsOpenPointInTimeResponse](./kibana-plugin-core-server.savedobjectsopenpointintimeresponse.md) + +## SavedObjectsOpenPointInTimeResponse interface + + +Signature: + +```typescript +export interface SavedObjectsOpenPointInTimeResponse +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [id](./kibana-plugin-core-server.savedobjectsopenpointintimeresponse.id.md) | string | PIT ID returned from ES. | + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.id.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.id.md new file mode 100644 index 0000000000000..cb4d4a65727d7 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.id.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsPitParams](./kibana-plugin-core-server.savedobjectspitparams.md) > [id](./kibana-plugin-core-server.savedobjectspitparams.id.md) + +## SavedObjectsPitParams.id property + +Signature: + +```typescript +id: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.keepalive.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.keepalive.md new file mode 100644 index 0000000000000..1011a908f210a --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.keepalive.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsPitParams](./kibana-plugin-core-server.savedobjectspitparams.md) > [keepAlive](./kibana-plugin-core-server.savedobjectspitparams.keepalive.md) + +## SavedObjectsPitParams.keepAlive property + +Signature: + +```typescript +keepAlive?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.md new file mode 100644 index 0000000000000..7bffca7cda281 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectspitparams.md @@ -0,0 +1,20 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsPitParams](./kibana-plugin-core-server.savedobjectspitparams.md) + +## SavedObjectsPitParams interface + + +Signature: + +```typescript +export interface SavedObjectsPitParams +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [id](./kibana-plugin-core-server.savedobjectspitparams.id.md) | string | | +| [keepAlive](./kibana-plugin-core-server.savedobjectspitparams.keepalive.md) | string | | + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.closepointintime.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.closepointintime.md new file mode 100644 index 0000000000000..8f9dca35fa362 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.closepointintime.md @@ -0,0 +1,58 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsRepository](./kibana-plugin-core-server.savedobjectsrepository.md) > [closePointInTime](./kibana-plugin-core-server.savedobjectsrepository.closepointintime.md) + +## SavedObjectsRepository.closePointInTime() method + +Closes a Point In Time (PIT) by ID. This simply proxies the request to ES via the Elasticsearch client, and is included in the Saved Objects Client as a convenience for consumers who are using `openPointInTimeForType`. + +Signature: + +```typescript +closePointInTime(id: string, options?: SavedObjectsClosePointInTimeOptions): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| id | string | | +| options | SavedObjectsClosePointInTimeOptions | | + +Returns: + +`Promise` + +{promise} - [SavedObjectsClosePointInTimeResponse](./kibana-plugin-core-server.savedobjectsclosepointintimeresponse.md) + +## Remarks + +While the `keepAlive` that is provided will cause a PIT to automatically close, it is highly recommended to explicitly close a PIT when you are done with it in order to avoid consuming unneeded resources in Elasticsearch. + +## Example + + +```ts +const repository = coreStart.savedObjects.createInternalRepository(); + +const { id } = await repository.openPointInTimeForType( + type: 'index-pattern', + { keepAlive: '2m' }, +); + +const response = await repository.find({ + type: 'index-pattern', + search: 'foo*', + sortField: 'name', + sortOrder: 'desc', + pit: { + id: 'abc123', + keepAlive: '2m', + }, + searchAfter: [1234, 'abcd'], +}); + +await repository.closePointInTime(response.pit_id); + +``` + diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.md index 4d13fea12572c..632d9c279cb88 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.md @@ -20,6 +20,7 @@ export declare class SavedObjectsRepository | [bulkGet(objects, options)](./kibana-plugin-core-server.savedobjectsrepository.bulkget.md) | | Returns an array of objects by id | | [bulkUpdate(objects, options)](./kibana-plugin-core-server.savedobjectsrepository.bulkupdate.md) | | Updates multiple objects in bulk | | [checkConflicts(objects, options)](./kibana-plugin-core-server.savedobjectsrepository.checkconflicts.md) | | Check what conflicts will result when creating a given array of saved objects. This includes "unresolvable conflicts", which are multi-namespace objects that exist in a different namespace; such conflicts cannot be resolved/overwritten. | +| [closePointInTime(id, options)](./kibana-plugin-core-server.savedobjectsrepository.closepointintime.md) | | Closes a Point In Time (PIT) by ID. This simply proxies the request to ES via the Elasticsearch client, and is included in the Saved Objects Client as a convenience for consumers who are using openPointInTimeForType. | | [create(type, attributes, options)](./kibana-plugin-core-server.savedobjectsrepository.create.md) | | Persists an object | | [delete(type, id, options)](./kibana-plugin-core-server.savedobjectsrepository.delete.md) | | Deletes an object | | [deleteByNamespace(namespace, options)](./kibana-plugin-core-server.savedobjectsrepository.deletebynamespace.md) | | Deletes all objects from the provided namespace. | @@ -27,6 +28,7 @@ export declare class SavedObjectsRepository | [find(options)](./kibana-plugin-core-server.savedobjectsrepository.find.md) | | | | [get(type, id, options)](./kibana-plugin-core-server.savedobjectsrepository.get.md) | | Gets a single object | | [incrementCounter(type, id, counterFields, options)](./kibana-plugin-core-server.savedobjectsrepository.incrementcounter.md) | | Increments all the specified counter fields (by one by default). Creates the document if one doesn't exist for the given id. | +| [openPointInTimeForType(type, { keepAlive, preference })](./kibana-plugin-core-server.savedobjectsrepository.openpointintimefortype.md) | | Opens a Point In Time (PIT) against the indices for the specified Saved Object types. The returned id can then be passed to SavedObjects.find to search against that PIT. | | [removeReferencesTo(type, id, options)](./kibana-plugin-core-server.savedobjectsrepository.removereferencesto.md) | | Updates all objects containing a reference to the given {type, id} tuple to remove the said reference. | | [resolve(type, id, options)](./kibana-plugin-core-server.savedobjectsrepository.resolve.md) | | Resolves a single object, using any legacy URL alias if it exists | | [update(type, id, attributes, options)](./kibana-plugin-core-server.savedobjectsrepository.update.md) | | Updates an object | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.openpointintimefortype.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.openpointintimefortype.md new file mode 100644 index 0000000000000..63956ebee68f7 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.openpointintimefortype.md @@ -0,0 +1,57 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsRepository](./kibana-plugin-core-server.savedobjectsrepository.md) > [openPointInTimeForType](./kibana-plugin-core-server.savedobjectsrepository.openpointintimefortype.md) + +## SavedObjectsRepository.openPointInTimeForType() method + +Opens a Point In Time (PIT) against the indices for the specified Saved Object types. The returned `id` can then be passed to `SavedObjects.find` to search against that PIT. + +Signature: + +```typescript +openPointInTimeForType(type: string | string[], { keepAlive, preference }?: SavedObjectsOpenPointInTimeOptions): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| type | string | string[] | | +| { keepAlive, preference } | SavedObjectsOpenPointInTimeOptions | | + +Returns: + +`Promise` + +{promise} - { id: string } + +## Example + + +```ts +const repository = coreStart.savedObjects.createInternalRepository(); + +const { id } = await repository.openPointInTimeForType( + type: 'index-pattern', + { keepAlive: '2m' }, +); +const page1 = await savedObjectsClient.find({ + type: 'visualization', + sortField: 'updated_at', + sortOrder: 'asc', + pit, +}); + +const lastHit = page1.saved_objects[page1.saved_objects.length - 1]; +const page2 = await savedObjectsClient.find({ + type: 'visualization', + sortField: 'updated_at', + sortOrder: 'asc', + pit: { id: page1.pit_id }, + searchAfter: lastHit.sort, +}); + +await savedObjectsClient.closePointInTime(page2.pit_id); + +``` + diff --git a/docs/development/core/server/kibana-plugin-core-server.searchresponse.hits.md b/docs/development/core/server/kibana-plugin-core-server.searchresponse.hits.md index 1629e77425525..599c4e3ad6319 100644 --- a/docs/development/core/server/kibana-plugin-core-server.searchresponse.hits.md +++ b/docs/development/core/server/kibana-plugin-core-server.searchresponse.hits.md @@ -22,7 +22,7 @@ hits: { highlight?: any; inner_hits?: any; matched_queries?: string[]; - sort?: string[]; + sort?: unknown[]; }>; }; ``` diff --git a/docs/development/core/server/kibana-plugin-core-server.searchresponse.md b/docs/development/core/server/kibana-plugin-core-server.searchresponse.md index b53cbf0d87f24..cbaab4632014d 100644 --- a/docs/development/core/server/kibana-plugin-core-server.searchresponse.md +++ b/docs/development/core/server/kibana-plugin-core-server.searchresponse.md @@ -18,7 +18,8 @@ export interface SearchResponse | [\_scroll\_id](./kibana-plugin-core-server.searchresponse._scroll_id.md) | string | | | [\_shards](./kibana-plugin-core-server.searchresponse._shards.md) | ShardsResponse | | | [aggregations](./kibana-plugin-core-server.searchresponse.aggregations.md) | any | | -| [hits](./kibana-plugin-core-server.searchresponse.hits.md) | {
total: number;
max_score: number;
hits: Array<{
_index: string;
_type: string;
_id: string;
_score: number;
_source: T;
_version?: number;
_explanation?: Explanation;
fields?: any;
highlight?: any;
inner_hits?: any;
matched_queries?: string[];
sort?: string[];
}>;
} | | +| [hits](./kibana-plugin-core-server.searchresponse.hits.md) | {
total: number;
max_score: number;
hits: Array<{
_index: string;
_type: string;
_id: string;
_score: number;
_source: T;
_version?: number;
_explanation?: Explanation;
fields?: any;
highlight?: any;
inner_hits?: any;
matched_queries?: string[];
sort?: unknown[];
}>;
} | | +| [pit\_id](./kibana-plugin-core-server.searchresponse.pit_id.md) | string | | | [timed\_out](./kibana-plugin-core-server.searchresponse.timed_out.md) | boolean | | | [took](./kibana-plugin-core-server.searchresponse.took.md) | number | | diff --git a/docs/development/core/server/kibana-plugin-core-server.searchresponse.pit_id.md b/docs/development/core/server/kibana-plugin-core-server.searchresponse.pit_id.md new file mode 100644 index 0000000000000..f214bc0538045 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.searchresponse.pit_id.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SearchResponse](./kibana-plugin-core-server.searchresponse.md) > [pit\_id](./kibana-plugin-core-server.searchresponse.pit_id.md) + +## SearchResponse.pit\_id property + +Signature: + +```typescript +pit_id?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.md b/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.md index d35afc4a149d1..4bb7be77c595a 100644 --- a/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.md +++ b/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.md @@ -23,6 +23,7 @@ export interface UiSettingsParams | [name](./kibana-plugin-core-server.uisettingsparams.name.md) | string | title in the UI | | [optionLabels](./kibana-plugin-core-server.uisettingsparams.optionlabels.md) | Record<string, string> | text labels for 'select' type UI element | | [options](./kibana-plugin-core-server.uisettingsparams.options.md) | string[] | array of permitted values for this setting | +| [order](./kibana-plugin-core-server.uisettingsparams.order.md) | number | index of the settings within its category (ascending order, smallest will be displayed first). Used for ordering in the UI. settings without order defined will be displayed last and ordered by name | | [readonly](./kibana-plugin-core-server.uisettingsparams.readonly.md) | boolean | a flag indicating that value cannot be changed | | [requiresPageReload](./kibana-plugin-core-server.uisettingsparams.requirespagereload.md) | boolean | a flag indicating whether new value applying requires page reloading | | [schema](./kibana-plugin-core-server.uisettingsparams.schema.md) | Type<T> | | diff --git a/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.order.md b/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.order.md new file mode 100644 index 0000000000000..140bdad5d786b --- /dev/null +++ b/docs/development/core/server/kibana-plugin-core-server.uisettingsparams.order.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [UiSettingsParams](./kibana-plugin-core-server.uisettingsparams.md) > [order](./kibana-plugin-core-server.uisettingsparams.order.md) + +## UiSettingsParams.order property + +index of the settings within its category (ascending order, smallest will be displayed first). Used for ordering in the UI. + + settings without order defined will be displayed last and ordered by name + +Signature: + +```typescript +order?: number; +``` diff --git a/docs/development/core/server/kibana-plugin-core-server.uisettingstype.md b/docs/development/core/server/kibana-plugin-core-server.uisettingstype.md index 3c439897ea031..7edee442baa24 100644 --- a/docs/development/core/server/kibana-plugin-core-server.uisettingstype.md +++ b/docs/development/core/server/kibana-plugin-core-server.uisettingstype.md @@ -9,5 +9,5 @@ UI element type to represent the settings. Signature: ```typescript -export declare type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image'; +export declare type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image' | 'color'; ``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md index f576d795b93a5..d2e7ef9db05e8 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.md @@ -126,6 +126,7 @@ | [noSearchSessionStorageCapabilityMessage](./kibana-plugin-plugins-data-public.nosearchsessionstoragecapabilitymessage.md) | Message to display in case storing session session is disabled due to turned off capability | | [parseSearchSourceJSON](./kibana-plugin-plugins-data-public.parsesearchsourcejson.md) | | | [QueryStringInput](./kibana-plugin-plugins-data-public.querystringinput.md) | | +| [SEARCH\_SESSIONS\_MANAGEMENT\_ID](./kibana-plugin-plugins-data-public.search_sessions_management_id.md) | | | [search](./kibana-plugin-plugins-data-public.search.md) | | | [SearchBar](./kibana-plugin-plugins-data-public.searchbar.md) | | | [syncQueryStateWithUrl](./kibana-plugin-plugins-data-public.syncquerystatewithurl.md) | Helper to setup syncing of global data with the URL | diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.search_sessions_management_id.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.search_sessions_management_id.md new file mode 100644 index 0000000000000..ad16d21403a98 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.search_sessions_management_id.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [SEARCH\_SESSIONS\_MANAGEMENT\_ID](./kibana-plugin-plugins-data-public.search_sessions_management_id.md) + +## SEARCH\_SESSIONS\_MANAGEMENT\_ID variable + +Signature: + +```typescript +SEARCH_SESSIONS_MANAGEMENT_ID = "search_sessions" +``` diff --git a/docs/user/alerting/alerting-production-considerations.asciidoc b/docs/user/alerting/alerting-production-considerations.asciidoc index 3a68e81879e24..cc7adc87b150e 100644 --- a/docs/user/alerting/alerting-production-considerations.asciidoc +++ b/docs/user/alerting/alerting-production-considerations.asciidoc @@ -27,4 +27,9 @@ Because by default tasks are polled at 3 second intervals and only 10 tasks can For details on the settings that can influence the performance and throughput of Task Manager, see {task-manager-settings}. -============================================== \ No newline at end of file +============================================== + +[float] +=== Deployment considerations + +{es} and {kib} instances use the system clock to determine the current time. To ensure schedules are triggered when expected, you should synchronize the clocks of all nodes in the cluster using a time service such as http://www.ntp.org/[Network Time Protocol]. \ No newline at end of file diff --git a/docs/user/dashboard/vega-reference.asciidoc b/docs/user/dashboard/vega-reference.asciidoc index fa4a1902ebd81..327506b3eb949 100644 --- a/docs/user/dashboard/vega-reference.asciidoc +++ b/docs/user/dashboard/vega-reference.asciidoc @@ -401,7 +401,9 @@ Vega-Lite compilation. [[vega-expression-functions]] ===== Expression functions which can update the time range and dashboard filters -{kib} has extended the Vega expression language with these functions: +{kib} has extended the Vega expression language with these functions. +These functions will trigger new data to be fetched, which by default will reset Vega signals. +To keep signal values set `restoreSignalValuesOnRefresh: true` in the Vega config. ```js /** @@ -444,6 +446,8 @@ kibanaSetTimeFilter(start, end) hideWarnings: true // Vega renderer to use: `svg` or `canvas` (default) renderer: canvas + // Defaults to 'false', restores Vega signal values on refresh + restoreSignalValuesOnRefresh: false } } } diff --git a/docs/user/dev-tools.asciidoc b/docs/user/dev-tools.asciidoc index 0ee7fbc741e00..0c5bef489dd01 100644 --- a/docs/user/dev-tools.asciidoc +++ b/docs/user/dev-tools.asciidoc @@ -15,7 +15,7 @@ a| <> | Interact with the REST API of Elasticsearch, including sending requests and viewing API documentation. -a| <> +a| <> | Inspect and analyze your search queries. diff --git a/docs/user/security/audit-logging.asciidoc b/docs/user/security/audit-logging.asciidoc index 12a87b1422c5c..b9fc0c9c4ac46 100644 --- a/docs/user/security/audit-logging.asciidoc +++ b/docs/user/security/audit-logging.asciidoc @@ -85,6 +85,10 @@ Refer to the corresponding {es} logs for potential write errors. | `unknown` | User is creating a saved object. | `failure` | User is not authorized to create a saved object. +.2+| `saved_object_open_point_in_time` +| `unknown` | User is creating a Point In Time to use when querying saved objects. +| `failure` | User is not authorized to create a Point In Time for the provided saved object types. + .2+| `connector_create` | `unknown` | User is creating a connector. | `failure` | User is not authorized to create a connector. @@ -171,6 +175,10 @@ Refer to the corresponding {es} logs for potential write errors. | `unknown` | User is deleting a saved object. | `failure` | User is not authorized to delete a saved object. +.2+| `saved_object_close_point_in_time` +| `unknown` | User is deleting a Point In Time that was used to query saved objects. +| `failure` | User is not authorized to delete a Point In Time. + .2+| `connector_delete` | `unknown` | User is deleting a connector. | `failure` | User is not authorized to delete a connector. diff --git a/package.json b/package.json index 06c233b1ff544..2af2233836c90 100644 --- a/package.json +++ b/package.json @@ -348,7 +348,7 @@ "@cypress/webpack-preprocessor": "^5.5.0", "@elastic/apm-rum": "^5.6.1", "@elastic/apm-rum-react": "^1.2.5", - "@elastic/charts": "24.4.0", + "@elastic/charts": "24.5.1", "@elastic/eslint-config-kibana": "link:packages/elastic-eslint-config-kibana", "@elastic/eslint-plugin-eui": "0.0.2", "@elastic/github-checks-reporter": "0.0.20b3", diff --git a/packages/kbn-optimizer/limits.yml b/packages/kbn-optimizer/limits.yml index a364aa4c8de29..657aabca1e86d 100644 --- a/packages/kbn-optimizer/limits.yml +++ b/packages/kbn-optimizer/limits.yml @@ -91,7 +91,7 @@ pageLoadAssetSize: visTypeMetric: 42790 visTypeTable: 95078 visTypeTagcloud: 37575 - visTypeTimelion: 51933 + visTypeTimelion: 68883 visTypeTimeseries: 155347 visTypeVega: 153861 visTypeVislib: 242982 @@ -105,3 +105,4 @@ pageLoadAssetSize: spacesOss: 18817 osquery: 107090 fileUpload: 25664 + banners: 17946 diff --git a/packages/kbn-test/jest-preset.js b/packages/kbn-test/jest-preset.js index 127acd8d0beb5..d6e5ea637ed7b 100644 --- a/packages/kbn-test/jest-preset.js +++ b/packages/kbn-test/jest-preset.js @@ -37,7 +37,6 @@ module.exports = { '\\.ace\\.worker.js$': '/packages/kbn-test/target/jest/mocks/worker_module_mock.js', '\\.editor\\.worker.js$': '/packages/kbn-test/target/jest/mocks/worker_module_mock.js', '^(!!)?file-loader!': '/packages/kbn-test/target/jest/mocks/file_mock.js', - '^fixtures/(.*)': '/src/fixtures/$1', '^src/core/(.*)': '/src/core/$1', '^src/plugins/(.*)': '/src/plugins/$1', }, diff --git a/src/core/public/_mixins.scss b/src/core/public/_mixins.scss new file mode 100644 index 0000000000000..2dbef465e074e --- /dev/null +++ b/src/core/public/_mixins.scss @@ -0,0 +1,43 @@ +@import './variables'; + +/* stylelint-disable-next-line length-zero-no-unit -- need consistent unit to sum them */ +@mixin kibanaFullBodyHeight($additionalOffset: 0px) { + // default - header, no banner + height: calc(100vh - #{$kbnHeaderOffset + $additionalOffset}); + + @at-root { + // no header, no banner + .kbnBody--chromeHidden & { + height: calc(100vh - #{$additionalOffset}); + } + // header, banner + .kbnBody--hasHeaderBanner & { + height: calc(100vh - #{$kbnHeaderOffsetWithBanner + $additionalOffset}); + } + // no header, banner + .kbnBody--chromeHidden.kbnBody--hasHeaderBanner & { + height: calc(100vh - #{$kbnHeaderBannerHeight + $additionalOffset}); + } + } +} + +/* stylelint-disable-next-line length-zero-no-unit -- need consistent unit to sum them */ +@mixin kibanaFullBodyMinHeight($additionalOffset: 0px) { + // default - header, no banner + min-height: calc(100vh - #{$kbnHeaderOffset + $additionalOffset}); + + @at-root { + // no header, no banner + .kbnBody--chromeHidden & { + min-height: calc(100vh - #{$additionalOffset}); + } + // header, banner + .kbnBody--hasHeaderBanner & { + min-height: calc(100vh - #{$kbnHeaderOffsetWithBanner + $additionalOffset}); + } + // no header, banner + .kbnBody--chromeHidden.kbnBody--hasHeaderBanner & { + min-height: calc(100vh - #{$kbnHeaderBannerHeight + $additionalOffset}); + } + } +} diff --git a/src/core/public/_variables.scss b/src/core/public/_variables.scss index 8c054e770bd4b..f6ff5619bfc53 100644 --- a/src/core/public/_variables.scss +++ b/src/core/public/_variables.scss @@ -1,3 +1,8 @@ @import '@elastic/eui/src/global_styling/variables/header'; +// height of the header banner +$kbnHeaderBannerHeight: $euiSizeXL; +// total height of the header (when the banner is *not* present) $kbnHeaderOffset: $euiHeaderHeightCompensation * 2; +// total height of the header when the banner is present +$kbnHeaderOffsetWithBanner: $kbnHeaderOffset + $kbnHeaderBannerHeight; diff --git a/src/core/public/chrome/chrome_service.mock.ts b/src/core/public/chrome/chrome_service.mock.ts index cb0876f6bc725..ae9c58af69603 100644 --- a/src/core/public/chrome/chrome_service.mock.ts +++ b/src/core/public/chrome/chrome_service.mock.ts @@ -61,6 +61,8 @@ const createStartContractMock = () => { getIsNavDrawerLocked$: jest.fn(), getCustomNavLink$: jest.fn(), setCustomNavLink: jest.fn(), + setHeaderBanner: jest.fn(), + getBodyClasses$: jest.fn(), }; startContract.navLinks.getAll.mockReturnValue([]); startContract.getBrand$.mockReturnValue(new BehaviorSubject({} as ChromeBrand)); @@ -72,6 +74,7 @@ const createStartContractMock = () => { startContract.getCustomNavLink$.mockReturnValue(new BehaviorSubject(undefined)); startContract.getHelpExtension$.mockReturnValue(new BehaviorSubject(undefined)); startContract.getIsNavDrawerLocked$.mockReturnValue(new BehaviorSubject(false)); + startContract.getBodyClasses$.mockReturnValue(new BehaviorSubject([])); return startContract; }; diff --git a/src/core/public/chrome/chrome_service.tsx b/src/core/public/chrome/chrome_service.tsx index ee8d1c17ccd59..e69bf9025fdb9 100644 --- a/src/core/public/chrome/chrome_service.tsx +++ b/src/core/public/chrome/chrome_service.tsx @@ -6,69 +6,37 @@ * Side Public License, v 1. */ -import { EuiBreadcrumb, IconType } from '@elastic/eui'; import React from 'react'; import { FormattedMessage } from '@kbn/i18n/react'; import { BehaviorSubject, combineLatest, merge, Observable, of, ReplaySubject } from 'rxjs'; import { flatMap, map, takeUntil } from 'rxjs/operators'; import { parse } from 'url'; import { EuiLink } from '@elastic/eui'; -import { MountPoint } from '../types'; import { mountReactNode } from '../utils/mount'; import { InternalApplicationStart } from '../application'; import { DocLinksStart } from '../doc_links'; import { HttpStart } from '../http'; import { InjectedMetadataStart } from '../injected_metadata'; import { NotificationsStart } from '../notifications'; -import { IUiSettingsClient } from '../ui_settings'; import { KIBANA_ASK_ELASTIC_LINK } from './constants'; import { ChromeDocTitle, DocTitleService } from './doc_title'; import { ChromeNavControls, NavControlsService } from './nav_controls'; -import { ChromeNavLinks, NavLinksService, ChromeNavLink } from './nav_links'; +import { NavLinksService, ChromeNavLink } from './nav_links'; import { ChromeRecentlyAccessed, RecentlyAccessedService } from './recently_accessed'; import { Header } from './ui'; -import { ChromeHelpExtensionMenuLink } from './ui/header/header_help_menu'; export { ChromeNavControls, ChromeRecentlyAccessed, ChromeDocTitle }; +import { + ChromeBadge, + ChromeBrand, + ChromeBreadcrumb, + ChromeBreadcrumbsAppendExtension, + ChromeHelpExtension, + InternalChromeStart, + ChromeUserBanner, +} from './types'; const IS_LOCKED_KEY = 'core.chrome.isLocked'; -/** @public */ -export interface ChromeBadge { - text: string; - tooltip: string; - iconType?: IconType; -} - -/** @public */ -export interface ChromeBrand { - logo?: string; - smallLogo?: string; -} - -/** @public */ -export type ChromeBreadcrumb = EuiBreadcrumb; - -/** @public */ -export interface ChromeBreadcrumbsAppendExtension { - content: MountPoint; -} - -/** @public */ -export interface ChromeHelpExtension { - /** - * Provide your plugin's name to create a header for separation - */ - appName: string; - /** - * Creates unified links for sending users to documentation, GitHub, Discuss, or a custom link/button - */ - links?: ChromeHelpExtensionMenuLink[]; - /** - * Custom content to occur below the list of links - */ - content?: (element: HTMLDivElement) => () => void; -} - interface ConstructorParams { browserSupportsCsp: boolean; } @@ -79,7 +47,6 @@ interface StartDeps { http: HttpStart; injectedMetadata: InjectedMetadataStart; notifications: NotificationsStart; - uiSettings: IUiSettingsClient; } /** @internal */ @@ -132,7 +99,6 @@ export class ChromeService { http, injectedMetadata, notifications, - uiSettings, }: StartDeps): Promise { this.initVisibility(application); @@ -149,6 +115,17 @@ export class ChromeService { const helpSupportUrl$ = new BehaviorSubject(KIBANA_ASK_ELASTIC_LINK); const isNavDrawerLocked$ = new BehaviorSubject(localStorage.getItem(IS_LOCKED_KEY) === 'true'); + const headerBanner$ = new BehaviorSubject(undefined); + const bodyClasses$ = combineLatest([headerBanner$, this.isVisible$!]).pipe( + map(([headerBanner, isVisible]) => { + return [ + 'kbnBody', + headerBanner ? 'kbnBody--hasHeaderBanner' : 'kbnBody--noHeaderBanner', + isVisible ? 'kbnBody--chromeVisible' : 'kbnBody--chromeHidden', + ]; + }) + ); + const navControls = this.navControls.start(); const navLinks = this.navLinks.start({ application, http }); const recentlyAccessed = await this.recentlyAccessed.start({ http }); @@ -220,6 +197,7 @@ export class ChromeService { loadingCount$={http.getLoadingCount$()} application={application} appTitle$={appTitle$.pipe(takeUntil(this.stop$))} + headerBanner$={headerBanner$.pipe(takeUntil(this.stop$))} badge$={badge$.pipe(takeUntil(this.stop$))} basePath={http.basePath} breadcrumbs$={breadcrumbs$.pipe(takeUntil(this.stop$))} @@ -312,6 +290,12 @@ export class ChromeService { setCustomNavLink: (customNavLink?: ChromeNavLink) => { customNavLink$.next(customNavLink); }, + + setHeaderBanner: (headerBanner?: ChromeUserBanner) => { + headerBanner$.next(headerBanner); + }, + + getBodyClasses$: () => bodyClasses$.pipe(takeUntil(this.stop$)), }; } @@ -320,173 +304,3 @@ export class ChromeService { this.stop$.next(); } } - -/** - * ChromeStart allows plugins to customize the global chrome header UI and - * enrich the UX with additional information about the current location of the - * browser. - * - * @remarks - * While ChromeStart exposes many APIs, they should be used sparingly and the - * developer should understand how they affect other plugins and applications. - * - * @example - * How to add a recently accessed item to the sidebar: - * ```ts - * core.chrome.recentlyAccessed.add('/app/map/1234', 'Map 1234', '1234'); - * ``` - * - * @example - * How to set the help dropdown extension: - * ```tsx - * core.chrome.setHelpExtension(elem => { - * ReactDOM.render(, elem); - * return () => ReactDOM.unmountComponentAtNode(elem); - * }); - * ``` - * - * @public - */ -export interface ChromeStart { - /** {@inheritdoc ChromeNavLinks} */ - navLinks: ChromeNavLinks; - /** {@inheritdoc ChromeNavControls} */ - navControls: ChromeNavControls; - /** {@inheritdoc ChromeRecentlyAccessed} */ - recentlyAccessed: ChromeRecentlyAccessed; - /** {@inheritdoc ChromeDocTitle} */ - docTitle: ChromeDocTitle; - - /** - * Sets the current app's title - * - * @internalRemarks - * This should be handled by the application service once it is in charge - * of mounting applications. - */ - setAppTitle(appTitle: string): void; - - /** - * Get an observable of the current brand information. - */ - getBrand$(): Observable; - - /** - * Set the brand configuration. - * - * @remarks - * Normally the `logo` property will be rendered as the - * CSS background for the home link in the chrome navigation, but when the page is - * rendered in a small window the `smallLogo` will be used and rendered at about - * 45px wide. - * - * @example - * ```js - * chrome.setBrand({ - * logo: 'url(/plugins/app/logo.png) center no-repeat' - * smallLogo: 'url(/plugins/app/logo-small.png) center no-repeat' - * }) - * ``` - * - */ - setBrand(brand: ChromeBrand): void; - - /** - * Get an observable of the current visibility state of the chrome. - */ - getIsVisible$(): Observable; - - /** - * Set the temporary visibility for the chrome. This does nothing if the chrome is hidden - * by default and should be used to hide the chrome for things like full-screen modes - * with an exit button. - */ - setIsVisible(isVisible: boolean): void; - - /** - * Get the current set of classNames that will be set on the application container. - */ - getApplicationClasses$(): Observable; - - /** - * Add a className that should be set on the application container. - */ - addApplicationClass(className: string): void; - - /** - * Remove a className added with `addApplicationClass()`. If className is unknown it is ignored. - */ - removeApplicationClass(className: string): void; - - /** - * Get an observable of the current badge - */ - getBadge$(): Observable; - - /** - * Override the current badge - */ - setBadge(badge?: ChromeBadge): void; - - /** - * Get an observable of the current list of breadcrumbs - */ - getBreadcrumbs$(): Observable; - - /** - * Override the current set of breadcrumbs - */ - setBreadcrumbs(newBreadcrumbs: ChromeBreadcrumb[]): void; - - /** - * Get an observable of the current extension appended to breadcrumbs - */ - getBreadcrumbsAppendExtension$(): Observable; - - /** - * Mount an element next to the last breadcrumb - */ - setBreadcrumbsAppendExtension( - breadcrumbsAppendExtension?: ChromeBreadcrumbsAppendExtension - ): void; - - /** - * Get an observable of the current custom nav link - */ - getCustomNavLink$(): Observable | undefined>; - - /** - * Override the current set of custom nav link - */ - setCustomNavLink(newCustomNavLink?: Partial): void; - - /** - * Get an observable of the current custom help conttent - */ - getHelpExtension$(): Observable; - - /** - * Override the current set of custom help content - */ - setHelpExtension(helpExtension?: ChromeHelpExtension): void; - - /** - * Override the default support URL shown in the help menu - * @param url The updated support URL - */ - setHelpSupportUrl(url: string): void; - - /** - * Get an observable of the current locked state of the nav drawer. - */ - getIsNavDrawerLocked$(): Observable; -} - -/** @internal */ -export interface InternalChromeStart extends ChromeStart { - /** - * Used only by MountingService to render the header UI - * @internal - */ - getHeaderComponent(): JSX.Element; -} diff --git a/src/core/public/chrome/index.ts b/src/core/public/chrome/index.ts index cf4106a42e0d4..069d29ca70d01 100644 --- a/src/core/public/chrome/index.ts +++ b/src/core/public/chrome/index.ts @@ -6,15 +6,7 @@ * Side Public License, v 1. */ -export { - ChromeBadge, - ChromeBreadcrumb, - ChromeService, - ChromeStart, - InternalChromeStart, - ChromeBrand, - ChromeHelpExtension, -} from './chrome_service'; +export { ChromeService } from './chrome_service'; export { ChromeHelpExtensionLinkBase, ChromeHelpExtensionMenuLink, @@ -28,3 +20,13 @@ export { ChromeNavLink, ChromeNavLinks, ChromeNavLinkUpdateableFields } from './ export { ChromeRecentlyAccessed, ChromeRecentlyAccessedHistoryItem } from './recently_accessed'; export { ChromeNavControl, ChromeNavControls } from './nav_controls'; export { ChromeDocTitle } from './doc_title'; +export { + InternalChromeStart, + ChromeStart, + ChromeHelpExtension, + ChromeBreadcrumbsAppendExtension, + ChromeBreadcrumb, + ChromeBrand, + ChromeBadge, + ChromeUserBanner, +} from './types'; diff --git a/src/core/public/chrome/types.ts b/src/core/public/chrome/types.ts new file mode 100644 index 0000000000000..732236f1ba4a1 --- /dev/null +++ b/src/core/public/chrome/types.ts @@ -0,0 +1,241 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { EuiBreadcrumb, IconType } from '@elastic/eui'; +import { Observable } from 'rxjs'; +import { MountPoint } from '../types'; +import { ChromeDocTitle } from './doc_title'; +import { ChromeNavControls } from './nav_controls'; +import { ChromeNavLinks, ChromeNavLink } from './nav_links'; +import { ChromeRecentlyAccessed } from './recently_accessed'; +import { ChromeHelpExtensionMenuLink } from './ui/header/header_help_menu'; + +/** @public */ +export interface ChromeBadge { + text: string; + tooltip: string; + iconType?: IconType; +} + +/** @public */ +export interface ChromeBrand { + logo?: string; + smallLogo?: string; +} + +/** @public */ +export type ChromeBreadcrumb = EuiBreadcrumb; + +/** @public */ +export interface ChromeBreadcrumbsAppendExtension { + content: MountPoint; +} + +/** @public */ +export interface ChromeUserBanner { + content: MountPoint; +} + +/** @public */ +export interface ChromeHelpExtension { + /** + * Provide your plugin's name to create a header for separation + */ + appName: string; + /** + * Creates unified links for sending users to documentation, GitHub, Discuss, or a custom link/button + */ + links?: ChromeHelpExtensionMenuLink[]; + /** + * Custom content to occur below the list of links + */ + content?: (element: HTMLDivElement) => () => void; +} + +/** + * ChromeStart allows plugins to customize the global chrome header UI and + * enrich the UX with additional information about the current location of the + * browser. + * + * @remarks + * While ChromeStart exposes many APIs, they should be used sparingly and the + * developer should understand how they affect other plugins and applications. + * + * @example + * How to add a recently accessed item to the sidebar: + * ```ts + * core.chrome.recentlyAccessed.add('/app/map/1234', 'Map 1234', '1234'); + * ``` + * + * @example + * How to set the help dropdown extension: + * ```tsx + * core.chrome.setHelpExtension(elem => { + * ReactDOM.render(, elem); + * return () => ReactDOM.unmountComponentAtNode(elem); + * }); + * ``` + * + * @public + */ +export interface ChromeStart { + /** {@inheritdoc ChromeNavLinks} */ + navLinks: ChromeNavLinks; + /** {@inheritdoc ChromeNavControls} */ + navControls: ChromeNavControls; + /** {@inheritdoc ChromeRecentlyAccessed} */ + recentlyAccessed: ChromeRecentlyAccessed; + /** {@inheritdoc ChromeDocTitle} */ + docTitle: ChromeDocTitle; + + /** + * Sets the current app's title + * + * @internalRemarks + * This should be handled by the application service once it is in charge + * of mounting applications. + */ + setAppTitle(appTitle: string): void; + + /** + * Get an observable of the current brand information. + */ + getBrand$(): Observable; + + /** + * Set the brand configuration. + * + * @remarks + * Normally the `logo` property will be rendered as the + * CSS background for the home link in the chrome navigation, but when the page is + * rendered in a small window the `smallLogo` will be used and rendered at about + * 45px wide. + * + * @example + * ```js + * chrome.setBrand({ + * logo: 'url(/plugins/app/logo.png) center no-repeat' + * smallLogo: 'url(/plugins/app/logo-small.png) center no-repeat' + * }) + * ``` + * + */ + setBrand(brand: ChromeBrand): void; + + /** + * Get an observable of the current visibility state of the chrome. + */ + getIsVisible$(): Observable; + + /** + * Set the temporary visibility for the chrome. This does nothing if the chrome is hidden + * by default and should be used to hide the chrome for things like full-screen modes + * with an exit button. + */ + setIsVisible(isVisible: boolean): void; + + /** + * Get the current set of classNames that will be set on the application container. + */ + getApplicationClasses$(): Observable; + + /** + * Add a className that should be set on the application container. + */ + addApplicationClass(className: string): void; + + /** + * Remove a className added with `addApplicationClass()`. If className is unknown it is ignored. + */ + removeApplicationClass(className: string): void; + + /** + * Get an observable of the current badge + */ + getBadge$(): Observable; + + /** + * Override the current badge + */ + setBadge(badge?: ChromeBadge): void; + + /** + * Get an observable of the current list of breadcrumbs + */ + getBreadcrumbs$(): Observable; + + /** + * Override the current set of breadcrumbs + */ + setBreadcrumbs(newBreadcrumbs: ChromeBreadcrumb[]): void; + + /** + * Get an observable of the current extension appended to breadcrumbs + */ + getBreadcrumbsAppendExtension$(): Observable; + + /** + * Mount an element next to the last breadcrumb + */ + setBreadcrumbsAppendExtension( + breadcrumbsAppendExtension?: ChromeBreadcrumbsAppendExtension + ): void; + + /** + * Get an observable of the current custom nav link + */ + getCustomNavLink$(): Observable | undefined>; + + /** + * Override the current set of custom nav link + */ + setCustomNavLink(newCustomNavLink?: Partial): void; + + /** + * Get an observable of the current custom help conttent + */ + getHelpExtension$(): Observable; + + /** + * Override the current set of custom help content + */ + setHelpExtension(helpExtension?: ChromeHelpExtension): void; + + /** + * Override the default support URL shown in the help menu + * @param url The updated support URL + */ + setHelpSupportUrl(url: string): void; + + /** + * Get an observable of the current locked state of the nav drawer. + */ + getIsNavDrawerLocked$(): Observable; + + /** + * Set the banner that will appear on top of the chrome header. + * + * @remarks Using `undefined` when invoking this API will remove the banner. + */ + setHeaderBanner(headerBanner?: ChromeUserBanner): void; +} + +/** @internal */ +export interface InternalChromeStart extends ChromeStart { + /** + * Used only by the rendering service to render the header UI + * @internal + */ + getHeaderComponent(): JSX.Element; + /** + * Used only by the rendering service to retrieve the set of classNames + * that will be set on the body element. + * @internal + */ + getBodyClasses$(): Observable; +} diff --git a/src/core/public/chrome/ui/header/__snapshots__/header.test.tsx.snap b/src/core/public/chrome/ui/header/__snapshots__/header.test.tsx.snap index 9e7906250949e..4f31c952b8826 100644 --- a/src/core/public/chrome/ui/header/__snapshots__/header.test.tsx.snap +++ b/src/core/public/chrome/ui/header/__snapshots__/header.test.tsx.snap @@ -452,6 +452,55 @@ exports[`Header renders 1`] = ` "thrownError": null, } } + headerBanner$={ + BehaviorSubject { + "_isScalar": false, + "_value": undefined, + "closed": false, + "hasError": false, + "isStopped": false, + "observers": Array [ + Subscriber { + "_parentOrParents": null, + "_subscriptions": Array [ + SubjectSubscription { + "_parentOrParents": [Circular], + "_subscriptions": null, + "closed": false, + "subject": [Circular], + "subscriber": [Circular], + }, + ], + "closed": false, + "destination": SafeSubscriber { + "_complete": undefined, + "_context": [Circular], + "_error": undefined, + "_next": [Function], + "_parentOrParents": null, + "_parentSubscriber": [Circular], + "_subscriptions": null, + "closed": false, + "destination": Object { + "closed": true, + "complete": [Function], + "error": [Function], + "next": [Function], + }, + "isStopped": false, + "syncErrorThrowable": false, + "syncErrorThrown": false, + "syncErrorValue": null, + }, + "isStopped": false, + "syncErrorThrowable": true, + "syncErrorThrown": false, + "syncErrorValue": null, + }, + ], + "thrownError": null, + } + } helpExtension$={ BehaviorSubject { "_isScalar": false, @@ -1699,14 +1748,67 @@ exports[`Header renders 1`] = ` } } > +
({ htmlIdGenerator: () => () => 'mockId', @@ -63,6 +63,7 @@ describe('Header', () => { const navLinks$ = new BehaviorSubject([ { id: 'kibana', title: 'kibana', baseUrl: '', href: '' }, ]); + const headerBanner$ = new BehaviorSubject(undefined); const customNavLink$ = new BehaviorSubject({ id: 'cloud-deployment-link', title: 'Manage cloud deployment', @@ -85,6 +86,7 @@ describe('Header', () => { isLocked$={isLocked$} customNavLink$={customNavLink$} breadcrumbsAppendExtension$={breadcrumbsAppendExtension$} + headerBanner$={headerBanner$} /> ); expect(component.find('EuiHeader').exists()).toBeFalsy(); diff --git a/src/core/public/chrome/ui/header/header.tsx b/src/core/public/chrome/ui/header/header.tsx index b55e7fc412b61..16c89fdca380a 100644 --- a/src/core/public/chrome/ui/header/header.tsx +++ b/src/core/public/chrome/ui/header/header.tsx @@ -32,7 +32,11 @@ import { } from '../..'; import { InternalApplicationStart } from '../../../application/types'; import { HttpStart } from '../../../http'; -import { ChromeBreadcrumbsAppendExtension, ChromeHelpExtension } from '../../chrome_service'; +import { + ChromeBreadcrumbsAppendExtension, + ChromeHelpExtension, + ChromeUserBanner, +} from '../../types'; import { OnIsLockedUpdate } from './'; import { CollapsibleNav } from './collapsible_nav'; import { HeaderBadge } from './header_badge'; @@ -42,10 +46,12 @@ import { HeaderLogo } from './header_logo'; import { HeaderNavControls } from './header_nav_controls'; import { HeaderActionMenu } from './header_action_menu'; import { HeaderExtension } from './header_extension'; +import { HeaderTopBanner } from './header_top_banner'; export interface HeaderProps { kibanaVersion: string; application: InternalApplicationStart; + headerBanner$: Observable; appTitle$: Observable; badge$: Observable; breadcrumbs$: Observable; @@ -84,7 +90,12 @@ export function Header({ const breadcrumbsAppendExtension = useObservable(breadcrumbsAppendExtension$); if (!isVisible) { - return ; + return ( + <> + + + + ); } const toggleCollapsibleNavRef = createRef(); @@ -97,11 +108,13 @@ export function Header({ return ( <> +
-
+
- + ; diff --git a/src/core/public/chrome/ui/header/header_breadcrumbs.tsx b/src/core/public/chrome/ui/header/header_breadcrumbs.tsx index 4db79d0233ae9..0e2bae82a3ad3 100644 --- a/src/core/public/chrome/ui/header/header_breadcrumbs.tsx +++ b/src/core/public/chrome/ui/header/header_breadcrumbs.tsx @@ -11,7 +11,7 @@ import classNames from 'classnames'; import React from 'react'; import useObservable from 'react-use/lib/useObservable'; import { Observable } from 'rxjs'; -import { ChromeBreadcrumb } from '../../chrome_service'; +import { ChromeBreadcrumb } from '../../types'; interface Props { appTitle$: Observable; diff --git a/src/core/public/chrome/ui/header/header_help_menu.tsx b/src/core/public/chrome/ui/header/header_help_menu.tsx index a20613f7e77ef..c6a09c1177a5e 100644 --- a/src/core/public/chrome/ui/header/header_help_menu.tsx +++ b/src/core/public/chrome/ui/header/header_help_menu.tsx @@ -26,7 +26,7 @@ import { import { InternalApplicationStart } from '../../../application'; import { GITHUB_CREATE_ISSUE_LINK, KIBANA_FEEDBACK_LINK } from '../../constants'; -import { ChromeHelpExtension } from '../../chrome_service'; +import { ChromeHelpExtension } from '../../types'; import { HeaderExtension } from './header_extension'; import { isModifiedOrPrevented } from './nav_link'; diff --git a/src/core/public/chrome/ui/header/header_top_banner.tsx b/src/core/public/chrome/ui/header/header_top_banner.tsx new file mode 100644 index 0000000000000..667cf9025880f --- /dev/null +++ b/src/core/public/chrome/ui/header/header_top_banner.tsx @@ -0,0 +1,34 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import React, { FC } from 'react'; +import useObservable from 'react-use/lib/useObservable'; +import { Observable } from 'rxjs'; +import { ChromeUserBanner } from '../../types'; +import { HeaderExtension } from './header_extension'; + +export interface HeaderTopBannerProps { + headerBanner$: Observable; +} + +export const HeaderTopBanner: FC = ({ headerBanner$ }) => { + const headerBanner = useObservable(headerBanner$, undefined); + if (!headerBanner) { + return null; + } + + return ( +
+ +
+ ); +}; diff --git a/src/core/public/core_app/styles/_mixins.scss b/src/core/public/core_app/styles/_mixins.scss index 6da7fab8c2f76..d088a47144f33 100644 --- a/src/core/public/core_app/styles/_mixins.scss +++ b/src/core/public/core_app/styles/_mixins.scss @@ -1,3 +1,5 @@ +@import '../../variables'; + @mixin flexParent($grow: 1, $shrink: 1, $basis: auto, $direction: column) { flex: $grow $shrink $basis; display: flex; @@ -82,6 +84,12 @@ overflow: auto; animation: kibanaFullScreenGraphics_FadeIn $euiAnimSpeedExtraSlow $euiAnimSlightResistance 0s forwards; + @at-root { + .kbnBody--hasHeaderBanner & { + top: $kbnHeaderBannerHeight; + } + } + &::before { position: absolute; top: 0; diff --git a/src/core/public/core_system.ts b/src/core/public/core_system.ts index 02e12ddf4b78b..278bbe469e862 100644 --- a/src/core/public/core_system.ts +++ b/src/core/public/core_system.ts @@ -194,7 +194,6 @@ export class CoreSystem { http, injectedMetadata, notifications, - uiSettings, }); this.coreApp.start({ application, http, notifications, uiSettings }); diff --git a/src/core/public/index.scss b/src/core/public/index.scss index 6ba9254e5d381..04e2759c91d5d 100644 --- a/src/core/public/index.scss +++ b/src/core/public/index.scss @@ -1,4 +1,5 @@ @import './variables'; +@import './mixins'; @import './core'; @import './chrome/index'; @import './overlays/index'; diff --git a/src/core/public/index.ts b/src/core/public/index.ts index 423b2c84072b8..3cb6e1beb4e6e 100644 --- a/src/core/public/index.ts +++ b/src/core/public/index.ts @@ -46,6 +46,7 @@ import { ChromeStart, ChromeRecentlyAccessed, ChromeRecentlyAccessedHistoryItem, + ChromeUserBanner, NavType, } from './chrome'; import { FatalErrorsSetup, FatalErrorsStart, FatalErrorInfo } from './fatal_errors'; @@ -300,6 +301,7 @@ export { ChromeDocTitle, ChromeRecentlyAccessed, ChromeRecentlyAccessedHistoryItem, + ChromeUserBanner, ChromeStart, DocLinksStart, FatalErrorInfo, diff --git a/src/core/public/overlays/modal/modal_service.tsx b/src/core/public/overlays/modal/modal_service.tsx index ecc80b8b6aa04..1f96e00fef0f8 100644 --- a/src/core/public/overlays/modal/modal_service.tsx +++ b/src/core/public/overlays/modal/modal_service.tsx @@ -101,6 +101,7 @@ export interface OverlayModalOpenOptions { className?: string; closeButtonAriaLabel?: string; 'data-test-subj'?: string; + maxWidth?: boolean | number | string; } interface StartDeps { diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index 99579ada8ec58..f3f7ba9680384 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -378,11 +378,18 @@ export interface ChromeStart { setBreadcrumbs(newBreadcrumbs: ChromeBreadcrumb[]): void; setBreadcrumbsAppendExtension(breadcrumbsAppendExtension?: ChromeBreadcrumbsAppendExtension): void; setCustomNavLink(newCustomNavLink?: Partial): void; + setHeaderBanner(headerBanner?: ChromeUserBanner): void; setHelpExtension(helpExtension?: ChromeHelpExtension): void; setHelpSupportUrl(url: string): void; setIsVisible(isVisible: boolean): void; } +// @public (undocumented) +export interface ChromeUserBanner { + // (undocumented) + content: MountPoint; +} + // @internal (undocumented) export interface CoreContext { // Warning: (ae-forgotten-export) The symbol "CoreId" needs to be exported by the entry point index.d.ts @@ -969,6 +976,8 @@ export interface OverlayModalOpenOptions { className?: string; // (undocumented) closeButtonAriaLabel?: string; + // (undocumented) + maxWidth?: boolean | number | string; } // @public @@ -1197,9 +1206,13 @@ export interface SavedObjectsFindOptions { page?: number; // (undocumented) perPage?: number; + // Warning: (ae-forgotten-export) The symbol "SavedObjectsPitParams" needs to be exported by the entry point index.d.ts + // Warning: (ae-unresolved-link) The @link reference could not be resolved: No member was found with name "openPointInTimeForType" + pit?: SavedObjectsPitParams; preference?: string; rootSearchFields?: string[]; search?: string; + searchAfter?: unknown[]; searchFields?: string[]; // (undocumented) sortField?: string; @@ -1519,6 +1532,7 @@ export interface UiSettingsParams { name?: string; optionLabels?: Record; options?: string[]; + order?: number; readonly?: boolean; requiresPageReload?: boolean; // (undocumented) @@ -1537,7 +1551,7 @@ export interface UiSettingsState { } // @public -export type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image'; +export type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image' | 'color'; // @public export type UnmountCallback = () => void; diff --git a/src/core/public/rendering/_base.scss b/src/core/public/rendering/_base.scss index b806ac270331d..de13785a17f5b 100644 --- a/src/core/public/rendering/_base.scss +++ b/src/core/public/rendering/_base.scss @@ -1,4 +1,4 @@ -@include euiHeaderAffordForFixed($kbnHeaderOffset); +@import '../mixins'; /** * stretch the root element of the Kibana application to set the base-size that @@ -15,11 +15,8 @@ display: flex; flex-flow: column nowrap; margin: 0 auto; - min-height: calc(100vh - #{$kbnHeaderOffset}); - &.hidden-chrome { - min-height: 100vh; - } + @include kibanaFullBodyMinHeight(); } .app-wrapper-panel { @@ -33,3 +30,28 @@ flex-shrink: 0; } } + +// adapted from euiHeaderAffordForFixed as we need to handle the top banner +@mixin kbnAffordForHeader($headerHeight) { + padding-top: $headerHeight; + + .euiFlyout, + .euiCollapsibleNav { + top: $headerHeight; + height: calc(100% - #{$headerHeight}); + } +} + +.kbnBody { + @include kbnAffordForHeader($kbnHeaderOffset); + + &.kbnBody--hasHeaderBanner { + @include kbnAffordForHeader($kbnHeaderOffsetWithBanner); + } + &.kbnBody--chromeHidden { + @include kbnAffordForHeader(0); + } + &.kbnBody--chromeHidden.kbnBody--hasHeaderBanner { + @include kbnAffordForHeader($kbnHeaderBannerHeight); + } +} diff --git a/src/core/public/rendering/rendering_service.tsx b/src/core/public/rendering/rendering_service.tsx index 47b340eed8468..843f2a253f33e 100644 --- a/src/core/public/rendering/rendering_service.tsx +++ b/src/core/public/rendering/rendering_service.tsx @@ -9,6 +9,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; +import { pairwise, startWith } from 'rxjs/operators'; import { InternalChromeStart } from '../chrome'; import { InternalApplicationStart } from '../application'; @@ -32,19 +33,27 @@ interface StartDeps { */ export class RenderingService { start({ application, chrome, overlays, targetDomElement }: StartDeps) { - const chromeUi = chrome.getHeaderComponent(); - const appUi = application.getComponent(); - const bannerUi = overlays.banners.getComponent(); + const chromeHeader = chrome.getHeaderComponent(); + const appComponent = application.getComponent(); + const bannerComponent = overlays.banners.getComponent(); + + const body = document.querySelector('body')!; + chrome + .getBodyClasses$() + .pipe(startWith([]), pairwise()) + .subscribe(([previousClasses, newClasses]) => { + body.classList.remove(...previousClasses); + body.classList.add(...newClasses); + }); ReactDOM.render(
- {chromeUi} - + {chromeHeader}
-
{bannerUi}
- {appUi} +
{bannerComponent}
+ {appComponent}
diff --git a/src/core/public/saved_objects/saved_objects_client.ts b/src/core/public/saved_objects/saved_objects_client.ts index 9c0a44b2d3da0..44466025de7e3 100644 --- a/src/core/public/saved_objects/saved_objects_client.ts +++ b/src/core/public/saved_objects/saved_objects_client.ts @@ -21,12 +21,14 @@ import { import { SimpleSavedObject } from './simple_saved_object'; import { HttpFetchOptions, HttpSetup } from '../http'; +type PromiseType> = T extends Promise ? U : never; + type SavedObjectsFindOptions = Omit< SavedObjectFindOptionsServer, - 'sortOrder' | 'rootSearchFields' | 'typeToNamespacesMap' + 'pit' | 'rootSearchFields' | 'searchAfter' | 'sortOrder' | 'typeToNamespacesMap' >; -type PromiseType> = T extends Promise ? U : never; +type SavedObjectsFindResponse = Omit>, 'pit_id'>; /** @public */ export interface SavedObjectsCreateOptions { @@ -345,10 +347,7 @@ export class SavedObjectsClient { query, }); return request.then((resp) => { - return renameKeys< - PromiseType>, - SavedObjectsFindResponsePublic - >( + return renameKeys( { saved_objects: 'savedObjects', total: 'total', diff --git a/src/core/server/core_usage_data/core_usage_data_service.mock.ts b/src/core/server/core_usage_data/core_usage_data_service.mock.ts index 1a0706917b5dd..21a599e45da01 100644 --- a/src/core/server/core_usage_data/core_usage_data_service.mock.ts +++ b/src/core/server/core_usage_data/core_usage_data_service.mock.ts @@ -105,6 +105,7 @@ const createStartContractMock = () => { loggersConfiguredCount: 0, }, savedObjects: { + customIndex: false, maxImportExportSizeBytes: 10000, maxImportPayloadBytes: 26214400, }, diff --git a/src/core/server/core_usage_data/core_usage_data_service.test.ts b/src/core/server/core_usage_data/core_usage_data_service.test.ts index 795700abf518a..ddd041b0f544e 100644 --- a/src/core/server/core_usage_data/core_usage_data_service.test.ts +++ b/src/core/server/core_usage_data/core_usage_data_service.test.ts @@ -238,6 +238,7 @@ describe('CoreUsageDataService', () => { "loggersConfiguredCount": 0, }, "savedObjects": Object { + "customIndex": false, "maxImportExportSizeBytes": 10000, "maxImportPayloadBytes": 26214400, }, diff --git a/src/core/server/core_usage_data/core_usage_data_service.ts b/src/core/server/core_usage_data/core_usage_data_service.ts index a4c6c6e8c66f4..bd5f23b1c09bc 100644 --- a/src/core/server/core_usage_data/core_usage_data_service.ts +++ b/src/core/server/core_usage_data/core_usage_data_service.ts @@ -59,6 +59,19 @@ const kibanaOrTaskManagerIndex = (index: string, kibanaConfigIndex: string) => { return index === kibanaConfigIndex ? '.kibana' : '.kibana_task_manager'; }; +/** + * This is incredibly hacky... The config service doesn't allow you to determine + * whether or not a config value has been changed from the default value, and the + * default value is defined in legacy code. + * + * This will be going away in 8.0, so please look away for a few months + * + * @param index The `kibana.index` setting from the `kibana.yml` + */ +const isCustomIndex = (index: string) => { + return index !== '.kibana'; +}; + export class CoreUsageDataService implements CoreService { private logger: Logger; private elasticsearchConfig?: ElasticsearchConfigType; @@ -220,6 +233,7 @@ export class CoreUsageDataService implements CoreService { highlight?: any; inner_hits?: any; matched_queries?: string[]; - sort?: string[]; + sort?: unknown[]; }>; }; aggregations?: any; + pit_id?: string; } /** diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 6f478004c204e..dac2d210eb395 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -260,6 +260,8 @@ export { SavedObjectsClientWrapperOptions, SavedObjectsClientFactory, SavedObjectsClientFactoryProvider, + SavedObjectsClosePointInTimeOptions, + SavedObjectsClosePointInTimeResponse, SavedObjectsCreateOptions, SavedObjectsErrorHelpers, SavedObjectsExportResultDetails, @@ -277,6 +279,8 @@ export { SavedObjectsImportUnsupportedTypeError, SavedObjectMigrationContext, SavedObjectsMigrationLogger, + SavedObjectsOpenPointInTimeOptions, + SavedObjectsOpenPointInTimeResponse, SavedObjectsRawDoc, SavedObjectsRawDocParseOptions, SavedObjectSanitizedDoc, @@ -373,6 +377,7 @@ export { SavedObjectsClientContract, SavedObjectsFindOptions, SavedObjectsFindOptionsReference, + SavedObjectsPitParams, SavedObjectsMigrationVersion, } from './types'; diff --git a/src/core/server/logging/integration_tests/rolling_file_appender.test.ts b/src/core/server/logging/integration_tests/rolling_file_appender.test.ts index 714485da1654a..fb2a714adb687 100644 --- a/src/core/server/logging/integration_tests/rolling_file_appender.test.ts +++ b/src/core/server/logging/integration_tests/rolling_file_appender.test.ts @@ -48,11 +48,10 @@ describe('RollingFileAppender', () => { }); afterEach(async () => { - try { - await rmdir(testDir); - } catch (e) { - /* trap */ + if (testDir) { + await rmdir(testDir, { recursive: true }); } + if (root) { await root.shutdown(); } diff --git a/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts b/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts index 4dc912680ec63..f3a92c896b014 100644 --- a/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts +++ b/src/core/server/plugins/discovery/plugin_manifest_parser.test.ts @@ -9,12 +9,10 @@ import { mockReadFile } from './plugin_manifest_parser.test.mocks'; import { PluginDiscoveryErrorType } from './plugin_discovery_error'; -import { loggingSystemMock } from '../../logging/logging_system.mock'; import { resolve } from 'path'; import { parseManifest } from './plugin_manifest_parser'; -const logger = loggingSystemMock.createLogger(); const pluginPath = resolve('path', 'existent-dir'); const pluginManifestPath = resolve(pluginPath, 'kibana.json'); const packageInfo = { @@ -34,7 +32,7 @@ test('return error when manifest is empty', async () => { cb(null, Buffer.from('')); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Unexpected end of JSON input (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -46,7 +44,7 @@ test('return error when manifest content is null', async () => { cb(null, Buffer.from('null')); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin manifest must contain a JSON encoded object. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -58,7 +56,7 @@ test('return error when manifest content is not a valid JSON', async () => { cb(null, Buffer.from('not-json')); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Unexpected token o in JSON at position 1 (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -70,7 +68,7 @@ test('return error when plugin id is missing', async () => { cb(null, Buffer.from(JSON.stringify({ version: 'some-version' }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin manifest must contain an "id" property. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -82,37 +80,24 @@ test('return error when plugin id includes `.` characters', async () => { cb(null, Buffer.from(JSON.stringify({ id: 'some.name', version: 'some-version' }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin "id" must not include \`.\` characters. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, }); }); -test('logs warning if pluginId is not in camelCase format', async () => { +test('return error when pluginId is not in camelCase format', async () => { + expect.assertions(1); mockReadFile.mockImplementation((path, cb) => { cb(null, Buffer.from(JSON.stringify({ id: 'some_name', version: 'kibana', server: true }))); }); - expect(loggingSystemMock.collect(logger).warn).toHaveLength(0); - await parseManifest(pluginPath, packageInfo, logger); - expect(loggingSystemMock.collect(logger).warn).toMatchInlineSnapshot(` - Array [ - Array [ - "Expect plugin \\"id\\" in camelCase, but found: some_name", - ], - ] - `); -}); - -test('does not log pluginId format warning in dist mode', async () => { - mockReadFile.mockImplementation((path, cb) => { - cb(null, Buffer.from(JSON.stringify({ id: 'some_name', version: 'kibana', server: true }))); + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ + message: `Plugin "id" must be camelCase, but found: some_name. (invalid-manifest, ${pluginManifestPath})`, + type: PluginDiscoveryErrorType.InvalidManifest, + path: pluginManifestPath, }); - - expect(loggingSystemMock.collect(logger).warn).toHaveLength(0); - await parseManifest(pluginPath, { ...packageInfo, dist: true }, logger); - expect(loggingSystemMock.collect(logger).warn.length).toBe(0); }); test('return error when plugin version is missing', async () => { @@ -120,7 +105,7 @@ test('return error when plugin version is missing', async () => { cb(null, Buffer.from(JSON.stringify({ id: 'someId' }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin manifest for "someId" must contain a "version" property. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -132,7 +117,7 @@ test('return error when plugin expected Kibana version is lower than actual vers cb(null, Buffer.from(JSON.stringify({ id: 'someId', version: '6.4.2' }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin "someId" is only compatible with Kibana version "6.4.2", but used Kibana version is "7.0.0-alpha1". (incompatible-version, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.IncompatibleVersion, path: pluginManifestPath, @@ -147,7 +132,7 @@ test('return error when plugin expected Kibana version cannot be interpreted as ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin "someId" is only compatible with Kibana version "non-sem-ver", but used Kibana version is "7.0.0-alpha1". (incompatible-version, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.IncompatibleVersion, path: pluginManifestPath, @@ -159,7 +144,7 @@ test('return error when plugin config path is not a string', async () => { cb(null, Buffer.from(JSON.stringify({ id: 'someId', version: '7.0.0', configPath: 2 }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `The "configPath" in plugin manifest for "someId" should either be a string or an array of strings. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -174,7 +159,7 @@ test('return error when plugin config path is an array that contains non-string ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `The "configPath" in plugin manifest for "someId" should either be a string or an array of strings. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -186,7 +171,7 @@ test('return error when plugin expected Kibana version is higher than actual ver cb(null, Buffer.from(JSON.stringify({ id: 'someId', version: '7.0.1' }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Plugin "someId" is only compatible with Kibana version "7.0.1", but used Kibana version is "7.0.0-alpha1". (incompatible-version, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.IncompatibleVersion, path: pluginManifestPath, @@ -198,7 +183,7 @@ test('return error when both `server` and `ui` are set to `false` or missing', a cb(null, Buffer.from(JSON.stringify({ id: 'someId', version: '7.0.0' }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Both "server" and "ui" are missing or set to "false" in plugin manifest for "someId", but at least one of these must be set to "true". (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -211,7 +196,7 @@ test('return error when both `server` and `ui` are set to `false` or missing', a ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Both "server" and "ui" are missing or set to "false" in plugin manifest for "someId", but at least one of these must be set to "true". (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -234,7 +219,7 @@ test('return error when manifest contains unrecognized properties', async () => ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).rejects.toMatchObject({ + await expect(parseManifest(pluginPath, packageInfo)).rejects.toMatchObject({ message: `Manifest for plugin "someId" contains the following unrecognized properties: unknownOne,unknownTwo. (invalid-manifest, ${pluginManifestPath})`, type: PluginDiscoveryErrorType.InvalidManifest, path: pluginManifestPath, @@ -247,20 +232,20 @@ describe('configPath', () => { cb(null, Buffer.from(JSON.stringify({ id: 'plugin', version: '7.0.0', server: true }))); }); - const manifest = await parseManifest(pluginPath, packageInfo, logger); + const manifest = await parseManifest(pluginPath, packageInfo); expect(manifest.configPath).toBe(manifest.id); }); test('falls back to plugin id in snakeCase format', async () => { mockReadFile.mockImplementation((path, cb) => { - cb(null, Buffer.from(JSON.stringify({ id: 'SomeId', version: '7.0.0', server: true }))); + cb(null, Buffer.from(JSON.stringify({ id: 'someId', version: '7.0.0', server: true }))); }); - const manifest = await parseManifest(pluginPath, packageInfo, logger); + const manifest = await parseManifest(pluginPath, packageInfo); expect(manifest.configPath).toBe('some_id'); }); - test('not formated to snakeCase if defined explicitly as string', async () => { + test('not formatted to snakeCase if defined explicitly as string', async () => { mockReadFile.mockImplementation((path, cb) => { cb( null, @@ -270,11 +255,11 @@ describe('configPath', () => { ); }); - const manifest = await parseManifest(pluginPath, packageInfo, logger); + const manifest = await parseManifest(pluginPath, packageInfo); expect(manifest.configPath).toBe('somePath'); }); - test('not formated to snakeCase if defined explicitly as an array of strings', async () => { + test('not formatted to snakeCase if defined explicitly as an array of strings', async () => { mockReadFile.mockImplementation((path, cb) => { cb( null, @@ -284,7 +269,7 @@ describe('configPath', () => { ); }); - const manifest = await parseManifest(pluginPath, packageInfo, logger); + const manifest = await parseManifest(pluginPath, packageInfo); expect(manifest.configPath).toEqual(['somePath']); }); }); @@ -294,7 +279,7 @@ test('set defaults for all missing optional fields', async () => { cb(null, Buffer.from(JSON.stringify({ id: 'someId', version: '7.0.0', server: true }))); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).resolves.toEqual({ + await expect(parseManifest(pluginPath, packageInfo)).resolves.toEqual({ id: 'someId', configPath: 'some_id', version: '7.0.0', @@ -325,7 +310,7 @@ test('return all set optional fields as they are in manifest', async () => { ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).resolves.toEqual({ + await expect(parseManifest(pluginPath, packageInfo)).resolves.toEqual({ id: 'someId', configPath: ['some', 'path'], version: 'some-version', @@ -355,7 +340,7 @@ test('return manifest when plugin expected Kibana version matches actual version ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).resolves.toEqual({ + await expect(parseManifest(pluginPath, packageInfo)).resolves.toEqual({ id: 'someId', configPath: 'some-path', version: 'some-version', @@ -385,7 +370,7 @@ test('return manifest when plugin expected Kibana version is `kibana`', async () ); }); - await expect(parseManifest(pluginPath, packageInfo, logger)).resolves.toEqual({ + await expect(parseManifest(pluginPath, packageInfo)).resolves.toEqual({ id: 'someId', configPath: 'some_id', version: 'some-version', diff --git a/src/core/server/plugins/discovery/plugin_manifest_parser.ts b/src/core/server/plugins/discovery/plugin_manifest_parser.ts index 9db68bcaa4cce..eae0e73e86c46 100644 --- a/src/core/server/plugins/discovery/plugin_manifest_parser.ts +++ b/src/core/server/plugins/discovery/plugin_manifest_parser.ts @@ -12,7 +12,6 @@ import { coerce } from 'semver'; import { promisify } from 'util'; import { snakeCase } from 'lodash'; import { isConfigPath, PackageInfo } from '../../config'; -import { Logger } from '../../logging'; import { PluginManifest } from '../types'; import { PluginDiscoveryError } from './plugin_discovery_error'; import { isCamelCase } from './is_camel_case'; @@ -63,8 +62,7 @@ const KNOWN_MANIFEST_FIELDS = (() => { */ export async function parseManifest( pluginPath: string, - packageInfo: PackageInfo, - log: Logger + packageInfo: PackageInfo ): Promise { const manifestPath = resolve(pluginPath, MANIFEST_FILE_NAME); @@ -105,8 +103,11 @@ export async function parseManifest( ); } - if (!packageInfo.dist && !isCamelCase(manifest.id)) { - log.warn(`Expect plugin "id" in camelCase, but found: ${manifest.id}`); + if (!isCamelCase(manifest.id)) { + throw PluginDiscoveryError.invalidManifest( + manifestPath, + new Error(`Plugin "id" must be camelCase, but found: ${manifest.id}.`) + ); } if (!manifest.version || typeof manifest.version !== 'string') { diff --git a/src/core/server/plugins/discovery/plugins_discovery.ts b/src/core/server/plugins/discovery/plugins_discovery.ts index 61eccff982593..368795968a7cb 100644 --- a/src/core/server/plugins/discovery/plugins_discovery.ts +++ b/src/core/server/plugins/discovery/plugins_discovery.ts @@ -179,7 +179,7 @@ function createPlugin$( coreContext: CoreContext, instanceInfo: InstanceInfo ) { - return from(parseManifest(path, coreContext.env.packageInfo, log)).pipe( + return from(parseManifest(path, coreContext.env.packageInfo)).pipe( map((manifest) => { log.debug(`Successfully discovered plugin "${manifest.id}" at "${path}"`); const opaqueId = Symbol(manifest.id); diff --git a/src/core/server/saved_objects/export/point_in_time_finder.test.ts b/src/core/server/saved_objects/export/point_in_time_finder.test.ts new file mode 100644 index 0000000000000..cd79c7a4b81e5 --- /dev/null +++ b/src/core/server/saved_objects/export/point_in_time_finder.test.ts @@ -0,0 +1,321 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { savedObjectsClientMock } from '../service/saved_objects_client.mock'; +import { loggerMock, MockedLogger } from '../../logging/logger.mock'; +import { SavedObjectsFindOptions } from '../types'; +import { SavedObjectsFindResult } from '../service'; + +import { createPointInTimeFinder } from './point_in_time_finder'; + +const mockHits = [ + { + id: '2', + type: 'search', + attributes: {}, + score: 1, + references: [ + { + name: 'name', + type: 'visualization', + id: '1', + }, + ], + sort: [], + }, + { + id: '1', + type: 'visualization', + attributes: {}, + score: 1, + references: [], + sort: [], + }, +]; + +describe('createPointInTimeFinder()', () => { + let logger: MockedLogger; + let savedObjectsClient: ReturnType; + + beforeEach(() => { + logger = loggerMock.create(); + savedObjectsClient = savedObjectsClientMock.create(); + }); + + describe('#find', () => { + test('throws if a PIT is already open', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'abc123', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: mockHits, + pit_id: 'abc123', + per_page: 1, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: mockHits, + pit_id: 'abc123', + per_page: 1, + page: 1, + }); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + perPage: 1, + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + await finder.find().next(); + + expect(savedObjectsClient.find).toHaveBeenCalledTimes(1); + savedObjectsClient.find.mockClear(); + + expect(async () => { + await finder.find().next(); + }).rejects.toThrowErrorMatchingInlineSnapshot( + `"Point In Time has already been opened for this finder instance. Please call \`close()\` before calling \`find()\` again."` + ); + expect(savedObjectsClient.find).toHaveBeenCalledTimes(0); + }); + + test('works with a single page of results', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'abc123', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: mockHits, + pit_id: 'abc123', + per_page: 2, + page: 0, + }); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + const hits: SavedObjectsFindResult[] = []; + for await (const result of finder.find()) { + hits.push(...result.saved_objects); + } + + expect(hits.length).toBe(2); + expect(savedObjectsClient.openPointInTimeForType).toHaveBeenCalledTimes(1); + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(1); + expect(savedObjectsClient.find).toHaveBeenCalledTimes(1); + expect(savedObjectsClient.find).toHaveBeenCalledWith( + expect.objectContaining({ + pit: expect.objectContaining({ id: 'abc123', keepAlive: '2m' }), + sortField: 'updated_at', + sortOrder: 'desc', + type: ['visualization'], + }) + ); + }); + + test('works with multiple pages of results', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'abc123', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [mockHits[0]], + pit_id: 'abc123', + per_page: 1, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [mockHits[1]], + pit_id: 'abc123', + per_page: 1, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [], + per_page: 1, + pit_id: 'abc123', + page: 0, + }); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + perPage: 1, + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + const hits: SavedObjectsFindResult[] = []; + for await (const result of finder.find()) { + hits.push(...result.saved_objects); + } + + expect(hits.length).toBe(2); + expect(savedObjectsClient.openPointInTimeForType).toHaveBeenCalledTimes(1); + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(1); + // called 3 times since we need a 3rd request to check if we + // are done paginating through results. + expect(savedObjectsClient.find).toHaveBeenCalledTimes(3); + expect(savedObjectsClient.find).toHaveBeenCalledWith( + expect.objectContaining({ + pit: expect.objectContaining({ id: 'abc123', keepAlive: '2m' }), + sortField: 'updated_at', + sortOrder: 'desc', + type: ['visualization'], + }) + ); + }); + }); + + describe('#close', () => { + test('calls closePointInTime with correct ID', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'test', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1, + saved_objects: [mockHits[0]], + pit_id: 'test', + per_page: 1, + page: 0, + }); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + perPage: 2, + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + const hits: SavedObjectsFindResult[] = []; + for await (const result of finder.find()) { + hits.push(...result.saved_objects); + await finder.close(); + } + + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledWith('test'); + }); + + test('causes generator to stop', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'test', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [mockHits[0]], + pit_id: 'test', + per_page: 1, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [mockHits[1]], + pit_id: 'test', + per_page: 1, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: [], + per_page: 1, + pit_id: 'test', + page: 0, + }); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + perPage: 1, + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + const hits: SavedObjectsFindResult[] = []; + for await (const result of finder.find()) { + hits.push(...result.saved_objects); + await finder.close(); + } + + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(1); + expect(hits.length).toBe(1); + }); + + test('is called if `find` throws an error', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'test', + }); + savedObjectsClient.find.mockRejectedValueOnce(new Error('oops')); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + perPage: 2, + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + const hits: SavedObjectsFindResult[] = []; + try { + for await (const result of finder.find()) { + hits.push(...result.saved_objects); + } + } catch (e) { + // intentionally empty + } + + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledWith('test'); + }); + + test('finder can be reused after closing', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'abc123', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: mockHits, + pit_id: 'abc123', + per_page: 1, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 2, + saved_objects: mockHits, + pit_id: 'abc123', + per_page: 1, + page: 1, + }); + + const findOptions: SavedObjectsFindOptions = { + type: ['visualization'], + search: 'foo*', + perPage: 1, + }; + + const finder = createPointInTimeFinder({ findOptions, logger, savedObjectsClient }); + + const findA = finder.find(); + await findA.next(); + await finder.close(); + + const findB = finder.find(); + await findB.next(); + await finder.close(); + + expect((await findA.next()).done).toBe(true); + expect((await findB.next()).done).toBe(true); + expect(savedObjectsClient.openPointInTimeForType).toHaveBeenCalledTimes(2); + expect(savedObjectsClient.find).toHaveBeenCalledTimes(2); + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(2); + }); + }); +}); diff --git a/src/core/server/saved_objects/export/point_in_time_finder.ts b/src/core/server/saved_objects/export/point_in_time_finder.ts new file mode 100644 index 0000000000000..dc0bac6b6bfd9 --- /dev/null +++ b/src/core/server/saved_objects/export/point_in_time_finder.ts @@ -0,0 +1,192 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { Logger } from '../../logging'; +import { SavedObjectsClientContract, SavedObjectsFindOptions } from '../types'; +import { SavedObjectsFindResponse } from '../service'; + +/** + * Returns a generator to help page through large sets of saved objects. + * + * The generator wraps calls to `SavedObjects.find` and iterates over + * multiple pages of results using `_pit` and `search_after`. This will + * open a new Point In Time (PIT), and continue paging until a set of + * results is received that's smaller than the designated `perPage`. + * + * Once you have retrieved all of the results you need, it is recommended + * to call `close()` to clean up the PIT and prevent Elasticsearch from + * consuming resources unnecessarily. This will automatically be done for + * you if you reach the last page of results. + * + * @example + * ```ts + * const findOptions: SavedObjectsFindOptions = { + * type: 'visualization', + * search: 'foo*', + * perPage: 100, + * }; + * + * const finder = createPointInTimeFinder({ + * logger, + * savedObjectsClient, + * findOptions, + * }); + * + * const responses: SavedObjectFindResponse[] = []; + * for await (const response of finder.find()) { + * responses.push(...response); + * if (doneSearching) { + * await finder.close(); + * } + * } + * ``` + */ +export function createPointInTimeFinder({ + findOptions, + logger, + savedObjectsClient, +}: { + findOptions: SavedObjectsFindOptions; + logger: Logger; + savedObjectsClient: SavedObjectsClientContract; +}) { + return new PointInTimeFinder({ findOptions, logger, savedObjectsClient }); +} + +/** + * @internal + */ +export class PointInTimeFinder { + readonly #log: Logger; + readonly #savedObjectsClient: SavedObjectsClientContract; + readonly #findOptions: SavedObjectsFindOptions; + #open: boolean = false; + #pitId?: string; + + constructor({ + findOptions, + logger, + savedObjectsClient, + }: { + findOptions: SavedObjectsFindOptions; + logger: Logger; + savedObjectsClient: SavedObjectsClientContract; + }) { + this.#log = logger; + this.#savedObjectsClient = savedObjectsClient; + this.#findOptions = { + // Default to 1000 items per page as a tradeoff between + // speed and memory consumption. + perPage: 1000, + ...findOptions, + }; + } + + async *find() { + if (this.#open) { + throw new Error( + 'Point In Time has already been opened for this finder instance. ' + + 'Please call `close()` before calling `find()` again.' + ); + } + + // Open PIT and request our first page of hits + await this.open(); + + let lastResultsCount: number; + let lastHitSortValue: unknown[] | undefined; + do { + const results = await this.findNext({ + findOptions: this.#findOptions, + id: this.#pitId, + ...(lastHitSortValue ? { searchAfter: lastHitSortValue } : {}), + }); + this.#pitId = results.pit_id; + lastResultsCount = results.saved_objects.length; + lastHitSortValue = this.getLastHitSortValue(results); + + this.#log.debug(`Collected [${lastResultsCount}] saved objects for export.`); + + // Close PIT if this was our last page + if (this.#pitId && lastResultsCount < this.#findOptions.perPage!) { + await this.close(); + } + + yield results; + // We've reached the end when there are fewer hits than our perPage size, + // or when `close()` has been called. + } while (this.#open && lastResultsCount >= this.#findOptions.perPage!); + + return; + } + + async close() { + try { + if (this.#pitId) { + this.#log.debug(`Closing PIT for types [${this.#findOptions.type}]`); + await this.#savedObjectsClient.closePointInTime(this.#pitId); + this.#pitId = undefined; + } + this.#open = false; + } catch (e) { + this.#log.error(`Failed to close PIT for types [${this.#findOptions.type}]`); + throw e; + } + } + + private async open() { + try { + const { id } = await this.#savedObjectsClient.openPointInTimeForType(this.#findOptions.type); + this.#pitId = id; + this.#open = true; + } catch (e) { + // Since `find` swallows 404s, it is expected that exporter will do the same, + // so we only rethrow non-404 errors here. + if (e.output.statusCode !== 404) { + throw e; + } + this.#log.debug(`Unable to open PIT for types [${this.#findOptions.type}]: 404 ${e}`); + } + } + + private async findNext({ + findOptions, + id, + searchAfter, + }: { + findOptions: SavedObjectsFindOptions; + id?: string; + searchAfter?: unknown[]; + }) { + try { + return await this.#savedObjectsClient.find({ + // Sort fields are required to use searchAfter, so we set some defaults here + sortField: 'updated_at', + sortOrder: 'desc', + // Bump keep_alive by 2m on every new request to allow for the ES client + // to make multiple retries in the event of a network failure. + ...(id ? { pit: { id, keepAlive: '2m' } } : {}), + ...(searchAfter ? { searchAfter } : {}), + ...findOptions, + }); + } catch (e) { + if (id) { + // Clean up PIT on any errors. + await this.close(); + } + throw e; + } + } + + private getLastHitSortValue(res: SavedObjectsFindResponse): unknown[] | undefined { + if (res.saved_objects.length < 1) { + return undefined; + } + return res.saved_objects[res.saved_objects.length - 1].sort; + } +} diff --git a/src/core/server/saved_objects/export/saved_objects_exporter.test.ts b/src/core/server/saved_objects/export/saved_objects_exporter.test.ts index c16623f785b08..cf60ada5ba90a 100644 --- a/src/core/server/saved_objects/export/saved_objects_exporter.test.ts +++ b/src/core/server/saved_objects/export/saved_objects_exporter.test.ts @@ -11,6 +11,7 @@ import { SavedObjectsExporter } from './saved_objects_exporter'; import { savedObjectsClientMock } from '../service/saved_objects_client.mock'; import { SavedObjectTypeRegistry } from '../saved_objects_type_registry'; import { httpServerMock } from '../../http/http_server.mocks'; +import { loggerMock, MockedLogger } from '../../logging/logger.mock'; import { Readable } from 'stream'; import { createPromiseFromStreams, createConcatStream } from '@kbn/utils'; @@ -18,18 +19,25 @@ async function readStreamToCompletion(stream: Readable): Promise { + let logger: MockedLogger; let savedObjectsClient: ReturnType; let typeRegistry: SavedObjectTypeRegistry; let exporter: SavedObjectsExporter; beforeEach(() => { + logger = loggerMock.create(); typeRegistry = new SavedObjectTypeRegistry(); savedObjectsClient = savedObjectsClientMock.create(); - exporter = new SavedObjectsExporter({ savedObjectsClient, exportSizeLimit, typeRegistry }); + exporter = new SavedObjectsExporter({ + exportSizeLimit, + logger, + savedObjectsClient, + typeRegistry, + }); }); describe('#exportByTypes', () => { @@ -58,7 +66,7 @@ describe('getSortedObjectsForExport()', () => { references: [], }, ], - per_page: 1, + per_page: 1000, page: 0, }); const exportStream = await exporter.exportByTypes({ @@ -96,30 +104,232 @@ describe('getSortedObjectsForExport()', () => { ] `); expect(savedObjectsClient.find).toMatchInlineSnapshot(` - [MockFunction] { - "calls": Array [ - Array [ - Object { - "hasReference": undefined, - "hasReferenceOperator": undefined, - "namespaces": undefined, - "perPage": 500, - "search": undefined, - "type": Array [ - "index-pattern", - "search", - ], - }, - ], - ], - "results": Array [ - Object { - "type": "return", - "value": Promise {}, - }, + [MockFunction] { + "calls": Array [ + Array [ + Object { + "hasReference": undefined, + "hasReferenceOperator": undefined, + "namespaces": undefined, + "perPage": 1000, + "pit": Object { + "id": "some_pit_id", + "keepAlive": "2m", + }, + "search": undefined, + "sortField": "updated_at", + "sortOrder": "desc", + "type": Array [ + "index-pattern", + "search", ], - } + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": Promise {}, + }, + ], + } + `); + }); + + describe('pages through results with PIT', () => { + function generateHits( + hitCount: number, + { + attributes = {}, + sort = [], + type = 'index-pattern', + }: { + attributes?: Record; + sort?: unknown[]; + type?: string; + } = {} + ) { + const hits = []; + for (let i = 1; i <= hitCount; i++) { + hits.push({ + id: `${i}`, + type, + attributes, + sort, + score: 1, + references: [], + }); + } + return hits; + } + + describe('<1k hits', () => { + const mockHits = generateHits(20); + + test('requests a single page', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + total: 20, + saved_objects: mockHits, + per_page: 1000, + page: 0, + }); + + const exportStream = await exporter.exportByTypes({ + request, + types: ['index-pattern'], + }); + + const response = await readStreamToCompletion(exportStream); + + expect(savedObjectsClient.find).toHaveBeenCalledTimes(1); + expect(response[response.length - 1]).toMatchInlineSnapshot(` + Object { + "exportedCount": 20, + "missingRefCount": 0, + "missingReferences": Array [], + } + `); + }); + + test('opens and closes PIT', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + total: 20, + saved_objects: mockHits, + per_page: 1000, + page: 0, + pit_id: 'abc123', + }); + + const exportStream = await exporter.exportByTypes({ + request, + types: ['index-pattern'], + }); + + await readStreamToCompletion(exportStream); + + expect(savedObjectsClient.openPointInTimeForType).toHaveBeenCalledTimes(1); + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(1); + }); + + test('passes correct PIT ID to `find`', async () => { + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'abc123', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 20, + saved_objects: mockHits, + per_page: 1000, + page: 0, + }); + + const exportStream = await exporter.exportByTypes({ + request, + types: ['index-pattern'], + }); + + await readStreamToCompletion(exportStream); + + expect(savedObjectsClient.find).toHaveBeenCalledWith( + expect.objectContaining({ + pit: expect.objectContaining({ id: 'abc123', keepAlive: '2m' }), + sortField: 'updated_at', + sortOrder: 'desc', + type: ['index-pattern'], + }) + ); + }); + }); + + describe('>1k hits', () => { + const firstMockHits = generateHits(1000, { sort: ['a', 'b'] }); + const secondMockHits = generateHits(500); + + test('requests multiple pages', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1500, + saved_objects: firstMockHits, + per_page: 1000, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1500, + saved_objects: secondMockHits, + per_page: 500, + page: 1, + }); + + const exportStream = await exporter.exportByTypes({ + request, + types: ['index-pattern'], + }); + + const response = await readStreamToCompletion(exportStream); + + expect(savedObjectsClient.find).toHaveBeenCalledTimes(2); + expect(response[response.length - 1]).toMatchInlineSnapshot(` + Object { + "exportedCount": 1500, + "missingRefCount": 0, + "missingReferences": Array [], + } `); + }); + + test('opens and closes PIT', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1500, + saved_objects: firstMockHits, + per_page: 1000, + page: 0, + pit_id: 'abc123', + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1500, + saved_objects: secondMockHits, + per_page: 500, + page: 1, + pit_id: 'abc123', + }); + + const exportStream = await exporter.exportByTypes({ + request, + types: ['index-pattern'], + }); + + await readStreamToCompletion(exportStream); + + expect(savedObjectsClient.openPointInTimeForType).toHaveBeenCalledTimes(1); + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(1); + }); + + test('passes sort values to searchAfter', async () => { + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1500, + saved_objects: firstMockHits, + per_page: 1000, + page: 0, + }); + savedObjectsClient.find.mockResolvedValueOnce({ + total: 1500, + saved_objects: secondMockHits, + per_page: 500, + page: 1, + }); + + const exportStream = await exporter.exportByTypes({ + request, + types: ['index-pattern'], + }); + + await readStreamToCompletion(exportStream); + + expect(savedObjectsClient.find.mock.calls[1][0]).toEqual( + expect.objectContaining({ + searchAfter: ['a', 'b'], + }) + ); + }); + }); }); test('applies the export transforms', async () => { @@ -138,7 +348,12 @@ describe('getSortedObjectsForExport()', () => { }, }, }); - exporter = new SavedObjectsExporter({ savedObjectsClient, exportSizeLimit, typeRegistry }); + exporter = new SavedObjectsExporter({ + exportSizeLimit, + logger, + savedObjectsClient, + typeRegistry, + }); savedObjectsClient.find.mockResolvedValueOnce({ total: 1, @@ -233,30 +448,36 @@ describe('getSortedObjectsForExport()', () => { ] `); expect(savedObjectsClient.find).toMatchInlineSnapshot(` - [MockFunction] { - "calls": Array [ - Array [ - Object { - "hasReference": undefined, - "hasReferenceOperator": undefined, - "namespaces": undefined, - "perPage": 500, - "search": undefined, - "type": Array [ - "index-pattern", - "search", - ], - }, - ], - ], - "results": Array [ - Object { - "type": "return", - "value": Promise {}, - }, + [MockFunction] { + "calls": Array [ + Array [ + Object { + "hasReference": undefined, + "hasReferenceOperator": undefined, + "namespaces": undefined, + "perPage": 1000, + "pit": Object { + "id": "some_pit_id", + "keepAlive": "2m", + }, + "search": undefined, + "sortField": "updated_at", + "sortOrder": "desc", + "type": Array [ + "index-pattern", + "search", ], - } - `); + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": Promise {}, + }, + ], + } + `); }); test('exclude export details if option is specified', async () => { @@ -383,30 +604,36 @@ describe('getSortedObjectsForExport()', () => { ] `); expect(savedObjectsClient.find).toMatchInlineSnapshot(` - [MockFunction] { - "calls": Array [ - Array [ - Object { - "hasReference": undefined, - "hasReferenceOperator": undefined, - "namespaces": undefined, - "perPage": 500, - "search": "foo", - "type": Array [ - "index-pattern", - "search", - ], - }, - ], - ], - "results": Array [ - Object { - "type": "return", - "value": Promise {}, - }, + [MockFunction] { + "calls": Array [ + Array [ + Object { + "hasReference": undefined, + "hasReferenceOperator": undefined, + "namespaces": undefined, + "perPage": 1000, + "pit": Object { + "id": "some_pit_id", + "keepAlive": "2m", + }, + "search": "foo", + "sortField": "updated_at", + "sortOrder": "desc", + "type": Array [ + "index-pattern", + "search", ], - } - `); + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": Promise {}, + }, + ], + } + `); }); test('exports selected types with references when present', async () => { @@ -465,35 +692,41 @@ describe('getSortedObjectsForExport()', () => { ] `); expect(savedObjectsClient.find).toMatchInlineSnapshot(` - [MockFunction] { - "calls": Array [ - Array [ - Object { - "hasReference": Array [ - Object { - "id": "1", - "type": "index-pattern", - }, - ], - "hasReferenceOperator": "OR", - "namespaces": undefined, - "perPage": 500, - "search": undefined, - "type": Array [ - "index-pattern", - "search", - ], - }, - ], - ], - "results": Array [ + [MockFunction] { + "calls": Array [ + Array [ + Object { + "hasReference": Array [ Object { - "type": "return", - "value": Promise {}, + "id": "1", + "type": "index-pattern", }, ], - } - `); + "hasReferenceOperator": "OR", + "namespaces": undefined, + "perPage": 1000, + "pit": Object { + "id": "some_pit_id", + "keepAlive": "2m", + }, + "search": undefined, + "sortField": "updated_at", + "sortOrder": "desc", + "type": Array [ + "index-pattern", + "search", + ], + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": Promise {}, + }, + ], + } + `); }); test('exports from the provided namespace when present', async () => { @@ -521,7 +754,7 @@ describe('getSortedObjectsForExport()', () => { references: [], }, ], - per_page: 1, + per_page: 1000, page: 0, }); const exportStream = await exporter.exportByTypes({ @@ -560,36 +793,56 @@ describe('getSortedObjectsForExport()', () => { ] `); expect(savedObjectsClient.find).toMatchInlineSnapshot(` - [MockFunction] { - "calls": Array [ - Array [ - Object { - "hasReference": undefined, - "hasReferenceOperator": undefined, - "namespaces": Array [ - "foo", - ], - "perPage": 500, - "search": undefined, - "type": Array [ - "index-pattern", - "search", - ], - }, - ], + [MockFunction] { + "calls": Array [ + Array [ + Object { + "hasReference": undefined, + "hasReferenceOperator": undefined, + "namespaces": Array [ + "foo", ], - "results": Array [ - Object { - "type": "return", - "value": Promise {}, - }, + "perPage": 1000, + "pit": Object { + "id": "some_pit_id", + "keepAlive": "2m", + }, + "search": undefined, + "sortField": "updated_at", + "sortOrder": "desc", + "type": Array [ + "index-pattern", + "search", ], - } - `); + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": Promise {}, + }, + ], + } + `); }); test('export selected types throws error when exceeding exportSizeLimit', async () => { - exporter = new SavedObjectsExporter({ savedObjectsClient, exportSizeLimit: 1, typeRegistry }); + exporter = new SavedObjectsExporter({ + exportSizeLimit: 1, + logger, + savedObjectsClient, + typeRegistry, + }); + + savedObjectsClient.openPointInTimeForType.mockResolvedValueOnce({ + id: 'abc123', + }); + + savedObjectsClient.closePointInTime.mockResolvedValueOnce({ + succeeded: true, + num_freed: 1, + }); savedObjectsClient.find.mockResolvedValueOnce({ total: 2, @@ -617,6 +870,7 @@ describe('getSortedObjectsForExport()', () => { ], per_page: 1, page: 0, + pit_id: 'abc123', }); await expect( exporter.exportByTypes({ @@ -624,12 +878,13 @@ describe('getSortedObjectsForExport()', () => { types: ['index-pattern', 'search'], }) ).rejects.toThrowErrorMatchingInlineSnapshot(`"Can't export more than 1 objects"`); + expect(savedObjectsClient.closePointInTime).toHaveBeenCalledTimes(1); }); test('sorts objects within type', async () => { savedObjectsClient.find.mockResolvedValueOnce({ total: 3, - per_page: 10000, + per_page: 1000, page: 1, saved_objects: [ { @@ -836,7 +1091,12 @@ describe('getSortedObjectsForExport()', () => { }); test('export selected objects throws error when exceeding exportSizeLimit', async () => { - exporter = new SavedObjectsExporter({ savedObjectsClient, exportSizeLimit: 1, typeRegistry }); + exporter = new SavedObjectsExporter({ + exportSizeLimit: 1, + logger, + savedObjectsClient, + typeRegistry, + }); const exportOpts = { request, diff --git a/src/core/server/saved_objects/export/saved_objects_exporter.ts b/src/core/server/saved_objects/export/saved_objects_exporter.ts index 295a3d7a119d4..c1c0ea73f0bd3 100644 --- a/src/core/server/saved_objects/export/saved_objects_exporter.ts +++ b/src/core/server/saved_objects/export/saved_objects_exporter.ts @@ -8,7 +8,9 @@ import { createListStream } from '@kbn/utils'; import { PublicMethodsOf } from '@kbn/utility-types'; -import { SavedObject, SavedObjectsClientContract } from '../types'; +import { Logger } from '../../logging'; +import { SavedObject, SavedObjectsClientContract, SavedObjectsFindOptions } from '../types'; +import { SavedObjectsFindResult } from '../service'; import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry'; import { fetchNestedDependencies } from './fetch_nested_dependencies'; import { sortObjects } from './sort_objects'; @@ -21,6 +23,7 @@ import { } from './types'; import { SavedObjectsExportError } from './errors'; import { applyExportTransforms } from './apply_export_transforms'; +import { createPointInTimeFinder } from './point_in_time_finder'; import { byIdAscComparator, getPreservedOrderComparator, SavedObjectComparator } from './utils'; /** @@ -35,16 +38,20 @@ export class SavedObjectsExporter { readonly #savedObjectsClient: SavedObjectsClientContract; readonly #exportTransforms: Record; readonly #exportSizeLimit: number; + readonly #log: Logger; constructor({ savedObjectsClient, typeRegistry, exportSizeLimit, + logger, }: { savedObjectsClient: SavedObjectsClientContract; typeRegistry: ISavedObjectTypeRegistry; exportSizeLimit: number; + logger: Logger; }) { + this.#log = logger; this.#savedObjectsClient = savedObjectsClient; this.#exportSizeLimit = exportSizeLimit; this.#exportTransforms = typeRegistry.getAllTypes().reduce((transforms, type) => { @@ -66,6 +73,7 @@ export class SavedObjectsExporter { * @throws SavedObjectsExportError */ public async exportByTypes(options: SavedObjectsExportByTypeOptions) { + this.#log.debug(`Initiating export for types: [${options.types}]`); const objects = await this.fetchByTypes(options); return this.processObjects(objects, byIdAscComparator, { request: options.request, @@ -83,6 +91,7 @@ export class SavedObjectsExporter { * @throws SavedObjectsExportError */ public async exportByObjects(options: SavedObjectsExportByObjectOptions) { + this.#log.debug(`Initiating export of [${options.objects.length}] objects.`); if (options.objects.length > this.#exportSizeLimit) { throw SavedObjectsExportError.exportSizeExceeded(this.#exportSizeLimit); } @@ -106,6 +115,7 @@ export class SavedObjectsExporter { namespace, }: SavedObjectExportBaseOptions ) { + this.#log.debug(`Processing [${savedObjects.length}] saved objects.`); let exportedObjects: Array>; let missingReferences: SavedObjectsExportResultDetails['missingReferences'] = []; @@ -117,6 +127,7 @@ export class SavedObjectsExporter { }); if (includeReferencesDeep) { + this.#log.debug(`Fetching saved objects references.`); const fetchResult = await fetchNestedDependencies( savedObjects, this.#savedObjectsClient, @@ -138,6 +149,7 @@ export class SavedObjectsExporter { missingRefCount: missingReferences.length, missingReferences, }; + this.#log.debug(`Exporting [${redactedObjects.length}] saved objects.`); return createListStream([...redactedObjects, ...(excludeExportDetails ? [] : [exportDetails])]); } @@ -156,21 +168,32 @@ export class SavedObjectsExporter { hasReference, search, }: SavedObjectsExportByTypeOptions) { - const findResponse = await this.#savedObjectsClient.find({ + const findOptions: SavedObjectsFindOptions = { type: types, hasReference, hasReferenceOperator: hasReference ? 'OR' : undefined, search, - perPage: this.#exportSizeLimit, namespaces: namespace ? [namespace] : undefined, + }; + + const finder = createPointInTimeFinder({ + findOptions, + logger: this.#log, + savedObjectsClient: this.#savedObjectsClient, }); - if (findResponse.total > this.#exportSizeLimit) { - throw SavedObjectsExportError.exportSizeExceeded(this.#exportSizeLimit); + + const hits: SavedObjectsFindResult[] = []; + for await (const result of finder.find()) { + hits.push(...result.saved_objects); + if (hits.length > this.#exportSizeLimit) { + await finder.close(); + throw SavedObjectsExportError.exportSizeExceeded(this.#exportSizeLimit); + } } // sorts server-side by _id, since it's only available in fielddata return ( - findResponse.saved_objects + hits // exclude the find-specific `score` property from the exported objects .map(({ score, ...obj }) => obj) .sort(byIdAscComparator) diff --git a/src/core/server/saved_objects/saved_objects_service.ts b/src/core/server/saved_objects/saved_objects_service.ts index 4ad0a34acc2ef..fce7f12384456 100644 --- a/src/core/server/saved_objects/saved_objects_service.ts +++ b/src/core/server/saved_objects/saved_objects_service.ts @@ -459,6 +459,7 @@ export class SavedObjectsService savedObjectsClient, typeRegistry: this.typeRegistry, exportSizeLimit: this.config!.maxImportExportSize, + logger: this.logger.get('exporter'), }), createImporter: (savedObjectsClient) => new SavedObjectsImporter({ diff --git a/src/core/server/saved_objects/service/lib/filter_utils.test.ts b/src/core/server/saved_objects/service/lib/filter_utils.test.ts index f6f8f88e84304..05a936db4bfee 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.test.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.test.ts @@ -9,7 +9,12 @@ // @ts-expect-error no ts import { esKuery } from '../../es_query'; -import { validateFilterKueryNode, validateConvertFilterToKueryNode } from './filter_utils'; +import { + validateFilterKueryNode, + validateConvertFilterToKueryNode, + fieldDefined, + hasFilterKeyError, +} from './filter_utils'; const mockMappings = { properties: { @@ -39,6 +44,18 @@ const mockMappings = { }, }, }, + bean: { + properties: { + canned: { + fields: { + text: { + type: 'text', + }, + }, + type: 'keyword', + }, + }, + }, alert: { properties: { actions: { @@ -90,6 +107,15 @@ describe('Filter Utils', () => { validateConvertFilterToKueryNode(['foo'], 'foo.attributes.title: "best"', mockMappings) ).toEqual(esKuery.fromKueryExpression('foo.title: "best"')); }); + test('Validate a multi-field KQL expression filter', () => { + expect( + validateConvertFilterToKueryNode( + ['bean'], + 'bean.attributes.canned.text: "best"', + mockMappings + ) + ).toEqual(esKuery.fromKueryExpression('bean.canned.text: "best"')); + }); test('Assemble filter kuery node saved object attributes with one saved object type', () => { expect( validateConvertFilterToKueryNode( @@ -485,4 +511,100 @@ describe('Filter Utils', () => { ]); }); }); + + describe('#hasFilterKeyError', () => { + test('Return no error if filter key is valid', () => { + const hasError = hasFilterKeyError('bean.attributes.canned.text', ['bean'], mockMappings); + + expect(hasError).toBeNull(); + }); + + test('Return error if key is not defined', () => { + const hasError = hasFilterKeyError(undefined, ['bean'], mockMappings); + + expect(hasError).toEqual( + 'The key is empty and needs to be wrapped by a saved object type like bean' + ); + }); + + test('Return error if key is null', () => { + const hasError = hasFilterKeyError(null, ['bean'], mockMappings); + + expect(hasError).toEqual( + 'The key is empty and needs to be wrapped by a saved object type like bean' + ); + }); + + test('Return error if key does not identify an SO wrapper', () => { + const hasError = hasFilterKeyError('beanattributescannedtext', ['bean'], mockMappings); + + expect(hasError).toEqual( + "This key 'beanattributescannedtext' need to be wrapped by a saved object type like bean" + ); + }); + + test('Return error if key does not match an SO type', () => { + const hasError = hasFilterKeyError('canned.attributes.bean.text', ['bean'], mockMappings); + + expect(hasError).toEqual('This type canned is not allowed'); + }); + + test('Return error if key does not match SO attribute structure', () => { + const hasError = hasFilterKeyError('bean.canned.text', ['bean'], mockMappings); + + expect(hasError).toEqual( + "This key 'bean.canned.text' does NOT match the filter proposition SavedObjectType.attributes.key" + ); + }); + + test('Return error if key matches SO attribute parent, not attribute itself', () => { + const hasError = hasFilterKeyError('alert.actions', ['alert'], mockMappings); + + expect(hasError).toEqual( + "This key 'alert.actions' does NOT match the filter proposition SavedObjectType.attributes.key" + ); + }); + + test('Return error if key refers to a non-existent attribute parent', () => { + const hasError = hasFilterKeyError('alert.not_a_key', ['alert'], mockMappings); + + expect(hasError).toEqual( + "This key 'alert.not_a_key' does NOT exist in alert saved object index patterns" + ); + }); + + test('Return error if key refers to a non-existent attribute', () => { + const hasError = hasFilterKeyError('bean.attributes.red', ['bean'], mockMappings); + + expect(hasError).toEqual( + "This key 'bean.attributes.red' does NOT exist in bean saved object index patterns" + ); + }); + }); + + describe('#fieldDefined', () => { + test('Return false if filter is using an non-existing key', () => { + const isFieldDefined = fieldDefined(mockMappings, 'foo.not_a_key'); + + expect(isFieldDefined).toBeFalsy(); + }); + + test('Return true if filter is using an existing key', () => { + const isFieldDefined = fieldDefined(mockMappings, 'foo.title'); + + expect(isFieldDefined).toBeTruthy(); + }); + + test('Return true if filter is using a default for a multi-field property', () => { + const isFieldDefined = fieldDefined(mockMappings, 'bean.canned'); + + expect(isFieldDefined).toBeTruthy(); + }); + + test('Return true if filter is using a non-default for a multi-field property', () => { + const isFieldDefined = fieldDefined(mockMappings, 'bean.canned.text'); + + expect(isFieldDefined).toBeTruthy(); + }); + }); }); diff --git a/src/core/server/saved_objects/service/lib/filter_utils.ts b/src/core/server/saved_objects/service/lib/filter_utils.ts index b81c7d3e0885a..54b0033c9fcbe 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.ts @@ -205,7 +205,25 @@ export const hasFilterKeyError = ( return null; }; -const fieldDefined = (indexMappings: IndexMapping, key: string) => { +export const fieldDefined = (indexMappings: IndexMapping, key: string): boolean => { const mappingKey = 'properties.' + key.split('.').join('.properties.'); - return get(indexMappings, mappingKey) != null; + const potentialKey = get(indexMappings, mappingKey); + + // If the `mappingKey` does not match a valid path, before returning null, + // we want to check and see if the intended path was for a multi-field + // such as `x.attributes.field.text` where `field` is mapped to both text + // and keyword + if (potentialKey == null) { + const propertiesAttribute = 'properties'; + const indexOfLastProperties = mappingKey.lastIndexOf(propertiesAttribute); + const fieldMapping = mappingKey.substr(0, indexOfLastProperties); + const fieldType = mappingKey.substr( + mappingKey.lastIndexOf(propertiesAttribute) + `${propertiesAttribute}.`.length + ); + const mapping = `${fieldMapping}fields.${fieldType}`; + + return get(indexMappings, mapping) != null; + } else { + return true; + } }; diff --git a/src/core/server/saved_objects/service/lib/repository.mock.ts b/src/core/server/saved_objects/service/lib/repository.mock.ts index c853e208f27aa..a3610b1e437e2 100644 --- a/src/core/server/saved_objects/service/lib/repository.mock.ts +++ b/src/core/server/saved_objects/service/lib/repository.mock.ts @@ -17,6 +17,8 @@ const create = (): jest.Mocked => ({ bulkGet: jest.fn(), find: jest.fn(), get: jest.fn(), + closePointInTime: jest.fn(), + openPointInTimeForType: jest.fn().mockResolvedValue({ id: 'some_pit_id' }), resolve: jest.fn(), update: jest.fn(), addToNamespaces: jest.fn(), diff --git a/src/core/server/saved_objects/service/lib/repository.test.js b/src/core/server/saved_objects/service/lib/repository.test.js index aac508fb5b909..e77143d13612f 100644 --- a/src/core/server/saved_objects/service/lib/repository.test.js +++ b/src/core/server/saved_objects/service/lib/repository.test.js @@ -2813,6 +2813,13 @@ describe('SavedObjectsRepository', () => { expect(client.search).not.toHaveBeenCalled(); }); + it(`throws when a preference is provided with pit`, async () => { + await expect( + savedObjectsRepository.find({ type: 'foo', pit: { id: 'abc123' }, preference: 'hi' }) + ).rejects.toThrowError('options.preference must be excluded when options.pit is used'); + expect(client.search).not.toHaveBeenCalled(); + }); + it(`throws when KQL filter syntax is invalid`, async () => { const findOpts = { namespaces: [namespace], @@ -2973,6 +2980,32 @@ describe('SavedObjectsRepository', () => { }); }); + it(`accepts searchAfter`, async () => { + const relevantOpts = { + ...commonOptions, + searchAfter: [1, 'a'], + }; + + await findSuccess(relevantOpts, namespace); + expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith(mappings, registry, { + ...relevantOpts, + searchAfter: [1, 'a'], + }); + }); + + it(`accepts pit`, async () => { + const relevantOpts = { + ...commonOptions, + pit: { id: 'abc123', keepAlive: '2m' }, + }; + + await findSuccess(relevantOpts, namespace); + expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith(mappings, registry, { + ...relevantOpts, + pit: { id: 'abc123', keepAlive: '2m' }, + }); + }); + it(`accepts KQL expression filter and passes KueryNode to getSearchDsl`, async () => { const findOpts = { namespaces: [namespace], @@ -4393,4 +4426,136 @@ describe('SavedObjectsRepository', () => { }); }); }); + + describe('#openPointInTimeForType', () => { + const type = 'index-pattern'; + + const generateResults = (id) => ({ id: id || null }); + const successResponse = async (type, options) => { + client.openPointInTime.mockResolvedValueOnce( + elasticsearchClientMock.createSuccessTransportRequestPromise(generateResults()) + ); + const result = await savedObjectsRepository.openPointInTimeForType(type, options); + expect(client.openPointInTime).toHaveBeenCalledTimes(1); + return result; + }; + + describe('client calls', () => { + it(`should use the ES PIT API`, async () => { + await successResponse(type); + expect(client.openPointInTime).toHaveBeenCalledTimes(1); + }); + + it(`accepts preference`, async () => { + await successResponse(type, { preference: 'pref' }); + expect(client.openPointInTime).toHaveBeenCalledWith( + expect.objectContaining({ + preference: 'pref', + }), + expect.anything() + ); + }); + + it(`accepts keepAlive`, async () => { + await successResponse(type, { keepAlive: '2m' }); + expect(client.openPointInTime).toHaveBeenCalledWith( + expect.objectContaining({ + keep_alive: '2m', + }), + expect.anything() + ); + }); + + it(`defaults keepAlive to 5m`, async () => { + await successResponse(type); + expect(client.openPointInTime).toHaveBeenCalledWith( + expect.objectContaining({ + keep_alive: '5m', + }), + expect.anything() + ); + }); + }); + + describe('errors', () => { + const expectNotFoundError = async (types) => { + await expect(savedObjectsRepository.openPointInTimeForType(types)).rejects.toThrowError( + createGenericNotFoundError() + ); + }; + + it(`throws when ES is unable to find the index`, async () => { + client.openPointInTime.mockResolvedValueOnce( + elasticsearchClientMock.createSuccessTransportRequestPromise({}, { statusCode: 404 }) + ); + await expectNotFoundError(type); + expect(client.openPointInTime).toHaveBeenCalledTimes(1); + }); + + it(`should return generic not found error when attempting to find only invalid or hidden types`, async () => { + const test = async (types) => { + await expectNotFoundError(types); + expect(client.openPointInTime).not.toHaveBeenCalled(); + }; + + await test('unknownType'); + await test(HIDDEN_TYPE); + await test(['unknownType', HIDDEN_TYPE]); + }); + }); + + describe('returns', () => { + it(`returns id in the expected format`, async () => { + const id = 'abc123'; + const results = generateResults(id); + client.openPointInTime.mockResolvedValueOnce( + elasticsearchClientMock.createSuccessTransportRequestPromise(results) + ); + const response = await savedObjectsRepository.openPointInTimeForType(type); + expect(response).toEqual({ id }); + }); + }); + }); + + describe('#closePointInTime', () => { + const generateResults = () => ({ succeeded: true, num_freed: 3 }); + const successResponse = async (id) => { + client.closePointInTime.mockResolvedValueOnce( + elasticsearchClientMock.createSuccessTransportRequestPromise(generateResults()) + ); + const result = await savedObjectsRepository.closePointInTime(id); + expect(client.closePointInTime).toHaveBeenCalledTimes(1); + return result; + }; + + describe('client calls', () => { + it(`should use the ES PIT API`, async () => { + await successResponse('abc123'); + expect(client.closePointInTime).toHaveBeenCalledTimes(1); + }); + + it(`accepts id`, async () => { + await successResponse('abc123'); + expect(client.closePointInTime).toHaveBeenCalledWith( + expect.objectContaining({ + body: expect.objectContaining({ + id: 'abc123', + }), + }), + expect.anything() + ); + }); + }); + + describe('returns', () => { + it(`returns response body from ES`, async () => { + const results = generateResults('abc123'); + client.closePointInTime.mockResolvedValueOnce( + elasticsearchClientMock.createSuccessTransportRequestPromise(results) + ); + const response = await savedObjectsRepository.closePointInTime('abc123'); + expect(response).toEqual(results); + }); + }); + }); }); diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index fcd72aa4326a2..b8a72377b0d76 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -36,6 +36,10 @@ import { SavedObjectsCreateOptions, SavedObjectsFindResponse, SavedObjectsFindResult, + SavedObjectsClosePointInTimeOptions, + SavedObjectsClosePointInTimeResponse, + SavedObjectsOpenPointInTimeOptions, + SavedObjectsOpenPointInTimeResponse, SavedObjectsUpdateOptions, SavedObjectsUpdateResponse, SavedObjectsBulkUpdateObject, @@ -708,11 +712,13 @@ export class SavedObjectsRepository { * Query field argument for more information * @property {integer} [options.page=1] * @property {integer} [options.perPage=20] + * @property {Array} [options.searchAfter] * @property {string} [options.sortField] * @property {string} [options.sortOrder] * @property {Array} [options.fields] * @property {string} [options.namespace] * @property {object} [options.hasReference] - { type, id } + * @property {string} [options.pit] * @property {string} [options.preference] * @returns {promise} - { saved_objects: [{ id, type, version, attributes }], total, per_page, page } */ @@ -726,6 +732,8 @@ export class SavedObjectsRepository { hasReferenceOperator, page = FIND_DEFAULT_PAGE, perPage = FIND_DEFAULT_PER_PAGE, + pit, + searchAfter, sortField, sortOrder, fields, @@ -752,6 +760,10 @@ export class SavedObjectsRepository { throw SavedObjectsErrorHelpers.createBadRequestError( 'options.namespaces must be an empty array when options.typeToNamespacesMap is used' ); + } else if (preference?.length && pit) { + throw SavedObjectsErrorHelpers.createBadRequestError( + 'options.preference must be excluded when options.pit is used' + ); } const types = type @@ -787,20 +799,24 @@ export class SavedObjectsRepository { } const esOptions = { - index: this.getIndicesForTypes(allowedTypes), - size: perPage, - from: perPage * (page - 1), + // If `pit` is provided, we drop the `index`, otherwise ES returns 400. + ...(pit ? {} : { index: this.getIndicesForTypes(allowedTypes) }), + // If `searchAfter` is provided, we drop `from` as it will not be used for pagination. + ...(searchAfter ? {} : { from: perPage * (page - 1) }), _source: includedFields(type, fields), - rest_total_hits_as_int: true, preference, + rest_total_hits_as_int: true, + size: perPage, body: { seq_no_primary_term: true, ...getSearchDsl(this._mappings, this._registry, { search, defaultSearchOperator, searchFields, + pit, rootSearchFields, type: allowedTypes, + searchAfter, sortField, sortOrder, namespaces, @@ -834,8 +850,10 @@ export class SavedObjectsRepository { (hit: SavedObjectsRawDoc): SavedObjectsFindResult => ({ ...this._rawToSavedObject(hit), score: (hit as any)._score, + ...((hit as any).sort && { sort: (hit as any).sort }), }) ), + ...(body.pit_id && { pit_id: body.pit_id }), } as SavedObjectsFindResponse; } @@ -1764,6 +1782,118 @@ export class SavedObjectsRepository { }; } + /** + * Opens a Point In Time (PIT) against the indices for the specified Saved Object types. + * The returned `id` can then be passed to `SavedObjects.find` to search against that PIT. + * + * @example + * ```ts + * const { id } = await savedObjectsClient.openPointInTimeForType( + * type: 'visualization', + * { keepAlive: '5m' }, + * ); + * const page1 = await savedObjectsClient.find({ + * type: 'visualization', + * sortField: 'updated_at', + * sortOrder: 'asc', + * pit: { id, keepAlive: '2m' }, + * }); + * const lastHit = page1.saved_objects[page1.saved_objects.length - 1]; + * const page2 = await savedObjectsClient.find({ + * type: 'visualization', + * sortField: 'updated_at', + * sortOrder: 'asc', + * pit: { id: page1.pit_id }, + * searchAfter: lastHit.sort, + * }); + * await savedObjectsClient.closePointInTime(page2.pit_id); + * ``` + * + * @param {string|Array} type + * @param {object} [options] - {@link SavedObjectsOpenPointInTimeOptions} + * @property {string} [options.keepAlive] + * @property {string} [options.preference] + * @returns {promise} - { id: string } + */ + async openPointInTimeForType( + type: string | string[], + { keepAlive = '5m', preference }: SavedObjectsOpenPointInTimeOptions = {} + ): Promise { + const types = Array.isArray(type) ? type : [type]; + const allowedTypes = types.filter((t) => this._allowedTypes.includes(t)); + if (allowedTypes.length === 0) { + throw SavedObjectsErrorHelpers.createGenericNotFoundError(); + } + + const esOptions = { + index: this.getIndicesForTypes(allowedTypes), + keep_alive: keepAlive, + ...(preference ? { preference } : {}), + }; + + const { + body, + statusCode, + } = await this.client.openPointInTime(esOptions, { + ignore: [404], + }); + if (statusCode === 404) { + throw SavedObjectsErrorHelpers.createGenericNotFoundError(); + } + + return { + id: body.id, + }; + } + + /** + * Closes a Point In Time (PIT) by ID. This simply proxies the request to ES + * via the Elasticsearch client, and is included in the Saved Objects Client + * as a convenience for consumers who are using `openPointInTimeForType`. + * + * @remarks + * While the `keepAlive` that is provided will cause a PIT to automatically close, + * it is highly recommended to explicitly close a PIT when you are done with it + * in order to avoid consuming unneeded resources in Elasticsearch. + * + * @example + * ```ts + * const repository = coreStart.savedObjects.createInternalRepository(); + * + * const { id } = await repository.openPointInTimeForType( + * type: 'index-pattern', + * { keepAlive: '2m' }, + * ); + * + * const response = await repository.find({ + * type: 'index-pattern', + * search: 'foo*', + * sortField: 'name', + * sortOrder: 'desc', + * pit: { + * id: 'abc123', + * keepAlive: '2m', + * }, + * searchAfter: [1234, 'abcd'], + * }); + * + * await repository.closePointInTime(response.pit_id); + * ``` + * + * @param {string} id + * @param {object} [options] - {@link SavedObjectsClosePointInTimeOptions} + * @returns {promise} - {@link SavedObjectsClosePointInTimeResponse} + */ + async closePointInTime( + id: string, + options?: SavedObjectsClosePointInTimeOptions + ): Promise { + const { body } = await this.client.closePointInTime({ + body: { id }, + }); + return body; + } + /** * Returns index specified by the given type or the default index * diff --git a/src/core/server/saved_objects/service/lib/repository_es_client.ts b/src/core/server/saved_objects/service/lib/repository_es_client.ts index dae72819726ad..6a601b1ed0c83 100644 --- a/src/core/server/saved_objects/service/lib/repository_es_client.ts +++ b/src/core/server/saved_objects/service/lib/repository_es_client.ts @@ -14,11 +14,13 @@ import { decorateEsError } from './decorate_es_error'; const methods = [ 'bulk', + 'closePointInTime', 'create', 'delete', 'get', 'index', 'mget', + 'openPointInTime', 'search', 'update', 'updateByQuery', diff --git a/src/core/server/saved_objects/service/lib/search_dsl/pit_params.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/pit_params.test.ts new file mode 100644 index 0000000000000..5a99168792e83 --- /dev/null +++ b/src/core/server/saved_objects/service/lib/search_dsl/pit_params.test.ts @@ -0,0 +1,28 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { getPitParams } from './pit_params'; + +describe('searchDsl/getPitParams', () => { + it('returns only an ID by default', () => { + expect(getPitParams({ id: 'abc123' })).toEqual({ + pit: { + id: 'abc123', + }, + }); + }); + + it('includes keepAlive if provided and rewrites to snake case', () => { + expect(getPitParams({ id: 'abc123', keepAlive: '2m' })).toEqual({ + pit: { + id: 'abc123', + keep_alive: '2m', + }, + }); + }); +}); diff --git a/src/fixtures/mock_state.js b/src/core/server/saved_objects/service/lib/search_dsl/pit_params.ts similarity index 60% rename from src/fixtures/mock_state.js rename to src/core/server/saved_objects/service/lib/search_dsl/pit_params.ts index cb18dac7b767d..1a8dcb5cca2e9 100644 --- a/src/fixtures/mock_state.js +++ b/src/core/server/saved_objects/service/lib/search_dsl/pit_params.ts @@ -6,15 +6,13 @@ * Side Public License, v 1. */ -import _ from 'lodash'; -import sinon from 'sinon'; +import { SavedObjectsPitParams } from '../../../types'; -function MockState(defaults) { - this.on = _.noop; - this.off = _.noop; - this.save = sinon.stub(); - this.replace = sinon.stub(); - _.assign(this, defaults); +export function getPitParams(pit: SavedObjectsPitParams) { + return { + pit: { + id: pit.id, + ...(pit.keepAlive ? { keep_alive: pit.keepAlive } : {}), + }, + }; } - -export default MockState; diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts index 9e91e585f74f0..fc26c837d5e52 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts @@ -6,14 +6,17 @@ * Side Public License, v 1. */ +jest.mock('./pit_params'); jest.mock('./query_params'); jest.mock('./sorting_params'); import { typeRegistryMock } from '../../../saved_objects_type_registry.mock'; +import * as pitParamsNS from './pit_params'; import * as queryParamsNS from './query_params'; import { getSearchDsl } from './search_dsl'; import * as sortParamsNS from './sorting_params'; +const getPitParams = pitParamsNS.getPitParams as jest.Mock; const getQueryParams = queryParamsNS.getQueryParams as jest.Mock; const getSortingParams = sortParamsNS.getSortingParams as jest.Mock; @@ -84,6 +87,7 @@ describe('getSearchDsl', () => { type: 'foo', sortField: 'bar', sortOrder: 'baz', + pit: { id: 'abc123' }, }; getSearchDsl(mappings, registry, opts); @@ -92,7 +96,8 @@ describe('getSearchDsl', () => { mappings, opts.type, opts.sortField, - opts.sortOrder + opts.sortOrder, + opts.pit ); }); @@ -101,5 +106,33 @@ describe('getSearchDsl', () => { getSortingParams.mockReturnValue({ b: 'b' }); expect(getSearchDsl(mappings, registry, { type: 'foo' })).toEqual({ a: 'a', b: 'b' }); }); + + it('returns searchAfter if provided', () => { + getQueryParams.mockReturnValue({ a: 'a' }); + getSortingParams.mockReturnValue({ b: 'b' }); + expect(getSearchDsl(mappings, registry, { type: 'foo', searchAfter: [1, 'bar'] })).toEqual({ + a: 'a', + b: 'b', + search_after: [1, 'bar'], + }); + }); + + it('returns pit if provided', () => { + getQueryParams.mockReturnValue({ a: 'a' }); + getSortingParams.mockReturnValue({ b: 'b' }); + getPitParams.mockReturnValue({ pit: { id: 'abc123' } }); + expect( + getSearchDsl(mappings, registry, { + type: 'foo', + searchAfter: [1, 'bar'], + pit: { id: 'abc123' }, + }) + ).toEqual({ + a: 'a', + b: 'b', + pit: { id: 'abc123' }, + search_after: [1, 'bar'], + }); + }); }); }); diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts index 4b4fa8865ee9d..cae5e43897bcf 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts @@ -9,7 +9,9 @@ import Boom from '@hapi/boom'; import { IndexMapping } from '../../../mappings'; +import { SavedObjectsPitParams } from '../../../types'; import { getQueryParams, HasReferenceQueryParams, SearchOperator } from './query_params'; +import { getPitParams } from './pit_params'; import { getSortingParams } from './sorting_params'; import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry'; @@ -21,9 +23,11 @@ interface GetSearchDslOptions { defaultSearchOperator?: SearchOperator; searchFields?: string[]; rootSearchFields?: string[]; + searchAfter?: unknown[]; sortField?: string; sortOrder?: string; namespaces?: string[]; + pit?: SavedObjectsPitParams; typeToNamespacesMap?: Map; hasReference?: HasReferenceQueryParams | HasReferenceQueryParams[]; hasReferenceOperator?: SearchOperator; @@ -41,9 +45,11 @@ export function getSearchDsl( defaultSearchOperator, searchFields, rootSearchFields, + searchAfter, sortField, sortOrder, namespaces, + pit, typeToNamespacesMap, hasReference, hasReferenceOperator, @@ -72,6 +78,8 @@ export function getSearchDsl( hasReferenceOperator, kueryNode, }), - ...getSortingParams(mappings, type, sortField, sortOrder), + ...getSortingParams(mappings, type, sortField, sortOrder, pit), + ...(pit ? getPitParams(pit) : {}), + ...(searchAfter ? { search_after: searchAfter } : {}), }; } diff --git a/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.test.ts index 1376f0d50a9da..73c7065705fc5 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.test.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.test.ts @@ -79,6 +79,11 @@ describe('searchDsl/getSortParams', () => { ], }); }); + it('appends tiebreaker when PIT is provided', () => { + expect(getSortingParams(MAPPINGS, 'saved', 'title', undefined, { id: 'abc' }).sort).toEqual( + expect.arrayContaining([{ _shard_doc: 'asc' }]) + ); + }); }); describe('sortField is simple root property with multiple types', () => { it('returns correct params', () => { @@ -93,6 +98,11 @@ describe('searchDsl/getSortParams', () => { ], }); }); + it('appends tiebreaker when PIT is provided', () => { + expect( + getSortingParams(MAPPINGS, ['saved', 'pending'], 'type', undefined, { id: 'abc' }).sort + ).toEqual(expect.arrayContaining([{ _shard_doc: 'asc' }])); + }); }); describe('sortField is simple non-root property with multiple types', () => { it('returns correct params', () => { @@ -114,6 +124,11 @@ describe('searchDsl/getSortParams', () => { ], }); }); + it('appends tiebreaker when PIT is provided', () => { + expect( + getSortingParams(MAPPINGS, 'saved', 'title.raw', undefined, { id: 'abc' }).sort + ).toEqual(expect.arrayContaining([{ _shard_doc: 'asc' }])); + }); }); describe('sortField is multi-field with single type as array', () => { it('returns correct params', () => { @@ -128,6 +143,11 @@ describe('searchDsl/getSortParams', () => { ], }); }); + it('appends tiebreaker when PIT is provided', () => { + expect( + getSortingParams(MAPPINGS, ['saved'], 'title.raw', undefined, { id: 'abc' }).sort + ).toEqual(expect.arrayContaining([{ _shard_doc: 'asc' }])); + }); }); describe('sortField is root multi-field with multiple types', () => { it('returns correct params', () => { @@ -142,6 +162,12 @@ describe('searchDsl/getSortParams', () => { ], }); }); + it('appends tiebreaker when PIT is provided', () => { + expect( + getSortingParams(MAPPINGS, ['saved', 'pending'], 'type.raw', undefined, { id: 'abc' }) + .sort + ).toEqual(expect.arrayContaining([{ _shard_doc: 'asc' }])); + }); }); describe('sortField is not-root multi-field with multiple types', () => { it('returns correct params', () => { diff --git a/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts b/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts index e3bfba6a80f59..abef9bfa0a300 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts @@ -8,6 +8,12 @@ import Boom from '@hapi/boom'; import { getProperty, IndexMapping } from '../../../mappings'; +import { SavedObjectsPitParams } from '../../../types'; + +// TODO: The plan is for ES to automatically add this tiebreaker when +// using PIT. We should remove this logic once that is resolved. +// https://github.com/elastic/elasticsearch/issues/56828 +const ES_PROVIDED_TIEBREAKER = { _shard_doc: 'asc' }; const TOP_LEVEL_FIELDS = ['_id', '_score']; @@ -15,7 +21,8 @@ export function getSortingParams( mappings: IndexMapping, type: string | string[], sortField?: string, - sortOrder?: string + sortOrder?: string, + pit?: SavedObjectsPitParams ) { if (!sortField) { return {}; @@ -31,6 +38,7 @@ export function getSortingParams( order: sortOrder, }, }, + ...(pit ? [ES_PROVIDED_TIEBREAKER] : []), ], }; } @@ -51,6 +59,7 @@ export function getSortingParams( unmapped_type: rootField.type, }, }, + ...(pit ? [ES_PROVIDED_TIEBREAKER] : []), ], }; } @@ -75,6 +84,7 @@ export function getSortingParams( unmapped_type: field.type, }, }, + ...(pit ? [ES_PROVIDED_TIEBREAKER] : []), ], }; } diff --git a/src/core/server/saved_objects/service/saved_objects_client.mock.ts b/src/core/server/saved_objects/service/saved_objects_client.mock.ts index 72f5561aa7027..ecca652cace37 100644 --- a/src/core/server/saved_objects/service/saved_objects_client.mock.ts +++ b/src/core/server/saved_objects/service/saved_objects_client.mock.ts @@ -20,6 +20,8 @@ const create = () => bulkGet: jest.fn(), find: jest.fn(), get: jest.fn(), + closePointInTime: jest.fn(), + openPointInTimeForType: jest.fn().mockResolvedValue({ id: 'some_pit_id' }), resolve: jest.fn(), update: jest.fn(), addToNamespaces: jest.fn(), diff --git a/src/core/server/saved_objects/service/saved_objects_client.test.js b/src/core/server/saved_objects/service/saved_objects_client.test.js index 45b0cf70b0dc6..7cbddaf195dc9 100644 --- a/src/core/server/saved_objects/service/saved_objects_client.test.js +++ b/src/core/server/saved_objects/service/saved_objects_client.test.js @@ -115,6 +115,36 @@ test(`#get`, async () => { expect(result).toBe(returnValue); }); +test(`#openPointInTimeForType`, async () => { + const returnValue = Symbol(); + const mockRepository = { + openPointInTimeForType: jest.fn().mockResolvedValue(returnValue), + }; + const client = new SavedObjectsClient(mockRepository); + + const type = Symbol(); + const options = Symbol(); + const result = await client.openPointInTimeForType(type, options); + + expect(mockRepository.openPointInTimeForType).toHaveBeenCalledWith(type, options); + expect(result).toBe(returnValue); +}); + +test(`#closePointInTime`, async () => { + const returnValue = Symbol(); + const mockRepository = { + closePointInTime: jest.fn().mockResolvedValue(returnValue), + }; + const client = new SavedObjectsClient(mockRepository); + + const id = Symbol(); + const options = Symbol(); + const result = await client.closePointInTime(id, options); + + expect(mockRepository.closePointInTime).toHaveBeenCalledWith(id, options); + expect(result).toBe(returnValue); +}); + test(`#resolve`, async () => { const returnValue = Symbol(); const mockRepository = { diff --git a/src/core/server/saved_objects/service/saved_objects_client.ts b/src/core/server/saved_objects/service/saved_objects_client.ts index b90540fbfa971..b93f3022e4236 100644 --- a/src/core/server/saved_objects/service/saved_objects_client.ts +++ b/src/core/server/saved_objects/service/saved_objects_client.ts @@ -129,6 +129,35 @@ export interface SavedObjectsFindResult extends SavedObject { * The Elasticsearch `_score` of this result. */ score: number; + /** + * The Elasticsearch `sort` value of this result. + * + * @remarks + * This can be passed directly to the `searchAfter` param in the {@link SavedObjectsFindOptions} + * in order to page through large numbers of hits. It is recommended you use this alongside + * a Point In Time (PIT) that was opened with {@link SavedObjectsClient.openPointInTimeForType}. + * + * @example + * ```ts + * const { id } = await savedObjectsClient.openPointInTimeForType('visualization'); + * const page1 = await savedObjectsClient.find({ + * type: 'visualization', + * sortField: 'updated_at', + * sortOrder: 'asc', + * pit: { id }, + * }); + * const lastHit = page1.saved_objects[page1.saved_objects.length - 1]; + * const page2 = await savedObjectsClient.find({ + * type: 'visualization', + * sortField: 'updated_at', + * sortOrder: 'asc', + * pit: { id: page1.pit_id }, + * searchAfter: lastHit.sort, + * }); + * await savedObjectsClient.closePointInTime(page2.pit_id); + * ``` + */ + sort?: unknown[]; } /** @@ -144,6 +173,7 @@ export interface SavedObjectsFindResponse { total: number; per_page: number; page: number; + pit_id?: string; } /** @@ -311,6 +341,50 @@ export interface SavedObjectsResolveResponse { outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; } +/** + * @public + */ +export interface SavedObjectsOpenPointInTimeOptions extends SavedObjectsBaseOptions { + /** + * Optionally specify how long ES should keep the PIT alive until the next request. Defaults to `5m`. + */ + keepAlive?: string; + /** + * An optional ES preference value to be used for the query. + */ + preference?: string; +} + +/** + * @public + */ +export interface SavedObjectsOpenPointInTimeResponse { + /** + * PIT ID returned from ES. + */ + id: string; +} + +/** + * @public + */ +export type SavedObjectsClosePointInTimeOptions = SavedObjectsBaseOptions; + +/** + * @public + */ +export interface SavedObjectsClosePointInTimeResponse { + /** + * If true, all search contexts associated with the PIT id are + * successfully closed. + */ + succeeded: boolean; + /** + * The number of search contexts that have been successfully closed. + */ + num_freed: number; +} + /** * * @public @@ -504,4 +578,25 @@ export class SavedObjectsClient { ) { return await this._repository.removeReferencesTo(type, id, options); } + + /** + * Opens a Point In Time (PIT) against the indices for the specified Saved Object types. + * The returned `id` can then be passed to {@link SavedObjectsClient.find} to search + * against that PIT. + */ + async openPointInTimeForType( + type: string | string[], + options: SavedObjectsOpenPointInTimeOptions = {} + ) { + return await this._repository.openPointInTimeForType(type, options); + } + + /** + * Closes a Point In Time (PIT) by ID. This simply proxies the request to ES via the + * Elasticsearch client, and is included in the Saved Objects Client as a convenience + * for consumers who are using {@link SavedObjectsClient.openPointInTimeForType}. + */ + async closePointInTime(id: string, options?: SavedObjectsClosePointInTimeOptions) { + return await this._repository.closePointInTime(id, options); + } } diff --git a/src/core/server/saved_objects/types.ts b/src/core/server/saved_objects/types.ts index d122e92aba398..66110d096213f 100644 --- a/src/core/server/saved_objects/types.ts +++ b/src/core/server/saved_objects/types.ts @@ -62,6 +62,14 @@ export interface SavedObjectsFindOptionsReference { id: string; } +/** + * @public + */ +export interface SavedObjectsPitParams { + id: string; + keepAlive?: string; +} + /** * * @public @@ -82,6 +90,10 @@ export interface SavedObjectsFindOptions { search?: string; /** The fields to perform the parsed query against. See Elasticsearch Simple Query String `fields` argument for more information */ searchFields?: string[]; + /** + * Use the sort values from the previous page to retrieve the next page of results. + */ + searchAfter?: unknown[]; /** * The fields to perform the parsed query against. Unlike the `searchFields` argument, these are expected to be root fields and will not * be modified. If used in conjunction with `searchFields`, both are concatenated together. @@ -114,6 +126,10 @@ export interface SavedObjectsFindOptions { typeToNamespacesMap?: Map; /** An optional ES preference value to be used for the query **/ preference?: string; + /** + * Search against a specific Point In Time (PIT) that you've opened with {@link SavedObjectsClient.openPointInTimeForType}. + */ + pit?: SavedObjectsPitParams; } /** diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index 09207608908a4..b5f8b9d69abf3 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -449,6 +449,7 @@ export interface CoreConfigUsageData { }; // (undocumented) savedObjects: { + customIndex: boolean; maxImportPayloadBytes: number; maxImportExportSizeBytes: number; }; @@ -2222,6 +2223,7 @@ export class SavedObjectsClient { bulkGet(objects?: SavedObjectsBulkGetObject[], options?: SavedObjectsBaseOptions): Promise>; bulkUpdate(objects: Array>, options?: SavedObjectsBulkUpdateOptions): Promise>; checkConflicts(objects?: SavedObjectsCheckConflictsObject[], options?: SavedObjectsBaseOptions): Promise; + closePointInTime(id: string, options?: SavedObjectsClosePointInTimeOptions): Promise; create(type: string, attributes: T, options?: SavedObjectsCreateOptions): Promise>; delete(type: string, id: string, options?: SavedObjectsDeleteOptions): Promise<{}>; deleteFromNamespaces(type: string, id: string, namespaces: string[], options?: SavedObjectsDeleteFromNamespacesOptions): Promise; @@ -2231,6 +2233,7 @@ export class SavedObjectsClient { errors: typeof SavedObjectsErrorHelpers; find(options: SavedObjectsFindOptions): Promise>; get(type: string, id: string, options?: SavedObjectsBaseOptions): Promise>; + openPointInTimeForType(type: string | string[], options?: SavedObjectsOpenPointInTimeOptions): Promise; removeReferencesTo(type: string, id: string, options?: SavedObjectsRemoveReferencesToOptions): Promise; resolve(type: string, id: string, options?: SavedObjectsBaseOptions): Promise>; update(type: string, id: string, attributes: Partial, options?: SavedObjectsUpdateOptions): Promise>; @@ -2269,6 +2272,15 @@ export interface SavedObjectsClientWrapperOptions { typeRegistry: ISavedObjectTypeRegistry; } +// @public (undocumented) +export type SavedObjectsClosePointInTimeOptions = SavedObjectsBaseOptions; + +// @public (undocumented) +export interface SavedObjectsClosePointInTimeResponse { + num_freed: number; + succeeded: boolean; +} + // @public export interface SavedObjectsComplexFieldMapping { // (undocumented) @@ -2419,10 +2431,11 @@ export interface SavedObjectsExportByTypeOptions extends SavedObjectExportBaseOp export class SavedObjectsExporter { // (undocumented) #private; - constructor({ savedObjectsClient, typeRegistry, exportSizeLimit, }: { + constructor({ savedObjectsClient, typeRegistry, exportSizeLimit, logger, }: { savedObjectsClient: SavedObjectsClientContract; typeRegistry: ISavedObjectTypeRegistry; exportSizeLimit: number; + logger: Logger; }); exportByObjects(options: SavedObjectsExportByObjectOptions): Promise; exportByTypes(options: SavedObjectsExportByTypeOptions): Promise; @@ -2480,9 +2493,11 @@ export interface SavedObjectsFindOptions { page?: number; // (undocumented) perPage?: number; + pit?: SavedObjectsPitParams; preference?: string; rootSearchFields?: string[]; search?: string; + searchAfter?: unknown[]; searchFields?: string[]; // (undocumented) sortField?: string; @@ -2508,6 +2523,8 @@ export interface SavedObjectsFindResponse { // (undocumented) per_page: number; // (undocumented) + pit_id?: string; + // (undocumented) saved_objects: Array>; // (undocumented) total: number; @@ -2516,6 +2533,7 @@ export interface SavedObjectsFindResponse { // @public (undocumented) export interface SavedObjectsFindResult extends SavedObject { score: number; + sort?: unknown[]; } // @public @@ -2742,6 +2760,25 @@ export interface SavedObjectsMigrationVersion { // @public export type SavedObjectsNamespaceType = 'single' | 'multiple' | 'agnostic'; +// @public (undocumented) +export interface SavedObjectsOpenPointInTimeOptions extends SavedObjectsBaseOptions { + keepAlive?: string; + preference?: string; +} + +// @public (undocumented) +export interface SavedObjectsOpenPointInTimeResponse { + id: string; +} + +// @public (undocumented) +export interface SavedObjectsPitParams { + // (undocumented) + id: string; + // (undocumented) + keepAlive?: string; +} + // @public export interface SavedObjectsRawDoc { // (undocumented) @@ -2778,6 +2815,7 @@ export class SavedObjectsRepository { bulkGet(objects?: SavedObjectsBulkGetObject[], options?: SavedObjectsBaseOptions): Promise>; bulkUpdate(objects: Array>, options?: SavedObjectsBulkUpdateOptions): Promise>; checkConflicts(objects?: SavedObjectsCheckConflictsObject[], options?: SavedObjectsBaseOptions): Promise; + closePointInTime(id: string, options?: SavedObjectsClosePointInTimeOptions): Promise; create(type: string, attributes: T, options?: SavedObjectsCreateOptions): Promise>; // Warning: (ae-forgotten-export) The symbol "IKibanaMigrator" needs to be exported by the entry point index.d.ts // @@ -2790,6 +2828,7 @@ export class SavedObjectsRepository { find(options: SavedObjectsFindOptions): Promise>; get(type: string, id: string, options?: SavedObjectsBaseOptions): Promise>; incrementCounter(type: string, id: string, counterFields: Array, options?: SavedObjectsIncrementCounterOptions): Promise>; + openPointInTimeForType(type: string | string[], { keepAlive, preference }?: SavedObjectsOpenPointInTimeOptions): Promise; removeReferencesTo(type: string, id: string, options?: SavedObjectsRemoveReferencesToOptions): Promise; resolve(type: string, id: string, options?: SavedObjectsBaseOptions): Promise>; update(type: string, id: string, attributes: Partial, options?: SavedObjectsUpdateOptions): Promise>; @@ -2954,10 +2993,12 @@ export interface SearchResponse { highlight?: any; inner_hits?: any; matched_queries?: string[]; - sort?: string[]; + sort?: unknown[]; }>; }; // (undocumented) + pit_id?: string; + // (undocumented) _scroll_id?: string; // (undocumented) _shards: ShardsResponse; @@ -3112,6 +3153,7 @@ export interface UiSettingsParams { name?: string; optionLabels?: Record; options?: string[]; + order?: number; readonly?: boolean; requiresPageReload?: boolean; // (undocumented) @@ -3134,7 +3176,7 @@ export interface UiSettingsServiceStart { } // @public -export type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image'; +export type UiSettingsType = 'undefined' | 'json' | 'markdown' | 'number' | 'select' | 'boolean' | 'string' | 'array' | 'image' | 'color'; // @public export interface UserProvidedValues { diff --git a/src/core/server/types.ts b/src/core/server/types.ts index 1839ee68190aa..2ae51d4452a4e 100644 --- a/src/core/server/types.ts +++ b/src/core/server/types.ts @@ -31,6 +31,7 @@ export type { SavedObjectStatusMeta, SavedObjectsFindOptionsReference, SavedObjectsFindOptions, + SavedObjectsPitParams, SavedObjectsBaseOptions, MutatingOperationRefreshSetting, SavedObjectsClientContract, diff --git a/src/core/types/ui_settings.ts b/src/core/types/ui_settings.ts index e50dc18d9ff1f..235553293d153 100644 --- a/src/core/types/ui_settings.ts +++ b/src/core/types/ui_settings.ts @@ -22,7 +22,8 @@ export type UiSettingsType = | 'boolean' | 'string' | 'array' - | 'image'; + | 'image' + | 'color'; /** * UiSettings deprecation field options. @@ -65,6 +66,13 @@ export interface UiSettingsParams { type?: UiSettingsType; /** optional deprecation information. Used to generate a deprecation warning. */ deprecation?: DeprecationSettings; + /** + * index of the settings within its category (ascending order, smallest will be displayed first). + * Used for ordering in the UI. + * + * @remark settings without order defined will be displayed last and ordered by name + */ + order?: number; /* * Allows defining a custom validation applicable to value change on the client. * @deprecated diff --git a/src/dev/build/tasks/os_packages/create_os_package_tasks.ts b/src/dev/build/tasks/os_packages/create_os_package_tasks.ts index e18460d65a3d0..e37a61582c6a8 100644 --- a/src/dev/build/tasks/os_packages/create_os_package_tasks.ts +++ b/src/dev/build/tasks/os_packages/create_os_package_tasks.ts @@ -54,15 +54,13 @@ export const CreateDockerCentOS: Task = { async run(config, log, build) { await runDockerGenerator(config, log, build, { - ubi: false, - context: false, architecture: 'x64', + context: false, image: true, }); await runDockerGenerator(config, log, build, { - ubi: false, - context: false, architecture: 'aarch64', + context: false, image: true, }); }, @@ -74,9 +72,9 @@ export const CreateDockerUBI: Task = { async run(config, log, build) { if (!build.isOss()) { await runDockerGenerator(config, log, build, { - ubi: true, - context: false, architecture: 'x64', + context: false, + ubi: true, image: true, }); } @@ -88,7 +86,6 @@ export const CreateDockerContexts: Task = { async run(config, log, build) { await runDockerGenerator(config, log, build, { - ubi: false, context: true, image: false, }); @@ -99,6 +96,11 @@ export const CreateDockerContexts: Task = { context: true, image: false, }); + await runDockerGenerator(config, log, build, { + ironbank: true, + context: true, + image: false, + }); } }, }; diff --git a/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts b/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts index 7eeeaebe6e4be..a633e919cc5db 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/bundle_dockerfiles.ts @@ -7,18 +7,18 @@ */ import { resolve } from 'path'; +import { readFileSync } from 'fs'; import { ToolingLog } from '@kbn/dev-utils'; +import Mustache from 'mustache'; import { compressTar, copyAll, mkdirp, write, Config } from '../../../lib'; import { dockerfileTemplate } from './templates'; import { TemplateContext } from './template_context'; export async function bundleDockerFiles(config: Config, log: ToolingLog, scope: TemplateContext) { - log.info( - `Generating kibana${scope.imageFlavor}${scope.ubiImageFlavor} docker build context bundle` - ); - const dockerFilesDirName = `kibana${scope.imageFlavor}${scope.ubiImageFlavor}-${scope.version}-docker-build-context`; + log.info(`Generating kibana${scope.imageFlavor} docker build context bundle`); + const dockerFilesDirName = `kibana${scope.imageFlavor}-${scope.version}-docker-build-context`; const dockerFilesBuildDir = resolve(scope.dockerBuildDir, dockerFilesDirName); const dockerFilesOutputDir = config.resolveFromTarget(`${dockerFilesDirName}.tar.gz`); @@ -38,6 +38,17 @@ export async function bundleDockerFiles(config: Config, log: ToolingLog, scope: // dockerfiles folder await copyAll(resolve(scope.dockerBuildDir, 'bin'), resolve(dockerFilesBuildDir, 'bin')); await copyAll(resolve(scope.dockerBuildDir, 'config'), resolve(dockerFilesBuildDir, 'config')); + if (scope.ironbank) { + await copyAll(resolve(scope.dockerBuildDir), resolve(dockerFilesBuildDir), { + select: ['LICENSE'], + }); + const templates = ['hardening_manifest.yml', 'README.md']; + for (const template of templates) { + const file = readFileSync(resolve(__dirname, 'templates/ironbank', template)); + const output = Mustache.render(file.toString(), scope); + await write(resolve(dockerFilesBuildDir, template), output); + } + } // Compress dockerfiles dir created inside // docker build dir as output it as a target diff --git a/src/dev/build/tasks/os_packages/docker_generator/resources/bin/kibana-docker b/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker similarity index 100% rename from src/dev/build/tasks/os_packages/docker_generator/resources/bin/kibana-docker rename to src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker diff --git a/src/dev/build/tasks/os_packages/docker_generator/resources/ironbank/LICENSE b/src/dev/build/tasks/os_packages/docker_generator/resources/ironbank/LICENSE new file mode 100644 index 0000000000000..632c3abe22e9b --- /dev/null +++ b/src/dev/build/tasks/os_packages/docker_generator/resources/ironbank/LICENSE @@ -0,0 +1,280 @@ +ELASTIC LICENSE AGREEMENT + +PLEASE READ CAREFULLY THIS ELASTIC LICENSE AGREEMENT (THIS "AGREEMENT"), WHICH +CONSTITUTES A LEGALLY BINDING AGREEMENT AND GOVERNS ALL OF YOUR USE OF ALL OF +THE ELASTIC SOFTWARE WITH WHICH THIS AGREEMENT IS INCLUDED ("ELASTIC SOFTWARE") +THAT IS PROVIDED IN OBJECT CODE FORMAT, AND, IN ACCORDANCE WITH SECTION 2 BELOW, +CERTAIN OF THE ELASTIC SOFTWARE THAT IS PROVIDED IN SOURCE CODE FORMAT. BY +INSTALLING OR USING ANY OF THE ELASTIC SOFTWARE GOVERNED BY THIS AGREEMENT, YOU +ARE ASSENTING TO THE TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE +WITH SUCH TERMS AND CONDITIONS, YOU MAY NOT INSTALL OR USE THE ELASTIC SOFTWARE +GOVERNED BY THIS AGREEMENT. IF YOU ARE INSTALLING OR USING THE SOFTWARE ON +BEHALF OF A LEGAL ENTITY, YOU REPRESENT AND WARRANT THAT YOU HAVE THE ACTUAL +AUTHORITY TO AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT ON BEHALF OF +SUCH ENTITY. + +Posted Date: April 20, 2018 + +This Agreement is entered into by and between Elasticsearch BV ("Elastic") and +You, or the legal entity on behalf of whom You are acting (as applicable, +"You"). + +1. OBJECT CODE END USER LICENSES, RESTRICTIONS AND THIRD PARTY OPEN SOURCE +SOFTWARE + + 1.1 Object Code End User License. Subject to the terms and conditions of + Section 1.2 of this Agreement, Elastic hereby grants to You, AT NO CHARGE and + for so long as you are not in breach of any provision of this Agreement, a + License to the Basic Features and Functions of the Elastic Software. + + 1.2 Reservation of Rights; Restrictions. As between Elastic and You, Elastic + and its licensors own all right, title and interest in and to the Elastic + Software, and except as expressly set forth in Sections 1.1, and 2.1 of this + Agreement, no other license to the Elastic Software is granted to You under + this Agreement, by implication, estoppel or otherwise. You agree not to: (i) + reverse engineer or decompile, decrypt, disassemble or otherwise reduce any + Elastic Software provided to You in Object Code, or any portion thereof, to + Source Code, except and only to the extent any such restriction is prohibited + by applicable law, (ii) except as expressly permitted in this Agreement, + prepare derivative works from, modify, copy or use the Elastic Software Object + Code or the Commercial Software Source Code in any manner; (iii) except as + expressly permitted in Section 1.1 above, transfer, sell, rent, lease, + distribute, sublicense, loan or otherwise transfer, Elastic Software Object + Code, in whole or in part, to any third party; (iv) use Elastic Software + Object Code for providing time-sharing services, any software-as-a-service, + service bureau services or as part of an application services provider or + other service offering (collectively, "SaaS Offering") where obtaining access + to the Elastic Software or the features and functions of the Elastic Software + is a primary reason or substantial motivation for users of the SaaS Offering + to access and/or use the SaaS Offering ("Prohibited SaaS Offering"); (v) + circumvent the limitations on use of Elastic Software provided to You in + Object Code format that are imposed or preserved by any License Key, or (vi) + alter or remove any Marks and Notices in the Elastic Software. If You have any + question as to whether a specific SaaS Offering constitutes a Prohibited SaaS + Offering, or are interested in obtaining Elastic's permission to engage in + commercial or non-commercial distribution of the Elastic Software, please + contact elastic_license@elastic.co. + + 1.3 Third Party Open Source Software. The Commercial Software may contain or + be provided with third party open source libraries, components, utilities and + other open source software (collectively, "Open Source Software"), which Open + Source Software may have applicable license terms as identified on a website + designated by Elastic. Notwithstanding anything to the contrary herein, use of + the Open Source Software shall be subject to the license terms and conditions + applicable to such Open Source Software, to the extent required by the + applicable licensor (which terms shall not restrict the license rights granted + to You hereunder, but may contain additional rights). To the extent any + condition of this Agreement conflicts with any license to the Open Source + Software, the Open Source Software license will govern with respect to such + Open Source Software only. Elastic may also separately provide you with + certain open source software that is licensed by Elastic. Your use of such + Elastic open source software will not be governed by this Agreement, but by + the applicable open source license terms. + +2. COMMERCIAL SOFTWARE SOURCE CODE + + 2.1 Limited License. Subject to the terms and conditions of Section 2.2 of + this Agreement, Elastic hereby grants to You, AT NO CHARGE and for so long as + you are not in breach of any provision of this Agreement, a limited, + non-exclusive, non-transferable, fully paid up royalty free right and license + to the Commercial Software in Source Code format, without the right to grant + or authorize sublicenses, to prepare Derivative Works of the Commercial + Software, provided You (i) do not hack the licensing mechanism, or otherwise + circumvent the intended limitations on the use of Elastic Software to enable + features other than Basic Features and Functions or those features You are + entitled to as part of a Subscription, and (ii) use the resulting object code + only for reasonable testing purposes. + + 2.2 Restrictions. Nothing in Section 2.1 grants You the right to (i) use the + Commercial Software Source Code other than in accordance with Section 2.1 + above, (ii) use a Derivative Work of the Commercial Software outside of a + Non-production Environment, in any production capacity, on a temporary or + permanent basis, or (iii) transfer, sell, rent, lease, distribute, sublicense, + loan or otherwise make available the Commercial Software Source Code, in whole + or in part, to any third party. Notwithstanding the foregoing, You may + maintain a copy of the repository in which the Source Code of the Commercial + Software resides and that copy may be publicly accessible, provided that you + include this Agreement with Your copy of the repository. + +3. TERMINATION + + 3.1 Termination. This Agreement will automatically terminate, whether or not + You receive notice of such Termination from Elastic, if You breach any of its + provisions. + + 3.2 Post Termination. Upon any termination of this Agreement, for any reason, + You shall promptly cease the use of the Elastic Software in Object Code format + and cease use of the Commercial Software in Source Code format. For the + avoidance of doubt, termination of this Agreement will not affect Your right + to use Elastic Software, in either Object Code or Source Code formats, made + available under the Apache License Version 2.0. + + 3.3 Survival. Sections 1.2, 2.2. 3.3, 4 and 5 shall survive any termination or + expiration of this Agreement. + +4. DISCLAIMER OF WARRANTIES AND LIMITATION OF LIABILITY + + 4.1 Disclaimer of Warranties. TO THE MAXIMUM EXTENT PERMITTED UNDER APPLICABLE + LAW, THE ELASTIC SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, + AND ELASTIC AND ITS LICENSORS MAKE NO WARRANTIES WHETHER EXPRESSED, IMPLIED OR + STATUTORY REGARDING OR RELATING TO THE ELASTIC SOFTWARE. TO THE MAXIMUM EXTENT + PERMITTED UNDER APPLICABLE LAW, ELASTIC AND ITS LICENSORS SPECIFICALLY + DISCLAIM ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + PURPOSE AND NON-INFRINGEMENT WITH RESPECT TO THE ELASTIC SOFTWARE, AND WITH + RESPECT TO THE USE OF THE FOREGOING. FURTHER, ELASTIC DOES NOT WARRANT RESULTS + OF USE OR THAT THE ELASTIC SOFTWARE WILL BE ERROR FREE OR THAT THE USE OF THE + ELASTIC SOFTWARE WILL BE UNINTERRUPTED. + + 4.2 Limitation of Liability. IN NO EVENT SHALL ELASTIC OR ITS LICENSORS BE + LIABLE TO YOU OR ANY THIRD PARTY FOR ANY DIRECT OR INDIRECT DAMAGES, + INCLUDING, WITHOUT LIMITATION, FOR ANY LOSS OF PROFITS, LOSS OF USE, BUSINESS + INTERRUPTION, LOSS OF DATA, COST OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY + SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY KIND, IN CONNECTION WITH + OR ARISING OUT OF THE USE OR INABILITY TO USE THE ELASTIC SOFTWARE, OR THE + PERFORMANCE OF OR FAILURE TO PERFORM THIS AGREEMENT, WHETHER ALLEGED AS A + BREACH OF CONTRACT OR TORTIOUS CONDUCT, INCLUDING NEGLIGENCE, EVEN IF ELASTIC + HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +5. MISCELLANEOUS + + This Agreement completely and exclusively states the entire agreement of the + parties regarding the subject matter herein, and it supersedes, and its terms + govern, all prior proposals, agreements, or other communications between the + parties, oral or written, regarding such subject matter. This Agreement may be + modified by Elastic from time to time, and any such modifications will be + effective upon the "Posted Date" set forth at the top of the modified + Agreement. If any provision hereof is held unenforceable, this Agreement will + continue without said provision and be interpreted to reflect the original + intent of the parties. This Agreement and any non-contractual obligation + arising out of or in connection with it, is governed exclusively by Dutch law. + This Agreement shall not be governed by the 1980 UN Convention on Contracts + for the International Sale of Goods. All disputes arising out of or in + connection with this Agreement, including its existence and validity, shall be + resolved by the courts with jurisdiction in Amsterdam, The Netherlands, except + where mandatory law provides for the courts at another location in The + Netherlands to have jurisdiction. The parties hereby irrevocably waive any and + all claims and defenses either might otherwise have in any such action or + proceeding in any of such courts based upon any alleged lack of personal + jurisdiction, improper venue, forum non conveniens or any similar claim or + defense. A breach or threatened breach, by You of Section 2 may cause + irreparable harm for which damages at law may not provide adequate relief, and + therefore Elastic shall be entitled to seek injunctive relief without being + required to post a bond. You may not assign this Agreement (including by + operation of law in connection with a merger or acquisition), in whole or in + part to any third party without the prior written consent of Elastic, which + may be withheld or granted by Elastic in its sole and absolute discretion. + Any assignment in violation of the preceding sentence is void. Notices to + Elastic may also be sent to legal@elastic.co. + +6. DEFINITIONS + + The following terms have the meanings ascribed: + + 6.1 "Affiliate" means, with respect to a party, any entity that controls, is + controlled by, or which is under common control with, such party, where + "control" means ownership of at least fifty percent (50%) of the outstanding + voting shares of the entity, or the contractual right to establish policy for, + and manage the operations of, the entity. + + 6.2 "Basic Features and Functions" means those features and functions of the + Elastic Software that are eligible for use under a Basic license, as set forth + at https://www.elastic.co/subscriptions, as may be modified by Elastic from + time to time. + + 6.3 "Commercial Software" means the Elastic Software Source Code in any file + containing a header stating the contents are subject to the Elastic License or + which is contained in the repository folder labeled "x-pack", unless a LICENSE + file present in the directory subtree declares a different license. + + 6.4 "Derivative Work of the Commercial Software" means, for purposes of this + Agreement, any modification(s) or enhancement(s) to the Commercial Software, + which represent, as a whole, an original work of authorship. + + 6.5 "License" means a limited, non-exclusive, non-transferable, fully paid up, + royalty free, right and license, without the right to grant or authorize + sublicenses, solely for Your internal business operations to (i) install and + use the applicable Features and Functions of the Elastic Software in Object + Code, and (ii) permit Contractors and Your Affiliates to use the Elastic + software as set forth in (i) above, provided that such use by Contractors must + be solely for Your benefit and/or the benefit of Your Affiliates, and You + shall be responsible for all acts and omissions of such Contractors and + Affiliates in connection with their use of the Elastic software that are + contrary to the terms and conditions of this Agreement. + + 6.6 "License Key" means a sequence of bytes, including but not limited to a + JSON blob, that is used to enable certain features and functions of the + Elastic Software. + + 6.7 "Marks and Notices" means all Elastic trademarks, trade names, logos and + notices present on the Documentation as originally provided by Elastic. + + 6.8 "Non-production Environment" means an environment for development, testing + or quality assurance, where software is not used for production purposes. + + 6.9 "Object Code" means any form resulting from mechanical transformation or + translation of Source Code form, including but not limited to compiled object + code, generated documentation, and conversions to other media types. + + 6.10 "Source Code" means the preferred form of computer software for making + modifications, including but not limited to software source code, + documentation source, and configuration files. + + 6.11 "Subscription" means the right to receive Support Services and a License + to the Commercial Software. + + +GOVERNMENT END USER ADDENDUM TO THE ELASTIC LICENSE AGREEMENT + + This ADDENDUM TO THE ELASTIC LICENSE AGREEMENT (this "Addendum") applies +only to U.S. Federal Government, State Government, and Local Government +entities ("Government End Users") of the Elastic Software. This Addendum is +subject to, and hereby incorporated into, the Elastic License Agreement, +which is being entered into as of even date herewith, by Elastic and You (the +"Agreement"). This Addendum sets forth additional terms and conditions +related to Your use of the Elastic Software. Capitalized terms not defined in +this Addendum have the meaning set forth in the Agreement. + + 1. LIMITED LICENSE TO DISTRIBUTE (DSOP ONLY). Subject to the terms and +conditions of the Agreement (including this Addendum), Elastic grants the +Department of Defense Enterprise DevSecOps Initiative (DSOP) a royalty-free, +non-exclusive, non-transferable, limited license to reproduce and distribute +the Elastic Software solely through a software distribution repository +controlled and managed by DSOP, provided that DSOP: (i) distributes the +Elastic Software complete and unmodified, inclusive of the Agreement +(including this Addendum) and (ii) does not remove or alter any proprietary +legends or notices contained in the Elastic Software. + + 2. CHOICE OF LAW. The choice of law and venue provisions set forth shall +prevail over those set forth in Section 5 of the Agreement. + + "For U.S. Federal Government Entity End Users. This Agreement and any + non-contractual obligation arising out of or in connection with it, is + governed exclusively by U.S. Federal law. To the extent permitted by + federal law, the laws of the State of Delaware (excluding Delaware choice + of law rules) will apply in the absence of applicable federal law. + + For State and Local Government Entity End Users. This Agreement and any + non-contractual obligation arising out of or in connection with it, is + governed exclusively by the laws of the state in which you are located + without reference to conflict of laws. Furthermore, the Parties agree that + the Uniform Computer Information Transactions Act or any version thereof, + adopted by any state in any form ('UCITA'), shall not apply to this + Agreement and, to the extent that UCITA is applicable, the Parties agree to + opt out of the applicability of UCITA pursuant to the opt-out provision(s) + contained therein." + + 3. ELASTIC LICENSE MODIFICATION. Section 5 of the Agreement is hereby +amended to replace + + "This Agreement may be modified by Elastic from time to time, and any + such modifications will be effective upon the "Posted Date" set forth at + the top of the modified Agreement." + + with: + + "This Agreement may be modified by Elastic from time to time; provided, + however, that any such modifications shall apply only to Elastic Software + that is installed after the "Posted Date" set forth at the top of the + modified Agreement." + +V100820.0 diff --git a/src/dev/build/tasks/os_packages/docker_generator/run.ts b/src/dev/build/tasks/os_packages/docker_generator/run.ts index 18c04b0428afa..21d2582f205f3 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/run.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/run.ts @@ -12,6 +12,7 @@ import { promisify } from 'util'; import { ToolingLog } from '@kbn/dev-utils'; +import { branch } from '../../../../../../package.json'; import { write, copyAll, mkdirp, exec, Config, Build } from '../../../lib'; import * as dockerTemplates from './templates'; import { TemplateContext } from './template_context'; @@ -30,21 +31,26 @@ export async function runDockerGenerator( architecture?: string; context: boolean; image: boolean; - ubi: boolean; + ubi?: boolean; + ironbank?: boolean; } ) { // UBI var config const baseOSImage = flags.ubi ? 'docker.elastic.co/ubi8/ubi-minimal:latest' : 'centos:8'; const ubiVersionTag = 'ubi8'; - const ubiImageFlavor = flags.ubi ? `-${ubiVersionTag}` : ''; + + let imageFlavor = ''; + if (flags.ubi) imageFlavor += `-${ubiVersionTag}`; + if (flags.ironbank) imageFlavor += '-ironbank'; + if (build.isOss()) imageFlavor += '-oss'; // General docker var config const license = build.isOss() ? 'ASL 2.0' : 'Elastic License'; - const imageFlavor = build.isOss() ? '-oss' : ''; const imageTag = 'docker.elastic.co/kibana/kibana'; const version = config.getBuildVersion(); const artifactArchitecture = flags.architecture === 'aarch64' ? 'aarch64' : 'x86_64'; - const artifactPrefix = `kibana${imageFlavor}-${version}-linux`; + const artifactFlavor = build.isOss() ? '-oss' : ''; + const artifactPrefix = `kibana${artifactFlavor}-${version}-linux`; const artifactTarball = `${artifactPrefix}-${artifactArchitecture}.tar.gz`; const artifactsDir = config.resolveFromTarget('.'); const dockerBuildDate = new Date().toISOString(); @@ -52,26 +58,27 @@ export async function runDockerGenerator( const dockerBuildDir = config.resolveFromRepo( 'build', 'kibana-docker', - build.isOss() ? `oss` : `default${ubiImageFlavor}` + build.isOss() ? `oss` : `default${imageFlavor}` ); const imageArchitecture = flags.architecture === 'aarch64' ? '-aarch64' : ''; const dockerTargetFilename = config.resolveFromTarget( - `kibana${imageFlavor}${ubiImageFlavor}-${version}-docker-image${imageArchitecture}.tar.gz` + `kibana${imageFlavor}-${version}-docker-image${imageArchitecture}.tar.gz` ); const scope: TemplateContext = { artifactPrefix, artifactTarball, imageFlavor, version, + branch, license, artifactsDir, imageTag, dockerBuildDir, dockerTargetFilename, baseOSImage, - ubiImageFlavor, dockerBuildDate, ubi: flags.ubi, + ironbank: flags.ironbank, architecture: flags.architecture, revision: config.getBuildSha(), }; @@ -107,10 +114,17 @@ export async function runDockerGenerator( // in order to build the docker image accordingly the dockerfile defined // under templates/kibana_yml.template/js await copyAll( - config.resolveFromRepo('src/dev/build/tasks/os_packages/docker_generator/resources'), + config.resolveFromRepo('src/dev/build/tasks/os_packages/docker_generator/resources/base'), dockerBuildDir ); + if (flags.ironbank) { + await copyAll( + config.resolveFromRepo('src/dev/build/tasks/os_packages/docker_generator/resources/ironbank'), + dockerBuildDir + ); + } + // Build docker image into the target folder // In order to do this we just call the file we // created from the templates/build_docker_sh.template.js diff --git a/src/dev/build/tasks/os_packages/docker_generator/template_context.ts b/src/dev/build/tasks/os_packages/docker_generator/template_context.ts index 845d0449437ba..9c9949c9f57ea 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/template_context.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/template_context.ts @@ -9,6 +9,7 @@ export interface TemplateContext { artifactPrefix: string; artifactTarball: string; + branch: string; imageFlavor: string; version: string; license: string; @@ -17,10 +18,10 @@ export interface TemplateContext { dockerBuildDir: string; dockerTargetFilename: string; baseOSImage: string; - ubiImageFlavor: string; dockerBuildDate: string; usePublicArtifact?: boolean; - ubi: boolean; + ubi?: boolean; + ironbank?: boolean; revision: string; architecture?: string; } diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/Dockerfile b/src/dev/build/tasks/os_packages/docker_generator/templates/base/Dockerfile similarity index 100% rename from src/dev/build/tasks/os_packages/docker_generator/templates/Dockerfile rename to src/dev/build/tasks/os_packages/docker_generator/templates/base/Dockerfile diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/build_docker_sh.template.ts b/src/dev/build/tasks/os_packages/docker_generator/templates/build_docker_sh.template.ts index 89e6cc1040a02..05b9b4d100c53 100644 --- a/src/dev/build/tasks/os_packages/docker_generator/templates/build_docker_sh.template.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/build_docker_sh.template.ts @@ -16,7 +16,6 @@ function generator({ version, dockerTargetFilename, baseOSImage, - ubiImageFlavor, architecture, }: TemplateContext) { return dedent(` @@ -54,10 +53,10 @@ function generator({ retry_docker_pull ${baseOSImage} - echo "Building: kibana${imageFlavor}${ubiImageFlavor}-docker"; \\ - docker build -t ${imageTag}${imageFlavor}${ubiImageFlavor}:${version} -f Dockerfile . || exit 1; + echo "Building: kibana${imageFlavor}-docker"; \\ + docker build -t ${imageTag}${imageFlavor}:${version} -f Dockerfile . || exit 1; - docker save ${imageTag}${imageFlavor}${ubiImageFlavor}:${version} | gzip -c > ${dockerTargetFilename} + docker save ${imageTag}${imageFlavor}:${version} | gzip -c > ${dockerTargetFilename} exit 0 `); diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/dockerfile.template.ts b/src/dev/build/tasks/os_packages/docker_generator/templates/dockerfile.template.ts index 01a45a4809431..e668299a3acc3 100755 --- a/src/dev/build/tasks/os_packages/docker_generator/templates/dockerfile.template.ts +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/dockerfile.template.ts @@ -13,10 +13,10 @@ import Mustache from 'mustache'; import { TemplateContext } from '../template_context'; function generator(options: TemplateContext) { - const template = readFileSync(resolve(__dirname, './Dockerfile')); + const dir = options.ironbank ? 'ironbank' : 'base'; + const template = readFileSync(resolve(__dirname, dir, './Dockerfile')); return Mustache.render(template.toString(), { - packageManager: options.ubiImageFlavor ? 'microdnf' : 'yum', - tiniBin: options.architecture === 'aarch64' ? 'tini-arm64' : 'tini-amd64', + packageManager: options.ubi ? 'microdnf' : 'yum', ...options, }); } diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile new file mode 100644 index 0000000000000..6893883bf16a4 --- /dev/null +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/Dockerfile @@ -0,0 +1,77 @@ +################################################################################ +# Build stage 0 +# Extract Kibana and make various file manipulations. +################################################################################ +ARG BASE_REGISTRY=registry1.dsop.io +ARG BASE_IMAGE=redhat/ubi/ubi8 +ARG BASE_TAG=8.3 + +FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG} as prep_files + +RUN yum update --setopt=tsflags=nodocs -y && \ + yum install -y tar gzip && \ + yum clean all + +RUN mkdir /usr/share/kibana +WORKDIR /usr/share/kibana +COPY --chown=1000:0 {{artifactTarball}} . +RUN tar --strip-components=1 -zxf {{artifactTarball}} + +# Ensure that group permissions are the same as user permissions. +# This will help when relying on GID-0 to run Kibana, rather than UID-1000. +# OpenShift does this, for example. +# REF: https://docs.openshift.org/latest/creating_images/guidelines.html +RUN chmod -R g=u /usr/share/kibana + + +################################################################################ +# Build stage 1 +# Copy prepared files from the previous stage and complete the image. +################################################################################ +FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG} +EXPOSE 5601 + +RUN yum update --setopt=tsflags=nodocs -y && \ + yum install -y fontconfig freetype shadow-utils nss && \ + yum clean all + +COPY LICENSE /licenses/elastic-kibana + +# Add a dumb init process +COPY tini /bin/tini +RUN chmod +x /bin/tini + +# Noto Fonts +RUN mkdir /usr/share/fonts/local +COPY NotoSansCJK-Regular.ttc /usr/share/fonts/local/NotoSansCJK-Regular.ttc +RUN fc-cache -v + +# Bring in Kibana from the initial stage. +COPY --from=prep_files --chown=1000:0 /usr/share/kibana /usr/share/kibana +WORKDIR /usr/share/kibana +RUN ln -s /usr/share/kibana /opt/kibana + +ENV ELASTIC_CONTAINER true +ENV PATH=/usr/share/kibana/bin:$PATH + +# Set some Kibana configuration defaults. +COPY --chown=1000:0 config/kibana.yml /usr/share/kibana/config/kibana.yml + +# Add the launcher/wrapper script. It knows how to interpret environment +# variables and translate them to Kibana CLI options. +COPY --chown=1000:0 scripts/kibana-docker /usr/local/bin/ + +# Remove the suid bit everywhere to mitigate "Stack Clash" +RUN find / -xdev -perm -4000 -exec chmod u-s {} + + +# Provide a non-root user to run the process. +RUN groupadd --gid 1000 kibana && \ + useradd --uid 1000 --gid 1000 -G 0 \ + --home-dir /usr/share/kibana --no-create-home \ + kibana + +ENTRYPOINT ["/bin/tini", "--"] + +CMD ["/usr/local/bin/kibana-docker"] + +HEALTHCHECK --interval=10s --timeout=5s --start-period=1m --retries=5 CMD curl -I -f --max-time 5 http://localhost:5601 || exit 1 diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/README.md b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/README.md new file mode 100644 index 0000000000000..d297d135149f4 --- /dev/null +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/README.md @@ -0,0 +1,39 @@ +# Kibana + +**Kibana** lets you visualize your Elasticsearch data and navigate the Elastic Stack, +so you can do anything from learning why you're getting paged at 2:00 a.m. to +understanding the impact rain might have on your quarterly numbers. + +For more information about Kibana, please visit +https://www.elastic.co/products/kibana. + +### Installation instructions + +Please follow the documentation on [running Kibana on Docker](https://www.elastic.co/guide/en/kibana/{{branch}}/docker.html). + +### Where to file issues and PRs + +- [Issues](https://github.com/elastic/kibana/issues) +- [PRs](https://github.com/elastic/kibana/pulls) + +### DoD Restrictions + +Due to the [NODE-SECURITY-1184](https://www.npmjs.com/advisories/1184) issue, Kibana users should not use the `ALL_PROXY` environment variable to specify a proxy when installing Kibana plugins with the kibana-plugin command line application. + +### Where to get help + +- [Kibana Discuss Forums](https://discuss.elastic.co/c/kibana) +- [Kibana Documentation](https://www.elastic.co/guide/en/kibana/current/index.html) + +### Still need help? + +You can learn more about the Elastic Community and also understand how to get more help +visiting [Elastic Community](https://www.elastic.co/community). + +This software is governed by the [Elastic +License](https://github.com/elastic/elasticsearch/blob/{{branch}}/licenses/ELASTIC-LICENSE.txt), +and includes the full set of [free +features](https://www.elastic.co/subscriptions). + +View the detailed release notes +[here](https://www.elastic.co/guide/en/elasticsearch/reference/{{branch}}/es-release-notes.html). diff --git a/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yml b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yml new file mode 100644 index 0000000000000..8de5ac2973358 --- /dev/null +++ b/src/dev/build/tasks/os_packages/docker_generator/templates/ironbank/hardening_manifest.yml @@ -0,0 +1,58 @@ +--- +apiVersion: v1 + +# The repository name in registry1, excluding /ironbank/ +name: 'elastic/kibana/kibana' + +# List of tags to push for the repository in registry1 +# The most specific version should be the first tag and will be shown +# on ironbank.dsop.io +tags: + - '{{version}}' + - 'latest' + +# Build args passed to Dockerfile ARGs +args: + BASE_IMAGE: 'redhat/ubi/ubi8' + BASE_TAG: '8.3' + +# Docker image labels +labels: + org.opencontainers.image.title: 'kibana' + org.opencontainers.image.description: 'Your window into the Elastic Stack.' + org.opencontainers.image.licenses: 'Elastic License' + org.opencontainers.image.url: 'https://www.elastic.co/products/kibana' + org.opencontainers.image.vendor: 'Elastic' + org.opencontainers.image.version: '{{version}}' + # mil.dso.ironbank.image.keywords: "" + # mil.dso.ironbank.image.type: "commercial" + mil.dso.ironbank.product.name: 'Kibana' + +# List of resources to make available to the offline build context +resources: + - filename: kibana-{{version}}-linux-x86_64.tar.gz + url: /kibana-{{version}}-linux-x86_64.tar.gz + validation: + type: sha512 + value: aa68f850cc09cf5dcb7c0b48bb8df788ca58eaad38d96141b8e59917fd38b42c728c0968f7cb2c8132c5aaeb595525cdde0859554346c496f53c569e03abe412 + - filename: tini + url: https://github.com/krallin/tini/releases/download/v0.19.0/tini-amd64 + validation: + type: sha512 + value: 8053cc21a3a9bdd6042a495349d1856ae8d3b3e7664c9654198de0087af031f5d41139ec85a2f5d7d2febd22ec3f280767ff23b9d5f63d490584e2b7ad3c218c + - filename: NotoSansCJK-Regular.ttc + url: https://github.com/googlefonts/noto-cjk/raw/NotoSansV2.001/NotoSansCJK-Regular.ttc + validation: + type: sha512 + value: 0ce56bde1853fed3e53282505bac65707385275a27816c29712ab04c187aa249797c82c58759b2b36c210d4e2683eda92359d739a8045cb8385c2c34d37cc9e1 + +# List of project maintainers +maintainers: + - email: 'tyler.smalley@elastic.co' + name: 'Tyler Smalley' + username: 'tylersmalley' + cht_member: false + - email: 'klepal_alexander@bah.com' + name: 'Alexander Klepal' + username: 'alexander.klepal' + cht_member: true diff --git a/src/dev/precommit_hook/casing_check_config.js b/src/dev/precommit_hook/casing_check_config.js index baed8284347c8..144da018c300c 100644 --- a/src/dev/precommit_hook/casing_check_config.js +++ b/src/dev/precommit_hook/casing_check_config.js @@ -123,7 +123,6 @@ export const REMOVE_EXTENSION = ['packages/kbn-plugin-generator/template/**/*.ej * @type {Array} */ export const TEMPORARILY_IGNORED_PATHS = [ - 'src/fixtures/config_upgrade_from_4.0.0_to_4.0.1-snapshot.json', 'src/core/server/core_app/assets/favicons/android-chrome-192x192.png', 'src/core/server/core_app/assets/favicons/android-chrome-256x256.png', 'src/core/server/core_app/assets/favicons/android-chrome-512x512.png', diff --git a/src/fixtures/agg_resp/date_histogram.js b/src/fixtures/agg_resp/date_histogram.js deleted file mode 100644 index 29b34f1ce69d0..0000000000000 --- a/src/fixtures/agg_resp/date_histogram.js +++ /dev/null @@ -1,258 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -export default { - took: 35, - timed_out: false, - _shards: { - total: 2, - successful: 2, - failed: 0, - }, - hits: { - total: 32899, - max_score: 0, - hits: [], - }, - aggregations: { - 1: { - buckets: [ - { - key_as_string: '2015-01-30T01:00:00.000Z', - key: 1422579600000, - doc_count: 18, - }, - { - key_as_string: '2015-01-30T02:00:00.000Z', - key: 1422583200000, - doc_count: 68, - }, - { - key_as_string: '2015-01-30T03:00:00.000Z', - key: 1422586800000, - doc_count: 146, - }, - { - key_as_string: '2015-01-30T04:00:00.000Z', - key: 1422590400000, - doc_count: 149, - }, - { - key_as_string: '2015-01-30T05:00:00.000Z', - key: 1422594000000, - doc_count: 363, - }, - { - key_as_string: '2015-01-30T06:00:00.000Z', - key: 1422597600000, - doc_count: 555, - }, - { - key_as_string: '2015-01-30T07:00:00.000Z', - key: 1422601200000, - doc_count: 878, - }, - { - key_as_string: '2015-01-30T08:00:00.000Z', - key: 1422604800000, - doc_count: 1133, - }, - { - key_as_string: '2015-01-30T09:00:00.000Z', - key: 1422608400000, - doc_count: 1438, - }, - { - key_as_string: '2015-01-30T10:00:00.000Z', - key: 1422612000000, - doc_count: 1719, - }, - { - key_as_string: '2015-01-30T11:00:00.000Z', - key: 1422615600000, - doc_count: 1813, - }, - { - key_as_string: '2015-01-30T12:00:00.000Z', - key: 1422619200000, - doc_count: 1790, - }, - { - key_as_string: '2015-01-30T13:00:00.000Z', - key: 1422622800000, - doc_count: 1582, - }, - { - key_as_string: '2015-01-30T14:00:00.000Z', - key: 1422626400000, - doc_count: 1439, - }, - { - key_as_string: '2015-01-30T15:00:00.000Z', - key: 1422630000000, - doc_count: 1154, - }, - { - key_as_string: '2015-01-30T16:00:00.000Z', - key: 1422633600000, - doc_count: 847, - }, - { - key_as_string: '2015-01-30T17:00:00.000Z', - key: 1422637200000, - doc_count: 588, - }, - { - key_as_string: '2015-01-30T18:00:00.000Z', - key: 1422640800000, - doc_count: 374, - }, - { - key_as_string: '2015-01-30T19:00:00.000Z', - key: 1422644400000, - doc_count: 152, - }, - { - key_as_string: '2015-01-30T20:00:00.000Z', - key: 1422648000000, - doc_count: 140, - }, - { - key_as_string: '2015-01-30T21:00:00.000Z', - key: 1422651600000, - doc_count: 73, - }, - { - key_as_string: '2015-01-30T22:00:00.000Z', - key: 1422655200000, - doc_count: 28, - }, - { - key_as_string: '2015-01-30T23:00:00.000Z', - key: 1422658800000, - doc_count: 9, - }, - { - key_as_string: '2015-01-31T00:00:00.000Z', - key: 1422662400000, - doc_count: 29, - }, - { - key_as_string: '2015-01-31T01:00:00.000Z', - key: 1422666000000, - doc_count: 38, - }, - { - key_as_string: '2015-01-31T02:00:00.000Z', - key: 1422669600000, - doc_count: 70, - }, - { - key_as_string: '2015-01-31T03:00:00.000Z', - key: 1422673200000, - doc_count: 136, - }, - { - key_as_string: '2015-01-31T04:00:00.000Z', - key: 1422676800000, - doc_count: 173, - }, - { - key_as_string: '2015-01-31T05:00:00.000Z', - key: 1422680400000, - doc_count: 370, - }, - { - key_as_string: '2015-01-31T06:00:00.000Z', - key: 1422684000000, - doc_count: 545, - }, - { - key_as_string: '2015-01-31T07:00:00.000Z', - key: 1422687600000, - doc_count: 845, - }, - { - key_as_string: '2015-01-31T08:00:00.000Z', - key: 1422691200000, - doc_count: 1070, - }, - { - key_as_string: '2015-01-31T09:00:00.000Z', - key: 1422694800000, - doc_count: 1419, - }, - { - key_as_string: '2015-01-31T10:00:00.000Z', - key: 1422698400000, - doc_count: 1725, - }, - { - key_as_string: '2015-01-31T11:00:00.000Z', - key: 1422702000000, - doc_count: 1801, - }, - { - key_as_string: '2015-01-31T12:00:00.000Z', - key: 1422705600000, - doc_count: 1823, - }, - { - key_as_string: '2015-01-31T13:00:00.000Z', - key: 1422709200000, - doc_count: 1657, - }, - { - key_as_string: '2015-01-31T14:00:00.000Z', - key: 1422712800000, - doc_count: 1454, - }, - { - key_as_string: '2015-01-31T15:00:00.000Z', - key: 1422716400000, - doc_count: 1131, - }, - { - key_as_string: '2015-01-31T16:00:00.000Z', - key: 1422720000000, - doc_count: 810, - }, - { - key_as_string: '2015-01-31T17:00:00.000Z', - key: 1422723600000, - doc_count: 583, - }, - { - key_as_string: '2015-01-31T18:00:00.000Z', - key: 1422727200000, - doc_count: 384, - }, - { - key_as_string: '2015-01-31T19:00:00.000Z', - key: 1422730800000, - doc_count: 165, - }, - { - key_as_string: '2015-01-31T20:00:00.000Z', - key: 1422734400000, - doc_count: 135, - }, - { - key_as_string: '2015-01-31T21:00:00.000Z', - key: 1422738000000, - doc_count: 72, - }, - { - key_as_string: '2015-01-31T22:00:00.000Z', - key: 1422741600000, - doc_count: 8, - }, - ], - }, - }, -}; diff --git a/src/fixtures/agg_resp/geohash_grid.js b/src/fixtures/agg_resp/geohash_grid.js deleted file mode 100644 index 4a8fb3704c9b3..0000000000000 --- a/src/fixtures/agg_resp/geohash_grid.js +++ /dev/null @@ -1,84 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import _ from 'lodash'; -export default function GeoHashGridAggResponseFixture() { - // for vis: - // - // vis = new Vis(indexPattern, { - // type: 'tile_map', - // aggs:[ - // { schema: 'metric', type: 'avg', params: { field: 'bytes' } }, - // { schema: 'split', type: 'terms', params: { field: '@tags', size: 10 } }, - // { schema: 'segment', type: 'geohash_grid', params: { field: 'geo.coordinates', precision: 3 } } - // ], - // params: { - // isDesaturated: true, - // mapType: 'Scaled%20Circle%20Markers' - // }, - // }); - - const geoHashCharts = _.union( - _.range(48, 57), // 0-9 - _.range(65, 90), // A-Z - _.range(97, 122) // a-z - ); - - const tags = _.times(_.random(4, 20), function (i) { - // random number of tags - let docCount = 0; - const buckets = _.times(_.random(40, 200), function () { - return _.sampleSize(geoHashCharts, 3).join(''); - }) - .sort() - .map(function (geoHash) { - const count = _.random(1, 5000); - - docCount += count; - - return { - key: geoHash, - doc_count: count, - 1: { - value: 2048 + i, - }, - }; - }); - - return { - key: 'tag ' + (i + 1), - doc_count: docCount, - 3: { - buckets: buckets, - }, - 1: { - value: 1000 + i, - }, - }; - }); - - return { - took: 3, - timed_out: false, - _shards: { - total: 4, - successful: 4, - failed: 0, - }, - hits: { - total: 298, - max_score: 0.0, - hits: [], - }, - aggregations: { - 2: { - buckets: tags, - }, - }, - }; -} diff --git a/src/fixtures/agg_resp/range.js b/src/fixtures/agg_resp/range.js deleted file mode 100644 index ca15f535add82..0000000000000 --- a/src/fixtures/agg_resp/range.js +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -export default { - took: 35, - timed_out: false, - _shards: { - total: 7, - successful: 7, - failed: 0, - }, - hits: { - total: 218512, - max_score: 0, - hits: [], - }, - aggregations: { - 1: { - buckets: { - '*-1024.0': { - to: 1024, - to_as_string: '1024.0', - doc_count: 20904, - }, - '1024.0-2560.0': { - from: 1024, - from_as_string: '1024.0', - to: 2560, - to_as_string: '2560.0', - doc_count: 23358, - }, - '2560.0-*': { - from: 2560, - from_as_string: '2560.0', - doc_count: 174250, - }, - }, - }, - }, -}; diff --git a/src/fixtures/config_upgrade_from_4.0.0.json b/src/fixtures/config_upgrade_from_4.0.0.json deleted file mode 100644 index 522de78648c9b..0000000000000 --- a/src/fixtures/config_upgrade_from_4.0.0.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "took": 1, - "timed_out": false, - "_shards": { - "total": 1, - "successful": 1, - "failed": 0 - }, - "hits": { - "total": 1, - "max_score": 1, - "hits": [ - { - "_index": ".kibana", - "_type": "config", - "_id": "4.0.0", - "_score": 1, - "_source": { - "buildNum": 5888, - "defaultIndex": "logstash-*" - } - } - ] - } -} diff --git a/src/fixtures/config_upgrade_from_4.0.0_to_4.0.1-snapshot.json b/src/fixtures/config_upgrade_from_4.0.0_to_4.0.1-snapshot.json deleted file mode 100644 index 8767232dcdc1c..0000000000000 --- a/src/fixtures/config_upgrade_from_4.0.0_to_4.0.1-snapshot.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "took": 1, - "timed_out": false, - "_shards": { - "total": 1, - "successful": 1, - "failed": 0 - }, - "hits": { - "total": 2, - "max_score": 1, - "hits": [ - { - "_index": ".kibana", - "_type": "config", - "_id": "4.0.1-SNAPSHOT", - "_score": 1, - "_source": { - "buildNum": 5921, - "defaultIndex": "logstash-*" - } - }, - { - "_index": ".kibana", - "_type": "config", - "_id": "4.0.0", - "_score": 1, - "_source": { - "buildNum": 5888, - "defaultIndex": "logstash-*" - } - } - ] - } -} diff --git a/src/fixtures/config_upgrade_from_4.0.0_to_4.0.1.json b/src/fixtures/config_upgrade_from_4.0.0_to_4.0.1.json deleted file mode 100644 index 57b486491b397..0000000000000 --- a/src/fixtures/config_upgrade_from_4.0.0_to_4.0.1.json +++ /dev/null @@ -1,35 +0,0 @@ -{ - "took": 1, - "timed_out": false, - "_shards": { - "total": 1, - "successful": 1, - "failed": 0 - }, - "hits": { - "total": 2, - "max_score": 1, - "hits": [ - { - "_index": ".kibana", - "_type": "config", - "_id": "4.0.1", - "_score": 1, - "_source": { - "buildNum": 5921, - "defaultIndex": "logstash-*" - } - }, - { - "_index": ".kibana", - "_type": "config", - "_id": "4.0.0", - "_score": 1, - "_source": { - "buildNum": 5888, - "defaultIndex": "logstash-*" - } - } - ] - } -} diff --git a/src/fixtures/fake_chart_events.js b/src/fixtures/fake_chart_events.js deleted file mode 100644 index 71f49cb4713b8..0000000000000 --- a/src/fixtures/fake_chart_events.js +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -const results = {}; - -results.timeSeries = { - data: { - ordered: { - date: true, - interval: 600000, - max: 1414437217559, - min: 1414394017559, - }, - }, - label: 'apache', - value: 44, - point: { - label: 'apache', - x: 1414400400000, - y: 44, - y0: 0, - }, -}; diff --git a/src/fixtures/fake_hierarchical_data.ts b/src/fixtures/fake_hierarchical_data.ts deleted file mode 100644 index 2e23acfc3a803..0000000000000 --- a/src/fixtures/fake_hierarchical_data.ts +++ /dev/null @@ -1,621 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -export const metricOnly = { - hits: { total: 1000, hits: [], max_score: 0 }, - aggregations: { - agg_1: { value: 412032 }, - }, -}; - -export const threeTermBuckets = { - hits: { total: 1000, hits: [], max_score: 0 }, - aggregations: { - agg_2: { - buckets: [ - { - key: 'png', - doc_count: 50, - agg_1: { value: 412032 }, - agg_3: { - buckets: [ - { - key: 'IT', - doc_count: 10, - agg_1: { value: 9299 }, - agg_4: { - buckets: [ - { key: 'win', doc_count: 4, agg_1: { value: 0 } }, - { key: 'mac', doc_count: 6, agg_1: { value: 9299 } }, - ], - }, - }, - { - key: 'US', - doc_count: 20, - agg_1: { value: 8293 }, - agg_4: { - buckets: [ - { key: 'linux', doc_count: 12, agg_1: { value: 3992 } }, - { key: 'mac', doc_count: 8, agg_1: { value: 3029 } }, - ], - }, - }, - ], - }, - }, - { - key: 'css', - doc_count: 20, - agg_1: { value: 412032 }, - agg_3: { - buckets: [ - { - key: 'MX', - doc_count: 7, - agg_1: { value: 9299 }, - agg_4: { - buckets: [ - { key: 'win', doc_count: 3, agg_1: { value: 4992 } }, - { key: 'mac', doc_count: 4, agg_1: { value: 5892 } }, - ], - }, - }, - { - key: 'US', - doc_count: 13, - agg_1: { value: 8293 }, - agg_4: { - buckets: [ - { key: 'linux', doc_count: 12, agg_1: { value: 3992 } }, - { key: 'mac', doc_count: 1, agg_1: { value: 3029 } }, - ], - }, - }, - ], - }, - }, - { - key: 'html', - doc_count: 90, - agg_1: { value: 412032 }, - agg_3: { - buckets: [ - { - key: 'CN', - doc_count: 85, - agg_1: { value: 9299 }, - agg_4: { - buckets: [ - { key: 'win', doc_count: 46, agg_1: { value: 4992 } }, - { key: 'mac', doc_count: 39, agg_1: { value: 5892 } }, - ], - }, - }, - { - key: 'FR', - doc_count: 15, - agg_1: { value: 8293 }, - agg_4: { - buckets: [ - { key: 'win', doc_count: 3, agg_1: { value: 3992 } }, - { key: 'mac', doc_count: 12, agg_1: { value: 3029 } }, - ], - }, - }, - ], - }, - }, - ], - }, - }, -}; - -export const oneTermOneHistogramBucketWithTwoMetricsOneTopHitOneDerivative = { - hits: { total: 1000, hits: [], max_score: 0 }, - aggregations: { - agg_3: { - buckets: [ - { - key: 'png', - doc_count: 50, - agg_4: { - buckets: [ - { - key_as_string: '2014-09-28T00:00:00.000Z', - key: 1411862400000, - doc_count: 1, - agg_1: { value: 9283 }, - agg_2: { value: 1411862400000 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 23, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-09-29T00:00:00.000Z', - key: 1411948800000, - doc_count: 2, - agg_1: { value: 28349 }, - agg_2: { value: 1411948800000 }, - agg_5: { value: 203 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 39, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-09-30T00:00:00.000Z', - key: 1412035200000, - doc_count: 3, - agg_1: { value: 84330 }, - agg_2: { value: 1412035200000 }, - agg_5: { value: 200 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 329, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-01T00:00:00.000Z', - key: 1412121600000, - doc_count: 4, - agg_1: { value: 34992 }, - agg_2: { value: 1412121600000 }, - agg_5: { value: 103 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 22, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-02T00:00:00.000Z', - key: 1412208000000, - doc_count: 5, - agg_1: { value: 145432 }, - agg_2: { value: 1412208000000 }, - agg_5: { value: 153 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 93, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-03T00:00:00.000Z', - key: 1412294400000, - doc_count: 35, - agg_1: { value: 220943 }, - agg_2: { value: 1412294400000 }, - agg_5: { value: 239 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 72, - }, - }, - ], - }, - }, - }, - ], - }, - }, - { - key: 'css', - doc_count: 20, - agg_4: { - buckets: [ - { - key_as_string: '2014-09-28T00:00:00.000Z', - key: 1411862400000, - doc_count: 1, - agg_1: { value: 9283 }, - agg_2: { value: 1411862400000 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 75, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-09-29T00:00:00.000Z', - key: 1411948800000, - doc_count: 2, - agg_1: { value: 28349 }, - agg_2: { value: 1411948800000 }, - agg_5: { value: 10 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 11, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-09-30T00:00:00.000Z', - key: 1412035200000, - doc_count: 3, - agg_1: { value: 84330 }, - agg_2: { value: 1412035200000 }, - agg_5: { value: 24 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 238, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-01T00:00:00.000Z', - key: 1412121600000, - doc_count: 4, - agg_1: { value: 34992 }, - agg_2: { value: 1412121600000 }, - agg_5: { value: 49 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 343, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-02T00:00:00.000Z', - key: 1412208000000, - doc_count: 5, - agg_1: { value: 145432 }, - agg_2: { value: 1412208000000 }, - agg_5: { value: 100 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 837, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-03T00:00:00.000Z', - key: 1412294400000, - doc_count: 5, - agg_1: { value: 220943 }, - agg_2: { value: 1412294400000 }, - agg_5: { value: 23 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 302, - }, - }, - ], - }, - }, - }, - ], - }, - }, - { - key: 'html', - doc_count: 90, - agg_4: { - buckets: [ - { - key_as_string: '2014-09-28T00:00:00.000Z', - key: 1411862400000, - doc_count: 10, - agg_1: { value: 9283 }, - agg_2: { value: 1411862400000 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 30, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-09-29T00:00:00.000Z', - key: 1411948800000, - doc_count: 20, - agg_1: { value: 28349 }, - agg_2: { value: 1411948800000 }, - agg_5: { value: 1 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 43, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-09-30T00:00:00.000Z', - key: 1412035200000, - doc_count: 30, - agg_1: { value: 84330 }, - agg_2: { value: 1412035200000 }, - agg_5: { value: 5 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 88, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-01T00:00:00.000Z', - key: 1412121600000, - doc_count: 11, - agg_1: { value: 34992 }, - agg_2: { value: 1412121600000 }, - agg_5: { value: 10 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 91, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-02T00:00:00.000Z', - key: 1412208000000, - doc_count: 12, - agg_1: { value: 145432 }, - agg_2: { value: 1412208000000 }, - agg_5: { value: 43 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 534, - }, - }, - ], - }, - }, - }, - { - key_as_string: '2014-10-03T00:00:00.000Z', - key: 1412294400000, - doc_count: 7, - agg_1: { value: 220943 }, - agg_2: { value: 1412294400000 }, - agg_5: { value: 1 }, - agg_6: { - hits: { - total: 2, - hits: [ - { - fields: { - bytes: 553, - }, - }, - ], - }, - }, - }, - ], - }, - }, - ], - }, - }, -}; - -export const oneRangeBucket = { - took: 35, - timed_out: false, - _shards: { - total: 1, - successful: 1, - failed: 0, - }, - hits: { - total: 6039, - max_score: 0, - hits: [], - }, - aggregations: { - agg_2: { - buckets: { - '0.0-1000.0': { - from: 0, - from_as_string: '0.0', - to: 1000, - to_as_string: '1000.0', - doc_count: 606, - }, - '1000.0-2000.0': { - from: 1000, - from_as_string: '1000.0', - to: 2000, - to_as_string: '2000.0', - doc_count: 298, - }, - }, - }, - }, -}; - -export const oneFilterBucket = { - took: 11, - timed_out: false, - _shards: { - total: 1, - successful: 1, - failed: 0, - }, - hits: { - total: 6005, - max_score: 0, - hits: [], - }, - aggregations: { - agg_2: { - buckets: { - 'type:apache': { - doc_count: 4844, - }, - 'type:nginx': { - doc_count: 1161, - }, - }, - }, - }, -}; - -export const oneHistogramBucket = { - took: 37, - timed_out: false, - _shards: { - total: 6, - successful: 6, - failed: 0, - }, - hits: { - total: 49208, - max_score: 0, - hits: [], - }, - aggregations: { - agg_2: { - buckets: [ - { - key_as_string: '2014-09-28T00:00:00.000Z', - key: 1411862400000, - doc_count: 8247, - }, - { - key_as_string: '2014-09-29T00:00:00.000Z', - key: 1411948800000, - doc_count: 8184, - }, - { - key_as_string: '2014-09-30T00:00:00.000Z', - key: 1412035200000, - doc_count: 8269, - }, - { - key_as_string: '2014-10-01T00:00:00.000Z', - key: 1412121600000, - doc_count: 8141, - }, - { - key_as_string: '2014-10-02T00:00:00.000Z', - key: 1412208000000, - doc_count: 8148, - }, - { - key_as_string: '2014-10-03T00:00:00.000Z', - key: 1412294400000, - doc_count: 8219, - }, - ], - }, - }, -}; diff --git a/src/fixtures/field_mapping.js b/src/fixtures/field_mapping.js deleted file mode 100644 index 5077e361d5458..0000000000000 --- a/src/fixtures/field_mapping.js +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -export default { - test: { - mappings: { - testType: { - baz: { - full_name: 'baz', - mapping: { - bar: { - type: 'long', - }, - }, - }, - 'foo.bar': { - full_name: 'foo.bar', - mapping: { - bar: { - type: 'string', - }, - }, - }, - not_analyzed_field: { - full_name: 'not_analyzed_field', - mapping: { - bar: { - type: 'string', - index: 'not_analyzed', - }, - }, - }, - index_no_field: { - full_name: 'index_no_field', - mapping: { - bar: { - type: 'string', - index: 'no', - }, - }, - }, - _id: { - full_name: '_id', - mapping: { - _id: { - store: false, - index: 'no', - }, - }, - }, - _timestamp: { - full_name: '_timestamp', - mapping: { - _timestamp: { - store: true, - index: 'no', - }, - }, - }, - }, - }, - }, -}; diff --git a/src/fixtures/hits.js b/src/fixtures/hits.js deleted file mode 100644 index 6cbb080031cf2..0000000000000 --- a/src/fixtures/hits.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -export default function fitsFixture() { - return [ - // extension - // | machine.os - //timestamp | | bytes - //| ssl ip | | | request - [0, true, '192.168.0.1', 'php', 'Linux', 10, 'foo'], - [1, true, '192.168.0.1', 'php', 'Linux', 20, 'bar'], - [2, true, '192.168.0.1', 'php', 'Linux', 30, 'bar'], - [3, true, '192.168.0.1', 'php', 'Linux', 30, 'baz'], - [4, true, '192.168.0.1', 'php', 'Linux', 30, 'baz'], - [5, true, '192.168.0.1', 'php', 'Linux', 40.141592, 'bat'], - [6, true, '192.168.0.1', 'php', 'Linux', 40.141592, 'bat'], - [7, true, '192.168.0.1', 'php', 'Linux', 40.141592, 'bat'], - [8, true, '192.168.0.1', 'php', 'Linux', 40.141592, 'bat'], - [9, true, '192.168.0.1', 'php', 'Linux', 40.141592, 'bat'], - ].map((row, i) => { - return { - _score: 1, - _id: 1000 + i, - _type: 'test', - _index: 'test-index', - _source: { - '@timestamp': row[0], - ssl: row[1], - ip: row[2], - extension: row[3], - 'machine.os': row[4], - bytes: row[5], - request: row[6], - }, - }; - }); -} diff --git a/src/fixtures/mapping_with_dupes.js b/src/fixtures/mapping_with_dupes.js deleted file mode 100644 index 7f6da2600c9a8..0000000000000 --- a/src/fixtures/mapping_with_dupes.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -export default { - test: { - mappings: { - testType: { - baz: { - full_name: 'baz', - mapping: { - bar: { - type: 'long', - }, - }, - }, - 'foo.bar': { - full_name: 'foo.bar', - mapping: { - bar: { - type: 'string', - }, - }, - }, - }, - }, - }, - duplicates: { - mappings: { - testType: { - baz: { - full_name: 'baz', - mapping: { - bar: { - type: 'date', - }, - }, - }, - }, - }, - }, -}; diff --git a/src/fixtures/mock_index_patterns.js b/src/fixtures/mock_index_patterns.js deleted file mode 100644 index ce44b71613b01..0000000000000 --- a/src/fixtures/mock_index_patterns.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import sinon from 'sinon'; -import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; - -export default function (Private) { - const indexPatterns = Private(FixturesStubbedLogstashIndexPatternProvider); - const getIndexPatternStub = sinon.stub().resolves(indexPatterns); - - return { - get: getIndexPatternStub, - }; -} diff --git a/src/fixtures/mock_ui_state.js b/src/fixtures/mock_ui_state.js deleted file mode 100644 index fc0a18137a5fd..0000000000000 --- a/src/fixtures/mock_ui_state.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { set } from '@elastic/safer-lodash-set'; -import _ from 'lodash'; -let values = {}; -export default { - get: function (path, def) { - return _.get(values, path, def); - }, - set: function (path, val) { - set(values, path, val); - return val; - }, - setSilent: function (path, val) { - set(values, path, val); - return val; - }, - emit: _.noop, - on: _.noop, - off: _.noop, - clearAllKeys: function () { - values = {}; - }, - _reset: function () { - values = {}; - }, -}; diff --git a/src/fixtures/stubbed_search_source.js b/src/fixtures/stubbed_search_source.js deleted file mode 100644 index ea41e7bbe681c..0000000000000 --- a/src/fixtures/stubbed_search_source.js +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import sinon from 'sinon'; -import searchResponse from 'fixtures/search_response'; -import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern'; - -export default function stubSearchSource(Private, $q, Promise) { - let deferedResult = $q.defer(); - const indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider); - - let onResultsCount = 0; - return { - setField: sinon.spy(), - fetch: sinon.spy(), - destroy: sinon.spy(), - getField: function (param) { - switch (param) { - case 'index': - return indexPattern; - default: - throw new Error(`Param "${param}" is not implemented in the stubbed search source`); - } - }, - crankResults: function () { - deferedResult.resolve(searchResponse); - deferedResult = $q.defer(); - }, - onResults: function () { - onResultsCount++; - - // Up to the test to resolve this manually - // For example: - // someHandler.resolve(require('fixtures/search_response')) - return deferedResult.promise; - }, - getOnResultsCount: function () { - return onResultsCount; - }, - _flatten: function () { - return Promise.resolve({ index: indexPattern, body: {} }); - }, - _requestStartHandlers: [], - onRequestStart(fn) { - this._requestStartHandlers.push(fn); - }, - requestIsStopped() {}, - }; -} diff --git a/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx b/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx index 0be582a4c0294..c7a8c0a6135c7 100644 --- a/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx +++ b/src/plugins/advanced_settings/public/management_app/advanced_settings.tsx @@ -12,7 +12,7 @@ import { UnregisterCallback } from 'history'; import { parse } from 'query-string'; import { UiCounterMetricType } from '@kbn/analytics'; -import { Comparators, EuiFlexGroup, EuiFlexItem, EuiSpacer, Query } from '@elastic/eui'; +import { EuiFlexGroup, EuiFlexItem, EuiSpacer, Query } from '@elastic/eui'; import { IUiSettingsClient, @@ -28,7 +28,7 @@ import { Form } from './components/form'; import { AdvancedSettingsVoiceAnnouncement } from './components/advanced_settings_voice_announcement'; import { ComponentRegistry } from '../'; -import { getAriaName, toEditableConfig, DEFAULT_CATEGORY } from './lib'; +import { getAriaName, toEditableConfig, fieldSorter, DEFAULT_CATEGORY } from './lib'; import { FieldSetting, SettingsChanges } from './types'; import { parseErrorMsg } from './components/search/search'; @@ -185,17 +185,17 @@ export class AdvancedSettings extends Component { + .map(([settingId, settingDef]) => { return toEditableConfig({ - def: setting[1], - name: setting[0], - value: setting[1].userValue, - isCustom: config.isCustom(setting[0]), - isOverridden: config.isOverridden(setting[0]), + def: settingDef, + name: settingId, + value: settingDef.userValue, + isCustom: config.isCustom(settingId), + isOverridden: config.isOverridden(settingId), }); }) - .filter((c) => !c.readonly) - .sort(Comparators.property('name', Comparators.default('asc'))); + .filter((c) => !c.readOnly) + .sort(fieldSorter); } mapSettings(settings: FieldSetting[]) { diff --git a/src/plugins/advanced_settings/public/management_app/components/field/__snapshots__/field.test.tsx.snap b/src/plugins/advanced_settings/public/management_app/components/field/__snapshots__/field.test.tsx.snap index 19bf9e6d73757..517a6238c2519 100644 --- a/src/plugins/advanced_settings/public/management_app/components/field/__snapshots__/field.test.tsx.snap +++ b/src/plugins/advanced_settings/public/management_app/components/field/__snapshots__/field.test.tsx.snap @@ -866,6 +866,419 @@ exports[`Field for boolean setting should render user value if there is user val `; +exports[`Field for color setting should render as read only if saving is disabled 1`] = ` + +
+ + } + fullWidth={true} + id="color:test:setting-group" + title={ +

+ + Color test setting + + + +

+ } +> + + + + +`; + +exports[`Field for color setting should render as read only with help text if overridden 1`] = ` + +
+ + + + + + null + , + } + } + /> + + + + + } + fullWidth={true} + id="color:test:setting-group" + title={ +

+ + Color test setting + + + +

+ } +> + + + + } + label="color:test:setting" + labelType="label" + > + + + +`; + +exports[`Field for color setting should render custom setting icon if it is custom 1`] = ` + +
+ + } + fullWidth={true} + id="color:test:setting-group" + title={ +

+ + Color test setting + + + } + type="asterisk" + /> + +

+ } +> + + + + +`; + +exports[`Field for color setting should render default value if there is no user value set 1`] = ` + +
+ + } + fullWidth={true} + id="color:test:setting-group" + title={ +

+ + Color test setting + + + +

+ } +> + + + + +`; + +exports[`Field for color setting should render unsaved value if there are unsaved changes 1`] = ` + +
+ + } + fullWidth={true} + id="color:test:setting-group" + title={ +

+ + Color test setting + + + } + type="asterisk" + /> + +

+ } +> + + + +

+ Setting is currently not saved. +

+
+
+ +`; + +exports[`Field for color setting should render user value if there is user value is set 1`] = ` + +
+ + + + + + null + , + } + } + /> + + + + + } + fullWidth={true} + id="color:test:setting-group" + title={ +

+ + Color test setting + + + +

+ } +> + + + + + +     + + + } + label="color:test:setting" + labelType="label" + > + + + +`; + exports[`Field for image setting should render as read only if saving is disabled 1`] = ` = { isOverridden: false, ...defaults, }, + color: { + name: 'color:test:setting', + ariaName: 'color test setting', + displayName: 'Color test setting', + description: 'Description for Color test setting', + type: 'color', + value: undefined, + defVal: null, + isCustom: false, + isOverridden: false, + ...defaults, + }, }; const userValues = { array: ['user', 'value'], @@ -174,6 +187,7 @@ const userValues = { select: 'banana', string: 'foo', stringWithValidation: 'fooUserValue', + color: '#FACF0C', }; const invalidUserValues = { @@ -187,6 +201,8 @@ const getFieldSettingValue = (wrapper: ReactWrapper, name: string, type: string) const field = findTestSubject(wrapper, `advancedSetting-editField-${name}`); if (type === 'boolean') { return field.props()['aria-checked']; + } else if (type === 'color') { + return field.props().color; } else { return field.props().value; } @@ -423,6 +439,36 @@ describe('Field', () => { }); } }); + } else if (type === 'color') { + describe(`for changing ${type} setting`, () => { + const { wrapper, component } = setup(); + const userValue = userValues[type]; + + it('should be able to change value', async () => { + await (component.instance() as Field).onFieldChange(userValue); + const updated = wrapper.update(); + expect(handleChange).toBeCalledWith(setting.name, { value: userValue }); + updated.setProps({ unsavedChanges: { value: userValue } }); + const currentValue = wrapper.find('EuiColorPicker').prop('color'); + expect(currentValue).toEqual(userValue); + }); + + it('should be able to reset to default value', async () => { + await wrapper.setProps({ + unsavedChanges: {}, + setting: { ...setting, value: userValue }, + }); + const updated = wrapper.update(); + findTestSubject(updated, `advancedSetting-resetField-${setting.name}`).simulate('click'); + const expectedEditableValue = getEditableValue(setting.type, setting.defVal); + expect(handleChange).toBeCalledWith(setting.name, { + value: expectedEditableValue, + }); + updated.setProps({ unsavedChanges: { value: expectedEditableValue } }); + const currentValue = wrapper.find('EuiColorPicker').prop('color'); + expect(currentValue).toEqual(expectedEditableValue); + }); + }); } else { describe(`for changing ${type} setting`, () => { const { wrapper, component } = setup(); diff --git a/src/plugins/advanced_settings/public/management_app/components/field/field.tsx b/src/plugins/advanced_settings/public/management_app/components/field/field.tsx index 5569a6e11872a..f5db5c3e371b3 100644 --- a/src/plugins/advanced_settings/public/management_app/components/field/field.tsx +++ b/src/plugins/advanced_settings/public/management_app/components/field/field.tsx @@ -17,6 +17,7 @@ import { EuiBadge, EuiCode, EuiCodeBlock, + EuiColorPicker, EuiScreenReaderOnly, EuiCodeEditor, EuiDescribedFormGroup, @@ -392,6 +393,17 @@ export class Field extends PureComponent { data-test-subj={`advancedSetting-editField-${name}`} /> ); + case 'color': + return ( + + ); default: return ( ): FieldSetting => ({ + displayName: 'displayName', + name: 'field', + value: 'value', + requiresPageReload: false, + type: 'string', + category: [], + ariaName: 'ariaName', + isOverridden: false, + defVal: 'defVal', + isCustom: false, + ...parts, +}); + +describe('fieldSorter', () => { + it('sort fields based on their `order` field if present on both', () => { + const fieldA = createField({ order: 3 }); + const fieldB = createField({ order: 1 }); + const fieldC = createField({ order: 2 }); + + expect([fieldA, fieldB, fieldC].sort(fieldSorter)).toEqual([fieldB, fieldC, fieldA]); + }); + it('fields with order defined are ordered first', () => { + const fieldA = createField({ order: 2 }); + const fieldB = createField({ order: undefined }); + const fieldC = createField({ order: 1 }); + + expect([fieldA, fieldB, fieldC].sort(fieldSorter)).toEqual([fieldC, fieldA, fieldB]); + }); + it('sorts by `name` when fields have the same `order`', () => { + const fieldA = createField({ order: 2, name: 'B' }); + const fieldB = createField({ order: 1 }); + const fieldC = createField({ order: 2, name: 'A' }); + + expect([fieldA, fieldB, fieldC].sort(fieldSorter)).toEqual([fieldB, fieldC, fieldA]); + }); + + it('sorts by `name` when fields have no `order`', () => { + const fieldA = createField({ order: undefined, name: 'B' }); + const fieldB = createField({ order: undefined, name: 'A' }); + const fieldC = createField({ order: 1 }); + + expect([fieldA, fieldB, fieldC].sort(fieldSorter)).toEqual([fieldC, fieldB, fieldA]); + }); +}); diff --git a/src/plugins/advanced_settings/public/management_app/lib/sort_fields.ts b/src/plugins/advanced_settings/public/management_app/lib/sort_fields.ts new file mode 100644 index 0000000000000..90bfa18d2198e --- /dev/null +++ b/src/plugins/advanced_settings/public/management_app/lib/sort_fields.ts @@ -0,0 +1,31 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { Comparators } from '@elastic/eui'; +import { FieldSetting } from '../types'; + +const cmp = Comparators.default('asc'); + +export const fieldSorter = (a: FieldSetting, b: FieldSetting): number => { + const aOrder = a.order !== undefined; + const bOrder = b.order !== undefined; + + if (aOrder && bOrder) { + if (a.order === b.order) { + return cmp(a.name, b.name); + } + return cmp(a.order, b.order); + } + if (aOrder) { + return -1; + } + if (bOrder) { + return 1; + } + return cmp(a.name, b.name); +}; diff --git a/src/plugins/advanced_settings/public/management_app/lib/to_editable_config.ts b/src/plugins/advanced_settings/public/management_app/lib/to_editable_config.ts index b2b7f1c1016cd..49abe3b279a28 100644 --- a/src/plugins/advanced_settings/public/management_app/lib/to_editable_config.ts +++ b/src/plugins/advanced_settings/public/management_app/lib/to_editable_config.ts @@ -12,6 +12,7 @@ import { StringValidationRegexString, SavedObjectAttribute, } from 'src/core/public'; +import { FieldSetting } from '../types'; import { getValType } from './get_val_type'; import { getAriaName } from './get_aria_name'; import { DEFAULT_CATEGORY } from './default_category'; @@ -41,7 +42,7 @@ export function toEditableConfig({ const validationTyped = def.validation as StringValidationRegexString; - const conf = { + const conf: FieldSetting = { name, displayName: def.name || name, ariaName: def.name || getAriaName(name), @@ -49,7 +50,7 @@ export function toEditableConfig({ category: def.category && def.category.length ? def.category : [DEFAULT_CATEGORY], isCustom, isOverridden, - readonly: !!def.readonly, + readOnly: !!def.readonly, defVal: def.value, type: getValType(def, value), description: def.description, @@ -63,6 +64,7 @@ export function toEditableConfig({ : def.validation, options: def.options, optionLabels: def.optionLabels, + order: def.order, requiresPageReload: !!def.requiresPageReload, metric: def.metric, }; diff --git a/src/plugins/advanced_settings/public/management_app/types.ts b/src/plugins/advanced_settings/public/management_app/types.ts index 0563fa310bc77..50b39114d2143 100644 --- a/src/plugins/advanced_settings/public/management_app/types.ts +++ b/src/plugins/advanced_settings/public/management_app/types.ts @@ -25,6 +25,7 @@ export interface FieldSetting { isCustom: boolean; validation?: StringValidation | ImageValidation; readOnly?: boolean; + order?: number; deprecation?: { message: string; docLinksKey: string; diff --git a/src/plugins/dashboard/kibana.json b/src/plugins/dashboard/kibana.json index e074d529917d2..8286a4badcbe5 100644 --- a/src/plugins/dashboard/kibana.json +++ b/src/plugins/dashboard/kibana.json @@ -10,7 +10,8 @@ "savedObjects", "share", "uiActions", - "urlForwarding" + "urlForwarding", + "presentationUtil" ], "optionalPlugins": [ "home", diff --git a/src/plugins/dashboard/public/application/actions/add_to_library_action.tsx b/src/plugins/dashboard/public/application/actions/add_to_library_action.tsx index 5d384ed8ebd82..ef730e16bc5cf 100644 --- a/src/plugins/dashboard/public/application/actions/add_to_library_action.tsx +++ b/src/plugins/dashboard/public/application/actions/add_to_library_action.tsx @@ -22,7 +22,7 @@ import { NotificationsStart } from '../../services/core'; import { dashboardAddToLibraryAction } from '../../dashboard_strings'; import { DashboardPanelState, DASHBOARD_CONTAINER_TYPE, DashboardContainer } from '..'; -export const ACTION_ADD_TO_LIBRARY = 'addToFromLibrary'; +export const ACTION_ADD_TO_LIBRARY = 'saveToLibrary'; export interface AddToLibraryActionContext { embeddable: IEmbeddable; diff --git a/src/plugins/dashboard/public/application/actions/copy_to_dashboard_action.tsx b/src/plugins/dashboard/public/application/actions/copy_to_dashboard_action.tsx new file mode 100644 index 0000000000000..f16486dd65e3c --- /dev/null +++ b/src/plugins/dashboard/public/application/actions/copy_to_dashboard_action.tsx @@ -0,0 +1,90 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import React from 'react'; +import { OverlayStart } from '../../../../../core/public'; +import { dashboardCopyToDashboardAction } from '../../dashboard_strings'; +import { EmbeddableStateTransfer, IEmbeddable } from '../../services/embeddable'; +import { toMountPoint } from '../../services/kibana_react'; +import { PresentationUtilPluginStart } from '../../services/presentation_util'; +import { Action, IncompatibleActionError } from '../../services/ui_actions'; +import { DASHBOARD_CONTAINER_TYPE, DashboardContainer } from '../embeddable'; +import { CopyToDashboardModal } from './copy_to_dashboard_modal'; + +export const ACTION_COPY_TO_DASHBOARD = 'copyToDashboard'; + +export interface CopyToDashboardActionContext { + embeddable: IEmbeddable; +} + +export interface DashboardCopyToCapabilities { + canCreateNew: boolean; + canEditExisting: boolean; +} + +function isDashboard(embeddable: IEmbeddable): embeddable is DashboardContainer { + return embeddable.type === DASHBOARD_CONTAINER_TYPE; +} + +export class CopyToDashboardAction implements Action { + public readonly type = ACTION_COPY_TO_DASHBOARD; + public readonly id = ACTION_COPY_TO_DASHBOARD; + public order = 1; + + constructor( + private overlays: OverlayStart, + private stateTransfer: EmbeddableStateTransfer, + private capabilities: DashboardCopyToCapabilities, + private PresentationUtilContext: PresentationUtilPluginStart['ContextProvider'] + ) {} + + public getDisplayName({ embeddable }: CopyToDashboardActionContext) { + if (!embeddable.parent || !isDashboard(embeddable.parent)) { + throw new IncompatibleActionError(); + } + + return dashboardCopyToDashboardAction.getDisplayName(); + } + + public getIconType({ embeddable }: CopyToDashboardActionContext) { + if (!embeddable.parent || !isDashboard(embeddable.parent)) { + throw new IncompatibleActionError(); + } + return 'exit'; + } + + public async isCompatible({ embeddable }: CopyToDashboardActionContext) { + return Boolean( + embeddable.parent && + isDashboard(embeddable.parent) && + (this.capabilities.canCreateNew || this.capabilities.canEditExisting) + ); + } + + public async execute({ embeddable }: CopyToDashboardActionContext) { + if (!embeddable.parent || !isDashboard(embeddable.parent)) { + throw new IncompatibleActionError(); + } + const session = this.overlays.openModal( + toMountPoint( + session.close()} + stateTransfer={this.stateTransfer} + capabilities={this.capabilities} + dashboardId={(embeddable.parent as DashboardContainer).getInput().id} + embeddable={embeddable} + /> + ), + { + maxWidth: 400, + 'data-test-subj': 'copyToDashboardPanel', + } + ); + } +} diff --git a/src/plugins/dashboard/public/application/actions/copy_to_dashboard_modal.tsx b/src/plugins/dashboard/public/application/actions/copy_to_dashboard_modal.tsx new file mode 100644 index 0000000000000..b16c0f5d34663 --- /dev/null +++ b/src/plugins/dashboard/public/application/actions/copy_to_dashboard_modal.tsx @@ -0,0 +1,139 @@ +/* + * 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 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ +import React, { useCallback, useState } from 'react'; +import { omit } from 'lodash'; +import { + EuiButton, + EuiButtonEmpty, + EuiFormRow, + EuiModalBody, + EuiModalFooter, + EuiModalHeader, + EuiModalHeaderTitle, + EuiPanel, + EuiRadio, + EuiSpacer, + EuiText, +} from '@elastic/eui'; +import { DashboardCopyToCapabilities } from './copy_to_dashboard_action'; +import { DashboardPicker } from '../../services/presentation_util'; +import { dashboardCopyToDashboardAction } from '../../dashboard_strings'; +import { EmbeddableStateTransfer, IEmbeddable } from '../../services/embeddable'; +import { createDashboardEditUrl, DashboardConstants } from '../..'; + +interface CopyToDashboardModalProps { + capabilities: DashboardCopyToCapabilities; + stateTransfer: EmbeddableStateTransfer; + PresentationUtilContext: React.FC; + embeddable: IEmbeddable; + dashboardId?: string; + closeModal: () => void; +} + +export function CopyToDashboardModal({ + PresentationUtilContext, + stateTransfer, + capabilities, + dashboardId, + embeddable, + closeModal, +}: CopyToDashboardModalProps) { + const [dashboardOption, setDashboardOption] = useState<'new' | 'existing'>('existing'); + const [selectedDashboard, setSelectedDashboard] = useState<{ id: string; name: string } | null>( + null + ); + + const onSubmit = useCallback(() => { + const state = { + input: omit(embeddable.getInput(), 'id'), + type: embeddable.type, + }; + + const path = + dashboardOption === 'existing' && selectedDashboard + ? `#${createDashboardEditUrl(selectedDashboard.id, true)}` + : `#${DashboardConstants.CREATE_NEW_DASHBOARD_URL}`; + + closeModal(); + stateTransfer.navigateToWithEmbeddablePackage('dashboards', { + state, + path, + }); + }, [dashboardOption, embeddable, selectedDashboard, stateTransfer, closeModal]); + + return ( + + + {dashboardCopyToDashboardAction.getDisplayName()} + + + + <> + +

{dashboardCopyToDashboardAction.getDescription()}

+
+ + + +
+ {capabilities.canEditExisting && ( + <> + setDashboardOption('existing')} + /> +
+ setSelectedDashboard(dashboard)} + /> +
+ + + )} + {capabilities.canCreateNew && ( + <> + setDashboardOption('new')} + /> + + + )} +
+
+
+ +
+ + + closeModal()}> + {dashboardCopyToDashboardAction.getCancelButtonName()} + + + {dashboardCopyToDashboardAction.getAcceptButtonName()} + + +
+ ); +} diff --git a/src/plugins/dashboard/public/application/actions/index.ts b/src/plugins/dashboard/public/application/actions/index.ts index ce858d0bb7970..827ae5bcb4419 100644 --- a/src/plugins/dashboard/public/application/actions/index.ts +++ b/src/plugins/dashboard/public/application/actions/index.ts @@ -31,6 +31,11 @@ export { UnlinkFromLibraryActionContext, ACTION_UNLINK_FROM_LIBRARY, } from './unlink_from_library_action'; +export { + CopyToDashboardAction, + CopyToDashboardActionContext, + ACTION_COPY_TO_DASHBOARD, +} from './copy_to_dashboard_action'; export { LibraryNotificationActionContext, LibraryNotificationAction, diff --git a/src/plugins/dashboard/public/application/listing/confirm_overlays.tsx b/src/plugins/dashboard/public/application/listing/confirm_overlays.tsx index 4e17fa1f62c14..41b27b4fd6926 100644 --- a/src/plugins/dashboard/public/application/listing/confirm_overlays.tsx +++ b/src/plugins/dashboard/public/application/listing/confirm_overlays.tsx @@ -9,7 +9,6 @@ import { EuiButton, EuiButtonEmpty, - EuiModal, EuiModalBody, EuiModalFooter, EuiModalHeader, @@ -48,7 +47,7 @@ export const confirmCreateWithUnsaved = ( ) => { const session = overlays.openModal( toMountPoint( - session.close()}> + <> {createConfirmStrings.getCreateTitle()} @@ -85,7 +84,7 @@ export const confirmCreateWithUnsaved = ( {createConfirmStrings.getContinueButtonText()} - + ), { 'data-test-subj': 'dashboardCreateConfirmModal', diff --git a/src/plugins/dashboard/public/application/top_nav/dashboard_top_nav.tsx b/src/plugins/dashboard/public/application/top_nav/dashboard_top_nav.tsx index 0caaac6764bbe..786afc81c400c 100644 --- a/src/plugins/dashboard/public/application/top_nav/dashboard_top_nav.tsx +++ b/src/plugins/dashboard/public/application/top_nav/dashboard_top_nav.tsx @@ -321,6 +321,33 @@ export function DashboardTopNav({ dashboardStateManager, ]); + const runQuickSave = useCallback(async () => { + const currentTitle = dashboardStateManager.getTitle(); + const currentDescription = dashboardStateManager.getDescription(); + const currentTimeRestore = dashboardStateManager.getTimeRestore(); + + let currentTags: string[] = []; + if (savedObjectsTagging) { + const dashboard = dashboardStateManager.savedDashboard; + if (savedObjectsTagging.ui.hasTagDecoration(dashboard)) { + currentTags = dashboard.getTags(); + } + } + + save({}).then((response: SaveResult) => { + // If the save wasn't successful, put the original values back. + if (!(response as { id: string }).id) { + dashboardStateManager.setTitle(currentTitle); + dashboardStateManager.setDescription(currentDescription); + dashboardStateManager.setTimeRestore(currentTimeRestore); + if (savedObjectsTagging) { + dashboardStateManager.setTags(currentTags); + } + } + return response; + }); + }, [save, savedObjectsTagging, dashboardStateManager]); + const runClone = useCallback(() => { const currentTitle = dashboardStateManager.getTitle(); const onClone = async ( @@ -356,9 +383,8 @@ export function DashboardTopNav({ [TopNavIds.ENTER_EDIT_MODE]: () => onChangeViewMode(ViewMode.EDIT), [TopNavIds.DISCARD_CHANGES]: onDiscardChanges, [TopNavIds.SAVE]: runSave, + [TopNavIds.QUICK_SAVE]: runQuickSave, [TopNavIds.CLONE]: runClone, - [TopNavIds.ADD_EXISTING]: addFromLibrary, - [TopNavIds.VISUALIZE]: createNew, [TopNavIds.OPTIONS]: (anchorElement) => { showOptionsPopover({ anchorElement, @@ -394,10 +420,9 @@ export function DashboardTopNav({ onDiscardChanges, onChangeViewMode, savedDashboard, - addFromLibrary, - createNew, runClone, runSave, + runQuickSave, share, ]); @@ -419,11 +444,11 @@ export function DashboardTopNav({ const showFilterBar = shouldShowFilterBar(Boolean(embedSettings?.forceHideFilterBar)); const showSearchBar = showQueryBar || showFilterBar; - const topNav = getTopNavConfig( - viewMode, - dashboardTopNavActions, - dashboardCapabilities.hideWriteControls - ); + const topNav = getTopNavConfig(viewMode, dashboardTopNavActions, { + hideWriteControls: dashboardCapabilities.hideWriteControls, + isNewDashboard: !savedDashboard.id, + isDirty: dashboardStateManager.isDirty, + }); return { appName: 'dashboard', diff --git a/src/plugins/dashboard/public/application/top_nav/get_top_nav_config.ts b/src/plugins/dashboard/public/application/top_nav/get_top_nav_config.ts index 37414cb948d5a..abc128369017c 100644 --- a/src/plugins/dashboard/public/application/top_nav/get_top_nav_config.ts +++ b/src/plugins/dashboard/public/application/top_nav/get_top_nav_config.ts @@ -20,11 +20,11 @@ import { NavAction } from '../../types'; export function getTopNavConfig( dashboardMode: ViewMode, actions: { [key: string]: NavAction }, - hideWriteControls: boolean + options: { hideWriteControls: boolean; isNewDashboard: boolean; isDirty: boolean } ) { switch (dashboardMode) { case ViewMode.VIEW: - return hideWriteControls + return options.hideWriteControls ? [ getFullScreenConfig(actions[TopNavIds.FULL_SCREEN]), getShareConfig(actions[TopNavIds.SHARE]), @@ -36,20 +36,39 @@ export function getTopNavConfig( getEditConfig(actions[TopNavIds.ENTER_EDIT_MODE]), ]; case ViewMode.EDIT: - return [ - getOptionsConfig(actions[TopNavIds.OPTIONS]), - getShareConfig(actions[TopNavIds.SHARE]), - getAddConfig(actions[TopNavIds.ADD_EXISTING]), - getViewConfig(actions[TopNavIds.EXIT_EDIT_MODE]), - getDiscardConfig(actions[TopNavIds.DISCARD_CHANGES]), - getSaveConfig(actions[TopNavIds.SAVE]), - getCreateNewConfig(actions[TopNavIds.VISUALIZE]), - ]; + return options.isNewDashboard + ? [ + getOptionsConfig(actions[TopNavIds.OPTIONS]), + getShareConfig(actions[TopNavIds.SHARE]), + getViewConfig(actions[TopNavIds.EXIT_EDIT_MODE]), + getDiscardConfig(actions[TopNavIds.DISCARD_CHANGES]), + getSaveConfig(actions[TopNavIds.SAVE], options.isNewDashboard), + ] + : [ + getOptionsConfig(actions[TopNavIds.OPTIONS]), + getShareConfig(actions[TopNavIds.SHARE]), + getViewConfig(actions[TopNavIds.EXIT_EDIT_MODE]), + getDiscardConfig(actions[TopNavIds.DISCARD_CHANGES]), + getSaveConfig(actions[TopNavIds.SAVE]), + getQuickSave(actions[TopNavIds.QUICK_SAVE]), + ]; default: return []; } } +function getSaveButtonLabel() { + return i18n.translate('dashboard.topNave.saveButtonAriaLabel', { + defaultMessage: 'save', + }); +} + +function getSaveAsButtonLabel() { + return i18n.translate('dashboard.topNave.saveAsButtonAriaLabel', { + defaultMessage: 'save as', + }); +} + function getFullScreenConfig(action: NavAction) { return { id: 'full-screen', @@ -89,17 +108,32 @@ function getEditConfig(action: NavAction) { /** * @returns {kbnTopNavConfig} */ -function getSaveConfig(action: NavAction) { +function getQuickSave(action: NavAction) { return { - id: 'save', - label: i18n.translate('dashboard.topNave.saveButtonAriaLabel', { - defaultMessage: 'save', - }), + id: 'quick-save', + emphasize: true, + label: getSaveButtonLabel(), description: i18n.translate('dashboard.topNave.saveConfigDescription', { - defaultMessage: 'Save your dashboard', + defaultMessage: 'Quick save your dashboard without any prompts', + }), + testId: 'dashboardQuickSaveMenuItem', + run: action, + }; +} + +/** + * @returns {kbnTopNavConfig} + */ +function getSaveConfig(action: NavAction, isNewDashboard = false) { + return { + id: 'save', + label: isNewDashboard ? getSaveButtonLabel() : getSaveAsButtonLabel(), + description: i18n.translate('dashboard.topNave.saveAsConfigDescription', { + defaultMessage: 'Save as a new dashboard', }), testId: 'dashboardSaveMenuItem', run: action, + emphasize: isNewDashboard, }; } @@ -157,42 +191,6 @@ function getCloneConfig(action: NavAction) { /** * @returns {kbnTopNavConfig} */ -function getAddConfig(action: NavAction) { - return { - id: 'add', - label: i18n.translate('dashboard.topNave.addButtonAriaLabel', { - defaultMessage: 'Library', - }), - description: i18n.translate('dashboard.topNave.addConfigDescription', { - defaultMessage: 'Add an existing visualization to the dashboard', - }), - testId: 'dashboardAddPanelButton', - run: action, - }; -} - -/** - * @returns {kbnTopNavConfig} - */ -function getCreateNewConfig(action: NavAction) { - return { - emphasize: true, - iconType: 'plusInCircleFilled', - id: 'addNew', - label: i18n.translate('dashboard.topNave.addNewButtonAriaLabel', { - defaultMessage: 'Create panel', - }), - description: i18n.translate('dashboard.topNave.addNewConfigDescription', { - defaultMessage: 'Create a new panel on this dashboard', - }), - testId: 'dashboardAddNewPanelButton', - run: action, - }; -} - -// /** -// * @returns {kbnTopNavConfig} -// */ function getShareConfig(action: NavAction | undefined) { return { id: 'share', diff --git a/src/plugins/dashboard/public/application/top_nav/panel_toolbar/__snapshots__/panel_toolbar.stories.storyshot b/src/plugins/dashboard/public/application/top_nav/panel_toolbar/__snapshots__/panel_toolbar.stories.storyshot index f822a7e70d523..afbbecb3935e0 100644 --- a/src/plugins/dashboard/public/application/top_nav/panel_toolbar/__snapshots__/panel_toolbar.stories.storyshot +++ b/src/plugins/dashboard/public/application/top_nav/panel_toolbar/__snapshots__/panel_toolbar.stories.storyshot @@ -10,7 +10,7 @@ exports[`Storyshots components/PanelToolbar default 1`] = ` > + +
+ +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; + +exports[`Details Panel Component DetailsPanel:EventDetails: rendering it should render the Event Details view in the Details Panel when the panelView is eventDetail and the eventId is set 1`] = `null`; + +exports[`Details Panel Component DetailsPanel:EventDetails: rendering it should render the Event Details view of the Details Panel in the flyout when the panelView is eventDetail and the eventId is set 1`] = ` +Array [ + .c1 { + -webkit-flex: 0; + -ms-flex: 0; + flex: 0; +} + +.c2 .euiFlyoutBody__overflow { + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; + overflow: hidden; +} + +.c2 .euiFlyoutBody__overflow .euiFlyoutBody__overflowContent { + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; + overflow: hidden; + padding: 4px 16px 64px; +} + +.c0 { + z-index: 7000; +} + + + + + +
+
+ + + + + + + +
+ + + +
+ +
+ +
+
+
+
+
+
+
+
+ + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+
+
+
+
+
, + .c1 { + -webkit-flex: 0; + -ms-flex: 0; + flex: 0; +} + +.c2 .euiFlyoutBody__overflow { + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; + overflow: hidden; +} + +.c2 .euiFlyoutBody__overflow .euiFlyoutBody__overflowContent { + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; + overflow: hidden; + padding: 4px 16px 64px; +} + +.c0 { + z-index: 7000; +} + + + + +
+
+ + + + + + + +
+ + + +
+ +
+ +
+
+
+
+
+
+
+
+ + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+
+
+
+
, + .c1 { + -webkit-flex: 0; + -ms-flex: 0; + flex: 0; +} + +.c2 .euiFlyoutBody__overflow { + display: -webkit-box; + display: -webkit-flex; + display: -ms-flexbox; + display: flex; + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; + overflow: hidden; +} + +.c2 .euiFlyoutBody__overflow .euiFlyoutBody__overflowContent { + -webkit-flex: 1; + -ms-flex: 1; + flex: 1; + overflow: hidden; + padding: 4px 16px 64px; +} + +.c0 { + z-index: 7000; +} + +
+ + + + + + + +
+ + + +
+ +
+ +
+
+
+
+
+
+
+
+ + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+
, +] +`; + +exports[`Details Panel Component DetailsPanel:HostDetails: rendering it should render the Host Details view in the Details Panel when the panelView is hostDetail and the hostName is set 1`] = `null`; + +exports[`Details Panel Component DetailsPanel:NetworkDetails: rendering it should render the Network Details view in the Details Panel when the panelView is networkDetail and the ip is set 1`] = `null`; diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/expandable_event/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/expandable_event.tsx similarity index 96% rename from x-pack/plugins/security_solution/public/timelines/components/timeline/expandable_event/index.tsx rename to x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/expandable_event.tsx index 159745c5a3f86..6e8238dfe4b25 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/expandable_event/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/expandable_event.tsx @@ -21,7 +21,7 @@ import { import React, { useMemo, useState } from 'react'; import styled from 'styled-components'; -import { TimelineExpandedEventType, TimelineTabs } from '../../../../../common/types/timeline'; +import { TimelineTabs } from '../../../../../common/types/timeline'; import { BrowserFields } from '../../../../common/containers/source'; import { EventDetails, @@ -36,7 +36,7 @@ export type HandleOnEventClosed = () => void; interface Props { browserFields: BrowserFields; detailsData: TimelineEventsDetailsItem[] | null; - event: TimelineExpandedEventType; + event: { eventId: string; indexName: string }; isAlert: boolean; loading: boolean; messageHeight?: number; diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/index.tsx new file mode 100644 index 0000000000000..d8b9e7121f60d --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/index.tsx @@ -0,0 +1,109 @@ +/* + * 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 { some } from 'lodash/fp'; +import { EuiFlyoutHeader, EuiFlyoutBody, EuiSpacer } from '@elastic/eui'; +import React from 'react'; +import styled from 'styled-components'; +import deepEqual from 'fast-deep-equal'; + +import { BrowserFields, DocValueFields } from '../../../../common/containers/source'; +import { ExpandableEvent, ExpandableEventTitle } from './expandable_event'; +import { useTimelineEventsDetails } from '../../../containers/details'; +import { TimelineTabs } from '../../../../../common/types/timeline'; + +const StyledEuiFlyoutBody = styled(EuiFlyoutBody)` + .euiFlyoutBody__overflow { + display: flex; + flex: 1; + overflow: hidden; + + .euiFlyoutBody__overflowContent { + flex: 1; + overflow: hidden; + padding: ${({ theme }) => `${theme.eui.paddingSizes.xs} ${theme.eui.paddingSizes.m} 64px`}; + } + } +`; + +interface EventDetailsPanelProps { + browserFields: BrowserFields; + docValueFields: DocValueFields[]; + expandedEvent: { eventId: string; indexName: string }; + handleOnEventClosed: () => void; + isFlyoutView?: boolean; + tabType: TimelineTabs; + timelineId: string; +} + +const EventDetailsPanelComponent: React.FC = ({ + browserFields, + docValueFields, + expandedEvent, + handleOnEventClosed, + isFlyoutView, + tabType, + timelineId, +}) => { + const [loading, detailsData] = useTimelineEventsDetails({ + docValueFields, + indexName: expandedEvent.indexName ?? '', + eventId: expandedEvent.eventId ?? '', + skip: !expandedEvent.eventId, + }); + + const isAlert = some({ category: 'signal', field: 'signal.rule.id' }, detailsData); + + if (!expandedEvent?.eventId) { + return null; + } + + return isFlyoutView ? ( + <> + + + + + + + + ) : ( + <> + + + + + ); +}; + +export const EventDetailsPanel = React.memo( + EventDetailsPanelComponent, + (prevProps, nextProps) => + deepEqual(prevProps.browserFields, nextProps.browserFields) && + deepEqual(prevProps.docValueFields, nextProps.docValueFields) && + prevProps.timelineId === nextProps.timelineId +); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/expandable_event/translations.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/translations.ts similarity index 77% rename from x-pack/plugins/security_solution/public/timelines/components/timeline/expandable_event/translations.tsx rename to x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/translations.ts index 234f3ac49e64d..2910e04747e39 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/expandable_event/translations.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/event_details/translations.ts @@ -14,13 +14,6 @@ export const MESSAGE = i18n.translate( } ); -export const COPY_TO_CLIPBOARD = i18n.translate( - 'xpack.securitySolution.timeline.expandableEvent.copyToClipboardToolTip', - { - defaultMessage: 'Copy to Clipboard', - } -); - export const CLOSE = i18n.translate( 'xpack.securitySolution.timeline.expandableEvent.closeEventDetailsLabel', { @@ -28,13 +21,6 @@ export const CLOSE = i18n.translate( } ); -export const EVENT = i18n.translate( - 'xpack.securitySolution.timeline.expandableEvent.eventToolTipTitle', - { - defaultMessage: 'Event', - } -); - export const EVENT_DETAILS_PLACEHOLDER = i18n.translate( 'xpack.securitySolution.timeline.expandableEvent.placeholder', { diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/host_details/expandable_host.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/host_details/expandable_host.tsx new file mode 100644 index 0000000000000..4e101e29bb484 --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/host_details/expandable_host.tsx @@ -0,0 +1,94 @@ +/* + * 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 React from 'react'; +import { i18n } from '@kbn/i18n'; +import { EuiTitle } from '@elastic/eui'; +import { HostDetailsLink } from '../../../../common/components/links'; +import { useGlobalTime } from '../../../../common/containers/use_global_time'; +import { useSourcererScope } from '../../../../common/containers/sourcerer'; +import { HostOverview } from '../../../../overview/components/host_overview'; +import { setAbsoluteRangeDatePicker } from '../../../../common/store/inputs/actions'; +import { HostItem } from '../../../../../common/search_strategy'; +import { AnomalyTableProvider } from '../../../../common/components/ml/anomaly/anomaly_table_provider'; +import { hostToCriteria } from '../../../../common/components/ml/criteria/host_to_criteria'; +import { scoreIntervalToDateTime } from '../../../../common/components/ml/score/score_interval_to_datetime'; +import { HostOverviewByNameQuery } from '../../../../hosts/containers/hosts/details'; + +interface ExpandableHostProps { + hostName: string; +} + +export const ExpandableHostDetailsTitle = ({ hostName }: ExpandableHostProps) => ( + +

+ {i18n.translate('xpack.securitySolution.timeline.sidePanel.hostDetails.title', { + defaultMessage: 'Host details', + })} + {`: ${hostName}`} +

+
+); + +export const ExpandableHostDetailsPageLink = ({ hostName }: ExpandableHostProps) => ( + + {i18n.translate('xpack.securitySolution.timeline.sidePanel.hostDetails.hostDetailsPageLink', { + defaultMessage: 'View details page', + })} + +); + +export const ExpandableHostDetails = ({ + contextID, + hostName, +}: ExpandableHostProps & { contextID: string }) => { + const { to, from, isInitializing } = useGlobalTime(); + const { docValueFields, selectedPatterns } = useSourcererScope(); + return ( + + {({ hostOverview, loading, id }) => ( + + {({ isLoadingAnomaliesData, anomaliesData }) => ( + { + const fromTo = scoreIntervalToDateTime(score, interval); + setAbsoluteRangeDatePicker({ + id: 'global', + from: fromTo.from, + to: fromTo.to, + }); + }} + /> + )} + + )} + + ); +}; diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/host_details/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/host_details/index.tsx new file mode 100644 index 0000000000000..39064cda16001 --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/host_details/index.tsx @@ -0,0 +1,116 @@ +/* + * 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. + */ + +/* eslint-disable react/display-name */ + +import { + EuiFlexGroup, + EuiFlyoutHeader, + EuiFlyoutBody, + EuiFlexItem, + EuiButtonIcon, + EuiSpacer, +} from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import React from 'react'; +import styled from 'styled-components'; +import { + ExpandableHostDetails, + ExpandableHostDetailsPageLink, + ExpandableHostDetailsTitle, +} from './expandable_host'; + +const StyledEuiFlyoutBody = styled(EuiFlyoutBody)` + .euiFlyoutBody__overflow { + display: flex; + flex: 1; + overflow-x: hidden; + overflow-y: scroll; + + .euiFlyoutBody__overflowContent { + flex: 1; + overflow-x: hidden; + overflow-y: scroll; + padding: ${({ theme }) => `${theme.eui.paddingSizes.xs} ${theme.eui.paddingSizes.m} 64px`}; + } + } +`; + +const StyledEuiFlexGroup = styled(EuiFlexGroup)` + flex: 0; +`; + +const StyledEuiFlexItem = styled(EuiFlexItem)` + &.euiFlexItem { + flex: 1 0 0; + overflow-y: scroll; + overflow-x: hidden; + } +`; + +const StyledEuiFlexButtonWrapper = styled(EuiFlexItem)` + align-self: flex-start; +`; + +interface HostDetailsProps { + contextID: string; + expandedHost: { hostName: string }; + handleOnHostClosed: () => void; + isFlyoutView?: boolean; +} + +export const HostDetailsPanel: React.FC = React.memo( + ({ contextID, expandedHost, handleOnHostClosed, isFlyoutView }) => { + const { hostName } = expandedHost; + + if (!hostName) { + return null; + } + + return isFlyoutView ? ( + <> + + + + + + + + + + + ) : ( + <> + + + + + + + + + + + + + + + + + + ); + } +); diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/index.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/index.test.tsx new file mode 100644 index 0000000000000..71ab7f01ddd54 --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/index.test.tsx @@ -0,0 +1,204 @@ +/* + * 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 { mount } from 'enzyme'; +import React from 'react'; + +import '../../../common/mock/match_media'; +import { + apolloClientObservable, + mockGlobalState, + TestProviders, + SUB_PLUGINS_REDUCER, + kibanaObservable, + createSecuritySolutionStorageMock, +} from '../../../common/mock'; +import { createStore, State } from '../../../common/store'; +import { DetailsPanel } from './index'; +import { TimelineExpandedDetail, TimelineTabs } from '../../../../common/types/timeline'; +import { FlowTarget } from '../../../../common/search_strategy/security_solution/network'; + +describe('Details Panel Component', () => { + const state: State = { ...mockGlobalState }; + + const { storage } = createSecuritySolutionStorageMock(); + let store = createStore( + state, + SUB_PLUGINS_REDUCER, + apolloClientObservable, + kibanaObservable, + storage + ); + + const dataLessExpandedDetail = { + [TimelineTabs.query]: { + panelView: 'hostDetail', + params: {}, + }, + }; + + const hostExpandedDetail: TimelineExpandedDetail = { + [TimelineTabs.query]: { + panelView: 'hostDetail', + params: { + hostName: 'woohoo!', + }, + }, + }; + + const networkExpandedDetail: TimelineExpandedDetail = { + [TimelineTabs.query]: { + panelView: 'networkDetail', + params: { + ip: 'woohoo!', + flowTarget: FlowTarget.source, + }, + }, + }; + + const eventExpandedDetail: TimelineExpandedDetail = { + [TimelineTabs.query]: { + panelView: 'eventDetail', + params: { + eventId: 'my-id', + indexName: 'my-index', + }, + }, + }; + + const mockProps = { + browserFields: {}, + docValueFields: [], + handleOnPanelClosed: jest.fn(), + isFlyoutView: false, + tabType: TimelineTabs.query, + timelineId: 'test', + }; + + describe('DetailsPanel: rendering', () => { + beforeEach(() => { + store = createStore( + state, + SUB_PLUGINS_REDUCER, + apolloClientObservable, + kibanaObservable, + storage + ); + }); + + test('it should not render the DetailsPanel if no expanded detail has been set in the reducer', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find('DetailsPanel')).toMatchSnapshot(); + }); + + test('it should not render the DetailsPanel if an expanded detail with a panelView, but not params have been set', () => { + state.timeline.timelineById.test.expandedDetail = dataLessExpandedDetail as TimelineExpandedDetail; // Casting as the dataless doesn't meet the actual type requirements + const wrapper = mount( + + + + ); + + expect(wrapper.find('DetailsPanel')).toMatchSnapshot(); + }); + }); + + describe('DetailsPanel:EventDetails: rendering', () => { + beforeEach(() => { + state.timeline.timelineById.test.expandedDetail = eventExpandedDetail; + store = createStore( + state, + SUB_PLUGINS_REDUCER, + apolloClientObservable, + kibanaObservable, + storage + ); + }); + + test('it should render the Details Panel when the panelView is set and the associated params are set', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find('DetailsPanel')).toMatchSnapshot(); + }); + + test('it should render the Event Details view of the Details Panel in the flyout when the panelView is eventDetail and the eventId is set', () => { + const currentProps = { ...mockProps, isFlyoutView: true }; + const wrapper = mount( + + + + ); + + expect(wrapper.find('[data-test-subj="timeline:details-panel:flyout"]')).toMatchSnapshot(); + }); + + test('it should render the Event Details view in the Details Panel when the panelView is eventDetail and the eventId is set', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find('EventDetails')).toMatchSnapshot(); + }); + }); + + describe('DetailsPanel:HostDetails: rendering', () => { + beforeEach(() => { + state.timeline.timelineById.test.expandedDetail = hostExpandedDetail; + store = createStore( + state, + SUB_PLUGINS_REDUCER, + apolloClientObservable, + kibanaObservable, + storage + ); + }); + + test('it should render the Host Details view in the Details Panel when the panelView is hostDetail and the hostName is set', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find('HostDetails')).toMatchSnapshot(); + }); + }); + + describe('DetailsPanel:NetworkDetails: rendering', () => { + beforeEach(() => { + state.timeline.timelineById.test.expandedDetail = networkExpandedDetail; + store = createStore( + state, + SUB_PLUGINS_REDUCER, + apolloClientObservable, + kibanaObservable, + storage + ); + }); + + test('it should render the Network Details view in the Details Panel when the panelView is networkDetail and the ip is set', () => { + const wrapper = mount( + + + + ); + + expect(wrapper.find('NetworkDetails')).toMatchSnapshot(); + }); + }); +}); diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/index.tsx new file mode 100644 index 0000000000000..0482491562f57 --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/index.tsx @@ -0,0 +1,120 @@ +/* + * 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 React, { useCallback, useMemo } from 'react'; +import { useDispatch } from 'react-redux'; +import { EuiFlyout } from '@elastic/eui'; +import styled from 'styled-components'; +import { timelineActions, timelineSelectors } from '../../store/timeline'; +import { timelineDefaults } from '../../store/timeline/defaults'; +import { BrowserFields, DocValueFields } from '../../../common/containers/source'; +import { TimelineTabs } from '../../../../common/types/timeline'; +import { useDeepEqualSelector } from '../../../common/hooks/use_selector'; +import { EventDetailsPanel } from './event_details'; +import { HostDetailsPanel } from './host_details'; +import { NetworkDetailsPanel } from './network_details'; + +const StyledEuiFlyout = styled(EuiFlyout)` + z-index: ${({ theme }) => theme.eui.euiZLevel7}; +`; + +interface DetailsPanelProps { + browserFields: BrowserFields; + docValueFields: DocValueFields[]; + handleOnPanelClosed?: () => void; + isFlyoutView?: boolean; + tabType?: TimelineTabs; + timelineId: string; +} + +/** + * This panel is used in both the main timeline as well as the flyouts on the host, detection, cases, and network pages. + * To prevent duplication the `isFlyoutView` prop is passed to determine the layout that should be used + * `tabType` defaults to query and `handleOnPanelClosed` defaults to unsetting the default query tab which is used for the flyout panel + */ +export const DetailsPanel = React.memo( + ({ + browserFields, + docValueFields, + handleOnPanelClosed, + isFlyoutView, + tabType, + timelineId, + }: DetailsPanelProps) => { + const dispatch = useDispatch(); + const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []); + const expandedDetail = useDeepEqualSelector((state) => { + return (getTimeline(state, timelineId) ?? timelineDefaults).expandedDetail; + }); + + // To be used primarily in the flyout scenario where we don't want to maintain the tabType + const defaultOnPanelClose = useCallback(() => { + dispatch(timelineActions.toggleDetailPanel({ timelineId })); + }, [dispatch, timelineId]); + + const activeTab = tabType ?? TimelineTabs.query; + const closePanel = useCallback(() => { + if (handleOnPanelClosed) handleOnPanelClosed(); + else defaultOnPanelClose(); + }, [defaultOnPanelClose, handleOnPanelClosed]); + + if (!expandedDetail) return null; + + const currentTabDetail = expandedDetail[activeTab]; + + if (!currentTabDetail?.panelView) return null; + + let visiblePanel = null; // store in variable to make return statement more readable + const contextID = `${timelineId}-${activeTab}`; + + if (currentTabDetail?.panelView === 'eventDetail' && currentTabDetail?.params?.eventId) { + visiblePanel = ( + + ); + } + + if (currentTabDetail?.panelView === 'hostDetail' && currentTabDetail?.params?.hostName) { + visiblePanel = ( + + ); + } + + if (currentTabDetail?.panelView === 'networkDetail' && currentTabDetail?.params?.ip) { + visiblePanel = ( + + ); + } + + return isFlyoutView ? ( + + {visiblePanel} + + ) : ( + visiblePanel + ); + } +); + +DetailsPanel.displayName = 'DetailsPanel'; diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/network_details/expandable_network.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/network_details/expandable_network.tsx new file mode 100644 index 0000000000000..b12b575681acf --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/network_details/expandable_network.tsx @@ -0,0 +1,134 @@ +/* + * 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 { EuiTitle } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import React, { useCallback, useMemo } from 'react'; +import { useDispatch } from 'react-redux'; +import { FlowTarget } from '../../../../../common/search_strategy'; +import { NetworkDetailsLink } from '../../../../common/components/links'; +import { IpOverview } from '../../../../network/components/details'; +import { useDeepEqualSelector } from '../../../../common/hooks/use_selector'; +import { useGlobalTime } from '../../../../common/containers/use_global_time'; +import { networkToCriteria } from '../../../../common/components/ml/criteria/network_to_criteria'; +import { scoreIntervalToDateTime } from '../../../../common/components/ml/score/score_interval_to_datetime'; +import { useKibana } from '../../../../common/lib/kibana'; +import { convertToBuildEsQuery } from '../../../../common/lib/keury'; +import { inputsSelectors } from '../../../../common/store'; +import { setAbsoluteRangeDatePicker } from '../../../../common/store/inputs/actions'; +import { OverviewEmpty } from '../../../../overview/components/overview_empty'; +import { esQuery } from '../../../../../../../../src/plugins/data/public'; +import { useSourcererScope } from '../../../../common/containers/sourcerer'; +import { useNetworkDetails } from '../../../../network/containers/details'; +import { networkModel } from '../../../../network/store'; +import { useAnomaliesTableData } from '../../../../common/components/ml/anomaly/use_anomalies_table_data'; + +interface ExpandableNetworkProps { + expandedNetwork: { ip: string; flowTarget: FlowTarget }; +} + +export const ExpandableNetworkDetailsTitle = ({ ip }: { ip: string }) => ( + +

+ {i18n.translate('xpack.securitySolution.timeline.sidePanel.networkDetails.title', { + defaultMessage: 'Network details', + })} + {`: ${ip}`} +

+
+); + +export const ExpandableNetworkDetailsPageLink = ({ + expandedNetwork: { ip, flowTarget }, +}: ExpandableNetworkProps) => ( + + {i18n.translate( + 'xpack.securitySolution.timeline.sidePanel.networkDetails.networkDetailsPageLink', + { + defaultMessage: 'View details page', + } + )} + +); + +export const ExpandableNetworkDetails = ({ + contextID, + expandedNetwork, +}: ExpandableNetworkProps & { contextID: string }) => { + const { ip, flowTarget } = expandedNetwork; + const dispatch = useDispatch(); + const { to, from, isInitializing } = useGlobalTime(); + const getGlobalQuerySelector = useMemo(() => inputsSelectors.globalQuerySelector(), []); + const getGlobalFiltersQuerySelector = useMemo( + () => inputsSelectors.globalFiltersQuerySelector(), + [] + ); + + const query = useDeepEqualSelector(getGlobalQuerySelector); + const filters = useDeepEqualSelector(getGlobalFiltersQuerySelector); + + const type = networkModel.NetworkType.details; + const narrowDateRange = useCallback( + (score, interval) => { + const fromTo = scoreIntervalToDateTime(score, interval); + dispatch( + setAbsoluteRangeDatePicker({ + id: 'global', + from: fromTo.from, + to: fromTo.to, + }) + ); + }, + [dispatch] + ); + const { + services: { uiSettings }, + } = useKibana(); + + const { docValueFields, indicesExist, indexPattern, selectedPatterns } = useSourcererScope(); + const filterQuery = convertToBuildEsQuery({ + config: esQuery.getEsQueryConfig(uiSettings), + indexPattern, + queries: [query], + filters, + }); + + const [loading, { id, networkDetails }] = useNetworkDetails({ + docValueFields, + skip: isInitializing, + filterQuery, + indexNames: selectedPatterns, + ip, + }); + + const [isLoadingAnomaliesData, anomaliesData] = useAnomaliesTableData({ + criteriaFields: networkToCriteria(ip, flowTarget), + startDate: from, + endDate: to, + skip: isInitializing, + }); + + return indicesExist ? ( + + ) : ( + + ); +}; diff --git a/x-pack/plugins/security_solution/public/timelines/components/side_panel/network_details/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/side_panel/network_details/index.tsx new file mode 100644 index 0000000000000..e05c9435fc456 --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/side_panel/network_details/index.tsx @@ -0,0 +1,113 @@ +/* + * 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. + */ + +/* eslint-disable react/display-name */ + +import { + EuiFlexGroup, + EuiFlyoutHeader, + EuiFlyoutBody, + EuiFlexItem, + EuiButtonIcon, + EuiSpacer, +} from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import React from 'react'; +import styled from 'styled-components'; +import { FlowTarget } from '../../../../../common/search_strategy'; +import { + ExpandableNetworkDetailsTitle, + ExpandableNetworkDetailsPageLink, + ExpandableNetworkDetails, +} from './expandable_network'; + +const StyledEuiFlyoutBody = styled(EuiFlyoutBody)` + .euiFlyoutBody__overflow { + display: flex; + flex: 1; + overflow-x: hidden; + overflow-y: scroll; + + .euiFlyoutBody__overflowContent { + flex: 1; + overflow-x: hidden; + overflow-y: scroll; + padding: ${({ theme }) => `${theme.eui.paddingSizes.xs} ${theme.eui.paddingSizes.m} 64px`}; + } + } +`; + +const StyledEuiFlexGroup = styled(EuiFlexGroup)` + flex: 0; +`; + +const StyledEuiFlexItem = styled(EuiFlexItem)` + &.euiFlexItem { + flex: 1 0 0; + overflow-y: scroll; + overflow-x: hidden; + } +`; + +const StyledEuiFlexButtonWrapper = styled(EuiFlexItem)` + align-self: flex-start; +`; + +interface NetworkDetailsProps { + contextID: string; + expandedNetwork: { ip: string; flowTarget: FlowTarget }; + handleOnNetworkClosed: () => void; + isFlyoutView?: boolean; +} + +export const NetworkDetailsPanel = React.memo( + ({ contextID, expandedNetwork, handleOnNetworkClosed, isFlyoutView }: NetworkDetailsProps) => { + const { ip } = expandedNetwork; + + return isFlyoutView ? ( + <> + + + + + + + + + + + ) : ( + <> + + + + + + + + + + + + + + + + + + ); + } +); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.test.tsx index 1ee5e39dfaa26..16e2b28a120d7 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.test.tsx @@ -30,10 +30,9 @@ describe('Actions', () => { ariaRowindex={2} checked={false} columnValues={'abc def'} - expanded={false} eventId="abc" loadingEventIds={[]} - onEventToggled={jest.fn()} + onEventDetailsPanelOpened={jest.fn()} onRowSelected={jest.fn()} showCheckboxes={true} /> @@ -52,9 +51,8 @@ describe('Actions', () => { checked={false} columnValues={'abc def'} eventId="abc" - expanded={false} loadingEventIds={[]} - onEventToggled={jest.fn()} + onEventDetailsPanelOpened={jest.fn()} onRowSelected={jest.fn()} showCheckboxes={false} /> diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.tsx index 2bbf793b9c78f..9ce27aa936783 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/actions/index.tsx @@ -20,10 +20,9 @@ interface Props { columnValues: string; checked: boolean; onRowSelected: OnRowSelected; - expanded: boolean; eventId: string; loadingEventIds: Readonly; - onEventToggled: () => void; + onEventDetailsPanelOpened: () => void; showCheckboxes: boolean; } @@ -33,10 +32,9 @@ const ActionsComponent: React.FC = ({ additionalActions, checked, columnValues, - expanded, eventId, loadingEventIds, - onEventToggled, + onEventDetailsPanelOpened, onRowSelected, showCheckboxes, }) => { @@ -78,9 +76,8 @@ const ActionsComponent: React.FC = ({ diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.test.tsx index 9be338e6b44b3..abdfda3272d6a 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.test.tsx @@ -51,7 +51,7 @@ describe('EventColumnView', () => { loading: false, loadingEventIds: [], notesCount: 0, - onEventToggled: jest.fn(), + onEventDetailsPanelOpened: jest.fn(), onPinEvent: jest.fn(), onRowSelected: jest.fn(), onUnPinEvent: jest.fn(), diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.tsx index 0afb31984ee8e..9d7b76af25a59 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/event_column_view.tsx @@ -42,12 +42,11 @@ interface Props { data: TimelineNonEcsData[]; ecsData: Ecs; eventIdToNoteIds: Readonly>; - expanded: boolean; isEventPinned: boolean; isEventViewer?: boolean; loadingEventIds: Readonly; notesCount: number; - onEventToggled: () => void; + onEventDetailsPanelOpened: () => void; onPinEvent: OnPinEvent; onRowSelected: OnRowSelected; onUnPinEvent: OnUnPinEvent; @@ -74,12 +73,11 @@ export const EventColumnView = React.memo( data, ecsData, eventIdToNoteIds, - expanded, isEventPinned = false, isEventViewer = false, loadingEventIds, notesCount, - onEventToggled, + onEventDetailsPanelOpened, onPinEvent, onRowSelected, onUnPinEvent, @@ -220,14 +218,12 @@ export const EventColumnView = React.memo( checked={Object.keys(selectedEventIds).includes(id)} columnValues={columnValues} onRowSelected={onRowSelected} - expanded={expanded} data-test-subj="actions" eventId={id} loadingEventIds={loadingEventIds} - onEventToggled={onEventToggled} + onEventDetailsPanelOpened={onEventDetailsPanelOpened} showCheckboxes={showCheckboxes} /> - = ({ }) => { const trGroupRef = useRef(null); const dispatch = useDispatch(); + // Store context in state rather than creating object in provider value={} to prevent re-renders caused by a new object being created + const [activeStatefulEventContext] = useState({ timelineID: timelineId, tabType }); const [showNotes, setShowNotes] = useState<{ [eventId: string]: boolean }>({}); const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []); - const expandedEvent = useDeepEqualSelector( - (state) => - (getTimeline(state, timelineId) ?? timelineDefaults).expandedEvent[ - tabType ?? TimelineTabs.query - ] ?? {} + const expandedDetail = useDeepEqualSelector( + (state) => (getTimeline(state, timelineId) ?? timelineDefaults).expandedDetail ?? {} ); + const hostName = useMemo(() => { + const hostNameArr = getMappedNonEcsValue({ data: event?.data, fieldName: 'host.name' }); + return hostNameArr && hostNameArr.length > 0 ? hostNameArr[0] : null; + }, [event?.data]); + + const hostIPAddresses = useMemo(() => { + const ipList = getMappedNonEcsValue({ data: event?.data, fieldName: 'host.ip' }); + return ipList; + }, [event?.data]); + + const activeTab = tabType ?? TimelineTabs.query; + const activeExpandedDetail = expandedDetail[activeTab]; + + const isDetailPanelExpanded: boolean = + (activeExpandedDetail?.panelView === 'eventDetail' && + activeExpandedDetail?.params?.eventId === event._id) || + (activeExpandedDetail?.panelView === 'hostDetail' && + activeExpandedDetail?.params?.hostName === hostName) || + (activeExpandedDetail?.panelView === 'networkDetail' && + activeExpandedDetail?.params?.ip && + hostIPAddresses?.includes(activeExpandedDetail?.params?.ip)) || + false; + const getNotesByIds = useMemo(() => appSelectors.notesByIdsSelector(), []); const notesById = useDeepEqualSelector(getNotesByIds); const noteIds: string[] = eventIdToNoteIds[event._id] || emptyNotes; - const isExpanded = useMemo(() => expandedEvent && expandedEvent.eventId === event._id, [ - event._id, - expandedEvent, - ]); const notes: TimelineResultNote[] = useMemo( () => @@ -153,23 +177,28 @@ const StatefulEventComponent: React.FC = ({ [dispatch, timelineId] ); - const handleOnEventToggled = useCallback(() => { + const handleOnEventDetailPanelOpened = useCallback(() => { const eventId = event._id; const indexName = event._index!; + const updatedExpandedDetail: TimelineExpandedDetailType = { + panelView: 'eventDetail', + params: { + eventId, + indexName, + }, + }; + dispatch( - timelineActions.toggleExpandedEvent({ + timelineActions.toggleDetailPanel({ + ...updatedExpandedDetail, tabType, timelineId, - event: { - eventId, - indexName, - }, }) ); if (timelineId === TimelineId.active && tabType === TimelineTabs.query) { - activeTimeline.toggleExpandedEvent({ eventId, indexName }); + activeTimeline.toggleExpandedDetail({ ...updatedExpandedDetail }); } }, [dispatch, event._id, event._index, tabType, timelineId]); @@ -209,63 +238,64 @@ const StatefulEventComponent: React.FC = ({ ); return ( - - + + + - - - - + + + + - {RowRendererContent} - - + {RowRendererContent} + + + ); }; diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/stateful_event_context.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/stateful_event_context.tsx new file mode 100644 index 0000000000000..34abc06371aac --- /dev/null +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/events/stateful_event_context.tsx @@ -0,0 +1,17 @@ +/* + * 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 React from 'react'; +import { TimelineTabs } from '../../../../../../common/types/timeline'; + +interface StatefulEventContext { + tabType: TimelineTabs | undefined; + timelineID: string; +} + +// This context is available to all children of the stateful_event component where the provider is currently set +export const StatefulEventContext = React.createContext(null); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.test.tsx index 7decff8270736..723e4c3de5c27 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.test.tsx @@ -240,14 +240,15 @@ describe('Body', () => { expect(mockDispatch).toBeCalledTimes(1); expect(mockDispatch.mock.calls[0][0]).toEqual({ payload: { - event: { + panelView: 'eventDetail', + params: { eventId: '1', indexName: undefined, }, tabType: 'query', timelineId: 'timeline-test', }, - type: 'x-pack/security_solution/local/timeline/TOGGLE_EXPANDED_EVENT', + type: 'x-pack/security_solution/local/timeline/TOGGLE_DETAIL_PANEL', }); }); @@ -263,14 +264,15 @@ describe('Body', () => { expect(mockDispatch).toBeCalledTimes(1); expect(mockDispatch.mock.calls[0][0]).toEqual({ payload: { - event: { + panelView: 'eventDetail', + params: { eventId: '1', indexName: undefined, }, tabType: 'pinned', timelineId: 'timeline-test', }, - type: 'x-pack/security_solution/local/timeline/TOGGLE_EXPANDED_EVENT', + type: 'x-pack/security_solution/local/timeline/TOGGLE_DETAIL_PANEL', }); }); @@ -286,14 +288,15 @@ describe('Body', () => { expect(mockDispatch).toBeCalledTimes(1); expect(mockDispatch.mock.calls[0][0]).toEqual({ payload: { - event: { + panelView: 'eventDetail', + params: { eventId: '1', indexName: undefined, }, tabType: 'notes', timelineId: 'timeline-test', }, - type: 'x-pack/security_solution/local/timeline/TOGGLE_EXPANDED_EVENT', + type: 'x-pack/security_solution/local/timeline/TOGGLE_DETAIL_PANEL', }); }); }); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.tsx index 8aa1425bbe52d..4df6eb16ccb62 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/index.tsx @@ -60,6 +60,10 @@ const EXTRA_WIDTH = 4; // px export type StatefulBodyProps = OwnProps & PropsFromRedux; +/** + * The Body component is used everywhere timeline is used within the security application. It is the highest level component + * that is shared across all implementations of the timeline. + */ export const BodyComponent = React.memo( ({ activePage, diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/formatted_field.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/formatted_field.test.tsx index e97738d95e43f..9d716f8325cbc 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/formatted_field.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/formatted_field.test.tsx @@ -243,7 +243,7 @@ describe('Events', () => { expect(wrapper.find('[data-test-subj="truncatable-message"]').exists()).toEqual(false); }); - test('it renders a hyperlink to the hosts details page when fieldName is host.name, and a hostname is provided', () => { + test('it renders a button to open the hosts details panel when fieldName is host.name, and a hostname is provided', () => { const wrapper = mount( { /> ); - expect(wrapper.find('[data-test-subj="host-details-link"]').exists()).toEqual(true); + expect(wrapper.find('[data-test-subj="host-details-button"]').exists()).toEqual(true); }); - test('it does NOT render a hyperlink to the hosts details page when fieldName is host.name, but a hostname is NOT provided', () => { + test('it does NOT render a button to open the hosts details panel when fieldName is host.name, but a hostname is NOT provided', () => { const wrapper = mount( { /> ); - expect(wrapper.find('[data-test-subj="host-details-link"]').exists()).toEqual(false); + expect(wrapper.find('[data-test-subj="host-details-button"]').exists()).toEqual(false); }); test('it renders placeholder text when fieldName is host.name, but a hostname is NOT provided', () => { diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/host_name.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/host_name.tsx index 50ed97d5fd8b6..c57cfce3cebe6 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/host_name.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/body/renderers/host_name.tsx @@ -5,13 +5,21 @@ * 2.0. */ -import React from 'react'; +import React, { useCallback, useContext } from 'react'; +import { useDispatch } from 'react-redux'; import { isString } from 'lodash/fp'; - +import { LinkAnchor } from '../../../../../common/components/links'; +import { + TimelineId, + TimelineTabs, + TimelineExpandedDetailType, +} from '../../../../../../common/types/timeline'; import { DefaultDraggable } from '../../../../../common/components/draggables'; import { getEmptyTagValue } from '../../../../../common/components/empty_value'; -import { HostDetailsLink } from '../../../../../common/components/links'; import { TruncatableText } from '../../../../../common/components/truncatable_text'; +import { StatefulEventContext } from '../events/stateful_event_context'; +import { activeTimeline } from '../../../../containers/active_timeline_context'; +import { timelineActions } from '../../../../store/timeline'; interface Props { contextId: string; @@ -21,18 +29,48 @@ interface Props { } const HostNameComponent: React.FC = ({ fieldName, contextId, eventId, value }) => { - const hostname = `${value}`; + const dispatch = useDispatch(); + const eventContext = useContext(StatefulEventContext); + const hostName = `${value}`; + + const openHostDetailsSidePanel = useCallback( + (e) => { + e.preventDefault(); + if (hostName && eventContext?.tabType && eventContext?.timelineID) { + const { timelineID, tabType } = eventContext; + const updatedExpandedDetail: TimelineExpandedDetailType = { + panelView: 'hostDetail', + params: { + hostName, + }, + }; + + dispatch( + timelineActions.toggleDetailPanel({ + ...updatedExpandedDetail, + timelineId: timelineID, + tabType, + }) + ); + + if (timelineID === TimelineId.active && tabType === TimelineTabs.query) { + activeTimeline.toggleExpandedDetail({ ...updatedExpandedDetail }); + } + } + }, + [dispatch, eventContext, hostName] + ); - return isString(value) && hostname.length > 0 ? ( + return isString(value) && hostName.length > 0 ? ( - - {value} - + + {hostName} + ) : ( getEmptyTagValue() diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/event_details.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/event_details.tsx deleted file mode 100644 index 6b8381c54de01..0000000000000 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/event_details.tsx +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 { some } from 'lodash/fp'; -import { EuiSpacer } from '@elastic/eui'; -import React, { useMemo } from 'react'; -import deepEqual from 'fast-deep-equal'; - -import { BrowserFields, DocValueFields } from '../../../common/containers/source'; -import { - ExpandableEvent, - ExpandableEventTitle, - HandleOnEventClosed, -} from '../../../timelines/components/timeline/expandable_event'; -import { useDeepEqualSelector } from '../../../common/hooks/use_selector'; -import { useTimelineEventsDetails } from '../../containers/details'; -import { timelineSelectors } from '../../store/timeline'; -import { timelineDefaults } from '../../store/timeline/defaults'; -import { TimelineTabs } from '../../../../common/types/timeline'; - -interface EventDetailsProps { - browserFields: BrowserFields; - docValueFields: DocValueFields[]; - tabType: TimelineTabs; - timelineId: string; - handleOnEventClosed?: HandleOnEventClosed; -} - -const EventDetailsComponent: React.FC = ({ - browserFields, - docValueFields, - tabType, - timelineId, - handleOnEventClosed, -}) => { - const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []); - const expandedEvent = useDeepEqualSelector( - (state) => (getTimeline(state, timelineId) ?? timelineDefaults).expandedEvent[tabType] ?? {} - ); - - const [loading, detailsData] = useTimelineEventsDetails({ - docValueFields, - indexName: expandedEvent.indexName!, - eventId: expandedEvent.eventId!, - skip: !expandedEvent.eventId, - }); - - const isAlert = useMemo( - () => some({ category: 'signal', field: 'signal.rule.id' }, detailsData), - [detailsData] - ); - - return ( - <> - - - - - ); -}; - -export const EventDetails = React.memo( - EventDetailsComponent, - (prevProps, nextProps) => - deepEqual(prevProps.browserFields, nextProps.browserFields) && - deepEqual(prevProps.docValueFields, nextProps.docValueFields) && - prevProps.timelineId === nextProps.timelineId && - prevProps.handleOnEventClosed === nextProps.handleOnEventClosed -); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/graph_tab_content/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/graph_tab_content/index.tsx index db4867e1abfe7..1678a92c4cdaa 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/graph_tab_content/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/graph_tab_content/index.tsx @@ -9,10 +9,11 @@ import React, { useMemo } from 'react'; import { timelineSelectors } from '../../../store/timeline'; import { useShallowEqualSelector } from '../../../../common/hooks/use_selector'; +import { TimelineId } from '../../../../../common/types/timeline'; import { GraphOverlay } from '../../graph_overlay'; interface GraphTabContentProps { - timelineId: string; + timelineId: TimelineId; } const GraphTabContentComponent: React.FC = ({ timelineId }) => { diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/index.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/index.test.tsx index 219d32f147b60..e7422e32805a9 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/index.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/index.test.tsx @@ -12,7 +12,7 @@ import useResizeObserver from 'use-resize-observer/polyfilled'; import { DragDropContextWrapper } from '../../../common/components/drag_and_drop/drag_drop_context_wrapper'; import '../../../common/mock/match_media'; import { mockBrowserFields, mockDocValueFields } from '../../../common/containers/source/mock'; - +import { TimelineId } from '../../../../common/types/timeline'; import { mockIndexNames, mockIndexPattern, TestProviders } from '../../../common/mock'; import { StatefulTimeline, Props as StatefulTimelineOwnProps } from './index'; @@ -55,7 +55,7 @@ jest.mock('../../../common/containers/sourcerer', () => { }); describe('StatefulTimeline', () => { const props: StatefulTimelineOwnProps = { - timelineId: 'timeline-test', + timelineId: TimelineId.test, }; beforeEach(() => { @@ -91,7 +91,7 @@ describe('StatefulTimeline', () => { ); expect( wrapper - .find(`[data-timeline-id="timeline-test"].${SELECTOR_TIMELINE_GLOBAL_CONTAINER}`) + .find(`[data-timeline-id="test"].${SELECTOR_TIMELINE_GLOBAL_CONTAINER}`) .first() .exists() ).toEqual(true); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/index.tsx index 3f91f78f56383..e4a40f7ba7d5a 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/index.tsx @@ -18,7 +18,7 @@ import { isTab } from '../../../common/components/accessibility/helpers'; import { useSourcererScope } from '../../../common/containers/sourcerer'; import { SourcererScopeName } from '../../../common/store/sourcerer/model'; import { FlyoutHeader, FlyoutHeaderPanel } from '../flyout/header'; -import { TimelineType, TimelineTabs } from '../../../../common/types/timeline'; +import { TimelineType, TimelineId } from '../../../../common/types/timeline'; import { useDeepEqualSelector, useShallowEqualSelector } from '../../../common/hooks/use_selector'; import { activeTimeline } from '../../containers/active_timeline_context'; import { EVENTS_COUNT_BUTTON_CLASS_NAME, onTimelineTabKeyPressed } from './helpers'; @@ -35,7 +35,7 @@ const TimelineTemplateBadge = styled.div` `; export interface Props { - timelineId: string; + timelineId: TimelineId; } const TimelineSavingProgressComponent: React.FC = ({ timelineId }) => { @@ -69,9 +69,7 @@ const StatefulTimelineComponent: React.FC = ({ timelineId }) => { id: timelineId, columns: defaultHeaders, indexNames: selectedPatterns, - expandedEvent: { - [TimelineTabs.query]: activeTimeline.getExpandedEvent(), - }, + expandedDetail: activeTimeline.getExpandedDetail(), show: false, }) ); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/index.tsx index 9ed230fd1e202..0d32e790dab50 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/index.tsx @@ -31,8 +31,8 @@ import { CREATED_BY, NOTES } from '../../notes/translations'; import { PARTICIPANTS } from '../../../../cases/translations'; import { NotePreviews } from '../../open_timeline/note_previews'; import { TimelineResultNote } from '../../open_timeline/types'; -import { EventDetails } from '../event_details'; import { getTimelineNoteSelector } from './selectors'; +import { DetailsPanel } from '../../side_panel'; const FullWidthFlexGroup = styled(EuiFlexGroup)` width: 100%; @@ -125,7 +125,7 @@ const NotesTabContentComponent: React.FC = ({ timelineId } const getTimelineNotes = useMemo(() => getTimelineNoteSelector(), []); const { createdBy, - expandedEvent, + expandedDetail, eventIdToNoteIds, noteIds, status: timelineStatus, @@ -162,22 +162,22 @@ const NotesTabContentComponent: React.FC = ({ timelineId } [dispatch, timelineId] ); - const handleOnEventClosed = useCallback(() => { - dispatch(timelineActions.toggleExpandedEvent({ tabType: TimelineTabs.notes, timelineId })); + const handleOnPanelClosed = useCallback(() => { + dispatch(timelineActions.toggleDetailPanel({ tabType: TimelineTabs.notes, timelineId })); }, [dispatch, timelineId]); - const EventDetailsContent = useMemo( + const DetailsPanelContent = useMemo( () => - expandedEvent != null && expandedEvent.eventId != null ? ( - ) : null, - [browserFields, docValueFields, expandedEvent, handleOnEventClosed, timelineId] + [browserFields, docValueFields, expandedDetail, handleOnPanelClosed, timelineId] ); const SidebarContent = useMemo( @@ -216,7 +216,7 @@ const NotesTabContentComponent: React.FC = ({ timelineId } - {EventDetailsContent ?? SidebarContent} + {DetailsPanelContent ?? SidebarContent} ); }; diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/selectors.ts b/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/selectors.ts index 84e39e5481afd..bc0317f4c4282 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/selectors.ts +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/notes_tab_content/selectors.ts @@ -13,7 +13,7 @@ export const getTimelineNoteSelector = () => createSelector(timelineSelectors.selectTimeline, (timeline) => { return { createdBy: timeline.createdBy, - expandedEvent: timeline.expandedEvent?.notes ?? {}, + expandedDetail: timeline.expandedDetail ?? {}, eventIdToNoteIds: timeline?.eventIdToNoteIds ?? {}, noteIds: timeline.noteIds, status: timeline.status, diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/__snapshots__/index.test.tsx.snap b/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/__snapshots__/index.test.tsx.snap index f5064ba66cf2f..e55c1cc8f0af3 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/__snapshots__/index.test.tsx.snap +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/__snapshots__/index.test.tsx.snap @@ -135,7 +135,7 @@ In other use cases the message field can be used to concatenate different values } onEventClosed={[MockFunction]} pinnedEventIds={Object {}} - showEventDetails={false} + showExpandedDetails={false} sort={ Array [ Object { diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.test.tsx index 56d53c5fecb96..2107969df22b8 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.test.tsx @@ -96,7 +96,7 @@ describe('PinnedTabContent', () => { itemsPerPageOptions: [5, 10, 20], sort, pinnedEventIds: {}, - showEventDetails: false, + showExpandedDetails: false, onEventClosed: jest.fn(), }; }); diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.tsx index 98cc130a38de3..68461a7234d09 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/pinned_tab_content/index.tsx @@ -25,11 +25,11 @@ import { SourcererScopeName } from '../../../../common/store/sourcerer/model'; import { timelineDefaults } from '../../../store/timeline/defaults'; import { useSourcererScope } from '../../../../common/containers/sourcerer'; import { TimelineModel } from '../../../store/timeline/model'; -import { EventDetails } from '../event_details'; -import { ToggleExpandedEvent } from '../../../store/timeline/actions'; +import { ToggleDetailPanel } from '../../../store/timeline/actions'; import { State } from '../../../../common/store'; import { calculateTotalPages } from '../helpers'; import { TimelineTabs } from '../../../../../common/types/timeline'; +import { DetailsPanel } from '../../side_panel'; const StyledEuiFlyoutBody = styled(EuiFlyoutBody)` overflow-y: hidden; @@ -90,7 +90,7 @@ export const PinnedTabContentComponent: React.FC = ({ itemsPerPageOptions, pinnedEventIds, onEventClosed, - showEventDetails, + showExpandedDetails, sort, }) => { const { browserFields, docValueFields, loading: loadingSourcerer } = useSourcererScope( @@ -169,7 +169,7 @@ export const PinnedTabContentComponent: React.FC = ({ timerangeKind: undefined, }); - const handleOnEventClosed = useCallback(() => { + const handleOnPanelClosed = useCallback(() => { onEventClosed({ tabType: TimelineTabs.pinned, timelineId }); }, [timelineId, onEventClosed]); @@ -217,16 +217,16 @@ export const PinnedTabContentComponent: React.FC = ({ - {showEventDetails && ( + {showExpandedDetails && ( <> - @@ -242,7 +242,7 @@ const makeMapStateToProps = () => { const timeline: TimelineModel = getTimeline(state, timelineId) ?? timelineDefaults; const { columns, - expandedEvent, + expandedDetail, itemsPerPage, itemsPerPageOptions, pinnedEventIds, @@ -255,7 +255,8 @@ const makeMapStateToProps = () => { itemsPerPage, itemsPerPageOptions, pinnedEventIds, - showEventDetails: !!expandedEvent[TimelineTabs.pinned]?.eventId, + showExpandedDetails: + !!expandedDetail[TimelineTabs.pinned] && !!expandedDetail[TimelineTabs.pinned]?.panelView, sort, }; }; @@ -263,8 +264,8 @@ const makeMapStateToProps = () => { }; const mapDispatchToProps = (dispatch: Dispatch, { timelineId }: OwnProps) => ({ - onEventClosed: (args: ToggleExpandedEvent) => { - dispatch(timelineActions.toggleExpandedEvent(args)); + onEventClosed: (args: ToggleDetailPanel) => { + dispatch(timelineActions.toggleDetailPanel(args)); }, }); @@ -278,7 +279,7 @@ const PinnedTabContent = connector( (prevProps, nextProps) => prevProps.itemsPerPage === nextProps.itemsPerPage && prevProps.onEventClosed === nextProps.onEventClosed && - prevProps.showEventDetails === nextProps.showEventDetails && + prevProps.showExpandedDetails === nextProps.showExpandedDetails && prevProps.timelineId === nextProps.timelineId && deepEqual(prevProps.columns, nextProps.columns) && deepEqual(prevProps.itemsPerPageOptions, nextProps.itemsPerPageOptions) && diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/__snapshots__/index.test.tsx.snap b/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/__snapshots__/index.test.tsx.snap index 4fbf7788d9122..0688a10b31eef 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/__snapshots__/index.test.tsx.snap +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/__snapshots__/index.test.tsx.snap @@ -262,7 +262,7 @@ In other use cases the message field can be used to concatenate different values } end="2018-03-24T03:33:52.253Z" eventType="all" - expandedEvent={Object {}} + expandedDetail={Object {}} filters={Array []} isLive={false} itemsPerPage={5} @@ -278,7 +278,7 @@ In other use cases the message field can be used to concatenate different values onEventClosed={[MockFunction]} show={true} showCallOutUnauthorizedMsg={false} - showEventDetails={false} + showExpandedDetails={false} sort={ Array [ Object { diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.test.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.test.tsx index 882c0c90973b3..c7d27da64c650 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.test.tsx @@ -96,9 +96,8 @@ describe('Timeline', () => { columns: defaultHeaders, dataProviders: mockDataProviders, end: endDate, - expandedEvent: {}, eventType: 'all', - showEventDetails: false, + expandedDetail: {}, filters: [], timelineId: TimelineId.test, isLive: false, @@ -108,6 +107,7 @@ describe('Timeline', () => { kqlQueryExpression: '', onEventClosed: jest.fn(), showCallOutUnauthorizedMsg: false, + showExpandedDetails: false, sort, start: startDate, status: TimelineStatus.active, diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.tsx index 25acd48916944..c61be4951db76 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/query_tab_content/index.tsx @@ -46,12 +46,12 @@ import { timelineDefaults } from '../../../../timelines/store/timeline/defaults' import { useSourcererScope } from '../../../../common/containers/sourcerer'; import { useTimelineEventsCountPortal } from '../../../../common/hooks/use_timeline_events_count'; import { TimelineModel } from '../../../../timelines/store/timeline/model'; -import { EventDetails } from '../event_details'; import { TimelineDatePickerLock } from '../date_picker_lock'; import { HideShowContainer } from '../styles'; import { useTimelineFullScreen } from '../../../../common/containers/use_full_screen'; import { activeTimeline } from '../../../containers/active_timeline_context'; -import { ToggleExpandedEvent } from '../../../store/timeline/actions'; +import { ToggleDetailPanel } from '../../../store/timeline/actions'; +import { DetailsPanel } from '../../side_panel'; const TimelineHeaderContainer = styled.div` margin-top: 6px; @@ -139,7 +139,7 @@ export const QueryTabContentComponent: React.FC = ({ dataProviders, end, eventType, - expandedEvent, + expandedDetail, filters, timelineId, isLive, @@ -150,7 +150,7 @@ export const QueryTabContentComponent: React.FC = ({ onEventClosed, show, showCallOutUnauthorizedMsg, - showEventDetails, + showExpandedDetails, start, status, sort, @@ -245,16 +245,17 @@ export const QueryTabContentComponent: React.FC = ({ timerangeKind, }); - const handleOnEventClosed = useCallback(() => { + const handleOnPanelClosed = useCallback(() => { onEventClosed({ tabType: TimelineTabs.query, timelineId }); - if (timelineId === TimelineId.active) { - activeTimeline.toggleExpandedEvent({ - eventId: expandedEvent.eventId!, - indexName: expandedEvent.indexName!, - }); + if ( + expandedDetail[TimelineTabs.query]?.panelView && + timelineId === TimelineId.active && + showExpandedDetails + ) { + activeTimeline.toggleExpandedDetail({}); } - }, [timelineId, onEventClosed, expandedEvent.eventId, expandedEvent.indexName]); + }, [onEventClosed, timelineId, expandedDetail, showExpandedDetails]); useEffect(() => { setIsTimelineLoading({ id: timelineId, isLoading: isQueryLoading || loadingSourcerer }); @@ -350,16 +351,16 @@ export const QueryTabContentComponent: React.FC = ({ - {showEventDetails && ( + {showExpandedDetails && ( <> - @@ -382,7 +383,7 @@ const makeMapStateToProps = () => { columns, dataProviders, eventType, - expandedEvent, + expandedDetail, filters, itemsPerPage, itemsPerPageOptions, @@ -406,7 +407,7 @@ const makeMapStateToProps = () => { dataProviders, eventType: eventType ?? 'raw', end: input.timerange.to, - expandedEvent: expandedEvent[TimelineTabs.query] ?? {}, + expandedDetail, filters: timelineFilter, timelineId, isLive: input.policy.kind === 'interval', @@ -415,8 +416,9 @@ const makeMapStateToProps = () => { kqlMode, kqlQueryExpression, showCallOutUnauthorizedMsg: getShowCallOutUnauthorizedMsg(state), - showEventDetails: !!expandedEvent[TimelineTabs.query]?.eventId, show, + showExpandedDetails: + !!expandedDetail[TimelineTabs.query] && !!expandedDetail[TimelineTabs.query]?.panelView, sort, start: input.timerange.from, status, @@ -437,8 +439,8 @@ const mapDispatchToProps = (dispatch: Dispatch, { timelineId }: OwnProps) => ({ }) ); }, - onEventClosed: (args: ToggleExpandedEvent) => { - dispatch(timelineActions.toggleExpandedEvent(args)); + onEventClosed: (args: ToggleDetailPanel) => { + dispatch(timelineActions.toggleDetailPanel(args)); }, }); @@ -460,7 +462,7 @@ const QueryTabContent = connector( prevProps.onEventClosed === nextProps.onEventClosed && prevProps.show === nextProps.show && prevProps.showCallOutUnauthorizedMsg === nextProps.showCallOutUnauthorizedMsg && - prevProps.showEventDetails === nextProps.showEventDetails && + prevProps.showExpandedDetails === nextProps.showExpandedDetails && prevProps.status === nextProps.status && prevProps.timelineId === nextProps.timelineId && prevProps.updateEventTypeAndIndexesName === nextProps.updateEventTypeAndIndexesName && diff --git a/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs_content/index.tsx b/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs_content/index.tsx index 9f6bfcf7e320c..ca70e4ae64686 100644 --- a/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs_content/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/components/timeline/tabs_content/index.tsx @@ -9,7 +9,7 @@ import { EuiBadge, EuiLoadingContent, EuiTabs, EuiTab } from '@elastic/eui'; import React, { lazy, memo, Suspense, useCallback, useEffect, useMemo } from 'react'; import { useDispatch } from 'react-redux'; import styled from 'styled-components'; -import { TimelineTabs } from '../../../../../common/types/timeline'; +import { TimelineTabs, TimelineId } from '../../../../../common/types/timeline'; import { useShallowEqualSelector, @@ -42,7 +42,7 @@ const NotesTabContent = lazy(() => import('../notes_tab_content')); const PinnedTabContent = lazy(() => import('../pinned_tab_content')); interface BasicTimelineTab { - timelineId: string; + timelineId: TimelineId; graphEventId?: string; } diff --git a/x-pack/plugins/security_solution/public/timelines/containers/active_timeline_context.ts b/x-pack/plugins/security_solution/public/timelines/containers/active_timeline_context.ts index 190cf53689ec0..93e53fa544bbc 100644 --- a/x-pack/plugins/security_solution/public/timelines/containers/active_timeline_context.ts +++ b/x-pack/plugins/security_solution/public/timelines/containers/active_timeline_context.ts @@ -5,7 +5,11 @@ * 2.0. */ -import { TimelineExpandedEventType } from '../../../common/types/timeline'; +import { + TimelineExpandedDetail, + TimelineExpandedDetailType, + TimelineTabs, +} from '../../../common/types/timeline'; import { TimelineEventsAllRequestOptions } from '../../../common/search_strategy/timeline'; import { TimelineArgs } from '.'; @@ -22,7 +26,7 @@ import { TimelineArgs } from '.'; class ActiveTimelineEvents { private _activePage: number = 0; - private _expandedEvent: TimelineExpandedEventType = {}; + private _expandedDetail: TimelineExpandedDetail = {}; private _pageName: string = ''; private _request: TimelineEventsAllRequestOptions | null = null; private _response: TimelineArgs | null = null; @@ -35,20 +39,40 @@ class ActiveTimelineEvents { this._activePage = activePage; } - getExpandedEvent() { - return this._expandedEvent; + getExpandedDetail() { + return this._expandedDetail; } - toggleExpandedEvent(expandedEvent: TimelineExpandedEventType) { - if (expandedEvent.eventId === this._expandedEvent.eventId) { - this._expandedEvent = {}; + toggleExpandedDetail(expandedDetail: TimelineExpandedDetailType) { + const queryTab = TimelineTabs.query; + const currentExpandedDetail = this._expandedDetail[queryTab]; + let isSameExpandedDetail; + + // Check if the stored details matches the incoming detail + if (currentExpandedDetail?.panelView === 'eventDetail') { + isSameExpandedDetail = + expandedDetail?.panelView === 'eventDetail' && + expandedDetail?.params?.eventId === currentExpandedDetail?.params?.eventId; + } else if (currentExpandedDetail?.panelView === 'hostDetail') { + isSameExpandedDetail = + expandedDetail?.panelView === 'hostDetail' && + expandedDetail?.params?.hostName === currentExpandedDetail?.params?.hostName; + } else if (currentExpandedDetail?.panelView === 'networkDetail') { + isSameExpandedDetail = + expandedDetail?.panelView === 'networkDetail' && + expandedDetail?.params?.ip === currentExpandedDetail?.params?.ip; + } + + // if so, unset it, otherwise set it + if (isSameExpandedDetail) { + this._expandedDetail = {}; } else { - this._expandedEvent = expandedEvent; + this._expandedDetail = { [queryTab]: { ...expandedDetail } }; } } - setExpandedEvent(expandedEvent: TimelineExpandedEventType) { - this._expandedEvent = expandedEvent; + setExpandedDetail(expandedDetail: TimelineExpandedDetail) { + this._expandedDetail = expandedDetail; } getPageName() { diff --git a/x-pack/plugins/security_solution/public/timelines/containers/index.tsx b/x-pack/plugins/security_solution/public/timelines/containers/index.tsx index 57815a6d6bcd7..0d53d01fa7131 100644 --- a/x-pack/plugins/security_solution/public/timelines/containers/index.tsx +++ b/x-pack/plugins/security_solution/public/timelines/containers/index.tsx @@ -113,7 +113,7 @@ export const useTimelineEvents = ({ clearSignalsState(); if (id === TimelineId.active) { - activeTimeline.setExpandedEvent({}); + activeTimeline.setExpandedDetail({}); activeTimeline.setActivePage(newActivePage); } @@ -178,7 +178,7 @@ export const useTimelineEvents = ({ updatedAt: Date.now(), }; if (id === TimelineId.active) { - activeTimeline.setExpandedEvent({}); + activeTimeline.setExpandedDetail({}); activeTimeline.setPageName(pageName); activeTimeline.setRequest(request); activeTimeline.setResponse(newTimelineResponse); diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/actions.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/actions.ts index a38d81a68d1bf..c9e3c8305a30d 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/actions.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/actions.ts @@ -20,10 +20,10 @@ import { KqlMode, TimelineModel, ColumnHeaderOptions } from './model'; import { TimelineNonEcsData } from '../../../../common/search_strategy/timeline'; import { TimelineEventsType, - TimelineExpandedEventType, + TimelineExpandedDetail, + TimelineExpandedDetailType, TimelineTypeLiteral, RowRendererId, - TimelineExpandedEvent, TimelineTabs, } from '../../../../common/types/timeline'; import { InsertTimeline } from './types'; @@ -38,12 +38,12 @@ export const addNoteToEvent = actionCreator<{ id: string; noteId: string; eventI 'ADD_NOTE_TO_EVENT' ); -export interface ToggleExpandedEvent { - event?: TimelineExpandedEventType; +export type ToggleDetailPanel = TimelineExpandedDetailType & { tabType?: TimelineTabs; timelineId: string; -} -export const toggleExpandedEvent = actionCreator('TOGGLE_EXPANDED_EVENT'); +}; + +export const toggleDetailPanel = actionCreator('TOGGLE_DETAIL_PANEL'); export const upsertColumn = actionCreator<{ column: ColumnHeaderOptions; @@ -67,7 +67,7 @@ export interface TimelineInput { end: string; }; excludedRowRendererIds?: RowRendererId[]; - expandedEvent?: TimelineExpandedEvent; + expandedDetail?: TimelineExpandedDetail; filters?: Filter[]; columns: ColumnHeaderOptions[]; itemsPerPage?: number; diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts index aaaf369f7bd5c..44a5c05e398f1 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/defaults.ts @@ -25,7 +25,7 @@ export const timelineDefaults: SubsetTimelineModel & Pick { description: '', eventIdToNoteIds: {}, eventType: 'all', - expandedEvent: {}, + expandedDetail: {}, excludedRowRendererIds: [], highlightedDropAndProviderId: '', historyIds: [], diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/epic_local_storage.test.tsx b/x-pack/plugins/security_solution/public/timelines/store/timeline/epic_local_storage.test.tsx index 584d270d8bea4..3d92397f4ab50 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/epic_local_storage.test.tsx +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/epic_local_storage.test.tsx @@ -82,7 +82,7 @@ describe('epicLocalStorage', () => { dataProviders: mockDataProviders, end: endDate, eventType: 'all', - expandedEvent: {}, + expandedDetail: {}, filters: [], isLive: false, itemsPerPage: 5, @@ -91,7 +91,7 @@ describe('epicLocalStorage', () => { kqlQueryExpression: '', onEventClosed: jest.fn(), showCallOutUnauthorizedMsg: false, - showEventDetails: false, + showExpandedDetails: false, start: startDate, status: TimelineStatus.active, sort, diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts index d5d60857abb9a..864e52fc377a0 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/helpers.ts @@ -8,6 +8,7 @@ import { getOr, omit, uniq, isEmpty, isEqualWith, union } from 'lodash/fp'; import uuid from 'uuid'; +import { ToggleDetailPanel } from './actions'; import { Filter } from '../../../../../../../src/plugins/data/public'; import { getColumnWidthFromType } from '../../../timelines/components/timeline/body/column_headers/helpers'; @@ -24,12 +25,13 @@ import { SerializedFilterQuery } from '../../../common/store/model'; import { TimelineNonEcsData } from '../../../../common/search_strategy/timeline'; import { TimelineEventsType, - TimelineExpandedEvent, + TimelineExpandedDetail, TimelineTypeLiteral, TimelineType, RowRendererId, TimelineStatus, TimelineId, + TimelineTabs, } from '../../../../common/types/timeline'; import { normalizeTimeRange } from '../../../common/components/url_state/normalize_time_range'; @@ -144,7 +146,7 @@ export const addTimelineToStore = ({ }: AddTimelineParams): TimelineById => { if (shouldResetActiveTimelineContext(id, timelineById[id], timeline)) { activeTimeline.setActivePage(0); - activeTimeline.setExpandedEvent({}); + activeTimeline.setExpandedDetail({}); } return { ...timelineById, @@ -171,7 +173,7 @@ interface AddNewTimelineParams { end: string; }; excludedRowRendererIds?: RowRendererId[]; - expandedEvent?: TimelineExpandedEvent; + expandedDetail?: TimelineExpandedDetail; filters?: Filter[]; id: string; itemsPerPage?: number; @@ -192,7 +194,7 @@ export const addNewTimeline = ({ dataProviders = [], dateRange: maybeDateRange, excludedRowRendererIds = [], - expandedEvent = {}, + expandedDetail = {}, filters = timelineDefaults.filters, id, itemsPerPage = timelineDefaults.itemsPerPage, @@ -221,7 +223,7 @@ export const addNewTimeline = ({ columns, dataProviders, dateRange, - expandedEvent, + expandedDetail, excludedRowRendererIds, filters, itemsPerPage, @@ -1431,3 +1433,21 @@ export const updateExcludedRowRenderersIds = ({ }, }; }; + +export const updateTimelineDetailsPanel = (action: ToggleDetailPanel) => { + const { tabType } = action; + + const panelViewOptions = new Set(['eventDetail', 'hostDetail', 'networkDetail']); + const expandedTabType = tabType ?? TimelineTabs.query; + + return action.panelView && panelViewOptions.has(action.panelView) + ? { + [expandedTabType]: { + params: action.params ? { ...action.params } : {}, + panelView: action.panelView, + }, + } + : { + [expandedTabType]: {}, + }; +}; diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/model.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/model.ts index cc9b47383e9c9..e5036efd41df4 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/model.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/model.ts @@ -14,7 +14,7 @@ import { TimelineNonEcsData } from '../../../../common/search_strategy/timeline' import { SerializedFilterQuery } from '../../../common/store/types'; import type { TimelineEventsType, - TimelineExpandedEvent, + TimelineExpandedDetail, TimelineType, TimelineStatus, RowRendererId, @@ -63,7 +63,8 @@ export interface TimelineModel { eventIdToNoteIds: Record; /** A list of Ids of excluded Row Renderers */ excludedRowRendererIds: RowRendererId[]; - expandedEvent: TimelineExpandedEvent; + /** This holds the view information for the flyout when viewing timeline in a consuming view (i.e. hosts page) or the side panel in the primary timeline view */ + expandedDetail: TimelineExpandedDetail; filters?: Filter[]; /** When non-empty, display a graph view for this event */ graphEventId?: string; @@ -143,7 +144,7 @@ export type SubsetTimelineModel = Readonly< | 'eventType' | 'eventIdToNoteIds' | 'excludedRowRendererIds' - | 'expandedEvent' + | 'expandedDetail' | 'graphEventId' | 'highlightedDropAndProviderId' | 'historyIds' diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.test.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.test.ts index 346a82ed0da1d..c4988673f49b6 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.test.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.test.ts @@ -79,7 +79,7 @@ const basicTimeline: TimelineModel = { description: '', eventIdToNoteIds: {}, excludedRowRendererIds: [], - expandedEvent: {}, + expandedDetail: {}, highlightedDropAndProviderId: '', historyIds: [], id: 'foo', diff --git a/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.ts b/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.ts index 791100a8b9e2a..7271eafa14863 100644 --- a/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.ts +++ b/x-pack/plugins/security_solution/public/timelines/store/timeline/reducer.ts @@ -35,7 +35,7 @@ import { showCallOutUnauthorizedMsg, showTimeline, startTimelineSaving, - toggleExpandedEvent, + toggleDetailPanel, unPinEvent, updateAutoSaveMsg, updateColumns, @@ -99,11 +99,12 @@ import { updateSavedQuery, updateGraphEventId, updateFilters, + updateTimelineDetailsPanel, updateTimelineEventType, } from './helpers'; import { TimelineState, EMPTY_TIMELINE_BY_ID } from './types'; -import { TimelineType, TimelineTabs } from '../../../../common/types/timeline'; +import { TimelineType } from '../../../../common/types/timeline'; export const initialTimelineState: TimelineState = { timelineById: EMPTY_TIMELINE_BY_ID, @@ -130,6 +131,7 @@ export const timelineReducer = reducerWithInitialState(initialTimelineState) dataProviders, dateRange, excludedRowRendererIds, + expandedDetail = {}, show, columns, itemsPerPage, @@ -148,6 +150,7 @@ export const timelineReducer = reducerWithInitialState(initialTimelineState) dataProviders, dateRange, excludedRowRendererIds, + expandedDetail, filters, id, itemsPerPage, @@ -178,22 +181,19 @@ export const timelineReducer = reducerWithInitialState(initialTimelineState) ...state, timelineById: addTimelineNoteToEvent({ id, noteId, eventId, timelineById: state.timelineById }), })) - .case(toggleExpandedEvent, (state, { tabType, timelineId, event = {} }) => { - const expandedTabType = tabType ?? TimelineTabs.query; - return { - ...state, - timelineById: { - ...state.timelineById, - [timelineId]: { - ...state.timelineById[timelineId], - expandedEvent: { - ...state.timelineById[timelineId].expandedEvent, - [expandedTabType]: event, - }, + .case(toggleDetailPanel, (state, action) => ({ + ...state, + timelineById: { + ...state.timelineById, + [action.timelineId]: { + ...state.timelineById[action.timelineId], + expandedDetail: { + ...state.timelineById[action.timelineId].expandedDetail, + ...updateTimelineDetailsPanel(action), }, }, - }; - }) + }, + })) .case(addProvider, (state, { id, provider }) => ({ ...state, timelineById: addTimelineProvider({ id, provider, timelineById: state.timelineById }), diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold_find_previous_signals.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold_find_previous_signals.ts index a623608ef6006..dbad1d12d2be6 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold_find_previous_signals.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold_find_previous_signals.ts @@ -48,6 +48,7 @@ export const findPreviousThresholdSignals = async ({ threshold: { terms: { field: 'signal.threshold_result.value', + size: 10000, }, aggs: { lastSignalTimestamp: { diff --git a/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts index 8a749b5009334..f5917e78135ec 100644 --- a/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts +++ b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.test.ts @@ -589,5 +589,57 @@ const ERROR_NAMESPACE_SPECIFIED = 'Spaces currently determines the namespaces'; }); }); }); + + describe('#openPointInTimeForType', () => { + test(`throws error if options.namespace is specified`, async () => { + const { client } = createSpacesSavedObjectsClient(); + + await expect(client.openPointInTimeForType('foo', { namespace: 'bar' })).rejects.toThrow( + ERROR_NAMESPACE_SPECIFIED + ); + }); + + test(`supplements options with the current namespace`, async () => { + const { client, baseClient } = createSpacesSavedObjectsClient(); + const expectedReturnValue = { id: 'abc123' }; + baseClient.openPointInTimeForType.mockReturnValue(Promise.resolve(expectedReturnValue)); + + const options = Object.freeze({ foo: 'bar' }); + // @ts-expect-error + const actualReturnValue = await client.openPointInTimeForType('foo', options); + + expect(actualReturnValue).toBe(expectedReturnValue); + expect(baseClient.openPointInTimeForType).toHaveBeenCalledWith('foo', { + foo: 'bar', + namespace: currentSpace.expectedNamespace, + }); + }); + }); + + describe('#closePointInTime', () => { + test(`throws error if options.namespace is specified`, async () => { + const { client } = createSpacesSavedObjectsClient(); + + await expect(client.closePointInTime('foo', { namespace: 'bar' })).rejects.toThrow( + ERROR_NAMESPACE_SPECIFIED + ); + }); + + test(`supplements options with the current namespace`, async () => { + const { client, baseClient } = createSpacesSavedObjectsClient(); + const expectedReturnValue = { succeeded: true, num_freed: 1 }; + baseClient.closePointInTime.mockReturnValue(Promise.resolve(expectedReturnValue)); + + const options = Object.freeze({ foo: 'bar' }); + // @ts-expect-error + const actualReturnValue = await client.closePointInTime('foo', options); + + expect(actualReturnValue).toBe(expectedReturnValue); + expect(baseClient.closePointInTime).toHaveBeenCalledWith('foo', { + foo: 'bar', + namespace: currentSpace.expectedNamespace, + }); + }); + }); }); }); diff --git a/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts index 9316d86b19bdd..433f95d2b5cf6 100644 --- a/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts +++ b/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.ts @@ -15,6 +15,8 @@ import { SavedObjectsClientContract, SavedObjectsCreateOptions, SavedObjectsFindOptions, + SavedObjectsClosePointInTimeOptions, + SavedObjectsOpenPointInTimeOptions, SavedObjectsUpdateOptions, SavedObjectsAddToNamespacesOptions, SavedObjectsDeleteFromNamespacesOptions, @@ -378,4 +380,42 @@ export class SpacesSavedObjectsClient implements SavedObjectsClientContract { namespace: spaceIdToNamespace(this.spaceId), }); } + + /** + * Opens a Point In Time (PIT) against the indices for the specified Saved Object types. + * The returned `id` can then be passed to `SavedObjects.find` to search against that PIT. + * + * @param {string|Array} type + * @param {object} [options] - {@link SavedObjectsOpenPointInTimeOptions} + * @property {string} [options.keepAlive] + * @property {string} [options.preference] + * @returns {promise} - { id: string } + */ + async openPointInTimeForType( + type: string | string[], + options: SavedObjectsOpenPointInTimeOptions = {} + ) { + throwErrorIfNamespaceSpecified(options); + return await this.client.openPointInTimeForType(type, { + ...options, + namespace: spaceIdToNamespace(this.spaceId), + }); + } + + /** + * Closes a Point In Time (PIT) by ID. This simply proxies the request to ES + * via the Elasticsearch client, and is included in the Saved Objects Client + * as a convenience for consumers who are using `openPointInTimeForType`. + * + * @param {string} id - ID returned from `openPointInTimeForType` + * @param {object} [options] - {@link SavedObjectsClosePointInTimeOptions} + * @returns {promise} - { succeeded: boolean; num_freed: number } + */ + async closePointInTime(id: string, options: SavedObjectsClosePointInTimeOptions = {}) { + throwErrorIfNamespaceSpecified(options); + return await this.client.closePointInTime(id, { + ...options, + namespace: spaceIdToNamespace(this.spaceId), + }); + } } diff --git a/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.test.tsx b/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.test.tsx index 27ddb28eed779..f475d97e2f39d 100644 --- a/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.test.tsx +++ b/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.test.tsx @@ -147,6 +147,7 @@ describe('EsQueryAlertTypeExpression', () => { {}} setAlertProperty={() => {}} diff --git a/x-pack/plugins/stack_alerts/public/alert_types/threshold/expression.test.tsx b/x-pack/plugins/stack_alerts/public/alert_types/threshold/expression.test.tsx index 01c2bc18f35e8..28f0f3db19614 100644 --- a/x-pack/plugins/stack_alerts/public/alert_types/threshold/expression.test.tsx +++ b/x-pack/plugins/stack_alerts/public/alert_types/threshold/expression.test.tsx @@ -89,6 +89,7 @@ describe('IndexThresholdAlertTypeExpression', () => { {}} setAlertProperty={() => {}} diff --git a/x-pack/plugins/task_manager/README.md b/x-pack/plugins/task_manager/README.md index 9be3be14ea3fc..c20bc4b29bcc8 100644 --- a/x-pack/plugins/task_manager/README.md +++ b/x-pack/plugins/task_manager/README.md @@ -85,10 +85,10 @@ export class Plugin { // This defaults to what is configured at the task manager level. maxAttempts: 5, - // The clusterMonitoring task occupies 2 workers, so if the system has 10 worker slots, - // 5 clusterMonitoring tasks could run concurrently per Kibana instance. This value is - // overridden by the `override_num_workers` config value, if specified. - numWorkers: 2, + // The maximum number tasks of this type that can be run concurrently per Kibana instance. + // Setting this value will force Task Manager to poll for this task type seperatly from other task types which + // can add significant load to the ES cluster, so please use this configuration only when absolutly necesery. + maxConcurrency: 1, // The createTaskRunner function / method returns an object that is responsible for // performing the work of the task. context: { taskInstance }, is documented below. diff --git a/x-pack/plugins/task_manager/server/buffered_task_store.test.ts b/x-pack/plugins/task_manager/server/buffered_task_store.test.ts index 70d24b235d880..45607713a3128 100644 --- a/x-pack/plugins/task_manager/server/buffered_task_store.test.ts +++ b/x-pack/plugins/task_manager/server/buffered_task_store.test.ts @@ -13,19 +13,17 @@ import { TaskStatus } from './task'; describe('Buffered Task Store', () => { test('proxies the TaskStore for `maxAttempts` and `remove`', async () => { - const taskStore = taskStoreMock.create({ maxAttempts: 10 }); + const taskStore = taskStoreMock.create(); taskStore.bulkUpdate.mockResolvedValue([]); const bufferedStore = new BufferedTaskStore(taskStore, {}); - expect(bufferedStore.maxAttempts).toEqual(10); - bufferedStore.remove('1'); expect(taskStore.remove).toHaveBeenCalledWith('1'); }); describe('update', () => { test("proxies the TaskStore's `bulkUpdate`", async () => { - const taskStore = taskStoreMock.create({ maxAttempts: 10 }); + const taskStore = taskStoreMock.create(); const bufferedStore = new BufferedTaskStore(taskStore, {}); const task = mockTask(); @@ -37,7 +35,7 @@ describe('Buffered Task Store', () => { }); test('handles partially successfull bulkUpdates resolving each call appropriately', async () => { - const taskStore = taskStoreMock.create({ maxAttempts: 10 }); + const taskStore = taskStoreMock.create(); const bufferedStore = new BufferedTaskStore(taskStore, {}); const tasks = [mockTask(), mockTask(), mockTask()]; @@ -61,7 +59,7 @@ describe('Buffered Task Store', () => { }); test('handles multiple items with the same id', async () => { - const taskStore = taskStoreMock.create({ maxAttempts: 10 }); + const taskStore = taskStoreMock.create(); const bufferedStore = new BufferedTaskStore(taskStore, {}); const duplicateIdTask = mockTask(); diff --git a/x-pack/plugins/task_manager/server/buffered_task_store.ts b/x-pack/plugins/task_manager/server/buffered_task_store.ts index 4e4a533303867..ca735dd6f3638 100644 --- a/x-pack/plugins/task_manager/server/buffered_task_store.ts +++ b/x-pack/plugins/task_manager/server/buffered_task_store.ts @@ -26,10 +26,6 @@ export class BufferedTaskStore implements Updatable { ); } - public get maxAttempts(): number { - return this.taskStore.maxAttempts; - } - public async update(doc: ConcreteTaskInstance): Promise { return unwrapPromise(this.bufferedUpdate(doc)); } diff --git a/x-pack/plugins/task_manager/server/lib/fill_pool.test.ts b/x-pack/plugins/task_manager/server/lib/fill_pool.test.ts index 79a0d2f690042..8e0396a453b3d 100644 --- a/x-pack/plugins/task_manager/server/lib/fill_pool.test.ts +++ b/x-pack/plugins/task_manager/server/lib/fill_pool.test.ts @@ -10,27 +10,32 @@ import sinon from 'sinon'; import { fillPool, FillPoolResult } from './fill_pool'; import { TaskPoolRunResult } from '../task_pool'; import { asOk, Result } from './result_type'; -import { ClaimOwnershipResult } from '../task_store'; import { ConcreteTaskInstance, TaskStatus } from '../task'; import { TaskManagerRunner } from '../task_running/task_runner'; +import { from, Observable } from 'rxjs'; +import { ClaimOwnershipResult } from '../queries/task_claiming'; jest.mock('../task_running/task_runner'); describe('fillPool', () => { function mockFetchAvailableTasks( tasksToMock: number[][] - ): () => Promise> { - const tasks: ConcreteTaskInstance[][] = tasksToMock.map((ids) => mockTaskInstances(ids)); - let index = 0; - return async () => - asOk({ - stats: { - tasksUpdated: tasks[index + 1]?.length ?? 0, - tasksConflicted: 0, - tasksClaimed: 0, - }, - docs: tasks[index++] || [], - }); + ): () => Observable> { + const claimCycles: ConcreteTaskInstance[][] = tasksToMock.map((ids) => mockTaskInstances(ids)); + return () => + from( + claimCycles.map((tasks) => + asOk({ + stats: { + tasksUpdated: tasks?.length ?? 0, + tasksConflicted: 0, + tasksClaimed: 0, + tasksRejected: 0, + }, + docs: tasks, + }) + ) + ); } const mockTaskInstances = (ids: number[]): ConcreteTaskInstance[] => @@ -51,7 +56,7 @@ describe('fillPool', () => { ownerId: null, })); - test('stops filling when pool runs all claimed tasks, even if there is more capacity', async () => { + test('fills task pool with all claimed tasks until fetchAvailableTasks stream closes', async () => { const tasks = [ [1, 2, 3], [4, 5], @@ -62,21 +67,7 @@ describe('fillPool', () => { await fillPool(fetchAvailableTasks, converter, run); - expect(_.flattenDeep(run.args)).toEqual(mockTaskInstances([1, 2, 3])); - }); - - test('stops filling when the pool has no more capacity', async () => { - const tasks = [ - [1, 2, 3], - [4, 5], - ]; - const fetchAvailableTasks = mockFetchAvailableTasks(tasks); - const run = sinon.spy(async () => TaskPoolRunResult.RanOutOfCapacity); - const converter = _.identity; - - await fillPool(fetchAvailableTasks, converter, run); - - expect(_.flattenDeep(run.args)).toEqual(mockTaskInstances([1, 2, 3])); + expect(_.flattenDeep(run.args)).toEqual(mockTaskInstances([1, 2, 3, 4, 5])); }); test('calls the converter on the records prior to running', async () => { @@ -91,7 +82,7 @@ describe('fillPool', () => { await fillPool(fetchAvailableTasks, converter, run); - expect(_.flattenDeep(run.args)).toEqual(['1', '2', '3']); + expect(_.flattenDeep(run.args)).toEqual(['1', '2', '3', '4', '5']); }); describe('error handling', () => { @@ -101,7 +92,10 @@ describe('fillPool', () => { (instance.id as unknown) as TaskManagerRunner; try { - const fetchAvailableTasks = async () => Promise.reject('fetch is not working'); + const fetchAvailableTasks = () => + new Observable>((obs) => + obs.error('fetch is not working') + ); await fillPool(fetchAvailableTasks, converter, run); } catch (err) { diff --git a/x-pack/plugins/task_manager/server/lib/fill_pool.ts b/x-pack/plugins/task_manager/server/lib/fill_pool.ts index 45a33081bde51..c9050ebb75d69 100644 --- a/x-pack/plugins/task_manager/server/lib/fill_pool.ts +++ b/x-pack/plugins/task_manager/server/lib/fill_pool.ts @@ -6,12 +6,14 @@ */ import { performance } from 'perf_hooks'; +import { Observable } from 'rxjs'; +import { concatMap, last } from 'rxjs/operators'; +import { ClaimOwnershipResult } from '../queries/task_claiming'; import { ConcreteTaskInstance } from '../task'; import { WithTaskTiming, startTaskTimer } from '../task_events'; import { TaskPoolRunResult } from '../task_pool'; import { TaskManagerRunner } from '../task_running'; -import { ClaimOwnershipResult } from '../task_store'; -import { Result, map } from './result_type'; +import { Result, map as mapResult, asErr, asOk } from './result_type'; export enum FillPoolResult { Failed = 'Failed', @@ -22,6 +24,17 @@ export enum FillPoolResult { PoolFilled = 'PoolFilled', } +type FillPoolAndRunResult = Result< + { + result: TaskPoolRunResult; + stats?: ClaimOwnershipResult['stats']; + }, + { + result: FillPoolResult; + stats?: ClaimOwnershipResult['stats']; + } +>; + export type ClaimAndFillPoolResult = Partial> & { result: FillPoolResult; }; @@ -40,52 +53,81 @@ export type TimedFillPoolResult = WithTaskTiming; * @param converter - a function that converts task records to the appropriate task runner */ export async function fillPool( - fetchAvailableTasks: () => Promise>, + fetchAvailableTasks: () => Observable>, converter: (taskInstance: ConcreteTaskInstance) => TaskManagerRunner, run: (tasks: TaskManagerRunner[]) => Promise ): Promise { performance.mark('fillPool.start'); - const stopTaskTimer = startTaskTimer(); - const augmentTimingTo = ( - result: FillPoolResult, - stats?: ClaimOwnershipResult['stats'] - ): TimedFillPoolResult => ({ - result, - stats, - timing: stopTaskTimer(), - }); - return map>( - await fetchAvailableTasks(), - async ({ docs, stats }) => { - if (!docs.length) { - performance.mark('fillPool.bailNoTasks'); - performance.measure( - 'fillPool.activityDurationUntilNoTasks', - 'fillPool.start', - 'fillPool.bailNoTasks' - ); - return augmentTimingTo(FillPoolResult.NoTasksClaimed, stats); - } - - const tasks = docs.map(converter); - - switch (await run(tasks)) { - case TaskPoolRunResult.RanOutOfCapacity: - performance.mark('fillPool.bailExhaustedCapacity'); - performance.measure( - 'fillPool.activityDurationUntilExhaustedCapacity', - 'fillPool.start', - 'fillPool.bailExhaustedCapacity' + return new Promise((resolve, reject) => { + const stopTaskTimer = startTaskTimer(); + const augmentTimingTo = ( + result: FillPoolResult, + stats?: ClaimOwnershipResult['stats'] + ): TimedFillPoolResult => ({ + result, + stats, + timing: stopTaskTimer(), + }); + fetchAvailableTasks() + .pipe( + // each ClaimOwnershipResult will be sequencially consumed an ran using the `run` handler + concatMap(async (res) => + mapResult>( + res, + async ({ docs, stats }) => { + if (!docs.length) { + performance.mark('fillPool.bailNoTasks'); + performance.measure( + 'fillPool.activityDurationUntilNoTasks', + 'fillPool.start', + 'fillPool.bailNoTasks' + ); + return asOk({ result: TaskPoolRunResult.NoTaskWereRan, stats }); + } + return asOk( + await run(docs.map(converter)).then((runResult) => ({ + result: runResult, + stats, + })) + ); + }, + async (fillPoolResult) => asErr({ result: fillPoolResult }) + ) + ), + // when the final call to `run` completes, we'll complete the stream and emit the + // final accumulated result + last() + ) + .subscribe( + (claimResults) => { + resolve( + mapResult( + claimResults, + ({ result, stats }) => { + switch (result) { + case TaskPoolRunResult.RanOutOfCapacity: + performance.mark('fillPool.bailExhaustedCapacity'); + performance.measure( + 'fillPool.activityDurationUntilExhaustedCapacity', + 'fillPool.start', + 'fillPool.bailExhaustedCapacity' + ); + return augmentTimingTo(FillPoolResult.RanOutOfCapacity, stats); + case TaskPoolRunResult.RunningAtCapacity: + performance.mark('fillPool.cycle'); + return augmentTimingTo(FillPoolResult.RunningAtCapacity, stats); + case TaskPoolRunResult.NoTaskWereRan: + return augmentTimingTo(FillPoolResult.NoTasksClaimed, stats); + default: + performance.mark('fillPool.cycle'); + return augmentTimingTo(FillPoolResult.PoolFilled, stats); + } + }, + ({ result, stats }) => augmentTimingTo(result, stats) + ) ); - return augmentTimingTo(FillPoolResult.RanOutOfCapacity, stats); - case TaskPoolRunResult.RunningAtCapacity: - performance.mark('fillPool.cycle'); - return augmentTimingTo(FillPoolResult.RunningAtCapacity, stats); - default: - performance.mark('fillPool.cycle'); - return augmentTimingTo(FillPoolResult.PoolFilled, stats); - } - }, - async (result) => augmentTimingTo(result) - ); + }, + (err) => reject(err) + ); + }); } diff --git a/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.test.ts b/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.test.ts index 5c32c3e7225c4..7040d5acd4eaf 100644 --- a/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.test.ts +++ b/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.test.ts @@ -537,6 +537,7 @@ describe('Task Run Statistics', () => { asTaskPollingCycleEvent(asOk({ result: FillPoolResult.NoTasksClaimed, timing })) ); events$.next(asTaskManagerStatEvent('pollingDelay', asOk(0))); + events$.next(asTaskManagerStatEvent('claimDuration', asOk(10))); events$.next( asTaskPollingCycleEvent(asOk({ result: FillPoolResult.NoTasksClaimed, timing })) ); diff --git a/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.ts b/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.ts index 4b7bdf595f1f5..3185d3c449c32 100644 --- a/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.ts +++ b/x-pack/plugins/task_manager/server/monitoring/task_run_statistics.ts @@ -19,6 +19,7 @@ import { RanTask, TaskTiming, isTaskManagerStatEvent, + TaskManagerStat, } from '../task_events'; import { isOk, Ok, unwrap } from '../lib/result_type'; import { ConcreteTaskInstance } from '../task'; @@ -39,6 +40,7 @@ interface FillPoolStat extends JsonObject { last_successful_poll: string; last_polling_delay: string; duration: number[]; + claim_duration: number[]; claim_conflicts: number[]; claim_mismatches: number[]; result_frequency_percent_as_number: FillPoolResult[]; @@ -51,6 +53,7 @@ interface ExecutionStat extends JsonObject { export interface TaskRunStat extends JsonObject { drift: number[]; + drift_by_type: Record; load: number[]; execution: ExecutionStat; polling: Omit & @@ -125,6 +128,7 @@ export function createTaskRunAggregator( const resultFrequencyQueue = createRunningAveragedStat(runningAverageWindowSize); const pollingDurationQueue = createRunningAveragedStat(runningAverageWindowSize); + const claimDurationQueue = createRunningAveragedStat(runningAverageWindowSize); const claimConflictsQueue = createRunningAveragedStat(runningAverageWindowSize); const claimMismatchesQueue = createRunningAveragedStat(runningAverageWindowSize); const taskPollingEvents$: Observable> = combineLatest([ @@ -168,10 +172,26 @@ export function createTaskRunAggregator( ), map(() => new Date().toISOString()) ), + // get duration of task claim stage in polling + taskPollingLifecycle.events.pipe( + filter( + (taskEvent: TaskLifecycleEvent) => + isTaskManagerStatEvent(taskEvent) && + taskEvent.id === 'claimDuration' && + isOk(taskEvent.event) + ), + map((claimDurationEvent) => { + const duration = ((claimDurationEvent as TaskManagerStat).event as Ok).value; + return { + claimDuration: duration ? claimDurationQueue(duration) : claimDurationQueue(), + }; + }) + ), ]).pipe( - map(([{ polling }, pollingDelay]) => ({ + map(([{ polling }, pollingDelay, { claimDuration }]) => ({ polling: { last_polling_delay: pollingDelay, + claim_duration: claimDuration, ...polling, }, })) @@ -179,13 +199,18 @@ export function createTaskRunAggregator( return combineLatest([ taskRunEvents$.pipe( - startWith({ drift: [], execution: { duration: {}, result_frequency_percent_as_number: {} } }) + startWith({ + drift: [], + drift_by_type: {}, + execution: { duration: {}, result_frequency_percent_as_number: {} }, + }) ), taskManagerLoadStatEvents$.pipe(startWith({ load: [] })), taskPollingEvents$.pipe( startWith({ polling: { duration: [], + claim_duration: [], claim_conflicts: [], claim_mismatches: [], result_frequency_percent_as_number: [], @@ -218,6 +243,7 @@ function hasTiming(taskEvent: TaskLifecycleEvent) { function createTaskRunEventToStat(runningAverageWindowSize: number) { const driftQueue = createRunningAveragedStat(runningAverageWindowSize); + const driftByTaskQueue = createMapOfRunningAveragedStats(runningAverageWindowSize); const taskRunDurationQueue = createMapOfRunningAveragedStats(runningAverageWindowSize); const resultFrequencyQueue = createMapOfRunningAveragedStats( runningAverageWindowSize @@ -226,13 +252,17 @@ function createTaskRunEventToStat(runningAverageWindowSize: number) { task: ConcreteTaskInstance, timing: TaskTiming, result: TaskRunResult - ): Omit => ({ - drift: driftQueue(timing!.start - task.runAt.getTime()), - execution: { - duration: taskRunDurationQueue(task.taskType, timing!.stop - timing!.start), - result_frequency_percent_as_number: resultFrequencyQueue(task.taskType, result), - }, - }); + ): Omit => { + const drift = timing!.start - task.runAt.getTime(); + return { + drift: driftQueue(drift), + drift_by_type: driftByTaskQueue(task.taskType, drift), + execution: { + duration: taskRunDurationQueue(task.taskType, timing!.stop - timing!.start), + result_frequency_percent_as_number: resultFrequencyQueue(task.taskType, result), + }, + }; + }; } const DEFAULT_TASK_RUN_FREQUENCIES = { @@ -258,11 +288,15 @@ export function summarizeTaskRunStat( // eslint-disable-next-line @typescript-eslint/naming-convention last_polling_delay, duration: pollingDuration, + // eslint-disable-next-line @typescript-eslint/naming-convention + claim_duration, result_frequency_percent_as_number: pollingResultFrequency, claim_conflicts: claimConflicts, claim_mismatches: claimMismatches, }, drift, + // eslint-disable-next-line @typescript-eslint/naming-convention + drift_by_type, load, execution: { duration, result_frequency_percent_as_number: executionResultFrequency }, }: TaskRunStat, @@ -273,6 +307,9 @@ export function summarizeTaskRunStat( polling: { ...(last_successful_poll ? { last_successful_poll } : {}), ...(last_polling_delay ? { last_polling_delay } : {}), + ...(claim_duration + ? { claim_duration: calculateRunningAverage(claim_duration as number[]) } + : {}), duration: calculateRunningAverage(pollingDuration as number[]), claim_conflicts: calculateRunningAverage(claimConflicts as number[]), claim_mismatches: calculateRunningAverage(claimMismatches as number[]), @@ -282,6 +319,7 @@ export function summarizeTaskRunStat( }, }, drift: calculateRunningAverage(drift), + drift_by_type: mapValues(drift_by_type, (typedDrift) => calculateRunningAverage(typedDrift)), load: calculateRunningAverage(load), execution: { duration: mapValues(duration, (typedDurations) => calculateRunningAverage(typedDurations)), diff --git a/x-pack/plugins/task_manager/server/plugin.test.ts b/x-pack/plugins/task_manager/server/plugin.test.ts index 0a879ce92cba6..45db18a3e8385 100644 --- a/x-pack/plugins/task_manager/server/plugin.test.ts +++ b/x-pack/plugins/task_manager/server/plugin.test.ts @@ -70,6 +70,15 @@ describe('TaskManagerPlugin', () => { const setupApi = await taskManagerPlugin.setup(coreMock.createSetup()); + // we only start a poller if we have task types that we support and we track + // phases (moving from Setup to Start) based on whether the poller is working + setupApi.registerTaskDefinitions({ + setupTimeType: { + title: 'setupTimeType', + createTaskRunner: () => ({ async run() {} }), + }, + }); + await taskManagerPlugin.start(coreMock.createStart()); expect(() => diff --git a/x-pack/plugins/task_manager/server/plugin.ts b/x-pack/plugins/task_manager/server/plugin.ts index 149d111b08f02..507a021214a90 100644 --- a/x-pack/plugins/task_manager/server/plugin.ts +++ b/x-pack/plugins/task_manager/server/plugin.ts @@ -16,13 +16,12 @@ import { ServiceStatusLevels, CoreStatus, } from '../../../../src/core/server'; -import { TaskDefinition } from './task'; import { TaskPollingLifecycle } from './polling_lifecycle'; import { TaskManagerConfig } from './config'; import { createInitialMiddleware, addMiddlewareToChain, Middleware } from './lib/middleware'; import { removeIfExists } from './lib/remove_if_exists'; import { setupSavedObjects } from './saved_objects'; -import { TaskTypeDictionary } from './task_type_dictionary'; +import { TaskDefinitionRegistry, TaskTypeDictionary } from './task_type_dictionary'; import { FetchResult, SearchOpts, TaskStore } from './task_store'; import { createManagedConfiguration } from './lib/create_managed_configuration'; import { TaskScheduling } from './task_scheduling'; @@ -100,7 +99,7 @@ export class TaskManagerPlugin this.assertStillInSetup('add Middleware'); this.middleware = addMiddlewareToChain(this.middleware, middleware); }, - registerTaskDefinitions: (taskDefinition: Record) => { + registerTaskDefinitions: (taskDefinition: TaskDefinitionRegistry) => { this.assertStillInSetup('register task definitions'); this.definitions.registerTaskDefinitions(taskDefinition); }, @@ -110,12 +109,12 @@ export class TaskManagerPlugin public start({ savedObjects, elasticsearch }: CoreStart): TaskManagerStartContract { const savedObjectsRepository = savedObjects.createInternalRepository(['task']); + const serializer = savedObjects.createSerializer(); const taskStore = new TaskStore({ - serializer: savedObjects.createSerializer(), + serializer, savedObjectsRepository, esClient: elasticsearch.createClient('taskManager').asInternalUser, index: this.config!.index, - maxAttempts: this.config!.max_attempts, definitions: this.definitions, taskManagerId: `kibana:${this.taskManagerId!}`, }); @@ -151,6 +150,7 @@ export class TaskManagerPlugin taskStore, middleware: this.middleware, taskPollingLifecycle: this.taskPollingLifecycle, + definitions: this.definitions, }); return { diff --git a/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.test.ts b/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.test.ts index d4617d6549d60..f3af6f50336ea 100644 --- a/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.test.ts +++ b/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.test.ts @@ -64,6 +64,7 @@ describe('delayOnClaimConflicts', () => { tasksUpdated: 0, tasksConflicted: 8, tasksClaimed: 0, + tasksRejected: 0, }, docs: [], }) @@ -79,6 +80,63 @@ describe('delayOnClaimConflicts', () => { }) ); + test( + 'emits delay only once, no mater how many subscribers there are', + fakeSchedulers(async () => { + const taskLifecycleEvents$ = new Subject(); + + const delays$ = delayOnClaimConflicts(of(10), of(100), taskLifecycleEvents$, 80, 2); + + const firstSubscriber$ = delays$.pipe(take(2), bufferCount(2)).toPromise(); + const secondSubscriber$ = delays$.pipe(take(2), bufferCount(2)).toPromise(); + + taskLifecycleEvents$.next( + asTaskPollingCycleEvent( + asOk({ + result: FillPoolResult.PoolFilled, + stats: { + tasksUpdated: 0, + tasksConflicted: 8, + tasksClaimed: 0, + tasksRejected: 0, + }, + docs: [], + }) + ) + ); + + const thirdSubscriber$ = delays$.pipe(take(2), bufferCount(2)).toPromise(); + + taskLifecycleEvents$.next( + asTaskPollingCycleEvent( + asOk({ + result: FillPoolResult.PoolFilled, + stats: { + tasksUpdated: 0, + tasksConflicted: 10, + tasksClaimed: 0, + tasksRejected: 0, + }, + docs: [], + }) + ) + ); + + // should get the initial value of 0 delay + const [initialDelay, firstRandom] = await firstSubscriber$; + // should get the 0 delay (as a replay), which was the last value plus the first random value + const [initialDelayInSecondSub, firstRandomInSecondSub] = await secondSubscriber$; + // should get the first random value (as a replay) and the next random value + const [firstRandomInThirdSub, secondRandomInThirdSub] = await thirdSubscriber$; + + expect(initialDelay).toEqual(0); + expect(initialDelayInSecondSub).toEqual(0); + expect(firstRandom).toEqual(firstRandomInSecondSub); + expect(firstRandomInSecondSub).toEqual(firstRandomInThirdSub); + expect(secondRandomInThirdSub).toBeGreaterThanOrEqual(0); + }) + ); + test( 'doesnt emit a new delay when conflicts have reduced', fakeSchedulers(async () => { @@ -107,6 +165,7 @@ describe('delayOnClaimConflicts', () => { tasksUpdated: 0, tasksConflicted: 8, tasksClaimed: 0, + tasksRejected: 0, }, docs: [], }) @@ -127,6 +186,7 @@ describe('delayOnClaimConflicts', () => { tasksUpdated: 0, tasksConflicted: 7, tasksClaimed: 0, + tasksRejected: 0, }, docs: [], }) @@ -145,6 +205,7 @@ describe('delayOnClaimConflicts', () => { tasksUpdated: 0, tasksConflicted: 9, tasksClaimed: 0, + tasksRejected: 0, }, docs: [], }) diff --git a/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.ts b/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.ts index 73e7052b65a69..6d7cb77625b58 100644 --- a/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.ts +++ b/x-pack/plugins/task_manager/server/polling/delay_on_claim_conflicts.ts @@ -11,7 +11,7 @@ import stats from 'stats-lite'; import { isNumber, random } from 'lodash'; -import { merge, of, Observable, combineLatest } from 'rxjs'; +import { merge, of, Observable, combineLatest, ReplaySubject } from 'rxjs'; import { filter, map } from 'rxjs/operators'; import { Option, none, some, isSome, Some } from 'fp-ts/lib/Option'; import { isOk } from '../lib/result_type'; @@ -32,7 +32,9 @@ export function delayOnClaimConflicts( runningAverageWindowSize: number ): Observable { const claimConflictQueue = createRunningAveragedStat(runningAverageWindowSize); - return merge( + // return a subject to allow multicast and replay the last value to new subscribers + const multiCastDelays$ = new ReplaySubject(1); + merge( of(0), combineLatest([ maxWorkersConfiguration$, @@ -70,5 +72,9 @@ export function delayOnClaimConflicts( return random(pollInterval * 0.25, pollInterval * 0.75, false); }) ) - ); + ).subscribe((delay) => { + multiCastDelays$.next(delay); + }); + + return multiCastDelays$; } diff --git a/x-pack/plugins/task_manager/server/polling_lifecycle.test.ts b/x-pack/plugins/task_manager/server/polling_lifecycle.test.ts index 9f79445070237..63d7f6de81801 100644 --- a/x-pack/plugins/task_manager/server/polling_lifecycle.test.ts +++ b/x-pack/plugins/task_manager/server/polling_lifecycle.test.ts @@ -7,17 +7,30 @@ import _ from 'lodash'; import sinon from 'sinon'; -import { of, Subject } from 'rxjs'; +import { Observable, of, Subject } from 'rxjs'; import { TaskPollingLifecycle, claimAvailableTasks } from './polling_lifecycle'; import { createInitialMiddleware } from './lib/middleware'; import { TaskTypeDictionary } from './task_type_dictionary'; import { taskStoreMock } from './task_store.mock'; import { mockLogger } from './test_utils'; +import { taskClaimingMock } from './queries/task_claiming.mock'; +import { TaskClaiming, ClaimOwnershipResult } from './queries/task_claiming'; +import type { TaskClaiming as TaskClaimingClass } from './queries/task_claiming'; +import { asOk, Err, isErr, isOk, Result } from './lib/result_type'; +import { FillPoolResult } from './lib/fill_pool'; + +let mockTaskClaiming = taskClaimingMock.create({}); +jest.mock('./queries/task_claiming', () => { + return { + TaskClaiming: jest.fn().mockImplementation(() => { + return mockTaskClaiming; + }), + }; +}); describe('TaskPollingLifecycle', () => { let clock: sinon.SinonFakeTimers; - const taskManagerLogger = mockLogger(); const mockTaskStore = taskStoreMock.create({}); const taskManagerOpts = { @@ -50,8 +63,9 @@ describe('TaskPollingLifecycle', () => { }; beforeEach(() => { + mockTaskClaiming = taskClaimingMock.create({}); + (TaskClaiming as jest.Mock).mockClear(); clock = sinon.useFakeTimers(); - taskManagerOpts.definitions = new TaskTypeDictionary(taskManagerLogger); }); afterEach(() => clock.restore()); @@ -60,17 +74,58 @@ describe('TaskPollingLifecycle', () => { test('begins polling once the ES and SavedObjects services are available', () => { const elasticsearchAndSOAvailability$ = new Subject(); new TaskPollingLifecycle({ - elasticsearchAndSOAvailability$, ...taskManagerOpts, + elasticsearchAndSOAvailability$, }); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).not.toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).not.toHaveBeenCalled(); elasticsearchAndSOAvailability$.next(true); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).toHaveBeenCalled(); + }); + + test('provides TaskClaiming with the capacity available', () => { + const elasticsearchAndSOAvailability$ = new Subject(); + const maxWorkers$ = new Subject(); + taskManagerOpts.definitions.registerTaskDefinitions({ + report: { + title: 'report', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + quickReport: { + title: 'quickReport', + maxConcurrency: 5, + createTaskRunner: jest.fn(), + }, + }); + + new TaskPollingLifecycle({ + ...taskManagerOpts, + elasticsearchAndSOAvailability$, + maxWorkersConfiguration$: maxWorkers$, + }); + + const taskClaimingGetCapacity = (TaskClaiming as jest.Mock).mock + .calls[0][0].getCapacity; + + maxWorkers$.next(20); + expect(taskClaimingGetCapacity()).toEqual(20); + expect(taskClaimingGetCapacity('report')).toEqual(1); + expect(taskClaimingGetCapacity('quickReport')).toEqual(5); + + maxWorkers$.next(30); + expect(taskClaimingGetCapacity()).toEqual(30); + expect(taskClaimingGetCapacity('report')).toEqual(1); + expect(taskClaimingGetCapacity('quickReport')).toEqual(5); + + maxWorkers$.next(2); + expect(taskClaimingGetCapacity()).toEqual(2); + expect(taskClaimingGetCapacity('report')).toEqual(1); + expect(taskClaimingGetCapacity('quickReport')).toEqual(2); }); }); @@ -85,13 +140,13 @@ describe('TaskPollingLifecycle', () => { elasticsearchAndSOAvailability$.next(true); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).toHaveBeenCalled(); elasticsearchAndSOAvailability$.next(false); - mockTaskStore.claimAvailableTasks.mockClear(); + mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable.mockClear(); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).not.toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).not.toHaveBeenCalled(); }); test('restarts polling once the ES and SavedObjects services become available again', () => { @@ -104,68 +159,64 @@ describe('TaskPollingLifecycle', () => { elasticsearchAndSOAvailability$.next(true); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).toHaveBeenCalled(); elasticsearchAndSOAvailability$.next(false); - mockTaskStore.claimAvailableTasks.mockClear(); + mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable.mockClear(); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).not.toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).not.toHaveBeenCalled(); elasticsearchAndSOAvailability$.next(true); clock.tick(150); - expect(mockTaskStore.claimAvailableTasks).toHaveBeenCalled(); + expect(mockTaskClaiming.claimAvailableTasksIfCapacityIsAvailable).toHaveBeenCalled(); }); }); describe('claimAvailableTasks', () => { - test('should claim Available Tasks when there are available workers', () => { - const logger = mockLogger(); - const claim = jest.fn(() => - Promise.resolve({ - docs: [], - stats: { tasksUpdated: 0, tasksConflicted: 0, tasksClaimed: 0 }, - }) - ); - - const availableWorkers = 1; - - claimAvailableTasks([], claim, availableWorkers, logger); - - expect(claim).toHaveBeenCalledTimes(1); - }); - - test('should not claim Available Tasks when there are no available workers', () => { + test('should claim Available Tasks when there are available workers', async () => { const logger = mockLogger(); - const claim = jest.fn(() => - Promise.resolve({ - docs: [], - stats: { tasksUpdated: 0, tasksConflicted: 0, tasksClaimed: 0 }, - }) + const taskClaiming = taskClaimingMock.create({}); + taskClaiming.claimAvailableTasksIfCapacityIsAvailable.mockImplementation(() => + of( + asOk({ + docs: [], + stats: { tasksUpdated: 0, tasksConflicted: 0, tasksClaimed: 0, tasksRejected: 0 }, + }) + ) ); - const availableWorkers = 0; + expect( + isOk(await getFirstAsPromise(claimAvailableTasks([], taskClaiming, logger))) + ).toBeTruthy(); - claimAvailableTasks([], claim, availableWorkers, logger); - - expect(claim).not.toHaveBeenCalled(); + expect(taskClaiming.claimAvailableTasksIfCapacityIsAvailable).toHaveBeenCalledTimes(1); }); /** * This handles the case in which Elasticsearch has had inline script disabled. * This is achieved by setting the `script.allowed_types` flag on Elasticsearch to `none` */ - test('handles failure due to inline scripts being disabled', () => { + test('handles failure due to inline scripts being disabled', async () => { const logger = mockLogger(); - const claim = jest.fn(() => { - throw Object.assign(new Error(), { - response: - '{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":".kibana_task_manager_1","node":"24A4QbjHSK6prvtopAKLKw","reason":{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts"}}],"caused_by":{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts","caused_by":{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts"}}},"status":400}', - }); - }); + const taskClaiming = taskClaimingMock.create({}); + taskClaiming.claimAvailableTasksIfCapacityIsAvailable.mockImplementation( + () => + new Observable>((observer) => { + observer.error( + Object.assign(new Error(), { + response: + '{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":".kibana_task_manager_1","node":"24A4QbjHSK6prvtopAKLKw","reason":{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts"}}],"caused_by":{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts","caused_by":{"type":"illegal_argument_exception","reason":"cannot execute [inline] scripts"}}},"status":400}', + }) + ); + }) + ); + + const err = await getFirstAsPromise(claimAvailableTasks([], taskClaiming, logger)); - claimAvailableTasks([], claim, 10, logger); + expect(isErr(err)).toBeTruthy(); + expect((err as Err).error).toEqual(FillPoolResult.Failed); expect(logger.warn).toHaveBeenCalledTimes(1); expect(logger.warn).toHaveBeenCalledWith( @@ -174,3 +225,9 @@ describe('TaskPollingLifecycle', () => { }); }); }); + +function getFirstAsPromise(obs$: Observable): Promise { + return new Promise((resolve, reject) => { + obs$.subscribe(resolve, reject); + }); +} diff --git a/x-pack/plugins/task_manager/server/polling_lifecycle.ts b/x-pack/plugins/task_manager/server/polling_lifecycle.ts index db8eeaaf78dee..260f5ccc70f53 100644 --- a/x-pack/plugins/task_manager/server/polling_lifecycle.ts +++ b/x-pack/plugins/task_manager/server/polling_lifecycle.ts @@ -6,15 +6,12 @@ */ import { Subject, Observable, Subscription } from 'rxjs'; - -import { performance } from 'perf_hooks'; - import { pipe } from 'fp-ts/lib/pipeable'; import { Option, some, map as mapOptional } from 'fp-ts/lib/Option'; import { tap } from 'rxjs/operators'; import { Logger } from '../../../../src/core/server'; -import { Result, asErr, mapErr, asOk, map } from './lib/result_type'; +import { Result, asErr, mapErr, asOk, map, mapOk } from './lib/result_type'; import { ManagedConfiguration } from './lib/create_managed_configuration'; import { TaskManagerConfig } from './config'; @@ -41,11 +38,12 @@ import { } from './polling'; import { TaskPool } from './task_pool'; import { TaskManagerRunner, TaskRunner } from './task_running'; -import { TaskStore, OwnershipClaimingOpts, ClaimOwnershipResult } from './task_store'; +import { TaskStore } from './task_store'; import { identifyEsError } from './lib/identify_es_error'; import { BufferedTaskStore } from './buffered_task_store'; import { TaskTypeDictionary } from './task_type_dictionary'; import { delayOnClaimConflicts } from './polling'; +import { TaskClaiming, ClaimOwnershipResult } from './queries/task_claiming'; export type TaskPollingLifecycleOpts = { logger: Logger; @@ -71,6 +69,7 @@ export class TaskPollingLifecycle { private definitions: TaskTypeDictionary; private store: TaskStore; + private taskClaiming: TaskClaiming; private bufferedStore: BufferedTaskStore; private logger: Logger; @@ -106,8 +105,6 @@ export class TaskPollingLifecycle { this.store = taskStore; const emitEvent = (event: TaskLifecycleEvent) => this.events$.next(event); - // pipe store events into the lifecycle event stream - this.store.events.subscribe(emitEvent); this.bufferedStore = new BufferedTaskStore(this.store, { bufferMaxOperations: config.max_workers, @@ -120,6 +117,26 @@ export class TaskPollingLifecycle { }); this.pool.load.subscribe(emitEvent); + this.taskClaiming = new TaskClaiming({ + taskStore, + maxAttempts: config.max_attempts, + definitions, + logger: this.logger, + getCapacity: (taskType?: string) => + taskType && this.definitions.get(taskType)?.maxConcurrency + ? Math.max( + Math.min( + this.pool.availableWorkers, + this.definitions.get(taskType)!.maxConcurrency! - + this.pool.getOccupiedWorkersByType(taskType) + ), + 0 + ) + : this.pool.availableWorkers, + }); + // pipe taskClaiming events into the lifecycle event stream + this.taskClaiming.events.subscribe(emitEvent); + const { max_poll_inactivity_cycles: maxPollInactivityCycles, poll_interval: pollInterval, @@ -199,6 +216,7 @@ export class TaskPollingLifecycle { beforeRun: this.middleware.beforeRun, beforeMarkRunning: this.middleware.beforeMarkRunning, onTaskEvent: this.emitEvent, + defaultMaxAttempts: this.taskClaiming.maxAttempts, }); }; @@ -212,9 +230,18 @@ export class TaskPollingLifecycle { () => claimAvailableTasks( tasksToClaim.splice(0, this.pool.availableWorkers), - this.store.claimAvailableTasks, - this.pool.availableWorkers, + this.taskClaiming, this.logger + ).pipe( + tap( + mapOk(({ timing }: ClaimOwnershipResult) => { + if (timing) { + this.emitEvent( + asTaskManagerStatEvent('claimDuration', asOk(timing.stop - timing.start)) + ); + } + }) + ) ), // wrap each task in a Task Runner this.createTaskRunnerForTask, @@ -252,59 +279,40 @@ export class TaskPollingLifecycle { } } -export async function claimAvailableTasks( +export function claimAvailableTasks( claimTasksById: string[], - claim: (opts: OwnershipClaimingOpts) => Promise, - availableWorkers: number, + taskClaiming: TaskClaiming, logger: Logger -): Promise> { - if (availableWorkers > 0) { - performance.mark('claimAvailableTasks_start'); - - try { - const claimResult = await claim({ - size: availableWorkers, +): Observable> { + return new Observable((observer) => { + taskClaiming + .claimAvailableTasksIfCapacityIsAvailable({ claimOwnershipUntil: intervalFromNow('30s')!, claimTasksById, - }); - const { - docs, - stats: { tasksClaimed }, - } = claimResult; - - if (tasksClaimed === 0) { - performance.mark('claimAvailableTasks.noTasks'); - } - performance.mark('claimAvailableTasks_stop'); - performance.measure( - 'claimAvailableTasks', - 'claimAvailableTasks_start', - 'claimAvailableTasks_stop' + }) + .subscribe( + (claimResult) => { + observer.next(claimResult); + }, + (ex) => { + // if the `taskClaiming` stream errors out we want to catch it and see if + // we can identify the reason + // if we can - we emit an FillPoolResult error rather than erroring out the wrapping Observable + // returned by `claimAvailableTasks` + if (identifyEsError(ex).includes('cannot execute [inline] scripts')) { + logger.warn( + `Task Manager cannot operate when inline scripts are disabled in Elasticsearch` + ); + observer.next(asErr(FillPoolResult.Failed)); + observer.complete(); + } else { + // as we could't identify the reason - we'll error out the wrapping Observable too + observer.error(ex); + } + }, + () => { + observer.complete(); + } ); - - if (docs.length !== tasksClaimed) { - logger.warn( - `[Task Ownership error]: ${tasksClaimed} tasks were claimed by Kibana, but ${ - docs.length - } task(s) were fetched (${docs.map((doc) => doc.id).join(', ')})` - ); - } - return asOk(claimResult); - } catch (ex) { - if (identifyEsError(ex).includes('cannot execute [inline] scripts')) { - logger.warn( - `Task Manager cannot operate when inline scripts are disabled in Elasticsearch` - ); - return asErr(FillPoolResult.Failed); - } else { - throw ex; - } - } - } else { - performance.mark('claimAvailableTasks.noAvailableWorkers'); - logger.debug( - `[Task Ownership]: Task Manager has skipped Claiming Ownership of available tasks at it has ran out Available Workers.` - ); - return asErr(FillPoolResult.NoAvailableWorkers); - } + }); } diff --git a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts index 75b9b2cdfa977..57a4ab320367d 100644 --- a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts +++ b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts @@ -52,6 +52,7 @@ describe('mark_available_tasks_as_claimed', () => { fieldUpdates, claimTasksById || [], definitions.getAllTypes(), + [], Array.from(definitions).reduce((accumulator, [type, { maxAttempts }]) => { return { ...accumulator, [type]: maxAttempts || defaultMaxAttempts }; }, {}) @@ -116,18 +117,23 @@ if (doc['task.runAt'].size()!=0) { seq_no_primary_term: true, script: { source: ` - if (params.registeredTaskTypes.contains(ctx._source.task.taskType)) { - if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { + if (params.claimableTaskTypes.contains(ctx._source.task.taskType)) { + if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { + ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) + .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) + .join(' ')} + } else { + ctx._source.task.status = "failed"; + } + } else if (params.skippedTaskTypes.contains(ctx._source.task.taskType) && params.claimTasksById.contains(ctx._id)) { ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) .join(' ')} + } else if (!params.skippedTaskTypes.contains(ctx._source.task.taskType)) { + ctx._source.task.status = "unrecognized"; } else { - ctx._source.task.status = "failed"; - } - } else { - ctx._source.task.status = "unrecognized"; - } - `, + ctx.op = "noop"; + }`, lang: 'painless', params: { fieldUpdates: { @@ -135,7 +141,8 @@ if (doc['task.runAt'].size()!=0) { retryAt: claimOwnershipUntil, }, claimTasksById: [], - registeredTaskTypes: ['sampleTask', 'otherTask'], + claimableTaskTypes: ['sampleTask', 'otherTask'], + skippedTaskTypes: [], taskMaxAttempts: { sampleTask: 5, otherTask: 1, @@ -144,4 +151,76 @@ if (doc['task.runAt'].size()!=0) { }, }); }); + + describe(`script`, () => { + test('it supports claiming specific tasks by id', async () => { + const taskManagerId = '3478fg6-82374f6-83467gf5-384g6f'; + const claimOwnershipUntil = '2019-02-12T21:01:22.479Z'; + const fieldUpdates = { + ownerId: taskManagerId, + retryAt: claimOwnershipUntil, + }; + + const claimTasksById = [ + '33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ]; + + expect( + updateFieldsAndMarkAsFailed(fieldUpdates, claimTasksById, ['foo', 'bar'], [], { + foo: 5, + bar: 2, + }) + ).toMatchObject({ + source: ` + if (params.claimableTaskTypes.contains(ctx._source.task.taskType)) { + if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { + ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) + .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) + .join(' ')} + } else { + ctx._source.task.status = "failed"; + } + } else if (params.skippedTaskTypes.contains(ctx._source.task.taskType) && params.claimTasksById.contains(ctx._id)) { + ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) + .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) + .join(' ')} + } else if (!params.skippedTaskTypes.contains(ctx._source.task.taskType)) { + ctx._source.task.status = "unrecognized"; + } else { + ctx.op = "noop"; + }`, + lang: 'painless', + params: { + fieldUpdates, + claimTasksById: [ + '33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ], + claimableTaskTypes: ['foo', 'bar'], + skippedTaskTypes: [], + taskMaxAttempts: { + foo: 5, + bar: 2, + }, + }, + }); + }); + + test('it marks the update as a noop if the type is skipped', async () => { + const taskManagerId = '3478fg6-82374f6-83467gf5-384g6f'; + const claimOwnershipUntil = '2019-02-12T21:01:22.479Z'; + const fieldUpdates = { + ownerId: taskManagerId, + retryAt: claimOwnershipUntil, + }; + + expect( + updateFieldsAndMarkAsFailed(fieldUpdates, [], ['foo', 'bar'], [], { + foo: 5, + bar: 2, + }).source + ).toMatch(/ctx.op = "noop"/); + }); + }); }); diff --git a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts index 067de5a92adb7..8598980a4e236 100644 --- a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts +++ b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts @@ -14,6 +14,8 @@ import { mustBeAllOf, MustCondition, BoolClauseWithAnyCondition, + ShouldCondition, + FilterCondition, } from './query_clauses'; export const TaskWithSchedule: ExistsFilter = { @@ -39,14 +41,26 @@ export function taskWithLessThanMaxAttempts( }; } -export function tasksClaimedByOwner(taskManagerId: string) { +export function tasksOfType(taskTypes: string[]): ShouldCondition { + return { + bool: { + should: [...taskTypes].map((type) => ({ term: { 'task.taskType': type } })), + }, + }; +} + +export function tasksClaimedByOwner( + taskManagerId: string, + ...taskFilters: Array | ShouldCondition> +) { return mustBeAllOf( { term: { 'task.ownerId': taskManagerId, }, }, - { term: { 'task.status': 'claiming' } } + { term: { 'task.status': 'claiming' } }, + ...taskFilters ); } @@ -107,27 +121,35 @@ export const updateFieldsAndMarkAsFailed = ( [field: string]: string | number | Date; }, claimTasksById: string[], - registeredTaskTypes: string[], + claimableTaskTypes: string[], + skippedTaskTypes: string[], taskMaxAttempts: { [field: string]: number } -): ScriptClause => ({ - source: ` - if (params.registeredTaskTypes.contains(ctx._source.task.taskType)) { - if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { - ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) - .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) - .join(' ')} +): ScriptClause => { + const markAsClaimingScript = `ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) + .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) + .join(' ')}`; + return { + source: ` + if (params.claimableTaskTypes.contains(ctx._source.task.taskType)) { + if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { + ${markAsClaimingScript} + } else { + ctx._source.task.status = "failed"; + } + } else if (params.skippedTaskTypes.contains(ctx._source.task.taskType) && params.claimTasksById.contains(ctx._id)) { + ${markAsClaimingScript} + } else if (!params.skippedTaskTypes.contains(ctx._source.task.taskType)) { + ctx._source.task.status = "unrecognized"; } else { - ctx._source.task.status = "failed"; - } - } else { - ctx._source.task.status = "unrecognized"; - } - `, - lang: 'painless', - params: { - fieldUpdates, - claimTasksById, - registeredTaskTypes, - taskMaxAttempts, - }, -}); + ctx.op = "noop"; + }`, + lang: 'painless', + params: { + fieldUpdates, + claimTasksById, + claimableTaskTypes, + skippedTaskTypes, + taskMaxAttempts, + }, + }; +}; diff --git a/x-pack/plugins/task_manager/server/queries/task_claiming.mock.ts b/x-pack/plugins/task_manager/server/queries/task_claiming.mock.ts new file mode 100644 index 0000000000000..38f02780c485e --- /dev/null +++ b/x-pack/plugins/task_manager/server/queries/task_claiming.mock.ts @@ -0,0 +1,33 @@ +/* + * 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 { Observable, Subject } from 'rxjs'; +import { TaskClaim } from '../task_events'; + +import { TaskClaiming } from './task_claiming'; + +interface TaskClaimingOptions { + maxAttempts?: number; + taskManagerId?: string; + events?: Observable; +} +export const taskClaimingMock = { + create({ + maxAttempts = 0, + taskManagerId = '', + events = new Subject(), + }: TaskClaimingOptions) { + const mocked = ({ + claimAvailableTasks: jest.fn(), + claimAvailableTasksIfCapacityIsAvailable: jest.fn(), + maxAttempts, + taskManagerId, + events, + } as unknown) as jest.Mocked; + return mocked; + }, +}; diff --git a/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts b/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts new file mode 100644 index 0000000000000..bd1171d7fd2f8 --- /dev/null +++ b/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts @@ -0,0 +1,1516 @@ +/* + * 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 _ from 'lodash'; +import uuid from 'uuid'; +import { filter, take, toArray } from 'rxjs/operators'; +import { some, none } from 'fp-ts/lib/Option'; + +import { TaskStatus, ConcreteTaskInstance } from '../task'; +import { SearchOpts, StoreOpts, UpdateByQueryOpts, UpdateByQuerySearchOpts } from '../task_store'; +import { asTaskClaimEvent, ClaimTaskErr, TaskClaimErrorType, TaskEvent } from '../task_events'; +import { asOk, asErr } from '../lib/result_type'; +import { TaskTypeDictionary } from '../task_type_dictionary'; +import { BoolClauseWithAnyCondition, TermFilter } from '../queries/query_clauses'; +import { mockLogger } from '../test_utils'; +import { TaskClaiming, OwnershipClaimingOpts, TaskClaimingOpts } from './task_claiming'; +import { Observable } from 'rxjs'; +import { taskStoreMock } from '../task_store.mock'; + +const taskManagerLogger = mockLogger(); + +beforeEach(() => jest.resetAllMocks()); + +const mockedDate = new Date('2019-02-12T21:01:22.479Z'); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +(global as any).Date = class Date { + constructor() { + return mockedDate; + } + static now() { + return mockedDate.getTime(); + } +}; + +const taskDefinitions = new TaskTypeDictionary(taskManagerLogger); +taskDefinitions.registerTaskDefinitions({ + report: { + title: 'report', + createTaskRunner: jest.fn(), + }, + dernstraight: { + title: 'dernstraight', + createTaskRunner: jest.fn(), + }, + yawn: { + title: 'yawn', + createTaskRunner: jest.fn(), + }, +}); + +describe('TaskClaiming', () => { + test(`should log when a certain task type is skipped due to having a zero concurency configuration`, () => { + const definitions = new TaskTypeDictionary(mockLogger()); + definitions.registerTaskDefinitions({ + unlimited: { + title: 'unlimited', + createTaskRunner: jest.fn(), + }, + anotherUnlimited: { + title: 'anotherUnlimited', + createTaskRunner: jest.fn(), + }, + limitedToZero: { + title: 'limitedToZero', + maxConcurrency: 0, + createTaskRunner: jest.fn(), + }, + limitedToOne: { + title: 'limitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + anotherLimitedToZero: { + title: 'anotherLimitedToZero', + maxConcurrency: 0, + createTaskRunner: jest.fn(), + }, + limitedToTwo: { + title: 'limitedToTwo', + maxConcurrency: 2, + createTaskRunner: jest.fn(), + }, + }); + + new TaskClaiming({ + logger: taskManagerLogger, + definitions, + taskStore: taskStoreMock.create({ taskManagerId: '' }), + maxAttempts: 2, + getCapacity: () => 10, + }); + + expect(taskManagerLogger.info).toHaveBeenCalledTimes(1); + expect(taskManagerLogger.info.mock.calls[0][0]).toMatchInlineSnapshot( + `"Task Manager will never claim tasks of the following types as their \\"maxConcurrency\\" is set to 0: limitedToZero, anotherLimitedToZero"` + ); + }); + + describe('claimAvailableTasks', () => { + function initialiseTestClaiming({ + storeOpts = {}, + taskClaimingOpts = {}, + hits = [generateFakeTasks(1)], + versionConflicts = 2, + }: { + storeOpts: Partial; + taskClaimingOpts: Partial; + hits?: ConcreteTaskInstance[][]; + versionConflicts?: number; + }) { + const definitions = storeOpts.definitions ?? taskDefinitions; + const store = taskStoreMock.create({ taskManagerId: storeOpts.taskManagerId }); + store.convertToSavedObjectIds.mockImplementation((ids) => ids.map((id) => `task:${id}`)); + + if (hits.length === 1) { + store.fetch.mockResolvedValue({ docs: hits[0] }); + store.updateByQuery.mockResolvedValue({ + updated: hits[0].length, + version_conflicts: versionConflicts, + total: hits[0].length, + }); + } else { + for (const docs of hits) { + store.fetch.mockResolvedValueOnce({ docs }); + store.updateByQuery.mockResolvedValueOnce({ + updated: docs.length, + version_conflicts: versionConflicts, + total: docs.length, + }); + } + } + + const taskClaiming = new TaskClaiming({ + logger: taskManagerLogger, + definitions, + taskStore: store, + maxAttempts: taskClaimingOpts.maxAttempts ?? 2, + getCapacity: taskClaimingOpts.getCapacity ?? (() => 10), + ...taskClaimingOpts, + }); + + return { taskClaiming, store }; + } + + async function testClaimAvailableTasks({ + storeOpts = {}, + taskClaimingOpts = {}, + claimingOpts, + hits = [generateFakeTasks(1)], + versionConflicts = 2, + }: { + storeOpts: Partial; + taskClaimingOpts: Partial; + claimingOpts: Omit; + hits?: ConcreteTaskInstance[][]; + versionConflicts?: number; + }) { + const getCapacity = taskClaimingOpts.getCapacity ?? (() => 10); + const { taskClaiming, store } = initialiseTestClaiming({ + storeOpts, + taskClaimingOpts, + hits, + versionConflicts, + }); + + const results = await getAllAsPromise(taskClaiming.claimAvailableTasks(claimingOpts)); + + expect(store.updateByQuery.mock.calls[0][1]).toMatchObject({ + max_docs: getCapacity(), + }); + expect(store.fetch.mock.calls[0][0]).toMatchObject({ size: getCapacity() }); + return results.map((result, index) => ({ + result, + args: { + search: store.fetch.mock.calls[index][0] as SearchOpts & { + query: BoolClauseWithAnyCondition; + }, + updateByQuery: store.updateByQuery.mock.calls[index] as [ + UpdateByQuerySearchOpts, + UpdateByQueryOpts + ], + }, + })); + } + + test('it filters claimed tasks down by supported types, maxAttempts, status, and runAt', async () => { + const maxAttempts = _.random(2, 43); + const customMaxAttempts = _.random(44, 100); + + const definitions = new TaskTypeDictionary(mockLogger()); + definitions.registerTaskDefinitions({ + foo: { + title: 'foo', + createTaskRunner: jest.fn(), + }, + bar: { + title: 'bar', + maxAttempts: customMaxAttempts, + createTaskRunner: jest.fn(), + }, + }); + + const [ + { + args: { + updateByQuery: [{ query, sort }], + }, + }, + ] = await testClaimAvailableTasks({ + storeOpts: { + definitions, + }, + taskClaimingOpts: { + maxAttempts, + }, + claimingOpts: { + claimOwnershipUntil: new Date(), + }, + }); + expect(query).toMatchObject({ + bool: { + must: [ + { + bool: { + should: [ + { + bool: { + must: [ + { term: { 'task.status': 'idle' } }, + { range: { 'task.runAt': { lte: 'now' } } }, + ], + }, + }, + { + bool: { + must: [ + { + bool: { + should: [ + { term: { 'task.status': 'running' } }, + { term: { 'task.status': 'claiming' } }, + ], + }, + }, + { range: { 'task.retryAt': { lte: 'now' } } }, + ], + }, + }, + ], + }, + }, + ], + filter: [ + { + bool: { + must_not: [ + { + bool: { + should: [ + { term: { 'task.status': 'running' } }, + { term: { 'task.status': 'claiming' } }, + ], + must: { range: { 'task.retryAt': { gt: 'now' } } }, + }, + }, + ], + }, + }, + ], + }, + }); + expect(sort).toMatchObject([ + { + _script: { + type: 'number', + order: 'asc', + script: { + lang: 'painless', + source: ` +if (doc['task.retryAt'].size()!=0) { + return doc['task.retryAt'].value.toInstant().toEpochMilli(); +} +if (doc['task.runAt'].size()!=0) { + return doc['task.runAt'].value.toInstant().toEpochMilli(); +} + `, + }, + }, + }, + ]); + }); + + test('it supports claiming specific tasks by id', async () => { + const maxAttempts = _.random(2, 43); + const customMaxAttempts = _.random(44, 100); + const definitions = new TaskTypeDictionary(mockLogger()); + const taskManagerId = uuid.v1(); + const fieldUpdates = { + ownerId: taskManagerId, + retryAt: new Date(Date.now()), + }; + definitions.registerTaskDefinitions({ + foo: { + title: 'foo', + createTaskRunner: jest.fn(), + }, + bar: { + title: 'bar', + maxAttempts: customMaxAttempts, + createTaskRunner: jest.fn(), + }, + }); + const [ + { + args: { + updateByQuery: [{ query, script, sort }], + }, + }, + ] = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + definitions, + }, + taskClaimingOpts: { + maxAttempts, + }, + claimingOpts: { + claimOwnershipUntil: new Date(), + claimTasksById: [ + '33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ], + }, + }); + + expect(query).toMatchObject({ + bool: { + must: [ + { + pinned: { + ids: [ + 'task:33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'task:a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ], + organic: { + bool: { + must: [ + { + bool: { + should: [ + { + bool: { + must: [ + { term: { 'task.status': 'idle' } }, + { range: { 'task.runAt': { lte: 'now' } } }, + ], + }, + }, + { + bool: { + must: [ + { + bool: { + should: [ + { term: { 'task.status': 'running' } }, + { term: { 'task.status': 'claiming' } }, + ], + }, + }, + { range: { 'task.retryAt': { lte: 'now' } } }, + ], + }, + }, + ], + }, + }, + ], + }, + }, + }, + }, + ], + filter: [ + { + bool: { + must_not: [ + { + bool: { + should: [ + { term: { 'task.status': 'running' } }, + { term: { 'task.status': 'claiming' } }, + ], + must: { range: { 'task.retryAt': { gt: 'now' } } }, + }, + }, + ], + }, + }, + ], + }, + }); + + expect(script).toMatchObject({ + source: expect.any(String), + lang: 'painless', + params: { + fieldUpdates, + claimTasksById: [ + 'task:33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'task:a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ], + claimableTaskTypes: ['foo', 'bar'], + skippedTaskTypes: [], + taskMaxAttempts: { + bar: customMaxAttempts, + foo: maxAttempts, + }, + }, + }); + + expect(sort).toMatchObject([ + '_score', + { + _script: { + type: 'number', + order: 'asc', + script: { + lang: 'painless', + source: ` +if (doc['task.retryAt'].size()!=0) { + return doc['task.retryAt'].value.toInstant().toEpochMilli(); +} +if (doc['task.runAt'].size()!=0) { + return doc['task.runAt'].value.toInstant().toEpochMilli(); +} + `, + }, + }, + }, + ]); + }); + + test('it should claim in batches partitioned by maxConcurrency', async () => { + const maxAttempts = _.random(2, 43); + const definitions = new TaskTypeDictionary(mockLogger()); + const taskManagerId = uuid.v1(); + const fieldUpdates = { + ownerId: taskManagerId, + retryAt: new Date(Date.now()), + }; + definitions.registerTaskDefinitions({ + unlimited: { + title: 'unlimited', + createTaskRunner: jest.fn(), + }, + limitedToZero: { + title: 'limitedToZero', + maxConcurrency: 0, + createTaskRunner: jest.fn(), + }, + anotherUnlimited: { + title: 'anotherUnlimited', + createTaskRunner: jest.fn(), + }, + finalUnlimited: { + title: 'finalUnlimited', + createTaskRunner: jest.fn(), + }, + limitedToOne: { + title: 'limitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + anotherLimitedToOne: { + title: 'anotherLimitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + limitedToTwo: { + title: 'limitedToTwo', + maxConcurrency: 2, + createTaskRunner: jest.fn(), + }, + }); + const results = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + definitions, + }, + taskClaimingOpts: { + maxAttempts, + getCapacity: (type) => { + switch (type) { + case 'limitedToOne': + case 'anotherLimitedToOne': + return 1; + case 'limitedToTwo': + return 2; + default: + return 10; + } + }, + }, + claimingOpts: { + claimOwnershipUntil: new Date(), + claimTasksById: [ + '33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ], + }, + }); + + expect(results.length).toEqual(4); + + expect(results[0].args.updateByQuery[1].max_docs).toEqual(10); + expect(results[0].args.updateByQuery[0].script).toMatchObject({ + source: expect.any(String), + lang: 'painless', + params: { + fieldUpdates, + claimTasksById: [ + 'task:33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', + 'task:a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', + ], + claimableTaskTypes: ['unlimited', 'anotherUnlimited', 'finalUnlimited'], + skippedTaskTypes: [ + 'limitedToZero', + 'limitedToOne', + 'anotherLimitedToOne', + 'limitedToTwo', + ], + taskMaxAttempts: { + unlimited: maxAttempts, + }, + }, + }); + + expect(results[1].args.updateByQuery[1].max_docs).toEqual(1); + expect(results[1].args.updateByQuery[0].script).toMatchObject({ + source: expect.any(String), + lang: 'painless', + params: { + fieldUpdates, + claimTasksById: [], + claimableTaskTypes: ['limitedToOne'], + skippedTaskTypes: [ + 'unlimited', + 'limitedToZero', + 'anotherUnlimited', + 'finalUnlimited', + 'anotherLimitedToOne', + 'limitedToTwo', + ], + taskMaxAttempts: { + limitedToOne: maxAttempts, + }, + }, + }); + + expect(results[2].args.updateByQuery[1].max_docs).toEqual(1); + expect(results[2].args.updateByQuery[0].script).toMatchObject({ + source: expect.any(String), + lang: 'painless', + params: { + fieldUpdates, + claimTasksById: [], + claimableTaskTypes: ['anotherLimitedToOne'], + skippedTaskTypes: [ + 'unlimited', + 'limitedToZero', + 'anotherUnlimited', + 'finalUnlimited', + 'limitedToOne', + 'limitedToTwo', + ], + taskMaxAttempts: { + anotherLimitedToOne: maxAttempts, + }, + }, + }); + + expect(results[3].args.updateByQuery[1].max_docs).toEqual(2); + expect(results[3].args.updateByQuery[0].script).toMatchObject({ + source: expect.any(String), + lang: 'painless', + params: { + fieldUpdates, + claimTasksById: [], + claimableTaskTypes: ['limitedToTwo'], + skippedTaskTypes: [ + 'unlimited', + 'limitedToZero', + 'anotherUnlimited', + 'finalUnlimited', + 'limitedToOne', + 'anotherLimitedToOne', + ], + taskMaxAttempts: { + limitedToTwo: maxAttempts, + }, + }, + }); + }); + + test('it should reduce the available capacity from batch to batch', async () => { + const maxAttempts = _.random(2, 43); + const definitions = new TaskTypeDictionary(mockLogger()); + const taskManagerId = uuid.v1(); + definitions.registerTaskDefinitions({ + unlimited: { + title: 'unlimited', + createTaskRunner: jest.fn(), + }, + limitedToFive: { + title: 'limitedToFive', + maxConcurrency: 5, + createTaskRunner: jest.fn(), + }, + limitedToTwo: { + title: 'limitedToTwo', + maxConcurrency: 2, + createTaskRunner: jest.fn(), + }, + }); + const results = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + definitions, + }, + taskClaimingOpts: { + maxAttempts, + getCapacity: (type) => { + switch (type) { + case 'limitedToTwo': + return 2; + case 'limitedToFive': + return 5; + default: + return 10; + } + }, + }, + hits: [ + [ + // 7 returned by unlimited query + mockInstance({ + taskType: 'unlimited', + }), + mockInstance({ + taskType: 'unlimited', + }), + mockInstance({ + taskType: 'unlimited', + }), + mockInstance({ + taskType: 'unlimited', + }), + mockInstance({ + taskType: 'unlimited', + }), + mockInstance({ + taskType: 'unlimited', + }), + mockInstance({ + taskType: 'unlimited', + }), + ], + // 2 returned by limitedToFive query + [ + mockInstance({ + taskType: 'limitedToFive', + }), + mockInstance({ + taskType: 'limitedToFive', + }), + ], + // 1 reterned by limitedToTwo query + [ + mockInstance({ + taskType: 'limitedToTwo', + }), + ], + ], + claimingOpts: { + claimOwnershipUntil: new Date(), + claimTasksById: [], + }, + }); + + expect(results.length).toEqual(3); + + expect(results[0].args.updateByQuery[1].max_docs).toEqual(10); + + // only capacity for 3, even though 5 are allowed + expect(results[1].args.updateByQuery[1].max_docs).toEqual(3); + + // only capacity for 1, even though 2 are allowed + expect(results[2].args.updateByQuery[1].max_docs).toEqual(1); + }); + + test('it shuffles the types claimed in batches to ensure no type starves another', async () => { + const maxAttempts = _.random(2, 43); + const definitions = new TaskTypeDictionary(mockLogger()); + const taskManagerId = uuid.v1(); + definitions.registerTaskDefinitions({ + unlimited: { + title: 'unlimited', + createTaskRunner: jest.fn(), + }, + anotherUnlimited: { + title: 'anotherUnlimited', + createTaskRunner: jest.fn(), + }, + finalUnlimited: { + title: 'finalUnlimited', + createTaskRunner: jest.fn(), + }, + limitedToOne: { + title: 'limitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + anotherLimitedToOne: { + title: 'anotherLimitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + limitedToTwo: { + title: 'limitedToTwo', + maxConcurrency: 2, + createTaskRunner: jest.fn(), + }, + }); + + const { taskClaiming, store } = initialiseTestClaiming({ + storeOpts: { + taskManagerId, + definitions, + }, + taskClaimingOpts: { + maxAttempts, + getCapacity: (type) => { + switch (type) { + case 'limitedToOne': + case 'anotherLimitedToOne': + return 1; + case 'limitedToTwo': + return 2; + default: + return 10; + } + }, + }, + }); + + async function getUpdateByQueryScriptParams() { + return ( + await getAllAsPromise( + taskClaiming.claimAvailableTasks({ + claimOwnershipUntil: new Date(), + }) + ) + ).map( + (result, index) => + (store.updateByQuery.mock.calls[index][0] as { + query: BoolClauseWithAnyCondition; + size: number; + sort: string | string[]; + script: { + params: { + claimableTaskTypes: string[]; + }; + }; + }).script.params.claimableTaskTypes + ); + } + + const firstCycle = await getUpdateByQueryScriptParams(); + store.updateByQuery.mockClear(); + const secondCycle = await getUpdateByQueryScriptParams(); + + expect(firstCycle.length).toEqual(4); + expect(secondCycle.length).toEqual(4); + expect(firstCycle).not.toMatchObject(secondCycle); + }); + + test('it claims tasks by setting their ownerId, status and retryAt', async () => { + const taskManagerId = uuid.v1(); + const claimOwnershipUntil = new Date(Date.now()); + const fieldUpdates = { + ownerId: taskManagerId, + retryAt: claimOwnershipUntil, + }; + const [ + { + args: { + updateByQuery: [{ script }], + }, + }, + ] = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + }, + taskClaimingOpts: {}, + claimingOpts: { + claimOwnershipUntil, + }, + }); + expect(script).toMatchObject({ + source: expect.any(String), + lang: 'painless', + params: { + fieldUpdates, + claimableTaskTypes: ['report', 'dernstraight', 'yawn'], + skippedTaskTypes: [], + taskMaxAttempts: { + dernstraight: 2, + report: 2, + yawn: 2, + }, + }, + }); + }); + + test('it filters out running tasks', async () => { + const taskManagerId = uuid.v1(); + const claimOwnershipUntil = new Date(Date.now()); + const runAt = new Date(); + const tasks = [ + mockInstance({ + id: 'aaa', + runAt, + taskType: 'foo', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + }), + ]; + const [ + { + result: { docs }, + args: { + search: { query }, + }, + }, + ] = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + }, + taskClaimingOpts: {}, + claimingOpts: { + claimOwnershipUntil, + }, + hits: [tasks], + }); + + expect(query).toMatchObject({ + bool: { + must: [ + { + term: { + 'task.ownerId': taskManagerId, + }, + }, + { term: { 'task.status': 'claiming' } }, + { + bool: { + should: [ + { + term: { + 'task.taskType': 'report', + }, + }, + { + term: { + 'task.taskType': 'dernstraight', + }, + }, + { + term: { + 'task.taskType': 'yawn', + }, + }, + ], + }, + }, + ], + }, + }); + + expect(docs).toMatchObject([ + { + attempts: 0, + id: 'aaa', + schedule: undefined, + params: { hello: 'world' }, + runAt, + scope: ['reporting'], + state: { baby: 'Henhen' }, + status: 'claiming', + taskType: 'foo', + user: 'jimbo', + ownerId: taskManagerId, + }, + ]); + }); + + test('it returns task objects', async () => { + const taskManagerId = uuid.v1(); + const claimOwnershipUntil = new Date(Date.now()); + const runAt = new Date(); + const tasks = [ + mockInstance({ + id: 'aaa', + runAt, + taskType: 'foo', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + }), + mockInstance({ + id: 'bbb', + runAt, + taskType: 'bar', + schedule: { interval: '5m' }, + attempts: 2, + status: TaskStatus.Claiming, + params: { shazm: 1 }, + state: { henry: 'The 8th' }, + user: 'dabo', + scope: ['reporting', 'ceo'], + ownerId: taskManagerId, + }), + ]; + const [ + { + result: { docs }, + args: { + search: { query }, + }, + }, + ] = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + }, + taskClaimingOpts: {}, + claimingOpts: { + claimOwnershipUntil, + }, + hits: [tasks], + }); + + expect(query).toMatchObject({ + bool: { + must: [ + { + term: { + 'task.ownerId': taskManagerId, + }, + }, + { term: { 'task.status': 'claiming' } }, + { + bool: { + should: [ + { + term: { + 'task.taskType': 'report', + }, + }, + { + term: { + 'task.taskType': 'dernstraight', + }, + }, + { + term: { + 'task.taskType': 'yawn', + }, + }, + ], + }, + }, + ], + }, + }); + + expect(docs).toMatchObject([ + { + attempts: 0, + id: 'aaa', + schedule: undefined, + params: { hello: 'world' }, + runAt, + scope: ['reporting'], + state: { baby: 'Henhen' }, + status: 'claiming', + taskType: 'foo', + user: 'jimbo', + ownerId: taskManagerId, + }, + { + attempts: 2, + id: 'bbb', + schedule: { interval: '5m' }, + params: { shazm: 1 }, + runAt, + scope: ['reporting', 'ceo'], + state: { henry: 'The 8th' }, + status: 'claiming', + taskType: 'bar', + user: 'dabo', + ownerId: taskManagerId, + }, + ]); + }); + + test('it returns version_conflicts that do not include conflicts that were proceeded against', async () => { + const taskManagerId = uuid.v1(); + const claimOwnershipUntil = new Date(Date.now()); + const runAt = new Date(); + const tasks = [ + mockInstance({ + runAt, + taskType: 'foo', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + }), + mockInstance({ + runAt, + taskType: 'bar', + schedule: { interval: '5m' }, + attempts: 2, + status: TaskStatus.Claiming, + params: { shazm: 1 }, + state: { henry: 'The 8th' }, + user: 'dabo', + scope: ['reporting', 'ceo'], + ownerId: taskManagerId, + }), + ]; + const maxDocs = 10; + const [ + { + result: { + stats: { tasksUpdated, tasksConflicted, tasksClaimed }, + }, + }, + ] = await testClaimAvailableTasks({ + storeOpts: { + taskManagerId, + }, + taskClaimingOpts: { getCapacity: () => maxDocs }, + claimingOpts: { + claimOwnershipUntil, + }, + hits: [tasks], + // assume there were 20 version conflists, but thanks to `conflicts="proceed"` + // we proceeded to claim tasks + versionConflicts: 20, + }); + + expect(tasksUpdated).toEqual(2); + // ensure we only count conflicts that *may* have counted against max_docs, no more than that + expect(tasksConflicted).toEqual(10 - tasksUpdated!); + expect(tasksClaimed).toEqual(2); + }); + }); + + describe('task events', () => { + function generateTasks(taskManagerId: string) { + const runAt = new Date(); + const tasks = [ + { + id: 'claimed-by-id', + runAt, + taskType: 'foo', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }, + { + id: 'claimed-by-schedule', + runAt, + taskType: 'bar', + schedule: { interval: '5m' }, + attempts: 2, + status: TaskStatus.Claiming, + params: { shazm: 1 }, + state: { henry: 'The 8th' }, + user: 'dabo', + scope: ['reporting', 'ceo'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }, + { + id: 'already-running', + runAt, + taskType: 'bar', + schedule: { interval: '5m' }, + attempts: 2, + status: TaskStatus.Running, + params: { shazm: 1 }, + state: { henry: 'The 8th' }, + user: 'dabo', + scope: ['reporting', 'ceo'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }, + ]; + + return { taskManagerId, runAt, tasks }; + } + + function instantiateStoreWithMockedApiResponses({ + taskManagerId = uuid.v4(), + definitions = taskDefinitions, + getCapacity = () => 10, + tasksClaimed, + }: Partial> & { + taskManagerId?: string; + tasksClaimed?: ConcreteTaskInstance[][]; + } = {}) { + const { runAt, tasks: generatedTasks } = generateTasks(taskManagerId); + const taskCycles = tasksClaimed ?? [generatedTasks]; + + const taskStore = taskStoreMock.create({ taskManagerId }); + taskStore.convertToSavedObjectIds.mockImplementation((ids) => ids.map((id) => `task:${id}`)); + for (const docs of taskCycles) { + taskStore.fetch.mockResolvedValueOnce({ docs }); + taskStore.updateByQuery.mockResolvedValueOnce({ + updated: docs.length, + version_conflicts: 0, + total: docs.length, + }); + } + + taskStore.fetch.mockResolvedValue({ docs: [] }); + taskStore.updateByQuery.mockResolvedValue({ + updated: 0, + version_conflicts: 0, + total: 0, + }); + + const taskClaiming = new TaskClaiming({ + logger: taskManagerLogger, + definitions, + taskStore, + maxAttempts: 2, + getCapacity, + }); + + return { taskManagerId, runAt, taskClaiming }; + } + + test('emits an event when a task is succesfully claimed by id', async () => { + const { taskManagerId, runAt, taskClaiming } = instantiateStoreWithMockedApiResponses(); + + const promise = taskClaiming.events + .pipe( + filter( + (event: TaskEvent) => event.id === 'claimed-by-id' + ), + take(1) + ) + .toPromise(); + + await getFirstAsPromise( + taskClaiming.claimAvailableTasks({ + claimTasksById: ['claimed-by-id'], + claimOwnershipUntil: new Date(), + }) + ); + + const event = await promise; + expect(event).toMatchObject( + asTaskClaimEvent( + 'claimed-by-id', + asOk({ + id: 'claimed-by-id', + runAt, + taskType: 'foo', + schedule: undefined, + attempts: 0, + status: 'claiming' as TaskStatus, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }) + ) + ); + }); + + test('emits an event when a task is succesfully claimed by id by is rejected as it would exceed maxCapacity of its taskType', async () => { + const definitions = new TaskTypeDictionary(mockLogger()); + definitions.registerTaskDefinitions({ + unlimited: { + title: 'unlimited', + createTaskRunner: jest.fn(), + }, + limitedToOne: { + title: 'limitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + anotherLimitedToOne: { + title: 'anotherLimitedToOne', + maxConcurrency: 1, + createTaskRunner: jest.fn(), + }, + }); + + const taskManagerId = uuid.v4(); + const { runAt, taskClaiming } = instantiateStoreWithMockedApiResponses({ + taskManagerId, + definitions, + getCapacity: (type) => { + switch (type) { + case 'limitedToOne': + // return 0 as there's already a `limitedToOne` task running + return 0; + default: + return 10; + } + }, + tasksClaimed: [ + // find on first claim cycle + [ + { + id: 'claimed-by-id-limited-concurrency', + runAt: new Date(), + taskType: 'limitedToOne', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }, + ], + // second cycle + [ + { + id: 'claimed-by-schedule-unlimited', + runAt: new Date(), + taskType: 'unlimited', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }, + ], + ], + }); + + const promise = taskClaiming.events + .pipe( + filter( + (event: TaskEvent) => + event.id === 'claimed-by-id-limited-concurrency' + ), + take(1) + ) + .toPromise(); + + const [firstCycleResult, secondCycleResult] = await getAllAsPromise( + taskClaiming.claimAvailableTasks({ + claimTasksById: ['claimed-by-id-limited-concurrency'], + claimOwnershipUntil: new Date(), + }) + ); + + expect(firstCycleResult.stats.tasksClaimed).toEqual(0); + expect(firstCycleResult.stats.tasksRejected).toEqual(1); + expect(firstCycleResult.stats.tasksUpdated).toEqual(1); + + // values accumulate from cycle to cycle + expect(secondCycleResult.stats.tasksClaimed).toEqual(0); + expect(secondCycleResult.stats.tasksRejected).toEqual(1); + expect(secondCycleResult.stats.tasksUpdated).toEqual(1); + + const event = await promise; + expect(event).toMatchObject( + asTaskClaimEvent( + 'claimed-by-id-limited-concurrency', + asErr({ + task: some({ + id: 'claimed-by-id-limited-concurrency', + runAt, + taskType: 'limitedToOne', + schedule: undefined, + attempts: 0, + status: 'claiming' as TaskStatus, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }), + errorType: TaskClaimErrorType.CLAIMED_BY_ID_OUT_OF_CAPACITY, + }) + ) + ); + }); + + test('emits an event when a task is succesfully by scheduling', async () => { + const { taskManagerId, runAt, taskClaiming } = instantiateStoreWithMockedApiResponses(); + + const promise = taskClaiming.events + .pipe( + filter( + (event: TaskEvent) => + event.id === 'claimed-by-schedule' + ), + take(1) + ) + .toPromise(); + + await getFirstAsPromise( + taskClaiming.claimAvailableTasks({ + claimTasksById: ['claimed-by-id'], + claimOwnershipUntil: new Date(), + }) + ); + + const event = await promise; + expect(event).toMatchObject( + asTaskClaimEvent( + 'claimed-by-schedule', + asOk({ + id: 'claimed-by-schedule', + runAt, + taskType: 'bar', + schedule: { interval: '5m' }, + attempts: 2, + status: 'claiming' as TaskStatus, + params: { shazm: 1 }, + state: { henry: 'The 8th' }, + user: 'dabo', + scope: ['reporting', 'ceo'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }) + ) + ); + }); + + test('emits an event when the store fails to claim a required task by id', async () => { + const { taskManagerId, runAt, taskClaiming } = instantiateStoreWithMockedApiResponses(); + + const promise = taskClaiming.events + .pipe( + filter( + (event: TaskEvent) => event.id === 'already-running' + ), + take(1) + ) + .toPromise(); + + await getFirstAsPromise( + taskClaiming.claimAvailableTasks({ + claimTasksById: ['already-running'], + claimOwnershipUntil: new Date(), + }) + ); + + const event = await promise; + expect(event).toMatchObject( + asTaskClaimEvent( + 'already-running', + asErr({ + task: some({ + id: 'already-running', + runAt, + taskType: 'bar', + schedule: { interval: '5m' }, + attempts: 2, + status: 'running' as TaskStatus, + params: { shazm: 1 }, + state: { henry: 'The 8th' }, + user: 'dabo', + scope: ['reporting', 'ceo'], + ownerId: taskManagerId, + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + }), + errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_IN_CLAIMING_STATUS, + }) + ) + ); + }); + + test('emits an event when the store fails to find a task which was required by id', async () => { + const { taskClaiming } = instantiateStoreWithMockedApiResponses(); + + const promise = taskClaiming.events + .pipe( + filter( + (event: TaskEvent) => event.id === 'unknown-task' + ), + take(1) + ) + .toPromise(); + + await getFirstAsPromise( + taskClaiming.claimAvailableTasks({ + claimTasksById: ['unknown-task'], + claimOwnershipUntil: new Date(), + }) + ); + + const event = await promise; + expect(event).toMatchObject( + asTaskClaimEvent( + 'unknown-task', + asErr({ + task: none, + errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED, + }) + ) + ); + }); + }); +}); + +function generateFakeTasks(count: number = 1) { + return _.times(count, (index) => mockInstance({ id: `task:id-${index}` })); +} + +function mockInstance(instance: Partial = {}) { + return Object.assign( + { + id: uuid.v4(), + taskType: 'bar', + sequenceNumber: 32, + primaryTerm: 32, + runAt: new Date(), + scheduledAt: new Date(), + startedAt: null, + retryAt: null, + attempts: 0, + params: {}, + scope: ['reporting'], + state: {}, + status: 'idle', + user: 'example', + ownerId: null, + }, + instance + ); +} + +function getFirstAsPromise(obs$: Observable): Promise { + return new Promise((resolve, reject) => { + obs$.subscribe(resolve, reject); + }); +} +function getAllAsPromise(obs$: Observable): Promise { + return new Promise((resolve, reject) => { + obs$.pipe(toArray()).subscribe(resolve, reject); + }); +} diff --git a/x-pack/plugins/task_manager/server/queries/task_claiming.ts b/x-pack/plugins/task_manager/server/queries/task_claiming.ts new file mode 100644 index 0000000000000..b4e11dbf81eb1 --- /dev/null +++ b/x-pack/plugins/task_manager/server/queries/task_claiming.ts @@ -0,0 +1,488 @@ +/* + * 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. + */ + +/* + * This module contains helpers for managing the task manager storage layer. + */ +import apm from 'elastic-apm-node'; +import { Subject, Observable, from, of } from 'rxjs'; +import { map, mergeScan } from 'rxjs/operators'; +import { difference, partition, groupBy, mapValues, countBy, pick } from 'lodash'; +import { some, none } from 'fp-ts/lib/Option'; + +import { Logger } from '../../../../../src/core/server'; + +import { asOk, asErr, Result } from '../lib/result_type'; +import { ConcreteTaskInstance, TaskStatus } from '../task'; +import { + TaskClaim, + asTaskClaimEvent, + TaskClaimErrorType, + startTaskTimer, + TaskTiming, +} from '../task_events'; + +import { + asUpdateByQuery, + shouldBeOneOf, + mustBeAllOf, + filterDownBy, + asPinnedQuery, + matchesClauses, + SortOptions, +} from './query_clauses'; + +import { + updateFieldsAndMarkAsFailed, + IdleTaskWithExpiredRunAt, + InactiveTasks, + RunningOrClaimingTaskWithExpiredRetryAt, + SortByRunAtAndRetryAt, + tasksClaimedByOwner, + tasksOfType, +} from './mark_available_tasks_as_claimed'; +import { TaskTypeDictionary } from '../task_type_dictionary'; +import { + correctVersionConflictsForContinuation, + TaskStore, + UpdateByQueryResult, +} from '../task_store'; +import { FillPoolResult } from '../lib/fill_pool'; + +export interface TaskClaimingOpts { + logger: Logger; + definitions: TaskTypeDictionary; + taskStore: TaskStore; + maxAttempts: number; + getCapacity: (taskType?: string) => number; +} + +export interface OwnershipClaimingOpts { + claimOwnershipUntil: Date; + claimTasksById?: string[]; + size: number; + taskTypes: Set; +} +export type IncrementalOwnershipClaimingOpts = OwnershipClaimingOpts & { + precedingQueryResult: UpdateByQueryResult; +}; +export type IncrementalOwnershipClaimingReduction = ( + opts: IncrementalOwnershipClaimingOpts +) => Promise; + +export interface FetchResult { + docs: ConcreteTaskInstance[]; +} + +export interface ClaimOwnershipResult { + stats: { + tasksUpdated: number; + tasksConflicted: number; + tasksClaimed: number; + tasksRejected: number; + }; + docs: ConcreteTaskInstance[]; + timing?: TaskTiming; +} + +enum BatchConcurrency { + Unlimited, + Limited, +} + +type TaskClaimingBatches = Array; +interface TaskClaimingBatch { + concurrency: Concurrency; + tasksTypes: TaskType; +} +type UnlimitedBatch = TaskClaimingBatch>; +type LimitedBatch = TaskClaimingBatch; + +export class TaskClaiming { + public readonly errors$ = new Subject(); + public readonly maxAttempts: number; + + private definitions: TaskTypeDictionary; + private events$: Subject; + private taskStore: TaskStore; + private getCapacity: (taskType?: string) => number; + private logger: Logger; + private readonly taskClaimingBatchesByType: TaskClaimingBatches; + private readonly taskMaxAttempts: Record; + + /** + * Constructs a new TaskStore. + * @param {TaskClaimingOpts} opts + * @prop {number} maxAttempts - The maximum number of attempts before a task will be abandoned + * @prop {TaskDefinition} definition - The definition of the task being run + */ + constructor(opts: TaskClaimingOpts) { + this.definitions = opts.definitions; + this.maxAttempts = opts.maxAttempts; + this.taskStore = opts.taskStore; + this.getCapacity = opts.getCapacity; + this.logger = opts.logger; + this.taskClaimingBatchesByType = this.partitionIntoClaimingBatches(this.definitions); + this.taskMaxAttempts = Object.fromEntries(this.normalizeMaxAttempts(this.definitions)); + + this.events$ = new Subject(); + } + + private partitionIntoClaimingBatches(definitions: TaskTypeDictionary): TaskClaimingBatches { + const { + limitedConcurrency, + unlimitedConcurrency, + skippedTypes, + } = groupBy(definitions.getAllDefinitions(), (definition) => + definition.maxConcurrency + ? 'limitedConcurrency' + : definition.maxConcurrency === 0 + ? 'skippedTypes' + : 'unlimitedConcurrency' + ); + + if (skippedTypes?.length) { + this.logger.info( + `Task Manager will never claim tasks of the following types as their "maxConcurrency" is set to 0: ${skippedTypes + .map(({ type }) => type) + .join(', ')}` + ); + } + return [ + ...(unlimitedConcurrency + ? [asUnlimited(new Set(unlimitedConcurrency.map(({ type }) => type)))] + : []), + ...(limitedConcurrency ? limitedConcurrency.map(({ type }) => asLimited(type)) : []), + ]; + } + + private normalizeMaxAttempts(definitions: TaskTypeDictionary) { + return new Map( + [...definitions].map(([type, { maxAttempts }]) => [type, maxAttempts || this.maxAttempts]) + ); + } + + private claimingBatchIndex = 0; + private getClaimingBatches() { + // return all batches, starting at index and cycling back to where we began + const batch = [ + ...this.taskClaimingBatchesByType.slice(this.claimingBatchIndex), + ...this.taskClaimingBatchesByType.slice(0, this.claimingBatchIndex), + ]; + // shift claimingBatchIndex by one so that next cycle begins at the next index + this.claimingBatchIndex = (this.claimingBatchIndex + 1) % this.taskClaimingBatchesByType.length; + return batch; + } + + public get events(): Observable { + return this.events$; + } + + private emitEvents = (events: TaskClaim[]) => { + events.forEach((event) => this.events$.next(event)); + }; + + public claimAvailableTasksIfCapacityIsAvailable( + claimingOptions: Omit + ): Observable> { + if (this.getCapacity()) { + return this.claimAvailableTasks(claimingOptions).pipe( + map((claimResult) => asOk(claimResult)) + ); + } + this.logger.debug( + `[Task Ownership]: Task Manager has skipped Claiming Ownership of available tasks at it has ran out Available Workers.` + ); + return of(asErr(FillPoolResult.NoAvailableWorkers)); + } + + public claimAvailableTasks({ + claimOwnershipUntil, + claimTasksById = [], + }: Omit): Observable { + const initialCapacity = this.getCapacity(); + return from(this.getClaimingBatches()).pipe( + mergeScan( + (accumulatedResult, batch) => { + const stopTaskTimer = startTaskTimer(); + const capacity = Math.min( + initialCapacity - accumulatedResult.stats.tasksClaimed, + isLimited(batch) ? this.getCapacity(batch.tasksTypes) : this.getCapacity() + ); + // if we have no more capacity, short circuit here + if (capacity <= 0) { + return of(accumulatedResult); + } + return from( + this.executClaimAvailableTasks({ + claimOwnershipUntil, + claimTasksById: claimTasksById.splice(0, capacity), + size: capacity, + taskTypes: isLimited(batch) ? new Set([batch.tasksTypes]) : batch.tasksTypes, + }).then((result) => { + const { stats, docs } = accumulateClaimOwnershipResults(accumulatedResult, result); + stats.tasksConflicted = correctVersionConflictsForContinuation( + stats.tasksClaimed, + stats.tasksConflicted, + initialCapacity + ); + return { stats, docs, timing: stopTaskTimer() }; + }) + ); + }, + // initialise the accumulation with no results + accumulateClaimOwnershipResults(), + // only run one batch at a time + 1 + ) + ); + } + + private executClaimAvailableTasks = async ({ + claimOwnershipUntil, + claimTasksById = [], + size, + taskTypes, + }: OwnershipClaimingOpts): Promise => { + const claimTasksByIdWithRawIds = this.taskStore.convertToSavedObjectIds(claimTasksById); + const { + updated: tasksUpdated, + version_conflicts: tasksConflicted, + } = await this.markAvailableTasksAsClaimed({ + claimOwnershipUntil, + claimTasksById: claimTasksByIdWithRawIds, + size, + taskTypes, + }); + + const docs = + tasksUpdated > 0 + ? await this.sweepForClaimedTasks(claimTasksByIdWithRawIds, taskTypes, size) + : []; + + const [documentsReturnedById, documentsClaimedBySchedule] = partition(docs, (doc) => + claimTasksById.includes(doc.id) + ); + + const [documentsClaimedById, documentsRequestedButNotClaimed] = partition( + documentsReturnedById, + // we filter the schduled tasks down by status is 'claiming' in the esearch, + // but we do not apply this limitation on tasks claimed by ID so that we can + // provide more detailed error messages when we fail to claim them + (doc) => doc.status === TaskStatus.Claiming + ); + + // count how many tasks we've claimed by ID and validate we have capacity for them to run + const remainingCapacityOfClaimByIdByType = mapValues( + // This means we take the tasks that were claimed by their ID and count them by their type + countBy(documentsClaimedById, (doc) => doc.taskType), + (count, type) => this.getCapacity(type) - count + ); + + const [documentsClaimedByIdWithinCapacity, documentsClaimedByIdOutOfCapacity] = partition( + documentsClaimedById, + (doc) => { + // if we've exceeded capacity, we reject this task + if (remainingCapacityOfClaimByIdByType[doc.taskType] < 0) { + // as we're rejecting this task we can inc the count so that we know + // to keep the next one returned by ID of the same type + remainingCapacityOfClaimByIdByType[doc.taskType]++; + return false; + } + return true; + } + ); + + const documentsRequestedButNotReturned = difference( + claimTasksById, + documentsReturnedById.map((doc) => doc.id) + ); + + this.emitEvents([ + ...documentsClaimedByIdWithinCapacity.map((doc) => asTaskClaimEvent(doc.id, asOk(doc))), + ...documentsClaimedByIdOutOfCapacity.map((doc) => + asTaskClaimEvent( + doc.id, + asErr({ + task: some(doc), + errorType: TaskClaimErrorType.CLAIMED_BY_ID_OUT_OF_CAPACITY, + }) + ) + ), + ...documentsClaimedBySchedule.map((doc) => asTaskClaimEvent(doc.id, asOk(doc))), + ...documentsRequestedButNotClaimed.map((doc) => + asTaskClaimEvent( + doc.id, + asErr({ + task: some(doc), + errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_IN_CLAIMING_STATUS, + }) + ) + ), + ...documentsRequestedButNotReturned.map((id) => + asTaskClaimEvent( + id, + asErr({ task: none, errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED }) + ) + ), + ]); + + const stats = { + tasksUpdated, + tasksConflicted, + tasksRejected: documentsClaimedByIdOutOfCapacity.length, + tasksClaimed: documentsClaimedByIdWithinCapacity.length + documentsClaimedBySchedule.length, + }; + + if (docs.length !== stats.tasksClaimed + stats.tasksRejected) { + this.logger.warn( + `[Task Ownership error]: ${stats.tasksClaimed} tasks were claimed by Kibana, but ${ + docs.length + } task(s) were fetched (${docs.map((doc) => doc.id).join(', ')})` + ); + } + + return { + stats, + docs: [...documentsClaimedByIdWithinCapacity, ...documentsClaimedBySchedule], + }; + }; + + private async markAvailableTasksAsClaimed({ + claimOwnershipUntil, + claimTasksById, + size, + taskTypes, + }: OwnershipClaimingOpts): Promise { + const { taskTypesToSkip = [], taskTypesToClaim = [] } = groupBy( + this.definitions.getAllTypes(), + (type) => (taskTypes.has(type) ? 'taskTypesToClaim' : 'taskTypesToSkip') + ); + + const queryForScheduledTasks = mustBeAllOf( + // Either a task with idle status and runAt <= now or + // status running or claiming with a retryAt <= now. + shouldBeOneOf(IdleTaskWithExpiredRunAt, RunningOrClaimingTaskWithExpiredRetryAt) + ); + + // The documents should be sorted by runAt/retryAt, unless there are pinned + // tasks being queried, in which case we want to sort by score first, and then + // the runAt/retryAt. That way we'll get the pinned tasks first. Note that + // the score seems to favor newer documents rather than older documents, so + // if there are not pinned tasks being queried, we do NOT want to sort by score + // at all, just by runAt/retryAt. + const sort: SortOptions = [SortByRunAtAndRetryAt]; + if (claimTasksById && claimTasksById.length) { + sort.unshift('_score'); + } + + const apmTrans = apm.startTransaction(`taskManager markAvailableTasksAsClaimed`, 'taskManager'); + const result = await this.taskStore.updateByQuery( + asUpdateByQuery({ + query: matchesClauses( + claimTasksById && claimTasksById.length + ? mustBeAllOf(asPinnedQuery(claimTasksById, queryForScheduledTasks)) + : queryForScheduledTasks, + filterDownBy(InactiveTasks) + ), + update: updateFieldsAndMarkAsFailed( + { + ownerId: this.taskStore.taskManagerId, + retryAt: claimOwnershipUntil, + }, + claimTasksById || [], + taskTypesToClaim, + taskTypesToSkip, + pick(this.taskMaxAttempts, taskTypesToClaim) + ), + sort, + }), + { + max_docs: size, + } + ); + + if (apmTrans) apmTrans.end(); + return result; + } + + /** + * Fetches tasks from the index, which are owned by the current Kibana instance + */ + private async sweepForClaimedTasks( + claimTasksById: OwnershipClaimingOpts['claimTasksById'], + taskTypes: Set, + size: number + ): Promise { + const claimedTasksQuery = tasksClaimedByOwner( + this.taskStore.taskManagerId, + tasksOfType([...taskTypes]) + ); + const { docs } = await this.taskStore.fetch({ + query: + claimTasksById && claimTasksById.length + ? asPinnedQuery(claimTasksById, claimedTasksQuery) + : claimedTasksQuery, + size, + sort: SortByRunAtAndRetryAt, + seq_no_primary_term: true, + }); + + return docs; + } +} + +const emptyClaimOwnershipResult = () => { + return { + stats: { + tasksUpdated: 0, + tasksConflicted: 0, + tasksClaimed: 0, + tasksRejected: 0, + }, + docs: [], + }; +}; + +function accumulateClaimOwnershipResults( + prev: ClaimOwnershipResult = emptyClaimOwnershipResult(), + next?: ClaimOwnershipResult +) { + if (next) { + const { stats, docs, timing } = next; + const res = { + stats: { + tasksUpdated: stats.tasksUpdated + prev.stats.tasksUpdated, + tasksConflicted: stats.tasksConflicted + prev.stats.tasksConflicted, + tasksClaimed: stats.tasksClaimed + prev.stats.tasksClaimed, + tasksRejected: stats.tasksRejected + prev.stats.tasksRejected, + }, + docs, + timing, + }; + return res; + } + return prev; +} + +function isLimited( + batch: TaskClaimingBatch +): batch is LimitedBatch { + return batch.concurrency === BatchConcurrency.Limited; +} +function asLimited(tasksType: string): LimitedBatch { + return { + concurrency: BatchConcurrency.Limited, + tasksTypes: tasksType, + }; +} +function asUnlimited(tasksTypes: Set): UnlimitedBatch { + return { + concurrency: BatchConcurrency.Unlimited, + tasksTypes, + }; +} diff --git a/x-pack/plugins/task_manager/server/task.ts b/x-pack/plugins/task_manager/server/task.ts index 04589d696427a..4b86943ff8eca 100644 --- a/x-pack/plugins/task_manager/server/task.ts +++ b/x-pack/plugins/task_manager/server/task.ts @@ -127,6 +127,16 @@ export const taskDefinitionSchema = schema.object( min: 1, }) ), + /** + * The maximum number tasks of this type that can be run concurrently per Kibana instance. + * Setting this value will force Task Manager to poll for this task type seperatly from other task types + * which can add significant load to the ES cluster, so please use this configuration only when absolutly necesery. + */ + maxConcurrency: schema.maybe( + schema.number({ + min: 0, + }) + ), }, { validate({ timeout }) { diff --git a/x-pack/plugins/task_manager/server/task_events.ts b/x-pack/plugins/task_manager/server/task_events.ts index d3fb68aa367c1..aecf7c9a2b7e8 100644 --- a/x-pack/plugins/task_manager/server/task_events.ts +++ b/x-pack/plugins/task_manager/server/task_events.ts @@ -23,6 +23,12 @@ export enum TaskEventType { TASK_MANAGER_STAT = 'TASK_MANAGER_STAT', } +export enum TaskClaimErrorType { + CLAIMED_BY_ID_OUT_OF_CAPACITY = 'CLAIMED_BY_ID_OUT_OF_CAPACITY', + CLAIMED_BY_ID_NOT_RETURNED = 'CLAIMED_BY_ID_NOT_RETURNED', + CLAIMED_BY_ID_NOT_IN_CLAIMING_STATUS = 'CLAIMED_BY_ID_NOT_IN_CLAIMING_STATUS', +} + export interface TaskTiming { start: number; stop: number; @@ -47,14 +53,18 @@ export interface RanTask { export type ErroredTask = RanTask & { error: Error; }; +export interface ClaimTaskErr { + task: Option; + errorType: TaskClaimErrorType; +} export type TaskMarkRunning = TaskEvent; export type TaskRun = TaskEvent; -export type TaskClaim = TaskEvent>; +export type TaskClaim = TaskEvent; export type TaskRunRequest = TaskEvent; export type TaskPollingCycle = TaskEvent>; -export type TaskManagerStats = 'load' | 'pollingDelay'; +export type TaskManagerStats = 'load' | 'pollingDelay' | 'claimDuration'; export type TaskManagerStat = TaskEvent; export type OkResultOf = EventType extends TaskEvent @@ -92,7 +102,7 @@ export function asTaskRunEvent( export function asTaskClaimEvent( id: string, - event: Result>, + event: Result, timing?: TaskTiming ): TaskClaim { return { diff --git a/x-pack/plugins/task_manager/server/task_pool.test.ts b/x-pack/plugins/task_manager/server/task_pool.test.ts index 6f82c477dca9e..05eb7bd1b43e1 100644 --- a/x-pack/plugins/task_manager/server/task_pool.test.ts +++ b/x-pack/plugins/task_manager/server/task_pool.test.ts @@ -15,6 +15,7 @@ import { asOk } from './lib/result_type'; import { SavedObjectsErrorHelpers } from '../../../../src/core/server'; import moment from 'moment'; import uuid from 'uuid'; +import { TaskRunningStage } from './task_running'; describe('TaskPool', () => { test('occupiedWorkers are a sum of running tasks', async () => { @@ -370,6 +371,7 @@ describe('TaskPool', () => { cancel: async () => undefined, markTaskAsRunning: jest.fn(async () => true), run: mockRun(), + stage: TaskRunningStage.PENDING, toString: () => `TaskType "shooooo"`, get expiration() { return new Date(); diff --git a/x-pack/plugins/task_manager/server/task_pool.ts b/x-pack/plugins/task_manager/server/task_pool.ts index e30f9ef3154b2..14c0c4581a15b 100644 --- a/x-pack/plugins/task_manager/server/task_pool.ts +++ b/x-pack/plugins/task_manager/server/task_pool.ts @@ -25,6 +25,8 @@ interface Opts { } export enum TaskPoolRunResult { + // This mean we have no Run Result becuse no tasks were Ran in this cycle + NoTaskWereRan = 'NoTaskWereRan', // This means we're running all the tasks we claimed RunningAllClaimedTasks = 'RunningAllClaimedTasks', // This means we're running all the tasks we claimed and we're at capacity @@ -40,7 +42,7 @@ const VERSION_CONFLICT_MESSAGE = 'Task has been claimed by another Kibana servic */ export class TaskPool { private maxWorkers: number = 0; - private running = new Set(); + private tasksInPool = new Map(); private logger: Logger; private load$ = new Subject(); @@ -68,7 +70,7 @@ export class TaskPool { * Gets how many workers are currently in use. */ public get occupiedWorkers() { - return this.running.size; + return this.tasksInPool.size; } /** @@ -93,6 +95,16 @@ export class TaskPool { return this.maxWorkers - this.occupiedWorkers; } + /** + * Gets how many workers are currently in use by type. + */ + public getOccupiedWorkersByType(type: string) { + return [...this.tasksInPool.values()].reduce( + (count, runningTask) => (runningTask.definition.type === type ? ++count : count), + 0 + ); + } + /** * Attempts to run the specified list of tasks. Returns true if it was able * to start every task in the list, false if there was not enough capacity @@ -106,9 +118,11 @@ export class TaskPool { if (tasksToRun.length) { performance.mark('attemptToRun_start'); await Promise.all( - tasksToRun.map( - async (taskRunner) => - await taskRunner + tasksToRun + .filter((taskRunner) => !this.tasksInPool.has(taskRunner.id)) + .map(async (taskRunner) => { + this.tasksInPool.set(taskRunner.id, taskRunner); + return taskRunner .markTaskAsRunning() .then((hasTaskBeenMarkAsRunning: boolean) => hasTaskBeenMarkAsRunning @@ -118,8 +132,8 @@ export class TaskPool { message: VERSION_CONFLICT_MESSAGE, }) ) - .catch((err) => this.handleFailureOfMarkAsRunning(taskRunner, err)) - ) + .catch((err) => this.handleFailureOfMarkAsRunning(taskRunner, err)); + }) ); performance.mark('attemptToRun_stop'); @@ -139,13 +153,12 @@ export class TaskPool { public cancelRunningTasks() { this.logger.debug('Cancelling running tasks.'); - for (const task of this.running) { + for (const task of this.tasksInPool.values()) { this.cancelTask(task); } } private handleMarkAsRunning(taskRunner: TaskRunner) { - this.running.add(taskRunner); taskRunner .run() .catch((err) => { @@ -161,26 +174,31 @@ export class TaskPool { this.logger.warn(errorLogLine); } }) - .then(() => this.running.delete(taskRunner)); + .then(() => this.tasksInPool.delete(taskRunner.id)); } private handleFailureOfMarkAsRunning(task: TaskRunner, err: Error) { + this.tasksInPool.delete(task.id); this.logger.error(`Failed to mark Task ${task.toString()} as running: ${err.message}`); } private cancelExpiredTasks() { - for (const task of this.running) { - if (task.isExpired) { + for (const taskRunner of this.tasksInPool.values()) { + if (taskRunner.isExpired) { this.logger.warn( - `Cancelling task ${task.toString()} as it expired at ${task.expiration.toISOString()}${ - task.startedAt + `Cancelling task ${taskRunner.toString()} as it expired at ${taskRunner.expiration.toISOString()}${ + taskRunner.startedAt ? ` after running for ${durationAsString( - moment.duration(moment(new Date()).utc().diff(task.startedAt)) + moment.duration(moment(new Date()).utc().diff(taskRunner.startedAt)) )}` : `` - }${task.definition.timeout ? ` (with timeout set at ${task.definition.timeout})` : ``}.` + }${ + taskRunner.definition.timeout + ? ` (with timeout set at ${taskRunner.definition.timeout})` + : `` + }.` ); - this.cancelTask(task); + this.cancelTask(taskRunner); } } } @@ -188,7 +206,7 @@ export class TaskPool { private async cancelTask(task: TaskRunner) { try { this.logger.debug(`Cancelling task ${task.toString()}.`); - this.running.delete(task); + this.tasksInPool.delete(task.id); await task.cancel(); } catch (err) { this.logger.error(`Failed to cancel task ${task.toString()}: ${err}`); diff --git a/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts b/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts index dff8c1f24de0a..5a36d6affe686 100644 --- a/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts +++ b/x-pack/plugins/task_manager/server/task_running/task_runner.test.ts @@ -9,7 +9,7 @@ import _ from 'lodash'; import sinon from 'sinon'; import { secondsFromNow } from '../lib/intervals'; import { asOk, asErr } from '../lib/result_type'; -import { TaskManagerRunner, TaskRunResult } from '../task_running'; +import { TaskManagerRunner, TaskRunningStage, TaskRunResult } from '../task_running'; import { TaskEvent, asTaskRunEvent, asTaskMarkRunningEvent, TaskRun } from '../task_events'; import { ConcreteTaskInstance, TaskStatus } from '../task'; import { SavedObjectsErrorHelpers } from '../../../../../src/core/server'; @@ -17,6 +17,7 @@ import moment from 'moment'; import { TaskDefinitionRegistry, TaskTypeDictionary } from '../task_type_dictionary'; import { mockLogger } from '../test_utils'; import { throwUnrecoverableError } from './errors'; +import { taskStoreMock } from '../task_store.mock'; const minutesFromNow = (mins: number): Date => secondsFromNow(mins * 60); @@ -29,980 +30,834 @@ beforeAll(() => { afterAll(() => fakeTimer.restore()); describe('TaskManagerRunner', () => { - test('provides details about the task that is running', () => { - const { runner } = testOpts({ - instance: { - id: 'foo', - taskType: 'bar', - }, - }); + const pendingStageSetup = (opts: TestOpts) => testOpts(TaskRunningStage.PENDING, opts); + const readyToRunStageSetup = (opts: TestOpts) => testOpts(TaskRunningStage.READY_TO_RUN, opts); - expect(runner.id).toEqual('foo'); - expect(runner.taskType).toEqual('bar'); - expect(runner.toString()).toEqual('bar "foo"'); - }); - - test('queues a reattempt if the task fails', async () => { - const initialAttempts = _.random(0, 2); - const id = Date.now().toString(); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - params: { a: 'b' }, - state: { hey: 'there' }, - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - throw new Error('Dangit!'); - }, - }), + describe('Pending Stage', () => { + test('provides details about the task that is running', async () => { + const { runner } = await pendingStageSetup({ + instance: { + id: 'foo', + taskType: 'bar', }, - }, + }); + + expect(runner.id).toEqual('foo'); + expect(runner.taskType).toEqual('bar'); + expect(runner.toString()).toEqual('bar "foo"'); }); - await runner.run(); + test('calculates retryAt by schedule when running a recurring task', async () => { + const intervalMinutes = 10; + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(0, 2); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: { + interval: `${intervalMinutes}m`, + }, + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; + await runner.markTaskAsRunning(); - expect(instance.id).toEqual(id); - expect(instance.runAt.getTime()).toEqual(minutesFromNow(initialAttempts * 5).getTime()); - expect(instance.params).toEqual({ a: 'b' }); - expect(instance.state).toEqual({ hey: 'there' }); - }); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; - test('reschedules tasks that have an schedule', async () => { - const { runner, store } = testOpts({ - instance: { - schedule: { interval: '10m' }, - status: TaskStatus.Running, - startedAt: new Date(), - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - return { state: {} }; - }, - }), - }, - }, + expect(instance.retryAt!.getTime()).toEqual( + instance.startedAt!.getTime() + intervalMinutes * 60 * 1000 + ); }); - await runner.run(); + test('calculates retryAt by default timout when it exceeds the schedule of a recurring task', async () => { + const intervalSeconds = 20; + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(0, 2); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: { + interval: `${intervalSeconds}s`, + }, + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; + await runner.markTaskAsRunning(); - expect(instance.runAt.getTime()).toBeGreaterThan(minutesFromNow(9).getTime()); - expect(instance.runAt.getTime()).toBeLessThanOrEqual(minutesFromNow(10).getTime()); - }); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; - test('expiration returns time after which timeout will have elapsed from start', async () => { - const now = moment(); - const { runner } = testOpts({ - instance: { - schedule: { interval: '10m' }, - status: TaskStatus.Running, - startedAt: now.toDate(), - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `1m`, - createTaskRunner: () => ({ - async run() { - return { state: {} }; - }, - }), - }, - }, + expect(instance.retryAt!.getTime()).toEqual(instance.startedAt!.getTime() + 5 * 60 * 1000); }); - await runner.run(); - - expect(runner.isExpired).toBe(false); - expect(runner.expiration).toEqual(now.add(1, 'm').toDate()); - }); - - test('runDuration returns duration which has elapsed since start', async () => { - const now = moment().subtract(30, 's').toDate(); - const { runner } = testOpts({ - instance: { - schedule: { interval: '10m' }, - status: TaskStatus.Running, - startedAt: now, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `1m`, - createTaskRunner: () => ({ - async run() { - return { state: {} }; - }, - }), + test('calculates retryAt by timeout if it exceeds the schedule when running a recurring task', async () => { + const timeoutMinutes = 1; + const intervalSeconds = 20; + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(0, 2); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: { + interval: `${intervalSeconds}s`, + }, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - await runner.run(); + await runner.markTaskAsRunning(); - expect(runner.isExpired).toBe(false); - expect(runner.startedAt).toEqual(now); - }); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; - test('reschedules tasks that return a runAt', async () => { - const runAt = minutesFromNow(_.random(1, 10)); - const { runner, store } = testOpts({ - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - return { runAt, state: {} }; - }, - }), - }, - }, + expect(instance.retryAt!.getTime()).toEqual( + instance.startedAt!.getTime() + timeoutMinutes * 60 * 1000 + ); }); - await runner.run(); - - sinon.assert.calledOnce(store.update); - sinon.assert.calledWithMatch(store.update, { runAt }); - }); - - test('reschedules tasks that return a schedule', async () => { - const runAt = minutesFromNow(1); - const schedule = { - interval: '1m', - }; - const { runner, store } = testOpts({ - instance: { - status: TaskStatus.Running, - startedAt: new Date(), - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - return { schedule, state: {} }; - }, - }), + test('sets startedAt, status, attempts and retryAt when claiming a task', async () => { + const timeoutMinutes = 1; + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(0, 2); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - await runner.run(); + await runner.markTaskAsRunning(); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWithMatch(store.update, { runAt }); - }); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; - test(`doesn't reschedule recurring tasks that throw an unrecoverable error`, async () => { - const id = _.random(1, 20).toString(); - const error = new Error('Dangit!'); - const onTaskEvent = jest.fn(); - const { runner, store, instance: originalInstance } = testOpts({ - onTaskEvent, - instance: { id, status: TaskStatus.Running, startedAt: new Date() }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - throwUnrecoverableError(error); - }, - }), - }, - }, + expect(instance.attempts).toEqual(initialAttempts + 1); + expect(instance.status).toBe('running'); + expect(instance.startedAt!.getTime()).toEqual(Date.now()); + expect(instance.retryAt!.getTime()).toEqual( + minutesFromNow((initialAttempts + 1) * 5).getTime() + timeoutMinutes * 60 * 1000 + ); }); - await runner.run(); - - const instance = store.update.args[0][0]; - expect(instance.status).toBe('failed'); - - expect(onTaskEvent).toHaveBeenCalledWith( - withAnyTiming( - asTaskRunEvent( + test('uses getRetry (returning date) to set retryAt when defined', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = await pendingStageSetup({ + instance: { id, - asErr({ - error, - task: originalInstance, - result: TaskRunResult.Failed, - }) - ) - ) - ); - expect(onTaskEvent).toHaveBeenCalledTimes(1); - }); - - test('tasks that return runAt override the schedule', async () => { - const runAt = minutesFromNow(_.random(5)); - const { runner, store } = testOpts({ - instance: { - schedule: { interval: '20m' }, - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - return { runAt, state: {} }; - }, - }), + attempts: initialAttempts, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - await runner.run(); + await runner.markTaskAsRunning(); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWithMatch(store.update, { runAt }); - }); + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.calledWith(getRetryStub, initialAttempts + 1); + const instance = store.update.mock.calls[0][0]; - test('removes non-recurring tasks after they complete', async () => { - const id = _.random(1, 20).toString(); - const { runner, store } = testOpts({ - instance: { - id, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - return undefined; - }, - }), - }, - }, + expect(instance.retryAt!.getTime()).toEqual( + new Date(nextRetry.getTime() + timeoutMinutes * 60 * 1000).getTime() + ); }); - await runner.run(); - - sinon.assert.calledOnce(store.remove); - sinon.assert.calledWith(store.remove, id); - }); - - test('cancel cancels the task runner, if it is cancellable', async () => { - let wasCancelled = false; - const { runner, logger } = testOpts({ - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - async run() { - const promise = new Promise((r) => setTimeout(r, 1000)); - fakeTimer.tick(1000); - await promise; - }, - async cancel() { - wasCancelled = true; - }, - }), + test('it returns false when markTaskAsRunning fails due to VERSION_CONFLICT_STATUS', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - const promise = runner.run(); - await Promise.resolve(); - await runner.cancel(); - await promise; + store.update.mockRejectedValue( + SavedObjectsErrorHelpers.decorateConflictError(new Error('repo error')) + ); - expect(wasCancelled).toBeTruthy(); - expect(logger.warn).not.toHaveBeenCalled(); - }); + expect(await runner.markTaskAsRunning()).toEqual(false); + }); - test('debug logs if cancel is called on a non-cancellable task', async () => { - const { runner, logger } = testOpts({ - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - run: async () => undefined, - }), + test('it throw when markTaskAsRunning fails for unexpected reasons', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - const promise = runner.run(); - await runner.cancel(); - await promise; + store.update.mockRejectedValue( + SavedObjectsErrorHelpers.createGenericNotFoundError('type', 'id') + ); - expect(logger.debug).toHaveBeenCalledWith(`The task bar "foo" is not cancellable.`); - }); + return expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot( + `[Error: Saved object [type/id] not found]` + ); + }); - test('sets startedAt, status, attempts and retryAt when claiming a task', async () => { - const timeoutMinutes = 1; - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(0, 2); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - createTaskRunner: () => ({ - run: async () => undefined, - }), + test(`it tries to increment a task's attempts when markTaskAsRunning fails for unexpected reasons`, async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - await runner.markTaskAsRunning(); + store.update.mockRejectedValueOnce(SavedObjectsErrorHelpers.createBadRequestError('type')); + store.update.mockResolvedValueOnce( + mockInstance({ + id, + attempts: initialAttempts, + schedule: undefined, + }) + ); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; + await expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot( + `[Error: type: Bad Request]` + ); - expect(instance.attempts).toEqual(initialAttempts + 1); - expect(instance.status).toBe('running'); - expect(instance.startedAt.getTime()).toEqual(Date.now()); - expect(instance.retryAt.getTime()).toEqual( - minutesFromNow((initialAttempts + 1) * 5).getTime() + timeoutMinutes * 60 * 1000 - ); - }); + expect(store.update).toHaveBeenCalledWith({ + ...mockInstance({ + id, + attempts: initialAttempts + 1, + schedule: undefined, + }), + status: TaskStatus.Idle, + startedAt: null, + retryAt: null, + ownerId: null, + }); + }); - test('calculates retryAt by schedule when running a recurring task', async () => { - const intervalMinutes = 10; - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(0, 2); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: { - interval: `${intervalMinutes}m`, + test(`it doesnt try to increment a task's attempts when markTaskAsRunning fails for version conflict`, async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - run: async () => undefined, - }), + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, }, - }, - }); + }); - await runner.markTaskAsRunning(); + store.update.mockRejectedValueOnce( + SavedObjectsErrorHelpers.createConflictError('type', 'id') + ); + store.update.mockResolvedValueOnce( + mockInstance({ + id, + attempts: initialAttempts, + schedule: undefined, + }) + ); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; + await expect(runner.markTaskAsRunning()).resolves.toMatchInlineSnapshot(`false`); - expect(instance.retryAt.getTime()).toEqual( - instance.startedAt.getTime() + intervalMinutes * 60 * 1000 - ); - }); + expect(store.update).toHaveBeenCalledTimes(1); + }); - test('calculates retryAt by default timout when it exceeds the schedule of a recurring task', async () => { - const intervalSeconds = 20; - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(0, 2); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: { - interval: `${intervalSeconds}s`, + test(`it doesnt try to increment a task's attempts when markTaskAsRunning fails due to Saved Object not being found`, async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(nextRetry); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - definitions: { - bar: { - title: 'Bar!', - createTaskRunner: () => ({ - run: async () => undefined, - }), + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, }, - }, - }); + }); - await runner.markTaskAsRunning(); + store.update.mockRejectedValueOnce( + SavedObjectsErrorHelpers.createGenericNotFoundError('type', 'id') + ); + store.update.mockResolvedValueOnce( + mockInstance({ + id, + attempts: initialAttempts, + schedule: undefined, + }) + ); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; + await expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot( + `[Error: Saved object [type/id] not found]` + ); - expect(instance.retryAt.getTime()).toEqual(instance.startedAt.getTime() + 5 * 60 * 1000); - }); + expect(store.update).toHaveBeenCalledTimes(1); + }); - test('calculates retryAt by timeout if it exceeds the schedule when running a recurring task', async () => { - const timeoutMinutes = 1; - const intervalSeconds = 20; - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(0, 2); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: { - interval: `${intervalSeconds}s`, + test('uses getRetry (returning true) to set retryAt when defined', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(true); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - createTaskRunner: () => ({ - run: async () => undefined, - }), + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, }, - }, - }); + }); - await runner.markTaskAsRunning(); + await runner.markTaskAsRunning(); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.calledWith(getRetryStub, initialAttempts + 1); + const instance = store.update.mock.calls[0][0]; - expect(instance.retryAt.getTime()).toEqual( - instance.startedAt.getTime() + timeoutMinutes * 60 * 1000 - ); - }); + const attemptDelay = (initialAttempts + 1) * 5 * 60 * 1000; + const timeoutDelay = timeoutMinutes * 60 * 1000; + expect(instance.retryAt!.getTime()).toEqual( + new Date(Date.now() + attemptDelay + timeoutDelay).getTime() + ); + }); - test('uses getRetry function (returning date) on error when defined', async () => { - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const id = Date.now().toString(); - const getRetryStub = sinon.stub().returns(nextRetry); - const error = new Error('Dangit!'); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - }, - definitions: { - bar: { - title: 'Bar!', - getRetry: getRetryStub, - createTaskRunner: () => ({ - async run() { - throw error; - }, - }), + test('uses getRetry (returning false) to set retryAt when defined', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(false); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - await runner.run(); + await runner.markTaskAsRunning(); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWith(getRetryStub, initialAttempts, error); - const instance = store.update.args[0][0]; + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.calledWith(getRetryStub, initialAttempts + 1); + const instance = store.update.mock.calls[0][0]; - expect(instance.runAt.getTime()).toEqual(nextRetry.getTime()); - }); + expect(instance.retryAt!).toBeNull(); + expect(instance.status).toBe('running'); + }); - test('uses getRetry function (returning true) on error when defined', async () => { - const initialAttempts = _.random(1, 3); - const id = Date.now().toString(); - const getRetryStub = sinon.stub().returns(true); - const error = new Error('Dangit!'); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - }, - definitions: { - bar: { - title: 'Bar!', - getRetry: getRetryStub, - createTaskRunner: () => ({ - async run() { - throw error; - }, - }), + test('bypasses getRetry (returning false) of a recurring task to set retryAt when defined', async () => { + const id = _.random(1, 20).toString(); + const initialAttempts = _.random(1, 3); + const timeoutMinutes = 1; + const getRetryStub = sinon.stub().returns(false); + const { runner, store } = await pendingStageSetup({ + instance: { + id, + attempts: initialAttempts, + schedule: { interval: '1m' }, + startedAt: new Date(), }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + timeout: `${timeoutMinutes}m`, + getRetry: getRetryStub, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - await runner.run(); + await runner.markTaskAsRunning(); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWith(getRetryStub, initialAttempts, error); - const instance = store.update.args[0][0]; + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.notCalled(getRetryStub); + const instance = store.update.mock.calls[0][0]; - const expectedRunAt = new Date(Date.now() + initialAttempts * 5 * 60 * 1000); - expect(instance.runAt.getTime()).toEqual(expectedRunAt.getTime()); - }); + const timeoutDelay = timeoutMinutes * 60 * 1000; + expect(instance.retryAt!.getTime()).toEqual(new Date(Date.now() + timeoutDelay).getTime()); + }); - test('uses getRetry function (returning false) on error when defined', async () => { - const initialAttempts = _.random(1, 3); - const id = Date.now().toString(); - const getRetryStub = sinon.stub().returns(false); - const error = new Error('Dangit!'); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - }, - definitions: { - bar: { - title: 'Bar!', - getRetry: getRetryStub, - createTaskRunner: () => ({ - async run() { - throw error; + describe('TaskEvents', () => { + test('emits TaskEvent when a task is marked as running', async () => { + const id = _.random(1, 20).toString(); + const onTaskEvent = jest.fn(); + const { runner, instance, store } = await pendingStageSetup({ + onTaskEvent, + instance: { + id, + }, + definitions: { + bar: { + title: 'Bar!', + timeout: `1m`, + createTaskRunner: () => ({ + run: async () => undefined, + }), }, - }), - }, - }, - }); + }, + }); - await runner.run(); + store.update.mockResolvedValueOnce(instance); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWith(getRetryStub, initialAttempts, error); - const instance = store.update.args[0][0]; + await runner.markTaskAsRunning(); - expect(instance.status).toBe('failed'); - }); + expect(onTaskEvent).toHaveBeenCalledWith(asTaskMarkRunningEvent(id, asOk(instance))); + }); - test('bypasses getRetry function (returning false) on error of a recurring task', async () => { - const initialAttempts = _.random(1, 3); - const id = Date.now().toString(); - const getRetryStub = sinon.stub().returns(false); - const error = new Error('Dangit!'); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: { interval: '1m' }, - startedAt: new Date(), - }, - definitions: { - bar: { - title: 'Bar!', - getRetry: getRetryStub, - createTaskRunner: () => ({ - async run() { - throw error; - }, - }), - }, - }, - }); + test('emits TaskEvent when a task fails to be marked as running', async () => { + expect.assertions(2); - await runner.run(); + const id = _.random(1, 20).toString(); + const onTaskEvent = jest.fn(); + const { runner, store } = await pendingStageSetup({ + onTaskEvent, + instance: { + id, + }, + definitions: { + bar: { + title: 'Bar!', + timeout: `1m`, + createTaskRunner: () => ({ + run: async () => undefined, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - sinon.assert.notCalled(getRetryStub); - const instance = store.update.args[0][0]; + store.update.mockRejectedValueOnce(new Error('cant mark as running')); - const nextIntervalDelay = 60000; // 1m - const expectedRunAt = new Date(Date.now() + nextIntervalDelay); - expect(instance.runAt.getTime()).toEqual(expectedRunAt.getTime()); + try { + await runner.markTaskAsRunning(); + } catch (err) { + expect(onTaskEvent).toHaveBeenCalledWith(asTaskMarkRunningEvent(id, asErr(err))); + } + expect(onTaskEvent).toHaveBeenCalledTimes(1); + }); + }); }); - test('uses getRetry (returning date) to set retryAt when defined', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(nextRetry); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), + describe('Ready To Run Stage', () => { + test('queues a reattempt if the task fails', async () => { + const initialAttempts = _.random(0, 2); + const id = Date.now().toString(); + const { runner, store } = await readyToRunStageSetup({ + instance: { + id, + attempts: initialAttempts, + params: { a: 'b' }, + state: { hey: 'there' }, }, - }, - }); - - await runner.markTaskAsRunning(); + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + throw new Error('Dangit!'); + }, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWith(getRetryStub, initialAttempts + 1); - const instance = store.update.args[0][0]; + await runner.run(); - expect(instance.retryAt.getTime()).toEqual( - new Date(nextRetry.getTime() + timeoutMinutes * 60 * 1000).getTime() - ); - }); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; - test('it returns false when markTaskAsRunning fails due to VERSION_CONFLICT_STATUS', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(nextRetry); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), - }, - }, + expect(instance.id).toEqual(id); + expect(instance.runAt.getTime()).toEqual(minutesFromNow(initialAttempts * 5).getTime()); + expect(instance.params).toEqual({ a: 'b' }); + expect(instance.state).toEqual({ hey: 'there' }); }); - store.update = sinon - .stub() - .throws(SavedObjectsErrorHelpers.decorateConflictError(new Error('repo error'))); - - expect(await runner.markTaskAsRunning()).toEqual(false); - }); - - test('it throw when markTaskAsRunning fails for unexpected reasons', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(nextRetry); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), + test('reschedules tasks that have an schedule', async () => { + const { runner, store } = await readyToRunStageSetup({ + instance: { + schedule: { interval: '10m' }, + status: TaskStatus.Running, + startedAt: new Date(), }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { state: {} }; + }, + }), + }, + }, + }); - store.update = sinon - .stub() - .throws(SavedObjectsErrorHelpers.createGenericNotFoundError('type', 'id')); + await runner.run(); - return expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot( - `[Error: Saved object [type/id] not found]` - ); - }); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; - test(`it tries to increment a task's attempts when markTaskAsRunning fails for unexpected reasons`, async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(nextRetry); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), - }, - }, + expect(instance.runAt.getTime()).toBeGreaterThan(minutesFromNow(9).getTime()); + expect(instance.runAt.getTime()).toBeLessThanOrEqual(minutesFromNow(10).getTime()); }); - store.update = sinon.stub(); - store.update.onFirstCall().throws(SavedObjectsErrorHelpers.createBadRequestError('type')); - store.update.onSecondCall().resolves(); + test('expiration returns time after which timeout will have elapsed from start', async () => { + const now = moment(); + const { runner } = await readyToRunStageSetup({ + instance: { + schedule: { interval: '10m' }, + status: TaskStatus.Running, + startedAt: now.toDate(), + }, + definitions: { + bar: { + title: 'Bar!', + timeout: `1m`, + createTaskRunner: () => ({ + async run() { + return { state: {} }; + }, + }), + }, + }, + }); - await expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot( - `[Error: type: Bad Request]` - ); + await runner.run(); - sinon.assert.calledWith(store.update, { - ...mockInstance({ - id, - attempts: initialAttempts + 1, - schedule: undefined, - }), - status: TaskStatus.Idle, - startedAt: null, - retryAt: null, - ownerId: null, + expect(runner.isExpired).toBe(false); + expect(runner.expiration).toEqual(now.add(1, 'm').toDate()); }); - }); - test(`it doesnt try to increment a task's attempts when markTaskAsRunning fails for version conflict`, async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(nextRetry); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), + test('runDuration returns duration which has elapsed since start', async () => { + const now = moment().subtract(30, 's').toDate(); + const { runner } = await readyToRunStageSetup({ + instance: { + schedule: { interval: '10m' }, + status: TaskStatus.Running, + startedAt: now, }, - }, - }); - - store.update = sinon.stub(); - store.update.onFirstCall().throws(SavedObjectsErrorHelpers.createConflictError('type', 'id')); - store.update.onSecondCall().resolves(); - - await expect(runner.markTaskAsRunning()).resolves.toMatchInlineSnapshot(`false`); + definitions: { + bar: { + title: 'Bar!', + timeout: `1m`, + createTaskRunner: () => ({ + async run() { + return { state: {} }; + }, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - }); + await runner.run(); - test(`it doesnt try to increment a task's attempts when markTaskAsRunning fails due to Saved Object not being found`, async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(nextRetry); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), - }, - }, + expect(runner.isExpired).toBe(false); + expect(runner.startedAt).toEqual(now); }); - store.update = sinon.stub(); - store.update - .onFirstCall() - .throws(SavedObjectsErrorHelpers.createGenericNotFoundError('type', 'id')); - store.update.onSecondCall().resolves(); - - await expect(runner.markTaskAsRunning()).rejects.toMatchInlineSnapshot( - `[Error: Saved object [type/id] not found]` - ); + test('reschedules tasks that return a runAt', async () => { + const runAt = minutesFromNow(_.random(1, 10)); + const { runner, store } = await readyToRunStageSetup({ + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { runAt, state: {} }; + }, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - }); + await runner.run(); - test('uses getRetry (returning true) to set retryAt when defined', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(true); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), - }, - }, + expect(store.update).toHaveBeenCalledTimes(1); + expect(store.update).toHaveBeenCalledWith(expect.objectContaining({ runAt })); }); - await runner.markTaskAsRunning(); - - sinon.assert.calledOnce(store.update); - sinon.assert.calledWith(getRetryStub, initialAttempts + 1); - const instance = store.update.args[0][0]; + test('reschedules tasks that return a schedule', async () => { + const runAt = minutesFromNow(1); + const schedule = { + interval: '1m', + }; + const { runner, store } = await readyToRunStageSetup({ + instance: { + status: TaskStatus.Running, + startedAt: new Date(), + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { schedule, state: {} }; + }, + }), + }, + }, + }); - const attemptDelay = (initialAttempts + 1) * 5 * 60 * 1000; - const timeoutDelay = timeoutMinutes * 60 * 1000; - expect(instance.retryAt.getTime()).toEqual( - new Date(Date.now() + attemptDelay + timeoutDelay).getTime() - ); - }); + await runner.run(); - test('uses getRetry (returning false) to set retryAt when defined', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(false); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), - }, - }, + expect(store.update).toHaveBeenCalledTimes(1); + expect(store.update).toHaveBeenCalledWith(expect.objectContaining({ runAt })); }); - await runner.markTaskAsRunning(); + test(`doesn't reschedule recurring tasks that throw an unrecoverable error`, async () => { + const id = _.random(1, 20).toString(); + const error = new Error('Dangit!'); + const onTaskEvent = jest.fn(); + const { runner, store, instance: originalInstance } = await readyToRunStageSetup({ + onTaskEvent, + instance: { id, status: TaskStatus.Running, startedAt: new Date() }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + throwUnrecoverableError(error); + }, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - sinon.assert.calledWith(getRetryStub, initialAttempts + 1); - const instance = store.update.args[0][0]; + await runner.run(); - expect(instance.retryAt).toBeNull(); - expect(instance.status).toBe('running'); - }); + const instance = store.update.mock.calls[0][0]; + expect(instance.status).toBe('failed'); - test('bypasses getRetry (returning false) of a recurring task to set retryAt when defined', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = _.random(1, 3); - const timeoutMinutes = 1; - const getRetryStub = sinon.stub().returns(false); - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: { interval: '1m' }, - startedAt: new Date(), - }, - definitions: { - bar: { - title: 'Bar!', - timeout: `${timeoutMinutes}m`, - getRetry: getRetryStub, - createTaskRunner: () => ({ - run: async () => undefined, - }), - }, - }, + expect(onTaskEvent).toHaveBeenCalledWith( + withAnyTiming( + asTaskRunEvent( + id, + asErr({ + error, + task: originalInstance, + result: TaskRunResult.Failed, + }) + ) + ) + ); + expect(onTaskEvent).toHaveBeenCalledTimes(1); }); - await runner.markTaskAsRunning(); + test('tasks that return runAt override the schedule', async () => { + const runAt = minutesFromNow(_.random(5)); + const { runner, store } = await readyToRunStageSetup({ + instance: { + schedule: { interval: '20m' }, + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { runAt, state: {} }; + }, + }), + }, + }, + }); - sinon.assert.calledOnce(store.update); - sinon.assert.notCalled(getRetryStub); - const instance = store.update.args[0][0]; + await runner.run(); - const timeoutDelay = timeoutMinutes * 60 * 1000; - expect(instance.retryAt.getTime()).toEqual(new Date(Date.now() + timeoutDelay).getTime()); - }); + expect(store.update).toHaveBeenCalledTimes(1); + expect(store.update).toHaveBeenCalledWith(expect.objectContaining({ runAt })); + }); - test('Fails non-recurring task when maxAttempts reached', async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = 3; - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: undefined, - }, - definitions: { - bar: { - title: 'Bar!', - maxAttempts: 3, - createTaskRunner: () => ({ - run: async () => { - throw new Error(); - }, - }), + test('removes non-recurring tasks after they complete', async () => { + const id = _.random(1, 20).toString(); + const { runner, store } = await readyToRunStageSetup({ + instance: { + id, + schedule: undefined, }, - }, - }); + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return undefined; + }, + }), + }, + }, + }); - await runner.run(); + await runner.run(); - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; - expect(instance.attempts).toEqual(3); - expect(instance.status).toEqual('failed'); - expect(instance.retryAt).toBeNull(); - expect(instance.runAt.getTime()).toBeLessThanOrEqual(Date.now()); - }); + expect(store.remove).toHaveBeenCalledTimes(1); + expect(store.remove).toHaveBeenCalledWith(id); + }); - test(`Doesn't fail recurring tasks when maxAttempts reached`, async () => { - const id = _.random(1, 20).toString(); - const initialAttempts = 3; - const intervalSeconds = 10; - const { runner, store } = testOpts({ - instance: { - id, - attempts: initialAttempts, - schedule: { interval: `${intervalSeconds}s` }, - startedAt: new Date(), - }, - definitions: { - bar: { - title: 'Bar!', - maxAttempts: 3, - createTaskRunner: () => ({ - run: async () => { - throw new Error(); - }, - }), + test('cancel cancels the task runner, if it is cancellable', async () => { + let wasCancelled = false; + const { runner, logger } = await readyToRunStageSetup({ + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + const promise = new Promise((r) => setTimeout(r, 1000)); + fakeTimer.tick(1000); + await promise; + }, + async cancel() { + wasCancelled = true; + }, + }), + }, }, - }, - }); + }); - await runner.run(); + const promise = runner.run(); + await Promise.resolve(); + await runner.cancel(); + await promise; - sinon.assert.calledOnce(store.update); - const instance = store.update.args[0][0]; - expect(instance.attempts).toEqual(3); - expect(instance.status).toEqual('idle'); - expect(instance.runAt.getTime()).toEqual( - new Date(Date.now() + intervalSeconds * 1000).getTime() - ); - }); + expect(wasCancelled).toBeTruthy(); + expect(logger.warn).not.toHaveBeenCalled(); + }); - describe('TaskEvents', () => { - test('emits TaskEvent when a task is marked as running', async () => { - const id = _.random(1, 20).toString(); - const onTaskEvent = jest.fn(); - const { runner, instance, store } = testOpts({ - onTaskEvent, - instance: { - id, - }, + test('debug logs if cancel is called on a non-cancellable task', async () => { + const { runner, logger } = await readyToRunStageSetup({ definitions: { bar: { title: 'Bar!', - timeout: `1m`, createTaskRunner: () => ({ run: async () => undefined, }), @@ -1010,58 +865,63 @@ describe('TaskManagerRunner', () => { }, }); - store.update.returns(instance); + const promise = runner.run(); + await runner.cancel(); + await promise; - await runner.markTaskAsRunning(); - - expect(onTaskEvent).toHaveBeenCalledWith(asTaskMarkRunningEvent(id, asOk(instance))); + expect(logger.debug).toHaveBeenCalledWith(`The task bar "foo" is not cancellable.`); }); - test('emits TaskEvent when a task fails to be marked as running', async () => { - expect.assertions(2); - - const id = _.random(1, 20).toString(); - const onTaskEvent = jest.fn(); - const { runner, store } = testOpts({ - onTaskEvent, + test('uses getRetry function (returning date) on error when defined', async () => { + const initialAttempts = _.random(1, 3); + const nextRetry = new Date(Date.now() + _.random(15, 100) * 1000); + const id = Date.now().toString(); + const getRetryStub = sinon.stub().returns(nextRetry); + const error = new Error('Dangit!'); + const { runner, store } = await readyToRunStageSetup({ instance: { id, + attempts: initialAttempts, }, definitions: { bar: { title: 'Bar!', - timeout: `1m`, + getRetry: getRetryStub, createTaskRunner: () => ({ - run: async () => undefined, + async run() { + throw error; + }, }), }, }, }); - store.update.throws(new Error('cant mark as running')); + await runner.run(); - try { - await runner.markTaskAsRunning(); - } catch (err) { - expect(onTaskEvent).toHaveBeenCalledWith(asTaskMarkRunningEvent(id, asErr(err))); - } - expect(onTaskEvent).toHaveBeenCalledTimes(1); + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.calledWith(getRetryStub, initialAttempts, error); + const instance = store.update.mock.calls[0][0]; + + expect(instance.runAt.getTime()).toEqual(nextRetry.getTime()); }); - test('emits TaskEvent when a task is run successfully', async () => { - const id = _.random(1, 20).toString(); - const onTaskEvent = jest.fn(); - const { runner, instance } = testOpts({ - onTaskEvent, + test('uses getRetry function (returning true) on error when defined', async () => { + const initialAttempts = _.random(1, 3); + const id = Date.now().toString(); + const getRetryStub = sinon.stub().returns(true); + const error = new Error('Dangit!'); + const { runner, store } = await readyToRunStageSetup({ instance: { id, + attempts: initialAttempts, }, definitions: { bar: { title: 'Bar!', + getRetry: getRetryStub, createTaskRunner: () => ({ async run() { - return { state: {} }; + throw error; }, }), }, @@ -1070,27 +930,31 @@ describe('TaskManagerRunner', () => { await runner.run(); - expect(onTaskEvent).toHaveBeenCalledWith( - withAnyTiming(asTaskRunEvent(id, asOk({ task: instance, result: TaskRunResult.Success }))) - ); + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.calledWith(getRetryStub, initialAttempts, error); + const instance = store.update.mock.calls[0][0]; + + const expectedRunAt = new Date(Date.now() + initialAttempts * 5 * 60 * 1000); + expect(instance.runAt.getTime()).toEqual(expectedRunAt.getTime()); }); - test('emits TaskEvent when a recurring task is run successfully', async () => { - const id = _.random(1, 20).toString(); - const runAt = minutesFromNow(_.random(5)); - const onTaskEvent = jest.fn(); - const { runner, instance } = testOpts({ - onTaskEvent, + test('uses getRetry function (returning false) on error when defined', async () => { + const initialAttempts = _.random(1, 3); + const id = Date.now().toString(); + const getRetryStub = sinon.stub().returns(false); + const error = new Error('Dangit!'); + const { runner, store } = await readyToRunStageSetup({ instance: { id, - schedule: { interval: '1m' }, + attempts: initialAttempts, }, definitions: { bar: { title: 'Bar!', + getRetry: getRetryStub, createTaskRunner: () => ({ async run() { - return { runAt, state: {} }; + throw error; }, }), }, @@ -1099,23 +963,29 @@ describe('TaskManagerRunner', () => { await runner.run(); - expect(onTaskEvent).toHaveBeenCalledWith( - withAnyTiming(asTaskRunEvent(id, asOk({ task: instance, result: TaskRunResult.Success }))) - ); + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.calledWith(getRetryStub, initialAttempts, error); + const instance = store.update.mock.calls[0][0]; + + expect(instance.status).toBe('failed'); }); - test('emits TaskEvent when a task run throws an error', async () => { - const id = _.random(1, 20).toString(); + test('bypasses getRetry function (returning false) on error of a recurring task', async () => { + const initialAttempts = _.random(1, 3); + const id = Date.now().toString(); + const getRetryStub = sinon.stub().returns(false); const error = new Error('Dangit!'); - const onTaskEvent = jest.fn(); - const { runner, instance } = testOpts({ - onTaskEvent, + const { runner, store } = await readyToRunStageSetup({ instance: { id, + attempts: initialAttempts, + schedule: { interval: '1m' }, + startedAt: new Date(), }, definitions: { bar: { title: 'Bar!', + getRetry: getRetryStub, createTaskRunner: () => ({ async run() { throw error; @@ -1124,33 +994,34 @@ describe('TaskManagerRunner', () => { }, }, }); + await runner.run(); - expect(onTaskEvent).toHaveBeenCalledWith( - withAnyTiming( - asTaskRunEvent(id, asErr({ error, task: instance, result: TaskRunResult.RetryScheduled })) - ) - ); - expect(onTaskEvent).toHaveBeenCalledTimes(1); + expect(store.update).toHaveBeenCalledTimes(1); + sinon.assert.notCalled(getRetryStub); + const instance = store.update.mock.calls[0][0]; + + const nextIntervalDelay = 60000; // 1m + const expectedRunAt = new Date(Date.now() + nextIntervalDelay); + expect(instance.runAt.getTime()).toEqual(expectedRunAt.getTime()); }); - test('emits TaskEvent when a task run returns an error', async () => { + test('Fails non-recurring task when maxAttempts reached', async () => { const id = _.random(1, 20).toString(); - const error = new Error('Dangit!'); - const onTaskEvent = jest.fn(); - const { runner, instance } = testOpts({ - onTaskEvent, + const initialAttempts = 3; + const { runner, store } = await readyToRunStageSetup({ instance: { id, - schedule: { interval: '1m' }, - startedAt: new Date(), + attempts: initialAttempts, + schedule: undefined, }, definitions: { bar: { title: 'Bar!', + maxAttempts: 3, createTaskRunner: () => ({ - async run() { - return { error, state: {} }; + run: async () => { + throw new Error(); }, }), }, @@ -1159,31 +1030,32 @@ describe('TaskManagerRunner', () => { await runner.run(); - expect(onTaskEvent).toHaveBeenCalledWith( - withAnyTiming( - asTaskRunEvent(id, asErr({ error, task: instance, result: TaskRunResult.RetryScheduled })) - ) - ); - expect(onTaskEvent).toHaveBeenCalledTimes(1); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; + expect(instance.attempts).toEqual(3); + expect(instance.status).toEqual('failed'); + expect(instance.retryAt!).toBeNull(); + expect(instance.runAt.getTime()).toBeLessThanOrEqual(Date.now()); }); - test('emits TaskEvent when a task returns an error and is marked as failed', async () => { + test(`Doesn't fail recurring tasks when maxAttempts reached`, async () => { const id = _.random(1, 20).toString(); - const error = new Error('Dangit!'); - const onTaskEvent = jest.fn(); - const { runner, store, instance: originalInstance } = testOpts({ - onTaskEvent, + const initialAttempts = 3; + const intervalSeconds = 10; + const { runner, store } = await readyToRunStageSetup({ instance: { id, + attempts: initialAttempts, + schedule: { interval: `${intervalSeconds}s` }, startedAt: new Date(), }, definitions: { bar: { title: 'Bar!', - getRetry: () => false, + maxAttempts: 3, createTaskRunner: () => ({ - async run() { - return { error, state: {} }; + run: async () => { + throw new Error(); }, }), }, @@ -1192,29 +1064,190 @@ describe('TaskManagerRunner', () => { await runner.run(); - const instance = store.update.args[0][0]; - expect(instance.status).toBe('failed'); + expect(store.update).toHaveBeenCalledTimes(1); + const instance = store.update.mock.calls[0][0]; + expect(instance.attempts).toEqual(3); + expect(instance.status).toEqual('idle'); + expect(instance.runAt.getTime()).toEqual( + new Date(Date.now() + intervalSeconds * 1000).getTime() + ); + }); - expect(onTaskEvent).toHaveBeenCalledWith( - withAnyTiming( - asTaskRunEvent( + describe('TaskEvents', () => { + test('emits TaskEvent when a task is run successfully', async () => { + const id = _.random(1, 20).toString(); + const onTaskEvent = jest.fn(); + const { runner, instance } = await readyToRunStageSetup({ + onTaskEvent, + instance: { id, - asErr({ - error, - task: originalInstance, - result: TaskRunResult.Failed, - }) + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { state: {} }; + }, + }), + }, + }, + }); + + await runner.run(); + + expect(onTaskEvent).toHaveBeenCalledWith( + withAnyTiming(asTaskRunEvent(id, asOk({ task: instance, result: TaskRunResult.Success }))) + ); + }); + + test('emits TaskEvent when a recurring task is run successfully', async () => { + const id = _.random(1, 20).toString(); + const runAt = minutesFromNow(_.random(5)); + const onTaskEvent = jest.fn(); + const { runner, instance } = await readyToRunStageSetup({ + onTaskEvent, + instance: { + id, + schedule: { interval: '1m' }, + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { runAt, state: {} }; + }, + }), + }, + }, + }); + + await runner.run(); + + expect(onTaskEvent).toHaveBeenCalledWith( + withAnyTiming(asTaskRunEvent(id, asOk({ task: instance, result: TaskRunResult.Success }))) + ); + }); + + test('emits TaskEvent when a task run throws an error', async () => { + const id = _.random(1, 20).toString(); + const error = new Error('Dangit!'); + const onTaskEvent = jest.fn(); + const { runner, instance } = await readyToRunStageSetup({ + onTaskEvent, + instance: { + id, + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + throw error; + }, + }), + }, + }, + }); + await runner.run(); + + expect(onTaskEvent).toHaveBeenCalledWith( + withAnyTiming( + asTaskRunEvent( + id, + asErr({ error, task: instance, result: TaskRunResult.RetryScheduled }) + ) ) - ) - ); - expect(onTaskEvent).toHaveBeenCalledTimes(1); + ); + expect(onTaskEvent).toHaveBeenCalledTimes(1); + }); + + test('emits TaskEvent when a task run returns an error', async () => { + const id = _.random(1, 20).toString(); + const error = new Error('Dangit!'); + const onTaskEvent = jest.fn(); + const { runner, instance } = await readyToRunStageSetup({ + onTaskEvent, + instance: { + id, + schedule: { interval: '1m' }, + startedAt: new Date(), + }, + definitions: { + bar: { + title: 'Bar!', + createTaskRunner: () => ({ + async run() { + return { error, state: {} }; + }, + }), + }, + }, + }); + + await runner.run(); + + expect(onTaskEvent).toHaveBeenCalledWith( + withAnyTiming( + asTaskRunEvent( + id, + asErr({ error, task: instance, result: TaskRunResult.RetryScheduled }) + ) + ) + ); + expect(onTaskEvent).toHaveBeenCalledTimes(1); + }); + + test('emits TaskEvent when a task returns an error and is marked as failed', async () => { + const id = _.random(1, 20).toString(); + const error = new Error('Dangit!'); + const onTaskEvent = jest.fn(); + const { runner, store, instance: originalInstance } = await readyToRunStageSetup({ + onTaskEvent, + instance: { + id, + startedAt: new Date(), + }, + definitions: { + bar: { + title: 'Bar!', + getRetry: () => false, + createTaskRunner: () => ({ + async run() { + return { error, state: {} }; + }, + }), + }, + }, + }); + + await runner.run(); + + const instance = store.update.mock.calls[0][0]; + expect(instance.status).toBe('failed'); + + expect(onTaskEvent).toHaveBeenCalledWith( + withAnyTiming( + asTaskRunEvent( + id, + asErr({ + error, + task: originalInstance, + result: TaskRunResult.Failed, + }) + ) + ) + ); + expect(onTaskEvent).toHaveBeenCalledTimes(1); + }); }); }); interface TestOpts { instance?: Partial; definitions?: TaskDefinitionRegistry; - onTaskEvent?: (event: TaskEvent) => void; + onTaskEvent?: jest.Mock<(event: TaskEvent) => void>; } function withAnyTiming(taskRun: TaskRun) { @@ -1247,20 +1280,16 @@ describe('TaskManagerRunner', () => { ); } - function testOpts(opts: TestOpts) { + async function testOpts(stage: TaskRunningStage, opts: TestOpts) { const callCluster = sinon.stub(); const createTaskRunner = sinon.stub(); const logger = mockLogger(); const instance = mockInstance(opts.instance); - const store = { - update: sinon.stub(), - remove: sinon.stub(), - maxAttempts: 5, - }; + const store = taskStoreMock.create(); - store.update.returns(instance); + store.update.mockResolvedValue(instance); const definitions = new TaskTypeDictionary(logger); definitions.registerTaskDefinitions({ @@ -1274,6 +1303,7 @@ describe('TaskManagerRunner', () => { } const runner = new TaskManagerRunner({ + defaultMaxAttempts: 5, beforeRun: (context) => Promise.resolve(context), beforeMarkRunning: (context) => Promise.resolve(context), logger, @@ -1283,6 +1313,15 @@ describe('TaskManagerRunner', () => { onTaskEvent: opts.onTaskEvent, }); + if (stage === TaskRunningStage.READY_TO_RUN) { + await runner.markTaskAsRunning(); + // as we're testing the ReadyToRun stage specifically, clear mocks cakked by setup + store.update.mockClear(); + if (opts.onTaskEvent) { + opts.onTaskEvent.mockClear(); + } + } + return { callCluster, createTaskRunner, diff --git a/x-pack/plugins/task_manager/server/task_running/task_runner.ts b/x-pack/plugins/task_manager/server/task_running/task_runner.ts index ad5a2e11409ec..8e061eae46028 100644 --- a/x-pack/plugins/task_manager/server/task_running/task_runner.ts +++ b/x-pack/plugins/task_manager/server/task_running/task_runner.ts @@ -63,11 +63,22 @@ export interface TaskRunner { markTaskAsRunning: () => Promise; run: () => Promise>; id: string; + stage: string; toString: () => string; } +export enum TaskRunningStage { + PENDING = 'PENDING', + READY_TO_RUN = 'READY_TO_RUN', + RAN = 'RAN', +} +export interface TaskRunning { + timestamp: Date; + stage: Stage; + task: Instance; +} + export interface Updatable { - readonly maxAttempts: number; update(doc: ConcreteTaskInstance): Promise; remove(id: string): Promise; } @@ -78,6 +89,7 @@ type Opts = { instance: ConcreteTaskInstance; store: Updatable; onTaskEvent?: (event: TaskRun | TaskMarkRunning) => void; + defaultMaxAttempts: number; } & Pick; export enum TaskRunResult { @@ -91,6 +103,16 @@ export enum TaskRunResult { Failed = 'Failed', } +// A ConcreteTaskInstance which we *know* has a `startedAt` Date on it +type ConcreteTaskInstanceWithStartedAt = ConcreteTaskInstance & { startedAt: Date }; + +// The three possible stages for a Task Runner - Pending -> ReadyToRun -> Ran +type PendingTask = TaskRunning; +type ReadyToRunTask = TaskRunning; +type RanTask = TaskRunning; + +type TaskRunningInstance = PendingTask | ReadyToRunTask | RanTask; + /** * Runs a background task, ensures that errors are properly handled, * allows for cancellation. @@ -101,13 +123,14 @@ export enum TaskRunResult { */ export class TaskManagerRunner implements TaskRunner { private task?: CancellableTask; - private instance: ConcreteTaskInstance; + private instance: TaskRunningInstance; private definitions: TaskTypeDictionary; private logger: Logger; private bufferedTaskStore: Updatable; private beforeRun: Middleware['beforeRun']; private beforeMarkRunning: Middleware['beforeMarkRunning']; private onTaskEvent: (event: TaskRun | TaskMarkRunning) => void; + private defaultMaxAttempts: number; /** * Creates an instance of TaskManagerRunner. @@ -126,29 +149,38 @@ export class TaskManagerRunner implements TaskRunner { store, beforeRun, beforeMarkRunning, + defaultMaxAttempts, onTaskEvent = identity, }: Opts) { - this.instance = sanitizeInstance(instance); + this.instance = asPending(sanitizeInstance(instance)); this.definitions = definitions; this.logger = logger; this.bufferedTaskStore = store; this.beforeRun = beforeRun; this.beforeMarkRunning = beforeMarkRunning; this.onTaskEvent = onTaskEvent; + this.defaultMaxAttempts = defaultMaxAttempts; } /** * Gets the id of this task instance. */ public get id() { - return this.instance.id; + return this.instance.task.id; } /** * Gets the task type of this task instance. */ public get taskType() { - return this.instance.taskType; + return this.instance.task.taskType; + } + + /** + * Get the stage this TaskRunner is at + */ + public get stage() { + return this.instance.stage; } /** @@ -162,14 +194,21 @@ export class TaskManagerRunner implements TaskRunner { * Gets the time at which this task will expire. */ public get expiration() { - return intervalFromDate(this.instance.startedAt!, this.definition.timeout)!; + return intervalFromDate( + // if the task is running, use it's started at, otherwise use the timestamp at + // which it was last updated + // this allows us to catch tasks that remain in Pending/Finalizing without being + // cleaned up + isReadyToRun(this.instance) ? this.instance.task.startedAt : this.instance.timestamp, + this.definition.timeout + )!; } /** * Gets the duration of the current task run */ public get startedAt() { - return this.instance.startedAt; + return this.instance.task.startedAt; } /** @@ -195,9 +234,16 @@ export class TaskManagerRunner implements TaskRunner { * @returns {Promise>} */ public async run(): Promise> { + if (!isReadyToRun(this.instance)) { + throw new Error( + `Running task ${this} failed as it ${ + isPending(this.instance) ? `isn't ready to be ran` : `has already been ran` + }` + ); + } this.logger.debug(`Running task ${this}`); const modifiedContext = await this.beforeRun({ - taskInstance: this.instance, + taskInstance: this.instance.task, }); const stopTaskTimer = startTaskTimer(); @@ -230,10 +276,16 @@ export class TaskManagerRunner implements TaskRunner { * @returns {Promise} */ public async markTaskAsRunning(): Promise { + if (!isPending(this.instance)) { + throw new Error( + `Marking task ${this} as running has failed as it ${ + isReadyToRun(this.instance) ? `is already running` : `has already been ran` + }` + ); + } performance.mark('markTaskAsRunning_start'); const apmTrans = apm.startTransaction(`taskManager markTaskAsRunning`, 'taskManager'); - apmTrans?.addLabels({ taskType: this.taskType, }); @@ -241,7 +293,7 @@ export class TaskManagerRunner implements TaskRunner { const now = new Date(); try { const { taskInstance } = await this.beforeMarkRunning({ - taskInstance: this.instance, + taskInstance: this.instance.task, }); const attempts = taskInstance.attempts + 1; @@ -258,22 +310,29 @@ export class TaskManagerRunner implements TaskRunner { ); } - this.instance = await this.bufferedTaskStore.update({ - ...taskInstance, - status: TaskStatus.Running, - startedAt: now, - attempts, - retryAt: - (this.instance.schedule - ? maxIntervalFromDate(now, this.instance.schedule!.interval, this.definition.timeout) - : this.getRetryDelay({ - attempts, - // Fake an error. This allows retry logic when tasks keep timing out - // and lets us set a proper "retryAt" value each time. - error: new Error('Task timeout'), - addDuration: this.definition.timeout, - })) ?? null, - }); + this.instance = asReadyToRun( + (await this.bufferedTaskStore.update({ + ...taskInstance, + status: TaskStatus.Running, + startedAt: now, + attempts, + retryAt: + (this.instance.task.schedule + ? maxIntervalFromDate( + now, + this.instance.task.schedule.interval, + this.definition.timeout + ) + : this.getRetryDelay({ + attempts, + // Fake an error. This allows retry logic when tasks keep timing out + // and lets us set a proper "retryAt" value each time. + error: new Error('Task timeout'), + addDuration: this.definition.timeout, + })) ?? null, + // This is a safe convertion as we're setting the startAt above + })) as ConcreteTaskInstanceWithStartedAt + ); const timeUntilClaimExpiresAfterUpdate = howManyMsUntilOwnershipClaimExpires( ownershipClaimedUntil @@ -288,7 +347,7 @@ export class TaskManagerRunner implements TaskRunner { if (apmTrans) apmTrans.end('success'); performanceStopMarkingTaskAsRunning(); - this.onTaskEvent(asTaskMarkRunningEvent(this.id, asOk(this.instance))); + this.onTaskEvent(asTaskMarkRunningEvent(this.id, asOk(this.instance.task))); return true; } catch (error) { if (apmTrans) apmTrans.end('failure'); @@ -299,7 +358,7 @@ export class TaskManagerRunner implements TaskRunner { // try to release claim as an unknown failure prevented us from marking as running mapErr((errReleaseClaim: Error) => { this.logger.error( - `[Task Runner] Task ${this.instance.id} failed to release claim after failure: ${errReleaseClaim}` + `[Task Runner] Task ${this.id} failed to release claim after failure: ${errReleaseClaim}` ); }, await this.releaseClaimAndIncrementAttempts()); } @@ -336,9 +395,9 @@ export class TaskManagerRunner implements TaskRunner { private async releaseClaimAndIncrementAttempts(): Promise> { return promiseResult( this.bufferedTaskStore.update({ - ...this.instance, + ...this.instance.task, status: TaskStatus.Idle, - attempts: this.instance.attempts + 1, + attempts: this.instance.task.attempts + 1, startedAt: null, retryAt: null, ownerId: null, @@ -347,12 +406,12 @@ export class TaskManagerRunner implements TaskRunner { } private shouldTryToScheduleRetry(): boolean { - if (this.instance.schedule) { + if (this.instance.task.schedule) { return true; } - const maxAttempts = this.definition.maxAttempts || this.bufferedTaskStore.maxAttempts; - return this.instance.attempts < maxAttempts; + const maxAttempts = this.definition.maxAttempts || this.defaultMaxAttempts; + return this.instance.task.attempts < maxAttempts; } private rescheduleFailedRun = ( @@ -361,7 +420,7 @@ export class TaskManagerRunner implements TaskRunner { const { state, error } = failureResult; if (this.shouldTryToScheduleRetry() && !isUnrecoverableError(error)) { // if we're retrying, keep the number of attempts - const { schedule, attempts } = this.instance; + const { schedule, attempts } = this.instance.task; const reschedule = failureResult.runAt ? { runAt: failureResult.runAt } @@ -399,7 +458,7 @@ export class TaskManagerRunner implements TaskRunner { // if retrying is possible (new runAt) or this is an recurring task - reschedule mapOk( ({ runAt, schedule: reschedule, state, attempts = 0 }: Partial) => { - const { startedAt, schedule } = this.instance; + const { startedAt, schedule } = this.instance.task; return asOk({ runAt: runAt || intervalFromDate(startedAt!, reschedule?.interval ?? schedule?.interval)!, @@ -413,16 +472,18 @@ export class TaskManagerRunner implements TaskRunner { unwrap )(result); - await this.bufferedTaskStore.update( - defaults( - { - ...fieldUpdates, - // reset fields that track the lifecycle of the concluded `task run` - startedAt: null, - retryAt: null, - ownerId: null, - }, - this.instance + this.instance = asRan( + await this.bufferedTaskStore.update( + defaults( + { + ...fieldUpdates, + // reset fields that track the lifecycle of the concluded `task run` + startedAt: null, + retryAt: null, + ownerId: null, + }, + this.instance.task + ) ) ); @@ -436,7 +497,8 @@ export class TaskManagerRunner implements TaskRunner { private async processResultWhenDone(): Promise { // not a recurring task: clean up by removing the task instance from store try { - await this.bufferedTaskStore.remove(this.instance.id); + await this.bufferedTaskStore.remove(this.id); + this.instance = asRan(this.instance.task); } catch (err) { if (err.statusCode === 404) { this.logger.warn(`Task cleanup of ${this} failed in processing. Was remove called twice?`); @@ -451,7 +513,7 @@ export class TaskManagerRunner implements TaskRunner { result: Result, taskTiming: TaskTiming ): Promise> { - const task = this.instance; + const { task } = this.instance; await eitherAsync( result, async ({ runAt, schedule }: SuccessfulRunResult) => { @@ -528,3 +590,38 @@ function performanceStopMarkingTaskAsRunning() { 'markTaskAsRunning_stop' ); } + +// A type that extracts the Instance type out of TaskRunningStage +// This helps us to better communicate to the developer what the expected "stage" +// in a specific place in the code might be +type InstanceOf = T extends TaskRunning ? I : never; + +function isPending(taskRunning: TaskRunningInstance): taskRunning is PendingTask { + return taskRunning.stage === TaskRunningStage.PENDING; +} +function asPending(task: InstanceOf): PendingTask { + return { + timestamp: new Date(), + stage: TaskRunningStage.PENDING, + task, + }; +} +function isReadyToRun(taskRunning: TaskRunningInstance): taskRunning is ReadyToRunTask { + return taskRunning.stage === TaskRunningStage.READY_TO_RUN; +} +function asReadyToRun( + task: InstanceOf +): ReadyToRunTask { + return { + timestamp: new Date(), + stage: TaskRunningStage.READY_TO_RUN, + task, + }; +} +function asRan(task: InstanceOf): RanTask { + return { + timestamp: new Date(), + stage: TaskRunningStage.RAN, + task, + }; +} diff --git a/x-pack/plugins/task_manager/server/task_scheduling.test.ts b/x-pack/plugins/task_manager/server/task_scheduling.test.ts index e495d416d5ab8..b142f2091291e 100644 --- a/x-pack/plugins/task_manager/server/task_scheduling.test.ts +++ b/x-pack/plugins/task_manager/server/task_scheduling.test.ts @@ -7,13 +7,14 @@ import _ from 'lodash'; import { Subject } from 'rxjs'; -import { none } from 'fp-ts/lib/Option'; +import { none, some } from 'fp-ts/lib/Option'; import { asTaskMarkRunningEvent, asTaskRunEvent, asTaskClaimEvent, asTaskRunRequestEvent, + TaskClaimErrorType, } from './task_events'; import { TaskLifecycleEvent } from './polling_lifecycle'; import { taskPollingLifecycleMock } from './polling_lifecycle.mock'; @@ -24,17 +25,28 @@ import { createInitialMiddleware } from './lib/middleware'; import { taskStoreMock } from './task_store.mock'; import { TaskRunResult } from './task_running'; import { mockLogger } from './test_utils'; +import { TaskTypeDictionary } from './task_type_dictionary'; describe('TaskScheduling', () => { const mockTaskStore = taskStoreMock.create({}); const mockTaskManager = taskPollingLifecycleMock.create({}); + const definitions = new TaskTypeDictionary(mockLogger()); const taskSchedulingOpts = { taskStore: mockTaskStore, taskPollingLifecycle: mockTaskManager, logger: mockLogger(), middleware: createInitialMiddleware(), + definitions, }; + definitions.registerTaskDefinitions({ + foo: { + title: 'foo', + maxConcurrency: 2, + createTaskRunner: jest.fn(), + }, + }); + beforeEach(() => { jest.resetAllMocks(); }); @@ -114,7 +126,7 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - const task = { id } as ConcreteTaskInstance; + const task = mockTask({ id }); events$.next(asTaskRunEvent(id, asOk({ task, result: TaskRunResult.Success }))); return expect(result).resolves.toEqual({ id }); @@ -131,7 +143,7 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - const task = { id } as ConcreteTaskInstance; + const task = mockTask({ id }); events$.next(asTaskClaimEvent(id, asOk(task))); events$.next(asTaskMarkRunningEvent(id, asOk(task))); events$.next( @@ -161,7 +173,7 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - const task = { id } as ConcreteTaskInstance; + const task = mockTask({ id }); events$.next(asTaskClaimEvent(id, asOk(task))); events$.next(asTaskMarkRunningEvent(id, asErr(new Error('some thing gone wrong')))); @@ -183,7 +195,12 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - events$.next(asTaskClaimEvent(id, asErr(none))); + events$.next( + asTaskClaimEvent( + id, + asErr({ task: none, errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED }) + ) + ); await expect(result).rejects.toEqual( new Error(`Failed to run task "${id}" as it does not exist`) @@ -192,6 +209,34 @@ describe('TaskScheduling', () => { expect(mockTaskStore.getLifecycle).toHaveBeenCalledWith(id); }); + test('when a task claim due to insufficient capacity we return an explciit message', async () => { + const events$ = new Subject(); + const id = '01ddff11-e88a-4d13-bc4e-256164e755e2'; + + mockTaskStore.getLifecycle.mockResolvedValue(TaskLifecycleResult.NotFound); + + const taskScheduling = new TaskScheduling({ + ...taskSchedulingOpts, + taskPollingLifecycle: taskPollingLifecycleMock.create({ events$ }), + }); + + const result = taskScheduling.runNow(id); + + const task = mockTask({ id, taskType: 'foo' }); + events$.next( + asTaskClaimEvent( + id, + asErr({ task: some(task), errorType: TaskClaimErrorType.CLAIMED_BY_ID_OUT_OF_CAPACITY }) + ) + ); + + await expect(result).rejects.toEqual( + new Error( + `Failed to run task "${id}" as we would exceed the max concurrency of "${task.taskType}" which is 2. Rescheduled the task to ensure it is picked up as soon as possible.` + ) + ); + }); + test('when a task claim fails we ensure the task isnt already claimed', async () => { const events$ = new Subject(); const id = '01ddff11-e88a-4d13-bc4e-256164e755e2'; @@ -205,7 +250,12 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - events$.next(asTaskClaimEvent(id, asErr(none))); + events$.next( + asTaskClaimEvent( + id, + asErr({ task: none, errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED }) + ) + ); await expect(result).rejects.toEqual( new Error(`Failed to run task "${id}" as it is currently running`) @@ -227,7 +277,12 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - events$.next(asTaskClaimEvent(id, asErr(none))); + events$.next( + asTaskClaimEvent( + id, + asErr({ task: none, errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED }) + ) + ); await expect(result).rejects.toEqual( new Error(`Failed to run task "${id}" as it is currently running`) @@ -270,7 +325,12 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - events$.next(asTaskClaimEvent(id, asErr(none))); + events$.next( + asTaskClaimEvent( + id, + asErr({ task: none, errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED }) + ) + ); await expect(result).rejects.toMatchInlineSnapshot( `[Error: Failed to run task "01ddff11-e88a-4d13-bc4e-256164e755e2" for unknown reason (Current Task Lifecycle is "idle")]` @@ -292,7 +352,12 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - events$.next(asTaskClaimEvent(id, asErr(none))); + events$.next( + asTaskClaimEvent( + id, + asErr({ task: none, errorType: TaskClaimErrorType.CLAIMED_BY_ID_NOT_RETURNED }) + ) + ); await expect(result).rejects.toMatchInlineSnapshot( `[Error: Failed to run task "01ddff11-e88a-4d13-bc4e-256164e755e2" for unknown reason (Current Task Lifecycle is "failed")]` @@ -313,7 +378,7 @@ describe('TaskScheduling', () => { const result = taskScheduling.runNow(id); - const task = { id } as ConcreteTaskInstance; + const task = mockTask({ id }); const otherTask = { id: differentTask } as ConcreteTaskInstance; events$.next(asTaskClaimEvent(id, asOk(task))); events$.next(asTaskClaimEvent(differentTask, asOk(otherTask))); @@ -338,3 +403,23 @@ describe('TaskScheduling', () => { }); }); }); + +function mockTask(overrides: Partial = {}): ConcreteTaskInstance { + return { + id: 'claimed-by-id', + runAt: new Date(), + taskType: 'foo', + schedule: undefined, + attempts: 0, + status: TaskStatus.Claiming, + params: { hello: 'world' }, + state: { baby: 'Henhen' }, + user: 'jimbo', + scope: ['reporting'], + ownerId: '', + startedAt: null, + retryAt: null, + scheduledAt: new Date(), + ...overrides, + }; +} diff --git a/x-pack/plugins/task_manager/server/task_scheduling.ts b/x-pack/plugins/task_manager/server/task_scheduling.ts index 8ccedb85c560d..29e83ec911b79 100644 --- a/x-pack/plugins/task_manager/server/task_scheduling.ts +++ b/x-pack/plugins/task_manager/server/task_scheduling.ts @@ -8,7 +8,7 @@ import { filter } from 'rxjs/operators'; import { pipe } from 'fp-ts/lib/pipeable'; -import { Option, map as mapOptional, getOrElse } from 'fp-ts/lib/Option'; +import { Option, map as mapOptional, getOrElse, isSome } from 'fp-ts/lib/Option'; import { Logger } from '../../../../src/core/server'; import { asOk, either, map, mapErr, promiseResult } from './lib/result_type'; @@ -20,6 +20,8 @@ import { ErroredTask, OkResultOf, ErrResultOf, + ClaimTaskErr, + TaskClaimErrorType, } from './task_events'; import { Middleware } from './lib/middleware'; import { @@ -33,6 +35,7 @@ import { import { TaskStore } from './task_store'; import { ensureDeprecatedFieldsAreCorrected } from './lib/correct_deprecated_fields'; import { TaskLifecycleEvent, TaskPollingLifecycle } from './polling_lifecycle'; +import { TaskTypeDictionary } from './task_type_dictionary'; const VERSION_CONFLICT_STATUS = 409; @@ -41,6 +44,7 @@ export interface TaskSchedulingOpts { taskStore: TaskStore; taskPollingLifecycle: TaskPollingLifecycle; middleware: Middleware; + definitions: TaskTypeDictionary; } interface RunNowResult { @@ -52,6 +56,7 @@ export class TaskScheduling { private taskPollingLifecycle: TaskPollingLifecycle; private logger: Logger; private middleware: Middleware; + private definitions: TaskTypeDictionary; /** * Initializes the task manager, preventing any further addition of middleware, @@ -63,6 +68,7 @@ export class TaskScheduling { this.middleware = opts.middleware; this.taskPollingLifecycle = opts.taskPollingLifecycle; this.store = opts.taskStore; + this.definitions = opts.definitions; } /** @@ -122,10 +128,27 @@ export class TaskScheduling { .pipe(filter(({ id }: TaskLifecycleEvent) => id === taskId)) .subscribe((taskEvent: TaskLifecycleEvent) => { if (isTaskClaimEvent(taskEvent)) { - mapErr(async (error: Option) => { + mapErr(async (error: ClaimTaskErr) => { // reject if any error event takes place for the requested task subscription.unsubscribe(); - return reject(await this.identifyTaskFailureReason(taskId, error)); + if ( + isSome(error.task) && + error.errorType === TaskClaimErrorType.CLAIMED_BY_ID_OUT_OF_CAPACITY + ) { + const task = error.task.value; + const definition = this.definitions.get(task.taskType); + return reject( + new Error( + `Failed to run task "${taskId}" as we would exceed the max concurrency of "${ + definition?.title ?? task.taskType + }" which is ${ + definition?.maxConcurrency + }. Rescheduled the task to ensure it is picked up as soon as possible.` + ) + ); + } else { + return reject(await this.identifyTaskFailureReason(taskId, error.task)); + } }, taskEvent.event); } else { either, ErrResultOf>( diff --git a/x-pack/plugins/task_manager/server/task_store.mock.ts b/x-pack/plugins/task_manager/server/task_store.mock.ts index d4f863af6fe3b..38d570f96220b 100644 --- a/x-pack/plugins/task_manager/server/task_store.mock.ts +++ b/x-pack/plugins/task_manager/server/task_store.mock.ts @@ -5,38 +5,27 @@ * 2.0. */ -import { Observable, Subject } from 'rxjs'; -import { TaskClaim } from './task_events'; - import { TaskStore } from './task_store'; interface TaskStoreOptions { - maxAttempts?: number; index?: string; taskManagerId?: string; - events?: Observable; } export const taskStoreMock = { - create({ - maxAttempts = 0, - index = '', - taskManagerId = '', - events = new Subject(), - }: TaskStoreOptions) { + create({ index = '', taskManagerId = '' }: TaskStoreOptions = {}) { const mocked = ({ + convertToSavedObjectIds: jest.fn(), update: jest.fn(), remove: jest.fn(), schedule: jest.fn(), - claimAvailableTasks: jest.fn(), bulkUpdate: jest.fn(), get: jest.fn(), getLifecycle: jest.fn(), fetch: jest.fn(), aggregate: jest.fn(), - maxAttempts, + updateByQuery: jest.fn(), index, taskManagerId, - events, } as unknown) as jest.Mocked; return mocked; }, diff --git a/x-pack/plugins/task_manager/server/task_store.test.ts b/x-pack/plugins/task_manager/server/task_store.test.ts index dbf13a5f27281..25ee8cb0e2374 100644 --- a/x-pack/plugins/task_manager/server/task_store.test.ts +++ b/x-pack/plugins/task_manager/server/task_store.test.ts @@ -6,19 +6,16 @@ */ import _ from 'lodash'; -import uuid from 'uuid'; -import { filter, take, first } from 'rxjs/operators'; -import { Option, some, none } from 'fp-ts/lib/Option'; +import { first } from 'rxjs/operators'; import { TaskInstance, TaskStatus, TaskLifecycleResult, SerializedConcreteTaskInstance, - ConcreteTaskInstance, } from './task'; import { elasticsearchServiceMock } from '../../../../src/core/server/mocks'; -import { StoreOpts, OwnershipClaimingOpts, TaskStore, SearchOpts } from './task_store'; +import { TaskStore, SearchOpts } from './task_store'; import { savedObjectsRepositoryMock } from 'src/core/server/mocks'; import { SavedObjectsSerializer, @@ -26,12 +23,8 @@ import { SavedObjectAttributes, SavedObjectsErrorHelpers, } from 'src/core/server'; -import { asTaskClaimEvent, TaskEvent } from './task_events'; -import { asOk, asErr } from './lib/result_type'; import { TaskTypeDictionary } from './task_type_dictionary'; import { RequestEvent } from '@elastic/elasticsearch/lib/Transport'; -import { Search, UpdateByQuery } from '@elastic/elasticsearch/api/requestParams'; -import { BoolClauseWithAnyCondition, TermFilter } from './queries/query_clauses'; import { mockLogger } from './test_utils'; const savedObjectsClient = savedObjectsRepositoryMock.create(); @@ -76,7 +69,6 @@ describe('TaskStore', () => { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -209,7 +201,6 @@ describe('TaskStore', () => { taskManagerId: '', serializer, esClient, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -265,809 +256,6 @@ describe('TaskStore', () => { }); }); - describe('claimAvailableTasks', () => { - async function testClaimAvailableTasks({ - opts = {}, - hits = generateFakeTasks(1), - claimingOpts, - versionConflicts = 2, - }: { - opts: Partial; - hits?: unknown[]; - claimingOpts: OwnershipClaimingOpts; - versionConflicts?: number; - }) { - const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - esClient.search.mockResolvedValue(asApiResponse({ hits: { hits } })); - esClient.updateByQuery.mockResolvedValue( - asApiResponse({ - total: hits.length + versionConflicts, - updated: hits.length, - version_conflicts: versionConflicts, - }) - ); - - const store = new TaskStore({ - esClient, - maxAttempts: 2, - definitions: taskDefinitions, - serializer, - savedObjectsRepository: savedObjectsClient, - taskManagerId: '', - index: '', - ...opts, - }); - - const result = await store.claimAvailableTasks(claimingOpts); - - expect(esClient.updateByQuery.mock.calls[0][0]).toMatchObject({ - max_docs: claimingOpts.size, - }); - expect(esClient.search.mock.calls[0][0]).toMatchObject({ body: { size: claimingOpts.size } }); - return { - result, - args: { - search: esClient.search.mock.calls[0][0]! as Search<{ - query: BoolClauseWithAnyCondition; - size: number; - sort: string | string[]; - }>, - updateByQuery: esClient.updateByQuery.mock.calls[0][0]! as UpdateByQuery<{ - query: BoolClauseWithAnyCondition; - size: number; - sort: string | string[]; - script: object; - }>, - }, - }; - } - - test('it returns normally with no tasks when the index does not exist.', async () => { - const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - esClient.updateByQuery.mockResolvedValue( - asApiResponse({ - total: 0, - updated: 0, - }) - ); - const store = new TaskStore({ - index: 'tasky', - taskManagerId: '', - serializer, - esClient, - definitions: taskDefinitions, - maxAttempts: 2, - savedObjectsRepository: savedObjectsClient, - }); - const { docs } = await store.claimAvailableTasks({ - claimOwnershipUntil: new Date(), - size: 10, - }); - expect(esClient.updateByQuery.mock.calls[0][0]).toMatchObject({ - ignore_unavailable: true, - max_docs: 10, - }); - expect(docs.length).toBe(0); - }); - - test('it filters claimed tasks down by supported types, maxAttempts, status, and runAt', async () => { - const maxAttempts = _.random(2, 43); - const customMaxAttempts = _.random(44, 100); - - const definitions = new TaskTypeDictionary(mockLogger()); - definitions.registerTaskDefinitions({ - foo: { - title: 'foo', - createTaskRunner: jest.fn(), - }, - bar: { - title: 'bar', - maxAttempts: customMaxAttempts, - createTaskRunner: jest.fn(), - }, - }); - - const { - args: { - updateByQuery: { body: { query, sort } = {} }, - }, - } = await testClaimAvailableTasks({ - opts: { - maxAttempts, - definitions, - }, - claimingOpts: { claimOwnershipUntil: new Date(), size: 10 }, - }); - expect(query).toMatchObject({ - bool: { - must: [ - { term: { type: 'task' } }, - { - bool: { - must: [ - { - bool: { - must: [ - { - bool: { - should: [ - { - bool: { - must: [ - { term: { 'task.status': 'idle' } }, - { range: { 'task.runAt': { lte: 'now' } } }, - ], - }, - }, - { - bool: { - must: [ - { - bool: { - should: [ - { term: { 'task.status': 'running' } }, - { term: { 'task.status': 'claiming' } }, - ], - }, - }, - { range: { 'task.retryAt': { lte: 'now' } } }, - ], - }, - }, - ], - }, - }, - ], - }, - }, - ], - filter: [ - { - bool: { - must_not: [ - { - bool: { - should: [ - { term: { 'task.status': 'running' } }, - { term: { 'task.status': 'claiming' } }, - ], - must: { range: { 'task.retryAt': { gt: 'now' } } }, - }, - }, - ], - }, - }, - ], - }, - }, - ], - }, - }); - expect(sort).toMatchObject([ - { - _script: { - type: 'number', - order: 'asc', - script: { - lang: 'painless', - source: ` -if (doc['task.retryAt'].size()!=0) { - return doc['task.retryAt'].value.toInstant().toEpochMilli(); -} -if (doc['task.runAt'].size()!=0) { - return doc['task.runAt'].value.toInstant().toEpochMilli(); -} - `, - }, - }, - }, - ]); - }); - - test('it supports claiming specific tasks by id', async () => { - const maxAttempts = _.random(2, 43); - const customMaxAttempts = _.random(44, 100); - const definitions = new TaskTypeDictionary(mockLogger()); - const taskManagerId = uuid.v1(); - const fieldUpdates = { - ownerId: taskManagerId, - retryAt: new Date(Date.now()), - }; - definitions.registerTaskDefinitions({ - foo: { - title: 'foo', - createTaskRunner: jest.fn(), - }, - bar: { - title: 'bar', - maxAttempts: customMaxAttempts, - createTaskRunner: jest.fn(), - }, - }); - const { - args: { - updateByQuery: { body: { query, script, sort } = {} }, - }, - } = await testClaimAvailableTasks({ - opts: { - taskManagerId, - maxAttempts, - definitions, - }, - claimingOpts: { - claimOwnershipUntil: new Date(), - size: 10, - claimTasksById: [ - '33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', - 'a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', - ], - }, - }); - - expect(query).toMatchObject({ - bool: { - must: [ - { term: { type: 'task' } }, - { - bool: { - must: [ - { - pinned: { - ids: [ - 'task:33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', - 'task:a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', - ], - organic: { - bool: { - must: [ - { - bool: { - should: [ - { - bool: { - must: [ - { term: { 'task.status': 'idle' } }, - { range: { 'task.runAt': { lte: 'now' } } }, - ], - }, - }, - { - bool: { - must: [ - { - bool: { - should: [ - { term: { 'task.status': 'running' } }, - { term: { 'task.status': 'claiming' } }, - ], - }, - }, - { range: { 'task.retryAt': { lte: 'now' } } }, - ], - }, - }, - ], - }, - }, - ], - }, - }, - }, - }, - ], - filter: [ - { - bool: { - must_not: [ - { - bool: { - should: [ - { term: { 'task.status': 'running' } }, - { term: { 'task.status': 'claiming' } }, - ], - must: { range: { 'task.retryAt': { gt: 'now' } } }, - }, - }, - ], - }, - }, - ], - }, - }, - ], - }, - }); - - expect(script).toMatchObject({ - source: ` - if (params.registeredTaskTypes.contains(ctx._source.task.taskType)) { - if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { - ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) - .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) - .join(' ')} - } else { - ctx._source.task.status = "failed"; - } - } else { - ctx._source.task.status = "unrecognized"; - } - `, - lang: 'painless', - params: { - fieldUpdates, - claimTasksById: [ - 'task:33c6977a-ed6d-43bd-98d9-3f827f7b7cd8', - 'task:a208b22c-14ec-4fb4-995f-d2ff7a3b03b8', - ], - registeredTaskTypes: ['foo', 'bar'], - taskMaxAttempts: { - bar: customMaxAttempts, - foo: maxAttempts, - }, - }, - }); - - expect(sort).toMatchObject([ - '_score', - { - _script: { - type: 'number', - order: 'asc', - script: { - lang: 'painless', - source: ` -if (doc['task.retryAt'].size()!=0) { - return doc['task.retryAt'].value.toInstant().toEpochMilli(); -} -if (doc['task.runAt'].size()!=0) { - return doc['task.runAt'].value.toInstant().toEpochMilli(); -} - `, - }, - }, - }, - ]); - }); - - test('it claims tasks by setting their ownerId, status and retryAt', async () => { - const taskManagerId = uuid.v1(); - const claimOwnershipUntil = new Date(Date.now()); - const fieldUpdates = { - ownerId: taskManagerId, - retryAt: claimOwnershipUntil, - }; - const { - args: { - updateByQuery: { body: { script } = {} }, - }, - } = await testClaimAvailableTasks({ - opts: { - taskManagerId, - }, - claimingOpts: { - claimOwnershipUntil, - size: 10, - }, - }); - expect(script).toMatchObject({ - source: ` - if (params.registeredTaskTypes.contains(ctx._source.task.taskType)) { - if (ctx._source.task.schedule != null || ctx._source.task.attempts < params.taskMaxAttempts[ctx._source.task.taskType] || params.claimTasksById.contains(ctx._id)) { - ctx._source.task.status = "claiming"; ${Object.keys(fieldUpdates) - .map((field) => `ctx._source.task.${field}=params.fieldUpdates.${field};`) - .join(' ')} - } else { - ctx._source.task.status = "failed"; - } - } else { - ctx._source.task.status = "unrecognized"; - } - `, - lang: 'painless', - params: { - fieldUpdates, - claimTasksById: [], - registeredTaskTypes: ['report', 'dernstraight', 'yawn'], - taskMaxAttempts: { - dernstraight: 2, - report: 2, - yawn: 2, - }, - }, - }); - }); - - test('it filters out running tasks', async () => { - const taskManagerId = uuid.v1(); - const claimOwnershipUntil = new Date(Date.now()); - const runAt = new Date(); - const tasks = [ - { - _id: 'task:aaa', - _source: { - type: 'task', - task: { - runAt, - taskType: 'foo', - schedule: undefined, - attempts: 0, - status: 'claiming', - params: '{ "hello": "world" }', - state: '{ "baby": "Henhen" }', - user: 'jimbo', - scope: ['reporting'], - ownerId: taskManagerId, - }, - }, - _seq_no: 1, - _primary_term: 2, - sort: ['a', 1], - }, - { - // this is invalid as it doesn't have the `type` prefix - _id: 'bbb', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'claiming', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - ]; - const { - result: { docs }, - args: { - search: { body: { query } = {} }, - }, - } = await testClaimAvailableTasks({ - opts: { - taskManagerId, - }, - claimingOpts: { - claimOwnershipUntil, - size: 10, - }, - hits: tasks, - }); - - expect(query?.bool?.must).toContainEqual({ - bool: { - must: [ - { - term: { - 'task.ownerId': taskManagerId, - }, - }, - { term: { 'task.status': 'claiming' } }, - ], - }, - }); - - expect(docs).toMatchObject([ - { - attempts: 0, - id: 'aaa', - schedule: undefined, - params: { hello: 'world' }, - runAt, - scope: ['reporting'], - state: { baby: 'Henhen' }, - status: 'claiming', - taskType: 'foo', - user: 'jimbo', - ownerId: taskManagerId, - }, - ]); - }); - - test('it filters out invalid tasks that arent SavedObjects', async () => { - const taskManagerId = uuid.v1(); - const claimOwnershipUntil = new Date(Date.now()); - const runAt = new Date(); - const tasks = [ - { - _id: 'task:aaa', - _source: { - type: 'task', - task: { - runAt, - taskType: 'foo', - schedule: undefined, - attempts: 0, - status: 'claiming', - params: '{ "hello": "world" }', - state: '{ "baby": "Henhen" }', - user: 'jimbo', - scope: ['reporting'], - ownerId: taskManagerId, - }, - }, - _seq_no: 1, - _primary_term: 2, - sort: ['a', 1], - }, - { - _id: 'task:bbb', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'running', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - ]; - const { - result: { docs } = {}, - args: { - search: { body: { query } = {} }, - }, - } = await testClaimAvailableTasks({ - opts: { - taskManagerId, - }, - claimingOpts: { - claimOwnershipUntil, - size: 10, - }, - hits: tasks, - }); - - expect(query?.bool?.must).toContainEqual({ - bool: { - must: [ - { - term: { - 'task.ownerId': taskManagerId, - }, - }, - { term: { 'task.status': 'claiming' } }, - ], - }, - }); - - expect(docs).toMatchObject([ - { - attempts: 0, - id: 'aaa', - schedule: undefined, - params: { hello: 'world' }, - runAt, - scope: ['reporting'], - state: { baby: 'Henhen' }, - status: 'claiming', - taskType: 'foo', - user: 'jimbo', - ownerId: taskManagerId, - }, - ]); - }); - - test('it returns task objects', async () => { - const taskManagerId = uuid.v1(); - const claimOwnershipUntil = new Date(Date.now()); - const runAt = new Date(); - const tasks = [ - { - _id: 'task:aaa', - _source: { - type: 'task', - task: { - runAt, - taskType: 'foo', - schedule: undefined, - attempts: 0, - status: 'claiming', - params: '{ "hello": "world" }', - state: '{ "baby": "Henhen" }', - user: 'jimbo', - scope: ['reporting'], - ownerId: taskManagerId, - }, - }, - _seq_no: 1, - _primary_term: 2, - sort: ['a', 1], - }, - { - _id: 'task:bbb', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'claiming', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - ]; - const { - result: { docs } = {}, - args: { - search: { body: { query } = {} }, - }, - } = await testClaimAvailableTasks({ - opts: { - taskManagerId, - }, - claimingOpts: { - claimOwnershipUntil, - size: 10, - }, - hits: tasks, - }); - - expect(query?.bool?.must).toContainEqual({ - bool: { - must: [ - { - term: { - 'task.ownerId': taskManagerId, - }, - }, - { term: { 'task.status': 'claiming' } }, - ], - }, - }); - - expect(docs).toMatchObject([ - { - attempts: 0, - id: 'aaa', - schedule: undefined, - params: { hello: 'world' }, - runAt, - scope: ['reporting'], - state: { baby: 'Henhen' }, - status: 'claiming', - taskType: 'foo', - user: 'jimbo', - ownerId: taskManagerId, - }, - { - attempts: 2, - id: 'bbb', - schedule: { interval: '5m' }, - params: { shazm: 1 }, - runAt, - scope: ['reporting', 'ceo'], - state: { henry: 'The 8th' }, - status: 'claiming', - taskType: 'bar', - user: 'dabo', - ownerId: taskManagerId, - }, - ]); - }); - - test('it returns version_conflicts that do not include conflicts that were proceeded against', async () => { - const taskManagerId = uuid.v1(); - const claimOwnershipUntil = new Date(Date.now()); - const runAt = new Date(); - const tasks = [ - { - _id: 'task:aaa', - _source: { - type: 'task', - task: { - runAt, - taskType: 'foo', - schedule: undefined, - attempts: 0, - status: 'claiming', - params: '{ "hello": "world" }', - state: '{ "baby": "Henhen" }', - user: 'jimbo', - scope: ['reporting'], - ownerId: taskManagerId, - }, - }, - _seq_no: 1, - _primary_term: 2, - sort: ['a', 1], - }, - { - _id: 'task:bbb', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'claiming', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - ]; - const maxDocs = 10; - const { - result: { stats: { tasksUpdated, tasksConflicted, tasksClaimed } = {} } = {}, - } = await testClaimAvailableTasks({ - opts: { - taskManagerId, - }, - claimingOpts: { - claimOwnershipUntil, - size: maxDocs, - }, - hits: tasks, - // assume there were 20 version conflists, but thanks to `conflicts="proceed"` - // we proceeded to claim tasks - versionConflicts: 20, - }); - - expect(tasksUpdated).toEqual(2); - // ensure we only count conflicts that *may* have counted against max_docs, no more than that - expect(tasksConflicted).toEqual(10 - tasksUpdated!); - expect(tasksClaimed).toEqual(2); - }); - - test('pushes error from saved objects client to errors$', async () => { - const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - const store = new TaskStore({ - index: 'tasky', - taskManagerId: '', - serializer, - esClient, - definitions: taskDefinitions, - maxAttempts: 2, - savedObjectsRepository: savedObjectsClient, - }); - - const firstErrorPromise = store.errors$.pipe(first()).toPromise(); - esClient.updateByQuery.mockRejectedValue(new Error('Failure')); - await expect( - store.claimAvailableTasks({ - claimOwnershipUntil: new Date(), - size: 10, - }) - ).rejects.toThrowErrorMatchingInlineSnapshot(`"Failure"`); - expect(await firstErrorPromise).toMatchInlineSnapshot(`[Error: Failure]`); - }); - }); - describe('update', () => { let store: TaskStore; let esClient: ReturnType['asInternalUser']; @@ -1079,7 +267,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1179,7 +366,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1219,7 +405,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1251,7 +436,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1335,7 +519,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1355,7 +538,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1373,7 +555,6 @@ if (doc['task.runAt'].size()!=0) { taskManagerId: '', serializer, esClient: elasticsearchServiceMock.createClusterClient().asInternalUser, - maxAttempts: 2, definitions: taskDefinitions, savedObjectsRepository: savedObjectsClient, }); @@ -1381,283 +562,8 @@ if (doc['task.runAt'].size()!=0) { return expect(store.getLifecycle(randomId())).rejects.toThrow('Bad Request'); }); }); - - describe('task events', () => { - function generateTasks() { - const taskManagerId = uuid.v1(); - const runAt = new Date(); - const tasks = [ - { - _id: 'task:claimed-by-id', - _source: { - type: 'task', - task: { - runAt, - taskType: 'foo', - schedule: undefined, - attempts: 0, - status: 'claiming', - params: '{ "hello": "world" }', - state: '{ "baby": "Henhen" }', - user: 'jimbo', - scope: ['reporting'], - ownerId: taskManagerId, - startedAt: null, - retryAt: null, - scheduledAt: new Date(), - }, - }, - _seq_no: 1, - _primary_term: 2, - sort: ['a', 1], - }, - { - _id: 'task:claimed-by-schedule', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'claiming', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - startedAt: null, - retryAt: null, - scheduledAt: new Date(), - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - { - _id: 'task:already-running', - _source: { - type: 'task', - task: { - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'running', - params: '{ "shazm": 1 }', - state: '{ "henry": "The 8th" }', - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - startedAt: null, - retryAt: null, - scheduledAt: new Date(), - }, - }, - _seq_no: 3, - _primary_term: 4, - sort: ['b', 2], - }, - ]; - - return { taskManagerId, runAt, tasks }; - } - - function instantiateStoreWithMockedApiResponses() { - const { taskManagerId, runAt, tasks } = generateTasks(); - - const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - esClient.search.mockResolvedValue(asApiResponse({ hits: { hits: tasks } })); - esClient.updateByQuery.mockResolvedValue( - asApiResponse({ - total: tasks.length, - updated: tasks.length, - }) - ); - - const store = new TaskStore({ - esClient, - maxAttempts: 2, - definitions: taskDefinitions, - serializer, - savedObjectsRepository: savedObjectsClient, - taskManagerId, - index: '', - }); - - return { taskManagerId, runAt, store }; - } - - test('emits an event when a task is succesfully claimed by id', async () => { - const { taskManagerId, runAt, store } = instantiateStoreWithMockedApiResponses(); - - const promise = store.events - .pipe( - filter( - (event: TaskEvent>) => - event.id === 'claimed-by-id' - ), - take(1) - ) - .toPromise(); - - await store.claimAvailableTasks({ - claimTasksById: ['claimed-by-id'], - claimOwnershipUntil: new Date(), - size: 10, - }); - - const event = await promise; - expect(event).toMatchObject( - asTaskClaimEvent( - 'claimed-by-id', - asOk({ - id: 'claimed-by-id', - runAt, - taskType: 'foo', - schedule: undefined, - attempts: 0, - status: 'claiming' as TaskStatus, - params: { hello: 'world' }, - state: { baby: 'Henhen' }, - user: 'jimbo', - scope: ['reporting'], - ownerId: taskManagerId, - startedAt: null, - retryAt: null, - scheduledAt: new Date(), - }) - ) - ); - }); - - test('emits an event when a task is succesfully by scheduling', async () => { - const { taskManagerId, runAt, store } = instantiateStoreWithMockedApiResponses(); - - const promise = store.events - .pipe( - filter( - (event: TaskEvent>) => - event.id === 'claimed-by-schedule' - ), - take(1) - ) - .toPromise(); - - await store.claimAvailableTasks({ - claimTasksById: ['claimed-by-id'], - claimOwnershipUntil: new Date(), - size: 10, - }); - - const event = await promise; - expect(event).toMatchObject( - asTaskClaimEvent( - 'claimed-by-schedule', - asOk({ - id: 'claimed-by-schedule', - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'claiming' as TaskStatus, - params: { shazm: 1 }, - state: { henry: 'The 8th' }, - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - startedAt: null, - retryAt: null, - scheduledAt: new Date(), - }) - ) - ); - }); - - test('emits an event when the store fails to claim a required task by id', async () => { - const { taskManagerId, runAt, store } = instantiateStoreWithMockedApiResponses(); - - const promise = store.events - .pipe( - filter( - (event: TaskEvent>) => - event.id === 'already-running' - ), - take(1) - ) - .toPromise(); - - await store.claimAvailableTasks({ - claimTasksById: ['already-running'], - claimOwnershipUntil: new Date(), - size: 10, - }); - - const event = await promise; - expect(event).toMatchObject( - asTaskClaimEvent( - 'already-running', - asErr( - some({ - id: 'already-running', - runAt, - taskType: 'bar', - schedule: { interval: '5m' }, - attempts: 2, - status: 'running' as TaskStatus, - params: { shazm: 1 }, - state: { henry: 'The 8th' }, - user: 'dabo', - scope: ['reporting', 'ceo'], - ownerId: taskManagerId, - startedAt: null, - retryAt: null, - scheduledAt: new Date(), - }) - ) - ) - ); - }); - - test('emits an event when the store fails to find a task which was required by id', async () => { - const { store } = instantiateStoreWithMockedApiResponses(); - - const promise = store.events - .pipe( - filter( - (event: TaskEvent>) => - event.id === 'unknown-task' - ), - take(1) - ) - .toPromise(); - - await store.claimAvailableTasks({ - claimTasksById: ['unknown-task'], - claimOwnershipUntil: new Date(), - size: 10, - }); - - const event = await promise; - expect(event).toMatchObject(asTaskClaimEvent('unknown-task', asErr(none))); - }); - }); }); -function generateFakeTasks(count: number = 1) { - return _.times(count, (index) => ({ - _id: `task:id-${index}`, - _source: { - type: 'task', - task: {}, - }, - _seq_no: _.random(1, 5), - _primary_term: _.random(1, 5), - sort: ['a', _.random(1, 5)], - })); -} - const asApiResponse = (body: T): RequestEvent => ({ body, diff --git a/x-pack/plugins/task_manager/server/task_store.ts b/x-pack/plugins/task_manager/server/task_store.ts index b72f1826b813b..0b54f2779065f 100644 --- a/x-pack/plugins/task_manager/server/task_store.ts +++ b/x-pack/plugins/task_manager/server/task_store.ts @@ -8,13 +8,9 @@ /* * This module contains helpers for managing the task manager storage layer. */ -import apm from 'elastic-apm-node'; -import { Subject, Observable } from 'rxjs'; -import { omit, difference, partition, map, defaults } from 'lodash'; - -import { some, none } from 'fp-ts/lib/Option'; - -import { SearchResponse, UpdateDocumentByQueryResponse } from 'elasticsearch'; +import { Subject } from 'rxjs'; +import { omit, defaults } from 'lodash'; +import { ReindexResponseBase, SearchResponse, UpdateDocumentByQueryResponse } from 'elasticsearch'; import { SavedObject, SavedObjectsSerializer, @@ -32,38 +28,15 @@ import { TaskLifecycle, TaskLifecycleResult, SerializedConcreteTaskInstance, - TaskStatus, } from './task'; -import { TaskClaim, asTaskClaimEvent } from './task_events'; - -import { - asUpdateByQuery, - shouldBeOneOf, - mustBeAllOf, - filterDownBy, - asPinnedQuery, - matchesClauses, - SortOptions, -} from './queries/query_clauses'; - -import { - updateFieldsAndMarkAsFailed, - IdleTaskWithExpiredRunAt, - InactiveTasks, - RunningOrClaimingTaskWithExpiredRetryAt, - SortByRunAtAndRetryAt, - tasksClaimedByOwner, -} from './queries/mark_available_tasks_as_claimed'; import { TaskTypeDictionary } from './task_type_dictionary'; - import { ESSearchResponse, ESSearchBody } from '../../../typings/elasticsearch'; export interface StoreOpts { esClient: ElasticsearchClient; index: string; taskManagerId: string; - maxAttempts: number; definitions: TaskTypeDictionary; savedObjectsRepository: ISavedObjectsRepository; serializer: SavedObjectsSerializer; @@ -88,25 +61,10 @@ export interface UpdateByQueryOpts extends SearchOpts { max_docs?: number; } -export interface OwnershipClaimingOpts { - claimOwnershipUntil: Date; - claimTasksById?: string[]; - size: number; -} - export interface FetchResult { docs: ConcreteTaskInstance[]; } -export interface ClaimOwnershipResult { - stats: { - tasksUpdated: number; - tasksConflicted: number; - tasksClaimed: number; - }; - docs: ConcreteTaskInstance[]; -} - export type BulkUpdateResult = Result< ConcreteTaskInstance, { entity: ConcreteTaskInstance; error: Error } @@ -123,7 +81,6 @@ export interface UpdateByQueryResult { * interface into the index. */ export class TaskStore { - public readonly maxAttempts: number; public readonly index: string; public readonly taskManagerId: string; public readonly errors$ = new Subject(); @@ -132,14 +89,12 @@ export class TaskStore { private definitions: TaskTypeDictionary; private savedObjectsRepository: ISavedObjectsRepository; private serializer: SavedObjectsSerializer; - private events$: Subject; /** * Constructs a new TaskStore. * @param {StoreOpts} opts * @prop {esClient} esClient - An elasticsearch client * @prop {string} index - The name of the task manager index - * @prop {number} maxAttempts - The maximum number of attempts before a task will be abandoned * @prop {TaskDefinition} definition - The definition of the task being run * @prop {serializer} - The saved object serializer * @prop {savedObjectsRepository} - An instance to the saved objects repository @@ -148,21 +103,22 @@ export class TaskStore { this.esClient = opts.esClient; this.index = opts.index; this.taskManagerId = opts.taskManagerId; - this.maxAttempts = opts.maxAttempts; this.definitions = opts.definitions; this.serializer = opts.serializer; this.savedObjectsRepository = opts.savedObjectsRepository; - this.events$ = new Subject(); } - public get events(): Observable { - return this.events$; + /** + * Convert ConcreteTaskInstance Ids to match their SavedObject format as serialized + * in Elasticsearch + * @param tasks - The task being scheduled. + */ + public convertToSavedObjectIds( + taskIds: Array + ): Array { + return taskIds.map((id) => this.serializer.generateRawId(undefined, 'task', id)); } - private emitEvents = (events: TaskClaim[]) => { - events.forEach((event) => this.events$.next(event)); - }; - /** * Schedules a task. * @@ -201,144 +157,6 @@ export class TaskStore { }); } - /** - * Claims available tasks from the index, which are ready to be run. - * - runAt is now or past - * - is not currently claimed by any instance of Kibana - * - has a type that is in our task definitions - * - * @param {OwnershipClaimingOpts} options - * @returns {Promise} - */ - public claimAvailableTasks = async ({ - claimOwnershipUntil, - claimTasksById = [], - size, - }: OwnershipClaimingOpts): Promise => { - const claimTasksByIdWithRawIds = claimTasksById.map((id) => - this.serializer.generateRawId(undefined, 'task', id) - ); - - const { - updated: tasksUpdated, - version_conflicts: tasksConflicted, - } = await this.markAvailableTasksAsClaimed(claimOwnershipUntil, claimTasksByIdWithRawIds, size); - - const docs = - tasksUpdated > 0 ? await this.sweepForClaimedTasks(claimTasksByIdWithRawIds, size) : []; - - const [documentsReturnedById, documentsClaimedBySchedule] = partition(docs, (doc) => - claimTasksById.includes(doc.id) - ); - - const [documentsClaimedById, documentsRequestedButNotClaimed] = partition( - documentsReturnedById, - // we filter the schduled tasks down by status is 'claiming' in the esearch, - // but we do not apply this limitation on tasks claimed by ID so that we can - // provide more detailed error messages when we fail to claim them - (doc) => doc.status === TaskStatus.Claiming - ); - - const documentsRequestedButNotReturned = difference( - claimTasksById, - map(documentsReturnedById, 'id') - ); - - this.emitEvents([ - ...documentsClaimedById.map((doc) => asTaskClaimEvent(doc.id, asOk(doc))), - ...documentsClaimedBySchedule.map((doc) => asTaskClaimEvent(doc.id, asOk(doc))), - ...documentsRequestedButNotClaimed.map((doc) => asTaskClaimEvent(doc.id, asErr(some(doc)))), - ...documentsRequestedButNotReturned.map((id) => asTaskClaimEvent(id, asErr(none))), - ]); - - return { - stats: { - tasksUpdated, - tasksConflicted, - tasksClaimed: documentsClaimedById.length + documentsClaimedBySchedule.length, - }, - docs: docs.filter((doc) => doc.status === TaskStatus.Claiming), - }; - }; - - private async markAvailableTasksAsClaimed( - claimOwnershipUntil: OwnershipClaimingOpts['claimOwnershipUntil'], - claimTasksById: OwnershipClaimingOpts['claimTasksById'], - size: OwnershipClaimingOpts['size'] - ): Promise { - const registeredTaskTypes = this.definitions.getAllTypes(); - const taskMaxAttempts = [...this.definitions].reduce((accumulator, [type, { maxAttempts }]) => { - return { ...accumulator, [type]: maxAttempts || this.maxAttempts }; - }, {}); - const queryForScheduledTasks = mustBeAllOf( - // Either a task with idle status and runAt <= now or - // status running or claiming with a retryAt <= now. - shouldBeOneOf(IdleTaskWithExpiredRunAt, RunningOrClaimingTaskWithExpiredRetryAt) - ); - - // The documents should be sorted by runAt/retryAt, unless there are pinned - // tasks being queried, in which case we want to sort by score first, and then - // the runAt/retryAt. That way we'll get the pinned tasks first. Note that - // the score seems to favor newer documents rather than older documents, so - // if there are not pinned tasks being queried, we do NOT want to sort by score - // at all, just by runAt/retryAt. - const sort: SortOptions = [SortByRunAtAndRetryAt]; - if (claimTasksById && claimTasksById.length) { - sort.unshift('_score'); - } - - const apmTrans = apm.startTransaction(`taskManager markAvailableTasksAsClaimed`, 'taskManager'); - const result = await this.updateByQuery( - asUpdateByQuery({ - query: matchesClauses( - mustBeAllOf( - claimTasksById && claimTasksById.length - ? asPinnedQuery(claimTasksById, queryForScheduledTasks) - : queryForScheduledTasks - ), - filterDownBy(InactiveTasks) - ), - update: updateFieldsAndMarkAsFailed( - { - ownerId: this.taskManagerId, - retryAt: claimOwnershipUntil, - }, - claimTasksById || [], - registeredTaskTypes, - taskMaxAttempts - ), - sort, - }), - { - max_docs: size, - } - ); - - if (apmTrans) apmTrans.end(); - return result; - } - - /** - * Fetches tasks from the index, which are owned by the current Kibana instance - */ - private async sweepForClaimedTasks( - claimTasksById: OwnershipClaimingOpts['claimTasksById'], - size: OwnershipClaimingOpts['size'] - ): Promise { - const claimedTasksQuery = tasksClaimedByOwner(this.taskManagerId); - const { docs } = await this.search({ - query: - claimTasksById && claimTasksById.length - ? asPinnedQuery(claimTasksById, claimedTasksQuery) - : claimedTasksQuery, - size, - sort: SortByRunAtAndRetryAt, - seq_no_primary_term: true, - }); - - return docs; - } - /** * Updates the specified doc in the index, returning the doc * with its version up to date. @@ -527,7 +345,7 @@ export class TaskStore { return body; } - private async updateByQuery( + public async updateByQuery( opts: UpdateByQuerySearchOpts = {}, // eslint-disable-next-line @typescript-eslint/naming-convention { max_docs: max_docs }: UpdateByQueryOpts = {} @@ -549,17 +367,11 @@ export class TaskStore { }, }); - /** - * When we run updateByQuery with conflicts='proceed', it's possible for the `version_conflicts` - * to count against the specified `max_docs`, as per https://github.com/elastic/elasticsearch/issues/63671 - * In order to correct for that happening, we only count `version_conflicts` if we haven't updated as - * many docs as we could have. - * This is still no more than an estimation, as there might have been less docuemnt to update that the - * `max_docs`, but we bias in favour of over zealous `version_conflicts` as that's the best indicator we - * have for an unhealthy cluster distribution of Task Manager polling intervals - */ - const conflictsCorrectedForContinuation = - max_docs && version_conflicts + updated > max_docs ? max_docs - updated : version_conflicts; + const conflictsCorrectedForContinuation = correctVersionConflictsForContinuation( + updated, + version_conflicts, + max_docs + ); return { total, @@ -572,6 +384,22 @@ export class TaskStore { } } } +/** + * When we run updateByQuery with conflicts='proceed', it's possible for the `version_conflicts` + * to count against the specified `max_docs`, as per https://github.com/elastic/elasticsearch/issues/63671 + * In order to correct for that happening, we only count `version_conflicts` if we haven't updated as + * many docs as we could have. + * This is still no more than an estimation, as there might have been less docuemnt to update that the + * `max_docs`, but we bias in favour of over zealous `version_conflicts` as that's the best indicator we + * have for an unhealthy cluster distribution of Task Manager polling intervals + */ +export function correctVersionConflictsForContinuation( + updated: ReindexResponseBase['updated'], + versionConflicts: ReindexResponseBase['version_conflicts'], + maxDocs?: number +) { + return maxDocs && versionConflicts + updated > maxDocs ? maxDocs - updated : versionConflicts; +} function taskInstanceToAttributes(doc: TaskInstance): SerializedConcreteTaskInstance { return { diff --git a/x-pack/plugins/task_manager/server/task_type_dictionary.ts b/x-pack/plugins/task_manager/server/task_type_dictionary.ts index 4230eb9ce4b73..63a0548d79d32 100644 --- a/x-pack/plugins/task_manager/server/task_type_dictionary.ts +++ b/x-pack/plugins/task_manager/server/task_type_dictionary.ts @@ -28,6 +28,10 @@ export class TaskTypeDictionary { return [...this.definitions.keys()]; } + public getAllDefinitions() { + return [...this.definitions.values()]; + } + public has(type: string) { return this.definitions.has(type); } diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index 790d9139252fa..98248587ead30 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -660,10 +660,6 @@ "dashboard.topNav.saveModal.storeTimeWithDashboardFormRowHelpText": "有効化すると、ダッシュボードが読み込まれるごとに現在選択された時刻の時間フィルターが変更されます。", "dashboard.topNav.saveModal.storeTimeWithDashboardFormRowLabel": "ダッシュボードに時刻を保存", "dashboard.topNav.showCloneModal.dashboardCopyTitle": "{title}のコピー", - "dashboard.topNave.addButtonAriaLabel": "ライブラリ", - "dashboard.topNave.addConfigDescription": "既存のビジュアライゼーションをダッシュボードに追加", - "dashboard.topNave.addNewButtonAriaLabel": "パネルの作成", - "dashboard.topNave.addNewConfigDescription": "このダッシュボードに新規パネルを作成", "dashboard.topNave.cancelButtonAriaLabel": "キャンセル", "dashboard.topNave.cloneButtonAriaLabel": "クローンを作成", "dashboard.topNave.cloneConfigDescription": "ダッシュボードのコピーを作成します", @@ -19523,9 +19519,7 @@ "xpack.securitySolution.timeline.eventsTableAriaLabel": "イベント; {activePage}/{totalPages} ページ", "xpack.securitySolution.timeline.expandableEvent.alertTitleLabel": "アラートの詳細", "xpack.securitySolution.timeline.expandableEvent.closeEventDetailsLabel": "閉じる", - "xpack.securitySolution.timeline.expandableEvent.copyToClipboardToolTip": "クリップボードにコピー", "xpack.securitySolution.timeline.expandableEvent.eventTitleLabel": "イベントの詳細", - "xpack.securitySolution.timeline.expandableEvent.eventToolTipTitle": "イベント", "xpack.securitySolution.timeline.expandableEvent.messageTitle": "メッセージ", "xpack.securitySolution.timeline.expandableEvent.placeholder": "イベント詳細を表示するには、イベントを選択します", "xpack.securitySolution.timeline.fieldTooltip": "フィールド", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 7e09374aff931..c17703c546502 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -660,11 +660,7 @@ "dashboard.topNav.saveModal.storeTimeWithDashboardFormRowHelpText": "每次加载此仪表板时,都会将时间筛选更改为当前选定的时间。", "dashboard.topNav.saveModal.storeTimeWithDashboardFormRowLabel": "将时间随仪表板保存", "dashboard.topNav.showCloneModal.dashboardCopyTitle": "{title} 副本", - "dashboard.topNave.addButtonAriaLabel": "库", - "dashboard.topNave.addConfigDescription": "将现有可视化添加到仪表板", "dashboard.topNave.cancelButtonAriaLabel": "取消", - "dashboard.topNave.addNewButtonAriaLabel": "创建面板", - "dashboard.topNave.addNewConfigDescription": "在此仪表板上创建新的面板", "dashboard.topNave.cloneButtonAriaLabel": "克隆", "dashboard.topNave.cloneConfigDescription": "创建仪表板的副本", "dashboard.topNave.editButtonAriaLabel": "编辑", @@ -19569,9 +19565,7 @@ "xpack.securitySolution.timeline.eventsTableAriaLabel": "事件;第 {activePage} 页,共 {totalPages} 页", "xpack.securitySolution.timeline.expandableEvent.alertTitleLabel": "告警详情", "xpack.securitySolution.timeline.expandableEvent.closeEventDetailsLabel": "关闭", - "xpack.securitySolution.timeline.expandableEvent.copyToClipboardToolTip": "复制到剪贴板", "xpack.securitySolution.timeline.expandableEvent.eventTitleLabel": "事件详情", - "xpack.securitySolution.timeline.expandableEvent.eventToolTipTitle": "事件", "xpack.securitySolution.timeline.expandableEvent.messageTitle": "消息", "xpack.securitySolution.timeline.expandableEvent.placeholder": "选择事件以显示事件详情", "xpack.securitySolution.timeline.fieldTooltip": "字段", diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/helpers.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/helpers.ts new file mode 100644 index 0000000000000..314d224491128 --- /dev/null +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/helpers.ts @@ -0,0 +1,12 @@ +/* + * 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 { EuiSelectOption } from '@elastic/eui'; +import { Choice } from './types'; + +export const choicesToEuiOptions = (choices: Choice[]): EuiSelectOption[] => + choices.map((choice) => ({ value: choice.value, text: choice.label })); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.test.tsx index bfc32ef67e46f..e864a8d3fd114 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.test.tsx @@ -28,6 +28,8 @@ const actionParams = { severity: '1', urgency: '2', impact: '3', + category: 'software', + subcategory: 'os', externalId: null, }, comments: [], @@ -55,34 +57,48 @@ const defaultProps = { const useGetChoicesResponse = { isLoading: false, - choices: ['severity', 'urgency', 'impact'] - .map((element) => [ - { - dependent_value: '', - label: '1 - Critical', - value: '1', - element, - }, - { - dependent_value: '', - label: '2 - High', - value: '2', - element, - }, - { - dependent_value: '', - label: '3 - Moderate', - value: '3', - element, - }, - { - dependent_value: '', - label: '4 - Low', - value: '4', - element, - }, - ]) - .flat(), + choices: [ + { + dependent_value: '', + label: 'Software', + value: 'software', + element: 'category', + }, + { + dependent_value: 'software', + label: 'Operation System', + value: 'os', + element: 'subcategory', + }, + ...['severity', 'urgency', 'impact'] + .map((element) => [ + { + dependent_value: '', + label: '1 - Critical', + value: '1', + element, + }, + { + dependent_value: '', + label: '2 - High', + value: '2', + element, + }, + { + dependent_value: '', + label: '3 - Moderate', + value: '3', + element, + }, + { + dependent_value: '', + label: '4 - Low', + value: '4', + element, + }, + ]) + .flat(), + ], }; describe('ServiceNowITSMParamsFields renders', () => { @@ -101,6 +117,8 @@ describe('ServiceNowITSMParamsFields renders', () => { expect(wrapper.find('[data-test-subj="urgencySelect"]').exists()).toBeTruthy(); expect(wrapper.find('[data-test-subj="severitySelect"]').exists()).toBeTruthy(); expect(wrapper.find('[data-test-subj="impactSelect"]').exists()).toBeTruthy(); + expect(wrapper.find('[data-test-subj="categorySelect"]').exists()).toBeTruthy(); + expect(wrapper.find('[data-test-subj="subcategorySelect"]').exists()).toBeTruthy(); expect(wrapper.find('[data-test-subj="short_descriptionInput"]').exists()).toBeTruthy(); expect(wrapper.find('[data-test-subj="descriptionTextArea"]').exists()).toBeTruthy(); expect(wrapper.find('[data-test-subj="commentsTextArea"]').exists()).toBeTruthy(); @@ -153,6 +171,36 @@ describe('ServiceNowITSMParamsFields renders', () => { }); }); + test('it transforms the categories to options correctly', async () => { + const wrapper = mount(); + act(() => { + onChoices(useGetChoicesResponse.choices); + }); + + wrapper.update(); + expect(wrapper.find('[data-test-subj="categorySelect"]').first().prop('options')).toEqual([ + { + value: 'software', + text: 'Software', + }, + ]); + }); + + test('it transforms the subcategories to options correctly', async () => { + const wrapper = mount(); + act(() => { + onChoices(useGetChoicesResponse.choices); + }); + + wrapper.update(); + expect(wrapper.find('[data-test-subj="subcategorySelect"]').first().prop('options')).toEqual([ + { + text: 'Operation System', + value: 'os', + }, + ]); + }); + test('it transforms the options correctly', async () => { const wrapper = mount(); act(() => { @@ -179,6 +227,8 @@ describe('ServiceNowITSMParamsFields renders', () => { { dataTestSubj: '[data-test-subj="urgencySelect"]', key: 'urgency' }, { dataTestSubj: '[data-test-subj="severitySelect"]', key: 'severity' }, { dataTestSubj: '[data-test-subj="impactSelect"]', key: 'impact' }, + { dataTestSubj: '[data-test-subj="categorySelect"]', key: 'category' }, + { dataTestSubj: '[data-test-subj="subcategorySelect"]', key: 'subcategory' }, ]; simpleFields.forEach((field) => diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.tsx b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.tsx index 3befa232e5b52..84326a7ae9be8 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/servicenow_itsm_params.tsx @@ -16,17 +16,22 @@ import { } from '@elastic/eui'; import { useKibana } from '../../../../common/lib/kibana'; import { ActionParamsProps } from '../../../../types'; -import { ServiceNowITSMActionParams, Choice, Options } from './types'; +import { ServiceNowITSMActionParams, Choice, Fields } from './types'; import { TextAreaWithMessageVariables } from '../../text_area_with_message_variables'; import { TextFieldWithMessageVariables } from '../../text_field_with_message_variables'; import { useGetChoices } from './use_get_choices'; +import { choicesToEuiOptions } from './helpers'; + import * as i18n from './translations'; -const useGetChoicesFields = ['urgency', 'severity', 'impact']; -const defaultOptions: Options = { +const useGetChoicesFields = ['urgency', 'severity', 'impact', 'category', 'subcategory']; +const defaultFields: Fields = { + category: [], + subcategory: [], urgency: [], severity: [], impact: [], + priority: [], }; const ServiceNowParamsFields: React.FunctionComponent< @@ -48,7 +53,7 @@ const ServiceNowParamsFields: React.FunctionComponent< [actionParams.subActionParams] ); - const [options, setOptions] = useState(defaultOptions); + const [choices, setChoices] = useState(defaultFields); const editSubActionProperty = useCallback( (key: string, value: any) => { @@ -73,19 +78,32 @@ const ServiceNowParamsFields: React.FunctionComponent< [editSubActionProperty] ); - const onChoicesSuccess = (choices: Choice[]) => - setOptions( - choices.reduce( - (acc, choice) => ({ + const onChoicesSuccess = useCallback((values: Choice[]) => { + setChoices( + values.reduce( + (acc, value) => ({ ...acc, - [choice.element]: [ - ...(acc[choice.element] != null ? acc[choice.element] : []), - { value: choice.value, text: choice.label }, - ], + [value.element]: [...(acc[value.element] != null ? acc[value.element] : []), value], }), - defaultOptions + defaultFields ) ); + }, []); + + const categoryOptions = useMemo(() => choicesToEuiOptions(choices.category), [choices.category]); + const urgencyOptions = useMemo(() => choicesToEuiOptions(choices.urgency), [choices.urgency]); + const severityOptions = useMemo(() => choicesToEuiOptions(choices.severity), [choices.severity]); + const impactOptions = useMemo(() => choicesToEuiOptions(choices.impact), [choices.impact]); + + const subcategoryOptions = useMemo( + () => + choicesToEuiOptions( + choices.subcategory.filter( + (subcategory) => subcategory.dependent_value === incident.category + ) + ), + [choices.subcategory, incident.category] + ); const { isLoading: isLoadingChoices } = useGetChoices({ http, @@ -140,7 +158,7 @@ const ServiceNowParamsFields: React.FunctionComponent< hasNoInitialSelection isLoading={isLoadingChoices} disabled={isLoadingChoices} - options={options.urgency} + options={urgencyOptions} value={incident.urgency ?? ''} onChange={(e) => editSubActionProperty('urgency', e.target.value)} /> @@ -155,7 +173,7 @@ const ServiceNowParamsFields: React.FunctionComponent< hasNoInitialSelection isLoading={isLoadingChoices} disabled={isLoadingChoices} - options={options.severity} + options={severityOptions} value={incident.severity ?? ''} onChange={(e) => editSubActionProperty('severity', e.target.value)} /> @@ -169,7 +187,7 @@ const ServiceNowParamsFields: React.FunctionComponent< hasNoInitialSelection isLoading={isLoadingChoices} disabled={isLoadingChoices} - options={options.impact} + options={impactOptions} value={incident.impact ?? ''} onChange={(e) => editSubActionProperty('impact', e.target.value)} /> @@ -177,6 +195,47 @@ const ServiceNowParamsFields: React.FunctionComponent< + + + + { + editAction( + 'subActionParams', + { + incident: { ...incident, category: e.target.value, subcategory: null }, + comments, + }, + index + ); + }} + /> + + + + + editSubActionProperty('subcategory', e.target.value)} + /> + + + + - choices.map((choice) => ({ value: choice.value, text: choice.label })); - const ServiceNowSIRParamsFields: React.FunctionComponent< ActionParamsProps > = ({ actionConnector, actionParams, editAction, index, errors, messageVariables }) => { @@ -218,16 +215,7 @@ const ServiceNowSIRParamsFields: React.FunctionComponent< disabled={isLoadingChoices} options={priorityOptions} value={incident.priority ?? undefined} - onChange={(e) => { - editAction( - 'subActionParams', - { - incident: { ...incident, priority: e.target.value }, - comments, - }, - index - ); - }} + onChange={(e) => editSubActionProperty('priority', e.target.value)} /> diff --git a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/types.ts b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/types.ts index 09f27c92e8082..f252f4648e670 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/servicenow/types.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { EuiSelectOption } from '@elastic/eui'; import { UserConfiguredActionConnector } from '../../../../types'; import { ExecutorSubActionPushParamsITSM, @@ -45,4 +44,3 @@ export interface Choice { } export type Fields = Record; -export type Options = Record; diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx index 7358facf215c3..bd1e3b19e6510 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alert_form/alert_form.tsx @@ -591,6 +591,7 @@ export const AlertForm = ({ alertParams={alert.params} alertInterval={`${alertInterval ?? 1}${alertIntervalUnit}`} alertThrottle={`${alertThrottle ?? 1}${alertThrottleUnit}`} + alertNotifyWhen={alert.notifyWhen ?? 'onActionGroupChange'} errors={errors} setAlertParams={setAlertParams} setAlertProperty={setAlertProperty} diff --git a/x-pack/plugins/triggers_actions_ui/public/types.ts b/x-pack/plugins/triggers_actions_ui/public/types.ts index 6fb52cf1151d5..3e41d27596c34 100644 --- a/x-pack/plugins/triggers_actions_ui/public/types.ts +++ b/x-pack/plugins/triggers_actions_ui/public/types.ts @@ -198,6 +198,7 @@ export interface AlertTypeParamsExpressionProps< alertParams: Params; alertInterval: string; alertThrottle: string; + alertNotifyWhen: AlertNotifyWhenType; setAlertParams: (property: Key, value: Params[Key] | undefined) => void; setAlertProperty: ( key: Prop, diff --git a/x-pack/plugins/uptime/common/runtime_types/network_events.ts b/x-pack/plugins/uptime/common/runtime_types/network_events.ts index 668e17d2a848b..7b651b6a91951 100644 --- a/x-pack/plugins/uptime/common/runtime_types/network_events.ts +++ b/x-pack/plugins/uptime/common/runtime_types/network_events.ts @@ -20,24 +20,36 @@ const NetworkTimingsType = t.type({ ssl: t.number, }); -export type NetworkTimings = t.TypeOf; +const CertificateDataType = t.partial({ + validFrom: t.number, + validTo: t.number, + issuer: t.string, + subjectName: t.string, +}); const NetworkEventType = t.intersection([ t.type({ timestamp: t.string, requestSentTime: t.number, loadEndTime: t.number, + url: t.string, }), t.partial({ + bytesDownloadedCompressed: t.number, + certificates: CertificateDataType, + ip: t.string, method: t.string, - url: t.string, status: t.number, mimeType: t.string, requestStartTime: t.number, + responseHeaders: t.record(t.string, t.string), + requestHeaders: t.record(t.string, t.string), timings: NetworkTimingsType, }), ]); +export type NetworkTimings = t.TypeOf; +export type CertificateData = t.TypeOf; export type NetworkEvent = t.TypeOf; export const SyntheticsNetworkEventsApiResponseType = t.type({ diff --git a/x-pack/plugins/uptime/public/apps/plugin.ts b/x-pack/plugins/uptime/public/apps/plugin.ts index 8bbbecf8108fe..e7a22a080d79a 100644 --- a/x-pack/plugins/uptime/public/apps/plugin.ts +++ b/x-pack/plugins/uptime/public/apps/plugin.ts @@ -87,6 +87,28 @@ export class UptimePlugin order: 8400, title: PLUGIN.TITLE, category: DEFAULT_APP_CATEGORIES.observability, + meta: { + keywords: [ + 'Synthetics', + 'pings', + 'checks', + 'availability', + 'response duration', + 'response time', + 'outside in', + 'reachability', + 'reachable', + 'digital', + 'performance', + 'web performance', + 'web perf', + ], + searchDeepLinks: [ + { id: 'Down monitors', title: 'Down monitors', path: '/?statusFilter=down' }, + { id: 'Certificates', title: 'TLS Certificates', path: '/certificates' }, + { id: 'Settings', title: 'Settings', path: '/settings' }, + ], + }, mount: async (params: AppMountParameters) => { const [coreStart, corePlugins] = await core.getStartServices(); diff --git a/x-pack/plugins/uptime/public/components/common/charts/__snapshots__/donut_chart.test.tsx.snap b/x-pack/plugins/uptime/public/components/common/charts/__snapshots__/donut_chart.test.tsx.snap index 967d078bde210..238ce6c3f9cee 100644 --- a/x-pack/plugins/uptime/public/components/common/charts/__snapshots__/donut_chart.test.tsx.snap +++ b/x-pack/plugins/uptime/public/components/common/charts/__snapshots__/donut_chart.test.tsx.snap @@ -39,6 +39,7 @@ exports[`DonutChart component passes correct props without errors for valid prop "fill": "white", "opacity": 1, "radius": 2, + "shape": "circle", "strokeWidth": 1, "visible": false, }, @@ -134,6 +135,7 @@ exports[`DonutChart component passes correct props without errors for valid prop "fill": "white", "opacity": 1, "radius": 2, + "shape": "circle", "strokeWidth": 1, "visible": true, }, @@ -170,12 +172,17 @@ exports[`DonutChart component passes correct props without errors for valid prop "fill": "#F5F5F5", "visible": true, }, - "line": Object { + "crossLine": Object { "dash": Array [ 5, 5, ], - "stroke": "#777", + "stroke": "#98A2B3", + "strokeWidth": 1, + "visible": true, + }, + "line": Object { + "stroke": "#98A2B3", "strokeWidth": 1, "visible": true, }, @@ -196,6 +203,7 @@ exports[`DonutChart component passes correct props without errors for valid prop "fill": "white", "opacity": 1, "radius": 2, + "shape": "circle", "strokeWidth": 1, "visible": true, }, diff --git a/x-pack/plugins/uptime/public/components/common/charts/duration_line_bar_list.tsx b/x-pack/plugins/uptime/public/components/common/charts/duration_line_bar_list.tsx index 2c6ad63b51e7b..3d0fefbd083f8 100644 --- a/x-pack/plugins/uptime/public/components/common/charts/duration_line_bar_list.tsx +++ b/x-pack/plugins/uptime/public/components/common/charts/duration_line_bar_list.tsx @@ -7,8 +7,7 @@ import React from 'react'; import moment from 'moment'; -import { AnnotationTooltipFormatter, RectAnnotation } from '@elastic/charts'; -import { RectAnnotationDatum } from '@elastic/charts/dist/chart_types/xy_chart/utils/specs'; +import { AnnotationTooltipFormatter, RectAnnotation, RectAnnotationDatum } from '@elastic/charts'; import { AnnotationTooltip } from './annotation_tooltip'; import { ANOMALY_SEVERITY, diff --git a/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.test.tsx b/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.test.tsx index b96b61d874330..bf5b0215e7d7a 100644 --- a/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.test.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.test.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; -import { PingList } from './ping_list'; +import { formatDuration, PingList } from './ping_list'; import { Ping, PingsResponse } from '../../../../common/runtime_types'; import { ExpandedRowMap } from '../../overview/monitor_list/types'; import { rowShouldExpand, toggleDetails } from './columns/expand_row'; @@ -185,5 +185,23 @@ describe('PingList component', () => { expect(rowShouldExpand(ping)).toBe(true); }); }); + + describe('formatDuration', () => { + it('returns zero for < 1 millisecond', () => { + expect(formatDuration(984)).toBe('0 ms'); + }); + + it('returns milliseconds string if < 1 seconds', () => { + expect(formatDuration(921_039)).toBe('921 ms'); + }); + + it('returns seconds string if > 1 second', () => { + expect(formatDuration(1_032_100)).toBe('1 second'); + }); + + it('rounds to closest second', () => { + expect(formatDuration(1_832_100)).toBe('2 seconds'); + }); + }); }); }); diff --git a/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.tsx b/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.tsx index 110c46eca31d1..18bc5f5ec3ecb 100644 --- a/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/ping_list/ping_list.tsx @@ -34,6 +34,35 @@ export const SpanWithMargin = styled.span` const DEFAULT_PAGE_SIZE = 10; +// one second = 1 million micros +const ONE_SECOND_AS_MICROS = 1000000; + +// the limit for converting to seconds is >= 1 sec +const MILLIS_LIMIT = ONE_SECOND_AS_MICROS * 1; + +export const formatDuration = (durationMicros: number) => { + if (durationMicros < MILLIS_LIMIT) { + return i18n.translate('xpack.uptime.pingList.durationMsColumnFormatting', { + values: { millis: microsToMillis(durationMicros) }, + defaultMessage: '{millis} ms', + }); + } + const seconds = (durationMicros / ONE_SECOND_AS_MICROS).toFixed(0); + + // we format seconds with correct pulralization here and not for `ms` because it is much more likely users + // will encounter times of exactly '1' second. + if (seconds === '1') { + return i18n.translate('xpack.uptime.pingist.durationSecondsColumnFormatting.singular', { + values: { seconds }, + defaultMessage: '{seconds} second', + }); + } + return i18n.translate('xpack.uptime.pingist.durationSecondsColumnFormatting', { + values: { seconds }, + defaultMessage: '{seconds} seconds', + }); +}; + export const PingList = () => { const [pageSize, setPageSize] = useState(DEFAULT_PAGE_SIZE); const [pageIndex, setPageIndex] = useState(0); @@ -135,11 +164,7 @@ export const PingList = () => { name: i18n.translate('xpack.uptime.pingList.durationMsColumnLabel', { defaultMessage: 'Duration', }), - render: (duration: number) => - i18n.translate('xpack.uptime.pingList.durationMsColumnFormatting', { - values: { millis: microsToMillis(duration) }, - defaultMessage: '{millis} ms', - }), + render: (duration: number) => formatDuration(duration), }, { field: 'error.type', diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.test.ts b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.test.ts index a02116877f49a..9376a83f48b3d 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.test.ts +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.test.ts @@ -4,12 +4,25 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { colourPalette, getSeriesAndDomain, getSidebarItems } from './data_formatting'; -import { NetworkItems, MimeType } from './types'; +import moment from 'moment'; +import { + colourPalette, + getConnectingTime, + getSeriesAndDomain, + getSidebarItems, +} from './data_formatting'; +import { + NetworkItems, + MimeType, + FriendlyFlyoutLabels, + FriendlyTimingLabels, + Timings, + Metadata, +} from './types'; +import { mockMoment } from '../../../../../lib/helper/test_helpers'; import { WaterfallDataEntry } from '../../waterfall/types'; -const networkItems: NetworkItems = [ +export const networkItems: NetworkItems = [ { timestamp: '2021-01-05T19:22:28.928Z', method: 'GET', @@ -31,6 +44,20 @@ const networkItems: NetworkItems = [ ssl: 55.38700000033714, dns: 3.559999997378327, }, + bytesDownloadedCompressed: 1000, + requestHeaders: { + sample_request_header: 'sample request header', + }, + responseHeaders: { + sample_response_header: 'sample response header', + }, + certificates: { + issuer: 'Sample Issuer', + validFrom: 1578441600000, + validTo: 1617883200000, + subjectName: '*.elastic.co', + }, + ip: '104.18.8.22', }, { timestamp: '2021-01-05T19:22:28.928Z', @@ -56,7 +83,7 @@ const networkItems: NetworkItems = [ }, ]; -const networkItemsWithoutFullTimings: NetworkItems = [ +export const networkItemsWithoutFullTimings: NetworkItems = [ networkItems[0], { timestamp: '2021-01-05T19:22:28.928Z', @@ -81,7 +108,7 @@ const networkItemsWithoutFullTimings: NetworkItems = [ }, ]; -const networkItemsWithoutAnyTimings: NetworkItems = [ +export const networkItemsWithoutAnyTimings: NetworkItems = [ { timestamp: '2021-01-05T19:22:28.928Z', method: 'GET', @@ -105,7 +132,7 @@ const networkItemsWithoutAnyTimings: NetworkItems = [ }, ]; -const networkItemsWithoutTimingsObject: NetworkItems = [ +export const networkItemsWithoutTimingsObject: NetworkItems = [ { timestamp: '2021-01-05T19:22:28.928Z', method: 'GET', @@ -117,7 +144,7 @@ const networkItemsWithoutTimingsObject: NetworkItems = [ }, ]; -const networkItemsWithUncommonMimeType: NetworkItems = [ +export const networkItemsWithUncommonMimeType: NetworkItems = [ { timestamp: '2021-01-05T19:22:28.928Z', method: 'GET', @@ -142,6 +169,28 @@ const networkItemsWithUncommonMimeType: NetworkItems = [ }, ]; +describe('getConnectingTime', () => { + it('returns `connect` value if `ssl` is undefined', () => { + expect(getConnectingTime(10)).toBe(10); + }); + + it('returns `undefined` if `connect` is not defined', () => { + expect(getConnectingTime(undefined, 23)).toBeUndefined(); + }); + + it('returns `connect` value if `ssl` is 0', () => { + expect(getConnectingTime(10, 0)).toBe(10); + }); + + it('returns `connect` value if `ssl` is -1', () => { + expect(getConnectingTime(10, 0)).toBe(10); + }); + + it('reduces `connect` value by `ssl` value if both are defined', () => { + expect(getConnectingTime(10, 3)).toBe(7); + }); +}); + describe('Palettes', () => { it('A colour palette comprising timing and mime type colours is correctly generated', () => { expect(colourPalette).toEqual({ @@ -163,299 +212,326 @@ describe('Palettes', () => { }); describe('getSeriesAndDomain', () => { - it('formats timings', () => { + beforeEach(() => { + mockMoment(); + }); + + it('formats series timings', () => { const actual = getSeriesAndDomain(networkItems); - expect(actual).toMatchInlineSnapshot(` - Object { - "domain": Object { - "max": 140.7760000010603, - "min": 0, - }, - "series": Array [ - Object { - "config": Object { + expect(actual.series).toMatchInlineSnapshot(` + Array [ + Object { + "config": Object { + "colour": "#dcd4c4", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#dcd4c4", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#dcd4c4", - "value": "Queued / Blocked: 0.854ms", - }, + "value": "Queued / Blocked: 0.854ms", }, - "x": 0, - "y": 0.8540000017092098, - "y0": 0, }, - Object { - "config": Object { + "x": 0, + "y": 0.8540000017092098, + "y0": 0, + }, + Object { + "config": Object { + "colour": "#54b399", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#54b399", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#54b399", - "value": "DNS: 3.560ms", - }, + "value": "DNS: 3.560ms", }, - "x": 0, - "y": 4.413999999087537, - "y0": 0.8540000017092098, }, - Object { - "config": Object { + "x": 0, + "y": 4.413999999087537, + "y0": 0.8540000017092098, + }, + Object { + "config": Object { + "colour": "#da8b45", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#da8b45", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#da8b45", - "value": "Connecting: 25.721ms", - }, + "value": "Connecting: 25.721ms", }, - "x": 0, - "y": 30.135000000882428, - "y0": 4.413999999087537, }, - Object { - "config": Object { + "x": 0, + "y": 30.135000000882428, + "y0": 4.413999999087537, + }, + Object { + "config": Object { + "colour": "#edc5a2", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#edc5a2", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#edc5a2", - "value": "TLS: 55.387ms", - }, + "value": "TLS: 55.387ms", }, - "x": 0, - "y": 85.52200000121957, - "y0": 30.135000000882428, }, - Object { - "config": Object { + "x": 0, + "y": 85.52200000121957, + "y0": 30.135000000882428, + }, + Object { + "config": Object { + "colour": "#d36086", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#d36086", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#d36086", - "value": "Sending request: 0.360ms", - }, + "value": "Sending request: 0.360ms", }, - "x": 0, - "y": 85.88200000303914, - "y0": 85.52200000121957, }, - Object { - "config": Object { + "x": 0, + "y": 85.88200000303914, + "y0": 85.52200000121957, + }, + Object { + "config": Object { + "colour": "#b0c9e0", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#b0c9e0", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#b0c9e0", - "value": "Waiting (TTFB): 34.578ms", - }, + "value": "Waiting (TTFB): 34.578ms", }, - "x": 0, - "y": 120.4600000019127, - "y0": 85.88200000303914, }, - Object { - "config": Object { + "x": 0, + "y": 120.4600000019127, + "y0": 85.88200000303914, + }, + Object { + "config": Object { + "colour": "#ca8eae", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#ca8eae", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#ca8eae", - "value": "Content downloading (CSS): 0.552ms", - }, + "value": "Content downloading (CSS): 0.552ms", }, - "x": 0, - "y": 121.01200000324752, - "y0": 120.4600000019127, }, - Object { - "config": Object { + "x": 0, + "y": 121.01200000324752, + "y0": 120.4600000019127, + }, + Object { + "config": Object { + "colour": "#dcd4c4", + "id": 1, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#dcd4c4", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#dcd4c4", - "value": "Queued / Blocked: 84.546ms", - }, + "value": "Queued / Blocked: 84.546ms", }, - "x": 1, - "y": 84.90799999795854, - "y0": 0.3619999997317791, }, - Object { - "config": Object { + "x": 1, + "y": 84.90799999795854, + "y0": 0.3619999997317791, + }, + Object { + "config": Object { + "colour": "#d36086", + "id": 1, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#d36086", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#d36086", - "value": "Sending request: 0.239ms", - }, + "value": "Sending request: 0.239ms", }, - "x": 1, - "y": 85.14699999883305, - "y0": 84.90799999795854, }, - Object { - "config": Object { + "x": 1, + "y": 85.14699999883305, + "y0": 84.90799999795854, + }, + Object { + "config": Object { + "colour": "#b0c9e0", + "id": 1, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#b0c9e0", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#b0c9e0", - "value": "Waiting (TTFB): 52.561ms", - }, + "value": "Waiting (TTFB): 52.561ms", }, - "x": 1, - "y": 137.70799999925657, - "y0": 85.14699999883305, }, - Object { - "config": Object { + "x": 1, + "y": 137.70799999925657, + "y0": 85.14699999883305, + }, + Object { + "config": Object { + "colour": "#9170b8", + "id": 1, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#9170b8", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#9170b8", - "value": "Content downloading (JS): 3.068ms", - }, + "value": "Content downloading (JS): 3.068ms", }, - "x": 1, - "y": 140.7760000010603, - "y0": 137.70799999925657, }, - ], - "totalHighlightedRequests": 2, - } + "x": 1, + "y": 140.7760000010603, + "y0": 137.70799999925657, + }, + ] `); }); - it('handles formatting when only total timing values are available', () => { - const actual = getSeriesAndDomain(networkItemsWithoutFullTimings); - expect(actual).toMatchInlineSnapshot(` - Object { - "domain": Object { - "max": 121.01200000324752, - "min": 0, - }, - "series": Array [ - Object { - "config": Object { + it('handles series formatting when only total timing values are available', () => { + const { series } = getSeriesAndDomain(networkItemsWithoutFullTimings); + expect(series).toMatchInlineSnapshot(` + Array [ + Object { + "config": Object { + "colour": "#dcd4c4", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#dcd4c4", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#dcd4c4", - "value": "Queued / Blocked: 0.854ms", - }, + "value": "Queued / Blocked: 0.854ms", }, - "x": 0, - "y": 0.8540000017092098, - "y0": 0, }, - Object { - "config": Object { + "x": 0, + "y": 0.8540000017092098, + "y0": 0, + }, + Object { + "config": Object { + "colour": "#54b399", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#54b399", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#54b399", - "value": "DNS: 3.560ms", - }, + "value": "DNS: 3.560ms", }, - "x": 0, - "y": 4.413999999087537, - "y0": 0.8540000017092098, }, - Object { - "config": Object { + "x": 0, + "y": 4.413999999087537, + "y0": 0.8540000017092098, + }, + Object { + "config": Object { + "colour": "#da8b45", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#da8b45", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#da8b45", - "value": "Connecting: 25.721ms", - }, + "value": "Connecting: 25.721ms", }, - "x": 0, - "y": 30.135000000882428, - "y0": 4.413999999087537, }, - Object { - "config": Object { + "x": 0, + "y": 30.135000000882428, + "y0": 4.413999999087537, + }, + Object { + "config": Object { + "colour": "#edc5a2", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#edc5a2", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#edc5a2", - "value": "TLS: 55.387ms", - }, + "value": "TLS: 55.387ms", }, - "x": 0, - "y": 85.52200000121957, - "y0": 30.135000000882428, }, - Object { - "config": Object { + "x": 0, + "y": 85.52200000121957, + "y0": 30.135000000882428, + }, + Object { + "config": Object { + "colour": "#d36086", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#d36086", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#d36086", - "value": "Sending request: 0.360ms", - }, + "value": "Sending request: 0.360ms", }, - "x": 0, - "y": 85.88200000303914, - "y0": 85.52200000121957, }, - Object { - "config": Object { + "x": 0, + "y": 85.88200000303914, + "y0": 85.52200000121957, + }, + Object { + "config": Object { + "colour": "#b0c9e0", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#b0c9e0", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#b0c9e0", - "value": "Waiting (TTFB): 34.578ms", - }, + "value": "Waiting (TTFB): 34.578ms", }, - "x": 0, - "y": 120.4600000019127, - "y0": 85.88200000303914, }, - Object { - "config": Object { + "x": 0, + "y": 120.4600000019127, + "y0": 85.88200000303914, + }, + Object { + "config": Object { + "colour": "#ca8eae", + "id": 0, + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#ca8eae", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#ca8eae", - "value": "Content downloading (CSS): 0.552ms", - }, + "value": "Content downloading (CSS): 0.552ms", }, - "x": 0, - "y": 121.01200000324752, - "y0": 120.4600000019127, }, - Object { - "config": Object { + "x": 0, + "y": 121.01200000324752, + "y0": 120.4600000019127, + }, + Object { + "config": Object { + "colour": "#9170b8", + "isHighlighted": true, + "showTooltip": true, + "tooltipProps": Object { "colour": "#9170b8", - "isHighlighted": true, - "showTooltip": true, - "tooltipProps": Object { - "colour": "#9170b8", - "value": "Content downloading (JS): 2.793ms", - }, + "value": "Content downloading (JS): 2.793ms", }, - "x": 1, - "y": 3.714999998046551, - "y0": 0.9219999983906746, }, - ], - "totalHighlightedRequests": 2, - } + "x": 1, + "y": 3.714999998046551, + "y0": 0.9219999983906746, + }, + ] + `); + }); + + it('handles series formatting when there is no timing information available', () => { + const { series } = getSeriesAndDomain(networkItemsWithoutAnyTimings); + expect(series).toMatchInlineSnapshot(` + Array [ + Object { + "config": Object { + "colour": "", + "isHighlighted": true, + "showTooltip": false, + "tooltipProps": undefined, + }, + "x": 0, + "y": 0, + "y0": 0, + }, + ] `); }); @@ -467,6 +543,53 @@ describe('getSeriesAndDomain', () => { "max": 0, "min": 0, }, + "metadata": Array [ + Object { + "certificates": undefined, + "details": Array [ + Object { + "name": "Content type", + "value": "text/javascript", + }, + Object { + "name": "Request start", + "value": "0.000 ms", + }, + Object { + "name": "DNS", + "value": undefined, + }, + Object { + "name": "Connecting", + "value": undefined, + }, + Object { + "name": "TLS", + "value": undefined, + }, + Object { + "name": "Waiting (TTFB)", + "value": undefined, + }, + Object { + "name": "Content downloading", + "value": undefined, + }, + Object { + "name": "Bytes downloaded (compressed)", + "value": undefined, + }, + Object { + "name": "IP", + "value": undefined, + }, + ], + "requestHeaders": undefined, + "responseHeaders": undefined, + "url": "file:///Users/dominiqueclarke/dev/synthetics/examples/todos/app/app.js", + "x": 0, + }, + ], "series": Array [ Object { "config": Object { @@ -486,32 +609,24 @@ describe('getSeriesAndDomain', () => { }); it('handles formatting when the timings object is undefined', () => { - const actual = getSeriesAndDomain(networkItemsWithoutTimingsObject); - expect(actual).toMatchInlineSnapshot(` - Object { - "domain": Object { - "max": 0, - "min": 0, - }, - "series": Array [ - Object { - "config": Object { - "isHighlighted": true, - "showTooltip": false, - }, - "x": 0, - "y": 0, - "y0": 0, + const { series } = getSeriesAndDomain(networkItemsWithoutTimingsObject); + expect(series).toMatchInlineSnapshot(` + Array [ + Object { + "config": Object { + "isHighlighted": true, + "showTooltip": false, }, - ], - "totalHighlightedRequests": 1, - } + "x": 0, + "y": 0, + "y0": 0, + }, + ] `); }); it('handles formatting when mime type is not mapped to a specific mime type bucket', () => { - const actual = getSeriesAndDomain(networkItemsWithUncommonMimeType); - const { series } = actual; + const { series } = getSeriesAndDomain(networkItemsWithUncommonMimeType); /* verify that raw mime type appears in the tooltip config and that * the colour is mapped to mime type other */ const contentDownloadedingConfigItem = series.find((item: WaterfallDataEntry) => { @@ -527,6 +642,48 @@ describe('getSeriesAndDomain', () => { expect(contentDownloadedingConfigItem).toBeDefined(); }); + it.each([ + [FriendlyFlyoutLabels[Metadata.MimeType], 'text/css'], + [FriendlyFlyoutLabels[Metadata.RequestStart], '0.000 ms'], + [FriendlyTimingLabels[Timings.Dns], '3.560 ms'], + [FriendlyTimingLabels[Timings.Connect], '25.721 ms'], + [FriendlyTimingLabels[Timings.Ssl], '55.387 ms'], + [FriendlyTimingLabels[Timings.Wait], '34.578 ms'], + [FriendlyTimingLabels[Timings.Receive], '0.552 ms'], + [FriendlyFlyoutLabels[Metadata.BytesDownloadedCompressed], '1.000 KB'], + [FriendlyFlyoutLabels[Metadata.IP], '104.18.8.22'], + ])('handles metadata details formatting', (name, value) => { + const { metadata } = getSeriesAndDomain(networkItems); + const metadataEntry = metadata[0]; + expect( + metadataEntry.details.find((item) => item.value === value && item.name === name) + ).toBeDefined(); + }); + + it('handles metadata headers formatting', () => { + const { metadata } = getSeriesAndDomain(networkItems); + const metadataEntry = metadata[0]; + metadataEntry.requestHeaders?.forEach((header) => { + expect(header).toEqual({ name: header.name, value: header.value }); + }); + metadataEntry.responseHeaders?.forEach((header) => { + expect(header).toEqual({ name: header.name, value: header.value }); + }); + }); + + it('handles certificate formatting', () => { + const { metadata } = getSeriesAndDomain([networkItems[0]]); + const metadataEntry = metadata[0]; + expect(metadataEntry.certificates).toEqual([ + { name: 'Issuer', value: networkItems[0].certificates?.issuer }, + { name: 'Valid from', value: moment(networkItems[0].certificates?.validFrom).format('L LT') }, + { name: 'Valid until', value: moment(networkItems[0].certificates?.validTo).format('L LT') }, + { name: 'Common name', value: networkItems[0].certificates?.subjectName }, + ]); + metadataEntry.responseHeaders?.forEach((header) => { + expect(header).toEqual({ name: header.name, value: header.value }); + }); + }); it('counts the total number of highlighted items', () => { // only one CSS file in this array of network Items const actual = getSeriesAndDomain(networkItems, false, '', ['stylesheet']); diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.ts b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.ts index 46f0d23d0a6b9..23d9b2d8563ae 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.ts +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/data_formatting.ts @@ -6,20 +6,23 @@ */ import { euiPaletteColorBlind } from '@elastic/eui'; +import moment from 'moment'; import { NetworkItems, NetworkItem, + FriendlyFlyoutLabels, FriendlyTimingLabels, FriendlyMimetypeLabels, MimeType, MimeTypesMap, Timings, + Metadata, TIMING_ORDER, SidebarItems, LegendItems, } from './types'; -import { WaterfallData } from '../../waterfall'; +import { WaterfallData, WaterfallMetadata } from '../../waterfall'; import { NetworkEvent } from '../../../../../../common/runtime_types'; export const extractItems = (data: NetworkEvent[]): NetworkItems => { @@ -71,6 +74,29 @@ export const isHighlightedItem = ( return !!(matchQuery && matchFilters); }; +const getFriendlyMetadataValue = ({ value, postFix }: { value?: number; postFix?: string }) => { + // value === -1 indicates timing data cannot be extracted + if (value === undefined || value === -1) { + return undefined; + } + + let formattedValue = formatValueForDisplay(value); + + if (postFix) { + formattedValue = `${formattedValue} ${postFix}`; + } + + return formattedValue; +}; + +export const getConnectingTime = (connect?: number, ssl?: number) => { + if (ssl && connect && ssl > 0) { + return connect - ssl; + } else { + return connect; + } +}; + export const getSeriesAndDomain = ( items: NetworkItems, onlyHighlighted = false, @@ -80,34 +106,36 @@ export const getSeriesAndDomain = ( const getValueForOffset = (item: NetworkItem) => { return item.requestSentTime; }; - // The earliest point in time a request is sent or started. This will become our notion of "0". - const zeroOffset = items.reduce((acc, item) => { - const offsetValue = getValueForOffset(item); - return offsetValue < acc ? offsetValue : acc; - }, Infinity); + let zeroOffset = Infinity; + items.forEach((i) => (zeroOffset = Math.min(zeroOffset, getValueForOffset(i)))); const getValue = (timings: NetworkEvent['timings'], timing: Timings) => { if (!timings) return; // SSL is a part of the connect timing - if (timing === Timings.Connect && timings.ssl > 0) { - return timings.connect - timings.ssl; - } else { - return timings[timing]; + if (timing === Timings.Connect) { + return getConnectingTime(timings.connect, timings.ssl); } + return timings[timing]; }; + const series: WaterfallData = []; + const metadata: WaterfallMetadata = []; let totalHighlightedRequests = 0; - const series = items.reduce((acc, item, index) => { + items.forEach((item, index) => { + const mimeTypeColour = getColourForMimeType(item.mimeType); + const offsetValue = getValueForOffset(item); + let currentOffset = offsetValue - zeroOffset; + metadata.push(formatMetadata({ item, index, requestStart: currentOffset })); const isHighlighted = isHighlightedItem(item, query, activeFilters); if (isHighlighted) { totalHighlightedRequests++; } if (!item.timings) { - acc.push({ + series.push({ x: index, y0: 0, y: 0, @@ -116,14 +144,9 @@ export const getSeriesAndDomain = ( showTooltip: false, }, }); - return acc; + return; } - const offsetValue = getValueForOffset(item); - const mimeTypeColour = getColourForMimeType(item.mimeType); - - let currentOffset = offsetValue - zeroOffset; - let timingValueFound = false; TIMING_ORDER.forEach((timing) => { @@ -133,11 +156,12 @@ export const getSeriesAndDomain = ( const colour = timing === Timings.Receive ? mimeTypeColour : colourPalette[timing]; const y = currentOffset + value; - acc.push({ + series.push({ x: index, y0: currentOffset, y, config: { + id: index, colour, isHighlighted, showTooltip: true, @@ -161,7 +185,7 @@ export const getSeriesAndDomain = ( if (!timingValueFound) { const total = item.timings.total; const hasTotal = total !== -1; - acc.push({ + series.push({ x: index, y0: hasTotal ? currentOffset : 0, y: hasTotal ? currentOffset + item.timings.total : 0, @@ -182,8 +206,7 @@ export const getSeriesAndDomain = ( }, }); } - return acc; - }, []); + }); const yValues = series.map((serie) => serie.y); const domain = { min: 0, max: Math.max(...yValues) }; @@ -193,7 +216,108 @@ export const getSeriesAndDomain = ( filteredSeries = series.filter((item) => item.config.isHighlighted); } - return { series: filteredSeries, domain, totalHighlightedRequests }; + return { series: filteredSeries, domain, metadata, totalHighlightedRequests }; +}; + +const formatHeaders = (headers?: Record) => { + if (typeof headers === 'undefined') { + return undefined; + } + return Object.keys(headers).map((key) => ({ + name: key, + value: `${headers[key]}`, + })); +}; + +const formatMetadata = ({ + item, + index, + requestStart, +}: { + item: NetworkItem; + index: number; + requestStart: number; +}) => { + const { + bytesDownloadedCompressed, + certificates, + ip, + mimeType, + requestHeaders, + responseHeaders, + url, + } = item; + const { dns, connect, ssl, wait, receive, total } = item.timings || {}; + const contentDownloaded = receive && receive > 0 ? receive : total; + return { + x: index, + url, + requestHeaders: formatHeaders(requestHeaders), + responseHeaders: formatHeaders(responseHeaders), + certificates: certificates + ? [ + { + name: FriendlyFlyoutLabels[Metadata.CertificateIssuer], + value: certificates.issuer, + }, + { + name: FriendlyFlyoutLabels[Metadata.CertificateIssueDate], + value: certificates.validFrom + ? moment(certificates.validFrom).format('L LT') + : undefined, + }, + { + name: FriendlyFlyoutLabels[Metadata.CertificateExpiryDate], + value: certificates.validTo ? moment(certificates.validTo).format('L LT') : undefined, + }, + { + name: FriendlyFlyoutLabels[Metadata.CertificateSubject], + value: certificates.subjectName, + }, + ] + : undefined, + details: [ + { name: FriendlyFlyoutLabels[Metadata.MimeType], value: mimeType }, + { + name: FriendlyFlyoutLabels[Metadata.RequestStart], + value: getFriendlyMetadataValue({ value: requestStart, postFix: 'ms' }), + }, + { + name: FriendlyTimingLabels[Timings.Dns], + value: getFriendlyMetadataValue({ value: dns, postFix: 'ms' }), + }, + { + name: FriendlyTimingLabels[Timings.Connect], + value: getFriendlyMetadataValue({ value: getConnectingTime(connect, ssl), postFix: 'ms' }), + }, + { + name: FriendlyTimingLabels[Timings.Ssl], + value: getFriendlyMetadataValue({ value: ssl, postFix: 'ms' }), + }, + { + name: FriendlyTimingLabels[Timings.Wait], + value: getFriendlyMetadataValue({ value: wait, postFix: 'ms' }), + }, + { + name: FriendlyTimingLabels[Timings.Receive], + value: getFriendlyMetadataValue({ + value: contentDownloaded, + postFix: 'ms', + }), + }, + { + name: FriendlyFlyoutLabels[Metadata.BytesDownloadedCompressed], + value: getFriendlyMetadataValue({ + value: bytesDownloadedCompressed ? bytesDownloadedCompressed / 1000 : undefined, + postFix: 'KB', + }), + }, + { + name: FriendlyFlyoutLabels[Metadata.IP], + value: ip, + }, + ], + }; }; export const getSidebarItems = ( @@ -206,7 +330,7 @@ export const getSidebarItems = ( const isHighlighted = isHighlightedItem(item, query, activeFilters); const offsetIndex = index + 1; const { url, status, method } = item; - return { url, status, method, isHighlighted, offsetIndex }; + return { url, status, method, isHighlighted, offsetIndex, index }; }); if (onlyHighlighted) { return sideBarItems.filter((item) => item.isHighlighted); diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/types.ts b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/types.ts index e22caae0d9eb2..cedf9c667d0f2 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/types.ts +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/types.ts @@ -18,6 +18,17 @@ export enum Timings { Receive = 'receive', } +export enum Metadata { + BytesDownloadedCompressed = 'bytesDownloadedCompressed', + CertificateIssuer = 'certificateIssuer', + CertificateIssueDate = 'certificateIssueDate', + CertificateExpiryDate = 'certificateExpiryDate', + CertificateSubject = 'certificateSubject', + IP = 'ip', + MimeType = 'mimeType', + RequestStart = 'requestStart', +} + export const FriendlyTimingLabels = { [Timings.Blocked]: i18n.translate( 'xpack.uptime.synthetics.waterfallChart.labels.timings.blocked', @@ -51,6 +62,54 @@ export const FriendlyTimingLabels = { ), }; +export const FriendlyFlyoutLabels = { + [Metadata.MimeType]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.contentType', + { + defaultMessage: 'Content type', + } + ), + [Metadata.RequestStart]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.requestStart', + { + defaultMessage: 'Request start', + } + ), + [Metadata.BytesDownloadedCompressed]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.bytesDownloadedCompressed', + { + defaultMessage: 'Bytes downloaded (compressed)', + } + ), + [Metadata.CertificateIssuer]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.certificateIssuer', + { + defaultMessage: 'Issuer', + } + ), + [Metadata.CertificateIssueDate]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.certificateIssueDate', + { + defaultMessage: 'Valid from', + } + ), + [Metadata.CertificateExpiryDate]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.certificateExpiryDate', + { + defaultMessage: 'Valid until', + } + ), + [Metadata.CertificateSubject]: i18n.translate( + 'xpack.uptime.synthetics.waterfallChart.labels.metadata.certificateSubject', + { + defaultMessage: 'Common name', + } + ), + [Metadata.IP]: i18n.translate('xpack.uptime.synthetics.waterfallChart.labels.metadata.ip', { + defaultMessage: 'IP', + }), +}; + export const TIMING_ORDER = [ Timings.Blocked, Timings.Dns, @@ -61,6 +120,19 @@ export const TIMING_ORDER = [ Timings.Receive, ] as const; +export const META_DATA_ORDER_FLYOUT = [ + Metadata.MimeType, + Timings.Dns, + Timings.Connect, + Timings.Ssl, + Timings.Wait, + Timings.Receive, +] as const; + +export type CalculatedTimings = { + [K in Timings]?: number; +}; + export enum MimeType { Html = 'html', Script = 'script', @@ -155,6 +227,7 @@ export type NetworkItems = NetworkItem[]; export type SidebarItem = Pick & { isHighlighted: boolean; + index: number; offsetIndex: number; }; export type SidebarItems = SidebarItem[]; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.test.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.test.tsx index e22f4a4c63f59..47c18225f38d3 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.test.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.test.tsx @@ -6,14 +6,12 @@ */ import React from 'react'; -import { act, fireEvent } from '@testing-library/react'; -import { WaterfallChartWrapper } from './waterfall_chart_wrapper'; - +import { act, fireEvent, waitFor } from '@testing-library/react'; import { render } from '../../../../../lib/helper/rtl_helpers'; +import { WaterfallChartWrapper } from './waterfall_chart_wrapper'; +import { networkItems as mockNetworkItems } from './data_formatting.test'; import { extractItems, isHighlightedItem } from './data_formatting'; - -import 'jest-canvas-mock'; import { BAR_HEIGHT } from '../../waterfall/components/constants'; import { MimeType } from './types'; import { @@ -26,8 +24,10 @@ const getHighLightedItems = (query: string, filters: string[]) => { return NETWORK_EVENTS.events.filter((item) => isHighlightedItem(item, query, filters)); }; -describe('waterfall chart wrapper', () => { - jest.useFakeTimers(); +describe('WaterfallChartWrapper', () => { + beforeAll(() => { + jest.useFakeTimers(); + }); it('renders the correct sidebar items', () => { const { getAllByTestId } = render( @@ -129,6 +129,69 @@ describe('waterfall chart wrapper', () => { expect(queryAllByTestId('sideBarHighlightedItem')).toHaveLength(0); expect(queryAllByTestId('sideBarDimmedItem')).toHaveLength(0); }); + + it('opens flyout on sidebar click and closes on flyout close button', async () => { + const { getByText, getAllByText, getByTestId, queryByText, getByRole } = render( + + ); + + expect(getByText(`1. ${mockNetworkItems[0].url}`)).toBeInTheDocument(); + expect(queryByText('Content type')).not.toBeInTheDocument(); + expect(queryByText(`${mockNetworkItems[0]?.mimeType}`)).not.toBeInTheDocument(); + + // open flyout + // selecter matches both button and accessible text. Button is the second element in the array; + const sidebarButton = getAllByText(/1./)[1]; + fireEvent.click(sidebarButton); + + // check for sample flyout items + await waitFor(() => { + const waterfallFlyout = getByRole('dialog'); + expect(waterfallFlyout).toBeInTheDocument(); + expect(getByText('Content type')).toBeInTheDocument(); + expect(getByText(`${mockNetworkItems[0]?.mimeType}`)).toBeInTheDocument(); + // close flyout + const closeButton = getByTestId('euiFlyoutCloseButton'); + fireEvent.click(closeButton); + }); + + /* check that sample flyout items are gone from the DOM */ + await waitFor(() => { + expect(queryByText('Content type')).not.toBeInTheDocument(); + expect(queryByText(`${mockNetworkItems[0]?.mimeType}`)).not.toBeInTheDocument(); + }); + }); + + it('opens flyout on sidebar click and closes on second sidebar click', async () => { + const { getByText, getAllByText, getByTestId, queryByText } = render( + + ); + + expect(getByText(`1. ${mockNetworkItems[0].url}`)).toBeInTheDocument(); + expect(queryByText('Content type')).not.toBeInTheDocument(); + expect(queryByText(`${mockNetworkItems[0]?.mimeType}`)).not.toBeInTheDocument(); + + // open flyout + // selecter matches both button and accessible text. Button is the second element in the array; + const sidebarButton = getAllByText(/1./)[1]; + fireEvent.click(sidebarButton); + + // check for sample flyout items and that the flyout is focused + await waitFor(() => { + const waterfallFlyout = getByTestId('waterfallFlyout'); + expect(waterfallFlyout).toBeInTheDocument(); + expect(getByText('Content type')).toBeInTheDocument(); + expect(getByText(`${mockNetworkItems[0]?.mimeType}`)).toBeInTheDocument(); + }); + + fireEvent.click(sidebarButton); + + /* check that sample flyout items are gone from the DOM */ + await waitFor(() => { + expect(queryByText('Content type')).not.toBeInTheDocument(); + expect(queryByText(`${mockNetworkItems[0]?.mimeType}`)).not.toBeInTheDocument(); + }); + }); }); const NETWORK_EVENTS = { diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.tsx index 8a0e9729a635b..8557837abbe46 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_chart_wrapper.tsx @@ -7,11 +7,12 @@ import React, { useCallback, useMemo, useState } from 'react'; import { EuiHealth } from '@elastic/eui'; -import { useTrackMetric, METRIC_TYPE } from '../../../../../../../observability/public'; import { getSeriesAndDomain, getSidebarItems, getLegendItems } from './data_formatting'; import { SidebarItem, LegendItem, NetworkItems } from './types'; -import { WaterfallProvider, WaterfallChart, RenderItem } from '../../waterfall'; +import { WaterfallProvider, WaterfallChart, RenderItem, useFlyout } from '../../waterfall'; +import { useTrackMetric, METRIC_TYPE } from '../../../../../../../observability/public'; import { WaterfallFilter } from './waterfall_filter'; +import { WaterfallFlyout } from './waterfall_flyout'; import { WaterfallSidebarItem } from './waterfall_sidebar_item'; export const renderLegendItem: RenderItem = (item) => { @@ -32,7 +33,7 @@ export const WaterfallChartWrapper: React.FC = ({ data, total }) => { const hasFilters = activeFilters.length > 0; - const { series, domain, totalHighlightedRequests } = useMemo(() => { + const { series, domain, metadata, totalHighlightedRequests } = useMemo(() => { return getSeriesAndDomain(networkData, onlyHighlighted, query, activeFilters); }, [networkData, query, activeFilters, onlyHighlighted]); @@ -40,7 +41,18 @@ export const WaterfallChartWrapper: React.FC = ({ data, total }) => { return getSidebarItems(networkData, onlyHighlighted, query, activeFilters); }, [networkData, query, activeFilters, onlyHighlighted]); - const legendItems = getLegendItems(); + const legendItems = useMemo(() => { + return getLegendItems(); + }, []); + + const { + flyoutData, + onBarClick, + onProjectionClick, + onSidebarClick, + isFlyoutVisible, + onFlyoutClose, + } = useFlyout(metadata); const renderFilter = useCallback(() => { return ( @@ -55,16 +67,27 @@ export const WaterfallChartWrapper: React.FC = ({ data, total }) => { ); }, [activeFilters, setActiveFilters, onlyHighlighted, setOnlyHighlighted, query, setQuery]); + const renderFlyout = useCallback(() => { + return ( + + ); + }, [flyoutData, isFlyoutVisible, onFlyoutClose]); + const renderSidebarItem: RenderItem = useCallback( (item) => { return ( ); }, - [hasFilters, onlyHighlighted] + [hasFilters, onlyHighlighted, onSidebarClick] ); useTrackMetric({ app: 'uptime', metric: 'waterfall_chart_view', metricType: METRIC_TYPE.COUNT }); @@ -81,17 +104,21 @@ export const WaterfallChartWrapper: React.FC = ({ data, total }) => { fetchedNetworkRequests={networkData.length} highlightedNetworkRequests={totalHighlightedRequests} data={series} + onElementClick={useCallback(onBarClick, [onBarClick])} + onProjectionClick={useCallback(onProjectionClick, [onProjectionClick])} + onSidebarClick={onSidebarClick} showOnlyHighlightedNetworkRequests={onlyHighlighted} sidebarItems={sidebarItems} legendItems={legendItems} - renderTooltipItem={(tooltipProps) => { + metadata={metadata} + renderTooltipItem={useCallback((tooltipProps) => { return {tooltipProps?.value}; - }} + }, [])} > `${Number(d).toFixed(0)} ms`} + tickFormat={useCallback((d: number) => `${Number(d).toFixed(0)} ms`, [])} domain={domain} - barStyleAccessor={(datum) => { + barStyleAccessor={useCallback((datum) => { if (!datum.datum.config.isHighlighted) { return { rect: { @@ -101,9 +128,10 @@ export const WaterfallChartWrapper: React.FC = ({ data, total }) => { }; } return datum.datum.config.colour; - }} + }, [])} renderSidebarItem={renderSidebarItem} renderLegendItem={renderLegendItem} + renderFlyout={renderFlyout} renderFilter={renderFilter} fullHeight={true} /> diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_flyout.test.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_flyout.test.tsx new file mode 100644 index 0000000000000..4028bc0821b29 --- /dev/null +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_flyout.test.tsx @@ -0,0 +1,139 @@ +/* + * 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 React from 'react'; +import { render } from '../../../../../lib/helper/rtl_helpers'; +import { + WaterfallFlyout, + DETAILS, + CERTIFICATES, + REQUEST_HEADERS, + RESPONSE_HEADERS, +} from './waterfall_flyout'; +import { WaterfallMetadataEntry } from '../../waterfall/types'; + +describe('WaterfallFlyout', () => { + const flyoutData: WaterfallMetadataEntry = { + x: 0, + url: 'http://elastic.co', + requestHeaders: undefined, + responseHeaders: undefined, + certificates: undefined, + details: [ + { + name: 'Content type', + value: 'text/html', + }, + ], + }; + + const defaultProps = { + flyoutData, + isFlyoutVisible: true, + onFlyoutClose: () => null, + }; + + it('displays flyout information and omits sections that are undefined', () => { + const { getByText, queryByText } = render(); + + expect(getByText(flyoutData.url)).toBeInTheDocument(); + expect(queryByText(DETAILS)).toBeInTheDocument(); + flyoutData.details.forEach((detail) => { + expect(getByText(detail.name)).toBeInTheDocument(); + expect(getByText(`${detail.value}`)).toBeInTheDocument(); + }); + + expect(queryByText(CERTIFICATES)).not.toBeInTheDocument(); + expect(queryByText(REQUEST_HEADERS)).not.toBeInTheDocument(); + expect(queryByText(RESPONSE_HEADERS)).not.toBeInTheDocument(); + }); + + it('displays flyout certificates information', () => { + const certificates = [ + { + name: 'Issuer', + value: 'Sample Issuer', + }, + { + name: 'Valid From', + value: 'January 1, 2020 7:00PM', + }, + { + name: 'Valid Until', + value: 'January 31, 2020 7:00PM', + }, + { + name: 'Common Name', + value: '*.elastic.co', + }, + ]; + const flyoutDataWithCertificates = { + ...flyoutData, + certificates, + }; + + const { getByText } = render( + + ); + + expect(getByText(flyoutData.url)).toBeInTheDocument(); + expect(getByText(DETAILS)).toBeInTheDocument(); + expect(getByText(CERTIFICATES)).toBeInTheDocument(); + flyoutData.certificates?.forEach((detail) => { + expect(getByText(detail.name)).toBeInTheDocument(); + expect(getByText(`${detail.value}`)).toBeInTheDocument(); + }); + }); + + it('displays flyout request and response headers information', () => { + const requestHeaders = [ + { + name: 'sample_request_header', + value: 'Sample Request Header value', + }, + ]; + const responseHeaders = [ + { + name: 'sample_response_header', + value: 'sample response header value', + }, + ]; + const flyoutDataWithHeaders = { + ...flyoutData, + requestHeaders, + responseHeaders, + }; + const { getByText } = render( + + ); + + expect(getByText(flyoutData.url)).toBeInTheDocument(); + expect(getByText(DETAILS)).toBeInTheDocument(); + expect(getByText(REQUEST_HEADERS)).toBeInTheDocument(); + expect(getByText(RESPONSE_HEADERS)).toBeInTheDocument(); + flyoutData.requestHeaders?.forEach((detail) => { + expect(getByText(detail.name)).toBeInTheDocument(); + expect(getByText(`${detail.value}`)).toBeInTheDocument(); + }); + flyoutData.responseHeaders?.forEach((detail) => { + expect(getByText(detail.name)).toBeInTheDocument(); + expect(getByText(`${detail.value}`)).toBeInTheDocument(); + }); + }); + + it('renders null when isFlyoutVisible is false', () => { + const { queryByText } = render(); + + expect(queryByText(flyoutData.url)).not.toBeInTheDocument(); + }); + + it('renders null when flyoutData is undefined', () => { + const { queryByText } = render(); + + expect(queryByText(flyoutData.url)).not.toBeInTheDocument(); + }); +}); diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_flyout.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_flyout.tsx new file mode 100644 index 0000000000000..4f92c882340b9 --- /dev/null +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_flyout.tsx @@ -0,0 +1,125 @@ +/* + * 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 React, { useEffect, useRef } from 'react'; + +import styled from 'styled-components'; + +import { + EuiFlyout, + EuiFlyoutHeader, + EuiFlyoutBody, + EuiTitle, + EuiSpacer, + EuiFlexItem, +} from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { Table } from '../../waterfall/components/waterfall_flyout_table'; +import { MiddleTruncatedText } from '../../waterfall'; +import { WaterfallMetadataEntry } from '../../waterfall/types'; +import { OnFlyoutClose } from '../../waterfall/components/use_flyout'; +import { METRIC_TYPE, useUiTracker } from '../../../../../../../observability/public'; + +export const DETAILS = i18n.translate('xpack.uptime.synthetics.waterfall.flyout.details', { + defaultMessage: 'Details', +}); + +export const CERTIFICATES = i18n.translate( + 'xpack.uptime.synthetics.waterfall.flyout.certificates', + { + defaultMessage: 'Certificate headers', + } +); + +export const REQUEST_HEADERS = i18n.translate( + 'xpack.uptime.synthetics.waterfall.flyout.requestHeaders', + { + defaultMessage: 'Request headers', + } +); + +export const RESPONSE_HEADERS = i18n.translate( + 'xpack.uptime.synthetics.waterfall.flyout.responseHeaders', + { + defaultMessage: 'Response headers', + } +); + +const FlyoutContainer = styled(EuiFlyout)` + z-index: ${(props) => props.theme.eui.euiZLevel5}; +`; + +export interface WaterfallFlyoutProps { + flyoutData?: WaterfallMetadataEntry; + onFlyoutClose: OnFlyoutClose; + isFlyoutVisible?: boolean; +} + +export const WaterfallFlyout = ({ + flyoutData, + isFlyoutVisible, + onFlyoutClose, +}: WaterfallFlyoutProps) => { + const flyoutRef = useRef(null); + const trackMetric = useUiTracker({ app: 'uptime' }); + + useEffect(() => { + if (isFlyoutVisible && flyoutData && flyoutRef.current) { + flyoutRef.current?.focus(); + } + }, [flyoutData, isFlyoutVisible, flyoutRef]); + + if (!flyoutData || !isFlyoutVisible) { + return null; + } + + const { url, details, certificates, requestHeaders, responseHeaders } = flyoutData; + + trackMetric({ metric: 'waterfall_flyout', metricType: METRIC_TYPE.CLICK }); + + return ( +
+ + + +

+ + + +

+
+
+ + + {!!requestHeaders && ( + <> + +
+ + )} + {!!responseHeaders && ( + <> + +
+ + )} + {!!certificates && ( + <> + +
+ + )} + + + + ); +}; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_sidebar_item.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_sidebar_item.tsx index 25b577ef9403a..f9d56422ba75c 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_sidebar_item.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfall_sidebar_item.tsx @@ -5,20 +5,35 @@ * 2.0. */ -import React from 'react'; +import React, { RefObject, useMemo, useCallback, useState } from 'react'; import { EuiFlexGroup, EuiFlexItem, EuiBadge } from '@elastic/eui'; import { SidebarItem } from '../waterfall/types'; import { MiddleTruncatedText } from '../../waterfall'; import { SideBarItemHighlighter } from '../../waterfall/components/styles'; import { SIDEBAR_FILTER_MATCHES_SCREENREADER_LABEL } from '../../waterfall/components/translations'; +import { OnSidebarClick } from '../../waterfall/components/use_flyout'; interface SidebarItemProps { item: SidebarItem; renderFilterScreenReaderText?: boolean; + onClick?: OnSidebarClick; } -export const WaterfallSidebarItem = ({ item, renderFilterScreenReaderText }: SidebarItemProps) => { - const { status, offsetIndex, isHighlighted } = item; +export const WaterfallSidebarItem = ({ + item, + renderFilterScreenReaderText, + onClick, +}: SidebarItemProps) => { + const [buttonRef, setButtonRef] = useState>(); + const { status, offsetIndex, index, isHighlighted, url } = item; + + const handleSidebarClick = useMemo(() => { + if (onClick) { + return () => onClick({ buttonRef, networkItemIndex: index }); + } + }, [buttonRef, index, onClick]); + + const setRef = useCallback((ref) => setButtonRef(ref), [setButtonRef]); const isErrorStatusCode = (statusCode: number) => { const is400 = statusCode >= 400 && statusCode <= 499; @@ -40,11 +55,23 @@ export const WaterfallSidebarItem = ({ item, renderFilterScreenReaderText }: Sid data-test-subj={isHighlighted ? 'sideBarHighlightedItem' : 'sideBarDimmedItem'} > {!status || !isErrorStatusCode(status) ? ( - + ) : ( - - - + + + {status} diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfalll_sidebar_item.test.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfalll_sidebar_item.test.tsx index 578d66a1ea3f1..7f32cac92bd9f 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfalll_sidebar_item.test.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/step_detail/waterfall/waterfalll_sidebar_item.test.tsx @@ -6,20 +6,22 @@ */ import React from 'react'; -import { SidebarItem } from '../waterfall/types'; +import 'jest-canvas-mock'; +import { fireEvent } from '@testing-library/react'; +import { SidebarItem } from '../waterfall/types'; import { render } from '../../../../../lib/helper/rtl_helpers'; - -import 'jest-canvas-mock'; import { WaterfallSidebarItem } from './waterfall_sidebar_item'; import { SIDEBAR_FILTER_MATCHES_SCREENREADER_LABEL } from '../../waterfall/components/translations'; describe('waterfall filter', () => { const url = 'http://www.elastic.co'; - const offsetIndex = 1; + const index = 0; + const offsetIndex = index + 1; const item: SidebarItem = { url, isHighlighted: true, + index, offsetIndex, }; @@ -40,12 +42,14 @@ describe('waterfall filter', () => { }); it('does not render screen reader text when renderFilterScreenReaderText is false', () => { - const { queryByLabelText } = render( - + const onClick = jest.fn(); + const { getByRole } = render( + ); + const button = getByRole('button'); + fireEvent.click(button); - expect( - queryByLabelText(`${SIDEBAR_FILTER_MATCHES_SCREENREADER_LABEL} ${offsetIndex}. ${url}`) - ).not.toBeInTheDocument(); + expect(button).toBeInTheDocument(); + expect(onClick).toBeCalled(); }); }); diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.test.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.test.tsx index d6c1d777a40a7..de352186e26fd 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.test.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.test.tsx @@ -6,7 +6,7 @@ */ import { getChunks, MiddleTruncatedText } from './middle_truncated_text'; -import { render, within } from '@testing-library/react'; +import { render, within, fireEvent, waitFor } from '@testing-library/react'; import React from 'react'; const longString = @@ -25,9 +25,10 @@ describe('getChunks', () => { }); describe('Component', () => { + const url = 'http://www.elastic.co'; it('renders truncated text and aria label', () => { const { getByText, getByLabelText } = render( - + ); expect(getByText(first)).toBeInTheDocument(); @@ -38,11 +39,39 @@ describe('Component', () => { it('renders screen reader only text', () => { const { getByTestId } = render( - + ); const { getByText } = within(getByTestId('middleTruncatedTextSROnly')); expect(getByText(longString)).toBeInTheDocument(); }); + + it('renders external link', () => { + const { getByText } = render( + + ); + const link = getByText('Open resource in new tab').closest('a'); + + expect(link).toHaveAttribute('href', url); + expect(link).toHaveAttribute('target', '_blank'); + }); + + it('renders a button when onClick function is passed', async () => { + const handleClick = jest.fn(); + const { getByTestId } = render( + + ); + const button = getByTestId('middleTruncatedTextButton'); + fireEvent.click(button); + + await waitFor(() => { + expect(handleClick).toBeCalled(); + }); + }); }); diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.tsx index ec363ed2b40a4..a0993d54bbd07 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/middle_truncated_text.tsx @@ -7,41 +7,57 @@ import React, { useMemo } from 'react'; import styled from 'styled-components'; -import { EuiScreenReaderOnly, EuiToolTip } from '@elastic/eui'; +import { FormattedMessage } from '@kbn/i18n/react'; +import { EuiScreenReaderOnly, EuiToolTip, EuiButtonEmpty, EuiLink } from '@elastic/eui'; import { FIXED_AXIS_HEIGHT } from './constants'; interface Props { ariaLabel: string; text: string; + onClick?: (event: React.MouseEvent) => void; + setButtonRef?: (ref: HTMLButtonElement | HTMLAnchorElement | null) => void; + url: string; } -const OuterContainer = styled.div` - width: 100%; - height: 100%; +const OuterContainer = styled.span` position: relative; -`; + display: inline-flex; + align-items: center; + .euiToolTipAnchor { + min-width: 0; + } +`; // NOTE: min-width: 0 ensures flexbox and no-wrap children can co-exist const InnerContainer = styled.span` - position: absolute; - top: 0; - bottom: 0; - left: 0; - right: 0; overflow: hidden; display: flex; - min-width: 0; -`; // NOTE: min-width: 0 ensures flexbox and no-wrap children can co-exist + align-items: center; +`; const FirstChunk = styled.span` text-overflow: ellipsis; white-space: nowrap; overflow: hidden; line-height: ${FIXED_AXIS_HEIGHT}px; -`; + text-align: left; +`; // safari doesn't auto align text left in some cases const LastChunk = styled.span` flex-shrink: 0; line-height: ${FIXED_AXIS_HEIGHT}px; + text-align: left; +`; // safari doesn't auto align text left in some cases + +const StyledButton = styled(EuiButtonEmpty)` + &&& { + height: auto; + border: none; + + .euiButtonContent { + display: inline-block; + padding: 0; + } + } `; export const getChunks = (text: string) => { @@ -55,24 +71,49 @@ export const getChunks = (text: string) => { // Helper component for adding middle text truncation, e.g. // really-really-really-long....ompressed.js // Can be used to accomodate content in sidebar item rendering. -export const MiddleTruncatedText = ({ ariaLabel, text }: Props) => { +export const MiddleTruncatedText = ({ ariaLabel, text, onClick, setButtonRef, url }: Props) => { const chunks = useMemo(() => { return getChunks(text); }, [text]); return ( - <> - - - {text} - - - - {chunks.first} - {chunks.last} - - - - + + + {text} + + + <> + {onClick ? ( + + + {chunks.first} + {chunks.last} + + + ) : ( + + {chunks.first} + {chunks.last} + + )} + + + + + + + + + + + + ); }; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/sidebar.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/sidebar.tsx index 86ab4488cca93..0e57a210f032a 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/sidebar.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/sidebar.tsx @@ -5,15 +5,15 @@ * 2.0. */ -import React from 'react'; -import { EuiFlexItem } from '@elastic/eui'; +import React, { useMemo } from 'react'; import { FIXED_AXIS_HEIGHT, SIDEBAR_GROW_SIZE } from './constants'; -import { IWaterfallContext } from '../context/waterfall_chart'; +import { IWaterfallContext, useWaterfallContext } from '../context/waterfall_chart'; import { WaterfallChartSidebarContainer, WaterfallChartSidebarContainerInnerPanel, WaterfallChartSidebarContainerFlexGroup, WaterfallChartSidebarFlexItem, + WaterfallChartSidebarWrapper, } from './styles'; import { WaterfallChartProps } from './waterfall_chart'; @@ -23,8 +23,11 @@ interface SidebarProps { } export const Sidebar: React.FC = ({ items, render }) => { + const { onSidebarClick } = useWaterfallContext(); + const handleSidebarClick = useMemo(() => onSidebarClick, [onSidebarClick]); + return ( - + = ({ items, render }) => { gutterSize="none" responsive={false} > - {items.map((item) => ( - - {render(item)} - - ))} + {items.map((item, index) => { + return ( + + {render(item, index, handleSidebarClick)} + + ); + })} - + ); }; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/styles.ts b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/styles.ts index 9177902f8a613..c0a75e0e09b22 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/styles.ts +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/styles.ts @@ -5,12 +5,12 @@ * 2.0. */ -import { EuiPanel, EuiFlexGroup, EuiFlexItem, EuiText, EuiPanelProps } from '@elastic/eui'; -import { rgba } from 'polished'; import { FunctionComponent } from 'react'; import { StyledComponent } from 'styled-components'; +import { EuiPanel, EuiFlexGroup, EuiFlexItem, EuiText, EuiPanelProps } from '@elastic/eui'; +import { rgba } from 'polished'; +import { FIXED_AXIS_HEIGHT, SIDEBAR_GROW_SIZE } from './constants'; import { euiStyled, EuiTheme } from '../../../../../../../../../src/plugins/kibana_react/common'; -import { FIXED_AXIS_HEIGHT } from './constants'; interface WaterfallChartOuterContainerProps { height?: string; @@ -82,6 +82,11 @@ interface WaterfallChartSidebarContainer { height: number; } +export const WaterfallChartSidebarWrapper = euiStyled(EuiFlexItem)` + max-width: ${SIDEBAR_GROW_SIZE * 10}%; + z-index: ${(props) => props.theme.eui.euiZLevel5}; +`; + export const WaterfallChartSidebarContainer = euiStyled.div` height: ${(props) => `${props.height}px`}; overflow-y: hidden; @@ -104,10 +109,10 @@ export const WaterfallChartSidebarFlexItem = euiStyled(EuiFlexItem)` min-width: 0; padding-left: ${(props) => props.theme.eui.paddingSizes.m}; padding-right: ${(props) => props.theme.eui.paddingSizes.m}; - z-index: ${(props) => props.theme.eui.euiZLevel4}; + justify-content: space-around; `; -export const SideBarItemHighlighter = euiStyled.span<{ isHighlighted: boolean }>` +export const SideBarItemHighlighter = euiStyled(EuiFlexItem)<{ isHighlighted: boolean }>` opacity: ${(props) => (props.isHighlighted ? 1 : 0.4)}; height: 100%; `; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/use_flyout.test.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/use_flyout.test.tsx new file mode 100644 index 0000000000000..5b388874d508e --- /dev/null +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/use_flyout.test.tsx @@ -0,0 +1,91 @@ +/* + * 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 { renderHook, act } from '@testing-library/react-hooks'; +import { useFlyout } from './use_flyout'; +import { IWaterfallContext } from '../context/waterfall_chart'; + +import { ProjectedValues, XYChartElementEvent } from '@elastic/charts'; + +describe('useFlyoutHook', () => { + const metadata: IWaterfallContext['metadata'] = [ + { + x: 0, + url: 'http://elastic.co', + requestHeaders: undefined, + responseHeaders: undefined, + certificates: undefined, + details: [ + { + name: 'Content type', + value: 'text/html', + }, + ], + }, + ]; + + it('sets isFlyoutVisible to true and sets flyoutData when calling onSidebarClick', () => { + const index = 0; + const { result } = renderHook((props) => useFlyout(props.metadata), { + initialProps: { metadata }, + }); + + expect(result.current.isFlyoutVisible).toBe(false); + + act(() => { + result.current.onSidebarClick({ buttonRef: { current: null }, networkItemIndex: index }); + }); + + expect(result.current.isFlyoutVisible).toBe(true); + expect(result.current.flyoutData).toEqual(metadata[index]); + }); + + it('sets isFlyoutVisible to true and sets flyoutData when calling onBarClick', () => { + const index = 0; + const elementData = [ + { + datum: { + config: { + id: index, + }, + }, + }, + {}, + ]; + + const { result } = renderHook((props) => useFlyout(props.metadata), { + initialProps: { metadata }, + }); + + expect(result.current.isFlyoutVisible).toBe(false); + + act(() => { + result.current.onBarClick([elementData as XYChartElementEvent]); + }); + + expect(result.current.isFlyoutVisible).toBe(true); + expect(result.current.flyoutData).toEqual(metadata[0]); + }); + + it('sets isFlyoutVisible to true and sets flyoutData when calling onProjectionClick', () => { + const index = 0; + const geometry = { x: index }; + + const { result } = renderHook((props) => useFlyout(props.metadata), { + initialProps: { metadata }, + }); + + expect(result.current.isFlyoutVisible).toBe(false); + + act(() => { + result.current.onProjectionClick(geometry as ProjectedValues); + }); + + expect(result.current.isFlyoutVisible).toBe(true); + expect(result.current.flyoutData).toEqual(metadata[0]); + }); +}); diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/use_flyout.ts b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/use_flyout.ts new file mode 100644 index 0000000000000..206fc588c3053 --- /dev/null +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/use_flyout.ts @@ -0,0 +1,93 @@ +/* + * 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 { RefObject, useCallback, useState } from 'react'; + +import { + ElementClickListener, + ProjectionClickListener, + ProjectedValues, + XYChartElementEvent, +} from '@elastic/charts'; + +import { WaterfallMetadata, WaterfallMetadataEntry } from '../types'; + +interface OnSidebarClickParams { + buttonRef?: ButtonRef; + networkItemIndex: number; +} + +export type ButtonRef = RefObject; +export type OnSidebarClick = (params: OnSidebarClickParams) => void; +export type OnProjectionClick = ProjectionClickListener; +export type OnElementClick = ElementClickListener; +export type OnFlyoutClose = () => void; + +export const useFlyout = (metadata: WaterfallMetadata) => { + const [isFlyoutVisible, setIsFlyoutVisible] = useState(false); + const [flyoutData, setFlyoutData] = useState(undefined); + const [currentSidebarItemRef, setCurrentSidebarItemRef] = useState< + RefObject + >(); + + const handleFlyout = useCallback( + (flyoutEntry: WaterfallMetadataEntry) => { + setFlyoutData(flyoutEntry); + setIsFlyoutVisible(true); + }, + [setIsFlyoutVisible, setFlyoutData] + ); + + const onFlyoutClose = useCallback(() => { + setIsFlyoutVisible(false); + currentSidebarItemRef?.current?.focus(); + }, [currentSidebarItemRef, setIsFlyoutVisible]); + + const onBarClick: ElementClickListener = useCallback( + ([elementData]) => { + setIsFlyoutVisible(false); + const { datum } = (elementData as XYChartElementEvent)[0]; + const metadataEntry = metadata[datum.config.id]; + handleFlyout(metadataEntry); + }, + [metadata, handleFlyout] + ); + + const onProjectionClick: ProjectionClickListener = useCallback( + (projectionData) => { + setIsFlyoutVisible(false); + const { x } = projectionData as ProjectedValues; + if (typeof x === 'number' && x >= 0) { + const metadataEntry = metadata[x]; + handleFlyout(metadataEntry); + } + }, + [metadata, handleFlyout] + ); + + const onSidebarClick: OnSidebarClick = useCallback( + ({ buttonRef, networkItemIndex }) => { + if (isFlyoutVisible && buttonRef === currentSidebarItemRef) { + setIsFlyoutVisible(false); + } else { + const metadataEntry = metadata[networkItemIndex]; + setCurrentSidebarItemRef(buttonRef); + handleFlyout(metadataEntry); + } + }, + [currentSidebarItemRef, handleFlyout, isFlyoutVisible, metadata, setIsFlyoutVisible] + ); + + return { + flyoutData, + onBarClick, + onProjectionClick, + onSidebarClick, + isFlyoutVisible, + onFlyoutClose, + }; +}; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_bar_chart.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_bar_chart.tsx index df00df147fc6c..19a828aa097b6 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_bar_chart.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_bar_chart.tsx @@ -5,7 +5,7 @@ * 2.0. */ -import React from 'react'; +import React, { useMemo, useCallback } from 'react'; import { Axis, BarSeries, @@ -67,6 +67,10 @@ export const WaterfallBarChart = ({ index, }: Props) => { const theme = useChartTheme(); + const { onElementClick, onProjectionClick } = useWaterfallContext(); + const handleElementClick = useMemo(() => onElementClick, [onElementClick]); + const handleProjectionClick = useMemo(() => onProjectionClick, [onProjectionClick]); + const memoizedTickFormat = useCallback(tickFormat, [tickFormat]); return ( = (item: I, index?: number) => JSX.Element; -export type RenderFilter = () => JSX.Element; +export type RenderItem = ( + item: I, + index: number, + onClick?: (event: any) => void +) => JSX.Element; +export type RenderElement = () => JSX.Element; export interface WaterfallChartProps { tickFormat: TickFormatter; @@ -36,7 +40,8 @@ export interface WaterfallChartProps { barStyleAccessor: BarStyleAccessor; renderSidebarItem?: RenderItem; renderLegendItem?: RenderItem; - renderFilter?: RenderFilter; + renderFilter?: RenderElement; + renderFlyout?: RenderElement; maxHeight?: string; fullHeight?: boolean; } @@ -48,6 +53,7 @@ export const WaterfallChart = ({ renderSidebarItem, renderLegendItem, renderFilter, + renderFlyout, maxHeight = '800px', fullHeight = false, }: WaterfallChartProps) => { @@ -82,7 +88,7 @@ export const WaterfallChart = ({ {shouldRenderSidebar && ( - + {renderFilter()} )} - + )} {shouldRenderLegend && } + {renderFlyout && renderFlyout()} ); }; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_flyout_table.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_flyout_table.tsx new file mode 100644 index 0000000000000..8f723eb92fd94 --- /dev/null +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/components/waterfall_flyout_table.tsx @@ -0,0 +1,78 @@ +/* + * 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 React, { useMemo } from 'react'; +import styled from 'styled-components'; + +import { EuiText, EuiBasicTable, EuiSpacer } from '@elastic/eui'; + +interface Row { + name: string; + value?: string; +} + +interface Props { + rows: Row[]; + title: string; +} + +const StyledText = styled(EuiText)` + width: 100%; +`; + +class TableWithoutHeader extends EuiBasicTable { + renderTableHead() { + return <>; + } +} + +export const Table = (props: Props) => { + const { rows, title } = props; + const columns = useMemo( + () => [ + { + field: 'name', + name: '', + sortable: false, + render: (_name: string, item: Row) => ( + + {item.name} + + ), + }, + { + field: 'value', + name: '', + sortable: false, + render: (_name: string, item: Row) => { + return ( + + {item.value ?? '--'} + + ); + }, + }, + ], + [] + ); + + return ( + <> + +

{title}

+
+ + + + ); +}; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/context/waterfall_chart.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/context/waterfall_chart.tsx index 9e87d69ce38a8..b960491162010 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/context/waterfall_chart.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/context/waterfall_chart.tsx @@ -6,7 +6,8 @@ */ import React, { createContext, useContext, Context } from 'react'; -import { WaterfallData, WaterfallDataEntry } from '../types'; +import { WaterfallData, WaterfallDataEntry, WaterfallMetadata } from '../types'; +import { OnSidebarClick, OnElementClick, OnProjectionClick } from '../components/use_flyout'; import { SidebarItems } from '../../step_detail/waterfall/types'; export interface IWaterfallContext { @@ -14,9 +15,13 @@ export interface IWaterfallContext { highlightedNetworkRequests: number; fetchedNetworkRequests: number; data: WaterfallData; + onElementClick?: OnElementClick; + onProjectionClick?: OnProjectionClick; + onSidebarClick?: OnSidebarClick; showOnlyHighlightedNetworkRequests: boolean; sidebarItems?: SidebarItems; legendItems?: unknown[]; + metadata: WaterfallMetadata; renderTooltipItem: ( item: WaterfallDataEntry['config']['tooltipProps'], index?: number @@ -30,18 +35,26 @@ interface ProviderProps { highlightedNetworkRequests: number; fetchedNetworkRequests: number; data: IWaterfallContext['data']; + onElementClick?: IWaterfallContext['onElementClick']; + onProjectionClick?: IWaterfallContext['onProjectionClick']; + onSidebarClick?: IWaterfallContext['onSidebarClick']; showOnlyHighlightedNetworkRequests: IWaterfallContext['showOnlyHighlightedNetworkRequests']; sidebarItems?: IWaterfallContext['sidebarItems']; legendItems?: IWaterfallContext['legendItems']; + metadata: IWaterfallContext['metadata']; renderTooltipItem: IWaterfallContext['renderTooltipItem']; } export const WaterfallProvider: React.FC = ({ children, data, + onElementClick, + onProjectionClick, + onSidebarClick, showOnlyHighlightedNetworkRequests, sidebarItems, legendItems, + metadata, renderTooltipItem, totalNetworkRequests, highlightedNetworkRequests, @@ -54,6 +67,10 @@ export const WaterfallProvider: React.FC = ({ showOnlyHighlightedNetworkRequests, sidebarItems, legendItems, + metadata, + onElementClick, + onProjectionClick, + onSidebarClick, renderTooltipItem, totalNetworkRequests, highlightedNetworkRequests, diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/index.tsx b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/index.tsx index 5a6daa30450d1..0de1b50ecce8f 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/index.tsx +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/index.tsx @@ -8,4 +8,10 @@ export { WaterfallChart, RenderItem, WaterfallChartProps } from './components/waterfall_chart'; export { WaterfallProvider, useWaterfallContext } from './context/waterfall_chart'; export { MiddleTruncatedText } from './components/middle_truncated_text'; -export { WaterfallData, WaterfallDataEntry } from './types'; +export { useFlyout } from './components/use_flyout'; +export { + WaterfallData, + WaterfallDataEntry, + WaterfallMetadata, + WaterfallMetadataEntry, +} from './types'; diff --git a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/types.ts b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/types.ts index 6cffc3a2df382..f1775a6fd1892 100644 --- a/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/types.ts +++ b/x-pack/plugins/uptime/public/components/monitor/synthetics/waterfall/types.ts @@ -16,8 +16,26 @@ export interface WaterfallDataSeriesConfigProperties { showTooltip: boolean; } +export interface WaterfallMetadataItem { + name: string; + value?: string; +} + +export interface WaterfallMetadataEntry { + x: number; + url: string; + requestHeaders?: WaterfallMetadataItem[]; + responseHeaders?: WaterfallMetadataItem[]; + certificates?: WaterfallMetadataItem[]; + details: WaterfallMetadataItem[]; +} + export type WaterfallDataEntry = PlotProperties & { config: WaterfallDataSeriesConfigProperties & Record; }; +export type WaterfallMetadata = WaterfallMetadataEntry[]; + export type WaterfallData = WaterfallDataEntry[]; + +export type RenderItem = (item: I, index: number) => JSX.Element; diff --git a/x-pack/plugins/uptime/public/state/api/alert_actions.ts b/x-pack/plugins/uptime/public/state/api/alert_actions.ts index 3fce0499c4501..17b3354b666c4 100644 --- a/x-pack/plugins/uptime/public/state/api/alert_actions.ts +++ b/x-pack/plugins/uptime/public/state/api/alert_actions.ts @@ -119,6 +119,8 @@ function getServiceNowActionParams(): ServiceNowActionParams { impact: '2', severity: '2', urgency: '2', + category: null, + subcategory: null, externalId: null, }, comments: [], diff --git a/x-pack/plugins/uptime/server/lib/requests/get_network_events.test.ts b/x-pack/plugins/uptime/server/lib/requests/get_network_events.test.ts index 29faa9f7ba9e8..159ab76e74c58 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_network_events.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_network_events.test.ts @@ -239,11 +239,43 @@ describe('getNetworkEvents', () => { Object { "events": Array [ Object { + "bytesDownloadedCompressed": 337, + "certificates": Object { + "issuer": "DigiCert TLS RSA SHA256 2020 CA1", + "subjectName": "syndication.twitter.com", + "validFrom": 1606694400000, + "validTo": 1638230399000, + }, + "ip": "104.244.42.200", "loadEndTime": 3287298.251, "method": "GET", "mimeType": "image/gif", + "requestHeaders": Object { + "referer": "www.test.com", + "user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/88.0.4324.0 Safari/537.36", + }, "requestSentTime": 3287154.973, "requestStartTime": 3287155.502, + "responseHeaders": Object { + "cache_control": "no-cache, no-store, must-revalidate, pre-check=0, post-check=0", + "content_encoding": "gzip", + "content_length": "65", + "content_type": "image/gif;charset=utf-8", + "date": "Mon, 14 Dec 2020 10:46:39 GMT", + "expires": "Tue, 31 Mar 1981 05:00:00 GMT", + "last_modified": "Mon, 14 Dec 2020 10:46:39 GMT", + "pragma": "no-cache", + "server": "tsa_f", + "status": "200 OK", + "strict_transport_security": "max-age=631138519", + "x_connection_hash": "cb6fe99b8676f4e4b827cc3e6512c90d", + "x_content_type_options": "nosniff", + "x_frame_options": "SAMEORIGIN", + "x_response_time": "108", + "x_transaction": "008fff3d00a1e64c", + "x_twitter_response_tags": "BouncerCompliant", + "x_xss_protection": "0", + }, "status": 200, "timestamp": "2020-12-14T10:46:39.183Z", "timings": Object { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts b/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts index fa76da0025305..970af80576cad 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts @@ -50,6 +50,7 @@ export const getNetworkEvents: UMElasticsearchQueryFn< event._source.synthetics.payload.response.timing ? secondsToMillis(event._source.synthetics.payload.response.timing.request_time) : undefined; + const securityDetails = event._source.synthetics.payload.response?.security_details; return { timestamp: event._source['@timestamp'], @@ -61,6 +62,22 @@ export const getNetworkEvents: UMElasticsearchQueryFn< requestStartTime, loadEndTime, timings: event._source.synthetics.payload.timings, + bytesDownloadedCompressed: event._source.synthetics.payload.response?.encoded_data_length, + certificates: securityDetails + ? { + issuer: securityDetails.issuer, + subjectName: securityDetails.subject_name, + validFrom: securityDetails.valid_from + ? secondsToMillis(securityDetails.valid_from) + : undefined, + validTo: securityDetails.valid_to + ? secondsToMillis(securityDetails.valid_to) + : undefined, + } + : undefined, + requestHeaders: event._source.synthetics.payload.request?.headers, + responseHeaders: event._source.synthetics.payload.response?.headers, + ip: event._source.synthetics.payload.response?.remote_i_p_address, }; }), }; diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/aad/kibana.json b/x-pack/test/alerting_api_integration/common/fixtures/plugins/aad/kibana.json index 9a7bedbb5c6d5..6a43c7c74ad8c 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/aad/kibana.json +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/aad/kibana.json @@ -1,5 +1,5 @@ { - "id": "aad-fixtures", + "id": "aadFixtures", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/kibana.json b/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/kibana.json index 5f92b9e5479e8..f63d6ef0d45ac 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/kibana.json +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/kibana.json @@ -1,5 +1,5 @@ { - "id": "actions_simulators", + "id": "actionsSimulators", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/server/servicenow_simulation.ts b/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/server/servicenow_simulation.ts index e2cbd3628d5fa..8b8eb46989787 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/server/servicenow_simulation.ts +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/actions_simulators/server/servicenow_simulation.ts @@ -30,6 +30,8 @@ export function initPlugin(router: IRouter, path: string) { severity: schema.string({ defaultValue: '1' }), urgency: schema.string({ defaultValue: '1' }), impact: schema.string({ defaultValue: '1' }), + category: schema.maybe(schema.string()), + subcategory: schema.maybe(schema.string()), }), }, }, diff --git a/x-pack/test/alerting_api_integration/common/fixtures/plugins/task_manager_fixture/kibana.json b/x-pack/test/alerting_api_integration/common/fixtures/plugins/task_manager_fixture/kibana.json index 8f606276998f5..2f8117163471d 100644 --- a/x-pack/test/alerting_api_integration/common/fixtures/plugins/task_manager_fixture/kibana.json +++ b/x-pack/test/alerting_api_integration/common/fixtures/plugins/task_manager_fixture/kibana.json @@ -1,5 +1,5 @@ { - "id": "task_manager_fixture", + "id": "taskManagerFixture", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts index 2d49c409a18fc..2d584f764e5e4 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts @@ -40,6 +40,8 @@ export default function servicenowTest({ getService }: FtrProviderContext) { severity: '1', short_description: 'a title', urgency: '1', + category: 'software', + subcategory: 'software', }, comments: [ { diff --git a/x-pack/test/api_integration/apis/ml/modules/setup_module.ts b/x-pack/test/api_integration/apis/ml/modules/setup_module.ts index 8064d498774a3..c4b3a4ed0adcf 100644 --- a/x-pack/test/api_integration/apis/ml/modules/setup_module.ts +++ b/x-pack/test/api_integration/apis/ml/modules/setup_module.ts @@ -713,7 +713,8 @@ export default ({ getService }: FtrProviderContext) => { return successObjects; } - describe('module setup', function () { + // blocks ES snapshot promotion: https://github.com/elastic/kibana/issues/91224 + describe.skip('module setup', function () { before(async () => { await ml.testResources.setKibanaTimeZoneToUTC(); }); diff --git a/x-pack/test/api_integration/apis/search/session.ts b/x-pack/test/api_integration/apis/search/session.ts index 984f3e3f7dd4e..e7834ed3d8641 100644 --- a/x-pack/test/api_integration/apis/search/session.ts +++ b/x-pack/test/api_integration/apis/search/session.ts @@ -11,6 +11,7 @@ import { SearchSessionStatus } from '../../../../plugins/data_enhanced/common'; export default function ({ getService }: FtrProviderContext) { const supertest = getService('supertest'); + const retry = getService('retry'); describe('search session', () => { describe('session management', () => { @@ -152,20 +153,23 @@ export default function ({ getService }: FtrProviderContext) { const { id: id2 } = searchRes2.body; - const resp = await supertest - .get(`/internal/session/${sessionId}`) - .set('kbn-xsrf', 'foo') - .expect(200); - - const { name, touched, created, persisted, idMapping } = resp.body.attributes; - expect(persisted).to.be(true); - expect(name).to.be('My Session'); - expect(touched).not.to.be(undefined); - expect(created).not.to.be(undefined); - - const idMappings = Object.values(idMapping).map((value: any) => value.id); - expect(idMappings).to.contain(id1); - expect(idMappings).to.contain(id2); + await retry.waitForWithTimeout('searches persisted into session', 5000, async () => { + const resp = await supertest + .get(`/internal/session/${sessionId}`) + .set('kbn-xsrf', 'foo') + .expect(200); + + const { name, touched, created, persisted, idMapping } = resp.body.attributes; + expect(persisted).to.be(true); + expect(name).to.be('My Session'); + expect(touched).not.to.be(undefined); + expect(created).not.to.be(undefined); + + const idMappings = Object.values(idMapping).map((value: any) => value.id); + expect(idMappings).to.contain(id1); + expect(idMappings).to.contain(id2); + return true; + }); }); it('should create and extend a session', async () => { @@ -245,21 +249,24 @@ export default function ({ getService }: FtrProviderContext) { const { id: id2 } = searchRes2.body; - const resp = await supertest - .get(`/internal/session/${sessionId}`) - .set('kbn-xsrf', 'foo') - .expect(200); + await retry.waitForWithTimeout('searches persisted into session', 5000, async () => { + const resp = await supertest + .get(`/internal/session/${sessionId}`) + .set('kbn-xsrf', 'foo') + .expect(200); - const { appId, name, touched, created, persisted, idMapping } = resp.body.attributes; - expect(persisted).to.be(false); - expect(name).to.be(undefined); - expect(appId).to.be(undefined); - expect(touched).not.to.be(undefined); - expect(created).not.to.be(undefined); + const { appId, name, touched, created, persisted, idMapping } = resp.body.attributes; + expect(persisted).to.be(false); + expect(name).to.be(undefined); + expect(appId).to.be(undefined); + expect(touched).not.to.be(undefined); + expect(created).not.to.be(undefined); - const idMappings = Object.values(idMapping).map((value: any) => value.id); - expect(idMappings).to.contain(id1); - expect(idMappings).to.contain(id2); + const idMappings = Object.values(idMapping).map((value: any) => value.id); + expect(idMappings).to.contain(id1); + expect(idMappings).to.contain(id2); + return true; + }); }); it('touched time updates when you poll on an search', async () => { @@ -287,7 +294,7 @@ export default function ({ getService }: FtrProviderContext) { const { id: id1 } = searchRes1.body; // it might take the session a moment to be created - await new Promise((resolve) => setTimeout(resolve, 1000)); + await new Promise((resolve) => setTimeout(resolve, 2500)); const getSessionFirstTime = await supertest .get(`/internal/session/${sessionId}`) @@ -303,6 +310,9 @@ export default function ({ getService }: FtrProviderContext) { }) .expect(200); + // it might take the session a moment to be updated + await new Promise((resolve) => setTimeout(resolve, 2500)); + const getSessionSecondTime = await supertest .get(`/internal/session/${sessionId}`) .set('kbn-xsrf', 'foo') diff --git a/x-pack/test/case_api_integration/basic/tests/cases/push_case.ts b/x-pack/test/case_api_integration/basic/tests/cases/push_case.ts index ef7c57b3b4749..735c079c7b850 100644 --- a/x-pack/test/case_api_integration/basic/tests/cases/push_case.ts +++ b/x-pack/test/case_api_integration/basic/tests/cases/push_case.ts @@ -80,7 +80,13 @@ export default ({ getService }: FtrProviderContext): void => { id: connector.id, name: connector.name, type: connector.actionTypeId, - fields: { urgency: '2', impact: '2', severity: '2' }, + fields: { + urgency: '2', + impact: '2', + severity: '2', + category: 'software', + subcategory: 'os', + }, }).connector, }) .expect(200); @@ -143,7 +149,13 @@ export default ({ getService }: FtrProviderContext): void => { id: connector.id, name: connector.name, type: connector.actionTypeId, - fields: { urgency: '2', impact: '2', severity: '2' }, + fields: { + urgency: '2', + impact: '2', + severity: '2', + category: 'software', + subcategory: 'os', + }, }).connector, }) .expect(200); @@ -196,7 +208,13 @@ export default ({ getService }: FtrProviderContext): void => { id: connector.id, name: connector.name, type: connector.actionTypeId, - fields: { urgency: '2', impact: '2', severity: '2' }, + fields: { + urgency: '2', + impact: '2', + severity: '2', + category: 'software', + subcategory: 'os', + }, }).connector, }) .expect(200); @@ -268,7 +286,13 @@ export default ({ getService }: FtrProviderContext): void => { id: connector.id, name: connector.name, type: connector.actionTypeId, - fields: { urgency: '2', impact: '2', severity: '2' }, + fields: { + urgency: '2', + impact: '2', + severity: '2', + category: 'software', + subcategory: 'os', + }, }).connector, }) .expect(200); diff --git a/x-pack/test/case_api_integration/basic/tests/cases/user_actions/get_all_user_actions.ts b/x-pack/test/case_api_integration/basic/tests/cases/user_actions/get_all_user_actions.ts index d83d87da1e7af..1cbf79cb3326c 100644 --- a/x-pack/test/case_api_integration/basic/tests/cases/user_actions/get_all_user_actions.ts +++ b/x-pack/test/case_api_integration/basic/tests/cases/user_actions/get_all_user_actions.ts @@ -359,7 +359,13 @@ export default ({ getService }: FtrProviderContext): void => { id: connector.id, name: connector.name, type: connector.actionTypeId, - fields: { urgency: '2', impact: '2', severity: '2' }, + fields: { + urgency: '2', + impact: '2', + severity: '2', + category: 'software', + subcategory: 'os', + }, }).connector, }) .expect(200); diff --git a/x-pack/test/case_api_integration/basic/tests/connectors/case.ts b/x-pack/test/case_api_integration/basic/tests/connectors/case.ts index bb94c31c220d6..302c3a0423bed 100644 --- a/x-pack/test/case_api_integration/basic/tests/connectors/case.ts +++ b/x-pack/test/case_api_integration/basic/tests/connectors/case.ts @@ -476,6 +476,8 @@ export default ({ getService }: FtrProviderContext): void => { impact: null, severity: null, urgency: null, + category: null, + subcategory: null, }, }, created_by: { diff --git a/x-pack/test/fleet_api_integration/apis/agents_setup.ts b/x-pack/test/fleet_api_integration/apis/agents_setup.ts index c1abdfab566b9..20112afdf76a4 100644 --- a/x-pack/test/fleet_api_integration/apis/agents_setup.ts +++ b/x-pack/test/fleet_api_integration/apis/agents_setup.ts @@ -60,17 +60,8 @@ export default function (providerContext: FtrProviderContext) { cluster: ['monitor', 'manage_api_key'], indices: [ { - names: [ - 'logs-*', - 'metrics-*', - 'traces-*', - '.ds-logs-*', - '.ds-metrics-*', - '.ds-traces-*', - '.logs-endpoint.diagnostic.collection-*', - '.ds-.logs-endpoint.diagnostic.collection-*', - ], - privileges: ['write', 'create_index', 'indices:admin/auto_create'], + names: ['logs-*', 'metrics-*', 'traces-*', '.logs-endpoint.diagnostic.collection-*'], + privileges: ['auto_configure', 'create_doc'], allow_restricted_indices: false, }, ], diff --git a/x-pack/test/fleet_api_integration/apis/fleet_setup.ts b/x-pack/test/fleet_api_integration/apis/fleet_setup.ts index 2c15cddc81ea1..31d620cd34931 100644 --- a/x-pack/test/fleet_api_integration/apis/fleet_setup.ts +++ b/x-pack/test/fleet_api_integration/apis/fleet_setup.ts @@ -62,15 +62,8 @@ export default function (providerContext: FtrProviderContext) { cluster: ['monitor', 'manage_api_key'], indices: [ { - names: [ - 'logs-*', - 'metrics-*', - 'traces-*', - '.ds-logs-*', - '.ds-metrics-*', - '.ds-traces-*', - ], - privileges: ['write', 'create_index', 'indices:admin/auto_create'], + names: ['logs-*', 'metrics-*', 'traces-*'], + privileges: ['create_doc', 'indices:admin/auto_create'], allow_restricted_indices: false, }, ], @@ -101,17 +94,8 @@ export default function (providerContext: FtrProviderContext) { cluster: ['monitor', 'manage_api_key'], indices: [ { - names: [ - 'logs-*', - 'metrics-*', - 'traces-*', - '.ds-logs-*', - '.ds-metrics-*', - '.ds-traces-*', - '.logs-endpoint.diagnostic.collection-*', - '.ds-.logs-endpoint.diagnostic.collection-*', - ], - privileges: ['write', 'create_index', 'indices:admin/auto_create'], + names: ['logs-*', 'metrics-*', 'traces-*', '.logs-endpoint.diagnostic.collection-*'], + privileges: ['auto_configure', 'create_doc'], allow_restricted_indices: false, }, ], diff --git a/x-pack/test/fleet_api_integration/apis/index.js b/x-pack/test/fleet_api_integration/apis/index.js index 8c66db9c418ea..44431795a34ba 100644 --- a/x-pack/test/fleet_api_integration/apis/index.js +++ b/x-pack/test/fleet_api_integration/apis/index.js @@ -37,6 +37,7 @@ export default function ({ loadTestFile }) { loadTestFile(require.resolve('./package_policy/create')); loadTestFile(require.resolve('./package_policy/update')); loadTestFile(require.resolve('./package_policy/get')); + loadTestFile(require.resolve('./package_policy/delete')); // Agent policies loadTestFile(require.resolve('./agent_policy/index')); diff --git a/x-pack/test/fleet_api_integration/apis/package_policy/create.ts b/x-pack/test/fleet_api_integration/apis/package_policy/create.ts index 8e339bc78b087..c9c871e280f16 100644 --- a/x-pack/test/fleet_api_integration/apis/package_policy/create.ts +++ b/x-pack/test/fleet_api_integration/apis/package_policy/create.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import expect from '@kbn/expect'; import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { warnAndSkipTest } from '../../helpers'; @@ -39,6 +39,52 @@ export default function ({ getService }: FtrProviderContext) { .send({ agentPolicyId }); }); + it('should fail for managed agent policies', async function () { + if (server.enabled) { + // get a managed policy + const { + body: { item: managedPolicy }, + } = await supertest + .post(`/api/fleet/agent_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: `Managed policy from ${Date.now()}`, + namespace: 'default', + is_managed: true, + }); + + // try to add an integration to the managed policy + const { body } = await supertest + .post(`/api/fleet/package_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'filetest-1', + description: '', + namespace: 'default', + policy_id: managedPolicy.id, + enabled: true, + output_id: '', + inputs: [], + package: { + name: 'filetest', + title: 'For File Tests', + version: '0.1.0', + }, + }) + .expect(400); + + expect(body.statusCode).to.be(400); + expect(body.message).to.contain('Cannot add integrations to managed policy'); + + // delete policy we just made + await supertest.post(`/api/fleet/agent_policies/delete`).set('kbn-xsrf', 'xxxx').send({ + agentPolicyId: managedPolicy.id, + }); + } else { + warnAndSkipTest(this, log); + } + }); + it('should work with valid values', async function () { if (server.enabled) { await supertest diff --git a/x-pack/test/fleet_api_integration/apis/package_policy/delete.ts b/x-pack/test/fleet_api_integration/apis/package_policy/delete.ts new file mode 100644 index 0000000000000..e64ba8580d145 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/package_policy/delete.ts @@ -0,0 +1,127 @@ +/* + * 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 expect from '@kbn/expect'; +import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; +import { skipIfNoDockerRegistry } from '../../helpers'; + +export default function (providerContext: FtrProviderContext) { + const { getService } = providerContext; + const supertest = getService('supertest'); + + // use function () {} and not () => {} here + // because `this` has to point to the Mocha context + // see https://mochajs.org/#arrow-functions + + describe('Package Policy - delete', async function () { + skipIfNoDockerRegistry(providerContext); + let agentPolicy: any; + let packagePolicy: any; + + before(async function () { + let agentPolicyResponse = await supertest + .post(`/api/fleet/agent_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'Test policy', + namespace: 'default', + is_managed: false, + }); + + // if one already exists, re-use that + if (agentPolicyResponse.body.statusCode === 409) { + const errorRegex = /^agent policy \'(?[\w,\-]+)\' already exists/i; + const result = errorRegex.exec(agentPolicyResponse.body.message); + if (result?.groups?.id) { + agentPolicyResponse = await supertest + .put(`/api/fleet/agent_policies/${result.groups.id}`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'Test policy', + namespace: 'default', + is_managed: false, + }); + } + } + agentPolicy = agentPolicyResponse.body.item; + + const { body: packagePolicyResponse } = await supertest + .post(`/api/fleet/package_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'filetest-1', + description: '', + namespace: 'default', + policy_id: agentPolicy.id, + enabled: true, + output_id: '', + inputs: [], + package: { + name: 'filetest', + title: 'For File Tests', + version: '0.1.0', + }, + }); + packagePolicy = packagePolicyResponse.item; + }); + + after(async function () { + await supertest + .post(`/api/fleet/agent_policies/delete`) + .set('kbn-xsrf', 'xxxx') + .send({ agentPolicyId: agentPolicy.id }); + + await supertest + .post(`/api/fleet/package_policies/delete`) + .set('kbn-xsrf', 'xxxx') + .send({ packagePolicyIds: [packagePolicy.id] }); + }); + + it('should fail on managed agent policies', async function () { + // update existing policy to managed + await supertest + .put(`/api/fleet/agent_policies/${agentPolicy.id}`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: agentPolicy.name, + namespace: agentPolicy.namespace, + is_managed: true, + }) + .expect(200); + + // try to delete + const { body: results } = await supertest + .post(`/api/fleet/package_policies/delete`) + .set('kbn-xsrf', 'xxxx') + .send({ packagePolicyIds: [packagePolicy.id] }) + .expect(200); + + // delete always succeeds (returns 200) with Array<{success: boolean}> + expect(Array.isArray(results)); + expect(results.length).to.be(1); + expect(results[0].success).to.be(false); + expect(results[0].body.message).to.contain('Cannot remove integrations of managed policy'); + + // revert existing policy to unmanaged + await supertest + .put(`/api/fleet/agent_policies/${agentPolicy.id}`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: agentPolicy.name, + namespace: agentPolicy.namespace, + is_managed: false, + }) + .expect(200); + }); + + it('should work for unmanaged policies', async function () { + await supertest + .post(`/api/fleet/package_policies/delete`) + .set('kbn-xsrf', 'xxxx') + .send({ packagePolicyIds: [packagePolicy.id] }); + }); + }); +} diff --git a/x-pack/test/fleet_api_integration/apis/package_policy/update.ts b/x-pack/test/fleet_api_integration/apis/package_policy/update.ts index e0dc1a5d96b4b..9a70c6ad004dd 100644 --- a/x-pack/test/fleet_api_integration/apis/package_policy/update.ts +++ b/x-pack/test/fleet_api_integration/apis/package_policy/update.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import expect from '@kbn/expect'; import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; import { skipIfNoDockerRegistry } from '../../helpers'; @@ -21,6 +21,7 @@ export default function (providerContext: FtrProviderContext) { describe('Package Policy - update', async function () { skipIfNoDockerRegistry(providerContext); let agentPolicyId: string; + let managedAgentPolicyId: string; let packagePolicyId: string; let packagePolicyId2: string; @@ -35,8 +36,30 @@ export default function (providerContext: FtrProviderContext) { name: 'Test policy', namespace: 'default', }); + agentPolicyId = agentPolicyResponse.item.id; + const { body: managedAgentPolicyResponse } = await supertest + .post(`/api/fleet/agent_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'Test managed policy', + namespace: 'default', + is_managed: true, + }); + + // if one already exists, re-use that + const managedExists = managedAgentPolicyResponse.statusCode === 409; + if (managedExists) { + const errorRegex = /^agent policy \'(?[\w,\-]+)\' already exists/i; + const result = errorRegex.exec(managedAgentPolicyResponse.message); + if (result?.groups?.id) { + managedAgentPolicyId = result.groups.id; + } + } else { + managedAgentPolicyId = managedAgentPolicyResponse.item.id; + } + const { body: packagePolicyResponse } = await supertest .post(`/api/fleet/package_policies`) .set('kbn-xsrf', 'xxxx') @@ -83,6 +106,29 @@ export default function (providerContext: FtrProviderContext) { .send({ agentPolicyId }); }); + it('should fail on managed agent policies', async function () { + const { body } = await supertest + .put(`/api/fleet/package_policies/${packagePolicyId}`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'filetest-1', + description: '', + namespace: 'updated_namespace', + policy_id: managedAgentPolicyId, + enabled: true, + output_id: '', + inputs: [], + package: { + name: 'filetest', + title: 'For File Tests', + version: '0.1.0', + }, + }) + .expect(400); + + expect(body.message).to.contain('Cannot update integrations of managed policy'); + }); + it('should work with valid values', async function () { await supertest .put(`/api/fleet/package_policies/${packagePolicyId}`) diff --git a/x-pack/test/functional/apps/dashboard/dashboard_maps_by_value.ts b/x-pack/test/functional/apps/dashboard/dashboard_maps_by_value.ts new file mode 100644 index 0000000000000..15c76c3367a86 --- /dev/null +++ b/x-pack/test/functional/apps/dashboard/dashboard_maps_by_value.ts @@ -0,0 +1,132 @@ +/* + * 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 expect from '@kbn/expect'; +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ getPageObjects, getService }: FtrProviderContext) { + const PageObjects = getPageObjects([ + 'common', + 'dashboard', + 'maps', + 'timeToVisualize', + 'visualize', + ]); + + const log = getService('log'); + const esArchiver = getService('esArchiver'); + const dashboardVisualizations = getService('dashboardVisualizations'); + const dashboardPanelActions = getService('dashboardPanelActions'); + const testSubjects = getService('testSubjects'); + const appsMenu = getService('appsMenu'); + + const LAYER_NAME = 'World Countries'; + let mapCounter = 0; + + async function createAndAddMapByValue() { + log.debug(`createAndAddMapByValue`); + const inViewMode = await PageObjects.dashboard.getIsInViewMode(); + if (inViewMode) { + await PageObjects.dashboard.switchToEditMode(); + } + await PageObjects.visualize.clickMapsApp(); + await PageObjects.maps.clickSaveAndReturnButton(); + } + + async function editByValueMap(saveToLibrary = false, saveToDashboard = true) { + const inViewMode = await PageObjects.dashboard.getIsInViewMode(); + if (inViewMode) { + await PageObjects.dashboard.switchToEditMode(); + } + + await dashboardPanelActions.clickEdit(); + await PageObjects.maps.clickAddLayer(); + await PageObjects.maps.selectEMSBoundariesSource(); + await PageObjects.maps.selectVectorLayer(LAYER_NAME); + + if (saveToLibrary) { + await testSubjects.click('importFileButton'); + await testSubjects.click('mapSaveButton'); + await PageObjects.timeToVisualize.ensureSaveModalIsOpen; + + await PageObjects.timeToVisualize.saveFromModal(`my map ${mapCounter++}`, { + redirectToOrigin: saveToDashboard, + }); + + if (!saveToDashboard) { + await appsMenu.clickLink('Dashboard'); + } + } else { + await PageObjects.maps.clickSaveAndReturnButton(); + } + + await PageObjects.dashboard.waitForRenderComplete(); + } + + async function createNewDashboard() { + await PageObjects.common.navigateToApp('dashboard'); + await PageObjects.dashboard.preserveCrossAppState(); + await PageObjects.dashboard.clickNewDashboard(); + } + + describe('dashboard maps by value', function () { + before(async () => { + await esArchiver.loadIfNeeded('logstash_functional'); + await esArchiver.loadIfNeeded('lens/basic'); + }); + + describe('adding a map by value', () => { + it('can add a map by value', async () => { + await createNewDashboard(); + + await dashboardVisualizations.ensureNewVisualizationDialogIsShowing(); + await createAndAddMapByValue(); + const newPanelCount = await PageObjects.dashboard.getPanelCount(); + expect(newPanelCount).to.eql(1); + }); + }); + + describe('editing a map by value', () => { + before(async () => { + await createNewDashboard(); + await dashboardVisualizations.ensureNewVisualizationDialogIsShowing(); + await createAndAddMapByValue(); + await editByValueMap(); + }); + + it('retains the same number of panels', async () => { + const panelCount = await PageObjects.dashboard.getPanelCount(); + expect(panelCount).to.equal(1); + }); + + it('updates the panel on return', async () => { + const hasLayer = await PageObjects.maps.doesLayerExist(LAYER_NAME); + expect(hasLayer).to.be(true); + }); + }); + + describe('editing a map and adding to map library', () => { + beforeEach(async () => { + await createNewDashboard(); + await dashboardVisualizations.ensureNewVisualizationDialogIsShowing(); + await createAndAddMapByValue(); + }); + + it('updates the existing panel when adding to dashboard', async () => { + await editByValueMap(true); + + const hasLayer = await PageObjects.maps.doesLayerExist(LAYER_NAME); + + expect(hasLayer).to.be(true); + }); + + it('does not update the panel when only saving to library', async () => { + await editByValueMap(true, false); + }); + }); + }); +} diff --git a/x-pack/test/functional/apps/dashboard/index.ts b/x-pack/test/functional/apps/dashboard/index.ts index 5a8278535922e..1d046c7c18218 100644 --- a/x-pack/test/functional/apps/dashboard/index.ts +++ b/x-pack/test/functional/apps/dashboard/index.ts @@ -18,5 +18,6 @@ export default function ({ loadTestFile }: FtrProviderContext) { loadTestFile(require.resolve('./sync_colors')); loadTestFile(require.resolve('./_async_dashboard')); loadTestFile(require.resolve('./dashboard_lens_by_value')); + loadTestFile(require.resolve('./dashboard_maps_by_value')); }); } diff --git a/x-pack/test/functional/apps/dashboard/reporting/download_csv.ts b/x-pack/test/functional/apps/dashboard/reporting/download_csv.ts index bb910e187f925..72f07ef90d703 100644 --- a/x-pack/test/functional/apps/dashboard/reporting/download_csv.ts +++ b/x-pack/test/functional/apps/dashboard/reporting/download_csv.ts @@ -62,6 +62,10 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const savedSearchPanel = await testSubjects.find('embeddablePanelHeading-EcommerceData'); await dashboardPanelActions.toggleContextMenu(savedSearchPanel); + const actionExists = await testSubjects.exists('embeddablePanelAction-downloadCsvReport'); + if (!actionExists) { + await dashboardPanelActions.clickContextMenuMoreItem(); + } await testSubjects.existOrFail('embeddablePanelAction-downloadCsvReport'); // wait for the full panel to display or else the test runner could click the wrong option! await testSubjects.click('embeddablePanelAction-downloadCsvReport'); await testSubjects.existOrFail('csvDownloadStarted'); // validate toast panel @@ -80,6 +84,10 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { ); // panel title is hidden await dashboardPanelActions.toggleContextMenu(savedSearchPanel); + const actionExists = await testSubjects.exists('embeddablePanelAction-downloadCsvReport'); + if (!actionExists) { + await dashboardPanelActions.clickContextMenuMoreItem(); + } await testSubjects.existOrFail('embeddablePanelAction-downloadCsvReport'); await testSubjects.click('embeddablePanelAction-downloadCsvReport'); await testSubjects.existOrFail('csvDownloadStarted'); diff --git a/x-pack/test/functional/apps/dashboard_mode/dashboard_empty_screen.js b/x-pack/test/functional/apps/dashboard_mode/dashboard_empty_screen.js index c4cd87a5c3375..57925ad50d155 100644 --- a/x-pack/test/functional/apps/dashboard_mode/dashboard_empty_screen.js +++ b/x-pack/test/functional/apps/dashboard_mode/dashboard_empty_screen.js @@ -29,8 +29,8 @@ export default function ({ getPageObjects, getService }) { it('adds Lens visualization to empty dashboard', async () => { const title = 'Dashboard Test Lens'; - await testSubjects.exists('addVisualizationButton'); - await testSubjects.click('addVisualizationButton'); + await testSubjects.exists('dashboardAddNewPanelButton'); + await testSubjects.click('dashboardAddNewPanelButton'); await dashboardVisualizations.ensureNewVisualizationDialogIsShowing(); await PageObjects.lens.createAndAddLensFromDashboard({ title, redirectToOrigin: true }); await PageObjects.dashboard.waitForRenderComplete(); diff --git a/x-pack/test/functional/apps/discover/visualize_field.ts b/x-pack/test/functional/apps/discover/visualize_field.ts index f9312f453e8dd..d0d7c25c205e5 100644 --- a/x-pack/test/functional/apps/discover/visualize_field.ts +++ b/x-pack/test/functional/apps/discover/visualize_field.ts @@ -52,7 +52,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { await retry.try(async () => { const dimensions = await testSubjects.findAll('lns-dimensionTrigger'); expect(dimensions).to.have.length(2); - expect(await dimensions[1].getVisibleText()).to.be('Average of bytes'); + expect(await dimensions[1].getVisibleText()).to.be('Median of bytes'); }); }); diff --git a/x-pack/test/functional/apps/grok_debugger/grok_debugger.js b/x-pack/test/functional/apps/grok_debugger/grok_debugger.js index b2a1c5363fcb6..c21731a2bdc8a 100644 --- a/x-pack/test/functional/apps/grok_debugger/grok_debugger.js +++ b/x-pack/test/functional/apps/grok_debugger/grok_debugger.js @@ -11,8 +11,8 @@ export default function ({ getService, getPageObjects }) { const esArchiver = getService('esArchiver'); const PageObjects = getPageObjects(['grokDebugger']); - - describe('grok debugger app', function () { + // https://github.com/elastic/kibana/issues/84440 + describe.skip('grok debugger app', function () { this.tags('includeFirefox'); before(async () => { await esArchiver.load('empty_kibana'); diff --git a/x-pack/test/functional/apps/lens/dashboard.ts b/x-pack/test/functional/apps/lens/dashboard.ts index 738e45c1cbcf1..5cbd5dff45e1e 100644 --- a/x-pack/test/functional/apps/lens/dashboard.ts +++ b/x-pack/test/functional/apps/lens/dashboard.ts @@ -156,5 +156,43 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await panelActions.clickContextMenuMoreItem(); await testSubjects.existOrFail(ACTION_TEST_SUBJ); }); + + it('unlink lens panel from embeddable library', async () => { + await PageObjects.common.navigateToApp('dashboard'); + await PageObjects.dashboard.clickNewDashboard(); + await dashboardAddPanel.clickOpenAddPanel(); + await dashboardAddPanel.filterEmbeddableNames('lnsPieVis'); + await find.clickByButtonText('lnsPieVis'); + await dashboardAddPanel.closeAddPanel(); + + const originalPanel = await testSubjects.find('embeddablePanelHeading-lnsPieVis'); + await panelActions.unlinkFromLibary(originalPanel); + await testSubjects.existOrFail('unlinkPanelSuccess'); + + const updatedPanel = await testSubjects.find('embeddablePanelHeading-lnsPieVis'); + const libraryActionExists = await testSubjects.descendantExists( + 'embeddablePanelNotification-ACTION_LIBRARY_NOTIFICATION', + updatedPanel + ); + expect(libraryActionExists).to.be(false); + }); + + it('save lens panel to embeddable library', async () => { + const originalPanel = await testSubjects.find('embeddablePanelHeading-lnsPieVis'); + await panelActions.saveToLibrary('lnsPieVis - copy', originalPanel); + await testSubjects.click('confirmSaveSavedObjectButton'); + await testSubjects.existOrFail('addPanelToLibrarySuccess'); + + const updatedPanel = await testSubjects.find('embeddablePanelHeading-lnsPieVis-copy'); + const libraryActionExists = await testSubjects.descendantExists( + 'embeddablePanelNotification-ACTION_LIBRARY_NOTIFICATION', + updatedPanel + ); + expect(libraryActionExists).to.be(true); + + await dashboardAddPanel.clickOpenAddPanel(); + await dashboardAddPanel.filterEmbeddableNames('lnsPieVis'); + await find.existsByLinkText('lnsPieVis'); + }); }); } diff --git a/x-pack/test/functional/apps/lens/drag_and_drop.ts b/x-pack/test/functional/apps/lens/drag_and_drop.ts index a272b67de1b0a..0e4d428c26029 100644 --- a/x-pack/test/functional/apps/lens/drag_and_drop.ts +++ b/x-pack/test/functional/apps/lens/drag_and_drop.ts @@ -149,7 +149,7 @@ export default function ({ getPageObjects }: FtrProviderContext) { await PageObjects.lens.dragFieldWithKeyboard('bytes', 4); expect(await PageObjects.lens.getDimensionTriggersTexts('lnsXY_yDimensionPanel')).to.eql([ 'Count of records', - 'Average of bytes', + 'Median of bytes', ]); await PageObjects.lens.dragFieldWithKeyboard('@message.raw', 1, true); expect( @@ -169,7 +169,7 @@ export default function ({ getPageObjects }: FtrProviderContext) { await PageObjects.lens.dimensionKeyboardDragDrop('lnsXY_yDimensionPanel', 0, 1); expect(await PageObjects.lens.getDimensionTriggersTexts('lnsXY_yDimensionPanel')).to.eql([ 'Count of records', - 'Average of bytes', + 'Median of bytes', 'Count of records [1]', ]); diff --git a/x-pack/test/functional/apps/lens/persistent_context.ts b/x-pack/test/functional/apps/lens/persistent_context.ts index 0ed9506149f92..a3ef8ac33fb9a 100644 --- a/x-pack/test/functional/apps/lens/persistent_context.ts +++ b/x-pack/test/functional/apps/lens/persistent_context.ts @@ -49,7 +49,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { 'Sep 19, 2025 @ 06:31:44.000' ); await filterBar.toggleFilterEnabled('ip'); - await appsMenu.clickLink('Visualize', { category: 'kibana' }); + await appsMenu.clickLink('Visualize Library', { category: 'kibana' }); await PageObjects.visualize.clickNewVisualization(); await PageObjects.visualize.waitForGroupsSelectPage(); await PageObjects.visualize.clickVisType('lens'); diff --git a/x-pack/test/functional/apps/maps/embeddable/embeddable_library.js b/x-pack/test/functional/apps/maps/embeddable/embeddable_library.js new file mode 100644 index 0000000000000..40e73f0d8a763 --- /dev/null +++ b/x-pack/test/functional/apps/maps/embeddable/embeddable_library.js @@ -0,0 +1,80 @@ +/* + * 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 expect from '@kbn/expect'; + +export default function ({ getPageObjects, getService }) { + const find = getService('find'); + const testSubjects = getService('testSubjects'); + const PageObjects = getPageObjects(['common', 'dashboard', 'header', 'maps', 'visualize']); + const kibanaServer = getService('kibanaServer'); + const security = getService('security'); + const dashboardAddPanel = getService('dashboardAddPanel'); + const dashboardPanelActions = getService('dashboardPanelActions'); + const dashboardVisualizations = getService('dashboardVisualizations'); + + describe('maps in embeddable library', () => { + before(async () => { + await security.testUser.setRoles( + [ + 'test_logstash_reader', + 'global_maps_all', + 'geoshape_data_reader', + 'global_dashboard_all', + 'meta_for_geoshape_data_reader', + ], + false + ); + await kibanaServer.uiSettings.replace({ + defaultIndex: 'c698b940-e149-11e8-a35a-370a8516603a', + }); + await PageObjects.common.navigateToApp('dashboard'); + await PageObjects.dashboard.clickNewDashboard(); + await dashboardAddPanel.clickCreateNewLink(); + await dashboardVisualizations.ensureNewVisualizationDialogIsShowing(); + await PageObjects.visualize.clickMapsApp(); + await PageObjects.header.waitUntilLoadingHasFinished(); + await PageObjects.maps.waitForLayersToLoad(); + await PageObjects.maps.clickSaveAndReturnButton(); + await PageObjects.dashboard.waitForRenderComplete(); + }); + + after(async () => { + await security.testUser.restoreDefaults(); + }); + + it('save map panel to embeddable library', async () => { + await dashboardPanelActions.saveToLibrary('embeddable library map'); + await testSubjects.existOrFail('addPanelToLibrarySuccess'); + + const mapPanel = await testSubjects.find('embeddablePanelHeading-embeddablelibrarymap'); + const libraryActionExists = await testSubjects.descendantExists( + 'embeddablePanelNotification-ACTION_LIBRARY_NOTIFICATION', + mapPanel + ); + expect(libraryActionExists).to.be(true); + }); + + it('unlink map panel from embeddable library', async () => { + const originalPanel = await testSubjects.find('embeddablePanelHeading-embeddablelibrarymap'); + await dashboardPanelActions.unlinkFromLibary(originalPanel); + await testSubjects.existOrFail('unlinkPanelSuccess'); + + const updatedPanel = await testSubjects.find('embeddablePanelHeading-embeddablelibrarymap'); + const libraryActionExists = await testSubjects.descendantExists( + 'embeddablePanelNotification-ACTION_LIBRARY_NOTIFICATION', + updatedPanel + ); + expect(libraryActionExists).to.be(false); + + await dashboardAddPanel.clickOpenAddPanel(); + await dashboardAddPanel.filterEmbeddableNames('embeddable library map'); + await find.existsByLinkText('embeddable library map'); + await dashboardAddPanel.closeAddPanel(); + }); + }); +} diff --git a/x-pack/test/functional/apps/maps/embeddable/index.js b/x-pack/test/functional/apps/maps/embeddable/index.js index 88d8cf2d7bd54..552f830e2a379 100644 --- a/x-pack/test/functional/apps/maps/embeddable/index.js +++ b/x-pack/test/functional/apps/maps/embeddable/index.js @@ -10,6 +10,7 @@ export default function ({ loadTestFile }) { loadTestFile(require.resolve('./add_to_dashboard')); loadTestFile(require.resolve('./save_and_return')); loadTestFile(require.resolve('./dashboard')); + loadTestFile(require.resolve('./embeddable_library')); loadTestFile(require.resolve('./embeddable_state')); loadTestFile(require.resolve('./tooltip_filter_actions')); }); diff --git a/x-pack/test/functional/apps/transform/feature_controls/transform_security.ts b/x-pack/test/functional/apps/transform/feature_controls/transform_security.ts index b8d6b88e4ed9a..46e0c01afcc38 100644 --- a/x-pack/test/functional/apps/transform/feature_controls/transform_security.ts +++ b/x-pack/test/functional/apps/transform/feature_controls/transform_security.ts @@ -15,8 +15,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { const appsMenu = getService('appsMenu'); const managementMenu = getService('managementMenu'); - // FLAKY: https://github.com/elastic/kibana/issues/90576 - describe.skip('security', () => { + describe('security', () => { before(async () => { await esArchiver.load('empty_kibana'); await PageObjects.security.forceLogout(); diff --git a/x-pack/test/functional/apps/visualize/feature_controls/visualize_security.ts b/x-pack/test/functional/apps/visualize/feature_controls/visualize_security.ts index da94eaf19ea3f..d6644cee21198 100644 --- a/x-pack/test/functional/apps/visualize/feature_controls/visualize_security.ts +++ b/x-pack/test/functional/apps/visualize/feature_controls/visualize_security.ts @@ -81,7 +81,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { it('shows visualize navlink', async () => { const navLinks = (await appsMenu.readLinks()).map((link) => link.text); - expect(navLinks).to.eql(['Overview', 'Visualize']); + expect(navLinks).to.eql(['Overview', 'Visualize Library']); }); it(`landing page shows "Create new Visualization" button`, async () => { @@ -212,7 +212,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { it('shows visualize navlink', async () => { const navLinks = (await appsMenu.readLinks()).map((link) => link.text); - expect(navLinks).to.eql(['Overview', 'Visualize']); + expect(navLinks).to.eql(['Overview', 'Visualize Library']); }); it(`landing page shows "Create new Visualization" button`, async () => { @@ -327,7 +327,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { it('shows visualize navlink', async () => { const navLinks = (await appsMenu.readLinks()).map((link) => link.text); - expect(navLinks).to.eql(['Overview', 'Visualize']); + expect(navLinks).to.eql(['Overview', 'Visualize Library']); }); it(`landing page shows "Create new Visualization" button`, async () => { diff --git a/x-pack/test/functional/apps/visualize/feature_controls/visualize_spaces.ts b/x-pack/test/functional/apps/visualize/feature_controls/visualize_spaces.ts index 5c6ea66f1b049..469a337177065 100644 --- a/x-pack/test/functional/apps/visualize/feature_controls/visualize_spaces.ts +++ b/x-pack/test/functional/apps/visualize/feature_controls/visualize_spaces.ts @@ -44,7 +44,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { basePath: '/s/custom_space', }); const navLinks = (await appsMenu.readLinks()).map((link) => link.text); - expect(navLinks).to.contain('Visualize'); + expect(navLinks).to.contain('Visualize Library'); }); it(`can view existing Visualization`, async () => { @@ -85,7 +85,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) { basePath: '/s/custom_space', }); const navLinks = (await appsMenu.readLinks()).map((link) => link.text); - expect(navLinks).not.to.contain('Visualize'); + expect(navLinks).not.to.contain('Visualize Library'); }); it(`create new visualization shows 404`, async () => { diff --git a/x-pack/test/functional/apps/visualize/preserve_url.ts b/x-pack/test/functional/apps/visualize/preserve_url.ts index b48f82fc0fd2a..16267a544275c 100644 --- a/x-pack/test/functional/apps/visualize/preserve_url.ts +++ b/x-pack/test/functional/apps/visualize/preserve_url.ts @@ -27,7 +27,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.common.navigateToApp('visualize'); await PageObjects.visualize.openSavedVisualization('A Pie'); await PageObjects.common.navigateToApp('home'); - await appsMenu.clickLink('Visualize'); + await appsMenu.clickLink('Visualize Library'); await PageObjects.visChart.waitForVisualization(); const activeTitle = await globalNav.getLastBreadcrumb(); expect(activeTitle).to.be('A Pie'); @@ -43,7 +43,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.spaceSelector.expectHomePage('another-space'); // other space - await appsMenu.clickLink('Visualize'); + await appsMenu.clickLink('Visualize Library'); await PageObjects.visualize.openSavedVisualization('A Pie in another space'); await PageObjects.spaceSelector.openSpacesNav(); @@ -51,7 +51,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.spaceSelector.expectHomePage('default'); // default space - await appsMenu.clickLink('Visualize'); + await appsMenu.clickLink('Visualize Library'); await PageObjects.visChart.waitForVisualization(); const activeTitleDefaultSpace = await globalNav.getLastBreadcrumb(); expect(activeTitleDefaultSpace).to.be('A Pie'); @@ -61,7 +61,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { await PageObjects.spaceSelector.expectHomePage('another-space'); // other space - await appsMenu.clickLink('Visualize'); + await appsMenu.clickLink('Visualize Library'); await PageObjects.visChart.waitForVisualization(); const activeTitleOtherSpace = await globalNav.getLastBreadcrumb(); expect(activeTitleOtherSpace).to.be('A Pie in another space'); diff --git a/x-pack/test/functional_cors/plugins/kibana_cors_test/kibana.json b/x-pack/test/functional_cors/plugins/kibana_cors_test/kibana.json index 9c94f2006b7f8..a0ebde9bff4b7 100644 --- a/x-pack/test/functional_cors/plugins/kibana_cors_test/kibana.json +++ b/x-pack/test/functional_cors/plugins/kibana_cors_test/kibana.json @@ -1,5 +1,5 @@ { - "id": "kibana_cors_test", + "id": "kibanaCorsTest", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["test", "cors"], diff --git a/x-pack/test/functional_embedded/plugins/iframe_embedded/kibana.json b/x-pack/test/functional_embedded/plugins/iframe_embedded/kibana.json index ea9f55bd21c6e..919b7f69d28b9 100644 --- a/x-pack/test/functional_embedded/plugins/iframe_embedded/kibana.json +++ b/x-pack/test/functional_embedded/plugins/iframe_embedded/kibana.json @@ -1,5 +1,5 @@ { - "id": "iframe_embedded", + "id": "iframeEmbedded", "version": "1.0.0", "kibanaVersion": "kibana", "server": true, diff --git a/x-pack/test/functional_with_es_ssl/fixtures/plugins/alerts/kibana.json b/x-pack/test/functional_with_es_ssl/fixtures/plugins/alerts/kibana.json index 784a766e608bc..11a8fb977cd78 100644 --- a/x-pack/test/functional_with_es_ssl/fixtures/plugins/alerts/kibana.json +++ b/x-pack/test/functional_with_es_ssl/fixtures/plugins/alerts/kibana.json @@ -1,5 +1,5 @@ { - "id": "alerting_fixture", + "id": "alertingFixture", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/plugin_api_integration/plugins/elasticsearch_client/kibana.json b/x-pack/test/plugin_api_integration/plugins/elasticsearch_client/kibana.json index 37ec33c168e76..5f4cb3f7f7eb2 100644 --- a/x-pack/test/plugin_api_integration/plugins/elasticsearch_client/kibana.json +++ b/x-pack/test/plugin_api_integration/plugins/elasticsearch_client/kibana.json @@ -1,5 +1,5 @@ { - "id": "elasticsearch_client_xpack", + "id": "elasticsearchClientXpack", "version": "1.0.0", "kibanaVersion": "kibana", "server": true, diff --git a/x-pack/test/plugin_api_integration/plugins/event_log/kibana.json b/x-pack/test/plugin_api_integration/plugins/event_log/kibana.json index 4b467ce975012..4c940ffec1463 100644 --- a/x-pack/test/plugin_api_integration/plugins/event_log/kibana.json +++ b/x-pack/test/plugin_api_integration/plugins/event_log/kibana.json @@ -1,5 +1,5 @@ { - "id": "event_log_fixture", + "id": "eventLogFixture", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/plugin_api_integration/plugins/feature_usage_test/kibana.json b/x-pack/test/plugin_api_integration/plugins/feature_usage_test/kibana.json index b11b7ada24a57..b81f96362e9f5 100644 --- a/x-pack/test/plugin_api_integration/plugins/feature_usage_test/kibana.json +++ b/x-pack/test/plugin_api_integration/plugins/feature_usage_test/kibana.json @@ -1,5 +1,5 @@ { - "id": "feature_usage_test", + "id": "featureUsageTest", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack", "feature_usage_test"], diff --git a/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/kibana.json b/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/kibana.json index 416ef7fa34591..6a8a2221b48d3 100644 --- a/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/kibana.json +++ b/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/kibana.json @@ -1,5 +1,5 @@ { - "id": "sample_task_plugin", + "id": "sampleTaskPlugin", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/init_routes.ts b/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/init_routes.ts index 2878d7d5f8220..57beb40b16459 100644 --- a/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/init_routes.ts +++ b/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/init_routes.ts @@ -218,10 +218,9 @@ export function initRoutes( await ensureIndexIsRefreshed(); const taskManager = await taskManagerStart; return res.ok({ body: await taskManager.get(req.params.taskId) }); - } catch (err) { - return res.ok({ body: err }); + } catch ({ isBoom, output, message }) { + return res.ok({ body: isBoom ? output.payload : { message } }); } - return res.ok({ body: {} }); } ); @@ -251,6 +250,7 @@ export function initRoutes( res: KibanaResponseFactory ): Promise> { try { + await ensureIndexIsRefreshed(); let tasksFound = 0; const taskManager = await taskManagerStart; do { @@ -261,8 +261,8 @@ export function initRoutes( await Promise.all(tasks.map((task) => taskManager.remove(task.id))); } while (tasksFound > 0); return res.ok({ body: 'OK' }); - } catch (err) { - return res.ok({ body: err }); + } catch ({ isBoom, output, message }) { + return res.ok({ body: isBoom ? output.payload : { message } }); } } ); diff --git a/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/plugin.ts b/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/plugin.ts index 3aee35ed0bff3..2031551410894 100644 --- a/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/plugin.ts +++ b/x-pack/test/plugin_api_integration/plugins/sample_task_plugin/server/plugin.ts @@ -105,6 +105,20 @@ export class SampleTaskManagerFixturePlugin // fail after the first failed run maxAttempts: 1, }, + sampleTaskWithSingleConcurrency: { + ...defaultSampleTaskConfig, + title: 'Sample Task With Single Concurrency', + maxConcurrency: 1, + timeout: '60s', + description: 'A sample task that can only have one concurrent instance.', + }, + sampleTaskWithLimitedConcurrency: { + ...defaultSampleTaskConfig, + title: 'Sample Task With Max Concurrency of 2', + maxConcurrency: 2, + timeout: '60s', + description: 'A sample task that can only have two concurrent instance.', + }, sampleRecurringTaskTimingOut: { title: 'Sample Recurring Task that Times Out', description: 'A sample task that times out each run.', diff --git a/x-pack/test/plugin_api_integration/test_suites/task_manager/health_route.ts b/x-pack/test/plugin_api_integration/test_suites/task_manager/health_route.ts index 231150a814835..d99c1dac9a25e 100644 --- a/x-pack/test/plugin_api_integration/test_suites/task_manager/health_route.ts +++ b/x-pack/test/plugin_api_integration/test_suites/task_manager/health_route.ts @@ -34,6 +34,7 @@ interface MonitoringStats { timestamp: string; value: { drift: Record; + drift_by_type: Record>; load: Record; execution: { duration: Record>; @@ -43,6 +44,7 @@ interface MonitoringStats { last_successful_poll: string; last_polling_delay: string; duration: Record; + claim_duration: Record; result_frequency_percent_as_number: Record; }; }; @@ -174,7 +176,8 @@ export default function ({ getService }: FtrProviderContext) { const { runtime: { - value: { drift, load, polling, execution }, + // eslint-disable-next-line @typescript-eslint/naming-convention + value: { drift, drift_by_type, load, polling, execution }, }, } = (await getHealth()).stats; @@ -192,11 +195,21 @@ export default function ({ getService }: FtrProviderContext) { expect(typeof polling.duration.p95).to.eql('number'); expect(typeof polling.duration.p99).to.eql('number'); + expect(typeof polling.claim_duration.p50).to.eql('number'); + expect(typeof polling.claim_duration.p90).to.eql('number'); + expect(typeof polling.claim_duration.p95).to.eql('number'); + expect(typeof polling.claim_duration.p99).to.eql('number'); + expect(typeof drift.p50).to.eql('number'); expect(typeof drift.p90).to.eql('number'); expect(typeof drift.p95).to.eql('number'); expect(typeof drift.p99).to.eql('number'); + expect(typeof drift_by_type.sampleTask.p50).to.eql('number'); + expect(typeof drift_by_type.sampleTask.p90).to.eql('number'); + expect(typeof drift_by_type.sampleTask.p95).to.eql('number'); + expect(typeof drift_by_type.sampleTask.p99).to.eql('number'); + expect(typeof load.p50).to.eql('number'); expect(typeof load.p90).to.eql('number'); expect(typeof load.p95).to.eql('number'); diff --git a/x-pack/test/plugin_api_integration/test_suites/task_manager/task_management.ts b/x-pack/test/plugin_api_integration/test_suites/task_manager/task_management.ts index 353be5e872aed..26333ecabd505 100644 --- a/x-pack/test/plugin_api_integration/test_suites/task_manager/task_management.ts +++ b/x-pack/test/plugin_api_integration/test_suites/task_manager/task_management.ts @@ -51,7 +51,7 @@ type SerializedConcreteTaskInstance = Omit< }; export default function ({ getService }: FtrProviderContext) { - const es = getService('legacyEs'); + const es = getService('es'); const log = getService('log'); const retry = getService('retry'); const config = getService('config'); @@ -59,30 +59,46 @@ export default function ({ getService }: FtrProviderContext) { const supertest = supertestAsPromised(url.format(config.get('servers.kibana'))); describe('scheduling and running tasks', () => { - beforeEach( - async () => await supertest.delete('/api/sample_tasks').set('kbn-xsrf', 'xxx').expect(200) - ); + beforeEach(async () => { + // clean up before each test + return await supertest.delete('/api/sample_tasks').set('kbn-xsrf', 'xxx').expect(200); + }); beforeEach(async () => { const exists = await es.indices.exists({ index: testHistoryIndex }); - if (exists) { + if (exists.body) { await es.deleteByQuery({ index: testHistoryIndex, - q: 'type:task', refresh: true, + body: { query: { term: { type: 'task' } } }, }); } else { await es.indices.create({ index: testHistoryIndex, body: { mappings: { - properties: taskManagerIndexMapping, + properties: { + type: { + type: 'keyword', + }, + taskId: { + type: 'keyword', + }, + params: taskManagerIndexMapping.params, + state: taskManagerIndexMapping.state, + runAt: taskManagerIndexMapping.runAt, + }, }, }, }); } }); + after(async () => { + // clean up after last test + return await supertest.delete('/api/sample_tasks').set('kbn-xsrf', 'xxx').expect(200); + }); + function currentTasks(): Promise<{ docs: Array>; }> { @@ -98,7 +114,27 @@ export default function ({ getService }: FtrProviderContext) { return supertest .get(`/api/sample_tasks/task/${task}`) .send({ task }) - .expect(200) + .expect((response) => { + expect(response.status).to.eql(200); + expect(typeof JSON.parse(response.text).id).to.eql(`string`); + }) + .then((response) => response.body); + } + + function currentTaskError( + task: string + ): Promise<{ + statusCode: number; + error: string; + message: string; + }> { + return supertest + .get(`/api/sample_tasks/task/${task}`) + .send({ task }) + .expect(function (response) { + expect(response.status).to.eql(200); + expect(typeof JSON.parse(response.text).message).to.eql(`string`); + }) .then((response) => response.body); } @@ -106,13 +142,21 @@ export default function ({ getService }: FtrProviderContext) { return supertest.get(`/api/ensure_tasks_index_refreshed`).send({}).expect(200); } - function historyDocs(taskId?: string): Promise { + async function historyDocs(taskId?: string): Promise { return es .search({ index: testHistoryIndex, - q: taskId ? `taskId:${taskId}` : 'type:task', + body: { + query: { + term: { type: 'task' }, + }, + }, }) - .then((result: SearchResults) => result.hits.hits); + .then((result) => + ((result.body as unknown) as SearchResults).hits.hits.filter((task) => + taskId ? task._source?.taskId === taskId : true + ) + ); } function scheduleTask( @@ -123,7 +167,10 @@ export default function ({ getService }: FtrProviderContext) { .set('kbn-xsrf', 'xxx') .send({ task }) .expect(200) - .then((response: { body: SerializedConcreteTaskInstance }) => response.body); + .then((response: { body: SerializedConcreteTaskInstance }) => { + log.debug(`Task Scheduled: ${response.body.id}`); + return response.body; + }); } function runTaskNow(task: { id: string }) { @@ -252,8 +299,7 @@ export default function ({ getService }: FtrProviderContext) { }); await retry.try(async () => { - const [scheduledTask] = (await currentTasks()).docs; - expect(scheduledTask.id).to.eql(task.id); + const scheduledTask = await currentTask(task.id); expect(scheduledTask.attempts).to.be.greaterThan(0); expect(Date.parse(scheduledTask.runAt)).to.be.greaterThan( Date.parse(task.runAt) + 5 * 60 * 1000 @@ -271,8 +317,7 @@ export default function ({ getService }: FtrProviderContext) { }); await retry.try(async () => { - const [scheduledTask] = (await currentTasks()).docs; - expect(scheduledTask.id).to.eql(task.id); + const scheduledTask = await currentTask(task.id); const retryAt = Date.parse(scheduledTask.retryAt!); expect(isNaN(retryAt)).to.be(false); @@ -296,7 +341,7 @@ export default function ({ getService }: FtrProviderContext) { await retry.try(async () => { expect((await historyDocs(originalTask.id)).length).to.eql(1); - const [task] = (await currentTasks<{ count: number }>()).docs; + const task = await currentTask<{ count: number }>(originalTask.id); expect(task.attempts).to.eql(0); expect(task.state.count).to.eql(count + 1); @@ -467,6 +512,134 @@ export default function ({ getService }: FtrProviderContext) { }); }); + it('should only run as many instances of a task as its maxConcurrency will allow', async () => { + // should run as there's only one and maxConcurrency on this TaskType is 1 + const firstWithSingleConcurrency = await scheduleTask({ + taskType: 'sampleTaskWithSingleConcurrency', + params: { + waitForEvent: 'releaseFirstWaveOfTasks', + }, + }); + + // should run as there's only two and maxConcurrency on this TaskType is 2 + const [firstLimitedConcurrency, secondLimitedConcurrency] = await Promise.all([ + scheduleTask({ + taskType: 'sampleTaskWithLimitedConcurrency', + params: { + waitForEvent: 'releaseFirstWaveOfTasks', + }, + }), + scheduleTask({ + taskType: 'sampleTaskWithLimitedConcurrency', + params: { + waitForEvent: 'releaseSecondWaveOfTasks', + }, + }), + ]); + + await retry.try(async () => { + expect((await historyDocs(firstWithSingleConcurrency.id)).length).to.eql(1); + expect((await historyDocs(firstLimitedConcurrency.id)).length).to.eql(1); + expect((await historyDocs(secondLimitedConcurrency.id)).length).to.eql(1); + }); + + // should not run as there one running and maxConcurrency on this TaskType is 1 + const secondWithSingleConcurrency = await scheduleTask({ + taskType: 'sampleTaskWithSingleConcurrency', + params: { + waitForEvent: 'releaseSecondWaveOfTasks', + }, + }); + + // should not run as there are two running and maxConcurrency on this TaskType is 2 + const thirdWithLimitedConcurrency = await scheduleTask({ + taskType: 'sampleTaskWithLimitedConcurrency', + params: { + waitForEvent: 'releaseSecondWaveOfTasks', + }, + }); + + // schedule a task that should get picked up before the two blocked tasks + const taskWithUnlimitedConcurrency = await scheduleTask({ + taskType: 'sampleTask', + params: {}, + }); + + await retry.try(async () => { + expect((await historyDocs(taskWithUnlimitedConcurrency.id)).length).to.eql(1); + expect((await currentTask(secondWithSingleConcurrency.id)).status).to.eql('idle'); + expect((await currentTask(thirdWithLimitedConcurrency.id)).status).to.eql('idle'); + }); + + // release the running SingleConcurrency task and only one of the LimitedConcurrency tasks + await releaseTasksWaitingForEventToComplete('releaseFirstWaveOfTasks'); + + await retry.try(async () => { + // ensure the completed tasks were deleted + expect((await currentTaskError(firstWithSingleConcurrency.id)).message).to.eql( + `Saved object [task/${firstWithSingleConcurrency.id}] not found` + ); + expect((await currentTaskError(firstLimitedConcurrency.id)).message).to.eql( + `Saved object [task/${firstLimitedConcurrency.id}] not found` + ); + + // ensure blocked tasks is still running + expect((await currentTask(secondLimitedConcurrency.id)).status).to.eql('running'); + + // ensure the blocked tasks begin running + expect((await currentTask(secondWithSingleConcurrency.id)).status).to.eql('running'); + expect((await currentTask(thirdWithLimitedConcurrency.id)).status).to.eql('running'); + }); + + // release blocked task + await releaseTasksWaitingForEventToComplete('releaseSecondWaveOfTasks'); + }); + + it('should return a task run error result when RunNow is called at a time that would cause the task to exceed its maxConcurrency', async () => { + // should run as there's only one and maxConcurrency on this TaskType is 1 + const firstWithSingleConcurrency = await scheduleTask({ + taskType: 'sampleTaskWithSingleConcurrency', + // include a schedule so that the task isn't deleted after completion + schedule: { interval: `30m` }, + params: { + waitForEvent: 'releaseRunningTaskWithSingleConcurrency', + }, + }); + + // should not run as the first is running + const secondWithSingleConcurrency = await scheduleTask({ + taskType: 'sampleTaskWithSingleConcurrency', + params: { + waitForEvent: 'releaseRunningTaskWithSingleConcurrency', + }, + }); + + // run the first tasks once just so that we can be sure it runs in response to our + // runNow callm, rather than the initial execution + await retry.try(async () => { + expect((await historyDocs(firstWithSingleConcurrency.id)).length).to.eql(1); + }); + await releaseTasksWaitingForEventToComplete('releaseRunningTaskWithSingleConcurrency'); + + // wait for second task to stall + await retry.try(async () => { + expect((await historyDocs(secondWithSingleConcurrency.id)).length).to.eql(1); + }); + + // run the first task again using runNow - should fail due to concurrency concerns + const failedRunNowResult = await runTaskNow({ + id: firstWithSingleConcurrency.id, + }); + + expect(failedRunNowResult).to.eql({ + id: firstWithSingleConcurrency.id, + error: `Error: Failed to run task "${firstWithSingleConcurrency.id}" as we would exceed the max concurrency of "Sample Task With Single Concurrency" which is 1. Rescheduled the task to ensure it is picked up as soon as possible.`, + }); + + // release the second task + await releaseTasksWaitingForEventToComplete('releaseRunningTaskWithSingleConcurrency'); + }); + it('should return a task run error result when running a task now fails', async () => { const originalTask = await scheduleTask({ taskType: 'sampleTask', diff --git a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/kibana.json b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/kibana.json index 1fa480cd53c48..387f392c8db98 100644 --- a/x-pack/test/plugin_api_perf/plugins/task_manager_performance/kibana.json +++ b/x-pack/test/plugin_api_perf/plugins/task_manager_performance/kibana.json @@ -1,5 +1,5 @@ { - "id": "task_manager_performance", + "id": "taskManagerPerformance", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack"], diff --git a/x-pack/test/plugin_functional/plugins/resolver_test/kibana.json b/x-pack/test/plugin_functional/plugins/resolver_test/kibana.json index 499983561e89d..a203705e13ed6 100644 --- a/x-pack/test/plugin_functional/plugins/resolver_test/kibana.json +++ b/x-pack/test/plugin_functional/plugins/resolver_test/kibana.json @@ -1,5 +1,5 @@ { - "id": "resolver_test", + "id": "resolverTest", "version": "1.0.0", "kibanaVersion": "kibana", "configPath": ["xpack", "resolverTest"], diff --git a/x-pack/test/security_api_integration/fixtures/oidc/oidc_provider/kibana.json b/x-pack/test/security_api_integration/fixtures/oidc/oidc_provider/kibana.json index faaa0b9165828..aa7cd499a173a 100644 --- a/x-pack/test/security_api_integration/fixtures/oidc/oidc_provider/kibana.json +++ b/x-pack/test/security_api_integration/fixtures/oidc/oidc_provider/kibana.json @@ -1,5 +1,5 @@ { - "id": "oidc_provider_plugin", + "id": "oidcProviderPlugin", "version": "8.0.0", "kibanaVersion": "kibana", "server": true, diff --git a/x-pack/test/security_api_integration/fixtures/saml/saml_provider/kibana.json b/x-pack/test/security_api_integration/fixtures/saml/saml_provider/kibana.json index 3cbd37e38bb2d..81ec23fc3d2f3 100644 --- a/x-pack/test/security_api_integration/fixtures/saml/saml_provider/kibana.json +++ b/x-pack/test/security_api_integration/fixtures/saml/saml_provider/kibana.json @@ -1,5 +1,5 @@ { - "id": "saml_provider_plugin", + "id": "samlProviderPlugin", "version": "8.0.0", "kibanaVersion": "kibana", "server": true, diff --git a/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/index.ts b/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/index.ts index 994d91ae4a27b..0798a25a2e982 100644 --- a/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/index.ts +++ b/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/index.ts @@ -22,5 +22,6 @@ export default function ({ loadTestFile, getService }: FtrProviderContext) { }); loadTestFile(require.resolve('./sessions_management')); + loadTestFile(require.resolve('./sessions_management_permissions')); }); } diff --git a/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/sessions_management_permissions.ts b/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/sessions_management_permissions.ts new file mode 100644 index 0000000000000..48f4156afbe82 --- /dev/null +++ b/x-pack/test/send_search_to_background_integration/tests/apps/management/search_sessions/sessions_management_permissions.ts @@ -0,0 +1,111 @@ +/* + * 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 expect from '@kbn/expect'; +import { FtrProviderContext } from '../../../../ftr_provider_context'; + +export default function ({ getService, getPageObjects }: FtrProviderContext) { + const security = getService('security'); + const PageObjects = getPageObjects([ + 'common', + 'header', + 'dashboard', + 'visChart', + 'searchSessionsManagement', + 'security', + ]); + + const appsMenu = getService('appsMenu'); + const managementMenu = getService('managementMenu'); + + describe('Search sessions Management UI permissions', () => { + describe('Sessions management is not available if non of apps enable search sessions', () => { + before(async () => { + await security.role.create('data_analyst', { + elasticsearch: {}, + kibana: [ + { + feature: { + dashboard: ['read'], + }, + spaces: ['*'], + }, + ], + }); + + await security.user.create('analyst', { + password: 'analyst-password', + roles: ['data_analyst'], + full_name: 'test user', + }); + + await PageObjects.security.forceLogout(); + + await PageObjects.security.login('analyst', 'analyst-password', { + expectSpaceSelector: false, + }); + }); + + after(async () => { + await security.role.delete('data_analyst'); + await security.user.delete('analyst'); + await PageObjects.security.forceLogout(); + }); + + it('Sessions management is not available if non of apps enable search sessions', async () => { + const links = await appsMenu.readLinks(); + expect(links.map((link) => link.text)).to.not.contain('Stack Management'); + }); + }); + + describe('Sessions management is available if one of apps enables search sessions', () => { + before(async () => { + await security.role.create('data_analyst', { + elasticsearch: {}, + kibana: [ + { + feature: { + dashboard: ['read', 'store_search_session'], + }, + spaces: ['*'], + }, + ], + }); + + await security.user.create('analyst', { + password: 'analyst-password', + roles: ['data_analyst'], + full_name: 'test user', + }); + + await PageObjects.security.forceLogout(); + + await PageObjects.security.login('analyst', 'analyst-password', { + expectSpaceSelector: false, + }); + }); + + after(async () => { + await security.role.delete('data_analyst'); + await security.user.delete('analyst'); + await PageObjects.security.forceLogout(); + }); + + it('Sessions management is available if one of apps enables search sessions', async () => { + const links = await appsMenu.readLinks(); + expect(links.map((link) => link.text)).to.contain('Stack Management'); + await PageObjects.common.navigateToApp('management'); + const sections = await managementMenu.getSections(); + expect(sections).to.have.length(1); + expect(sections[0]).to.eql({ + sectionId: 'kibana', + sectionLinks: ['search_sessions'], + }); + }); + }); + }); +} diff --git a/x-pack/test/tsconfig.json b/x-pack/test/tsconfig.json index 6209503e75610..4b56ebc83d989 100644 --- a/x-pack/test/tsconfig.json +++ b/x-pack/test/tsconfig.json @@ -40,6 +40,7 @@ { "path": "../../src/plugins/usage_collection/tsconfig.json" }, { "path": "../plugins/actions/tsconfig.json" }, { "path": "../plugins/alerts/tsconfig.json" }, + { "path": "../plugins/banners/tsconfig.json" }, { "path": "../plugins/beats_management/tsconfig.json" }, { "path": "../plugins/cloud/tsconfig.json" }, { "path": "../plugins/code/tsconfig.json" }, diff --git a/x-pack/test/ui_capabilities/common/fixtures/plugins/foo_plugin/kibana.json b/x-pack/test/ui_capabilities/common/fixtures/plugins/foo_plugin/kibana.json index cec1640fbb047..912cf5d70e16b 100644 --- a/x-pack/test/ui_capabilities/common/fixtures/plugins/foo_plugin/kibana.json +++ b/x-pack/test/ui_capabilities/common/fixtures/plugins/foo_plugin/kibana.json @@ -1,5 +1,5 @@ { - "id": "foo_plugin", + "id": "fooPlugin", "version": "1.0.0", "kibanaVersion": "kibana", "requiredPlugins": ["features"], diff --git a/x-pack/test/usage_collection/plugins/stack_management_usage_test/kibana.json b/x-pack/test/usage_collection/plugins/stack_management_usage_test/kibana.json index b586de3fa4d79..c41fe744ca946 100644 --- a/x-pack/test/usage_collection/plugins/stack_management_usage_test/kibana.json +++ b/x-pack/test/usage_collection/plugins/stack_management_usage_test/kibana.json @@ -1,8 +1,8 @@ { - "id": "StackManagementUsageTest", + "id": "stackManagementUsageTest", "version": "1.0.0", "kibanaVersion": "kibana", - "configPath": ["xpack", "StackManagementUsageTest"], + "configPath": ["xpack", "stackManagementUsageTest"], "requiredPlugins": [], "server": false, "ui": true diff --git a/x-pack/tsconfig.json b/x-pack/tsconfig.json index 5589c62010db1..2c475083b589a 100644 --- a/x-pack/tsconfig.json +++ b/x-pack/tsconfig.json @@ -1,68 +1,24 @@ { "extends": "../tsconfig.base.json", - "include": ["mocks.ts", "typings/**/*", "plugins/**/*", "tasks/**/*"], + "include": [ + "mocks.ts", + "typings/**/*", + "tasks/**/*", + "plugins/apm/**/*", + "plugins/case/**/*", + "plugins/lists/**/*", + "plugins/logstash/**/*", + "plugins/monitoring/**/*", + "plugins/security_solution/**/*", + "plugins/xpack_legacy/**/*", + "plugins/drilldowns/url_drilldown/**/*" + ], "exclude": [ - "plugins/actions/**/*", - "plugins/alerts/**/*", + "test/**/*", "plugins/apm/e2e/cypress/**/*", "plugins/apm/ftr_e2e/**/*", "plugins/apm/scripts/**/*", - "plugins/canvas/**/*", - "plugins/console_extensions/**/*", - "plugins/code/**/*", - "plugins/data_enhanced/**/*", - "plugins/discover_enhanced/**/*", - "plugins/dashboard_mode/**/*", - "plugins/dashboard_enhanced/**/*", - "plugins/fleet/**/*", - "plugins/global_search/**/*", - "plugins/global_search_providers/**/*", - "plugins/graph/**/*", - "plugins/features/**/*", - "plugins/file_upload/**/*", - "plugins/embeddable_enhanced/**/*", - "plugins/event_log/**/*", - "plugins/enterprise_search/**/*", - "plugins/infra/**/*", - "plugins/licensing/**/*", - "plugins/lens/**/*", - "plugins/maps/**/*", - "plugins/maps_legacy_licensing/**/*", - "plugins/ml/**/*", - "plugins/observability/**/*", - "plugins/osquery/**/*", - "plugins/reporting/**/*", - "plugins/searchprofiler/**/*", - "plugins/security_solution/cypress/**/*", - "plugins/task_manager/**/*", - "plugins/telemetry_collection_xpack/**/*", - "plugins/transform/**/*", - "plugins/translations/**/*", - "plugins/triggers_actions_ui/**/*", - "plugins/ui_actions_enhanced/**/*", - "plugins/spaces/**/*", - "plugins/security/**/*", - "plugins/stack_alerts/**/*", - "plugins/encrypted_saved_objects/**/*", - "plugins/beats_management/**/*", - "plugins/cloud/**/*", - "plugins/saved_objects_tagging/**/*", - "plugins/global_search_bar/**/*", - "plugins/ingest_pipelines/**/*", - "plugins/license_management/**/*", - "plugins/snapshot_restore/**/*", - "plugins/painless_lab/**/*", - "plugins/watcher/**/*", - "plugins/runtime_fields/**/*", - "plugins/index_management/**/*", - "plugins/grokdebugger/**/*", - "plugins/upgrade_assistant/**/*", - "plugins/rollup/**/*", - "plugins/remote_clusters/**/*", - "plugins/cross_cluster_replication/**/*", - "plugins/index_lifecycle_management/**/*", - "plugins/uptime/**/*", - "test/**/*" + "plugins/security_solution/cypress/**/*" ], "compilerOptions": { // overhead is too significant @@ -120,6 +76,7 @@ { "path": "./plugins/event_log/tsconfig.json" }, { "path": "./plugins/features/tsconfig.json" }, { "path": "./plugins/file_upload/tsconfig.json" }, + { "path": "./plugins/fleet/tsconfig.json" }, { "path": "./plugins/global_search_bar/tsconfig.json" }, { "path": "./plugins/global_search_providers/tsconfig.json" }, { "path": "./plugins/global_search/tsconfig.json" }, diff --git a/yarn.lock b/yarn.lock index 73dedcce12480..22935195ec061 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2146,10 +2146,10 @@ dependencies: object-hash "^1.3.0" -"@elastic/charts@24.4.0": - version "24.4.0" - resolved "https://registry.yarnpkg.com/@elastic/charts/-/charts-24.4.0.tgz#217f55540f48a8f59c49250781d99c36110b2544" - integrity sha512-8dxDEs0g1mV4MjPgIArAmdDQDKjH8EitCLh8/Rouv8kkxvdXnL86VkSHpUbZNK9zPAecArwHBSkyCBZNmbqT2A== +"@elastic/charts@24.5.1": + version "24.5.1" + resolved "https://registry.yarnpkg.com/@elastic/charts/-/charts-24.5.1.tgz#4757721b0323b15412c92d696dd76fdef9b963f8" + integrity sha512-eHJna3xyHREaSfTRb+3/34EmyoINopH6yP9KReakXRb0jW8DD4n9IkbPFwpVN3uXQ6ND2x1ObA0ZzLPSLCPozg== dependencies: "@popperjs/core" "^2.4.0" chroma-js "^2.1.0" @@ -2161,7 +2161,6 @@ d3-scale "^1.0.7" d3-shape "^1.3.4" newtype-ts "^0.2.4" - path2d-polyfill "^0.4.2" prop-types "^15.7.2" re-reselect "^3.4.0" react-redux "^7.1.0" @@ -22841,11 +22840,6 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== -path2d-polyfill@^0.4.2: - version "0.4.2" - resolved "https://registry.yarnpkg.com/path2d-polyfill/-/path2d-polyfill-0.4.2.tgz#594d3103838ef6b9dd4a7fd498fe9a88f1f28531" - integrity sha512-JSeAnUfkFjl+Ml/EZL898ivMSbGHrOH63Mirx5EQ1ycJiryHDmj1Q7Are+uEPvenVGCUN9YbolfGfyUewJfJEg== - pathval@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.1.tgz#8534e77a77ce7ac5a2512ea21e0fdb8fcf6c3d8d"