rings_core/inspect/
snapshot.rs1use serde::Deserialize;
2use serde::Serialize;
3
4use super::compress_iter;
5use crate::dht::entry::Entry;
6use crate::dht::EntryStorage;
7use crate::dht::PeerRing;
8use crate::swarm::Swarm;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct SwarmInspect {
13 pub peers: Vec<ConnectionInspect>,
15 pub dht: DHTInspect,
17 pub persistence_storage: StorageInspect,
19 pub cache_storage: StorageInspect,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ConnectionInspect {
26 pub did: String,
28 pub state: String,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34pub struct DHTInspect {
35 pub did: String,
37 pub successors: Vec<String>,
39 #[serde(default)]
40 pub predecessor: Option<String>,
42 pub finger_table: Vec<(Option<String>, u64, u64)>,
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
48pub struct StorageInspect {
49 pub items: Vec<(String, Entry)>,
51}
52
53impl SwarmInspect {
54 pub async fn inspect(swarm: &Swarm) -> Self {
56 let dht = DHTInspect::inspect(&swarm.dht());
57 let peers = swarm.peers();
58 let persistence_storage = StorageInspect::inspect_kv_storage(&swarm.dht().storage).await;
59 let cache_storage = StorageInspect::inspect_kv_storage(&swarm.dht().cache).await;
60
61 Self {
62 peers,
63 dht,
64 persistence_storage,
65 cache_storage,
66 }
67 }
68}
69
70impl DHTInspect {
71 pub fn inspect(dht: &PeerRing) -> Self {
73 let did = dht.did.to_string();
74 let topology = dht.topology_state().ok();
75 let successors = topology
76 .as_ref()
77 .map(|state| {
78 state
79 .successors
80 .iter()
81 .copied()
82 .map(|s| s.to_string())
83 .collect()
84 })
85 .unwrap_or_default();
86 let predecessor = topology
87 .as_ref()
88 .and_then(|state| state.predecessor)
89 .map(|predecessor| predecessor.to_string());
90 let finger_table = topology
91 .map(|state| {
92 compress_iter(
93 state
94 .fingers
95 .into_iter()
96 .map(|finger| finger.map(|did| did.to_string())),
97 )
98 })
99 .unwrap_or_default();
100
101 Self {
102 did,
103 successors,
104 predecessor,
105 finger_table,
106 }
107 }
108}
109
110impl StorageInspect {
111 pub async fn inspect_kv_storage(storage: &EntryStorage) -> Self {
113 Self {
114 items: storage
115 .get_all()
116 .await
117 .unwrap_or_default()
118 .into_iter()
119 .collect(),
120 }
121 }
122}