Skip to content

Commit

Permalink
ordering datasets to move matching names to the top
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Dec 6, 2022
1 parent 983f473 commit 261b8f9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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 Down Expand Up @@ -43,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 @@ -178,7 +180,9 @@ export const PackagePolicyInputStreamConfig = memo<Props>(
};

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

return (
<>
Expand Down

0 comments on commit 261b8f9

Please sign in to comment.