ant-cli 0.5.2

CLI client for the Autonomi network
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
// Copyright 2025 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::{AnalysisErrorDisplay, ClosestPeerStatus, NetworkErrorDisplay};
use crate::commands::analyze::{HolderStatus, KAD_HOLDERS_QUERY_RANGE};
use autonomi::client::analyze::{Analysis, AnalysisError};
use autonomi::networking::NetworkAddress;
use color_eyre::eyre::Result;
use file_rotate::{ContentLimit, FileRotate, compression::Compression, suffix::AppendCount};
use serde::Serialize;
use std::io::Write;
use std::path::Path;
use std::time::SystemTime;

/// Root JSON output structure
#[derive(Debug, Serialize)]
pub struct JsonOutput {
    pub provided_address: String,
    pub ant_package_version: String,
    pub analyzed_addresses: Vec<AnalyzedAddress>,
}

/// Analysis result for a single address
#[derive(Debug, Serialize)]
pub struct AnalyzedAddress {
    pub target_address: String,
    pub kad_method: KadMethod,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub closest_method: Option<ClosestMethod>,
}

/// Kademlia query result
#[derive(Debug, Serialize)]
pub struct KadMethod {
    pub analysis_query: KadAnalysisQuery,
    pub holder_query: KadHolderQuery,
}

/// Kademlia analysis query result
#[derive(Debug, Serialize)]
pub struct KadAnalysisQuery {
    pub query_status: QueryStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_type: Option<AnalysisErrorDisplay>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address_type: Option<String>,
}

/// Kademlia holder query result
#[derive(Debug, Serialize)]
pub struct KadHolderQuery {
    pub query_range: u32,
    pub holding_count: u32,
    pub holders: Vec<KadHolders>,
}

#[derive(Debug, Serialize)]
pub struct KadHolders {
    pub peer_id: String,
    pub distance_to_target_ilog2: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_size_bytes: Option<usize>,
}

/// Closest peers query result
#[derive(Debug, Serialize)]
pub struct ClosestMethod {
    pub query_status: QueryStatus,
    pub closest_peers: Vec<ClosestPeer>,
    pub peer_count: usize,
    pub holders_among_7_closest: usize,
    pub holders_among_20_closest: usize,
}

/// Individual peer information
#[derive(Debug, Serialize)]
pub struct ClosestPeer {
    pub peer_id: String,
    pub holding_status: ClosestPeerHoldingStatus,
    pub distance_to_target_ilog2: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_size_bytes: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_type: Option<NetworkErrorDisplay>,
}

/// Status of the kademlia query
#[derive(Debug, Serialize)]
#[allow(dead_code)]
pub enum QueryStatus {
    Success,
    Error,
}

/// Holding status for individual peers in closest peers query
#[derive(Debug, Serialize)]
pub enum ClosestPeerHoldingStatus {
    Holding,
    NotHolding,
    FailedQuery,
}

impl JsonOutput {
    pub fn new(provided_address: String) -> Self {
        Self {
            provided_address,
            ant_package_version: env!("CARGO_PKG_VERSION").to_string(),
            analyzed_addresses: Vec::new(),
        }
    }

    pub fn add_address(&mut self, analyzed: AnalyzedAddress) {
        self.analyzed_addresses.push(analyzed);
    }
}

impl AnalyzedAddress {
    pub fn new(
        address: String,
        analysis_result: &Result<Analysis, AnalysisError>,
        closest_peers_data: Option<Vec<ClosestPeerStatus>>,
        holders: Option<Vec<HolderStatus>>,
        target_addr: &NetworkAddress,
    ) -> Self {
        let kad_method =
            KadMethod::from_analysis_and_holders(analysis_result, holders, target_addr);
        let closest_method =
            closest_peers_data.map(|peers| ClosestMethod::from_peer_statuses(peers, target_addr));
        Self {
            target_address: strip_0x_prefix(&address),
            kad_method,
            closest_method,
        }
    }
}

impl KadMethod {
    fn from_analysis_and_holders(
        analysis: &Result<Analysis, AnalysisError>,
        holders: Option<Vec<HolderStatus>>,
        target_addr: &NetworkAddress,
    ) -> Self {
        let analysis_query = KadAnalysisQuery::from_analysis(analysis);
        let holder_query = KadHolderQuery::from_holders(holders, target_addr);

        Self {
            analysis_query,
            holder_query,
        }
    }
}

impl KadAnalysisQuery {
    fn from_analysis(analysis: &Result<Analysis, AnalysisError>) -> Self {
        match analysis {
            Ok(analysis) => Self {
                query_status: QueryStatus::Success,
                error_type: None,
                address_type: Some(get_analysis_type(analysis)),
            },
            Err(err) => Self {
                query_status: QueryStatus::Error,
                error_type: Some(AnalysisErrorDisplay::from_analysis_error(err)),
                address_type: None,
            },
        }
    }
}

impl KadHolderQuery {
    fn from_holders(holders: Option<Vec<HolderStatus>>, target_addr: &NetworkAddress) -> Self {
        let kad_holders = if let Some(holders) = holders {
            holders
                .into_iter()
                .map(|holder| {
                    let peer_addr = NetworkAddress::from(holder.peer_id);
                    let distance = target_addr.distance(&peer_addr);
                    let distance_to_target_ilog2 = distance.ilog2().unwrap_or(0);
                    KadHolders {
                        peer_id: holder.peer_id.to_string(),
                        distance_to_target_ilog2,
                        record_size_bytes: Some(holder.size),
                    }
                })
                .collect()
        } else {
            Vec::new()
        };

        Self {
            query_range: KAD_HOLDERS_QUERY_RANGE,
            holding_count: kad_holders.len() as u32,
            holders: kad_holders,
        }
    }
}

impl ClosestMethod {
    fn from_peer_statuses(statuses: Vec<ClosestPeerStatus>, target_addr: &NetworkAddress) -> Self {
        let peer_count = statuses.len();

        // Check if any peer has a failed query
        let has_failed_query = statuses
            .iter()
            .any(|status| matches!(status, ClosestPeerStatus::FailedQuery { .. }));

        // Determine query status: success if all peers are error-free (Holding or NotHolding)
        // failure if even one peer had a FailedQuery
        let query_status = if has_failed_query {
            QueryStatus::Error
        } else {
            QueryStatus::Success
        };

        // Count holders among first 7 and all 20 peers
        let holders_among_7_closest = statuses
            .iter()
            .take(7)
            .filter(|status| matches!(status, ClosestPeerStatus::Holding { .. }))
            .count();

        let holders_among_20_closest = statuses
            .iter()
            .filter(|status| matches!(status, ClosestPeerStatus::Holding { .. }))
            .count();

        let closest_peers = statuses
            .into_iter()
            .map(|status| ClosestPeer::from_status(status, target_addr))
            .collect();

        Self {
            query_status,
            closest_peers,
            peer_count,
            holders_among_7_closest,
            holders_among_20_closest,
        }
    }
}

impl ClosestPeer {
    fn from_status(status: ClosestPeerStatus, target_addr: &NetworkAddress) -> Self {
        let peer_id = status.peer_id().to_string();
        let peer_addr = NetworkAddress::from(*status.peer_id());
        let distance = target_addr.distance(&peer_addr);
        let distance_ilog2 = distance.ilog2().unwrap_or(0);

        match status {
            ClosestPeerStatus::Holding { size, .. } => Self {
                peer_id,
                holding_status: ClosestPeerHoldingStatus::Holding,
                distance_to_target_ilog2: distance_ilog2,
                record_size_bytes: Some(size),
                error_type: None,
            },
            ClosestPeerStatus::NotHolding { .. } => Self {
                peer_id,
                holding_status: ClosestPeerHoldingStatus::NotHolding,
                distance_to_target_ilog2: distance_ilog2,
                record_size_bytes: None,
                error_type: None,
            },
            ClosestPeerStatus::FailedQuery { error, .. } => Self {
                peer_id,
                holding_status: ClosestPeerHoldingStatus::FailedQuery,
                distance_to_target_ilog2: distance_ilog2,
                record_size_bytes: None,
                error_type: Some(error),
            },
        }
    }
}

/// Get the type name from Analysis enum
fn get_analysis_type(analysis: &Analysis) -> String {
    match analysis {
        Analysis::Chunk(_) => "Chunk".to_string(),
        Analysis::GraphEntry(_) => "GraphEntry".to_string(),
        Analysis::Pointer(_) => "Pointer".to_string(),
        Analysis::Scratchpad(_) => "Scratchpad".to_string(),
        Analysis::Register { .. } => "Register".to_string(),
        Analysis::DataMap { .. } => "DataMap".to_string(),
        Analysis::RawDataMap { .. } => "RawDataMap".to_string(),
        Analysis::PublicArchive { .. } => "PublicArchive".to_string(),
        Analysis::PrivateArchive(_) => "PrivateArchive".to_string(),
    }
}

/// Remove 0x prefix from hex strings if present
fn strip_0x_prefix(s: &str) -> String {
    s.trim_start_matches("0x").to_string()
}

/// Flattened JSON entry for transformed output
#[derive(Debug, Serialize)]
pub struct FlattenedEntry {
    pub target_address: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peer: Option<usize>,
    pub source: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peer_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub distance_to_target_ilog2: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub record_size_bytes: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub holding_count: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub holding_status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub holders_among_closest: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query_status: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub address_type: Option<String>,
    pub timestamp: u128,
}

impl FlattenedEntry {
    /// Flatten holder_query entries
    fn from_holder_query(
        target_address: &str,
        peer_idx: usize,
        holder: &KadHolders,
        holding_count: u32,
        query_status: &str,
        address_type: Option<&str>,
        timestamp: u128,
    ) -> Self {
        Self {
            target_address: target_address.to_string(),
            peer: Some(peer_idx),
            source: "holder_query".to_string(),
            peer_id: Some(holder.peer_id.clone()),
            distance_to_target_ilog2: Some(holder.distance_to_target_ilog2),
            record_size_bytes: holder.record_size_bytes,
            holding_count: Some(holding_count),
            holding_status: None,
            holders_among_closest: None,
            query_status: Some(query_status.to_string()),
            address_type: address_type.map(|s| s.to_string()),
            timestamp,
        }
    }

    /// Flatten closest_peers entries
    fn from_closest_peer(
        target_address: &str,
        peer_idx: usize,
        peer: &ClosestPeer,
        query_status: &str,
        address_type: Option<&str>,
        timestamp: u128,
    ) -> Self {
        let holding_status = match &peer.holding_status {
            ClosestPeerHoldingStatus::Holding => "Holding",
            ClosestPeerHoldingStatus::NotHolding => "NotHolding",
            ClosestPeerHoldingStatus::FailedQuery => "FailedQuery",
        };

        Self {
            target_address: target_address.to_string(),
            peer: Some(peer_idx),
            source: "closest_peers".to_string(),
            peer_id: Some(peer.peer_id.clone()),
            distance_to_target_ilog2: Some(peer.distance_to_target_ilog2),
            record_size_bytes: peer.record_size_bytes,
            holding_count: None,
            holding_status: Some(holding_status.to_string()),
            holders_among_closest: None,
            query_status: Some(query_status.to_string()),
            address_type: address_type.map(|s| s.to_string()),
            timestamp,
        }
    }

    /// Flatten closest_7 or closest_20 summary
    fn from_closest_summary(
        target_address: &str,
        source: &str,
        holders_count: usize,
        query_status: &str,
        address_type: Option<&str>,
        timestamp: u128,
    ) -> Self {
        Self {
            target_address: target_address.to_string(),
            peer: None,
            source: source.to_string(),
            peer_id: None,
            distance_to_target_ilog2: None,
            record_size_bytes: None,
            holding_count: None,
            holding_status: None,
            holders_among_closest: Some(holders_count),
            query_status: Some(query_status.to_string()),
            address_type: address_type.map(|s| s.to_string()),
            timestamp,
        }
    }
}

const MAX_FILE_SIZE: usize = 50 * 1024 * 1024; // 50 MB
const MAX_FILES: usize = 10;

/// Writer for JSON output that supports both file and directory targets
pub struct JsonWriter {
    writer: WriterType,
    transformed_writer: WriterType,
    /// Current timestamp base for the current target address batch (in nanoseconds)
    current_timestamp_base_ns: u128,
}

enum WriterType {
    /// Simple file writer (append-only)
    File(std::fs::File),
    /// Rotating file writer for directories
    Rotating(FileRotate<AppendCount>),
}

impl JsonWriter {
    /// Create a new JSON writer
    ///
    /// If path is a file, opens it in append mode.
    /// If path is a directory, creates a rotating file appender with:
    /// - 50 MB max per file
    /// - 10 files max
    /// - No compression
    pub fn new(path: &Path) -> Result<Self> {
        let (writer, transformed_writer) = if path.is_dir() {
            // Directory: use rotating writers for both files
            let json_file_path = path.join("analyze.json");
            let file_rotate = FileRotate::new(
                json_file_path,
                AppendCount::new(MAX_FILES),
                ContentLimit::BytesSurpassed(MAX_FILE_SIZE),
                Compression::None,
                #[cfg(unix)]
                None,
            );

            let transformed_file_path = path.join("analyze_transformed.json");
            let transformed_rotate = FileRotate::new(
                transformed_file_path,
                AppendCount::new(MAX_FILES),
                ContentLimit::BytesSurpassed(MAX_FILE_SIZE),
                Compression::None,
                #[cfg(unix)]
                None,
            );

            (
                WriterType::Rotating(file_rotate),
                WriterType::Rotating(transformed_rotate),
            )
        } else {
            // File: use simple append mode
            // Create parent directory if it doesn't exist
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            let file = std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(path)?;

            // Create transformed file path by inserting "_transformed" before extension
            let transformed_path = if let Some(parent) = path.parent() {
                let file_name = path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("analyze");
                let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("json");
                parent.join(format!("{file_name}_transformed.{ext}"))
            } else {
                let file_name = path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("analyze");
                let ext = path.extension().and_then(|s| s.to_str()).unwrap_or("json");
                Path::new(&format!("{file_name}_transformed.{ext}")).to_path_buf()
            };

            let transformed_file = std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(transformed_path)?;

            (WriterType::File(file), WriterType::File(transformed_file))
        };

        // Initialize timestamp with current time in nanoseconds
        let current_timestamp_base_ns = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);

        Ok(Self {
            writer,
            transformed_writer,
            current_timestamp_base_ns,
        })
    }

    /// Write JSON string to the original analyze.json output
    pub fn write_json(&mut self, json_str: &str) -> Result<()> {
        match &mut self.writer {
            WriterType::File(file) => {
                writeln!(file, "{json_str}")?;
                file.flush()?;
            }
            WriterType::Rotating(rotate) => {
                writeln!(rotate, "{json_str}")?;
                rotate.flush()?;
            }
        }
        Ok(())
    }

    /// Write the transformed JSON output for the given JsonOutput
    pub fn write_transformed_json(&mut self, json_output: &JsonOutput) -> Result<()> {
        // Process each analyzed address
        for analyzed_addr in &json_output.analyzed_addresses {
            let base_timestamp = self.current_timestamp_base_ns;
            let mut timestamp = base_timestamp;

            // Get query status and address type from kad_method
            let kad_query_status = match analyzed_addr.kad_method.analysis_query.query_status {
                QueryStatus::Success => "Success",
                QueryStatus::Error => "Error",
            };
            let kad_address_type = analyzed_addr
                .kad_method
                .analysis_query
                .address_type
                .as_deref();

            // Flatten holder_query peers
            for (idx, holder) in analyzed_addr
                .kad_method
                .holder_query
                .holders
                .iter()
                .enumerate()
            {
                let entry = FlattenedEntry::from_holder_query(
                    &analyzed_addr.target_address,
                    idx,
                    holder,
                    analyzed_addr.kad_method.holder_query.holding_count,
                    kad_query_status,
                    kad_address_type,
                    timestamp,
                );
                self.write_flattened_entry(&entry)?;
                timestamp += 1;
            }

            // Process closest_method if present
            if let Some(closest_method) = &analyzed_addr.closest_method {
                let closest_query_status = match closest_method.query_status {
                    QueryStatus::Success => "Success",
                    QueryStatus::Error => "Error",
                };

                // Flatten closest_peers
                for (idx, peer) in closest_method.closest_peers.iter().enumerate() {
                    let entry = FlattenedEntry::from_closest_peer(
                        &analyzed_addr.target_address,
                        idx,
                        peer,
                        closest_query_status,
                        kad_address_type,
                        timestamp,
                    );
                    self.write_flattened_entry(&entry)?;
                    timestamp += 1;
                }

                // Flatten closest_7 summary (offset +0)
                let entry_7 = FlattenedEntry::from_closest_summary(
                    &analyzed_addr.target_address,
                    "closest_7",
                    closest_method.holders_among_7_closest,
                    closest_query_status,
                    kad_address_type,
                    base_timestamp,
                );
                self.write_flattened_entry(&entry_7)?;

                // Flatten closest_20 summary (offset +1)
                let entry_20 = FlattenedEntry::from_closest_summary(
                    &analyzed_addr.target_address,
                    "closest_20",
                    closest_method.holders_among_20_closest,
                    closest_query_status,
                    kad_address_type,
                    base_timestamp + 1,
                );
                self.write_flattened_entry(&entry_20)?;
            }

            // Increment base timestamp by 1ms for next target address
            self.current_timestamp_base_ns += 1_000_000;
        }

        Ok(())
    }

    /// Write a single flattened entry to the transformed output
    fn write_flattened_entry(&mut self, entry: &FlattenedEntry) -> Result<()> {
        let json_str = serde_json::to_string(entry)?;
        match &mut self.transformed_writer {
            WriterType::File(file) => {
                writeln!(file, "{json_str}")?;
                file.flush()?;
            }
            WriterType::Rotating(rotate) => {
                writeln!(rotate, "{json_str}")?;
                rotate.flush()?;
            }
        }
        Ok(())
    }
}