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

use itertools::Itertools as _;
use re_chunk_store::ChunkStoreDiffKind;
use re_chunk_store::{ChunkStoreEvent, ChunkStoreSubscriber};
use re_log_types::Timeline;

// ---

/// Number of messages per time.
pub type TimeHistogram = re_int_histogram::Int64Histogram;

/// Number of messages per time per timeline.
///
/// Does NOT include timeless.
#[derive(Default)]
pub struct TimeHistogramPerTimeline {
    /// When do we have data? Ignores timeless.
    times: BTreeMap<Timeline, TimeHistogram>,

    /// Extra bookkeeping used to seed any timelines that include static msgs.
    num_static_messages: u64,
}

impl TimeHistogramPerTimeline {
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.times.is_empty() && self.num_static_messages == 0
    }

    #[inline]
    pub fn is_static(&self) -> bool {
        self.num_static_messages > 0
    }

    #[inline]
    pub fn timelines(&self) -> impl ExactSizeIterator<Item = &Timeline> {
        self.times.keys()
    }

    #[inline]
    pub fn get(&self, timeline: &Timeline) -> Option<&TimeHistogram> {
        self.times.get(timeline)
    }

    #[inline]
    pub fn has_timeline(&self, timeline: &Timeline) -> bool {
        self.times.contains_key(timeline)
    }

    #[inline]
    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&Timeline, &TimeHistogram)> {
        self.times.iter()
    }

    #[inline]
    pub fn num_static_messages(&self) -> u64 {
        self.num_static_messages
    }

    /// Total number of temporal messages over all timelines.
    pub fn num_temporal_messages(&self) -> u64 {
        self.times.values().map(|hist| hist.total_count()).sum()
    }

    pub fn add(&mut self, times_per_timeline: &[(Timeline, &[i64])], n: u32) {
        re_tracing::profile_function!();

        if times_per_timeline.is_empty() {
            self.num_static_messages = self
                .num_static_messages
                .checked_add(n as u64)
                .unwrap_or_else(|| {
                    re_log::debug!(
                        current = self.num_static_messages,
                        added = n,
                        "bookkeeping overflowed"
                    );
                    u64::MAX
                });
        } else {
            for &(timeline, times) in times_per_timeline {
                let histogram = self.times.entry(timeline).or_default();
                for &time in times {
                    histogram.increment(time, n);
                }
            }
        }
    }

    pub fn remove(&mut self, times_per_timeline: &[(Timeline, &[i64])], n: u32) {
        re_tracing::profile_function!();

        if times_per_timeline.is_empty() {
            self.num_static_messages = self
                .num_static_messages
                .checked_sub(n as u64)
                .unwrap_or_else(|| {
                    // We used to hit this on plots demo, see https://github.com/rerun-io/rerun/issues/4355.
                    re_log::debug!(
                        current = self.num_static_messages,
                        removed = n,
                        "bookkeeping underflowed"
                    );
                    u64::MIN
                });
        } else {
            for &(timeline, times) in times_per_timeline {
                if let Some(histo) = self.times.get_mut(&timeline) {
                    for &time in times {
                        histo.decrement(time, n);
                    }
                    if histo.is_empty() {
                        self.times.remove(&timeline);
                    }
                }
            }
        }
    }
}

// NOTE: This is only to let people know that this is in fact a [`ChunkStoreSubscriber`], so they A) don't try
// to implement it on their own and B) don't try to register it.
impl ChunkStoreSubscriber for TimeHistogramPerTimeline {
    #[inline]
    fn name(&self) -> String {
        "rerun.store_subscriber.TimeHistogramPerTimeline".into()
    }

    #[inline]
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    #[inline]
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    #[allow(clippy::unimplemented)]
    fn on_events(&mut self, events: &[ChunkStoreEvent]) {
        re_tracing::profile_function!();

        for event in events {
            let times = event
                .chunk
                .timelines()
                .iter()
                .map(|(&timeline, time_column)| (timeline, time_column.times_raw()))
                .collect_vec();
            match event.kind {
                ChunkStoreDiffKind::Addition => {
                    self.add(&times, event.num_components() as _);
                }
                ChunkStoreDiffKind::Deletion => {
                    self.remove(&times, event.num_components() as _);
                }
            }
        }
    }
}