arcnix 0.2.0

A friendly desktop workspace for managing servers over SSH
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
use arcnix_core::diagnostics::{self, Check, ConfigurationCheck, Level, Plan, Validator};
use arcnix_core::tasks::{Status, Target};

use super::*;
#[path = "diagnostic_export_ui.rs"]
mod export_ui;
#[path = "diagnostic_logs_ui.rs"]
mod log_ui;
pub(super) fn log_event(
    ui: &Ui,
    serial: u64,
    result: std::result::Result<Box<batch_worker::LogObservation>, String>,
) {
    log_ui::event(ui, serial, result);
}
pub(super) fn dismiss(s: &mut State) {
    export_ui::dismiss(s);
    log_ui::dismiss(s);
    deleted_files_ui::dismiss(s);
    tcp_listeners_ui::dismiss(s);
}
pub(super) fn dialogs(ui: Ui) -> impl Widget {
    fragment((
        export_ui::dialog(ui.clone()),
        log_ui::dialog(ui.clone()),
        deleted_files_ui::dialog(ui.clone()),
        tcp_listeners_ui::dialog(ui),
    ))
}

#[derive(Clone)]
struct TlsDraft {
    host: String,
    port: String,
    name: String,
}
#[derive(Clone, Default)]
struct ResolutionDraft {
    host: String,
    expected: String,
}
impl TlsDraft {
    fn endpoint(&self) -> anyhow::Result<diagnostics::TlsEndpoint> {
        let port = self.port.trim();
        anyhow::ensure!(
            !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()),
            "TLS port must be between 1 and 65535"
        );
        Ok(diagnostics::TlsEndpoint {
            host: self.host.trim().to_owned(),
            port: port
                .parse()
                .map_err(|_| anyhow::anyhow!("TLS port must be between 1 and 65535"))?,
            server_name: self.name.trim().to_owned(),
        })
    }
}
#[derive(Clone)]
pub(super) struct DiagnosticsState {
    export: export_ui::ExportState,
    logs: log_ui::LogsState,
    plan: Plan,
    warning: String,
    critical: String,
    days: String,
    error: String,
    configuring: bool,
    expanded: HashSet<String>,
    certificates: Vec<(String, String)>,
    configurations: Vec<(String, ConfigurationCheck)>,
    tls: Vec<(String, TlsDraft)>,
    resolution: Vec<(String, ResolutionDraft)>,
}
impl Default for DiagnosticsState {
    fn default() -> Self {
        Self {
            export: Default::default(),
            logs: Default::default(),
            plan: Plan::default(),
            warning: "85".into(),
            critical: "95".into(),
            days: "30".into(),
            error: String::new(),
            configuring: false,
            expanded: HashSet::new(),
            certificates: vec![],
            configurations: vec![],
            tls: vec![],
            resolution: vec![],
        }
    }
}
fn same_server(s: &State, t: &Target) -> bool {
    !s.system_task_running
        && s.config.servers.iter().any(|p| {
            p.id == t.id
                && t.endpoint == format!("{}@{}:{}", p.username, p.host, p.port)
                && s.config.trusted_fingerprint(&p.host, p.port) == Some(t.fingerprint.as_str())
        })
}
fn scope_text(plan: &Plan) -> String {
    let mut fields = vec![];
    if plan.disk || plan.inodes {
        fields.push(i18n::translate(&format!(
            "Usage warning ≥{}%, critical ≥{}%",
            plan.warning_percent, plan.critical_percent
        )));
    }
    if plan.ports {
        fields.push(i18n::translate(&format!(
            "Expected TCP: {}",
            if plan.expected_ports.is_empty() {
                i18n::translate("Inventory only")
            } else {
                plan.expected_ports.clone()
            }
        )));
    }
    if !plan.certificate.is_empty() {
        fields.push(i18n::translate(&format!(
            "PEM: {} · {} days",
            plan.certificate, plan.certificate_days
        )));
    }
    if plan.validator != Validator::None {
        fields.push(i18n::translate(&format!(
            "Configuration: {} · {}",
            match plan.validator {
                Validator::Nginx => "Nginx",
                Validator::OpenSsh => "OpenSSH",
                Validator::None => "",
            },
            if plan.configuration_file.is_empty() {
                i18n::translate("Default configuration")
            } else {
                plan.configuration_file.clone()
            }
        )));
    }
    if !plan.certificates.is_empty() || !plan.configurations.is_empty() {
        fields.push(i18n::translate(&format!(
            "Additional certificates: {} · configurations: {}",
            plan.certificates.len(),
            plan.configurations.len()
        )));
    }
    if !plan.tls.is_empty() {
        fields.push(i18n::translate(&format!(
            "TLS endpoints: {} · {} days",
            plan.tls.len(),
            plan.certificate_days
        )));
    }
    if !plan.resolution.is_empty() {
        fields.push(i18n::translate(&format!(
            "Name queries: {}",
            plan.resolution.len()
        )));
    }
    fields.join(" · ")
}
impl Ui {
    fn diagnose(&self) {
        let result = self.state.with(|s| -> anyhow::Result<_> {
            let d = &s.diagnostics;
            let mut plan = d.plan.clone();
            plan.certificates = d
                .certificates
                .iter()
                .map(|(_, path)| path.clone())
                .collect();
            plan.configurations = d
                .configurations
                .iter()
                .map(|(_, check)| check.clone())
                .collect();
            plan.tls = d
                .tls
                .iter()
                .map(|(_, draft)| draft.endpoint())
                .collect::<anyhow::Result<_>>()?;
            plan.resolution = d
                .resolution
                .iter()
                .map(|(_, r)| diagnostics::NameQuery::parse(&r.host, &r.expected))
                .collect::<anyhow::Result<_>>()?;
            plan.warning_percent = d
                .warning
                .parse()
                .map_err(|_| anyhow::anyhow!("Enter numeric usage thresholds"))?;
            plan.critical_percent = d
                .critical
                .parse()
                .map_err(|_| anyhow::anyhow!("Enter numeric usage thresholds"))?;
            plan.certificate_days = d
                .days
                .parse()
                .map_err(|_| anyhow::anyhow!("Enter numeric certificate warning days"))?;
            plan.template()
        });
        match result {
            Ok(template) => {
                self.state.revise(|s| {
                    s.diagnostics.error.clear();
                    s.diagnostics.configuring = false;
                });
                self.choose_script(template);
            }
            Err(e) => self
                .state
                .revise(|s| s.diagnostics.error = format!("{e:#}")),
        }
    }
}
fn field(
    ui: &Ui,
    label: &'static str,
    read: fn(&DiagnosticsState) -> String,
    write: fn(&mut DiagnosticsState, String),
) -> impl Widget + use<> {
    let state = ui.state;
    col().class("field").fill((
        text().class("field-label").content(i18n::bind(label)),
        input()
            .name(i18n::bind(label))
            .value(state.map(move |s| read(&s.diagnostics)))
            .on(value_changed, move |e| {
                if let Event::ValueChanged { value, .. } = e {
                    state.revise(|s| write(&mut s.diagnostics, value));
                }
            }),
    ))
}
fn toggle(
    ui: &Ui,
    label: &'static str,
    read: fn(&Plan) -> bool,
    write: fn(&mut Plan, bool),
) -> impl Widget + use<> {
    let state = ui.state;
    btn(label, move || {
        state.revise(|s| {
            let value = !read(&s.diagnostics.plan);
            write(&mut s.diagnostics.plan, value);
        })
    })
    .class(state.map(move |s| {
        if read(&s.diagnostics.plan) {
            "choice active"
        } else {
            "choice"
        }
        .to_owned()
    }))
}
pub(super) fn sidebar(_ui: Ui) -> impl Widget {
    col().class("context-sidebar").fill(row().class("context-title help-heading").fill((text().content(i18n::bind("Diagnostics")),crate::help_ui::group("Diagnostics",&["Run selected checks across servers on demand. Results describe one point in time, not continuous monitoring.","Checks run in server-owned tasks. Review findings before taking action; no automatic cleanup, restart or reload is performed."]))))
}
fn additional_certificates(ui: Ui) -> impl Widget {
    let state = ui.state;
    col().class("diagnostic-extra-certificates form-stack").fill((
        row().class("script-actions").fill((
            btn("Add certificate",move||state.revise(|s|{if s.diagnostics.certificates.len()<16{s.diagnostics.certificates.push((new_id(),String::new()));}})).enabled(state.map(|s|s.diagnostics.certificates.len()<16)),
            crate::help_ui::tip("Multiple diagnostic items","Select up to 20 checks in total. Each PEM checks its first certificate's expiry; it does not validate the chain or the certificate served over the network. Configuration files must be full Nginx or OpenSSH configurations. Empty configuration paths use the validator default. Duplicate items are rejected."),
        )),
        each(state.map(|s|s.diagnostics.certificates.clone())).key(|row|row.0.clone()).children(move|item|{
            let id=item.value.get().0;let edit=id.clone();let remove=id.clone();
            row().class("diagnostic-item-row diagnostic-certificate-row").fill((
                col().class("field").fill((
                    text().class("field-label").content(i18n::bind("Additional PEM certificate path")),
                    input().name(i18n::bind("Additional PEM certificate path")).value(item.value.map(|row|row.1.clone())).on(value_changed,move|event|{if let Event::ValueChanged{value,..}=event{state.revise(|s|{if let Some(row)=s.diagnostics.certificates.iter_mut().find(|row|row.0==edit){row.1=value.clone();}});}}),
                )),
                icon_action("Remove certificate",Icon::Close).on_press(move||state.revise(|s|s.diagnostics.certificates.retain(|row|row.0!=remove))),
            ))
        }),
    ))
}
fn additional_configurations(ui: Ui) -> impl Widget {
    let state = ui.state;
    col()
        .class("diagnostic-extra-configurations form-stack")
        .fill((
            btn("Add configuration check", move || {
                state.revise(|s| {
                    if s.diagnostics.configurations.len() < 16 {
                        s.diagnostics.configurations.push((
                            new_id(),
                            ConfigurationCheck {
                                validator: Validator::Nginx,
                                path: String::new(),
                            },
                        ));
                    }
                })
            })
            .enabled(state.map(|s| s.diagnostics.configurations.len() < 16)),
            each(state.map(|s| s.diagnostics.configurations.clone()))
                .key(|row| row.0.clone())
                .children(move |item| {
                    let id = item.value.get().0;
                    let nginx = id.clone();
                    let ssh = id.clone();
                    let edit = id.clone();
                    let remove = id.clone();
                    col()
                        .class("diagnostic-configuration-row form-stack")
                        .fill((
                            row().class("script-actions").fill((
                                text()
                                    .class("field-label")
                                    .content(i18n::bind("Additional configuration")),
                                btn("Nginx", move || {
                                    state.revise(|s| {
                                        if let Some(row) = s
                                            .diagnostics
                                            .configurations
                                            .iter_mut()
                                            .find(|row| row.0 == nginx)
                                        {
                                            row.1.validator = Validator::Nginx;
                                        }
                                    })
                                })
                                .class(item.value.map(|row| {
                                    if row.1.validator == Validator::Nginx {
                                        "choice active"
                                    } else {
                                        "choice"
                                    }
                                    .to_owned()
                                })),
                                btn("OpenSSH", move || {
                                    state.revise(|s| {
                                        if let Some(row) = s
                                            .diagnostics
                                            .configurations
                                            .iter_mut()
                                            .find(|row| row.0 == ssh)
                                        {
                                            row.1.validator = Validator::OpenSsh;
                                        }
                                    })
                                })
                                .class(item.value.map(|row| {
                                    if row.1.validator == Validator::OpenSsh {
                                        "choice active"
                                    } else {
                                        "choice"
                                    }
                                    .to_owned()
                                })),
                                icon_action("Remove configuration check", Icon::Close).on_press(
                                    move || {
                                        state.revise(|s| {
                                            s.diagnostics
                                                .configurations
                                                .retain(|row| row.0 != remove)
                                        })
                                    },
                                ),
                            )),
                            input()
                                .name(i18n::bind(
                                    "Additional configuration file (optional; default if empty)",
                                ))
                                .value(item.value.map(|row| row.1.path.clone()))
                                .on(value_changed, move |event| {
                                    if let Event::ValueChanged { value, .. } = event {
                                        state.revise(|s| {
                                            if let Some(row) = s
                                                .diagnostics
                                                .configurations
                                                .iter_mut()
                                                .find(|row| row.0 == edit)
                                            {
                                                row.1.path = value.clone();
                                            }
                                        });
                                    }
                                }),
                        ))
                }),
        ))
}
fn tls_field(
    ui: &Ui,
    id: String,
    label: &'static str,
    record: Cage<(String, TlsDraft)>,
    read: fn(&TlsDraft) -> String,
    write: fn(&mut TlsDraft, String),
) -> impl Widget + use<> {
    let state = ui.state;
    col().class("field").fill((
        text().class("field-label").content(i18n::bind(label)),
        input()
            .name(i18n::bind(label))
            .value(record.map(move |row| read(&row.1)))
            .on(value_changed, move |event| {
                if let Event::ValueChanged { value, .. } = event {
                    state.revise(|s| {
                        if let Some(row) = s.diagnostics.tls.iter_mut().find(|r| r.0 == id) {
                            write(&mut row.1, value.clone());
                        }
                    });
                }
            }),
    ))
}
fn tls_endpoints(ui: Ui) -> impl Widget {
    let state = ui.state;
    col().class("diagnostic-tls form-stack").fill((
        row().class("script-actions help-heading").fill((
            btn("Add TLS endpoint",move||state.revise(|s|{if s.diagnostics.tls.len()<16{s.diagnostics.tls.push((new_id(),TlsDraft {host:String::new(),port:"443".into(),name:String::new()}));}})).enabled(state.map(|s|s.diagnostics.tls.len()<16)),
            crate::help_ui::group("TLS endpoints",&[
                "Connect from each selected SSH server to a DNS name or unbracketed IP and port. The optional verification name defaults to the connect host. DNS names also set SNI; IP identities do not send SNI. Use ASCII DNS names, including punycode for international domains.",
                "Verify the served leaf, hostname or IP, chain and validity with that server's OpenSSL CA store, then check its expiry warning threshold. Direct TLS only: no HTTP health, STARTTLS, client certificates or revocation check. A passing sample does not prove other network paths or all load-balancer nodes.",
                "Each connection is limited to 15 seconds, with bounded private output. Missing tools, network errors and timeouts remain inconclusive. TLS endpoints share the 20-check and request-size limits with other checks. Results keep the exact endpoint and identity; renewing or changing a service remains a separate operation.",
            ]),
        )),
        each(state.map(|s|s.diagnostics.tls.clone())).key(|row|row.0.clone()).children({let ui=ui.clone();move|item|{
            let id=item.value.get().0;let remove=id.clone();
            col().class("diagnostic-tls-row").fill((
                row().class("script-row-heading").fill((text().class("field-label").content(i18n::bind("TLS endpoint")),icon_action("Remove TLS endpoint",Icon::Close).on_press(move||state.revise(|s|s.diagnostics.tls.retain(|r|r.0!=remove))))),
                row().class("diagnostic-tls-fields").fill((
                    tls_field(&ui,id.clone(),"TLS connect host",item.value,|r|r.host.clone(),|r,v|r.host=v),
                    tls_field(&ui,id.clone(),"TLS port",item.value,|r|r.port.clone(),|r,v|r.port=v),
                    tls_field(&ui,id,"TLS verification name (optional)",item.value,|r|r.name.clone(),|r,v|r.name=v),
                )),
            ))
        }}),
    ))
}
fn resolution_field(
    ui: &Ui,
    id: String,
    record: Cage<(String, ResolutionDraft)>,
    expected: bool,
) -> impl Widget + use<> {
    let state = ui.state;
    let label = if expected {
        "Expected IP addresses (optional, comma-separated)"
    } else {
        "Hostname to resolve"
    };
    col().class("field").fill((
        text().class("field-label").content(i18n::bind(label)),
        input()
            .name(i18n::bind(label))
            .value(record.map(move |r| {
                if expected {
                    r.1.expected.clone()
                } else {
                    r.1.host.clone()
                }
            }))
            .on(value_changed, move |e| {
                if let Event::ValueChanged { value, .. } = e {
                    state.revise(|s| {
                        if let Some((_, r)) =
                            s.diagnostics.resolution.iter_mut().find(|r| r.0 == id)
                        {
                            if expected {
                                r.expected = value.clone();
                            } else {
                                r.host = value.clone();
                            }
                        }
                    });
                }
            }),
    ))
}
fn name_queries(ui: Ui) -> impl Widget {
    let state = ui.state;
    col().class("diagnostic-resolution form-stack").fill((
        row().class("script-actions help-heading").fill((
            btn("Add name resolution check",move||state.revise(|s|{
                if s.diagnostics.resolution.len()<16 {s.diagnostics.resolution.push((new_id(),ResolutionDraft::default()));}
            })).enabled(state.map(|s|s.diagnostics.resolution.len()<16)),
            crate::help_ui::group("Name resolution",&[
                "Resolve an ASCII hostname, including punycode, through each selected server's system resolver using Python 3. DNS, hosts entries and caches may contribute; this check does not identify the source. A final dot disables DNS search expansion.",
                "Optional expected IPv4 or unbracketed IPv6 addresses must all be present; additional addresses are allowed. At most 8 expected and 16 observed addresses per name. Resolution alone does not prove network or application health, DNSSEC or TTLs.",
                "Each lookup has a 15-second limit and bounded output. Missing tools, lookup errors, timeouts and oversized results remain inconclusive. Name queries share the 20-check and request-size limits; no resolver configuration is changed.",
            ]),
        )),
        each(state.map(|s|s.diagnostics.resolution.clone())).key(|r|r.0.clone()).children({let ui=ui.clone();move|item|{
            let id=item.value.get().0;let remove=id.clone();
            col().class("diagnostic-resolution-row form-stack").fill((
                row().class("script-row-heading").fill((
                    text().class("field-label").content(i18n::bind("Name resolution")),
                    icon_action("Remove name resolution check",Icon::Close).on_press(move||state.revise(|s|s.diagnostics.resolution.retain(|r|r.0!=remove))),
                )),
                row().class("diagnostic-resolution-fields").fill((
                    resolution_field(&ui,id.clone(),item.value,false),resolution_field(&ui,id,item.value,true),
                )),
            ))
        }}),
    ))
}
fn settings(ui: Ui) -> impl Widget {
    let state = ui.state;
    let run = ui.clone();
    col().class("diagnostic-settings").fill((
        fragment((
        row().class("script-actions").fill((
            toggle(&ui,"Filesystem space",|p|p.disk,|p,v|p.disk=v),
            toggle(&ui,"Filesystem inodes",|p|p.inodes,|p,v|p.inodes=v),
            toggle(&ui,"Failed systemd services",|p|p.services,|p,v|p.services=v),
            toggle(&ui,"Listening TCP ports",|p|p.ports,|p,v|p.ports=v),
        )),
        row().class("diagnostic-fields").fill((field(&ui,"Warning usage (%)",|s|s.warning.clone(),|s,v|s.warning=v),field(&ui,"Critical usage (%)",|s|s.critical.clone(),|s,v|s.critical=v))),
        field(&ui,"Expected TCP ports (optional, comma-separated)",|s|s.plan.expected_ports.clone(),|s,v|s.plan.expected_ports=v),
        crate::help_ui::tip("On-demand server diagnostics","Space checks exclude tmpfs, devtmpfs and squashfs. TCP checks inspect local listeners; they do not test network reachability."),
        row().class("diagnostic-fields").fill((field(&ui,"PEM certificate path (optional)",|s|s.plan.certificate.clone(),|s,v|s.plan.certificate=v),field(&ui,"Certificate warning days",|s|s.days.clone(),|s,v|s.days=v))),
        fragment((additional_certificates(ui.clone()),tls_endpoints(ui.clone()),name_queries(ui.clone()))),
        text().class("field-label").content(i18n::bind("Configuration validation")),
        row().class("script-actions").fill(each(vec![(Validator::None,"Skip configuration validation"),(Validator::Nginx,"Nginx"),(Validator::OpenSsh,"OpenSSH")]).key(|(v,_)|format!("{v:?}")).children(move|item|{
            let (v,label)=item.value.get();btn(label,move||state.revise(|s|s.diagnostics.plan.validator=v)).class(state.map(move|s|if s.diagnostics.plan.validator==v {"choice active"}else{"choice"}.to_owned()))
        })),
        when(state.map(|s|s.diagnostics.plan.validator!=Validator::None),{let ui=ui.clone();move||field(&ui,"Configuration file (optional; default if empty)",|s|s.plan.configuration_file.clone(),|s,v|s.plan.configuration_file=v)}),
        additional_configurations(ui.clone()),
        toggle(&ui,"Run checks as administrator",|p|p.administrator,|p,v|p.administrator=v),
        crate::help_ui::tip("Diagnostic access","Protected configuration checks may require administrator access. Missing tools, denied permissions and timed-out checks remain inconclusive."),
        )),
        text().class(state.map(|s|if s.diagnostics.error.is_empty(){"hidden"}else{"form-error"}.to_owned())).content(i18n::bind(state.map(|s|s.diagnostics.error.clone()))),
        btn("Choose servers and review checks…",move||run.diagnose()).class("button primary").enabled(state.map(|s|!scripts_ui::busy(s))),
    ))
}
pub(super) fn page(ui: Ui) -> impl Widget {
    let state = ui.state;
    let install = ui.clone();
    col().class("diagnostics-page page-scroll").fill((
        text().class("section-title").content(i18n::bind("On-demand server diagnostics")),
        btn("Configure checks…",move||state.revise(|s|s.diagnostics.configuring = !s.diagnostics.configuring)),
        btn("Install check tools…",move||{if let Some(template)=arcnix_core::scripts::catalog().into_iter().find(|t|t.snippet.id=="builtin:tools"){install.choose_script(template);}}).enabled(state.map(|s|!scripts_ui::busy(s))),
        when(state.map(|s|s.diagnostics.configuring||!s.scripts.tasks.iter().any(|t|t.script_id==diagnostics::SCRIPT_ID)),{let ui=ui.clone();move||settings(ui.clone())}),
        row().class("help-heading").fill((text().class("section-title").content(i18n::bind("Recent diagnostic runs")),crate::help_ui::tip("Recent diagnostic runs","Diagnostic runs share the recent 10-task history. A successful collection can still contain warnings, critical findings or unavailable checks."))),
        when(state.map(|s|!s.scripts.tasks.iter().any(|t|t.script_id==diagnostics::SCRIPT_ID)),||text().class("context-hint").content(i18n::bind("No diagnostic results yet. Choose checks and target servers to begin."))),
        each(state.map(|s|s.scripts.tasks.iter().filter(|t|t.script_id==diagnostics::SCRIPT_ID).cloned().collect::<Vec<_>>())).key(|t|t.id.clone()).children({let ui=ui.clone();move|item|{
            let task=item.value.get();let id=task.id.clone();let view=ui.clone();let view_id=id.clone();let card_task=id.clone();let plan=task.diagnostic.clone();let export=ui.clone();let export_id=task.id.clone();
            col().class("diagnostic-run").fill((
                row().class("script-row-heading").fill((text().class("section-title").content(formatting::timestamp(Some(task.created))),btn("Export report…",move||export.export_diagnostic_batch(&export_id)).class("button diagnostic-export-action"),btn("View task and raw output",move||{view.state.revise(|s|s.scripts.selected_task=Some(view_id.clone()));view.navigate(navigation::Page::Tasks);}))),
                each(state.map(move|s|s.scripts.tasks.iter().find(|t|t.id==id).map(|t|t.targets.clone()).unwrap_or_default())).key(|t|format!("{}:{:?}:{}:{:?}",t.id,t.status,t.output,t.exit_code)).children({let ui=ui.clone();move|item|target_card(ui.clone(),card_task.clone(),plan.clone(),item.value.get())}),
            ))
        }}),
    ))
}
fn target_card(ui: Ui, task: String, plan: Option<Plan>, target: Target) -> impl Widget {
    let state = ui.state;
    let log_context =
        arcnix_core::diagnostic_logs::Context::new(&task, &target, plan.as_ref()).ok();
    let log_ui = ui.clone();
    let log_enabled = log_context.clone();
    let open = ui.clone();
    let open_target = target.clone();
    let measure = ui.clone();
    let measure_target = target.clone();
    let deleted = ui.clone();
    let deleted_target = target.clone();
    let deleted_enabled = target.clone();
    let has_disk = deleted_files_ui::offered(&target, plan.as_ref());
    let has_ports = tcp_listeners_ui::offered(&target, plan.as_ref());
    let ports_ui = ui.clone();
    let ports_target = target.clone();
    let ports_enabled = target.clone();
    let ports_filter = plan
        .as_ref()
        .and_then(|p| p.tcp_ports().ok())
        .unwrap_or_default();
    let completed = target.status == Status::Succeeded && target.exit_code == Some(0);
    let report = completed
        .then(|| {
            let report = diagnostics::parse(&target.output)?;
            plan.as_ref()
                .ok_or_else(|| anyhow::anyhow!("Missing diagnostic check selection"))?
                .validate_report(&report)?;
            Ok::<_, anyhow::Error>(report)
        })
        .transpose();
    let environment = report
        .as_ref()
        .ok()
        .and_then(|r| r.as_ref())
        .map(|r| {
            r.environment
                .as_ref()
                .map(|rows| {
                    rows.iter()
                        .map(|row| {
                            format!(
                                "{}: {}",
                                i18n::translate(diagnostics::environment_label(&row.key)),
                                if row.recorded {
                                    row.value.trim_end().into()
                                } else {
                                    i18n::translate("Unavailable or inconclusive")
                                }
                            )
                        })
                        .collect::<Vec<_>>()
                        .join("\n")
                })
                .unwrap_or_else(|| {
                    i18n::translate("Environment versions were not recorded by this collector.")
                })
        })
        .unwrap_or_else(|| i18n::translate("Collection environment is not confirmed"));
    let (summary, findings) = match report {
        Ok(Some(report)) => {
            let issues = report
                .findings
                .iter()
                .filter(|f| matches!(f.level, Level::Warning | Level::Critical))
                .count();
            let unknown = report
                .findings
                .iter()
                .filter(|f| f.level == Level::Unknown)
                .count();
            (
                i18n::translate(&format!(
                    "Sampled {} · {} findings · {} inconclusive",
                    formatting::timestamp(Some(report.sampled)),
                    issues,
                    unknown
                )),
                report.findings,
            )
        }
        Err(_) => (
            i18n::translate(
                "Diagnostic results are not complete. Inspect the server task before repeating checks.",
            ),
            vec![],
        ),
        Ok(None) => (i18n::translate(target.status.label()), vec![]),
    };
    let enabled_target = target.clone();
    let measure_enabled = target.clone();
    let key = format!("{task}:{}", target.id);
    let show_key = key.clone();
    let active_key = key.clone();
    let has_findings = !findings.is_empty();
    let finding_plan = plan.clone();
    col().class("diagnostic-target").fill((
        row().class("help-heading").fill((
            text().class("section-title").content(target.name.clone()),
            crate::help_ui::tip("Collection environment", environment),
        )),
        text().class("subtle").content(target.endpoint.clone()),
        text().class("diagnostic-summary").content(summary),
        text()
            .class("subtle diagnostic-scope")
            .content(plan.as_ref().map(scope_text).unwrap_or_default()),
        btn("Findings and suggestions", move || {
            state.revise(|s| {
                if !s.diagnostics.expanded.remove(&key) {
                    s.diagnostics.expanded.insert(key.clone());
                }
            })
        })
        .enabled(has_findings)
        .class(state.map(move |s| {
            if s.diagnostics.expanded.contains(&active_key) {
                "choice active"
            } else {
                "choice"
            }
            .to_owned()
        })),
        when(
            state.map(move |s| s.diagnostics.expanded.contains(&show_key)),
            move || {
                let finding_plan = finding_plan.clone();
                each(findings.clone())
                    .key(|f| f.key.clone())
                    .children(move |item| {
                        let f = item.value.get();
                        let context = finding_plan
                            .as_ref()
                            .and_then(|plan| plan.finding_context(&f.key))
                            .map(|(validator, path)| {
                                let path = if path.is_empty() {
                                    i18n::translate("Default configuration")
                                } else {
                                    path.to_owned()
                                };
                                match validator {
                                    Some(Validator::Nginx) => format!("Nginx · {path}"),
                                    Some(Validator::OpenSsh) => format!("OpenSSH · {path}"),
                                    _ => path,
                                }
                            })
                            .or_else(|| {
                                finding_plan
                                    .as_ref()
                                    .and_then(|p| p.tls_context(&f.key))
                                    .map(|e| e.summary())
                            })
                            .or_else(|| {
                                finding_plan
                                    .as_ref()
                                    .and_then(|p| p.resolution_context(&f.key))
                                    .map(|q| q.summary())
                            })
                            .unwrap_or_default();
                        let value = match f.check {
                            Check::Disk | Check::Inodes => {
                                format!("{} · {}", i18n::translate(f.level.label()), f.value)
                            }
                            Check::Services => i18n::translate(&format!(
                                "{} · {} failed services",
                                i18n::translate(f.level.label()),
                                f.value
                            )),
                            Check::Ports => i18n::translate(&format!(
                                "{} · {} missing TCP ports",
                                i18n::translate(f.level.label()),
                                f.value
                            )),
                            Check::Resolution if f.value != "-" => i18n::translate(&format!(
                                "{} · {} resolved addresses",
                                i18n::translate(f.level.label()),
                                f.value
                            )),
                            _ => i18n::translate(f.level.label()),
                        };
                        col()
                            .class(format!("diagnostic-finding {}", f.level.class()))
                            .fill((
                                row().class("script-row-heading").fill((
                                    row().class("help-heading").fill((
                                        text().content(i18n::bind(f.check.label())),
                                        crate::help_ui::tip(f.check.label(), f.check.advice()),
                                    )),
                                    text().class("diagnostic-level").content(value),
                                )),
                                text()
                                    .class("diagnostic-item-context subtle")
                                    .content(context),
                                text()
                                    .class("diagnostic-detail")
                                    .selectable(true)
                                    .content(f.detail),
                            ))
                    })
            },
        ),
        row().class("script-actions").fill((
            when(Vary::Value(has_disk), move || {
                let ui = deleted.clone();
                let target = deleted_target.clone();
                btn("Inspect deleted open files…", move || {
                    ui.open_deleted_files(target.clone())
                })
                .class("button diagnostic-deleted-files")
                .enabled(state.map({
                    let target = deleted_enabled.clone();
                    move |s| deleted_files_ui::available(s, &target)
                }))
            }),
            when(Vary::Value(has_ports), move || {
                let ui = ports_ui.clone();
                let target = ports_target.clone();
                let ports = ports_filter.clone();
                btn("Inspect TCP listeners…", move || {
                    ui.open_tcp_listeners(target.clone(), ports.clone())
                })
                .class("button diagnostic-tcp-listeners")
                .enabled(state.map({
                    let target = ports_enabled.clone();
                    move |s| tcp_listeners_ui::available(s, &target)
                }))
            }),
            when(Vary::Value(log_context.is_some()), move || {
                let ui = log_ui.clone();
                let context = log_context.clone().unwrap();
                btn("Failed service logs…", move || {
                    ui.open_diagnostic_logs(context.clone())
                })
                .class("button diagnostic-service-logs")
                .enabled(state.map({
                    let context = log_enabled.clone().unwrap();
                    move |s| self::log_ui::available(s, &context)
                }))
            }),
            btn("Open server workspace", move || {
                if open.state.with(|s| same_server(s, &open_target)) {
                    open.select_server(open_target.id.clone());
                }
            })
            .enabled(state.map(move |s| same_server(s, &enabled_target))),
            btn("Measure a directory…", move || {
                if measure.state.with(|s| same_server(s, &measure_target))
                    && let Some(t) = arcnix_core::scripts::catalog()
                        .into_iter()
                        .find(|t| t.snippet.id == "builtin:directory")
                {
                    measure.choose_script(t);
                    measure.state.revise(|s| {
                        s.scripts.targets = std::iter::once(measure_target.id.clone()).collect()
                    });
                }
            })
            .enabled(state.map(move |s| !scripts_ui::busy(s) && same_server(s, &measure_enabled))),
        )),
    ))
}