@webiny/api-opensearch 0.0.0-unstable.e6f0dc8ca7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/client.d.ts +8 -0
  4. package/client.js +70 -0
  5. package/client.js.map +1 -0
  6. package/cursors.d.ts +10 -0
  7. package/cursors.js +37 -0
  8. package/cursors.js.map +1 -0
  9. package/db/entity.d.ts +7 -0
  10. package/db/entity.js +20 -0
  11. package/db/entity.js.map +1 -0
  12. package/db/index.d.ts +3 -0
  13. package/db/index.js +5 -0
  14. package/db/index.js.map +1 -0
  15. package/db/table.d.ts +7 -0
  16. package/db/table.js +17 -0
  17. package/db/table.js.map +1 -0
  18. package/db/types.d.ts +6 -0
  19. package/db/types.js +3 -0
  20. package/db/types.js.map +1 -0
  21. package/exports/api/opensearch.d.ts +4 -0
  22. package/exports/api/opensearch.js +6 -0
  23. package/exports/api/opensearch.js.map +1 -0
  24. package/features/OpenSearchClient/OpenSearchClient.d.ts +12 -0
  25. package/features/OpenSearchClient/OpenSearchClient.js +16 -0
  26. package/features/OpenSearchClient/OpenSearchClient.js.map +1 -0
  27. package/features/OpenSearchClient/abstraction.d.ts +9 -0
  28. package/features/OpenSearchClient/abstraction.js +4 -0
  29. package/features/OpenSearchClient/abstraction.js.map +1 -0
  30. package/features/OpenSearchClient/feature.d.ts +4 -0
  31. package/features/OpenSearchClient/feature.js +10 -0
  32. package/features/OpenSearchClient/feature.js.map +1 -0
  33. package/features/OpenSearchClientFactory/OpenSearchClientFactory.d.ts +9 -0
  34. package/features/OpenSearchClientFactory/OpenSearchClientFactory.js +16 -0
  35. package/features/OpenSearchClientFactory/OpenSearchClientFactory.js.map +1 -0
  36. package/features/OpenSearchClientFactory/abstraction.d.ts +10 -0
  37. package/features/OpenSearchClientFactory/abstraction.js +4 -0
  38. package/features/OpenSearchClientFactory/abstraction.js.map +1 -0
  39. package/features/OpenSearchClientFactory/feature.d.ts +4 -0
  40. package/features/OpenSearchClientFactory/feature.js +10 -0
  41. package/features/OpenSearchClientFactory/feature.js.map +1 -0
  42. package/features/OpenSearchContext/OpenSearchContext.d.ts +8 -0
  43. package/features/OpenSearchContext/OpenSearchContext.js +13 -0
  44. package/features/OpenSearchContext/OpenSearchContext.js.map +1 -0
  45. package/features/OpenSearchContext/abstraction.d.ts +14 -0
  46. package/features/OpenSearchContext/abstraction.js +4 -0
  47. package/features/OpenSearchContext/abstraction.js.map +1 -0
  48. package/features/OpenSearchContext/feature.d.ts +5 -0
  49. package/features/OpenSearchContext/feature.js +13 -0
  50. package/features/OpenSearchContext/feature.js.map +1 -0
  51. package/index.d.ts +21 -0
  52. package/index.js +40 -0
  53. package/index.js.map +1 -0
  54. package/indexConfiguration/base.d.ts +6 -0
  55. package/indexConfiguration/base.js +35 -0
  56. package/indexConfiguration/base.js.map +1 -0
  57. package/indexConfiguration/common.d.ts +9 -0
  58. package/indexConfiguration/common.js +44 -0
  59. package/indexConfiguration/common.js.map +1 -0
  60. package/indexConfiguration/index.d.ts +2 -0
  61. package/indexConfiguration/index.js +4 -0
  62. package/indexConfiguration/index.js.map +1 -0
  63. package/indexPrefix.d.ts +1 -0
  64. package/indexPrefix.js +5 -0
  65. package/indexPrefix.js.map +1 -0
  66. package/indices.d.ts +8 -0
  67. package/indices.js +20 -0
  68. package/indices.js.map +1 -0
  69. package/limit.d.ts +1 -0
  70. package/limit.js +32 -0
  71. package/limit.js.map +1 -0
  72. package/normalize.d.ts +6 -0
  73. package/normalize.js +54 -0
  74. package/normalize.js.map +1 -0
  75. package/operations/OpenSearchCatHealth.d.ts +7 -0
  76. package/operations/OpenSearchCatHealth.js +31 -0
  77. package/operations/OpenSearchCatHealth.js.map +1 -0
  78. package/operations/OpenSearchCatNodes.d.ts +7 -0
  79. package/operations/OpenSearchCatNodes.js +29 -0
  80. package/operations/OpenSearchCatNodes.js.map +1 -0
  81. package/operations/index.d.ts +3 -0
  82. package/operations/index.js +5 -0
  83. package/operations/index.js.map +1 -0
  84. package/operations/stripConnectionFromException.d.ts +1 -0
  85. package/operations/stripConnectionFromException.js +17 -0
  86. package/operations/stripConnectionFromException.js.map +1 -0
  87. package/operations/types.d.ts +12 -0
  88. package/operations/types.js +8 -0
  89. package/operations/types.js.map +1 -0
  90. package/operators.d.ts +6 -0
  91. package/operators.js +9 -0
  92. package/operators.js.map +1 -0
  93. package/package.json +40 -0
  94. package/plugins/definition/OpenSearchBodyModifierPlugin.d.ts +13 -0
  95. package/plugins/definition/OpenSearchBodyModifierPlugin.js +18 -0
  96. package/plugins/definition/OpenSearchBodyModifierPlugin.js.map +1 -0
  97. package/plugins/definition/OpenSearchFieldPlugin.d.ts +81 -0
  98. package/plugins/definition/OpenSearchFieldPlugin.js +69 -0
  99. package/plugins/definition/OpenSearchFieldPlugin.js.map +1 -0
  100. package/plugins/definition/OpenSearchIndexPlugin.d.ts +10 -0
  101. package/plugins/definition/OpenSearchIndexPlugin.js +17 -0
  102. package/plugins/definition/OpenSearchIndexPlugin.js.map +1 -0
  103. package/plugins/definition/OpenSearchQueryBuilderOperatorPlugin.d.ts +10 -0
  104. package/plugins/definition/OpenSearchQueryBuilderOperatorPlugin.js +9 -0
  105. package/plugins/definition/OpenSearchQueryBuilderOperatorPlugin.js.map +1 -0
  106. package/plugins/definition/OpenSearchQueryModifierPlugin.d.ts +14 -0
  107. package/plugins/definition/OpenSearchQueryModifierPlugin.js +18 -0
  108. package/plugins/definition/OpenSearchQueryModifierPlugin.js.map +1 -0
  109. package/plugins/definition/OpenSearchSortModifierPlugin.d.ts +13 -0
  110. package/plugins/definition/OpenSearchSortModifierPlugin.js +18 -0
  111. package/plugins/definition/OpenSearchSortModifierPlugin.js.map +1 -0
  112. package/plugins/definition/index.d.ts +6 -0
  113. package/plugins/definition/index.js +8 -0
  114. package/plugins/definition/index.js.map +1 -0
  115. package/plugins/index.d.ts +2 -0
  116. package/plugins/index.js +4 -0
  117. package/plugins/index.js.map +1 -0
  118. package/plugins/operator/andIn.d.ts +7 -0
  119. package/plugins/operator/andIn.js +35 -0
  120. package/plugins/operator/andIn.js.map +1 -0
  121. package/plugins/operator/between.d.ts +7 -0
  122. package/plugins/operator/between.js +32 -0
  123. package/plugins/operator/between.js.map +1 -0
  124. package/plugins/operator/contains.d.ts +7 -0
  125. package/plugins/operator/contains.js +24 -0
  126. package/plugins/operator/contains.js.map +1 -0
  127. package/plugins/operator/equal.d.ts +7 -0
  128. package/plugins/operator/equal.js +50 -0
  129. package/plugins/operator/equal.js.map +1 -0
  130. package/plugins/operator/gt.d.ts +7 -0
  131. package/plugins/operator/gt.js +22 -0
  132. package/plugins/operator/gt.js.map +1 -0
  133. package/plugins/operator/gte.d.ts +7 -0
  134. package/plugins/operator/gte.js +22 -0
  135. package/plugins/operator/gte.js.map +1 -0
  136. package/plugins/operator/in.d.ts +7 -0
  137. package/plugins/operator/in.js +29 -0
  138. package/plugins/operator/in.js.map +1 -0
  139. package/plugins/operator/index.d.ts +15 -0
  140. package/plugins/operator/index.js +17 -0
  141. package/plugins/operator/index.js.map +1 -0
  142. package/plugins/operator/lt.d.ts +7 -0
  143. package/plugins/operator/lt.js +22 -0
  144. package/plugins/operator/lt.js.map +1 -0
  145. package/plugins/operator/lte.d.ts +7 -0
  146. package/plugins/operator/lte.js +22 -0
  147. package/plugins/operator/lte.js.map +1 -0
  148. package/plugins/operator/not.d.ts +7 -0
  149. package/plugins/operator/not.js +44 -0
  150. package/plugins/operator/not.js.map +1 -0
  151. package/plugins/operator/notBetween.d.ts +7 -0
  152. package/plugins/operator/notBetween.js +32 -0
  153. package/plugins/operator/notBetween.js.map +1 -0
  154. package/plugins/operator/notContains.d.ts +7 -0
  155. package/plugins/operator/notContains.js +24 -0
  156. package/plugins/operator/notContains.js.map +1 -0
  157. package/plugins/operator/notIn.d.ts +7 -0
  158. package/plugins/operator/notIn.js +27 -0
  159. package/plugins/operator/notIn.js.map +1 -0
  160. package/plugins/operator/notStartsWith.d.ts +7 -0
  161. package/plugins/operator/notStartsWith.js +23 -0
  162. package/plugins/operator/notStartsWith.js.map +1 -0
  163. package/plugins/operator/startsWith.d.ts +7 -0
  164. package/plugins/operator/startsWith.js +23 -0
  165. package/plugins/operator/startsWith.js.map +1 -0
  166. package/sharedIndex.d.ts +1 -0
  167. package/sharedIndex.js +5 -0
  168. package/sharedIndex.js.map +1 -0
  169. package/sort.d.ts +13 -0
  170. package/sort.js +65 -0
  171. package/sort.js.map +1 -0
  172. package/types.d.ts +68 -0
  173. package/types.js +68 -0
  174. package/types.js.map +1 -0
  175. package/utils/createIndex.d.ts +19 -0
  176. package/utils/createIndex.js +88 -0
  177. package/utils/createIndex.js.map +1 -0
  178. package/utils/index.d.ts +2 -0
  179. package/utils/index.js +4 -0
  180. package/utils/index.js.map +1 -0
  181. package/utils/waitUntilHealthy/UnhealthyClusterError.d.ts +4 -0
  182. package/utils/waitUntilHealthy/UnhealthyClusterError.js +11 -0
  183. package/utils/waitUntilHealthy/UnhealthyClusterError.js.map +1 -0
  184. package/utils/waitUntilHealthy/WaitUntilHealthy.d.ts +72 -0
  185. package/utils/waitUntilHealthy/WaitUntilHealthy.js +161 -0
  186. package/utils/waitUntilHealthy/WaitUntilHealthy.js.map +1 -0
  187. package/utils/waitUntilHealthy/WaitingHealthyClusterAbortedError.d.ts +4 -0
  188. package/utils/waitUntilHealthy/WaitingHealthyClusterAbortedError.js +11 -0
  189. package/utils/waitUntilHealthy/WaitingHealthyClusterAbortedError.js.map +1 -0
  190. package/utils/waitUntilHealthy/index.d.ts +4 -0
  191. package/utils/waitUntilHealthy/index.js +6 -0
  192. package/utils/waitUntilHealthy/index.js.map +1 -0
  193. package/utils/waitUntilHealthy/reason/ClusterHealthReason.d.ts +15 -0
  194. package/utils/waitUntilHealthy/reason/ClusterHealthReason.js +13 -0
  195. package/utils/waitUntilHealthy/reason/ClusterHealthReason.js.map +1 -0
  196. package/utils/waitUntilHealthy/reason/IReason.d.ts +4 -0
  197. package/utils/waitUntilHealthy/reason/IReason.js +3 -0
  198. package/utils/waitUntilHealthy/reason/IReason.js.map +1 -0
  199. package/utils/waitUntilHealthy/reason/MemoryReason.d.ts +14 -0
  200. package/utils/waitUntilHealthy/reason/MemoryReason.js +13 -0
  201. package/utils/waitUntilHealthy/reason/MemoryReason.js.map +1 -0
  202. package/utils/waitUntilHealthy/reason/ProcessorReason.d.ts +14 -0
  203. package/utils/waitUntilHealthy/reason/ProcessorReason.js +13 -0
  204. package/utils/waitUntilHealthy/reason/ProcessorReason.js.map +1 -0
  205. package/utils/waitUntilHealthy/reason/index.d.ts +3 -0
  206. package/utils/waitUntilHealthy/reason/index.js +5 -0
  207. package/utils/waitUntilHealthy/reason/index.js.map +1 -0
  208. package/where.d.ts +17 -0
  209. package/where.js +82 -0
  210. package/where.js.map +1 -0
package/types.js ADDED
@@ -0,0 +1,68 @@
1
+ // ---------------------------------------------------------------------------
2
+ // Types inferred from the accessible API surface.
3
+ // The `_types/` directory in the opensearch package only ships `.d.ts` files
4
+ // (no `.js` runtime modules), so deep imports are unresolvable under
5
+ // `moduleResolution: "bundler"`. We infer what we can from the public
6
+ // barrel (`api/index.js`) and use `string` where structural extraction
7
+ // is not possible.
8
+ // ---------------------------------------------------------------------------
9
+
10
+ // Distribute over the SortOptions union and extract only the Record<string, FieldSort> variant.
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Local type aliases replacing elastic-ts
14
+ // ---------------------------------------------------------------------------
15
+
16
+ /** Replaces elastic-ts PrimitiveValue. Aligns with opensearch FieldValue. */
17
+
18
+ /** Replaces elastic-ts Query */
19
+
20
+ /** Replaces elastic-ts BoolQueryConfig */
21
+
22
+ /** Replaces elastic-ts FieldSortOptions */
23
+
24
+ /** Replaces elastic-ts SortOrder. Narrowed to "asc" | "desc" (excludes "_doc"). */
25
+
26
+ /** Replaces elastic-ts Sort */
27
+
28
+ /** Single sort entry as an object map. Replaces elastic-ts SortType. */
29
+
30
+ /** Replaces elastic-ts SearchBody. Overrides search_after to allow PrimitiveValue[] (null | boolean | string | number). */
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // Context
34
+ // ---------------------------------------------------------------------------
35
+
36
+ // ---------------------------------------------------------------------------
37
+ // Bool query with required arrays (our convention)
38
+ // ---------------------------------------------------------------------------
39
+
40
+ // ---------------------------------------------------------------------------
41
+ // Operator plugin types
42
+ // ---------------------------------------------------------------------------
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // Search response shapes (aligned with @opensearch-project/opensearch v3)
46
+ // ---------------------------------------------------------------------------
47
+
48
+ /**
49
+ * Extract the total count from an opensearch hits response.
50
+ * Handles both `TotalHits` (object with `value`) and plain `number` formats.
51
+ */
52
+ export const getTotalCount = total => {
53
+ if (total === undefined || total === null) {
54
+ return 0;
55
+ }
56
+ if (typeof total === "number") {
57
+ return total;
58
+ }
59
+ return total.value;
60
+ };
61
+
62
+ // ---------------------------------------------------------------------------
63
+ // Index request body shapes (re-exported from opensearch package)
64
+ // ---------------------------------------------------------------------------
65
+
66
+ // Re-export SearchSortOrder for consumers that need it (including "_doc")
67
+
68
+ //# sourceMappingURL=types.js.map
package/types.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getTotalCount","total","undefined","value"],"sources":["types.ts"],"sourcesContent":["import type { Client } from \"@opensearch-project/opensearch\";\nimport type {\n Search_RequestBody as SearchRequestBody,\n Search_Response,\n Indices_Create_RequestBody\n} from \"@opensearch-project/opensearch/api/index.js\";\nimport type { Context } from \"@webiny/api/types.js\";\n\n// ---------------------------------------------------------------------------\n// Types inferred from the accessible API surface.\n// The `_types/` directory in the opensearch package only ships `.d.ts` files\n// (no `.js` runtime modules), so deep imports are unresolvable under\n// `moduleResolution: \"bundler\"`. We infer what we can from the public\n// barrel (`api/index.js`) and use `string` where structural extraction\n// is not possible.\n// ---------------------------------------------------------------------------\n\ntype QueryDslQueryContainer = NonNullable<SearchRequestBody[\"query\"]>;\ntype QueryDslBoolQuery = NonNullable<QueryDslQueryContainer[\"bool\"]>;\ntype SearchSort = NonNullable<SearchRequestBody[\"sort\"]>;\ntype SearchSortOrder = \"asc\" | \"desc\";\n\n// Distribute over the SortOptions union and extract only the Record<string, FieldSort> variant.\ntype _ExtractRecordValue<T> = T extends Record<string, infer V> ? V : never;\ntype _SortMember =\n NonNullable<SearchRequestBody[\"sort\"]> extends (infer U)[] | (infer U) ? U : never;\ntype SearchFieldSort = _ExtractRecordValue<Exclude<_SortMember, string>>;\n\ntype DynamicTemplate = NonNullable<\n NonNullable<Indices_Create_RequestBody[\"mappings\"]>[\"dynamic_templates\"]\n>[number][string];\n\nexport type { ApiResponse } from \"@opensearch-project/opensearch\";\n\nexport type { Client };\n\n// ---------------------------------------------------------------------------\n// Local type aliases replacing elastic-ts\n// ---------------------------------------------------------------------------\n\n/** Replaces elastic-ts PrimitiveValue. Aligns with opensearch FieldValue. */\nexport type PrimitiveValue = boolean | undefined | number | string;\n\n/** Replaces elastic-ts Query */\nexport type { QueryDslQueryContainer };\n\n/** Replaces elastic-ts BoolQueryConfig */\nexport type { QueryDslBoolQuery };\n\n/** Replaces elastic-ts FieldSortOptions */\nexport type FieldSortOptions = SearchFieldSort;\n\n/** Replaces elastic-ts SortOrder. Narrowed to \"asc\" | \"desc\" (excludes \"_doc\"). */\nexport type SortOrder = \"asc\" | \"desc\";\n\n/** Replaces elastic-ts Sort */\nexport type { SearchSort as Sort };\n\n/** Single sort entry as an object map. Replaces elastic-ts SortType. */\nexport type SortType = Record<string, FieldSortOptions>;\n\n/** Replaces elastic-ts SearchBody. Overrides search_after to allow PrimitiveValue[] (null | boolean | string | number). */\nexport type SearchBody = Omit<SearchRequestBody, \"search_after\"> & {\n search_after?: PrimitiveValue[];\n};\n\n// ---------------------------------------------------------------------------\n// Context\n// ---------------------------------------------------------------------------\n\nexport interface OpenSearchContext extends Context {\n /**\n * @deprecated Use `context.opensearch` instead.\n */\n elasticsearch: Client;\n opensearch: Client;\n}\n\n// ---------------------------------------------------------------------------\n// Bool query with required arrays (our convention)\n// ---------------------------------------------------------------------------\n\nexport interface OpenSearchBoolQueryConfig extends QueryDslBoolQuery {\n must: QueryDslQueryContainer[];\n filter: QueryDslQueryContainer[];\n should: QueryDslQueryContainer[];\n must_not: QueryDslQueryContainer[];\n}\n\n// ---------------------------------------------------------------------------\n// Operator plugin types\n// ---------------------------------------------------------------------------\n\nexport type OpenSearchQueryOperator =\n | \"eq\"\n | \"not\"\n | \"in\"\n | \"not_in\"\n | \"contains\"\n | \"not_contains\"\n | \"between\"\n | \"not_between\"\n | \"gt\"\n | \"gte\"\n | \"lt\"\n | \"lte\"\n | string;\n\nexport interface OpenSearchQueryBuilderArgsPlugin {\n name: string;\n path: string;\n basePath: string;\n value: any;\n keyword: boolean;\n}\n\n// ---------------------------------------------------------------------------\n// Search response shapes (aligned with @opensearch-project/opensearch v3)\n// ---------------------------------------------------------------------------\n\nexport type OpenSearchSearchResponseHit = Search_Response[\"body\"][\"hits\"][\"hits\"][number];\n\nexport interface OpenSearchSearchResponseAggregationBucket {\n key: PrimitiveValue;\n doc_count: number;\n}\n\nexport type OpenSearchSearchResponseBodyHits = Search_Response[\"body\"][\"hits\"];\n\nexport type OpenSearchSearchResponseBody = Search_Response[\"body\"];\n\nexport type OpenSearchSearchResponse = Search_Response;\n\n/**\n * Extract the total count from an opensearch hits response.\n * Handles both `TotalHits` (object with `value`) and plain `number` formats.\n */\nexport const getTotalCount = (total: OpenSearchSearchResponseBodyHits[\"total\"]): number => {\n if (total === undefined || total === null) {\n return 0;\n }\n if (typeof total === \"number\") {\n return total;\n }\n return total.value;\n};\n\n// ---------------------------------------------------------------------------\n// Index request body shapes (re-exported from opensearch package)\n// ---------------------------------------------------------------------------\n\nexport type OpenSearchIndexRequestBodyMappingsDynamicTemplate = Record<string, DynamicTemplate>;\n\nexport type OpenSearchIndexRequestBody = Indices_Create_RequestBody;\n\n// Re-export SearchSortOrder for consumers that need it (including \"_doc\")\nexport type { SearchSortOrder };\n"],"mappings":"AAQA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAOA;;AAcA;AACA;AACA;;AAEA;;AAGA;;AAGA;;AAGA;;AAGA;;AAGA;;AAGA;;AAGA;;AAKA;AACA;AACA;;AAUA;AACA;AACA;;AASA;AACA;AACA;;AAyBA;AACA;AACA;;AAeA;AACA;AACA;AACA;AACA,OAAO,MAAMA,aAAa,GAAIC,KAAgD,IAAa;EACvF,IAAIA,KAAK,KAAKC,SAAS,IAAID,KAAK,KAAK,IAAI,EAAE;IACvC,OAAO,CAAC;EACZ;EACA,IAAI,OAAOA,KAAK,KAAK,QAAQ,EAAE;IAC3B,OAAOA,KAAK;EAChB;EACA,OAAOA,KAAK,CAACE,KAAK;AACtB,CAAC;;AAED;AACA;AACA;;AAMA","ignoreList":[]}
@@ -0,0 +1,19 @@
1
+ import type { Client } from "../client.js";
2
+ import type { PluginsContainer } from "@webiny/plugins";
3
+ interface OnExists {
4
+ (): void;
5
+ }
6
+ interface OnError {
7
+ (ex: Error): Error;
8
+ }
9
+ interface CreateIndexParams {
10
+ client: Client;
11
+ plugins: PluginsContainer;
12
+ type: string;
13
+ tenant: string;
14
+ index: string;
15
+ onExists?: OnExists;
16
+ onError?: OnError;
17
+ }
18
+ export declare const createIndex: (params: CreateIndexParams) => Promise<void>;
19
+ export {};
@@ -0,0 +1,88 @@
1
+ import { getLastAddedIndexPlugin } from "../indices.js";
2
+ import WebinyError from "@webiny/error";
3
+ const indexExists = async params => {
4
+ const {
5
+ client,
6
+ index,
7
+ onExists
8
+ } = params;
9
+ try {
10
+ const response = await client.indices.exists({
11
+ index,
12
+ ignore_unavailable: false,
13
+ allow_no_indices: true,
14
+ include_defaults: true,
15
+ flat_settings: false,
16
+ local: false
17
+ });
18
+ if (!response.body) {
19
+ return false;
20
+ }
21
+ if (onExists) {
22
+ onExists();
23
+ }
24
+ return true;
25
+ } catch {
26
+ console.error(`Could not determine if the index "${index}" exists.`);
27
+ }
28
+ return false;
29
+ };
30
+ const indexCreate = async params => {
31
+ const {
32
+ client,
33
+ index,
34
+ plugin,
35
+ tenant,
36
+ type,
37
+ onError
38
+ } = params;
39
+ try {
40
+ await client.indices.create({
41
+ index,
42
+ body: {
43
+ ...plugin.body
44
+ }
45
+ });
46
+ } catch (ex) {
47
+ let error = ex;
48
+ if (onError) {
49
+ error = onError(ex);
50
+ }
51
+ throw new WebinyError(error.message || `Could not create OpenSearch index for the ${type}.`, error.code || "CREATE_OPENSEARCH_INDEX_ERROR", {
52
+ error: {
53
+ ...error,
54
+ message: error.message,
55
+ code: error.code,
56
+ data: error.data
57
+ },
58
+ type,
59
+ tenant,
60
+ index,
61
+ body: plugin.body
62
+ });
63
+ }
64
+ };
65
+ export const createIndex = async params => {
66
+ const {
67
+ plugins,
68
+ type,
69
+ onExists
70
+ } = params;
71
+ const plugin = getLastAddedIndexPlugin({
72
+ container: plugins,
73
+ type
74
+ });
75
+ const exists = await indexExists(params);
76
+ if (exists) {
77
+ if (onExists) {
78
+ onExists();
79
+ }
80
+ return;
81
+ }
82
+ await indexCreate({
83
+ ...params,
84
+ plugin
85
+ });
86
+ };
87
+
88
+ //# sourceMappingURL=createIndex.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["getLastAddedIndexPlugin","WebinyError","indexExists","params","client","index","onExists","response","indices","exists","ignore_unavailable","allow_no_indices","include_defaults","flat_settings","local","body","console","error","indexCreate","plugin","tenant","type","onError","create","ex","message","code","data","createIndex","plugins","container"],"sources":["createIndex.ts"],"sourcesContent":["import type { Client } from \"~/client.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport { getLastAddedIndexPlugin } from \"~/indices.js\";\nimport type { OpenSearchIndexPlugin } from \"~/plugins/index.js\";\nimport WebinyError from \"@webiny/error\";\n\ninterface OnExists {\n (): void;\n}\n\ninterface OnError {\n (ex: Error): Error;\n}\n\ninterface ExistsIndexParams {\n client: Client;\n index: string;\n onExists?: OnExists;\n}\n\nconst indexExists = async (params: ExistsIndexParams): Promise<boolean> => {\n const { client, index, onExists } = params;\n\n try {\n const response = await client.indices.exists({\n index,\n ignore_unavailable: false,\n allow_no_indices: true,\n include_defaults: true,\n flat_settings: false,\n local: false\n });\n if (!response.body) {\n return false;\n }\n if (onExists) {\n onExists();\n }\n return true;\n } catch {\n console.error(`Could not determine if the index \"${index}\" exists.`);\n }\n return false;\n};\n\ninterface IndexCreateParams {\n client: Client;\n index: string;\n type: string;\n tenant: string;\n plugin: OpenSearchIndexPlugin;\n onError?: OnError;\n}\n\nconst indexCreate = async (params: IndexCreateParams): Promise<void> => {\n const { client, index, plugin, tenant, type, onError } = params;\n\n try {\n await client.indices.create({\n index,\n body: {\n ...plugin.body\n }\n });\n } catch (ex) {\n let error = ex;\n if (onError) {\n error = onError(ex);\n }\n throw new WebinyError(\n error.message || `Could not create OpenSearch index for the ${type}.`,\n error.code || \"CREATE_OPENSEARCH_INDEX_ERROR\",\n {\n error: {\n ...error,\n message: error.message,\n code: error.code,\n data: error.data\n },\n type,\n tenant,\n index,\n body: plugin.body\n }\n );\n }\n};\n\ninterface CreateIndexParams {\n client: Client;\n plugins: PluginsContainer;\n type: string;\n tenant: string;\n index: string;\n onExists?: OnExists;\n onError?: OnError;\n}\n\nexport const createIndex = async (params: CreateIndexParams): Promise<void> => {\n const { plugins, type, onExists } = params;\n const plugin = getLastAddedIndexPlugin<OpenSearchIndexPlugin>({\n container: plugins,\n type\n });\n\n const exists = await indexExists(params);\n if (exists) {\n if (onExists) {\n onExists();\n }\n return;\n }\n\n await indexCreate({\n ...params,\n plugin\n });\n};\n"],"mappings":"AAEA,SAASA,uBAAuB;AAEhC,OAAOC,WAAW,MAAM,eAAe;AAgBvC,MAAMC,WAAW,GAAG,MAAOC,MAAyB,IAAuB;EACvE,MAAM;IAAEC,MAAM;IAAEC,KAAK;IAAEC;EAAS,CAAC,GAAGH,MAAM;EAE1C,IAAI;IACA,MAAMI,QAAQ,GAAG,MAAMH,MAAM,CAACI,OAAO,CAACC,MAAM,CAAC;MACzCJ,KAAK;MACLK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,OAAO,MAAMa,WAAW,GAAG,MAAOzB,MAAyB,IAAoB;EAC3E,MAAM;IAAE0B,OAAO;IAAER,IAAI;IAAEf;EAAS,CAAC,GAAGH,MAAM;EAC1C,MAAMgB,MAAM,GAAGnB,uBAAuB,CAAwB;IAC1D8B,SAAS,EAAED,OAAO;IAClBR;EACJ,CAAC,CAAC;EAEF,MAAMZ,MAAM,GAAG,MAAMP,WAAW,CAACC,MAAM,CAAC;EACxC,IAAIM,MAAM,EAAE;IACR,IAAIH,QAAQ,EAAE;MACVA,QAAQ,CAAC,CAAC;IACd;IACA;EACJ;EAEA,MAAMY,WAAW,CAAC;IACd,GAAGf,MAAM;IACTgB;EACJ,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ export * from "./waitUntilHealthy/index.js";
2
+ export * from "./createIndex.js";
package/utils/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./waitUntilHealthy/index.js";
2
+ export * from "./createIndex.js";
3
+
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./waitUntilHealthy/index.js\";\nexport * from \"./createIndex.js\";\n"],"mappings":"AAAA;AACA","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export declare class UnhealthyClusterError extends WebinyError {
3
+ constructor(maxWaitingTime: number);
4
+ }
@@ -0,0 +1,11 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export class UnhealthyClusterError extends WebinyError {
3
+ constructor(maxWaitingTime) {
4
+ super({
5
+ message: `Cluster did not become healthy in ${maxWaitingTime} seconds.`,
6
+ code: "UNHEALTHY_CLUSTER"
7
+ });
8
+ }
9
+ }
10
+
11
+ //# sourceMappingURL=UnhealthyClusterError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["WebinyError","UnhealthyClusterError","constructor","maxWaitingTime","message","code"],"sources":["UnhealthyClusterError.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error\";\n\nexport class UnhealthyClusterError extends WebinyError {\n public constructor(maxWaitingTime: number) {\n super({\n message: `Cluster did not become healthy in ${maxWaitingTime} seconds.`,\n code: \"UNHEALTHY_CLUSTER\"\n });\n }\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,qBAAqB,SAASD,WAAW,CAAC;EAC5CE,WAAWA,CAACC,cAAsB,EAAE;IACvC,KAAK,CAAC;MACFC,OAAO,EAAE,qCAAqCD,cAAc,WAAW;MACvEE,IAAI,EAAE;IACV,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -0,0 +1,72 @@
1
+ import type { Client } from "../../client.js";
2
+ import { OpenSearchCatClusterHealthStatus } from "../../operations/types.js";
3
+ import type { ClusterHealthReason, MemoryReason, ProcessorReason } from "./reason/index.js";
4
+ export type WaitingReason = ProcessorReason | MemoryReason | ClusterHealthReason;
5
+ export interface IWaitUntilHealthyParams {
6
+ /**
7
+ * Minimum status allowed, otherwise the cluster is considered unhealthy.
8
+ */
9
+ minClusterHealthStatus: OpenSearchCatClusterHealthStatus.Green | OpenSearchCatClusterHealthStatus.Yellow;
10
+ /**
11
+ * Maximum processor percent allowed, otherwise the cluster is considered unhealthy.
12
+ */
13
+ maxProcessorPercent: number;
14
+ /**
15
+ * Maximum RAM percent allowed, otherwise the cluster is considered unhealthy.
16
+ */
17
+ maxRamPercent?: number;
18
+ /**
19
+ * Maximum time to wait in seconds.
20
+ * This is to prevent infinite waiting in case the cluster never becomes healthy.
21
+ */
22
+ maxWaitingTime: number;
23
+ /**
24
+ * Time in seconds to wait between each check.
25
+ * This is to prevent spamming the cluster with requests.
26
+ * Default is WAITING_TIME_STEP seconds.
27
+ */
28
+ waitingTimeStep?: number;
29
+ }
30
+ export interface IWaitOptionsOnUnhealthyParams {
31
+ startedAt: Date;
32
+ mustEndAt: Date;
33
+ waitingTimeStep: number;
34
+ runs: number;
35
+ waitingReason: WaitingReason;
36
+ }
37
+ export interface IWaitOptionsOnTimeoutParams {
38
+ startedAt: Date;
39
+ mustEndAt: Date;
40
+ waitingTimeStep: number;
41
+ runs: number;
42
+ waitingReason: WaitingReason;
43
+ }
44
+ export interface IWaitOptions {
45
+ onUnhealthy?(params: IWaitOptionsOnUnhealthyParams): Promise<void>;
46
+ onTimeout?(params: IWaitOptionsOnTimeoutParams): Promise<void>;
47
+ }
48
+ export interface IWaitUntilHealthyWaitResponse {
49
+ runningTime: number;
50
+ runs: number;
51
+ }
52
+ declare class WaitUntilHealthy {
53
+ private readonly client;
54
+ private readonly options;
55
+ private readonly catHealth;
56
+ private readonly catNodes;
57
+ private aborted;
58
+ constructor(client: Client, options: IWaitUntilHealthyParams);
59
+ abort(): void;
60
+ /**
61
+ * @throws UnhealthyClusterError
62
+ * @throws WaitingHealthyClusterAbortedError
63
+ */
64
+ wait(options?: IWaitOptions): Promise<IWaitUntilHealthyWaitResponse>;
65
+ private shouldWait;
66
+ private getProcessorPercent;
67
+ private getRamPercent;
68
+ private toClusterHealthStatus;
69
+ private transformClusterHealthStatus;
70
+ }
71
+ export type { WaitUntilHealthy };
72
+ export declare const createWaitUntilHealthy: (client: Client, params: IWaitUntilHealthyParams) => WaitUntilHealthy;
@@ -0,0 +1,161 @@
1
+ import { OpenSearchCatHealth } from "../../operations/OpenSearchCatHealth.js";
2
+ import { OpenSearchCatNodes } from "../../operations/OpenSearchCatNodes.js";
3
+ import { OpenSearchCatClusterHealthStatus } from "../../operations/types.js";
4
+ import { UnhealthyClusterError } from "./UnhealthyClusterError.js";
5
+ import { createClusterHealthStatusReason, createMemoryReason, createProcessorReason } from "./reason/index.js";
6
+ import { WaitingHealthyClusterAbortedError } from "./WaitingHealthyClusterAbortedError.js";
7
+ const WAITING_TIME_STEP = 10;
8
+ class WaitUntilHealthy {
9
+ aborted = false;
10
+ constructor(client, options) {
11
+ this.client = client;
12
+ this.options = options;
13
+ this.catHealth = new OpenSearchCatHealth(this.client);
14
+ this.catNodes = new OpenSearchCatNodes(this.client);
15
+ }
16
+ abort() {
17
+ this.aborted = true;
18
+ }
19
+ /**
20
+ * @throws UnhealthyClusterError
21
+ * @throws WaitingHealthyClusterAbortedError
22
+ */
23
+ async wait(options) {
24
+ if (this.aborted) {
25
+ throw new WaitingHealthyClusterAbortedError(`Waiting for the cluster to become healthy was aborted even before it started.`);
26
+ }
27
+ const startedAt = new Date();
28
+ const mustEndAt = new Date(startedAt.getTime() + this.options.maxWaitingTime * 1000);
29
+ const waitingTimeStep = this.options.waitingTimeStep || WAITING_TIME_STEP;
30
+ let runs = 1;
31
+ let waitingReason;
32
+ while (waitingReason = await this.shouldWait()) {
33
+ if (new Date() >= mustEndAt) {
34
+ if (options?.onTimeout) {
35
+ await options.onTimeout({
36
+ startedAt,
37
+ mustEndAt,
38
+ waitingTimeStep,
39
+ waitingReason,
40
+ runs
41
+ });
42
+ }
43
+ throw new UnhealthyClusterError(this.options.maxWaitingTime);
44
+ } else if (options?.onUnhealthy) {
45
+ await options.onUnhealthy({
46
+ startedAt,
47
+ mustEndAt,
48
+ waitingTimeStep,
49
+ waitingReason,
50
+ runs
51
+ });
52
+ }
53
+ /**
54
+ * Abort check is separated from other IFs because it can be aborted in onUnhealthy callback.
55
+ */
56
+ if (this.aborted) {
57
+ throw new WaitingHealthyClusterAbortedError();
58
+ }
59
+ runs++;
60
+ await new Promise(resolve => {
61
+ setTimeout(resolve, waitingTimeStep * 1000);
62
+ });
63
+ }
64
+ const runningTime = new Date().getTime() - startedAt.getTime();
65
+ return {
66
+ runningTime,
67
+ runs
68
+ };
69
+ }
70
+ async shouldWait() {
71
+ let health;
72
+ let nodes;
73
+ try {
74
+ health = await this.catHealth.getHealth();
75
+ } catch (ex) {
76
+ return createClusterHealthStatusReason({
77
+ description: ex.message,
78
+ minimum: this.options.minClusterHealthStatus,
79
+ current: OpenSearchCatClusterHealthStatus.Red
80
+ });
81
+ }
82
+ try {
83
+ nodes = await this.catNodes.getNodes();
84
+ } catch (ex) {
85
+ return createClusterHealthStatusReason({
86
+ description: ex.message,
87
+ minimum: this.options.minClusterHealthStatus,
88
+ current: OpenSearchCatClusterHealthStatus.Red
89
+ });
90
+ }
91
+ const currentStatus = this.toClusterHealthStatus(health.status);
92
+ const clusterHealthStatus = this.transformClusterHealthStatus(currentStatus);
93
+ const minClusterHealthStatus = this.transformClusterHealthStatus(this.options.minClusterHealthStatus);
94
+ if (clusterHealthStatus > minClusterHealthStatus) {
95
+ return createClusterHealthStatusReason({
96
+ minimum: this.options.minClusterHealthStatus,
97
+ current: currentStatus
98
+ });
99
+ }
100
+ const processorPercent = this.getProcessorPercent(nodes);
101
+ if (processorPercent > this.options.maxProcessorPercent) {
102
+ return createProcessorReason({
103
+ maximum: this.options.maxProcessorPercent,
104
+ current: processorPercent
105
+ });
106
+ }
107
+ /**
108
+ * Possibly no max ram definition?
109
+ */
110
+ if (this.options.maxRamPercent === undefined) {
111
+ return false;
112
+ }
113
+ const ramPercent = this.getRamPercent(nodes);
114
+ if (ramPercent > this.options.maxRamPercent) {
115
+ return createMemoryReason({
116
+ maximum: this.options.maxRamPercent,
117
+ current: ramPercent
118
+ });
119
+ }
120
+ return false;
121
+ }
122
+ getProcessorPercent(nodes) {
123
+ const total = nodes.reduce((total, node) => {
124
+ return total + parseFloat(node.cpu || "0");
125
+ }, 0);
126
+ return total / nodes.length;
127
+ }
128
+ getRamPercent(nodes) {
129
+ const total = nodes.reduce((total, node) => {
130
+ return total + parseFloat(node["ram.percent"] || "0");
131
+ }, 0);
132
+ return total / nodes.length;
133
+ }
134
+ toClusterHealthStatus(status) {
135
+ switch (status) {
136
+ case "green":
137
+ return OpenSearchCatClusterHealthStatus.Green;
138
+ case "yellow":
139
+ return OpenSearchCatClusterHealthStatus.Yellow;
140
+ default:
141
+ return OpenSearchCatClusterHealthStatus.Red;
142
+ }
143
+ }
144
+ transformClusterHealthStatus(status) {
145
+ switch (status) {
146
+ case OpenSearchCatClusterHealthStatus.Green:
147
+ return 1;
148
+ case OpenSearchCatClusterHealthStatus.Yellow:
149
+ return 2;
150
+ case OpenSearchCatClusterHealthStatus.Red:
151
+ return 3;
152
+ default:
153
+ return 99;
154
+ }
155
+ }
156
+ }
157
+ export const createWaitUntilHealthy = (client, params) => {
158
+ return new WaitUntilHealthy(client, params);
159
+ };
160
+
161
+ //# sourceMappingURL=WaitUntilHealthy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["OpenSearchCatHealth","OpenSearchCatNodes","OpenSearchCatClusterHealthStatus","UnhealthyClusterError","createClusterHealthStatusReason","createMemoryReason","createProcessorReason","WaitingHealthyClusterAbortedError","WAITING_TIME_STEP","WaitUntilHealthy","aborted","constructor","client","options","catHealth","catNodes","abort","wait","startedAt","Date","mustEndAt","getTime","maxWaitingTime","waitingTimeStep","runs","waitingReason","shouldWait","onTimeout","onUnhealthy","Promise","resolve","setTimeout","runningTime","health","nodes","getHealth","ex","description","message","minimum","minClusterHealthStatus","current","Red","getNodes","currentStatus","toClusterHealthStatus","status","clusterHealthStatus","transformClusterHealthStatus","processorPercent","getProcessorPercent","maxProcessorPercent","maximum","maxRamPercent","undefined","ramPercent","getRamPercent","total","reduce","node","parseFloat","cpu","length","Green","Yellow","createWaitUntilHealthy","params"],"sources":["WaitUntilHealthy.ts"],"sourcesContent":["import type { Client } from \"~/client.js\";\nimport { OpenSearchCatHealth } from \"~/operations/OpenSearchCatHealth.js\";\nimport { OpenSearchCatNodes } from \"~/operations/OpenSearchCatNodes.js\";\nimport type {\n IOpenSearchCatHealthResponse,\n IOpenSearchCatNodesResponse\n} from \"~/operations/types.js\";\nimport { OpenSearchCatClusterHealthStatus } from \"~/operations/types.js\";\nimport { UnhealthyClusterError } from \"~/utils/waitUntilHealthy/UnhealthyClusterError.js\";\nimport type { ClusterHealthReason, MemoryReason, ProcessorReason } from \"./reason/index.js\";\nimport {\n createClusterHealthStatusReason,\n createMemoryReason,\n createProcessorReason\n} from \"./reason/index.js\";\nimport { WaitingHealthyClusterAbortedError } from \"./WaitingHealthyClusterAbortedError.js\";\n\nconst WAITING_TIME_STEP = 10;\n\nexport type WaitingReason = ProcessorReason | MemoryReason | ClusterHealthReason;\n\nexport interface IWaitUntilHealthyParams {\n /**\n * Minimum status allowed, otherwise the cluster is considered unhealthy.\n */\n minClusterHealthStatus:\n | OpenSearchCatClusterHealthStatus.Green\n | OpenSearchCatClusterHealthStatus.Yellow;\n /**\n * Maximum processor percent allowed, otherwise the cluster is considered unhealthy.\n */\n maxProcessorPercent: number;\n /**\n * Maximum RAM percent allowed, otherwise the cluster is considered unhealthy.\n */\n maxRamPercent?: number;\n /**\n * Maximum time to wait in seconds.\n * This is to prevent infinite waiting in case the cluster never becomes healthy.\n */\n maxWaitingTime: number;\n /**\n * Time in seconds to wait between each check.\n * This is to prevent spamming the cluster with requests.\n * Default is WAITING_TIME_STEP seconds.\n */\n waitingTimeStep?: number;\n}\n\nexport interface IWaitOptionsOnUnhealthyParams {\n startedAt: Date;\n mustEndAt: Date;\n waitingTimeStep: number;\n runs: number;\n waitingReason: WaitingReason;\n}\n\nexport interface IWaitOptionsOnTimeoutParams {\n startedAt: Date;\n mustEndAt: Date;\n waitingTimeStep: number;\n runs: number;\n waitingReason: WaitingReason;\n}\n\nexport interface IWaitOptions {\n onUnhealthy?(params: IWaitOptionsOnUnhealthyParams): Promise<void>;\n onTimeout?(params: IWaitOptionsOnTimeoutParams): Promise<void>;\n}\n\nexport interface IWaitUntilHealthyWaitResponse {\n runningTime: number;\n runs: number;\n}\n\nclass WaitUntilHealthy {\n private readonly client: Client;\n private readonly options: IWaitUntilHealthyParams;\n\n private readonly catHealth: OpenSearchCatHealth;\n private readonly catNodes: OpenSearchCatNodes;\n\n private aborted = false;\n\n public constructor(client: Client, options: IWaitUntilHealthyParams) {\n this.client = client;\n this.options = options;\n\n this.catHealth = new OpenSearchCatHealth(this.client);\n this.catNodes = new OpenSearchCatNodes(this.client);\n }\n\n public abort(): void {\n this.aborted = true;\n }\n /**\n * @throws UnhealthyClusterError\n * @throws WaitingHealthyClusterAbortedError\n */\n public async wait(options?: IWaitOptions): Promise<IWaitUntilHealthyWaitResponse> {\n if (this.aborted) {\n throw new WaitingHealthyClusterAbortedError(\n `Waiting for the cluster to become healthy was aborted even before it started.`\n );\n }\n const startedAt = new Date();\n const mustEndAt = new Date(startedAt.getTime() + this.options.maxWaitingTime * 1000);\n const waitingTimeStep = this.options.waitingTimeStep || WAITING_TIME_STEP;\n let runs = 1;\n let waitingReason: WaitingReason | false;\n while ((waitingReason = await this.shouldWait())) {\n if (new Date() >= mustEndAt) {\n if (options?.onTimeout) {\n await options.onTimeout({\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason,\n runs\n });\n }\n throw new UnhealthyClusterError(this.options.maxWaitingTime);\n } else if (options?.onUnhealthy) {\n await options.onUnhealthy({\n startedAt,\n mustEndAt,\n waitingTimeStep,\n waitingReason,\n runs\n });\n }\n /**\n * Abort check is separated from other IFs because it can be aborted in onUnhealthy callback.\n */\n if (this.aborted) {\n throw new WaitingHealthyClusterAbortedError();\n }\n runs++;\n await new Promise(resolve => {\n setTimeout(resolve, waitingTimeStep * 1000);\n });\n }\n\n const runningTime = new Date().getTime() - startedAt.getTime();\n\n return {\n runningTime,\n runs\n };\n }\n\n private async shouldWait(): Promise<WaitingReason | false> {\n let health: IOpenSearchCatHealthResponse;\n let nodes: IOpenSearchCatNodesResponse;\n try {\n health = await this.catHealth.getHealth();\n } catch (ex) {\n return createClusterHealthStatusReason({\n description: ex.message,\n minimum: this.options.minClusterHealthStatus,\n current: OpenSearchCatClusterHealthStatus.Red\n });\n }\n try {\n nodes = await this.catNodes.getNodes();\n } catch (ex) {\n return createClusterHealthStatusReason({\n description: ex.message,\n minimum: this.options.minClusterHealthStatus,\n current: OpenSearchCatClusterHealthStatus.Red\n });\n }\n\n const currentStatus = this.toClusterHealthStatus(health.status);\n const clusterHealthStatus = this.transformClusterHealthStatus(currentStatus);\n const minClusterHealthStatus = this.transformClusterHealthStatus(\n this.options.minClusterHealthStatus\n );\n if (clusterHealthStatus > minClusterHealthStatus) {\n return createClusterHealthStatusReason({\n minimum: this.options.minClusterHealthStatus,\n current: currentStatus\n });\n }\n\n const processorPercent = this.getProcessorPercent(nodes);\n if (processorPercent > this.options.maxProcessorPercent) {\n return createProcessorReason({\n maximum: this.options.maxProcessorPercent,\n current: processorPercent\n });\n }\n /**\n * Possibly no max ram definition?\n */\n if (this.options.maxRamPercent === undefined) {\n return false;\n }\n\n const ramPercent = this.getRamPercent(nodes);\n if (ramPercent > this.options.maxRamPercent) {\n return createMemoryReason({\n maximum: this.options.maxRamPercent,\n current: ramPercent\n });\n }\n return false;\n }\n\n private getProcessorPercent(nodes: IOpenSearchCatNodesResponse): number {\n const total = nodes.reduce<number>((total, node) => {\n return total + parseFloat(node.cpu || \"0\");\n }, 0);\n return total / nodes.length;\n }\n\n private getRamPercent(nodes: IOpenSearchCatNodesResponse): number {\n const total = nodes.reduce<number>((total, node) => {\n return total + parseFloat(node[\"ram.percent\"] || \"0\");\n }, 0);\n return total / nodes.length;\n }\n\n private toClusterHealthStatus(status?: string): OpenSearchCatClusterHealthStatus {\n switch (status) {\n case \"green\":\n return OpenSearchCatClusterHealthStatus.Green;\n case \"yellow\":\n return OpenSearchCatClusterHealthStatus.Yellow;\n default:\n return OpenSearchCatClusterHealthStatus.Red;\n }\n }\n\n private transformClusterHealthStatus(status: OpenSearchCatClusterHealthStatus): number {\n switch (status) {\n case OpenSearchCatClusterHealthStatus.Green:\n return 1;\n case OpenSearchCatClusterHealthStatus.Yellow:\n return 2;\n case OpenSearchCatClusterHealthStatus.Red:\n return 3;\n default:\n return 99;\n }\n }\n}\n\nexport type { WaitUntilHealthy };\n\nexport const createWaitUntilHealthy = (\n client: Client,\n params: IWaitUntilHealthyParams\n): WaitUntilHealthy => {\n return new WaitUntilHealthy(client, params);\n};\n"],"mappings":"AACA,SAASA,mBAAmB;AAC5B,SAASC,kBAAkB;AAK3B,SAASC,gCAAgC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edError extends WebinyError {
3
+ constructor(message?: string);
4
+ }
@@ -0,0 +1,11 @@
1
+ import { WebinyError } from "@webiny/error";
2
+ export class WaitingHealthyClusterAbortedError extends WebinyError {
3
+ constructor(message) {
4
+ super({
5
+ message: message || `Waiting for the cluster to become healthy was aborted.`,
6
+ code: "WAITING_HEALTHY_CLUSTER_ABORTED"
7
+ });
8
+ }
9
+ }
10
+
11
+ //# sourceMappingURL=WaitingHealthyClusterAbortedError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["WebinyError","WaitingHealthyClusterAbortedError","constructor","message","code"],"sources":["WaitingHealthyClusterAbortedError.ts"],"sourcesContent":["import { WebinyError } from \"@webiny/error\";\n\nexport class WaitingHealthyClusterAbortedError extends WebinyError {\n public constructor(message?: string) {\n super({\n message: message || `Waiting for the cluster to become healthy was aborted.`,\n code: \"WAITING_HEALTHY_CLUSTER_ABORTED\"\n });\n }\n}\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,iCAAiC,SAASD,WAAW,CAAC;EACxDE,WAAWA,CAACC,OAAgB,EAAE;IACjC,KAAK,CAAC;MACFA,OAAO,EAAEA,OAAO,IAAI,wDAAwD;MAC5EC,IAAI,EAAE;IACV,CAAC,CAAC;EACN;AACJ","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ export * from "./WaitingHealthyClusterAbortedError.js";
2
+ export * from "./UnhealthyClusterError.js";
3
+ export * from "./WaitUntilHealthy.js";
4
+ export * from "./reason/index.js";
@@ -0,0 +1,6 @@
1
+ export * from "./WaitingHealthyClusterAbortedError.js";
2
+ export * from "./UnhealthyClusterError.js";
3
+ export * from "./WaitUntilHealthy.js";
4
+ export * from "./reason/index.js";
5
+
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export * from \"./WaitingHealthyClusterAbortedError.js\";\nexport * from \"./UnhealthyClusterError.js\";\nexport * from \"./WaitUntilHealthy.js\";\nexport * from \"./reason/index.js\";\n"],"mappings":"AAAA;AACA;AACA;AACA","ignoreList":[]}
@@ -0,0 +1,15 @@
1
+ import type { OpenSearchCatClusterHealthStatus } from "../../../operations/index.js";
2
+ import type { IReason } from "../../../utils/waitUntilHealthy/reason/IReason.js";
3
+ export interface IClusterHealthReasonParams {
4
+ minimum: OpenSearchCatClusterHealthStatus;
5
+ current: OpenSearchCatClusterHealthStatus;
6
+ description?: string;
7
+ }
8
+ export declare class ClusterHealthReason implements IReason {
9
+ readonly name = "clusterHealth";
10
+ readonly minimum: OpenSearchCatClusterHealthStatus;
11
+ readonly current: OpenSearchCatClusterHealthStatus;
12
+ readonly description?: string;
13
+ constructor(params: IClusterHealthReasonParams);
14
+ }
15
+ export declare const createClusterHealthStatusReason: (params: IClusterHealthReasonParams) => ClusterHealthReason;
@@ -0,0 +1,13 @@
1
+ export class ClusterHealthReason {
2
+ name = "clusterHealth";
3
+ constructor(params) {
4
+ this.minimum = params.minimum;
5
+ this.current = params.current;
6
+ this.description = params.description;
7
+ }
8
+ }
9
+ export const createClusterHealthStatusReason = params => {
10
+ return new ClusterHealthReason(params);
11
+ };
12
+
13
+ //# sourceMappingURL=ClusterHealthReason.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["ClusterHealthReason","name","constructor","params","minimum","current","description","createClusterHealthStatusReason"],"sources":["ClusterHealthReason.ts"],"sourcesContent":["import type { OpenSearchCatClusterHealthStatus } from \"~/operations/index.js\";\nimport type { IReason } from \"~/utils/waitUntilHealthy/reason/IReason.js\";\n\nexport interface IClusterHealthReasonParams {\n minimum: OpenSearchCatClusterHealthStatus;\n current: OpenSearchCatClusterHealthStatus;\n description?: string;\n}\n\nexport class ClusterHealthReason implements IReason {\n public readonly name = \"clusterHealth\";\n public readonly minimum: OpenSearchCatClusterHealthStatus;\n public readonly current: OpenSearchCatClusterHealthStatus;\n public readonly description?: string;\n\n public constructor(params: IClusterHealthReasonParams) {\n this.minimum = params.minimum;\n this.current = params.current;\n this.description = params.description;\n }\n}\n\nexport const createClusterHealthStatusReason = (\n params: IClusterHealthReasonParams\n): ClusterHealthReason => {\n return new ClusterHealthReason(params);\n};\n"],"mappings":"AASA,OAAO,MAAMA,mBAAmB,CAAoB;EAChCC,IAAI,GAAG,eAAe;EAK/BC,WAAWA,CAACC,MAAkC,EAAE;IACnD,IAAI,CAACC,OAAO,GAAGD,MAAM,CAACC,OAAO;IAC7B,IAAI,CAACC,OAAO,GAAGF,MAAM,CAACE,OAAO;IAC7B,IAAI,CAACC,WAAW,GAAGH,MAAM,CAACG,WAAW;EACzC;AACJ;AAEA,OAAO,MAAMC,+BAA+B,GACxCJ,MAAkC,IACZ;EACtB,OAAO,IAAIH,mBAAmB,CAACG,MAAM,CAAC;AAC1C,CAAC","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ export interface IReason {
2
+ name: string;
3
+ description?: string;
4
+ }
@@ -0,0 +1,3 @@
1
+ export {};
2
+
3
+ //# sourceMappingURL=IReason.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["IReason.ts"],"sourcesContent":["export interface IReason {\n name: string;\n description?: string;\n}\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,14 @@
1
+ import type { IReason } from "../../../utils/waitUntilHealthy/reason/IReason.js";
2
+ export interface IMemoryReasonParams {
3
+ maximum: number;
4
+ current: number;
5
+ description?: string;
6
+ }
7
+ export declare class MemoryReason implements IReason {
8
+ readonly name = "memory";
9
+ readonly maximum: number;
10
+ readonly current: number;
11
+ readonly description?: string;
12
+ constructor(params: IMemoryReasonParams);
13
+ }
14
+ export declare const createMemoryReason: (params: IMemoryReasonParams) => MemoryReason;
@@ -0,0 +1,13 @@
1
+ export class MemoryReason {
2
+ name = "memory";
3
+ constructor(params) {
4
+ this.maximum = params.maximum;
5
+ this.current = params.current;
6
+ this.description = params.description;
7
+ }
8
+ }
9
+ export const createMemoryReason = params => {
10
+ return new MemoryReason(params);
11
+ };
12
+
13
+ //# sourceMappingURL=MemoryReason.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["MemoryReason","name","constructor","params","maximum","current","description","createMemoryReason"],"sources":["MemoryReason.ts"],"sourcesContent":["import type { IReason } from \"~/utils/waitUntilHealthy/reason/IReason.js\";\n\nexport interface IMemoryReasonParams {\n maximum: number;\n current: number;\n description?: string;\n}\n\nexport class MemoryReason implements IReason {\n public readonly name = \"memory\";\n public readonly maximum: number;\n public readonly current: number;\n public readonly description?: string;\n\n public constructor(params: IMemoryReasonParams) {\n this.maximum = params.maximum;\n this.current = params.current;\n this.description = params.description;\n }\n}\n\nexport const createMemoryReason = (params: IMemoryReasonParams): MemoryReason => {\n return new MemoryReason(params);\n};\n"],"mappings":"AAQA,OAAO,MAAMA,YAAY,CAAoB;EACzBC,IAAI,GAAG,QAAQ;EAKxBC,WAAWA,CAACC,MAA2B,EAAE;IAC5C,IAAI,CAACC,OAAO,GAAGD,MAAM,CAACC,OAAO;IAC7B,IAAI,CAACC,OAAO,GAAGF,MAAM,CAACE,OAAO;IAC7B,IAAI,CAACC,WAAW,GAAGH,MAAM,CAACG,WAAW;EACzC;AACJ;AAEA,OAAO,MAAMC,kBAAkB,GAAIJ,MAA2B,IAAmB;EAC7E,OAAO,IAAIH,YAAY,CAACG,MAAM,CAAC;AACnC,CAAC","ignoreList":[]}