Skip to content
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

[Fleet] show dataset combo box for input packages #147015

Merged
merged 7 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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, useState } from 'react';
import { EuiComboBox } from '@elastic/eui';
import { i18n } from '@kbn/i18n';

export const DatasetComboBox: React.FC<{
value: any;
onChange: (newValue: any) => void;
datasets: string[];
}> = ({ value, onChange, datasets }) => {
const datasetOptions = datasets.map((dataset: string) => ({ label: dataset })) ?? [];
const defaultOption = 'generic';
const [selectedOptions, setSelectedOptions] = useState<Array<{ label: string }>>([
{
label: value ?? defaultOption,
},
]);

useEffect(() => {
if (!value) onChange(defaultOption);
}, [value, defaultOption, onChange]);

const onDatasetChange = (newSelectedOptions: Array<{ label: string }>) => {
setSelectedOptions(newSelectedOptions);
onChange(newSelectedOptions[0]?.label);
};

const onCreateOption = (searchValue: string = '') => {
const normalizedSearchValue = searchValue.trim().toLowerCase();
if (!normalizedSearchValue) {
return;
}
const newOption = {
label: searchValue,
};
setSelectedOptions([newOption]);
onChange(searchValue);
};

return (
<EuiComboBox
aria-label={i18n.translate('xpack.fleet.datasetCombo.ariaLabel', {
defaultMessage: 'Dataset combo box',
})}
placeholder={i18n.translate('xpack.fleet.datasetCombo.placeholder', {
defaultMessage: 'Select a dataset',
})}
singleSelection={{ asPlainText: true }}
options={datasetOptions}
selectedOptions={selectedOptions}
onCreateOption={onCreateOption}
onChange={onDatasetChange}
customOptionText={i18n.translate('xpack.fleet.datasetCombo.customOptionText', {
defaultMessage: 'Add {searchValue} as a custom option',
values: { searchValue: '{searchValue}' },
})}
isClearable={false}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 { orderDatasets } from './order_datasets';

describe('orderDatasets', () => {
it('should move datasets up that match name', () => {
const datasets = orderDatasets(
['system.memory', 'elastic_agent', 'elastic_agent.filebeat', 'system.cpu'],
'elastic_agent'
);

expect(datasets).toEqual([
'elastic_agent',
'elastic_agent.filebeat',
'system.cpu',
'system.memory',
]);
});

it('should order alphabetically if name does not match', () => {
const datasets = orderDatasets(['system.memory', 'elastic_agent'], 'nginx');

expect(datasets).toEqual(['elastic_agent', 'system.memory']);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { partition } from 'lodash';

export function orderDatasets(datasetList: string[], name: string): string[] {
const [relevantDatasets, otherDatasets] = partition(datasetList.sort(), (record) =>
record.startsWith(name)
);
const datasets = relevantDatasets.concat(otherDatasets);
return datasets;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import React, { useState, Fragment, memo, useMemo, useEffect, useRef, useCallback } from 'react';
import ReactMarkdown from 'react-markdown';
import styled from 'styled-components';
import { uniq } from 'lodash';
import { FormattedMessage } from '@kbn/i18n-react';
import {
EuiFlexGrid,
Expand All @@ -22,6 +23,8 @@ import {
} from '@elastic/eui';
import { useRouteMatch } from 'react-router-dom';

import { useGetDataStreams } from '../../../../../../../../hooks';

import { mapPackageReleaseToIntegrationCardRelease } from '../../../../../../../../services/package_prerelease';

import { getRegistryDataStreamAssetBaseName } from '../../../../../../../../../common/services';
Expand All @@ -41,6 +44,7 @@ import { PackagePolicyEditorDatastreamMappings } from '../../datastream_mappings

import { PackagePolicyInputVarField } from './package_policy_input_var_field';
import { useDataStreamId } from './hooks';
import { orderDatasets } from './order_datasets';

const ScrollAnchor = styled.div`
display: none;
Expand Down Expand Up @@ -175,6 +179,11 @@ export const PackagePolicyInputStreamConfig = memo<Props>(
});
};

const { data: dataStreamsData } = useGetDataStreams();
const datasetList =
uniq(dataStreamsData?.data_streams.map((dataStream) => dataStream.dataset)) ?? [];
const datasets = orderDatasets(datasetList, packageInfo.name);

return (
<>
<EuiFlexGrid columns={2}>
Expand Down Expand Up @@ -252,6 +261,8 @@ export const PackagePolicyInputStreamConfig = memo<Props>(
}}
errors={inputStreamValidationResults?.vars![varName]}
forceShowErrors={forceShowErrors}
packageType={packageInfo.type}
datasets={datasets}
/>
</EuiFlexItem>
);
Expand Down Expand Up @@ -311,6 +322,8 @@ export const PackagePolicyInputStreamConfig = memo<Props>(
}}
errors={inputStreamValidationResults?.vars![varName]}
forceShowErrors={forceShowErrors}
packageType={packageInfo.type}
datasets={datasets}
/>
</EuiFlexItem>
);
Expand Down
Loading