Skip to content

Commit

Permalink
Merge branch 'main' into 116443/experimental-flag-style-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Nov 3, 2021
2 parents 4fc617e + 6c4373f commit b6fd3c8
Show file tree
Hide file tree
Showing 33 changed files with 432 additions and 193 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
/src/core/ @elastic/kibana-core
/src/plugins/saved_objects_tagging_oss @elastic/kibana-core
/config/kibana.yml @elastic/kibana-core
/typings/ @elastic/kibana-core
/x-pack/plugins/banners/ @elastic/kibana-core
/x-pack/plugins/features/ @elastic/kibana-core
/x-pack/plugins/licensing/ @elastic/kibana-core
Expand Down
9 changes: 6 additions & 3 deletions x-pack/examples/reporting_example/public/containers/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ import { BrowserRouter as Router, useHistory } from 'react-router-dom';
import * as Rx from 'rxjs';
import { takeWhile } from 'rxjs/operators';
import { ScreenshotModePluginSetup } from 'src/plugins/screenshot_mode/public';
import type {
JobAppParamsPDF,
JobParamsPDFV2,
JobParamsPNGV2,
} from '../../../../plugins/reporting/public';
import { constants, ReportingStart } from '../../../../plugins/reporting/public';
import type { JobParamsPDFV2 } from '../../../../plugins/reporting/server/export_types/printable_pdf_v2/types';
import type { JobParamsPNGV2 } from '../../../../plugins/reporting/server/export_types/png_v2/types';

import { REPORTING_EXAMPLE_LOCATOR_ID } from '../../common';

Expand Down Expand Up @@ -81,7 +84,7 @@ export const Main = ({ basename, reporting, screenshotMode }: ReportingExampleAp
});
});

const getPDFJobParamsDefault = () => {
const getPDFJobParamsDefault = (): JobAppParamsPDF => {
return {
layout: {
id: constants.LAYOUT_TYPES.PRESERVE_LAYOUT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import Style from 'style-it';
// @ts-expect-error
import { WorkpadPage } from '../workpad_page';
import { Fullscreen } from '../fullscreen';
import { HEADER_BANNER_HEIGHT, WORKPAD_CANVAS_BUFFER } from '../../../common/lib/constants';
import {
HEADER_BANNER_HEIGHT,
WORKPAD_CANVAS_BUFFER,
DEFAULT_WORKPAD_CSS,
} from '../../../common/lib/constants';
import { CommitFn, CanvasPage } from '../../../types';
import { WorkpadShortcuts } from './workpad_shortcuts.component';

Expand Down Expand Up @@ -122,7 +126,7 @@ export const Workpad: FC<Props> = ({

// NOTE: the data-shared-* attributes here are used for reporting
return Style.it(
workpadCss,
workpadCss || DEFAULT_WORKPAD_CSS,
<div
className={`canvasWorkpad ${isFullscreenProp ? 'fullscreen' : ''}`}
style={fsStyle}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { RedirectOptions } from 'src/plugins/share/public';
import { CANVAS_APP_LOCATOR } from '../../../../common/locator';
import { CanvasAppLocatorParams } from '../../../../common/locator';
import { CanvasWorkpad } from '../../../../types';
import { JobAppParamsPDFV2 } from '../../../../../reporting/public';

export interface CanvasWorkpadSharingData {
workpad: Pick<CanvasWorkpad, 'id' | 'name' | 'height' | 'width'>;
Expand All @@ -18,7 +19,7 @@ export interface CanvasWorkpadSharingData {
export function getPdfJobParams(
{ workpad: { id, name: title, width, height }, pageCount }: CanvasWorkpadSharingData,
version: string
) {
): JobAppParamsPDFV2 {
// The viewport in Reporting by specifying the dimensions. In order for things to work,
// we need a viewport that will include all of the pages in the workpad. The viewport
// also needs to include any offset values from the 0,0 position, otherwise the cropped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,8 @@ export const AddSourceLogic = kea<MakeLogicType<AddSourceValues, AddSourceAction
navigateToUrl(getSourcesPath(SOURCES_PATH, isOrganization));
}
} catch (e) {
flashAPIErrors(e);
navigateToUrl(getSourcesPath(SOURCES_PATH, isOrganization));
flashAPIErrors(e);
}
},
createContentSource: async ({ serviceType, successCallback, errorCallback }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ import {

import styled from 'styled-components';

import type {
AgentPolicy,
PackageInfo,
PackagePolicy,
NewPackagePolicy,
RegistryVarsEntry,
} from '../../../types';
import type { AgentPolicy, PackageInfo, NewPackagePolicy, RegistryVarsEntry } from '../../../types';
import { packageToPackagePolicy, pkgKeyFromPackageInfo } from '../../../services';
import { Loading } from '../../../components';
import { useStartServices } from '../../../hooks';
import { useStartServices, useGetPackagePolicies } from '../../../hooks';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../../../../constants';
import { SO_SEARCH_LIMIT } from '../../../../../../common';

import { isAdvancedVar } from './services';
import type { PackagePolicyValidationResults } from './services';
Expand Down Expand Up @@ -65,6 +61,14 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
submitAttempted,
}) => {
const { docLinks } = useStartServices();

// Fetch all packagePolicies having the package name
const { data: packagePolicyData, isLoading: isLoadingPackagePolicies } = useGetPackagePolicies({
perPage: SO_SEARCH_LIMIT,
page: 1,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${packageInfo.name}`,
});

// Form show/hide states
const [isShowingAdvanced, setIsShowingAdvanced] = useState<boolean>(false);

Expand All @@ -84,33 +88,37 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{

// Update package policy's package and agent policy info
useEffect(() => {
if (isLoadingPackagePolicies) {
return;
}
const pkg = packagePolicy.package;
const currentPkgKey = pkg ? pkgKeyFromPackageInfo(pkg) : '';
const pkgKey = pkgKeyFromPackageInfo(packageInfo);

// If package has changed, create shell package policy with input&stream values based on package info
if (currentPkgKey !== pkgKey) {
// Existing package policies on the agent policy using the package name, retrieve highest number appended to package policy name
// Retrieve highest number appended to package policy name and increment it by one
const pkgPoliciesNamePattern = new RegExp(`${packageInfo.name}-(\\d+)`);
const pkgPoliciesWithMatchingNames = agentPolicy
? (agentPolicy.package_policies as PackagePolicy[])
const pkgPoliciesWithMatchingNames = packagePolicyData?.items
? packagePolicyData.items
.filter((ds) => Boolean(ds.name.match(pkgPoliciesNamePattern)))
.map((ds) => parseInt(ds.name.match(pkgPoliciesNamePattern)![1], 10))
.sort((a, b) => a - b)
: [];

const incrementedName = `${packageInfo.name}-${
pkgPoliciesWithMatchingNames.length
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
: 1
}`;

updatePackagePolicy(
packageToPackagePolicy(
packageInfo,
agentPolicy?.id || '',
packagePolicy.output_id,
packagePolicy.namespace,
packagePolicy.name ||
`${packageInfo.name}-${
pkgPoliciesWithMatchingNames.length
? pkgPoliciesWithMatchingNames[pkgPoliciesWithMatchingNames.length - 1] + 1
: 1
}`,
packagePolicy.name || incrementedName,
packagePolicy.description,
integrationToEnable
)
Expand All @@ -124,7 +132,15 @@ export const StepDefinePackagePolicy: React.FunctionComponent<{
namespace: agentPolicy.namespace,
});
}
}, [packagePolicy, agentPolicy, packageInfo, updatePackagePolicy, integrationToEnable]);
}, [
packagePolicy,
agentPolicy,
packageInfo,
updatePackagePolicy,
integrationToEnable,
packagePolicyData,
isLoadingPackagePolicies,
]);

return validationResults ? (
<FormGroupResponsiveFields
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,16 @@ class AgentPolicyService {
options
);

// Copy all package policies
// Copy all package policies and append (copy) to their names
if (baseAgentPolicy.package_policies.length) {
const newPackagePolicies = (baseAgentPolicy.package_policies as PackagePolicy[]).map(
(packagePolicy: PackagePolicy) => {
const { id: packagePolicyId, version, ...newPackagePolicy } = packagePolicy;
return newPackagePolicy;
const updatedPackagePolicy = {
...newPackagePolicy,
name: `${packagePolicy.name} (copy)`,
};
return updatedPackagePolicy;
}
);
await packagePolicyService.bulkCreate(
Expand Down
51 changes: 18 additions & 33 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import type {
} from '../../common';
import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../constants';
import {
HostedAgentPolicyRestrictionRelatedError,
IngestManagerError,
ingestErrorToResponseOptions,
PackagePolicyIneligibleForUpgradeError,
Expand Down Expand Up @@ -99,24 +98,14 @@ class PackagePolicyService {
skipEnsureInstalled?: boolean;
}
): Promise<PackagePolicy> {
// Check that its agent policy does not have a package policy with the same name
const parentAgentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id);
if (!parentAgentPolicy) {
throw new Error('Agent policy not found');
}
if (parentAgentPolicy.is_managed && !options?.force) {
throw new HostedAgentPolicyRestrictionRelatedError(
`Cannot add integrations to hosted agent policy ${parentAgentPolicy.id}`
);
}
if (
(parentAgentPolicy.package_policies as PackagePolicy[]).find(
(siblingPackagePolicy) => siblingPackagePolicy.name === packagePolicy.name
)
) {
throw new IngestManagerError(
'There is already a package with the same name on this agent policy'
);
const existingPoliciesWithName = await this.list(soClient, {
perPage: 1,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: "${packagePolicy.name}"`,
});

// Check that the name does not exist already
if (existingPoliciesWithName.items.length > 0) {
throw new IngestManagerError('There is already a package with the same name');
}
let elasticsearch: PackagePolicy['elasticsearch'];
// Add ids to stream
Expand Down Expand Up @@ -320,12 +309,12 @@ class PackagePolicyService {
});

return {
items: packagePolicies.saved_objects.map((packagePolicySO) => ({
items: packagePolicies?.saved_objects.map((packagePolicySO) => ({
id: packagePolicySO.id,
version: packagePolicySO.version,
...packagePolicySO.attributes,
})),
total: packagePolicies.total,
total: packagePolicies?.total,
page,
perPage,
};
Expand Down Expand Up @@ -369,19 +358,15 @@ class PackagePolicyService {
if (!oldPackagePolicy) {
throw new Error('Package policy not found');
}
// Check that the name does not exist already but exclude the current package policy
const existingPoliciesWithName = await this.list(soClient, {
perPage: 1,
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: "${packagePolicy.name}"`,
});
const filtered = (existingPoliciesWithName?.items || []).filter((p) => p.id !== id);

// Check that its agent policy does not have a package policy with the same name
const parentAgentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id);
if (!parentAgentPolicy) {
throw new Error('Agent policy not found');
}
if (
(parentAgentPolicy.package_policies as PackagePolicy[]).find(
(siblingPackagePolicy) =>
siblingPackagePolicy.id !== id && siblingPackagePolicy.name === packagePolicy.name
)
) {
throw new Error('There is already a package with the same name on this agent policy');
if (filtered.length > 0) {
throw new IngestManagerError('There is already a package with the same name');
}

let inputs = restOfPackagePolicy.inputs.map((input) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,31 @@ export function modelsProvider(
modelIds.map((id: string) => [id, null])
);

const { body, statusCode } = await client.asCurrentUser.ingest.getPipeline();

if (statusCode !== 200) {
return modelIdsMap;
}

for (const [pipelineName, pipelineDefinition] of Object.entries(body)) {
const { processors } = pipelineDefinition as { processors: Array<Record<string, any>> };

for (const processor of processors) {
const id = processor.inference?.model_id;
if (modelIdsMap.has(id)) {
const obj = modelIdsMap.get(id);
if (obj === null) {
modelIdsMap.set(id, { [pipelineName]: pipelineDefinition });
} else {
obj![pipelineName] = pipelineDefinition;
try {
const { body } = await client.asCurrentUser.ingest.getPipeline();

for (const [pipelineName, pipelineDefinition] of Object.entries(body)) {
const { processors } = pipelineDefinition as { processors: Array<Record<string, any>> };

for (const processor of processors) {
const id = processor.inference?.model_id;
if (modelIdsMap.has(id)) {
const obj = modelIdsMap.get(id);
if (obj === null) {
modelIdsMap.set(id, { [pipelineName]: pipelineDefinition });
} else {
obj![pipelineName] = pipelineDefinition;
}
}
}
}
} catch (error) {
if (error.statusCode === 404) {
// ES returns 404 when there are no pipelines
// Instead, we should return the modelIdsMap and a 200
return modelIdsMap;
}
throw error;
}

return modelIdsMap;
Expand Down
29 changes: 29 additions & 0 deletions x-pack/plugins/reporting/common/types/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { Ensure, SerializableRecord } from '@kbn/utility-types';
import type { LayoutParams } from './layout';

export type JobId = string;

export type BaseParams = Ensure<
{
layout?: LayoutParams;
objectType: string;
title: string;
browserTimezone: string; // to format dates in the user's time zone
version: string; // to handle any state migrations
},
SerializableRecord
>;

// base params decorated with encrypted headers that come into runJob functions
export interface BasePayload extends BaseParams {
headers: string;
spaceId?: string;
isDeprecated?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* 2.0.
*/

import type { FieldSpec } from 'src/plugins/data/common';
import { BaseParams, BasePayload } from '../../types';
import { BaseParams, BasePayload } from '../base';

export type RawValue = string | object | null | undefined;

Expand Down Expand Up @@ -57,16 +56,6 @@ export interface SearchRequestDeprecatedCSV {
| any;
}

type FormatsMapDeprecatedCSV = Map<
string,
{
id: string;
params: {
pattern: string;
};
}
>;

export interface SavedSearchGeneratorResultDeprecatedCSV {
maxSizeReached: boolean;
csvContainsFormulas?: boolean;
Expand Down
Loading

0 comments on commit b6fd3c8

Please sign in to comment.