nsg-cli 0.1.3

CLI tool for the Neuroscience Gateway (NSG) BRAIN Initiative API
Documentation
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
# NSG CLI

A command-line interface for the [Neuroscience Gateway (NSG)](https://www.nsgportal.org/) BRAIN Initiative API.

## Features

- **Secure credential storage** - Store NSG credentials in `~/.nsg/credentials.json`
- **Job management** - Submit, monitor, and download results from NSG HPC jobs
- **Parallel job fetching** - Fast job listing with concurrent API requests (up to 2x speedup)
- **Beautiful CLI** - Colored output with progress indicators
- **XML API support** - Full support for NSG's REST API (XML-based)
- **Multiple commands** - Login, list, status, submit, and download operations
- **Flexible compilation** - Feature flags for parallel vs sequential processing

## Installation

### From `crates.io`

```bash
cargo install nsg-cli
```

### Build from source

```bash
cd nsg-cli
cargo build --release
```

The binary will be at `target/release/nsg`.

### Install globally

```bash
cargo install --path .
```

This installs the `nsg` binary to `~/.cargo/bin/`.

## Performance

### Parallel Job Fetching

By default, `nsg-cli` uses parallel processing to fetch job details concurrently, significantly improving performance when listing many jobs.

**Benchmark Results (43 jobs):**
- **Parallel (default)**: ~1.75 seconds
- **Sequential**: ~3.47 seconds
- **Speedup**: ~2x faster with parallel processing

### Build Options

```bash
# Default build with parallel support (recommended)
cargo build --release

# Build without parallel support (sequential only)
cargo build --release --no-default-features

# Explicitly enable parallel support
cargo build --release --features parallel
```

### When to Use Sequential

Sequential processing may be preferable when:
- Running on systems with limited CPU resources
- API rate limiting is a concern
- You have very few jobs (< 5)

For most users, the default parallel build provides the best performance.

See [BENCHMARKING.md](BENCHMARKING.md) for detailed benchmarking instructions.

## Quick Start

### 1. Login

First, authenticate with your NSG credentials:

```bash
nsg login
```

You'll be prompted for:

- NSG Username
- NSG Password (hidden input)
- NSG Application Key

Your credentials are stored in `~/.nsg/credentials.json` with secure permissions (0600 on Unix).

**Get NSG credentials at:** https://www.nsgportal.org/

### 2. List Jobs

View all your NSG jobs with detailed information (tool, status, dates):

```bash
nsg list
```

The list command automatically fetches detailed information for each job, including:
- Tool name (e.g., PY_EXPANSE)
- Job status (COMPLETED, RUNNING, etc.)
- Submission date
- Completion date
- Failed status

For additional job messages:

```bash
nsg list --detailed
```

### 3. Check Job Status

Monitor a specific job:

```bash
nsg status <JOB_ID>
```

Examples:

```bash
nsg status NGBW-JOB-PY_EXPANSE-xxxxx
nsg status https://nsgr.sdsc.edu:8443/cipresrest/v1/job/username/NGBW-JOB-PY_EXPANSE-xxxxx
```

### 4. Submit a Job

Submit a new job to NSG:

```bash
nsg submit <ZIP_FILE> --tool <TOOL_NAME>
```

Example:

```bash
nsg submit my_analysis.zip --tool PY_EXPANSE
```

Available tools:

- `PY_EXPANSE` - Python on EXPANSE (default)
- `GPU_PY_EXPANSE` - GPU-accelerated Python
- Other NSG tools as supported

### 5. Download Results

Download results from a completed job:

```bash
nsg download <JOB_ID>
```

Specify output directory:

```bash
nsg download <JOB_ID> --output ./my_results
```

## Commands

### `nsg login`

Authenticate and save credentials.

**Options:**

- `-u, --username <USERNAME>` - NSG username (or prompt)
- `-p, --password <PASSWORD>` - NSG password (or prompt securely)
- `-a, --app-key <APP_KEY>` - NSG application key (or prompt)
- `--no-verify` - Skip connection test

**Example:**

```bash
nsg login --username myuser --app-key MY_APP_KEY
```

### `nsg list`

List all jobs for the authenticated user with detailed information (tool, status, dates).

**Note:** By default, this command fetches detailed status for each job using parallel processing for optimal performance.

**Options:**

- `--detailed` - Show job messages for each job
- `--recent <N>` - Show only the N most recent jobs (default: 20)
- `--limit <N>` - Limit number of jobs to display
- `--all` - Show all jobs (override default 20-job limit)

**Job Information Displayed:**
- Job ID
- Tool name (e.g., PY_EXPANSE)
- Status (COMPLETED, RUNNING, FAILED, etc.)
- Submission timestamp
- Completion timestamp (for terminal jobs)
- Failed status

**Examples:**

```bash
nsg list                    # Show 20 most recent jobs with details
nsg list --all              # Show all jobs with details
nsg list --recent 5         # Show 5 most recent jobs
nsg list --limit 10         # Show first 10 jobs
nsg list --detailed         # Show job messages for recent jobs
nsg list --all --detailed   # Show all jobs with messages
```

### `nsg status <JOB>`

Check status of a specific job.

**Arguments:**

- `<JOB>` - Job URL or Job ID

**Example:**

```bash
nsg status NGBW-JOB-PY_EXPANSE-xxxxx
```

### `nsg submit <ZIP_FILE>`

Submit a new job to NSG.

**Arguments:**

- `<ZIP_FILE>` - Path to ZIP file containing job data

**Options:**

- `-t, --tool <TOOL>` - NSG tool to use (default: PY_EXPANSE)
- `--no-wait` - Don't wait for job submission confirmation

**Example:**

```bash
nsg submit job_data.zip --tool PY_EXPANSE
```

### `nsg download <JOB>`

Download results from a completed job with real-time progress tracking.

**Arguments:**

- `<JOB>` - Job URL or Job ID

**Options:**

- `-o, --output <DIR>` - Output directory (default: ./nsg_results)

**Features:**

- Real-time progress bar showing download speed and ETA
- File size display in appropriate units (B, KB, MB, GB)
- Automatic file size formatting

**Example:**

```bash
nsg download NGBW-JOB-PY_EXPANSE-xxxxx --output ./results
```

## NSG Job Package Structure

When submitting jobs, NSG expects a specific ZIP structure. For Python jobs:

```
job.zip
└── modeldir/
    ├── input.py          # Main Python script (required)
    ├── data.edf          # Input data files
    ├── params.json       # Configuration
    └── ...               # Other files
```

The main script should be named `input.py` for PY_EXPANSE tool.

## Configuration

Credentials are stored in: `~/.nsg/credentials.json`

**Format:**

```json
{
  "username": "your_username",
  "password": "your_password",
  "app_key": "your_app_key"
}
```

**Security:**

- On Unix systems, the file permissions are set to `0600` (read/write for owner only)
- Never commit this file to version control
- Keep your credentials secure

## API Documentation

This CLI interfaces with the NSG REST API:

- **Base URL:** `https://nsgr.sdsc.edu:8443/cipresrest/v1`
- **Authentication:** HTTP Basic Auth + `cipres-appkey` header
- **Response format:** XML

## Development

### Project Structure

```
nsg-cli/
├── Cargo.toml
├── src/
│   ├── main.rs           # CLI entry point
│   ├── lib.rs            # Library exports
│   ├── client.rs         # NSG API client
│   ├── config.rs         # Credential management
│   ├── models.rs         # Data structures & XML parsing
│   └── commands/         # CLI commands
│       ├── mod.rs
│       ├── login.rs
│       ├── list.rs
│       ├── status.rs
│       ├── submit.rs
│       └── download.rs
└── README.md
```

### Dependencies

- **clap** - CLI argument parsing
- **reqwest** - HTTP client
- **quick-xml** - XML parsing
- **serde** - Serialization
- **colored** - Terminal colors
- **indicatif** - Progress bars
- **rpassword** - Secure password input
- **rayon** - Parallel iteration for performance
- **criterion** - Benchmarking framework (dev dependency)

### Building

```bash
cargo build                      # Debug build
cargo build --release            # Release build with parallel support (optimized)
cargo build --release --no-default-features  # Sequential build
cargo test                       # Run tests
cargo check                      # Type checking only (fast)
cargo bench                      # Run benchmarks
```

### Benchmarking

Compare parallel vs sequential performance:

```bash
# Benchmark with parallel support (default)
cargo bench --features parallel

# Benchmark without parallel support
cargo bench --no-default-features

# View benchmark reports
open target/criterion/report/index.html
```

For detailed benchmarking instructions, see [BENCHMARKING.md](BENCHMARKING.md).

## Troubleshooting

### Authentication Failed

If login fails:

1. Verify credentials at https://www.nsgportal.org/
2. Check that your NSG account is active
3. Ensure application key is correct
4. Try using `--no-verify` to skip connection test and save credentials anyway

### Job Not Found

If `status` or `download` can't find a job:

1. Verify job ID is correct
2. Use `nsg list` to see all your jobs
3. Try using the full job URL instead of just the ID

### Download Failed

If results download fails:

1. Check job is in COMPLETED stage with `nsg status`
2. Verify job has results available
3. Check output directory permissions

## License

MIT License

## Related Projects

- [DDALAB]https://github.com/sdraeger/DDALAB - Delay Differential Analysis Laboratory
- [NSG Portal]https://www.nsgportal.org/ - Neuroscience Gateway web interface

## Contributing

This tool was created as part of the DDALAB project. For issues or contributions, please use the DDALAB repository.

## Contact

For NSG-related issues: nsghelp@sdsc.edu
For DDALAB issues: https://github.com/sdraeger/DDALAB/issues