|
8 | 8 | import os
|
9 | 9 | from typing import Any, Dict, List, Optional, Set, Union
|
10 | 10 |
|
11 |
| -from stac_fastapi.core.models.patch import ElasticPath, ESCommandSet |
12 |
| -from stac_fastapi.types.stac import ( |
13 |
| - Item, |
14 |
| - PatchAddReplaceTest, |
15 |
| - PatchOperation, |
16 |
| - PatchRemove, |
17 |
| -) |
| 11 | +from stac_fastapi.types.stac import Item |
18 | 12 |
|
19 | 13 | MAX_LIMIT = 10000
|
20 | 14 |
|
@@ -184,195 +178,3 @@ def dict_deep_update(merge_to: Dict[str, Any], merge_from: Dict[str, Any]) -> No
|
184 | 178 | dict_deep_update(merge_to[k], merge_from[k])
|
185 | 179 | else:
|
186 | 180 | merge_to[k] = v
|
187 |
| - |
188 |
| - |
189 |
| -def merge_to_operations(data: Dict) -> List: |
190 |
| - """Convert merge operation to list of RF6902 operations. |
191 |
| -
|
192 |
| - Args: |
193 |
| - data: dictionary to convert. |
194 |
| -
|
195 |
| - Returns: |
196 |
| - List: list of RF6902 operations. |
197 |
| - """ |
198 |
| - operations = [] |
199 |
| - |
200 |
| - for key, value in data.copy().items(): |
201 |
| - |
202 |
| - if value is None: |
203 |
| - operations.append(PatchRemove(op="remove", path=key)) |
204 |
| - |
205 |
| - elif isinstance(value, dict): |
206 |
| - nested_operations = merge_to_operations(value) |
207 |
| - |
208 |
| - for nested_operation in nested_operations: |
209 |
| - nested_operation.path = f"{key}.{nested_operation.path}" |
210 |
| - operations.append(nested_operation) |
211 |
| - |
212 |
| - else: |
213 |
| - operations.append(PatchAddReplaceTest(op="add", path=key, value=value)) |
214 |
| - |
215 |
| - return operations |
216 |
| - |
217 |
| - |
218 |
| -def check_commands( |
219 |
| - commands: ESCommandSet, |
220 |
| - op: str, |
221 |
| - path: ElasticPath, |
222 |
| - from_path: bool = False, |
223 |
| -) -> None: |
224 |
| - """Add Elasticsearch checks to operation. |
225 |
| -
|
226 |
| - Args: |
227 |
| - commands (List[str]): current commands |
228 |
| - op (str): the operation of script |
229 |
| - path (Dict): path of variable to run operation on |
230 |
| - from_path (bool): True if path is a from path |
231 |
| -
|
232 |
| - """ |
233 |
| - if path.nest: |
234 |
| - commands.add( |
235 |
| - f"if (!ctx._source.containsKey('{path.nest}'))" |
236 |
| - f"{{Debug.explain('{path.nest} does not exist');}}" |
237 |
| - ) |
238 |
| - |
239 |
| - if path.index or op in ["remove", "replace", "test"] or from_path: |
240 |
| - commands.add( |
241 |
| - f"if (!ctx._source{path.es_nest}.containsKey('{path.key}'))" |
242 |
| - f"{{Debug.explain('{path.key} does not exist in {path.nest}');}}" |
243 |
| - ) |
244 |
| - |
245 |
| - if from_path and path.index is not None: |
246 |
| - commands.add( |
247 |
| - f"if ((ctx._source{path.es_location} instanceof ArrayList" |
248 |
| - f" && ctx._source{path.es_location}.size() < {path.index})" |
249 |
| - f" || (!(ctx._source{path.es_location} instanceof ArrayList)" |
250 |
| - f" && !ctx._source{path.es_location}.containsKey('{path.index}')))" |
251 |
| - f"{{Debug.explain('{path.path} does not exist');}}" |
252 |
| - ) |
253 |
| - |
254 |
| - |
255 |
| -def remove_commands(commands: ESCommandSet, path: ElasticPath) -> None: |
256 |
| - """Remove value at path. |
257 |
| -
|
258 |
| - Args: |
259 |
| - commands (List[str]): current commands |
260 |
| - path (ElasticPath): Path to value to be removed |
261 |
| -
|
262 |
| - """ |
263 |
| - if path.index is not None: |
264 |
| - commands.add( |
265 |
| - f"def {path.variable_name} = ctx._source{path.es_location}.remove({path.index});" |
266 |
| - ) |
267 |
| - |
268 |
| - else: |
269 |
| - commands.add( |
270 |
| - f"def {path.variable_name} = ctx._source{path.es_nest}.remove('{path.key}');" |
271 |
| - ) |
272 |
| - |
273 |
| - |
274 |
| -def add_commands( |
275 |
| - commands: ESCommandSet, |
276 |
| - operation: PatchOperation, |
277 |
| - path: ElasticPath, |
278 |
| - from_path: ElasticPath, |
279 |
| - params: Dict, |
280 |
| -) -> None: |
281 |
| - """Add value at path. |
282 |
| -
|
283 |
| - Args: |
284 |
| - commands (List[str]): current commands |
285 |
| - operation (PatchOperation): operation to run |
286 |
| - path (ElasticPath): path for value to be added |
287 |
| -
|
288 |
| - """ |
289 |
| - if from_path is not None: |
290 |
| - value = ( |
291 |
| - from_path.variable_name |
292 |
| - if operation.op == "move" |
293 |
| - else f"ctx._source.{from_path.es_path}" |
294 |
| - ) |
295 |
| - else: |
296 |
| - value = f"params.{path.param_key}" |
297 |
| - params[path.param_key] = operation.value |
298 |
| - |
299 |
| - if path.index is not None: |
300 |
| - commands.add( |
301 |
| - f"if (ctx._source{path.es_location} instanceof ArrayList)" |
302 |
| - f"{{ctx._source{path.es_location}.{'add' if operation.op in ['add', 'move'] else 'set'}({path.index}, {value})}}" |
303 |
| - f"else{{ctx._source.{path.es_path} = {value}}}" |
304 |
| - ) |
305 |
| - |
306 |
| - else: |
307 |
| - commands.add(f"ctx._source.{path.es_path} = {value};") |
308 |
| - |
309 |
| - |
310 |
| -def test_commands( |
311 |
| - commands: ESCommandSet, operation: PatchOperation, path: ElasticPath, params: Dict |
312 |
| -) -> None: |
313 |
| - """Test value at path. |
314 |
| -
|
315 |
| - Args: |
316 |
| - commands (List[str]): current commands |
317 |
| - operation (PatchOperation): operation to run |
318 |
| - path (ElasticPath): path for value to be tested |
319 |
| - """ |
320 |
| - value = f"params.{path.param_key}" |
321 |
| - params[path.param_key] = operation.value |
322 |
| - |
323 |
| - commands.add( |
324 |
| - f"if (ctx._source.{path.es_path} != {value})" |
325 |
| - f"{{Debug.explain('Test failed `{path.path}` | " |
326 |
| - f"{operation.json_value} != ' + ctx._source.{path.es_path});}}" |
327 |
| - ) |
328 |
| - |
329 |
| - |
330 |
| -def operations_to_script(operations: List) -> Dict: |
331 |
| - """Convert list of operation to painless script. |
332 |
| -
|
333 |
| - Args: |
334 |
| - operations: List of RF6902 operations. |
335 |
| -
|
336 |
| - Returns: |
337 |
| - Dict: elasticsearch update script. |
338 |
| - """ |
339 |
| - commands: ESCommandSet = ESCommandSet() |
340 |
| - params: Dict = {} |
341 |
| - |
342 |
| - for operation in operations: |
343 |
| - path = ElasticPath(path=operation.path) |
344 |
| - from_path = ( |
345 |
| - ElasticPath(path=operation.from_) if hasattr(operation, "from_") else None |
346 |
| - ) |
347 |
| - |
348 |
| - check_commands(commands=commands, op=operation.op, path=path) |
349 |
| - if from_path is not None: |
350 |
| - check_commands( |
351 |
| - commands=commands, op=operation.op, path=from_path, from_path=True |
352 |
| - ) |
353 |
| - |
354 |
| - if operation.op in ["remove", "move"]: |
355 |
| - remove_path = from_path if from_path else path |
356 |
| - remove_commands(commands=commands, path=remove_path) |
357 |
| - |
358 |
| - if operation.op in ["add", "replace", "copy", "move"]: |
359 |
| - add_commands( |
360 |
| - commands=commands, |
361 |
| - operation=operation, |
362 |
| - path=path, |
363 |
| - from_path=from_path, |
364 |
| - params=params, |
365 |
| - ) |
366 |
| - |
367 |
| - if operation.op == "test": |
368 |
| - test_commands( |
369 |
| - commands=commands, operation=operation, path=path, params=params |
370 |
| - ) |
371 |
| - |
372 |
| - source = "".join(commands) |
373 |
| - |
374 |
| - return { |
375 |
| - "source": source, |
376 |
| - "lang": "painless", |
377 |
| - "params": params, |
378 |
| - } |
0 commit comments