artisan_middleware 5.7.1

The main services of the artisan platform to allow communication and management of linux system services
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
//! # Manager Data Module
//!
//! This module provides structures, enums, and functions for managing and
//! communicating application states in the AIS Manager system. It includes
//! mechanisms for encrypting/decrypting data, sending/receiving messages
//! through Unix sockets, and storing application status information locally.
//!
//! ## Overview
//! - **CommandType**: Represents different commands (Start, Stop, Restart, etc.).
//! - **Status**: Represents the lifecycle states of an application (Starting, Running, etc.).
//! - **Command**: Used for issuing a command to an application.
//! - **Metrics**: Holds runtime metrics (CPU, memory usage, etc.).
//! - **AppStatus**: A snapshot of an application's current state.
//! - **CommandResponse**: A response from the system after processing a command.
//! - **RegisterApp / DeregisterApp / UpdateApp**: Messages used for registering,
//!   deregistering, or updating an application's status.
//! - **AppMessage**: Aggregates all message variants (Register, Deregister, Update, etc.).
//! - **save_registered_apps / load_registered_apps**: Handle storing and loading of
//!   `AppStatus` data locally.
//! - **register_app**: Registers an application with a remote aggregator if configured.

use chrono::Utc;
use colored::Colorize;
use dusa_collection_utils::core::logger::LogLevel;
use dusa_collection_utils::core::types::pathtype::PathType;
use dusa_collection_utils::core::types::stringy::Stringy;
use dusa_collection_utils::log;
use dusa_collection_utils::{core::errors::ErrorArrayItem, core::types::rwarc::LockWithTimeout};
use serde::{Deserialize, Serialize};
use serde_json::Error;
use std::collections::HashSet;
use std::fs::create_dir_all;
use std::io::BufRead;
use std::time::Duration;
use std::{
    collections::HashMap,
    fmt,
    fs::{File, OpenOptions},
    io::{Read, Write},
};
use tokio::sync::broadcast;
use tokio::time::interval;

use crate::config_bundle::ApplicationConfig;
use crate::encryption::{simple_decrypt, simple_encrypt};
use crate::portal::{ManagerData, ProjectInfo};

/// Path where the aggregator stores AIS Manager data.
pub const AGGREGATOR_PATH: &str = "/opt/artisan/tmp/.ais_manager_data";

/// A convenience type alias for string-based identifiers in this module.
type ID = Stringy;

//
// Enums
//

/// Represents different commands that can be sent to an application.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum CommandType {
    /// Instructs the application to start.
    Start,
    /// Instructs the application to stop.
    Stop,
    /// Instructs the application to restart.
    Restart,
    /// Requests the application’s current status.
    Status,
    /// Requests the status of all registered applications.
    AllStatus,
    /// Requests manager-level information.
    Info,
    /// Sends a custom command with a user-defined string.
    Custom(String),
}

impl fmt::Display for CommandType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CommandType::Start => write!(f, "{}", "Start".green()),
            CommandType::Stop => write!(f, "{}", "Stop".red()),
            CommandType::Restart => write!(f, "{}", "Restart".yellow()),
            CommandType::Status => write!(f, "{}", "Status".cyan()),
            CommandType::AllStatus => write!(f, "{}", "All Info".cyan()),
            CommandType::Info => write!(f, "{}", "Manager Info".blue()),
            CommandType::Custom(cmd) => write!(f, "{}: {}", "Custom".purple(), cmd),
        }
    }
}

/// Represents different lifecycle states an application can be in.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub enum Status {
    /// The application is in the process of starting.
    Starting,
    /// The application is currently running.
    Running,
    /// The application is running but idle.
    Idle,
    /// The application is in the process of stopping.
    Stopping,
    /// The application has stopped.
    Stopped,
    /// The application’s status cannot be determined.
    Unknown,
    /// The application is running with warnings.
    Warning,
    /// The application is in the process of building.
    Building,
}

impl fmt::Display for Status {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let status_str = match self {
            Status::Starting => "Starting".bright_green(),
            Status::Running => "Running".green().bold(),
            Status::Idle => "Idle".yellow(),
            Status::Stopping => "Stopping".bright_red(),
            Status::Stopped => "Stopped".red().bold(),
            Status::Unknown => "Unknown".bright_cyan().bold(),
            Status::Warning => "Warning".bright_yellow(),
            Status::Building => "Building".bright_blue(),
        };
        write!(f, "{}", status_str)
    }
}

//
// Structs
//

/// Represents a command that can be issued to an application, including the
/// application identifier, command type, and timestamp.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Command {
    /// The unique identifier of the target application.
    pub app_id: ID,
    /// The type of command (start, stop, custom, etc.).
    pub command_type: CommandType,
    /// A Unix timestamp marking when the command was created.
    pub timestamp: u64,
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {}, {}: {}, {}: {}",
            "App ID".bold().cyan(),
            self.app_id,
            "Command Type".bold().cyan(),
            self.command_type,
            "Timestamp".bold().cyan(),
            self.timestamp
        )
    }
}

/// Records network TXranmitted and RXcieved
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NetworkUsage {
    pub rx_bytes: u64,
    pub tx_bytes: u64,
}

impl NetworkUsage {
    pub fn set(&mut self, other: &Self) {
        // self.rx_bytes += other.rx_bytes;
        // self.tx_bytes += other.tx_bytes;
        self.rx_bytes = other.rx_bytes;
        self.tx_bytes = other.tx_bytes;
    }
}

/// Contains runtime metrics for an application, such as CPU and memory usage.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Metrics {
    /// CPU usage in percent.
    pub cpu_usage: f32,
    /// Memory usage in MB.
    pub memory_usage: f64,
    /// An optional field for additional metrics or notes.
    pub other: Option<NetworkUsage>,
}

impl Metrics {
    pub fn set(&mut self, other: &Self) {
        self.cpu_usage = other.cpu_usage;
        self.memory_usage = other.memory_usage;

        match (&mut self.other, &other.other) {
            (Some(existing), Some(new)) => existing.set(new),
            (None, Some(new)) => self.other = Some(new.clone()),
            _ => {} // Either both None, or only other is None — do nothing
        }
    }
}

impl fmt::Display for Metrics {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {:.2}%, {}: {} MB{}",
            "CPU Usage".bold().yellow(),
            self.cpu_usage,
            "Memory Usage".bold().yellow(),
            self.memory_usage,
            match &self.other {
                Some(info) => format!(", {}: {:?}", "Other".bold().yellow(), info),
                None => "".to_string(),
            }
        )
    }
}

/// Represents a real-time resource usage report from a running instance.
///
/// This is typically collected every few seconds to minutes and used to
/// update an in-memory accumulator which is later persisted for billing.
///
/// ### Example:
/// ```rust,no_run
/// use artisan_middleware::aggregator::LiveMetrics;
/// let _metrics = LiveMetrics {
///     runner_id: "abc123".into(),
///     instance_id: "xyz456".into(),
///     cpu_usage: 12.5,
///     memory_mb: 256.0,
///     rx_bytes: 15000,
///     tx_bytes: 5000,
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiveMetrics {
    pub runner_id: Stringy,
    pub instance_id: Stringy,
    pub cpu_usage: f32,
    pub memory_mb: f64,
    pub rx_bytes: u64,
    pub tx_bytes: u64,
}

/// A single aggregated usage record.
///
/// This structure is persisted to disk at regular intervals, containing
/// the summarized data of all metrics observed in that interval.
///
/// ### Fields:
/// - `timestamp_epoch`: The UNIX timestamp at which the aggregation occurred.
/// - `total_*`: Cumulative totals over the aggregation period.
/// - `peak_*`: Highest observed value during the period.
/// - `sample_count`: Number of metric samples aggregated.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageRecord {
    pub timestamp_epoch: i64,
    pub runner_id: Stringy,
    pub instance_id: Stringy,
    pub total_cpu: f32,
    pub peak_cpu: f32,
    pub total_memory: f64,
    pub peak_memory: f64,
    pub total_rx: u64,
    pub total_tx: u64,
    pub sample_count: u64,
}

/// The result of a cost calculation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BillingCosts {
    pub cpu_cost: f64,
    pub ram_cost: f64,
    pub bandwidth_cost: f64,
    pub total_cost: f64,
    pub instances: u64,
}

impl fmt::Display for BillingCosts {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "BillingCosts:\n\
             - CPU Cost: ${:.2}\n\
             - RAM Cost: ${:.2}\n\
             - Bandwidth Cost: ${:.2}\n\
             - Instance Cost: ${:.2}\n\
             - Total Cost: ${:.2}\n\
             - Instances: {}",
            self.cpu_cost,
            self.ram_cost,
            self.bandwidth_cost,
            (self.instances * 5),
            self.total_cost,
            self.instances
        )
    }
}

/// Accumulator that aggregates usage statistics over a time window.
///
/// This is stored in memory and updated every time a new `LiveMetrics` is received.
#[derive(Clone, Debug, Default)]
pub struct UsageAccumulator {
    pub total_cpu: f32,
    pub peak_cpu: f32,
    pub total_memory: f64,
    pub peak_memory: f64,
    pub total_rx: u64,
    pub total_tx: u64,
    pub last_rx: u64,
    pub last_tx: u64,
    pub has_network_baseline: bool,
    pub sample_count: u64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BilledUsageSummary {
    pub runner_id: Stringy,
    pub instance_id: Stringy,
    pub total_cpu: f32,
    pub peak_cpu: f32,
    pub avg_memory: f64,
    pub peak_memory: f64,
    pub total_rx: u64,
    pub total_tx: u64,
    pub total_samples: u64,
    pub instances: u64,
}

pub fn load_usage_records_from_dir(dir: &PathType) -> Result<Vec<UsageRecord>, std::io::Error> {
    let mut all_records = Vec::new();

    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.extension().map(|ext| ext == "jsonl").unwrap_or(false) {
            let file = std::fs::File::open(&path)?;
            let reader = std::io::BufReader::new(file);

            for line_result in reader.lines() {
                if let Ok(line) = line_result {
                    if let Ok(record) = serde_json::from_str::<UsageRecord>(&line) {
                        all_records.push(record);
                    }
                }
            }
        }
    }

    Ok(all_records)
}

pub fn summarize_usage(records: &[UsageRecord]) -> Option<BilledUsageSummary> {
    if records.is_empty() {
        log!(LogLevel::Warn, "No records given");
        return None;
    }

    let mut total_cpu_points: f32 = 0.0;
    let mut peak_cpu: f32 = 0.0;
    let mut total_memory_sum: f64 = 0.0;
    let mut peak_memory: f64 = 0.0;
    let mut total_sample_count: u64 = 0;

    let mut min_rx: u64 = u64::MAX;
    let mut max_rx: u64 = 0;
    let mut min_tx: u64 = u64::MAX;
    let mut max_tx: u64 = 0;

    let runner_id = records[0].runner_id.clone();
    let instance_id = records[0].instance_id.clone();
    let mut instance_seen: HashSet<Stringy> = HashSet::new();

    for r in records {
        total_cpu_points += r.total_cpu;
        peak_cpu = peak_cpu.max(r.peak_cpu);

        total_memory_sum += r.total_memory;
        peak_memory = peak_memory.max(r.peak_memory);

        total_sample_count += r.sample_count;

        min_rx = min_rx.min(r.total_rx);
        max_rx = max_rx.max(r.total_rx);
        min_tx = min_tx.min(r.total_tx);
        max_tx = max_tx.max(r.total_tx);

        if !instance_seen.contains(&r.instance_id) {
            instance_seen.insert(r.instance_id.clone());
        }
    }

    let avg_memory = if total_sample_count > 0 {
        total_memory_sum / total_sample_count as f64
    } else {
        0.0
    };

    let total_rx = max_rx.saturating_sub(min_rx);
    let total_tx = max_tx.saturating_sub(min_tx);

    // Convert CPU% points → core-seconds → core-hours
    let total_core_hours = total_cpu_points;

    Some(BilledUsageSummary {
        runner_id,
        instance_id,
        total_cpu: total_core_hours, // << total_cpu is now "core-hours"
        peak_cpu,
        avg_memory,
        peak_memory,
        total_rx,
        total_tx,
        total_samples: total_sample_count,
        instances: instance_seen.len() as u64,
    })
}

/// Key for mapping usage data per instance.
/// Tuple of (runner_id, instance_id).
pub type InstanceKey = (Stringy, Stringy);

/// Thread-safe map of usage accumulators.
/// Wrapped in a LockWithTimeout for safe concurrent access.
pub type UsageMap = LockWithTimeout<HashMap<InstanceKey, UsageAccumulator>>;

/// Shared application context containing communication and tracking handles.
#[derive(Clone)]
pub struct AppContext {
    pub usage_map: UsageMap,
    pub metrics_tx: broadcast::Sender<LiveMetrics>,
    pub project_tx: broadcast::Sender<ProjectInfo>,
}

/// Updates the accumulator with new live metrics.
///
/// This function is intended to be called every time an instance
/// reports its current resource usage.
///
/// It handles delta calculations for network traffic and peak tracking
/// for CPU and memory.
///
/// Returns an error if the lock on the usage map could not be acquired.
pub async fn update_metrics(live: LiveMetrics, usage_map: &UsageMap) -> Result<(), ErrorArrayItem> {
    let mut map = usage_map.try_write().await?;
    let key = (live.runner_id.clone(), live.instance_id.clone());
    let entry = map.entry(key).or_default();

    // CPU & RAM
    entry.total_cpu += live.cpu_usage;
    entry.total_memory += live.memory_mb;
    entry.peak_cpu = entry.peak_cpu.max(live.cpu_usage);
    entry.peak_memory = entry.peak_memory.max(live.memory_mb);
    entry.sample_count += 1;

    // Network deltas
    // First network sample seeds the baseline so we don't bill cumulative counters since boot.
    if !entry.has_network_baseline {
        entry.last_rx = live.rx_bytes;
        entry.last_tx = live.tx_bytes;
        entry.has_network_baseline = true;
        return Ok(());
    }

    if live.rx_bytes >= entry.last_rx {
        entry.total_rx += live.rx_bytes - entry.last_rx;
    } else {
        log!(
            LogLevel::Warn,
            "RX counter moved backwards for {}:{} ({} -> {}), resetting baseline",
            live.runner_id,
            live.instance_id,
            entry.last_rx,
            live.rx_bytes
        );
    }

    if live.tx_bytes >= entry.last_tx {
        entry.total_tx += live.tx_bytes - entry.last_tx;
    } else {
        log!(
            LogLevel::Warn,
            "TX counter moved backwards for {}:{} ({} -> {}), resetting baseline",
            live.runner_id,
            live.instance_id,
            entry.last_tx,
            live.tx_bytes
        );
    }

    entry.last_rx = live.rx_bytes;
    entry.last_tx = live.tx_bytes;
    Ok(())
}

/// Spawns a background task that flushes all current usage accumulators to disk.
///
/// This function is meant to be called once at startup. It sets up a background task
/// that runs every 5 minutes, serializing the accumulated usage data into JSONL files.
/// Each day's data is written into a separate file (e.g., `usage-2025-04-16.jsonl`).
///
/// Logs an error if the usage map cannot be written at flush time.
pub async fn spawn_flush_task(usage_map: UsageMap, output_dir: PathType) {
    create_dir_all(&output_dir).unwrap();
    tokio::spawn(async move {
        // let mut tick = interval(Duration::from_secs(30)); // every 5 min
        let mut tick = interval(Duration::from_secs(300)); // every 5 min
        loop {
            tick.tick().await;

            let mut map = match usage_map.try_write().await {
                Ok(val) => val,
                Err(err) => {
                    log!(LogLevel::Error, "Failed to access the usage map: {}", err);
                    continue;
                }
            };

            let now = Utc::now();
            let epoch = now.timestamp();
            for ((runner_id, instance_id), acc) in map.drain() {
                let record = UsageRecord {
                    timestamp_epoch: epoch,
                    runner_id,
                    instance_id,
                    total_cpu: acc.total_cpu,
                    peak_cpu: acc.peak_cpu,
                    total_memory: acc.total_memory,
                    peak_memory: acc.peak_memory,
                    total_rx: acc.total_rx,
                    total_tx: acc.total_tx,
                    sample_count: acc.sample_count,
                };

                let filename = output_dir.join(format!("usage-{}.jsonl", now.format("%Y-%m-%d")));
                if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(filename) {
                    if let Ok(line) = serde_json::to_string(&record) {
                        if let Err(err) = writeln!(file, "{}", line) {
                            log!(
                                LogLevel::Error,
                                "Error flushing metrics data: {}",
                                err.to_string()
                            );
                            continue;
                        };
                    } else {
                        log!(LogLevel::Error, "Error serializing json data");
                        continue;
                    }
                } else {
                    log!(LogLevel::Error, "Error Opening File");
                    continue;
                }
            }
        }
    });
}

/// Immediately flushes all current usage accumulators to disk.
///
/// This function is useful during shutdown, before reloads, or when manually
/// triggering a flush for billing or debugging purposes.
pub async fn flush_metrics_to_disk(
    usage_map: &UsageMap,
    output_dir: &PathType,
) -> Result<(), ErrorArrayItem> {
    let mut map = usage_map.try_write().await?;

    let now = Utc::now();
    let epoch = now.timestamp();

    for ((runner_id, instance_id), acc) in map.drain() {
        let record = UsageRecord {
            timestamp_epoch: epoch,
            runner_id,
            instance_id,
            total_cpu: acc.total_cpu,
            peak_cpu: acc.peak_cpu,
            total_memory: acc.total_memory,
            peak_memory: acc.peak_memory,
            total_rx: acc.total_rx,
            total_tx: acc.total_tx,
            sample_count: acc.sample_count,
        };

        let filename = output_dir.join(format!("usage-{}.jsonl", now.format("%Y-%m-%d")));
        if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(filename) {
            if let Ok(line) = serde_json::to_string(&record) {
                writeln!(file, "{}", line).map_err(ErrorArrayItem::from)?;
            } else {
                log!(LogLevel::Error, "Error serializing json data");
                continue;
            }
        } else {
            log!(LogLevel::Error, "Error Opening File");
            continue;
        }
    }
    Ok(())
}

/// Represents a snapshot of an application’s state, including status, version, metrics, etc.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AppStatus {
    /// Unique identifier for the application.
    pub app_id: ID,
    /// Additional identifier (often a Git commit SHA or branch).
    pub git_id: ID,
    /// The application state and all configuration data associated, refer to [`ApplicationConfig`]
    pub app_data: ApplicationConfig,
    /// The reported uptime of the instance
    pub uptime: Option<u64>,
    /// A list of errors encountered by the application.
    pub metrics: Option<Metrics>,
    /// The Unix timestamp when this status was recorded.
    pub timestamp: u64,
    /// The expected status set for this application (Running, Stopped, etc.).
    pub expected_status: Status,
}

impl AppStatus {
    /// Converts the `AppStatus` to a JSON string. Returns `None` if serialization fails.
    pub fn to_json(&self) -> Option<String> {
        match serde_json::to_string(self) {
            Ok(data) => Some(data),
            Err(e) => {
                log!(LogLevel::Error, "{}", e);
                None
            }
        }
    }

    /// Creates an `AppStatus` instance from a JSON string. Returns a `Result` with either
    /// `AppStatus` or a `serde_json::Error`.
    pub fn from_json(json_str: &str) -> Result<Self, Error> {
        serde_json::from_str(json_str)
    }

    /// Unsafely converts the `AppStatus` to a `String` (via JSON).
    /// Uses `unwrap_unchecked`, so be certain the data is valid.
    pub unsafe fn to_string(&self) -> String {
        serde_json::to_string(self).unwrap_unchecked()
    }

    /// Returns the `app_id` (primary identifier).
    pub fn get_id(&self) -> Stringy {
        self.app_id.clone()
    }
}

impl fmt::Display for AppStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let system = match self.app_data.is_system_application() {
            true => "YES".bold().green(),
            false => "NO".bold().red(),
        };

        write!(
            f,
            "{}: {}, {}: {} seconds, {}: {}, {}: {}, {}: {}, {} {}",
            "App ID".bold().cyan(),
            self.app_id,
            "Uptime".bold().cyan(),
            self.uptime.unwrap_or(0),
            "Metrics".bold().cyan(),
            self.metrics
                .as_ref()
                .map(|m| m.to_string())
                .unwrap_or_else(|| "None".to_string()),
            "State Data".bold().cyan(),
            format!("{}\n{}", self.app_data.state, self.app_data.config),
            "Timestamp".bold().cyan(),
            self.timestamp,
            "System App".bold().cyan(),
            system,
        )
    }
}

/// A response structure returned after processing a command.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CommandResponse {
    /// The unique identifier of the target application.
    pub app_id: ID,
    /// The type of command that was processed.
    pub command_type: CommandType,
    /// Indicates whether the command was successful.
    pub success: bool,
    /// An optional message that can contain error details or additional info.
    pub message: Option<String>,
}

impl fmt::Display for CommandResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {}, {}: {}, {}: {}, {}: {}",
            "App ID".bold().cyan(),
            self.app_id,
            "Command Type".bold().cyan(),
            self.command_type,
            "Success".bold().cyan(),
            if self.success {
                "Yes".green()
            } else {
                "No".red()
            },
            "Message".bold().cyan(),
            self.message.as_deref().unwrap_or("None")
        )
    }
}

/// Used to register a new application with the system (local or aggregator).
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RegisterApp {
    /// A unique identifier for the application to be registered.
    pub app_id: ID,
    /// Human-readable name of the application.
    pub app_name: String,
    /// The status that we expect the application to have once registered.
    pub expected_status: Status,
    /// Indicates if this application is part of system processes.
    pub system_application: bool,
    /// The timestamp when registration was requested.
    pub registration_timestamp: u64,
}

impl fmt::Display for RegisterApp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {}, {}: {}, {}: {}, {}: {}",
            "App ID".bold().cyan(),
            self.app_id,
            "App Name".bold().cyan(),
            self.app_name,
            "Expected Status".bold().cyan(),
            self.expected_status,
            "Registration Timestamp".bold().cyan(),
            self.registration_timestamp
        )
    }
}

/// Used to deregister an application from the system (local or aggregator).
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DeregisterApp {
    /// Unique identifier of the application being deregistered.
    pub app_id: ID,
    /// Timestamp when deregistration was requested.
    pub deregistration_timestamp: u64,
}

impl fmt::Display for DeregisterApp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {}, {}: {}",
            "App ID".bold().cyan(),
            self.app_id,
            "Deregistration Timestamp".bold().cyan(),
            self.deregistration_timestamp
        )
    }
}

/// A message for updating an application's status, errors, and metrics.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct UpdateApp {
    /// Identifier of the application to be updated.
    pub app_id: ID,
    /// A list of errors encountered by the application, if any.
    pub error: Option<Vec<ErrorArrayItem>>,
    /// Updated metrics (CPU, memory, etc.).
    pub metrics: Option<Metrics>,
    /// New status (running, stopped, warning, etc.).
    pub status: Status,
    /// The timestamp when the update was performed.
    pub timestamp: u64,
}

impl fmt::Display for UpdateApp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}: {}, {}: {}, {}: {}, {}: {}",
            "App ID".bold().cyan(),
            self.app_id,
            "Status".bold().cyan(),
            self.status,
            "Error".bold().cyan(),
            {
                let mut data = String::new();

                match self.error.clone() {
                    Some(err) => {
                        let errors = err.iter();
                        for e in errors {
                            data.push_str(&e.to_string());
                        }
                        data
                    }
                    None => "None".to_owned(),
                }
            },
            "Timestamp".bold().cyan(),
            self.timestamp
        )
    }
}

/// Encapsulates different message variants related to application registration,
/// updates, and aggregator communication.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum AppMessage {
    /// Register a new application.
    Register(RegisterApp),
    /// Deregister an existing application.
    Deregister(DeregisterApp),
    /// Update an existing application’s status, metrics, etc.
    Update(UpdateApp),
    /// A response to a previously-issued command.
    Response(CommandResponse),
    /// A command targeted at an application.
    Command(Command),
    /// Manager-level information data.
    ManagerInfo(ManagerData),
}

impl fmt::Display for AppMessage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AppMessage::Register(register) => write!(f, "Register: {}", register),
            AppMessage::Deregister(deregister) => write!(f, "Deregister: {}", deregister),
            AppMessage::Update(update) => write!(f, "Update: {}", update),
            AppMessage::Response(response) => write!(f, "Response: {}", response),
            AppMessage::Command(command) => write!(f, "Command: {}", command),
            AppMessage::ManagerInfo(manager_data) => write!(f, "Manager Data: {}", manager_data),
        }
    }
}

//
// Functions
//

/// Saves a slice of `AppStatus` objects to a JSON file at [`AGGREGATOR_PATH`],
/// encrypting the data before writing.
///
/// # Arguments
///
/// * `apps` - A reference to a slice of `AppStatus` structs to be saved.
///
/// # Returns
///
/// * `Ok(())` on success.
/// * `Err(ErrorArrayItem)` containing file I/O or serialization errors on failure.
pub async fn save_registered_apps(apps: &[AppStatus]) -> Result<(), ErrorArrayItem> {
    let mut file: File = OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(true)
        .open(AGGREGATOR_PATH)
        .map_err(ErrorArrayItem::from)?;
    let data: String = serde_json::to_string_pretty(apps).map_err(ErrorArrayItem::from)?;
    // let encrypted_data: Stringy = encrypt_text(data.into()).await?;
    let encrypted_data: Stringy = simple_encrypt(data.as_bytes())?;
    match file.write_all(encrypted_data.as_bytes()) {
        Ok(_) => Ok(()),
        Err(err) => Err(ErrorArrayItem::from(err)),
    }
}

/// Loads a list of `AppStatus` objects from the JSON file at [`AGGREGATOR_PATH`],
/// decrypting the data after reading.
///
/// # Returns
///
/// * `Ok(Vec<AppStatus>)` on success.
/// * `Err(ErrorArrayItem)` if file I/O, decryption, or JSON deserialization fails.
pub async fn load_registered_apps() -> Result<Vec<AppStatus>, ErrorArrayItem> {
    log!(LogLevel::Info, "Loading saved app status array");
    let mut file: File = File::open(AGGREGATOR_PATH)?;
    let mut encrypted_data: String = String::new();
    file.read_to_string(&mut encrypted_data)?;

    // let data: Stringy = decrypt_text(Stringy::from(encrypted_data)).await?;
    let data: Stringy = simple_decrypt(encrypted_data.as_bytes()).map(
        |data| -> Result<Stringy, ErrorArrayItem> {
            let d = String::from_utf8(data)
                .map_err(ErrorArrayItem::from)
                .map(Stringy::from)?;
            Ok(d)
        },
    )??;

    let apps: Vec<AppStatus> = serde_json::from_str(&data)?;
    for app in apps.clone() {
        log!(LogLevel::Debug, "App Status from file: {} \n", app);
    }
    // Clearing screen can be done if needed:
    // print!("\x1B[2J\x1B[H");
    Ok(apps)
}

/// Sets up the metrics system and project queue for asynchronous processing.
///
/// This function spawns the background task that flushes the usage map to disk,
/// and it also spawns a task that listens to the metrics broadcast channel to
/// update in-memory usage data. The returned `project_rx` should be wired into
/// a dedicated task that handles insertion of project data via the Clipas system.
///
/// Returns an `AppContext` to be passed throughout the application.
pub async fn initialize_app_context(
    output_dir: PathType,
) -> (
    AppContext,
    broadcast::Receiver<ProjectInfo>,
    // tokio::sync::mpsc::UnboundedReceiver<ProjectInfo>,
) {
    let usage_map: UsageMap = LockWithTimeout::new(HashMap::new());
    let (metrics_tx, mut metrics_rx) = broadcast::channel::<LiveMetrics>(2048);
    let (project_tx, project_rx) = broadcast::channel::<ProjectInfo>(2048);

    let usage_map_clone = usage_map.clone();
    tokio::spawn(async move {
        while let Ok(metric) = metrics_rx.recv().await {
            if let Err(err) = update_metrics(metric, &usage_map_clone).await {
                log!(
                    LogLevel::Warn,
                    "Error monitoring usage data: {}",
                    err.err_mesg
                )
            }
        }
    });

    spawn_flush_task(usage_map.clone(), output_dir).await;

    let context = AppContext {
        usage_map,
        metrics_tx,
        project_tx,
    };

    (context, project_rx)
}