Skip to content

Commit

Permalink
[Logs+] Aggregate unmanaged datasets (elastic#162144)
Browse files Browse the repository at this point in the history
## 📓 Summary

Closes elastic#162061 

This implementation updates and aggregates the unmanaged datasets once
they are retrieved by the state machine on initialization.

I implemented this step in the state machine against doing it on the
dataset service to keep the service pure and able to serve the whole
response in case we need to use it on other plugins/use cases.


https://github.com/elastic/kibana/assets/34506779/a5883521-6dd7-4291-9b90-0cd7665bed65

---------

Co-authored-by: Marco Antonio Ghiani <[email protected]>
  • Loading branch information
2 people authored and Devon Thomson committed Aug 1, 2023
1 parent d9b4e1b commit 4916f23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IndexPattern } from '@kbn/io-ts-utils';
import { DatasetId, DatasetType, IntegrationType } from '../types';

type IntegrationBase = Pick<IntegrationType, 'name' | 'title' | 'icons' | 'version'>;

interface DatasetDeps extends DatasetType {
iconType?: IconType;
}
Expand Down Expand Up @@ -41,6 +42,12 @@ export class Dataset {
: this.title;
}

getDatasetWildcard(): IndexPattern {
const [type, dataset, _namespace] = this.name.split('-');

return `${type}-${dataset}-*` as IndexPattern;
}

toDataviewSpec(): DataViewSpec {
// Invert the property because the API returns the index pattern as `name` and a readable name as `title`
return {
Expand Down Expand Up @@ -68,4 +75,15 @@ export class Dataset {
iconType: 'editorChecklist',
});
}

public static createWildcardDatasetsFrom(datasets: Dataset[]) {
// Gather unique list of wildcards
const wildcards = datasets.reduce(
(list, dataset) => list.add(dataset.getDatasetWildcard()),
new Set<IndexPattern>()
);

// Create new datasets for the retrieved wildcards
return Array.from(wildcards).map((wildcard) => Dataset.create({ name: wildcard }));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { isEmpty, isError, omitBy } from 'lodash';
import { assign, createMachine } from 'xstate';
import { Dataset } from '../../../../common/datasets';
import { IDatasetsClient } from '../../../services/datasets';
import { DEFAULT_CONTEXT } from './defaults';
import type {
Expand Down Expand Up @@ -39,7 +40,7 @@ export const createPureDatasetsStateMachine = (
src: 'loadDatasets',
onDone: {
target: 'loaded',
actions: ['storeInCache', 'storeDatasets', 'storeSearch'],
actions: ['storeInCache', 'aggregateAndStoreDatasets', 'storeSearch'],
},
onError: 'loadingFailed',
},
Expand Down Expand Up @@ -77,8 +78,10 @@ export const createPureDatasetsStateMachine = (
// Store search from search event
...('search' in event && { search: event.search }),
})),
storeDatasets: assign((_context, event) =>
'data' in event && !isError(event.data) ? { datasets: event.data.items } : {}
aggregateAndStoreDatasets: assign((_context, event) =>
'data' in event && !isError(event.data)
? { datasets: Dataset.createWildcardDatasetsFrom(event.data.items) }
: {}
),
storeInCache: (context, event) => {
if ('data' in event && !isError(event.data)) {
Expand Down

0 comments on commit 4916f23

Please sign in to comment.