UNPKG

4.33 kBJavaScriptView Raw
1const _ = require('lodash');
2const inspector = require('./inspector');
3const utils = require('../../config/utils');
4const config = require('../../config/index');
5
6const log = config.log;
7
8const Subtask = function (params) {
9 const self = this;
10
11 inspector.sanitize(SANITIZATION_SCHEMA, params);
12 const result = inspector.validate(VALIDATION_SCHEMA, params);
13
14 if (!result.valid) {
15 throw new Error(result.format());
16 }
17
18 _.merge(self, params);
19
20 const idSource = {};
21 _.merge(idSource, params);
22 delete idSource.count;
23
24 self.getID = () => JSON.stringify(idSource);
25 self.toString = () => JSON.stringify(params);
26
27 return self;
28};
29Subtask.coerce = (subtask) => subtask instanceof Subtask ? subtask : new Subtask(subtask);
30Subtask.DEFAULT_FLUSH_SIZE = 100;
31
32/**
33 * Static factory for creating subtasks directly from the ID and count
34 *
35 * @param id
36 * @param count
37 * @returns {Subtask}
38 */
39Subtask.createFromID = (id, count) => {
40 if (!utils.isNonZeroString(id)) {
41 throw new Error('id must be stringified json');
42 }
43
44 const params = JSON.parse(id);
45 params.count = count;
46
47 return new Subtask(params);
48};
49
50Subtask.createQuery = (index, type, flushSize, minSize, maxSize) => {
51 const request = {
52 index: index,
53 type: type,
54 size: flushSize,
55 };
56
57 if (flushSize > 0) {
58 request.scroll = '30m';
59 }
60
61 const finalMinSize = minSize || 0;
62 const finalMaxSize = maxSize || -1;
63 if (finalMinSize >= 0 && finalMaxSize >= 0) {
64 request.body = {
65 query: {
66 range: {
67 _size: {
68 gte: minSize,
69 lt: maxSize
70 }
71 }
72 }
73 };
74 }
75 log.info(`Generated Query: ${JSON.stringify(request, null, 2)}`);
76 return request;
77};
78
79const VALIDATION_SCHEMA = {
80 type: 'object',
81 strict: true,
82 properties: {
83 source: {
84 $type: 'elasticsearch_v'
85 },
86 destination: {
87 $type: 'elasticsearch_v'
88 },
89 transfer: {
90 type: 'object',
91 strict: true,
92 properties: {
93 index: {
94 type: 'string',
95 optional: true,
96 minLength: 1
97 },
98 template: {
99 type: 'string',
100 optional: true,
101 minLength: 1
102 },
103 flushSize: {
104 type: 'integer',
105 optional: false,
106 def: Subtask.DEFAULT_FLUSH_SIZE
107 },
108 documents: {
109 type: 'object',
110 optional: true,
111 strict: true,
112 properties: {
113 index: {
114 type: 'string',
115 minLength: 1
116 },
117 type: {
118 type: 'string',
119 minLength: 1
120 },
121 minSize: {
122 type: 'integer',
123 optional: false,
124 def: -1
125 },
126 maxSize: {
127 type: 'integer',
128 optional: false,
129 def: -1
130 },
131 }
132 }
133 }
134 },
135 mutators: {
136 $type: 'mutators_v',
137 optional: true
138 },
139 count: {
140 type: 'integer',
141 gte: 0
142 }
143 }
144};
145
146const SANITIZATION_SCHEMA = {
147 type: 'object',
148 properties: {
149 source: {
150 $type: 'elasticsearch_s'
151 },
152 destination: {
153 $type: 'elasticsearch_s'
154 },
155 transfer: {
156 type: 'object',
157 properties: {
158 flushSize: {
159 type: 'integer',
160 optional: false,
161 def: Subtask.DEFAULT_FLUSH_SIZE
162 },
163 index: {
164 minLength: 1
165 },
166 template: {
167 minLength: 1
168 },
169 documents: {
170 type: 'object',
171 optional: true,
172 properties: {
173 index: {
174 minLength: 1
175 },
176 type: {
177 minLength: 1
178 },
179 minSize: {
180 type: 'integer',
181 optional: false,
182 def: -1
183 },
184 maxSize: {
185 type: 'integer',
186 optional: false,
187 def: -1
188 },
189 }
190 }
191 }
192 },
193 mutators: {
194 $type: 'mutators_S',
195 optional: true
196 },
197 count: {
198 type: 'integer',
199 gte: 0
200 }
201 }
202};
203
204module.exports = Subtask;
\No newline at end of file