nextmv 0.30.0__py3-none-any.whl → 0.31.0__py3-none-any.whl

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.
@@ -0,0 +1,1147 @@
1
+ """
2
+ Application module for interacting with local Nextmv applications.
3
+
4
+ This module provides functionality to interact with applications in Nextmv,
5
+ including application management, running applications, and managing inputs.
6
+
7
+ Classes
8
+ -------
9
+ DownloadURL
10
+ Result of getting a download URL.
11
+ PollingOptions
12
+ Options for polling when waiting for run results.
13
+ UploadURL
14
+ Result of getting an upload URL.
15
+ Application
16
+ Class for interacting with applications in Nextmv Cloud.
17
+
18
+ Functions
19
+ ---------
20
+ poll
21
+ Function to poll for results with configurable options.
22
+ """
23
+
24
+ import json
25
+ import os
26
+ import shutil
27
+ import tempfile
28
+ import webbrowser
29
+ from dataclasses import dataclass
30
+ from datetime import datetime, timezone
31
+ from typing import Any, Optional, Union
32
+
33
+ from nextmv import cloud
34
+ from nextmv._serialization import deflated_serialize_json
35
+ from nextmv.base_model import BaseModel
36
+ from nextmv.input import DEFAULT_INPUT_JSON_FILE, INPUTS_KEY, Input, InputFormat
37
+ from nextmv.local.executor import LOGS_FILE
38
+ from nextmv.local.runner import run
39
+ from nextmv.logger import log
40
+ from nextmv.manifest import Manifest
41
+ from nextmv.options import Options
42
+ from nextmv.output import DEFAULT_OUTPUT_JSON_FILE, LOGS_KEY, OUTPUTS_KEY, SOLUTIONS_KEY, OutputFormat
43
+ from nextmv.polling import DEFAULT_POLLING_OPTIONS, PollingOptions, poll
44
+ from nextmv.run import ErrorLog, Format, RunConfiguration, RunInformation, RunResult, TrackedRun, TrackedRunStatus
45
+ from nextmv.safe import safe_id
46
+ from nextmv.status import StatusV2
47
+
48
+
49
+ @dataclass
50
+ class Application:
51
+ """
52
+ A decision model that can be executed.
53
+
54
+ You can import the `Application` class directly from `local`:
55
+
56
+ ```python
57
+ from nextmv.local import Application
58
+ ```
59
+
60
+ This class represents an application in Nextmv, providing methods to
61
+ interact with the application, run it with different inputs, manage
62
+ versions, instances, experiments, and more.
63
+
64
+ Parameters
65
+ ----------
66
+ src : str
67
+ Source of the application, when initialized locally. An application's
68
+ source typically refers to the directory containing the `app.yaml`
69
+ manifest.
70
+ description : Optional[str], default=None
71
+ Description of the application.
72
+
73
+ Examples
74
+ --------
75
+ >>> from nextmv.local import Application
76
+ >>> app = Application(src="path/to/app")
77
+ >>> # Retrieve an app's run result
78
+ >>> result = app.run_result("run-id")
79
+ """
80
+
81
+ src: str
82
+ """
83
+ Source of the application, when initialized locally. An application's
84
+ source typically refers to the directory containing the `app.yaml`
85
+ manifest.
86
+ """
87
+
88
+ description: Optional[str] = None
89
+ """Description of the application."""
90
+
91
+ @classmethod
92
+ def initialize(
93
+ cls,
94
+ src: Optional[str] = None,
95
+ description: Optional[str] = None,
96
+ destination: Optional[str] = None,
97
+ ) -> "Application":
98
+ """
99
+ Initialize a sample Nextmv application, locally.
100
+
101
+ This method will create a new application in the local file system. The
102
+ application is a dir with the name given by `src` (it becomes the
103
+ _source_ of the app), under the location given by `destination`. If the
104
+ `destination` parameter is not specified, the current working directory
105
+ is used as default. This method will scaffold the application with the
106
+ necessary files and directories to have an opinionated structure for
107
+ your decision model. Once the application is initialized, you are
108
+ encouraged to complete it with the decision model itself, so that the
109
+ application can be run locally.
110
+
111
+ If the `src` parameter is not provided, a random name will be generated
112
+ for the application.
113
+
114
+ Parameters
115
+ ----------
116
+ src : str, optional
117
+ Source (ID, name) of the application. Will be generated if not
118
+ provided.
119
+ description : str, optional
120
+ Description of the application.
121
+ destination : str, optional
122
+ Destination directory where the application will be initialized. If
123
+ not provided, the current working directory will be used.
124
+
125
+ Returns
126
+ -------
127
+ Application
128
+ The initialized application instance.
129
+ """
130
+
131
+ destination_dir = os.getcwd() if destination is None else destination
132
+ app_id = src if src is not None else safe_id("app")
133
+
134
+ # Create the new directory with the given name.
135
+ app_src = os.path.join(destination_dir, app_id)
136
+ if os.path.exists(app_src):
137
+ raise FileExistsError(f"destination dir for src already exists: {app_src}")
138
+
139
+ os.makedirs(app_src, exist_ok=False)
140
+
141
+ # Get the path to the initial app structure template.
142
+ current_file_dir = os.path.dirname(os.path.abspath(__file__))
143
+ initial_app_structure_path = os.path.join(current_file_dir, "..", "default_app")
144
+ initial_app_structure_path = os.path.normpath(initial_app_structure_path)
145
+
146
+ # Copy everything from initial_app_structure to the new directory.
147
+ if os.path.exists(initial_app_structure_path):
148
+ shutil.copytree(initial_app_structure_path, app_src, dirs_exist_ok=True)
149
+
150
+ return cls(
151
+ src=app_src,
152
+ description=description,
153
+ )
154
+
155
+ def run_metadata(self, run_id: str) -> RunInformation:
156
+ """
157
+ Get the metadata of a local run.
158
+
159
+ This method is the local equivalent to
160
+ `cloud.Application.run_metadata`, which retrieves the metadata of a
161
+ remote run in Nextmv Cloud. This method is used to get the metadata of
162
+ a run that was executed locally using the `new_run` or
163
+ `new_run_with_result` method.
164
+
165
+ Retrieves information about a run without including the run output.
166
+ This is useful when you only need the run's status and metadata.
167
+
168
+ Parameters
169
+ ----------
170
+ run_id : str
171
+ ID of the run to retrieve metadata for.
172
+
173
+ Returns
174
+ -------
175
+ RunInformation
176
+ Metadata of the run (run information without output).
177
+
178
+ Raises
179
+ ------
180
+ ValueError
181
+ If the `.nextmv/runs` directory does not exist at the application
182
+ source, or if the specified run ID does not exist.
183
+
184
+ Examples
185
+ --------
186
+ >>> metadata = app.run_metadata("run-789")
187
+ >>> print(metadata.metadata.status_v2)
188
+ StatusV2.succeeded
189
+ """
190
+
191
+ runs_dir = os.path.join(self.src, ".nextmv", "runs")
192
+ if not os.path.exists(runs_dir):
193
+ raise ValueError(f"`.nextmv/runs` dir does not exist at app source: {self.src}")
194
+
195
+ run_dir = os.path.join(runs_dir, run_id)
196
+ if not os.path.exists(run_dir):
197
+ raise ValueError(f"`{run_id}` run dir does not exist at: {runs_dir}")
198
+
199
+ info_file = os.path.join(run_dir, f"{run_id}.json")
200
+ if not os.path.exists(info_file):
201
+ raise ValueError(f"`{info_file}` file does not exist at: {run_dir}")
202
+
203
+ with open(info_file) as f:
204
+ info_dict = json.load(f)
205
+
206
+ info = RunInformation.from_dict(info_dict)
207
+
208
+ return info
209
+
210
+ def run_result(self, run_id: str, output_dir_path: Optional[str] = ".") -> RunResult:
211
+ """
212
+ Get the local result of a run.
213
+
214
+ This method is the local equivalent to `cloud.Application.run_result`,
215
+ which retrieves the result of a remote run in Nextmv Cloud. This method
216
+ is used to get the result of a run that was executed locally using the
217
+ `new_run` or `new_run_with_result` method.
218
+
219
+ Retrieves the complete result of a run, including the run output.
220
+
221
+ Parameters
222
+ ----------
223
+ run_id : str
224
+ ID of the run to get results for.
225
+ output_dir_path : Optional[str], default="."
226
+ Path to a directory where non-JSON output files will be saved. This
227
+ is required if the output is non-JSON. If the directory does not
228
+ exist, it will be created. Uses the current directory by default.
229
+
230
+ Returns
231
+ -------
232
+ RunResult
233
+ Result of the run, including output.
234
+
235
+ Raises
236
+ ------
237
+ ValueError
238
+ If the `.nextmv/runs` directory does not exist at the application
239
+ source, or if the specified run ID does not exist.
240
+
241
+ Examples
242
+ --------
243
+ >>> result = app.run_result("run-123")
244
+ >>> print(result.metadata.status_v2)
245
+ 'succeeded'
246
+ """
247
+
248
+ run_information = self.run_metadata(run_id=run_id)
249
+
250
+ return self.__run_result(
251
+ run_id=run_id,
252
+ run_information=run_information,
253
+ output_dir_path=output_dir_path,
254
+ )
255
+
256
+ def run_result_with_polling(
257
+ self,
258
+ run_id: str,
259
+ polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
260
+ output_dir_path: Optional[str] = ".",
261
+ ) -> RunResult:
262
+ """
263
+ Get the result of a local run with polling.
264
+
265
+ This method is the local equivalent to
266
+ `cloud.Application.run_result_with_polling`, which retrieves the result
267
+ of a remote run in Nextmv Cloud. This method is used to get the result
268
+ of a run that was executed locally using the `new_run` or
269
+ `new_run_with_result` method.
270
+
271
+ Retrieves the result of a run including the run output. This method
272
+ polls for the result until the run finishes executing or the polling
273
+ strategy is exhausted.
274
+
275
+ Parameters
276
+ ----------
277
+ run_id : str
278
+ ID of the run to retrieve the result for.
279
+ polling_options : PollingOptions, default=_DEFAULT_POLLING_OPTIONS
280
+ Options to use when polling for the run result.
281
+ output_dir_path : Optional[str], default="."
282
+ Path to a directory where non-JSON output files will be saved. This
283
+ is required if the output is non-JSON. If the directory does not
284
+ exist, it will be created. Uses the current directory by default.
285
+
286
+ Returns
287
+ -------
288
+ RunResult
289
+ Complete result of the run including output data.
290
+
291
+ Raises
292
+ ------
293
+ requests.HTTPError
294
+ If the response status code is not 2xx.
295
+ TimeoutError
296
+ If the run does not complete after the polling strategy is
297
+ exhausted based on time duration.
298
+ RuntimeError
299
+ If the run does not complete after the polling strategy is
300
+ exhausted based on number of tries.
301
+
302
+ Examples
303
+ --------
304
+ >>> from nextmv.cloud import PollingOptions
305
+ >>> # Create custom polling options
306
+ >>> polling_opts = PollingOptions(max_tries=50, max_duration=600)
307
+ >>> # Get run result with polling
308
+ >>> result = app.run_result_with_polling("run-123", polling_opts)
309
+ >>> print(result.output)
310
+ {'solution': {...}}
311
+ """
312
+
313
+ def polling_func() -> tuple[Any, bool]:
314
+ run_information = self.run_metadata(run_id=run_id)
315
+ if run_information.metadata.status_v2 in {
316
+ StatusV2.succeeded,
317
+ StatusV2.failed,
318
+ StatusV2.canceled,
319
+ }:
320
+ return run_information, True
321
+
322
+ return None, False
323
+
324
+ run_information = poll(polling_options=polling_options, polling_func=polling_func)
325
+
326
+ return self.__run_result(
327
+ run_id=run_id,
328
+ run_information=run_information,
329
+ output_dir_path=output_dir_path,
330
+ )
331
+
332
+ def run_visuals(self, run_id: str) -> None:
333
+ """
334
+ Open the local run visuals in a web browser.
335
+
336
+ This method opens the visual representation of a locally executed run
337
+ in the default web browser. It assumes that the run was executed locally
338
+ using the `new_run` or `new_run_with_result` method and that
339
+ the necessary visualization files are present.
340
+
341
+ If the run was correctly configured to produce visual assets, then the
342
+ run will contain a `visuals` directory with one or more HTML files.
343
+ Each file is opened in a new tab in the default web browser.
344
+
345
+ Parameters
346
+ ----------
347
+ run_id : str
348
+ ID of the local run to visualize.
349
+
350
+ Raises
351
+ ------
352
+ ValueError
353
+ If the `.nextmv/runs` directory does not exist at the application
354
+ source, or if the specified run ID does not exist.
355
+ """
356
+
357
+ runs_dir = os.path.join(self.src, ".nextmv", "runs")
358
+ if not os.path.exists(runs_dir):
359
+ raise ValueError(f"`.nextmv/runs` dir does not exist at app source: {self.src}")
360
+
361
+ run_dir = os.path.join(runs_dir, run_id)
362
+ if not os.path.exists(run_dir):
363
+ raise ValueError(f"`{run_id}` run dir does not exist at: {runs_dir}")
364
+
365
+ visuals_dir = os.path.join(run_dir, "visuals")
366
+ if not os.path.exists(visuals_dir):
367
+ raise ValueError(f"`visuals` dir does not exist at: {run_dir}")
368
+
369
+ for file in os.listdir(visuals_dir):
370
+ if file.endswith(".html"):
371
+ file_path = os.path.join(visuals_dir, file)
372
+ webbrowser.open_new_tab(f"file://{os.path.realpath(file_path)}")
373
+
374
+ def new_run(
375
+ self,
376
+ input: Union[Input, dict[str, Any], BaseModel, str] = None,
377
+ name: Optional[str] = None,
378
+ description: Optional[str] = None,
379
+ options: Optional[Union[Options, dict[str, str]]] = None,
380
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]] = None,
381
+ json_configurations: Optional[dict[str, Any]] = None,
382
+ input_dir_path: Optional[str] = None,
383
+ ) -> str:
384
+ """
385
+ Run the application locally with the provided input.
386
+
387
+ This method is the local equivalent to `cloud.Application.new_run`,
388
+ which submits the input to Nextmv Cloud. This method runs the
389
+ application locally using the `src` of the app.
390
+
391
+ Make sure that the `src` attribute is set on the `Application` class
392
+ before running locally, as it is required by the method.
393
+
394
+ Parameters
395
+ ----------
396
+ input: Union[Input, dict[str, Any], BaseModel, str]
397
+ Input to use for the run. This can be a `nextmv.Input` object,
398
+ `dict`, `BaseModel` or `str`.
399
+
400
+ If `nextmv.Input` is used, and the `input_format` is either
401
+ `nextmv.InputFormat.JSON` or `nextmv.InputFormat.TEXT`, then the
402
+ input data is extracted from the `.data` property.
403
+
404
+ If you want to work with `nextmv.InputFormat.CSV_ARCHIVE` or
405
+ `nextmv.InputFormat.MULTI_FILE`, you should use the
406
+ `input_dir_path` argument instead. This argument takes precedence
407
+ over the `input`. If `input_dir_path` is specified, this function
408
+ looks for files in that directory and tars them, to later be
409
+ uploaded using the `upload_large_input` method. If both the
410
+ `input_dir_path` and `input` arguments are provided, the `input`
411
+ is ignored.
412
+
413
+ When `input_dir_path` is specified, the `configuration` argument
414
+ must also be provided. More specifically, the
415
+ `RunConfiguration.format.format_input.input_type` parameter
416
+ dictates what kind of input is being submitted to the Nextmv Cloud.
417
+ Make sure that this parameter is specified when working with the
418
+ following input formats:
419
+
420
+ - `nextmv.InputFormat.CSV_ARCHIVE`
421
+ - `nextmv.InputFormat.MULTI_FILE`
422
+
423
+ When working with JSON or text data, use the `input` argument
424
+ directly.
425
+
426
+ In general, if an input is too large, it will be uploaded with the
427
+ `upload_large_input` method.
428
+ name: Optional[str]
429
+ Name of the local run.
430
+ description: Optional[str]
431
+ Description of the local run.
432
+ options: Optional[Union[Options, dict[str, str]]]
433
+ Options to use for the run. This can be a `nextmv.Options` object
434
+ or a dict. If a dict is used, the keys must be strings and the
435
+ values must be strings as well. If a `nextmv.Options` object is
436
+ used, the options are extracted from the `.to_cloud_dict()` method.
437
+ Note that specifying `options` overrides the `input.options` (if
438
+ the `input` is of type `nextmv.Input`).
439
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]]
440
+ Configuration to use for the run. This can be a
441
+ `cloud.RunConfiguration` object or a dict. If the object is used,
442
+ then the `.to_dict()` method is applied to extract the
443
+ configuration.
444
+ json_configurations: Optional[dict[str, Any]]
445
+ Optional configurations for JSON serialization. This is used to
446
+ customize the serialization before data is sent.
447
+ input_dir_path: Optional[str]
448
+ Path to a directory containing input files. This is useful for
449
+ input formats like `nextmv.InputFormat.CSV_ARCHIVE` or
450
+ `nextmv.InputFormat.MULTI_FILE`. If both `input` and
451
+ `input_dir_path` are specified, the `input` is ignored, and the
452
+ files in the directory are used instead.
453
+
454
+ Returns
455
+ -------
456
+ str
457
+ ID (`run_id`) of the local run that was executed.
458
+
459
+ Raises
460
+ ------
461
+ ValueError
462
+ If the `src` property for the `Application` is not specified.
463
+ If neither `input` nor `input_dir_path` is specified.
464
+ If `input_dir_path` is specified but `configuration` is not provided.
465
+ FileNotFoundError
466
+ If the manifest.yaml file cannot be found in the specified `src` directory.
467
+
468
+ Examples
469
+ --------
470
+ >>> from nextmv.local import Application
471
+ >>> app = Application(id="my-app", src="/path/to/app")
472
+ >>> run_id = app.new_run(
473
+ ... input={"vehicles": [{"id": "v1"}]},
474
+ ... options={"duration": "10s"}
475
+ ... )
476
+ >>> print(f"Local run completed with ID: {run_id}")
477
+ """
478
+
479
+ self.__validate_input_dir_path_and_configuration(input_dir_path, configuration)
480
+
481
+ if self.src is None:
482
+ raise ValueError("`src` property for the `Application` must be specified to run the application locally")
483
+
484
+ if input is None and input_dir_path is None:
485
+ raise ValueError("Either `input` or `input_directory` must be specified")
486
+
487
+ try:
488
+ manifest = Manifest.from_yaml(self.src)
489
+ except FileNotFoundError as e:
490
+ raise FileNotFoundError(
491
+ f"Could not find manifest.yaml in {self.src}. Maybe specify a different `src` dir?"
492
+ ) from e
493
+
494
+ input_data = None if input_dir_path else self.__extract_input_data(input)
495
+ options_dict = self.__extract_options_dict(options, json_configurations)
496
+ run_config_dict = self.__extract_run_config(input, configuration, input_dir_path)
497
+ run_id = run(
498
+ app_id=self.src,
499
+ src=self.src,
500
+ manifest=manifest,
501
+ run_config=run_config_dict,
502
+ name=name,
503
+ description=description,
504
+ input_data=input_data,
505
+ inputs_dir_path=input_dir_path,
506
+ options=options_dict,
507
+ )
508
+
509
+ return run_id
510
+
511
+ def new_run_with_result(
512
+ self,
513
+ input: Union[Input, dict[str, Any], BaseModel, str] = None,
514
+ name: Optional[str] = None,
515
+ description: Optional[str] = None,
516
+ run_options: Optional[Union[Options, dict[str, str]]] = None,
517
+ polling_options: PollingOptions = DEFAULT_POLLING_OPTIONS,
518
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]] = None,
519
+ json_configurations: Optional[dict[str, Any]] = None,
520
+ input_dir_path: Optional[str] = None,
521
+ output_dir_path: Optional[str] = ".",
522
+ ) -> RunResult:
523
+ """
524
+ Submit an input to start a new local run of the application and poll
525
+ for the result. This is a convenience method that combines the
526
+ `new_run` and `run_result_with_polling` methods, applying
527
+ polling logic to check when the local run succeeded.
528
+
529
+ This method is the local equivalent to
530
+ `cloud.Application.new_run_with_result`, which submits the input to
531
+ Nextmv Cloud. This method runs the application locally using the `src`
532
+ of the app.
533
+
534
+ Make sure that the `src` attribute is set on the `Application` class
535
+ before running locally, as it is required by the method.
536
+
537
+ Parameters
538
+ ----------
539
+ input: Union[Input, dict[str, Any], BaseModel, str]
540
+ Input to use for the run. This can be a `nextmv.Input` object,
541
+ `dict`, `BaseModel` or `str`.
542
+
543
+ If `nextmv.Input` is used, and the `input_format` is either
544
+ `nextmv.InputFormat.JSON` or `nextmv.InputFormat.TEXT`, then the
545
+ input data is extracted from the `.data` property.
546
+
547
+ If you want to work with `nextmv.InputFormat.CSV_ARCHIVE` or
548
+ `nextmv.InputFormat.MULTI_FILE`, you should use the
549
+ `input_dir_path` argument instead. This argument takes precedence
550
+ over the `input`. If `input_dir_path` is specified, this function
551
+ looks for files in that directory and tars them, to later be
552
+ uploaded using the `upload_large_input` method. If both the
553
+ `input_dir_path` and `input` arguments are provided, the `input` is
554
+ ignored.
555
+
556
+ When `input_dir_path` is specified, the `configuration` argument
557
+ must also be provided. More specifically, the
558
+ `RunConfiguration.format.format_input.input_type` parameter
559
+ dictates what kind of input is being submitted to the Nextmv Cloud.
560
+ Make sure that this parameter is specified when working with the
561
+ following input formats:
562
+
563
+ - `nextmv.InputFormat.CSV_ARCHIVE`
564
+ - `nextmv.InputFormat.MULTI_FILE`
565
+
566
+ When working with JSON or text data, use the `input` argument
567
+ directly.
568
+
569
+ In general, if an input is too large, it will be uploaded with the
570
+ `upload_large_input` method.
571
+ name: Optional[str]
572
+ Name of the local run.
573
+ description: Optional[str]
574
+ Description of the local run.
575
+ run_options: Optional[Union[Options, dict[str, str]]]
576
+ Options to use for the run. This can be a `nextmv.Options` object
577
+ or a dict. If a dict is used, the keys must be strings and the
578
+ values must be strings as well. If a `nextmv.Options` object is
579
+ used, the options are extracted from the `.to_cloud_dict()` method.
580
+ Note that specifying `options` overrides the `input.options` (if
581
+ the `input` is of type `nextmv.Input`).
582
+ polling_options: PollingOptions, default=_DEFAULT_POLLING_OPTIONS
583
+ Options to use when polling for the run result.
584
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]]
585
+ Configuration to use for the run. This can be a
586
+ `cloud.RunConfiguration` object or a dict. If the object is used,
587
+ then the `.to_dict()` method is applied to extract the
588
+ configuration.
589
+ json_configurations: Optional[dict[str, Any]]
590
+ Optional configurations for JSON serialization. This is used to
591
+ customize the serialization before data is sent.
592
+ input_dir_path: Optional[str]
593
+ Path to a directory containing input files. This is useful for
594
+ input formats like `nextmv.InputFormat.CSV_ARCHIVE` or
595
+ `nextmv.InputFormat.MULTI_FILE`. If both `input` and
596
+ `input_dir_path` are specified, the `input` is ignored, and the
597
+ files in the directory are used instead.
598
+ output_dir_path : Optional[str], default="."
599
+ Path to a directory where non-JSON output files will be saved. This
600
+ is required if the output is non-JSON. If the directory does not
601
+ exist, it will be created. Uses the current directory by default.
602
+
603
+ Returns
604
+ -------
605
+ RunResult
606
+ Result of the run, including output.
607
+
608
+ Raises
609
+ ------
610
+ ValueError
611
+ If the `src` property for the `Application` is not specified. If
612
+ neither `input` nor `inputs_dir_path` is specified. If
613
+ `inputs_dir_path` is specified but `configuration` is not provided.
614
+ FileNotFoundError
615
+ If the manifest.yaml file cannot be found in the specified `src`
616
+ directory.
617
+
618
+ Examples
619
+ --------
620
+ >>> from nextmv.local import Application
621
+ >>> app = Application(id="my-app", src="/path/to/app")
622
+ >>> run_result = app.new_run_with_result(
623
+ ... input={"vehicles": [{"id": "v1"}]},
624
+ ... options={"duration": "10s"}
625
+ ... )
626
+ >>> print(f"Local run completed with ID: {run_result.id}")
627
+ """
628
+
629
+ run_id = self.new_run(
630
+ input=input,
631
+ name=name,
632
+ description=description,
633
+ options=run_options,
634
+ configuration=configuration,
635
+ json_configurations=json_configurations,
636
+ input_dir_path=input_dir_path,
637
+ )
638
+
639
+ return self.run_result_with_polling(
640
+ run_id=run_id,
641
+ polling_options=polling_options,
642
+ output_dir_path=output_dir_path,
643
+ )
644
+
645
+ def sync( # noqa: C901
646
+ self,
647
+ target: cloud.Application,
648
+ run_ids: Optional[list[str]] = None,
649
+ instance_id: Optional[str] = None,
650
+ verbose: Optional[bool] = False,
651
+ ) -> None:
652
+ """
653
+ Sync the local application to a Nextmv Cloud application target.
654
+
655
+ The `Application` class allows you to perform and handle local
656
+ application runs with methods such as:
657
+
658
+ - `new_run`
659
+ - `new_run_with_result`
660
+ - `run_metadata`
661
+ - `run_result`
662
+ - `run_result_with_polling`
663
+
664
+ The runs produced locally live under `self.src/.nextmv/runs`. This
665
+ method syncs those runs to a Nextmv Cloud application target, making
666
+ them available for remote execution and management.
667
+
668
+ Parameters
669
+ ----------
670
+ target : cloud.Application
671
+ Target Nextmv Cloud application where the local application runs
672
+ will be synced to.
673
+ run_ids : Optional[list[str]], default=None
674
+ List of run IDs to sync. If None, all local runs found under
675
+ `self.src/.nextmv/runs` will be synced.
676
+ instance_id : Optional[str], default=None
677
+ Optional instance ID if you want to associate your runs with an
678
+ instance.
679
+ verbose : Optional[bool], default=False
680
+ Whether to print verbose output during the sync process. Useful for
681
+ debugging a large number of runs being synced.
682
+
683
+ Raises
684
+ ------
685
+ ValueError
686
+ If the `src` property is not specified.
687
+ ValueError
688
+ If the `client` property is not specified.
689
+ ValueError
690
+ If the application does not exist in Nextmv Cloud.
691
+ ValueError
692
+ If a run does not exist locally.
693
+ requests.HTTPError
694
+ If the response status code is not 2xx.
695
+ """
696
+ if self.src is None:
697
+ raise ValueError(
698
+ "`src` property for the `Application` must be specified to sync the application to Nextmv Cloud"
699
+ )
700
+
701
+ if target.client is None:
702
+ raise ValueError(
703
+ "`client` property for the target `Application` must be specified to sync the application to Cloud"
704
+ )
705
+
706
+ if not target.exists(target.client, target.id):
707
+ raise ValueError(
708
+ "target Application does not exist in Nextmv Cloud, create it with `cloud.Application.new`"
709
+ )
710
+ if verbose:
711
+ log(f"☁️ Starting sync of local application `{self.src}` to Nextmv Cloud application `{target.id}`.")
712
+
713
+ # Create a temp dir to store the outputs that are written by default to
714
+ # ".". During the sync process, we don't need to keep these outputs, so
715
+ # we can use a temp dir that will be deleted after the sync is done.
716
+ with tempfile.TemporaryDirectory(prefix="nextmv-sync-run-") as temp_results_dir:
717
+ runs_dir = os.path.join(self.src, ".nextmv", "runs")
718
+ if run_ids is None:
719
+ # If runs are not specified, by default we sync all local runs that
720
+ # can be found.
721
+ dirs = os.listdir(runs_dir)
722
+ run_ids = [d for d in dirs if os.path.isdir(os.path.join(runs_dir, d))]
723
+
724
+ if verbose:
725
+ log(f"ℹ️ Found {len(run_ids)} local runs to sync from {runs_dir}.")
726
+ else:
727
+ if verbose:
728
+ log(f"ℹ️ Syncing {len(run_ids)} specified local runs from {runs_dir}.")
729
+
730
+ total = 0
731
+ for run_id in run_ids:
732
+ synced = self.__sync_run(
733
+ target=target,
734
+ run_id=run_id,
735
+ runs_dir=runs_dir,
736
+ temp_dir=temp_results_dir,
737
+ instance_id=instance_id,
738
+ verbose=verbose,
739
+ )
740
+ if synced:
741
+ total += 1
742
+
743
+ if verbose:
744
+ log(
745
+ f"🚀 Process completed, synced local application `{self.src}` to "
746
+ f"Nextmv Cloud application `{target.id}`: "
747
+ f"{total}/{len(run_ids)} runs."
748
+ )
749
+
750
+ def __run_result(
751
+ self,
752
+ run_id: str,
753
+ run_information: RunInformation,
754
+ output_dir_path: Optional[str] = ".",
755
+ ) -> RunResult:
756
+ """
757
+ Get the result of a local run.
758
+
759
+ This is a private method that retrieves the complete result of a run,
760
+ including the output data, from a local source. This method serves as
761
+ the base implementation for retrieving run results, regardless of
762
+ polling strategy.
763
+
764
+ Parameters
765
+ ----------
766
+ run_id : str
767
+ ID of the run to retrieve the result for.
768
+ run_information : RunInformation
769
+ Information about the run, including metadata such as output size.
770
+ output_dir_path : Optional[str], default="."
771
+ Path to a directory where non-JSON output files will be saved. This
772
+ is required if the output is non-JSON. If the directory does not
773
+ exist, it will be created. Uses the current directory by default.
774
+
775
+ Returns
776
+ -------
777
+ RunResult
778
+ Result of the run, including all metadata and output data.
779
+
780
+ Raises
781
+ ------
782
+ ValueError
783
+ If the output format is not JSON and no output_dir_path is
784
+ provided.
785
+ If the output format is unknown.
786
+ """
787
+
788
+ result = RunResult.from_dict(run_information.to_dict())
789
+ if result.metadata.error:
790
+ result.error_log = ErrorLog(error=result.metadata.error)
791
+
792
+ if result.metadata.status_v2 != StatusV2.succeeded:
793
+ return result
794
+
795
+ # See whether we can attach the output directly or need to save to the given
796
+ # directory
797
+ output_type = run_information.metadata.format.format_output.output_type
798
+ if output_type != OutputFormat.JSON and (not output_dir_path or output_dir_path == ""):
799
+ raise ValueError(
800
+ "The output format is not JSON: an `output_dir_path` must be provided.",
801
+ )
802
+
803
+ runs_dir = os.path.join(self.src, ".nextmv", "runs")
804
+ solutions_dir = os.path.join(runs_dir, run_id, OUTPUTS_KEY, SOLUTIONS_KEY)
805
+
806
+ if output_type == OutputFormat.JSON:
807
+ with open(os.path.join(solutions_dir, DEFAULT_OUTPUT_JSON_FILE)) as f:
808
+ result.output = json.load(f)
809
+ elif output_type in {OutputFormat.CSV_ARCHIVE, OutputFormat.MULTI_FILE}:
810
+ shutil.copytree(solutions_dir, output_dir_path, dirs_exist_ok=True)
811
+ else:
812
+ raise ValueError(f"Unknown output type: {output_type}")
813
+
814
+ return result
815
+
816
+ def __validate_input_dir_path_and_configuration(
817
+ self,
818
+ input_dir_path: Optional[str],
819
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]],
820
+ ) -> None:
821
+ """
822
+ Auxiliary function to validate the directory path and configuration.
823
+ """
824
+
825
+ if input_dir_path is None or input_dir_path == "":
826
+ return
827
+
828
+ if configuration is None:
829
+ raise ValueError(
830
+ "If dir_path is provided, a RunConfiguration must also be provided.",
831
+ )
832
+
833
+ config_format = self.__extract_config_format(configuration)
834
+
835
+ if config_format is None:
836
+ raise ValueError(
837
+ "If dir_path is provided, RunConfiguration.format must also be provided.",
838
+ )
839
+
840
+ input_type = self.__extract_input_type(config_format)
841
+
842
+ if input_type is None or input_type in (InputFormat.JSON, InputFormat.TEXT):
843
+ raise ValueError(
844
+ "If dir_path is provided, RunConfiguration.format.format_input.input_type must be set to a valid type. "
845
+ f"Valid types are: {[InputFormat.CSV_ARCHIVE, InputFormat.MULTI_FILE]}",
846
+ )
847
+
848
+ def __extract_config_format(self, configuration: Union[RunConfiguration, dict[str, Any]]) -> Any:
849
+ """Extract format from configuration, handling both RunConfiguration objects and dicts."""
850
+ if isinstance(configuration, RunConfiguration):
851
+ return configuration.format
852
+
853
+ if isinstance(configuration, dict):
854
+ config_format = configuration.get("format")
855
+ if config_format is not None and isinstance(config_format, dict):
856
+ return Format.from_dict(config_format) if hasattr(Format, "from_dict") else config_format
857
+
858
+ return config_format
859
+
860
+ raise ValueError("Configuration must be a RunConfiguration object or a dict.")
861
+
862
+ def __extract_input_type(self, config_format: Any) -> Any:
863
+ """Extract input type from config format."""
864
+ if isinstance(config_format, dict):
865
+ format_input = config_format.get("format_input") or config_format.get("input")
866
+ if format_input is None:
867
+ raise ValueError(
868
+ "If dir_path is provided, RunConfiguration.format.format_input must also be provided.",
869
+ )
870
+
871
+ if isinstance(format_input, dict):
872
+ return format_input.get("input_type") or format_input.get("type")
873
+
874
+ return getattr(format_input, "input_type", None)
875
+
876
+ # Handle Format object
877
+ if config_format.format_input is None:
878
+ raise ValueError(
879
+ "If dir_path is provided, RunConfiguration.format.format_input must also be provided.",
880
+ )
881
+
882
+ return config_format.format_input.input_type
883
+
884
+ def __extract_input_data(
885
+ self,
886
+ input: Union[Input, dict[str, Any], BaseModel, str] = None,
887
+ ) -> Optional[Union[dict[str, Any], str]]:
888
+ """
889
+ Auxiliary function to extract the input data from the input, based on
890
+ its type.
891
+ """
892
+
893
+ input_data = None
894
+ if isinstance(input, BaseModel):
895
+ input_data = input.to_dict()
896
+ elif isinstance(input, dict) or isinstance(input, str):
897
+ input_data = input
898
+ elif isinstance(input, Input):
899
+ input_data = input.data
900
+
901
+ return input_data
902
+
903
+ def __extract_options_dict(
904
+ self,
905
+ options: Optional[Union[Options, dict[str, str]]] = None,
906
+ json_configurations: Optional[dict[str, Any]] = None,
907
+ ) -> dict[str, str]:
908
+ """
909
+ Auxiliary function to extract the options that will be sent to the
910
+ application for execution.
911
+ """
912
+
913
+ options_dict = {}
914
+ if options is not None:
915
+ if isinstance(options, Options):
916
+ options_dict = options.to_dict_cloud()
917
+ elif isinstance(options, dict):
918
+ for k, v in options.items():
919
+ if isinstance(v, str):
920
+ options_dict[k] = v
921
+ else:
922
+ options_dict[k] = deflated_serialize_json(v, json_configurations=json_configurations)
923
+
924
+ return options_dict
925
+
926
+ def __extract_run_config(
927
+ self,
928
+ input: Union[Input, dict[str, Any], BaseModel, str] = None,
929
+ configuration: Optional[Union[RunConfiguration, dict[str, Any]]] = None,
930
+ dir_path: Optional[str] = None,
931
+ ) -> dict[str, Any]:
932
+ """
933
+ Auxiliary function to extract the run configuration that will be sent
934
+ to the application for execution.
935
+ """
936
+
937
+ if configuration is not None:
938
+ configuration_dict = (
939
+ configuration.to_dict() if isinstance(configuration, RunConfiguration) else configuration
940
+ )
941
+ return configuration_dict
942
+
943
+ configuration = RunConfiguration()
944
+ configuration.resolve(input=input, dir_path=dir_path)
945
+ configuration_dict = configuration.to_dict()
946
+
947
+ return configuration_dict
948
+
949
+ def __sync_run( # noqa: C901
950
+ self,
951
+ target: cloud.Application,
952
+ run_id: str,
953
+ runs_dir: str,
954
+ temp_dir: str,
955
+ instance_id: Optional[str] = None,
956
+ verbose: Optional[bool] = False,
957
+ ) -> bool:
958
+ """
959
+ Syncs a local run to a Nextmv Cloud target application. Returns True if
960
+ the run was synced, False if it was skipped (already synced).
961
+ """
962
+
963
+ if verbose:
964
+ log(f"🔄 Syncing local run `{run_id}`... ")
965
+
966
+ # For files-based runs, the result files are written by default to ".".
967
+ # Avoid this using a dedicated temp dir.
968
+ run_result = self.run_result(run_id, output_dir_path=temp_dir)
969
+ input_type = run_result.metadata.format.format_input.input_type
970
+
971
+ # Skip runs that have already been synced.
972
+ already_synced = run_result.synced_run_id is not None and run_result.synced_at is not None
973
+ if already_synced:
974
+ if verbose:
975
+ log(f" ⏭️ Skipping local run `{run_id}`, already synced at {run_result.synced_at.isoformat()}.")
976
+
977
+ return False
978
+
979
+ # Skip runs that don't have the supported type. TODO: delete this when
980
+ # external runs support CSV_ARCHIVE and MULTI_FILE. Right now,
981
+ # submitting an external result with a new run is limited to JSON and
982
+ # TEXT. After this if statement is removed, the rest of the code should
983
+ # work with CSV_ARCHIVE and MULTI_FILE as well, as using the input dir
984
+ # path is already considered.
985
+ if input_type not in {InputFormat.JSON, InputFormat.TEXT}:
986
+ if verbose:
987
+ log(
988
+ f" ⏭️ Skipping local run `{run_id}`, unsupported input type: {input_type.value}. "
989
+ f"Supported types are: {[InputFormat.JSON.value, InputFormat.TEXT.value]}",
990
+ )
991
+
992
+ return False
993
+
994
+ # Check that it is a valid run with inputs, outputs, logs, etc.
995
+ if not self.__valid_run_result(run_result, runs_dir, run_id):
996
+ if verbose:
997
+ log(f" ❌ Skipping local run `{run_id}`, invalid run (missing inputs, outputs or logs).")
998
+
999
+ return False
1000
+
1001
+ status = TrackedRunStatus.SUCCEEDED
1002
+ if run_result.metadata.status_v2 != StatusV2.succeeded:
1003
+ status = TrackedRunStatus.FAILED
1004
+
1005
+ # Read the logs of the run and place each line as an element in a list
1006
+ run_dir = os.path.join(runs_dir, run_id)
1007
+ with open(os.path.join(run_dir, LOGS_KEY, LOGS_FILE)) as f:
1008
+ stderr_logs = f.readlines()
1009
+
1010
+ # Create the tracked run object and start configuring it.
1011
+ tracked_run = TrackedRun(
1012
+ status=status,
1013
+ duration=int(run_result.metadata.duration),
1014
+ error=run_result.metadata.error,
1015
+ logs=stderr_logs,
1016
+ name=run_result.name,
1017
+ description=run_result.description,
1018
+ )
1019
+
1020
+ # Resolve the input according to its type.
1021
+ inputs_path = os.path.join(run_dir, INPUTS_KEY)
1022
+ if input_type == InputFormat.JSON:
1023
+ with open(os.path.join(inputs_path, DEFAULT_INPUT_JSON_FILE)) as f:
1024
+ tracked_run.input = json.load(f)
1025
+ elif input_type == InputFormat.TEXT:
1026
+ with open(os.path.join(inputs_path, "input")) as f:
1027
+ tracked_run.input = f.read()
1028
+ else:
1029
+ tracked_run.input_dir_path = inputs_path
1030
+
1031
+ # Resolve the output according to its type.
1032
+ if run_result.metadata.format.format_output.output_type == OutputFormat.JSON:
1033
+ tracked_run.output = run_result.output
1034
+ else:
1035
+ tracked_run.output_dir_path = os.path.join(run_dir, OUTPUTS_KEY, SOLUTIONS_KEY)
1036
+
1037
+ # Actually sync the run by tracking it remotely on Nextmv Cloud.
1038
+ configuration = RunConfiguration(
1039
+ format=Format(
1040
+ format_input=run_result.metadata.format.format_input,
1041
+ format_output=run_result.metadata.format.format_output,
1042
+ ),
1043
+ )
1044
+ tracked_id = target.track_run(
1045
+ tracked_run=tracked_run,
1046
+ instance_id=instance_id,
1047
+ configuration=configuration,
1048
+ )
1049
+
1050
+ # Mark the local run as synced by updating the local run info.
1051
+ run_result.synced_run_id = tracked_id
1052
+ run_result.synced_at = datetime.now(timezone.utc)
1053
+ with open(os.path.join(run_dir, f"{run_id}.json"), "w") as f:
1054
+ json.dump(run_result.to_dict(), f, indent=2)
1055
+
1056
+ if verbose:
1057
+ log(f"✅ Synced local run `{run_id}` as remote run `{tracked_id}`.")
1058
+
1059
+ return True
1060
+
1061
+ def __valid_run_result(self, run_result: RunResult, runs_dir: str, run_id: str) -> bool:
1062
+ """
1063
+ Validate that a run result has all required files and directories.
1064
+
1065
+ This method checks that a local run has the expected directory structure
1066
+ and files, including inputs, outputs, and logs.
1067
+
1068
+ Parameters
1069
+ ----------
1070
+ run_result : RunResult
1071
+ The run result to validate.
1072
+ runs_dir : str
1073
+ Path to the runs directory.
1074
+ run_id : str
1075
+ ID of the run to validate.
1076
+
1077
+ Returns
1078
+ -------
1079
+ bool
1080
+ True if the run is valid, False otherwise.
1081
+ """
1082
+ run_dir = os.path.join(runs_dir, run_id)
1083
+
1084
+ # Check that the run directory exists
1085
+ if not os.path.exists(run_dir):
1086
+ return False
1087
+
1088
+ # Validate inputs
1089
+ if not self.__validate_inputs(run_dir, run_result.metadata.format.format_input.input_type):
1090
+ return False
1091
+
1092
+ # Validate outputs
1093
+ if not self.__validate_outputs(run_dir, run_result.metadata.format.format_output.output_type):
1094
+ return False
1095
+
1096
+ # Validate logs
1097
+ if not self.__validate_logs(run_dir):
1098
+ return False
1099
+
1100
+ return True
1101
+
1102
+ def __validate_inputs(self, run_dir: str, input_type: InputFormat) -> bool:
1103
+ """Validate that the inputs directory and files exist for the given input type."""
1104
+ inputs_path = os.path.join(run_dir, INPUTS_KEY)
1105
+ if not os.path.exists(inputs_path):
1106
+ return False
1107
+
1108
+ if input_type == InputFormat.JSON:
1109
+ input_file = os.path.join(inputs_path, DEFAULT_INPUT_JSON_FILE)
1110
+
1111
+ return os.path.isfile(input_file)
1112
+
1113
+ if input_type == InputFormat.TEXT:
1114
+ input_file = os.path.join(inputs_path, "input")
1115
+
1116
+ return os.path.isfile(input_file)
1117
+
1118
+ # For CSV_ARCHIVE and MULTI_FILE, inputs_path should be a directory
1119
+ return os.path.isdir(inputs_path)
1120
+
1121
+ def __validate_outputs(self, run_dir: str, output_type: OutputFormat) -> bool:
1122
+ """Validate that the outputs directory and files exist for the given output type."""
1123
+ outputs_dir = os.path.join(run_dir, OUTPUTS_KEY)
1124
+ if not os.path.exists(outputs_dir):
1125
+ return False
1126
+
1127
+ solutions_dir = os.path.join(outputs_dir, SOLUTIONS_KEY)
1128
+ if not os.path.exists(solutions_dir):
1129
+ return False
1130
+
1131
+ if output_type == OutputFormat.JSON:
1132
+ solution_file = os.path.join(solutions_dir, DEFAULT_OUTPUT_JSON_FILE)
1133
+
1134
+ return os.path.isfile(solution_file)
1135
+
1136
+ # For CSV_ARCHIVE and MULTI_FILE, solutions_dir should be a directory
1137
+ return os.path.isdir(solutions_dir)
1138
+
1139
+ def __validate_logs(self, run_dir: str) -> bool:
1140
+ """Validate that the logs directory and file exist."""
1141
+ logs_dir = os.path.join(run_dir, LOGS_KEY)
1142
+ if not os.path.exists(logs_dir):
1143
+ return False
1144
+
1145
+ logs_file = os.path.join(logs_dir, LOGS_FILE)
1146
+
1147
+ return os.path.isfile(logs_file)