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
use std::time::{Duration, Instant};

use crate::instruments::{
    AcceptAllLabels, Instrument, InstrumentAdapter, LabelFilter, LabelPredicate, Update, Updates,
};
use crate::snapshot::Snapshot;
use crate::util;
use crate::{Descriptive, PutsSnapshot};

use super::NameAlternation;

/// Changes the state based on the absence of
/// an observation
/// within a given time.
///
/// Can be used for alerting, e.g. if something
/// expected was not observed within a given time-frame.
///
/// Note:
/// The first occurrence will be when this instrument is
/// created so that the indicator does not turn on
/// right from the start.
pub struct NonOccurrenceIndicator {
    name: String,
    title: Option<String>,
    description: Option<String>,
    if_not_happened_within: Duration,
    happened_last: Instant,
    invert: bool,
    show_inverted: Option<NameAlternation>,
}

impl NonOccurrenceIndicator {
    pub fn new<T: Into<String>>(name: T) -> NonOccurrenceIndicator {
        NonOccurrenceIndicator {
            name: name.into(),
            title: None,
            description: None,
            if_not_happened_within: Duration::from_secs(60),
            happened_last: Instant::now(),
            invert: false,
            show_inverted: None,
        }
    }

    pub fn new_with_defaults<T: Into<String>>(name: T) -> NonOccurrenceIndicator {
        Self::new(name)
    }

    pub fn get_name(&self) -> &str {
        &self.name
    }

    pub fn set_name<T: Into<String>>(&mut self, name: T) {
        self.name = name.into();
    }

    pub fn name<T: Into<String>>(mut self, name: T) -> Self {
        self.set_name(name);
        self
    }

    pub fn set_title<T: Into<String>>(&mut self, title: T) {
        self.title = Some(title.into())
    }

    pub fn title<T: Into<String>>(mut self, title: T) -> Self {
        self.set_title(title);
        self
    }

    pub fn set_description<T: Into<String>>(&mut self, description: T) {
        self.description = Some(description.into())
    }

    pub fn description<T: Into<String>>(mut self, description: T) -> Self {
        self.set_description(description);
        self
    }

    /// Set whether the current value should be inverted in a snapshot or not
    ///
    /// Default is `false`
    pub fn set_invert_enabled(&mut self, invert: bool) {
        self.invert = invert
    }

    /// Set whether the current value should be inverted in a snapshot or not
    ///
    /// Default is `false`
    pub fn invert_enabled(mut self, invert: bool) -> Self {
        self.set_invert_enabled(invert);
        self
    }

    /// The current value should be inverted in a snapshot
    ///
    /// Same as `self.set_invert(true);`
    pub fn inverted(mut self) -> Self {
        self.set_invert_enabled(true);
        self
    }

    /// return whether invert is on or off
    pub fn is_inverted(&self) -> bool {
        self.invert
    }

    pub fn set_if_not_happened_within(&mut self, d: Duration) {
        self.if_not_happened_within = d;
    }

    pub fn if_not_happened_within(mut self, d: Duration) -> Self {
        self.set_if_not_happened_within(d);
        self
    }

    pub fn get_if_not_happened_within(&self) -> Duration {
        self.if_not_happened_within
    }

    /// Show the inverted value. Name will be adjusted with `name_alternation`.
    pub fn set_show_inverted(&mut self, name_alternation: NameAlternation) {
        self.show_inverted = Some(name_alternation)
    }

    /// Show the inverted value. Name will be adjusted with `name_alternation`.
    pub fn show_inverted(mut self, name_alternation: NameAlternation) -> Self {
        self.set_show_inverted(name_alternation);
        self
    }

    /// Show the inverted value. Name will be prefixed with `prefix`.
    pub fn set_show_inverted_prefixed<T: Into<String>>(&mut self, prefix: T) {
        self.set_show_inverted(NameAlternation::Prefix(prefix.into()))
    }

    /// Show the inverted value. Name will be prefixed with `prefix`.
    pub fn show_inverted_prefixed<T: Into<String>>(mut self, prefix: T) -> Self {
        self.set_show_inverted(NameAlternation::Prefix(prefix.into()));
        self
    }

    /// Show the inverted value. Name will be postfixed with `postfix`.
    pub fn set_show_inverted_postfixed<T: Into<String>>(&mut self, postfix: T) {
        self.set_show_inverted(NameAlternation::Postfix(postfix.into()))
    }

    /// Show the inverted value. Name will be postfixed with `postfix`.
    pub fn show_inverted_postfixed<T: Into<String>>(mut self, postfix: T) -> Self {
        self.set_show_inverted(NameAlternation::Postfix(postfix.into()));
        self
    }

    /// Show the inverted value. Name will be renamed with `new_name`.
    pub fn set_show_inverted_renamed<T: Into<String>>(&mut self, new_name: T) {
        self.set_show_inverted(NameAlternation::Rename(new_name.into()))
    }

    /// Show the inverted value. Name will be renamed with `new_name`.
    pub fn show_inverted_renamed<T: Into<String>>(mut self, new_name: T) -> Self {
        self.set_show_inverted(NameAlternation::Rename(new_name.into()));
        self
    }

    pub fn accept<L: Eq + Send + 'static, F: Into<LabelFilter<L>>>(
        self,
        accept: F,
    ) -> InstrumentAdapter<L, Self> {
        InstrumentAdapter::accept(accept, self)
    }

    /// Creates an `InstrumentAdapter` that makes this instrument
    /// react on observations on the given label.
    pub fn for_label<L: Eq + Send + 'static>(self, label: L) -> InstrumentAdapter<L, Self> {
        self.accept(label)
    }

    /// Creates an `InstrumentAdapter` that makes this instrument
    /// react on observations with the given labels.
    ///
    /// If `labels` is empty the instrument will not react to any observations
    pub fn for_labels<L: Eq + Send + 'static>(self, labels: Vec<L>) -> InstrumentAdapter<L, Self> {
        self.accept(labels)
    }

    /// Creates an `InstrumentAdapter` that makes this instrument react on
    /// all observations.
    pub fn for_all_labels<L: Eq + Send + 'static>(self) -> InstrumentAdapter<L, Self> {
        self.accept(AcceptAllLabels)
    }

    /// Creates an `InstrumentAdapter` that makes this instrument react on
    /// observations with labels specified by the predicate.
    pub fn for_labels_by_predicate<L, P>(self, label_predicate: P) -> InstrumentAdapter<L, Self>
    where
        L: Eq + Send + 'static,
        P: Fn(&L) -> bool + Send + 'static,
    {
        self.accept(LabelPredicate(label_predicate))
    }

    /// Creates an `InstrumentAdapter` that makes this instrument to no
    /// observations.
    pub fn adapter<L: Eq + Send + 'static>(self) -> InstrumentAdapter<L, Self> {
        InstrumentAdapter::deaf(self)
    }

    /// Returns the current state
    pub fn state(&self) -> bool {
        let must_have_happened_after = Instant::now() - self.if_not_happened_within;
        let current_state = self.happened_last > must_have_happened_after;

        if self.invert {
            !current_state
        } else {
            current_state
        }
    }
}

impl Instrument for NonOccurrenceIndicator {}

impl PutsSnapshot for NonOccurrenceIndicator {
    fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
        util::put_postfixed_descriptives(self, &self.name, into, descriptive);

        into.items.push((self.name.clone(), self.state().into()));
        if let Some(alternation) = &self.show_inverted {
            let label = alternation.adjust_name(&self.name);
            into.items.push((label.into(), (!self.state()).into()));
        }
    }
}

impl Updates for NonOccurrenceIndicator {
    fn update(&mut self, _: &Update) -> usize {
        self.happened_last = Instant::now();
        1
    }
}

impl Descriptive for NonOccurrenceIndicator {
    fn title(&self) -> Option<&str> {
        self.title.as_ref().map(|n| &**n)
    }

    fn description(&self) -> Option<&str> {
        self.description.as_ref().map(|n| &**n)
    }
}