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
use std::collections::BTreeMap;

use re_log_types::{EntityPath, TimeRange, Timeline};
use re_types_core::{components::InstanceKey, ComponentName, Loggable as _, SizeBytes as _};

use crate::{cache::CacheBucket, Caches, LatestAtCache, RangeCache};

// ---

/// Stats for all primary caches.
///
/// Fetch them via [`Caches::stats`].
#[derive(Default, Debug, Clone)]
pub struct CachesStats {
    pub latest_at: BTreeMap<EntityPath, CachedEntityStats>,
    pub range: BTreeMap<EntityPath, Vec<(Timeline, TimeRange, CachedEntityStats)>>,
}

impl CachesStats {
    #[inline]
    pub fn total_size_bytes(&self) -> u64 {
        re_tracing::profile_function!();

        let Self { latest_at, range } = self;

        let latest_at_size_bytes: u64 =
            latest_at.values().map(|stats| stats.total_size_bytes).sum();
        let range_size_bytes: u64 = range
            .values()
            .flat_map(|all_ranges| {
                all_ranges
                    .iter()
                    .map(|(_, _, stats)| stats.total_size_bytes)
            })
            .sum();

        latest_at_size_bytes + range_size_bytes
    }
}

/// Stats for a cached entity.
#[derive(Debug, Clone)]
pub struct CachedEntityStats {
    pub total_rows: u64,
    pub total_size_bytes: u64,

    /// Only if `detailed_stats` is `true` (see [`Caches::stats`]).
    pub per_component: Option<BTreeMap<ComponentName, CachedComponentStats>>,
}

impl CachedEntityStats {
    #[inline]
    pub fn is_empty(&self) -> bool {
        // NOTE: That looks non-sensical, but it can happen if the cache is bugged, which we'd like
        // to know.
        self.total_rows == 0 && self.total_size_bytes == 0
    }
}

/// Stats for a cached component.
#[derive(Default, Debug, Clone)]
pub struct CachedComponentStats {
    pub total_rows: u64,
    pub total_instances: u64,
    pub total_size_bytes: u64,
}

impl Caches {
    /// Computes the stats for all primary caches.
    ///
    /// `per_component` toggles per-component stats.
    pub fn stats(&self, detailed_stats: bool) -> CachesStats {
        re_tracing::profile_function!();

        fn upsert_bucket_stats(
            per_component: &mut BTreeMap<ComponentName, CachedComponentStats>,
            bucket: &CacheBucket,
        ) {
            let CacheBucket {
                data_times,
                pov_instance_keys,
                components,
                total_size_bytes: _,
            } = bucket;

            {
                let stats: &mut CachedComponentStats =
                    per_component.entry("<timepoints>".into()).or_default();
                stats.total_rows += data_times.len() as u64;
                stats.total_instances += data_times.len() as u64;
                stats.total_size_bytes += data_times.total_size_bytes();
            }

            {
                let stats: &mut CachedComponentStats =
                    per_component.entry(InstanceKey::name()).or_default();
                stats.total_rows += pov_instance_keys.num_entries() as u64;
                stats.total_instances += pov_instance_keys.num_values() as u64;
                stats.total_size_bytes += pov_instance_keys.total_size_bytes();
            }

            for (component_name, data) in components {
                let stats: &mut CachedComponentStats =
                    per_component.entry(*component_name).or_default();
                stats.total_rows += data.dyn_num_entries() as u64;
                stats.total_instances += data.dyn_num_values() as u64;
                stats.total_size_bytes += data.dyn_total_size_bytes();
            }
        }

        let caches = self.read().clone();
        // Implicitly releasing top-level cache mappings -- concurrent queries can run once again.

        let latest_at = caches
            .iter()
            .map(|(key, caches_per_arch)| {
                (key.entity_path.clone(), {
                    let mut total_size_bytes = 0u64;
                    let mut total_rows = 0u64;
                    let mut per_component = detailed_stats.then(BTreeMap::default);

                    for latest_at_cache in caches_per_arch
                        .read()
                        .latest_at_per_archetype
                        .read()
                        .values()
                    {
                        let latest_at_cache @ LatestAtCache {
                            per_query_time: _,
                            per_data_time,
                            timeless,
                            ..
                        } = &*latest_at_cache.read();

                        total_size_bytes += latest_at_cache.total_size_bytes();
                        total_rows = per_data_time.len() as u64 + timeless.is_some() as u64;

                        if let Some(per_component) = per_component.as_mut() {
                            re_tracing::profile_scope!("detailed");

                            if let Some(bucket) = &timeless {
                                upsert_bucket_stats(per_component, bucket);
                            }

                            for bucket in per_data_time.values() {
                                upsert_bucket_stats(per_component, bucket);
                            }
                        }
                    }

                    CachedEntityStats {
                        total_size_bytes,
                        total_rows,

                        per_component,
                    }
                })
            })
            .collect();

        let range = caches
            .iter()
            .map(|(key, caches_per_arch)| {
                (key.entity_path.clone(), {
                    caches_per_arch
                        .read()
                        .range_per_archetype
                        .read()
                        .values()
                        .map(|range_cache| {
                            let range_cache @ RangeCache {
                                per_data_time,
                                timeless,
                                timeline: _,
                            } = &*range_cache.read();

                            let total_rows = per_data_time.data_times.len() as u64;

                            let mut per_component = detailed_stats.then(BTreeMap::default);
                            if let Some(per_component) = per_component.as_mut() {
                                re_tracing::profile_scope!("detailed");

                                upsert_bucket_stats(per_component, timeless);
                                upsert_bucket_stats(per_component, per_data_time);
                            }

                            (
                                key.timeline,
                                per_data_time.time_range().unwrap_or(TimeRange::EMPTY),
                                CachedEntityStats {
                                    total_size_bytes: range_cache.total_size_bytes(),
                                    total_rows,

                                    per_component,
                                },
                            )
                        })
                        .collect()
                })
            })
            .collect();

        CachesStats { latest_at, range }
    }
}