/
ml.py
4573 lines (4357 loc) · 206 KB
/
ml.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import typing as t
from elastic_transport import ObjectApiResponse
from ._base import NamespacedClient
from .utils import SKIP_IN_PATH, _quote, _rewrite_parameters
class MlClient(NamespacedClient):
@_rewrite_parameters(
body_fields=True,
)
async def close_job(
self,
*,
job_id: str,
allow_no_match: t.Optional[bool] = None,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
force: t.Optional[bool] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
timeout: t.Optional[t.Union["t.Literal[-1]", "t.Literal[0]", str]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Closes one or more anomaly detection jobs. A job can be opened and closed multiple
times throughout its lifecycle.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-close-job.html>`_
:param job_id: Identifier for the anomaly detection job. It can be a job identifier,
a group name, or a wildcard expression. You can close multiple anomaly detection
jobs in a single API request by using a group name, a comma-separated list
of jobs, or a wildcard expression. You can close all jobs by using `_all`
or by specifying `*` as the job identifier.
:param allow_no_match: Refer to the description for the `allow_no_match` query
parameter.
:param force: Refer to the descriptiion for the `force` query parameter.
:param timeout: Refer to the description for the `timeout` query parameter.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/_close"
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if allow_no_match is not None:
__body["allow_no_match"] = allow_no_match
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if force is not None:
__body["force"] = force
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if timeout is not None:
__body["timeout"] = timeout
if not __body:
__body = None # type: ignore[assignment]
__headers = {"accept": "application/json"}
if __body is not None:
__headers["content-type"] = "application/json"
return await self.perform_request( # type: ignore[return-value]
"POST", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters()
async def delete_calendar(
self,
*,
calendar_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes a calendar.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-calendar.html>`_
:param calendar_id: A string that uniquely identifies a calendar.
"""
if calendar_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'calendar_id'")
__path = f"/_ml/calendars/{_quote(calendar_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_calendar_event(
self,
*,
calendar_id: str,
event_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes scheduled events from a calendar.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-calendar-event.html>`_
:param calendar_id: The ID of the calendar to modify
:param event_id: The ID of the event to remove from the calendar
"""
if calendar_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'calendar_id'")
if event_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'event_id'")
__path = f"/_ml/calendars/{_quote(calendar_id)}/events/{_quote(event_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_calendar_job(
self,
*,
calendar_id: str,
job_id: t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]],
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes anomaly detection jobs from a calendar.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-calendar-job.html>`_
:param calendar_id: A string that uniquely identifies a calendar.
:param job_id: An identifier for the anomaly detection jobs. It can be a job
identifier, a group name, or a comma-separated list of jobs or groups.
"""
if calendar_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'calendar_id'")
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
__path = f"/_ml/calendars/{_quote(calendar_id)}/jobs/{_quote(job_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_data_frame_analytics(
self,
*,
id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
force: t.Optional[bool] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
timeout: t.Optional[t.Union["t.Literal[-1]", "t.Literal[0]", str]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes an existing data frame analytics job.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/delete-dfanalytics.html>`_
:param id: Identifier for the data frame analytics job.
:param force: If `true`, it deletes a job that is not stopped; this method is
quicker than stopping and deleting the job.
:param timeout: The time to wait for the job to be deleted.
"""
if id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'id'")
__path = f"/_ml/data_frame/analytics/{_quote(id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if force is not None:
__query["force"] = force
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if timeout is not None:
__query["timeout"] = timeout
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_datafeed(
self,
*,
datafeed_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
force: t.Optional[bool] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes an existing datafeed.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-datafeed.html>`_
:param datafeed_id: A numerical character string that uniquely identifies the
datafeed. This identifier can contain lowercase alphanumeric characters (a-z
and 0-9), hyphens, and underscores. It must start and end with alphanumeric
characters.
:param force: Use to forcefully delete a started datafeed; this method is quicker
than stopping and deleting the datafeed.
"""
if datafeed_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'datafeed_id'")
__path = f"/_ml/datafeeds/{_quote(datafeed_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if force is not None:
__query["force"] = force
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters(
body_fields=True,
)
async def delete_expired_data(
self,
*,
job_id: t.Optional[str] = None,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
requests_per_second: t.Optional[float] = None,
timeout: t.Optional[t.Union["t.Literal[-1]", "t.Literal[0]", str]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes expired and unused machine learning data.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-expired-data.html>`_
:param job_id: Identifier for an anomaly detection job. It can be a job identifier,
a group name, or a wildcard expression.
:param requests_per_second: The desired requests per second for the deletion
processes. The default behavior is no throttling.
:param timeout: How long can the underlying delete processes run until they are
canceled.
"""
if job_id not in SKIP_IN_PATH:
__path = f"/_ml/_delete_expired_data/{_quote(job_id)}"
else:
__path = "/_ml/_delete_expired_data"
__query: t.Dict[str, t.Any] = {}
__body: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if requests_per_second is not None:
__body["requests_per_second"] = requests_per_second
if timeout is not None:
__body["timeout"] = timeout
if not __body:
__body = None # type: ignore[assignment]
__headers = {"accept": "application/json"}
if __body is not None:
__headers["content-type"] = "application/json"
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters()
async def delete_filter(
self,
*,
filter_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes a filter.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-filter.html>`_
:param filter_id: A string that uniquely identifies a filter.
"""
if filter_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'filter_id'")
__path = f"/_ml/filters/{_quote(filter_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_forecast(
self,
*,
job_id: str,
forecast_id: t.Optional[str] = None,
allow_no_forecasts: t.Optional[bool] = None,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
timeout: t.Optional[t.Union["t.Literal[-1]", "t.Literal[0]", str]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes forecasts from a machine learning job.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-forecast.html>`_
:param job_id: Identifier for the anomaly detection job.
:param forecast_id: A comma-separated list of forecast identifiers. If you do
not specify this optional parameter or if you specify `_all` or `*` the API
deletes all forecasts from the job.
:param allow_no_forecasts: Specifies whether an error occurs when there are no
forecasts. In particular, if this parameter is set to `false` and there are
no forecasts associated with the job, attempts to delete all forecasts return
an error.
:param timeout: Specifies the period of time to wait for the completion of the
delete operation. When this period of time elapses, the API fails and returns
an error.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
if job_id not in SKIP_IN_PATH and forecast_id not in SKIP_IN_PATH:
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/_forecast/{_quote(forecast_id)}"
elif job_id not in SKIP_IN_PATH:
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/_forecast"
else:
raise ValueError("Couldn't find a path for the given parameters")
__query: t.Dict[str, t.Any] = {}
if allow_no_forecasts is not None:
__query["allow_no_forecasts"] = allow_no_forecasts
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if timeout is not None:
__query["timeout"] = timeout
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_job(
self,
*,
job_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
force: t.Optional[bool] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
wait_for_completion: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes an existing anomaly detection job.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-job.html>`_
:param job_id: Identifier for the anomaly detection job.
:param force: Use to forcefully delete an opened job; this method is quicker
than closing and deleting the job.
:param wait_for_completion: Specifies whether the request should return immediately
or wait until the job deletion completes.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if force is not None:
__query["force"] = force
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if wait_for_completion is not None:
__query["wait_for_completion"] = wait_for_completion
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_model_snapshot(
self,
*,
job_id: str,
snapshot_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes an existing model snapshot.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-delete-snapshot.html>`_
:param job_id: Identifier for the anomaly detection job.
:param snapshot_id: Identifier for the model snapshot.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
if snapshot_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'snapshot_id'")
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/model_snapshots/{_quote(snapshot_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_trained_model(
self,
*,
model_id: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
force: t.Optional[bool] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes an existing trained inference model that is currently not referenced
by an ingest pipeline.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/delete-trained-models.html>`_
:param model_id: The unique identifier of the trained model.
:param force: Forcefully deletes a trained model that is referenced by ingest
pipelines or has a started deployment.
"""
if model_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'model_id'")
__path = f"/_ml/trained_models/{_quote(model_id)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if force is not None:
__query["force"] = force
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters()
async def delete_trained_model_alias(
self,
*,
model_id: str,
model_alias: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Deletes a model alias that refers to the trained model
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/delete-trained-models-aliases.html>`_
:param model_id: The trained model ID to which the model alias refers.
:param model_alias: The model alias to delete.
"""
if model_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'model_id'")
if model_alias in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'model_alias'")
__path = f"/_ml/trained_models/{_quote(model_id)}/model_aliases/{_quote(model_alias)}"
__query: t.Dict[str, t.Any] = {}
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"DELETE", __path, params=__query, headers=__headers
)
@_rewrite_parameters(
body_fields=True,
)
async def estimate_model_memory(
self,
*,
analysis_config: t.Optional[t.Mapping[str, t.Any]] = None,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
max_bucket_cardinality: t.Optional[t.Mapping[str, int]] = None,
overall_cardinality: t.Optional[t.Mapping[str, int]] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Estimates the model memory
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-apis.html>`_
:param analysis_config: For a list of the properties that you can specify in
the `analysis_config` component of the body of this API.
:param max_bucket_cardinality: Estimates of the highest cardinality in a single
bucket that is observed for influencer fields over the time period that the
job analyzes data. To produce a good answer, values must be provided for
all influencer fields. Providing values for fields that are not listed as
`influencers` has no effect on the estimation.
:param overall_cardinality: Estimates of the cardinality that is observed for
fields over the whole time period that the job analyzes data. To produce
a good answer, values must be provided for fields referenced in the `by_field_name`,
`over_field_name` and `partition_field_name` of any detectors. Providing
values for other fields has no effect on the estimation. It can be omitted
from the request if no detectors have a `by_field_name`, `over_field_name`
or `partition_field_name`.
"""
__path = "/_ml/anomaly_detectors/_estimate_model_memory"
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if analysis_config is not None:
__body["analysis_config"] = analysis_config
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if max_bucket_cardinality is not None:
__body["max_bucket_cardinality"] = max_bucket_cardinality
if overall_cardinality is not None:
__body["overall_cardinality"] = overall_cardinality
if pretty is not None:
__query["pretty"] = pretty
__headers = {"accept": "application/json", "content-type": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"POST", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters(
body_fields=True,
)
async def evaluate_data_frame(
self,
*,
evaluation: t.Mapping[str, t.Any],
index: str,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
query: t.Optional[t.Mapping[str, t.Any]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Evaluates the data frame analytics for an annotated index.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/evaluate-dfanalytics.html>`_
:param evaluation: Defines the type of evaluation you want to perform.
:param index: Defines the `index` in which the evaluation will be performed.
:param query: A query clause that retrieves a subset of data from the source
index.
"""
if evaluation is None:
raise ValueError("Empty value passed for parameter 'evaluation'")
if index is None:
raise ValueError("Empty value passed for parameter 'index'")
__path = "/_ml/data_frame/_evaluate"
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if evaluation is not None:
__body["evaluation"] = evaluation
if index is not None:
__body["index"] = index
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if query is not None:
__body["query"] = query
__headers = {"accept": "application/json", "content-type": "application/json"}
return await self.perform_request( # type: ignore[return-value]
"POST", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters(
body_fields=True,
)
async def explain_data_frame_analytics(
self,
*,
id: t.Optional[str] = None,
allow_lazy_start: t.Optional[bool] = None,
analysis: t.Optional[t.Mapping[str, t.Any]] = None,
analyzed_fields: t.Optional[t.Mapping[str, t.Any]] = None,
description: t.Optional[str] = None,
dest: t.Optional[t.Mapping[str, t.Any]] = None,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
max_num_threads: t.Optional[int] = None,
model_memory_limit: t.Optional[str] = None,
pretty: t.Optional[bool] = None,
source: t.Optional[t.Mapping[str, t.Any]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Explains a data frame analytics config.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/explain-dfanalytics.html>`_
:param id: Identifier for the data frame analytics job. This identifier can contain
lowercase alphanumeric characters (a-z and 0-9), hyphens, and underscores.
It must start and end with alphanumeric characters.
:param allow_lazy_start: Specifies whether this job can start when there is insufficient
machine learning node capacity for it to be immediately assigned to a node.
:param analysis: The analysis configuration, which contains the information necessary
to perform one of the following types of analysis: classification, outlier
detection, or regression.
:param analyzed_fields: Specify includes and/or excludes patterns to select which
fields will be included in the analysis. The patterns specified in excludes
are applied last, therefore excludes takes precedence. In other words, if
the same field is specified in both includes and excludes, then the field
will not be included in the analysis.
:param description: A description of the job.
:param dest: The destination configuration, consisting of index and optionally
results_field (ml by default).
:param max_num_threads: The maximum number of threads to be used by the analysis.
Using more threads may decrease the time necessary to complete the analysis
at the cost of using more CPU. Note that the process may use additional threads
for operational functionality other than the analysis itself.
:param model_memory_limit: The approximate maximum amount of memory resources
that are permitted for analytical processing. If your `elasticsearch.yml`
file contains an `xpack.ml.max_model_memory_limit` setting, an error occurs
when you try to create data frame analytics jobs that have `model_memory_limit`
values greater than that setting.
:param source: The configuration of how to source the analysis data. It requires
an index. Optionally, query and _source may be specified.
"""
if id not in SKIP_IN_PATH:
__path = f"/_ml/data_frame/analytics/{_quote(id)}/_explain"
else:
__path = "/_ml/data_frame/analytics/_explain"
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if allow_lazy_start is not None:
__body["allow_lazy_start"] = allow_lazy_start
if analysis is not None:
__body["analysis"] = analysis
if analyzed_fields is not None:
__body["analyzed_fields"] = analyzed_fields
if description is not None:
__body["description"] = description
if dest is not None:
__body["dest"] = dest
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if max_num_threads is not None:
__body["max_num_threads"] = max_num_threads
if model_memory_limit is not None:
__body["model_memory_limit"] = model_memory_limit
if pretty is not None:
__query["pretty"] = pretty
if source is not None:
__body["source"] = source
if not __body:
__body = None # type: ignore[assignment]
__headers = {"accept": "application/json"}
if __body is not None:
__headers["content-type"] = "application/json"
return await self.perform_request( # type: ignore[return-value]
"POST", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters(
body_fields=True,
)
async def flush_job(
self,
*,
job_id: str,
advance_time: t.Optional[t.Union[str, t.Any]] = None,
calc_interim: t.Optional[bool] = None,
end: t.Optional[t.Union[str, t.Any]] = None,
error_trace: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
pretty: t.Optional[bool] = None,
skip_time: t.Optional[t.Union[str, t.Any]] = None,
start: t.Optional[t.Union[str, t.Any]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Forces any buffered data to be processed by the job.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-flush-job.html>`_
:param job_id: Identifier for the anomaly detection job.
:param advance_time: Refer to the description for the `advance_time` query parameter.
:param calc_interim: Refer to the description for the `calc_interim` query parameter.
:param end: Refer to the description for the `end` query parameter.
:param skip_time: Refer to the description for the `skip_time` query parameter.
:param start: Refer to the description for the `start` query parameter.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/_flush"
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if advance_time is not None:
__body["advance_time"] = advance_time
if calc_interim is not None:
__body["calc_interim"] = calc_interim
if end is not None:
__body["end"] = end
if error_trace is not None:
__query["error_trace"] = error_trace
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if pretty is not None:
__query["pretty"] = pretty
if skip_time is not None:
__body["skip_time"] = skip_time
if start is not None:
__body["start"] = start
if not __body:
__body = None # type: ignore[assignment]
__headers = {"accept": "application/json"}
if __body is not None:
__headers["content-type"] = "application/json"
return await self.perform_request( # type: ignore[return-value]
"POST", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters(
body_fields=True,
)
async def forecast(
self,
*,
job_id: str,
duration: t.Optional[t.Union["t.Literal[-1]", "t.Literal[0]", str]] = None,
error_trace: t.Optional[bool] = None,
expires_in: t.Optional[t.Union["t.Literal[-1]", "t.Literal[0]", str]] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
human: t.Optional[bool] = None,
max_model_memory: t.Optional[str] = None,
pretty: t.Optional[bool] = None,
) -> ObjectApiResponse[t.Any]:
"""
Predicts the future behavior of a time series by using its historical behavior.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-forecast.html>`_
:param job_id: Identifier for the anomaly detection job. The job must be open
when you create a forecast; otherwise, an error occurs.
:param duration: Refer to the description for the `duration` query parameter.
:param expires_in: Refer to the description for the `expires_in` query parameter.
:param max_model_memory: Refer to the description for the `max_model_memory`
query parameter.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/_forecast"
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if duration is not None:
__body["duration"] = duration
if error_trace is not None:
__query["error_trace"] = error_trace
if expires_in is not None:
__body["expires_in"] = expires_in
if filter_path is not None:
__query["filter_path"] = filter_path
if human is not None:
__query["human"] = human
if max_model_memory is not None:
__body["max_model_memory"] = max_model_memory
if pretty is not None:
__query["pretty"] = pretty
if not __body:
__body = None # type: ignore[assignment]
__headers = {"accept": "application/json"}
if __body is not None:
__headers["content-type"] = "application/json"
return await self.perform_request( # type: ignore[return-value]
"POST", __path, params=__query, headers=__headers, body=__body
)
@_rewrite_parameters(
body_fields=True,
parameter_aliases={"from": "from_"},
)
async def get_buckets(
self,
*,
job_id: str,
timestamp: t.Optional[t.Union[str, t.Any]] = None,
anomaly_score: t.Optional[float] = None,
desc: t.Optional[bool] = None,
end: t.Optional[t.Union[str, t.Any]] = None,
error_trace: t.Optional[bool] = None,
exclude_interim: t.Optional[bool] = None,
expand: t.Optional[bool] = None,
filter_path: t.Optional[
t.Union[str, t.Union[t.List[str], t.Tuple[str, ...]]]
] = None,
from_: t.Optional[int] = None,
human: t.Optional[bool] = None,
page: t.Optional[t.Mapping[str, t.Any]] = None,
pretty: t.Optional[bool] = None,
size: t.Optional[int] = None,
sort: t.Optional[str] = None,
start: t.Optional[t.Union[str, t.Any]] = None,
) -> ObjectApiResponse[t.Any]:
"""
Retrieves anomaly detection job results for one or more buckets.
`<https://www.elastic.co/guide/en/elasticsearch/reference/8.4/ml-get-bucket.html>`_
:param job_id: Identifier for the anomaly detection job.
:param timestamp: The timestamp of a single bucket result. If you do not specify
this parameter, the API returns information about all buckets.
:param anomaly_score: Refer to the description for the `anomaly_score` query
parameter.
:param desc: Refer to the description for the `desc` query parameter.
:param end: Refer to the description for the `end` query parameter.
:param exclude_interim: Refer to the description for the `exclude_interim` query
parameter.
:param expand: Refer to the description for the `expand` query parameter.
:param from_: Skips the specified number of buckets.
:param page:
:param size: Specifies the maximum number of buckets to obtain.
:param sort: Refer to the desription for the `sort` query parameter.
:param start: Refer to the description for the `start` query parameter.
"""
if job_id in SKIP_IN_PATH:
raise ValueError("Empty value passed for parameter 'job_id'")
if job_id not in SKIP_IN_PATH and timestamp not in SKIP_IN_PATH:
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/results/buckets/{_quote(timestamp)}"
elif job_id not in SKIP_IN_PATH:
__path = f"/_ml/anomaly_detectors/{_quote(job_id)}/results/buckets"
else:
raise ValueError("Couldn't find a path for the given parameters")
__body: t.Dict[str, t.Any] = {}
__query: t.Dict[str, t.Any] = {}
if anomaly_score is not None:
__body["anomaly_score"] = anomaly_score
if desc is not None:
__body["desc"] = desc
if end is not None:
__body["end"] = end
if error_trace is not None:
__query["error_trace"] = error_trace
if exclude_interim is not None: