-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Logs+] Enforce dataset names #166654
Merged
Merged
[Logs+] Enforce dataset names #166654
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48e146f
Enforce dataset name validation (client and server side)
Kerry350 105b6bb
Remove formatting from previous approach
Kerry350 2b987f3
Merge remote-tracking branch 'upstream/main' into 163830-enforce-data…
Kerry350 2032b22
Merge branch 'main' into 163830-enforce-dataset-names
kibanamachine f70e1c9
Amend Cypress test
Kerry350 565d70b
Revert "Amend Cypress test"
Kerry350 7a1b2d4
Amend Cypress test
Kerry350 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
packages/kbn-custom-integrations/src/state_machines/create/pipelines/fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 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 { pipe } from 'fp-ts/lib/pipeable'; | ||
import { replaceSpecialChars } from '../../../components/create/utils'; | ||
import { CreateCustomIntegrationContext, UpdateFieldsEvent, WithTouchedFields } from '../types'; | ||
|
||
type ValuesTuple = [CreateCustomIntegrationContext, UpdateFieldsEvent]; | ||
|
||
// Pipeline for updating the fields and touchedFields properties within context | ||
export const executeFieldsPipeline = ( | ||
context: CreateCustomIntegrationContext, | ||
event: UpdateFieldsEvent | ||
) => { | ||
return pipe( | ||
[context, event] as ValuesTuple, | ||
updateFields(context), | ||
updateTouchedFields(context), | ||
maybeMatchDatasetNameToIntegrationName(context), | ||
replaceSpecialCharacters(context) | ||
); | ||
}; | ||
|
||
const updateFields = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
|
||
const mergedContext = { | ||
...context, | ||
fields: { | ||
...context.fields, | ||
...event.fields, | ||
}, | ||
}; | ||
return [mergedContext, event]; | ||
}; | ||
|
||
const updateTouchedFields = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
|
||
const mergedContext = { | ||
...context, | ||
touchedFields: { | ||
...context.touchedFields, | ||
...Object.keys(event.fields).reduce<WithTouchedFields['touchedFields']>( | ||
(acc, field) => ({ ...acc, [field]: true }), | ||
{} as WithTouchedFields['touchedFields'] | ||
), | ||
}, | ||
}; | ||
return [mergedContext, event]; | ||
}; | ||
|
||
const maybeMatchDatasetNameToIntegrationName = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
if (context.touchedFields.integrationName && !context.touchedFields.datasets) { | ||
return [ | ||
{ | ||
...context, | ||
fields: { | ||
...context.fields, | ||
datasets: context.fields.datasets.map((dataset, index) => ({ | ||
...dataset, | ||
name: index === 0 ? context.fields.integrationName : dataset.name, | ||
})), | ||
}, | ||
}, | ||
event, | ||
]; | ||
} else { | ||
return [context, event]; | ||
} | ||
}; | ||
|
||
const replaceSpecialCharacters = | ||
(originalContext: CreateCustomIntegrationContext) => | ||
(values: ValuesTuple): ValuesTuple => { | ||
const [context, event] = values; | ||
|
||
const mergedContext = { | ||
...context, | ||
fields: { | ||
...context.fields, | ||
integrationName: replaceSpecialChars(context.fields.integrationName), | ||
datasets: context.fields.datasets.map((dataset) => ({ | ||
...dataset, | ||
name: replaceSpecialChars(dataset.name), | ||
})), | ||
}, | ||
}; | ||
|
||
return [mergedContext, event]; | ||
}; | ||
|
||
export const getDatasetNamePrefix = (integrationName: string) => `${integrationName}.`; | ||
export const datasetNameIsPrefixed = (datasetName: string, integrationName: string) => | ||
datasetName.startsWith(getDatasetNamePrefix(integrationName)); | ||
export const datasetNameWillBePrefixed = (datasetName: string, integrationName: string) => | ||
datasetName !== integrationName; | ||
export const prefixDatasetName = (datasetName: string, integrationName: string) => | ||
`${getDatasetNamePrefix(integrationName)}${datasetName}`; | ||
|
||
// The content after the integration name prefix. | ||
export const getDatasetNameWithoutPrefix = (datasetName: string, integrationName: string) => | ||
datasetNameIsPrefixed(datasetName, integrationName) | ||
? datasetName.split(getDatasetNamePrefix(integrationName))[1] | ||
: datasetName; | ||
|
||
// The machine holds unprefixed names internally to dramatically reduce complexity and improve performance for input changes in the UI. | ||
// Prefixed names are used at the outermost edges e.g. when providing initial state and submitting to the API. | ||
export const normalizeDatasetNames = (fields: UpdateFieldsEvent['fields']) => { | ||
const value = { | ||
...fields, | ||
...(fields.datasets !== undefined && fields.integrationName !== undefined | ||
? { | ||
datasets: fields.datasets.map((dataset) => ({ | ||
...dataset, | ||
name: getDatasetNameWithoutPrefix(dataset.name, fields.integrationName!), | ||
})), | ||
} | ||
: {}), | ||
}; | ||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏