-
Notifications
You must be signed in to change notification settings - Fork 88
/
helpers.ts
71 lines (64 loc) · 2.91 KB
/
helpers.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import _ from "lodash";
import { ExplainAPIManagedIndexMetaData, QueryStringQuery } from "../models/interfaces";
import { MatchAllQuery } from "../models/types";
import { ManagedIndexMetaData } from "../../models/interfaces";
export function transformManagedIndexMetaData(metaData: ExplainAPIManagedIndexMetaData | undefined): ManagedIndexMetaData | null {
if (!metaData) return null;
// If this is not a managed index or we are still initializing we still return the
// plugins.index_state_management.policy_id setting, but nothing else from the explain API
if (!metaData.index) return null;
return {
index: metaData.index,
// We know indexUuid and policyName exist if index exists
indexUuid: metaData.index_uuid as string,
policyId: metaData.policy_id as string,
policySeqNo: metaData.policy_seq_no,
policyPrimaryTerm: metaData.policy_primary_term,
policyCompleted: metaData.policy_completed,
rolledOver: metaData.rolled_over,
transitionTo: metaData.transition_to,
state: metaData.state ? { name: metaData.state.name, startTime: metaData.state.start_time } : undefined,
action: metaData.action
? {
name: metaData.action.name,
startTime: metaData.action.start_time,
index: metaData.action.index,
failed: metaData.action.failed,
consumedRetries: metaData.action.consumed_retries,
}
: undefined,
retryInfo: metaData.retry_info
? { failed: metaData.retry_info.failed, consumedRetries: metaData.retry_info.consumed_retries }
: undefined,
info: metaData.info,
};
}
export function getMustQuery<T extends string>(field: T, search: string): MatchAllQuery | QueryStringQuery<T> {
const str = search.trim();
if (search.trim()) {
return {
query_string: {
default_field: field,
default_operator: "AND",
query: str ? `*${str.split(" ").join("* *")}*` : "*",
},
};
}
return { match_all: {} };
}
export function getSearchString(terms?: string[], indices?: string[], dataStreams?: string[], showDataStreams: boolean = true): string {
// Terms are searched with a wildcard around them.
const searchTerms = terms ? `*${_.castArray(terms).join("*,*")}*` : "";
// Indices and data streams are searched with an exact match.
const searchIndices = indices ? _.castArray(indices).join(",") : "";
const searchDataStreams = dataStreams ? _.castArray(dataStreams).join(",") : "";
// The overall search string is a combination of terms, indices, and data streams.
// If the search string is blank, then '*' is used to match everything.
const resolved = [searchTerms, searchIndices, searchDataStreams].filter((value) => value !== "").join(",") || "*";
// We don't want to fetch managed datastream indices if there are not selected by caller.
return showDataStreams ? resolved : resolved + " -.ds"
}