-
Notifications
You must be signed in to change notification settings - Fork 891
/
dataset_manager.ts
77 lines (65 loc) · 1.99 KB
/
dataset_manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { BehaviorSubject } from 'rxjs';
import { skip } from 'rxjs/operators';
import { CoreStart } from 'opensearch-dashboards/public';
import {
IndexPatternsService,
SIMPLE_DATA_SET_TYPES,
SimpleDataSet,
SimpleDataSource,
} from '../../../common';
export class DataSetManager {
private dataSet$: BehaviorSubject<SimpleDataSet | undefined>;
private indexPatterns?: IndexPatternsService;
constructor(private readonly uiSettings: CoreStart['uiSettings']) {
this.dataSet$ = new BehaviorSubject<SimpleDataSet | undefined>(undefined);
}
public init = (indexPatterns: IndexPatternsService) => {
this.indexPatterns = indexPatterns;
};
public getUpdates$ = () => {
return this.dataSet$.asObservable().pipe(skip(1));
};
public getDataSet = () => {
return this.dataSet$.getValue();
};
/**
* Updates the query.
* @param {Query} query
*/
public setDataSet = (dataSet: SimpleDataSet | undefined) => {
this.dataSet$.next(dataSet);
};
public getDefaultDataSet = async (): Promise<SimpleDataSet | undefined> => {
const defaultIndexPatternId = await this.uiSettings.get('defaultIndex');
if (!defaultIndexPatternId) {
return undefined;
}
const indexPattern = await this.indexPatterns?.get(defaultIndexPatternId);
if (!indexPattern) {
return undefined;
}
if (!indexPattern.id) {
return undefined;
}
return {
id: indexPattern.id,
title: indexPattern.title,
type: SIMPLE_DATA_SET_TYPES.INDEX_PATTERN,
timeFieldName: indexPattern.timeFieldName,
...(indexPattern.dataSourceRef
? {
dataSourceRef: {
id: indexPattern.dataSourceRef?.id,
name: indexPattern.dataSourceRef?.name,
type: indexPattern.dataSourceRef?.type,
} as SimpleDataSource,
}
: {}),
};
};
}
export type DataSetContract = PublicMethodsOf<DataSetManager>;