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

use re_data_store::{StoreEvent, StoreSubscriber};
use re_log_types::{TimeInt, TimePoint, 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: &[(Timeline, TimeInt)], n: u32) {
        if times.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, time) in times {
                self.times
                    .entry(timeline)
                    .or_default()
                    .increment(time.as_i64(), n);
            }
        }
    }

    pub fn remove(&mut self, timepoint: &TimePoint, n: u32) {
        if timepoint.is_static() {
            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, time_value) in timepoint.iter() {
                let remaining_count = self
                    .times
                    .entry(*timeline)
                    .or_default()
                    .decrement(time_value.as_i64(), n);
                if remaining_count == 0 {
                    self.times.remove(timeline);
                }
            }
        }
    }
}

// NOTE: This is only to let people know that this is in fact a [`StoreSubscriber`], so they A) don't try
// to implement it on their own and B) don't try to register it.
impl StoreSubscriber 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: &[StoreEvent]) {
        unimplemented!(
            r"TimeHistogramPerTimeline view is maintained as a sub-view of `EntityTree`",
        );
    }
}