raps-cli 4.15.0

RAPS (rapeseed) - Rust Autodesk Platform Services CLI
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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2025 Dmytro Yemelianov

//! Portfolio report commands
//!
//! Aggregated reports across multiple projects: RFI summaries, issue summaries,
//! submittals, checklists, and assets.

mod extended_reports;
mod issues_report;
mod rfi_report;
#[cfg(test)]
mod tests;

use anyhow::Result;
use clap::Subcommand;
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use serde::Serialize;

use raps_acc::admin::AccountAdminClient;
use raps_admin::ProjectFilter;
use raps_kernel::auth::AuthClient;
use raps_kernel::config::Config;
use raps_kernel::http::HttpClientConfig;

use crate::output::OutputFormat;

#[derive(Debug, Subcommand)]
#[allow(clippy::enum_variant_names)]
pub enum ReportCommands {
    /// RFI summary across portfolio projects
    RfiSummary {
        /// Account ID (defaults to APS_ACCOUNT_ID env var)
        #[arg(short, long)]
        account: Option<String>,

        /// Project filter expression (e.g., "status:active,name:*Hospital*")
        #[arg(short, long)]
        filter: Option<String>,

        /// Filter RFIs by status (open, answered, closed, void)
        #[arg(long)]
        status: Option<String>,

        /// Only include RFIs created after this date (YYYY-MM-DD)
        #[arg(long)]
        since: Option<String>,
    },

    /// Issue summary across portfolio projects
    IssuesSummary {
        /// Account ID (defaults to APS_ACCOUNT_ID env var)
        #[arg(short, long)]
        account: Option<String>,

        /// Project filter expression
        #[arg(short, long)]
        filter: Option<String>,

        /// Filter issues by status (open, closed, etc.)
        #[arg(long)]
        status: Option<String>,

        /// Only include issues created after this date (YYYY-MM-DD)
        #[arg(long)]
        since: Option<String>,
    },

    /// Submittal summary across portfolio projects
    SubmittalsSummary {
        /// Account ID (defaults to APS_ACCOUNT_ID env var)
        #[arg(short, long)]
        account: Option<String>,

        /// Project filter expression
        #[arg(short, long)]
        filter: Option<String>,

        /// Filter submittals by status
        #[arg(long)]
        status: Option<String>,
    },

    /// Checklist summary across portfolio projects
    ChecklistsSummary {
        /// Account ID (defaults to APS_ACCOUNT_ID env var)
        #[arg(short, long)]
        account: Option<String>,

        /// Project filter expression
        #[arg(short, long)]
        filter: Option<String>,

        /// Filter checklists by status
        #[arg(long)]
        status: Option<String>,
    },

    /// Asset summary across portfolio projects
    AssetsSummary {
        /// Account ID (defaults to APS_ACCOUNT_ID env var)
        #[arg(short, long)]
        account: Option<String>,

        /// Project filter expression
        #[arg(short, long)]
        filter: Option<String>,
    },
}

// ---------------------------------------------------------------------------
// Summary types
// ---------------------------------------------------------------------------

#[derive(Serialize)]
pub(super) struct RfiProjectSummary {
    pub(super) project_id: String,
    pub(super) project_name: String,
    pub(super) total: usize,
    pub(super) open: usize,
    pub(super) answered: usize,
    pub(super) closed: usize,
    pub(super) void: usize,
}

#[derive(Serialize)]
pub(super) struct IssueProjectSummary {
    pub(super) project_id: String,
    pub(super) project_name: String,
    pub(super) total: usize,
    pub(super) open: usize,
    pub(super) closed: usize,
    pub(super) other: usize,
}

#[derive(Serialize)]
pub(super) struct SubmittalProjectSummary {
    pub(super) project_id: String,
    pub(super) project_name: String,
    pub(super) total: usize,
}

#[derive(Serialize)]
pub(super) struct ChecklistProjectSummary {
    pub(super) project_id: String,
    pub(super) project_name: String,
    pub(super) total: usize,
}

#[derive(Serialize)]
pub(super) struct AssetProjectSummary {
    pub(super) project_id: String,
    pub(super) project_name: String,
    pub(super) total: usize,
}

#[derive(Serialize)]
pub(super) struct ReportSummaryOutput<T: Serialize> {
    pub(super) total_projects: usize,
    pub(super) projects: Vec<T>,
}

// ---------------------------------------------------------------------------
// Shared traits
// ---------------------------------------------------------------------------

pub(super) trait HasProjectName {
    fn project_name(&self) -> &str;
}

impl HasProjectName for SubmittalProjectSummary {
    fn project_name(&self) -> &str {
        &self.project_name
    }
}

impl HasProjectName for ChecklistProjectSummary {
    fn project_name(&self) -> &str {
        &self.project_name
    }
}

impl HasProjectName for AssetProjectSummary {
    fn project_name(&self) -> &str {
        &self.project_name
    }
}

pub(super) trait HasStatus {
    fn status(&self) -> &str;
}

impl HasStatus for raps_acc::Rfi {
    fn status(&self) -> &str {
        &self.status
    }
}

impl HasStatus for raps_acc::Issue {
    fn status(&self) -> &str {
        &self.status
    }
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

pub(super) fn get_account_id(account: Option<String>) -> Result<String> {
    match account.or_else(|| std::env::var("APS_ACCOUNT_ID").ok()) {
        Some(id) if !id.is_empty() => Ok(id),
        _ => {
            anyhow::bail!(
                "Account ID is required. Use --account or set APS_ACCOUNT_ID environment variable."
            );
        }
    }
}

pub(super) fn parse_project_filter(filter: &Option<String>) -> Result<ProjectFilter> {
    match filter {
        Some(f) => Ok(ProjectFilter::from_expression(f)?),
        None => Ok(ProjectFilter::new()),
    }
}

pub(super) fn create_progress_bar(
    output_format: OutputFormat,
    count: u64,
    message: &str,
) -> Option<ProgressBar> {
    if !output_format.supports_colors() {
        return None;
    }
    let template = format!(
        "{{spinner:.green}} [{{bar:40.cyan/blue}}] {{pos}}/{{len}} {}",
        message
    );
    let pb = ProgressBar::new(count);
    pb.set_style(
        ProgressStyle::default_bar()
            .template(&template)
            .expect("valid progress template")
            .progress_chars("=>-"),
    );
    Some(pb)
}

pub(super) fn print_report_header(
    output_format: OutputFormat,
    label: &str,
    account_id: &str,
    filter: &Option<String>,
) {
    if !output_format.supports_colors() {
        return;
    }
    println!(
        "\n{} {} for account {}",
        "".cyan(),
        label,
        account_id.cyan()
    );
    if let Some(f) = filter {
        println!("  Filter: {}", f);
    }
    println!();
}

pub(super) fn truncate_name(name: &str) -> String {
    if name.len() > 28 {
        format!("{}...", &name[..25])
    } else {
        name.to_string()
    }
}

pub(super) fn print_simple_table<T, F>(
    title: &str,
    output: &ReportSummaryOutput<T>,
    output_format: OutputFormat,
    get_total: F,
) -> Result<()>
where
    T: Serialize + HasProjectName,
    F: Fn(&T) -> usize,
{
    match output_format {
        OutputFormat::Table => {
            let grand_total: usize = output.projects.iter().map(&get_total).sum();

            println!("{}", format!("{} Portfolio Summary:", title).bold());
            println!("{}", "".repeat(45));
            println!("{:<30} {:>8}", "Project".bold(), "Total".bold());
            println!("{}", "".repeat(45));

            for s in &output.projects {
                println!(
                    "{:<30} {:>8}",
                    truncate_name(s.project_name()),
                    get_total(s).to_string().cyan(),
                );
            }

            println!("{}", "".repeat(45));
            println!(
                "{:<30} {:>8}",
                "TOTAL".bold(),
                grand_total.to_string().bold(),
            );
            println!(
                "\n{} {} projects scanned",
                "".cyan(),
                output.total_projects
            );
        }
        _ => {
            output_format.write(&output)?;
        }
    }
    Ok(())
}

pub(super) fn count_status(items: &[impl HasStatus], status: &str) -> usize {
    items
        .iter()
        .filter(|item| item.status().eq_ignore_ascii_case(status))
        .count()
}

// ---------------------------------------------------------------------------
// Shared project-listing boilerplate
// ---------------------------------------------------------------------------

pub(super) struct ReportContext {
    pub(super) http_config: HttpClientConfig,
    pub(super) filtered_projects: Vec<raps_acc::types::AccountProject>,
}

pub(super) async fn prepare_report(
    config: &Config,
    auth_client: &AuthClient,
    account: Option<String>,
    filter: &Option<String>,
    label: &str,
    output_format: OutputFormat,
) -> Result<Option<ReportContext>> {
    let account_id = get_account_id(account)?;
    let project_filter = parse_project_filter(filter)?;
    print_report_header(output_format, label, &account_id, filter);

    let http_config = HttpClientConfig::default();
    let admin_client = AccountAdminClient::new_with_http_config(
        config.clone(),
        auth_client.clone(),
        http_config.clone(),
    );

    let all_projects = admin_client.list_all_projects(&account_id).await?;
    let filtered_projects = project_filter.apply(all_projects);

    if filtered_projects.is_empty() {
        if output_format.supports_colors() {
            println!("{}", "No projects found matching the filter.".yellow());
        }
        return Ok(None);
    }

    Ok(Some(ReportContext {
        http_config,
        filtered_projects,
    }))
}

// ---------------------------------------------------------------------------
// Command dispatch
// ---------------------------------------------------------------------------

impl ReportCommands {
    pub async fn execute(
        self,
        config: &Config,
        auth_client: &AuthClient,
        output_format: OutputFormat,
    ) -> Result<()> {
        match self {
            ReportCommands::RfiSummary {
                account,
                filter,
                status,
                since,
            } => {
                rfi_report::rfi_summary(
                    config,
                    auth_client,
                    account,
                    filter,
                    status,
                    since,
                    output_format,
                )
                .await
            }
            ReportCommands::IssuesSummary {
                account,
                filter,
                status,
                since,
            } => {
                issues_report::issues_summary(
                    config,
                    auth_client,
                    account,
                    filter,
                    status,
                    since,
                    output_format,
                )
                .await
            }
            ReportCommands::SubmittalsSummary {
                account,
                filter,
                status,
            } => {
                extended_reports::submittals_summary(
                    config,
                    auth_client,
                    account,
                    filter,
                    status,
                    output_format,
                )
                .await
            }
            ReportCommands::ChecklistsSummary {
                account,
                filter,
                status,
            } => {
                extended_reports::checklists_summary(
                    config,
                    auth_client,
                    account,
                    filter,
                    status,
                    output_format,
                )
                .await
            }
            ReportCommands::AssetsSummary { account, filter } => {
                extended_reports::assets_summary(
                    config,
                    auth_client,
                    account,
                    filter,
                    output_format,
                )
                .await
            }
        }
    }
}