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
use crate::models::log_line::LogLine;
use parking_lot::{lock_api::RwLockReadGuard, RawRwLock, RwLock};

/// Store for managing processed logs.
///
/// Stores both the combined filtered log and the search log
pub trait AnalysisStore {
    /// Add a list of processed lines
    fn add_lines(&self, lines: &[LogLine]);
    /// Add a list of searched lines
    fn add_search_lines(&self, lines: &[LogLine]);
    /// Change the search query
    fn add_search_query(&self, query: &str);
    /// Get the current search query
    fn get_search_query(&self) -> Option<String>;
    /// Clear the processed log
    fn reset_log(&self);
    /// Clear the searched log
    fn reset_search(&self);
    /// Get a RwLock to the current processed log to avoid copying
    fn fetch_log(&self) -> RwLockReadGuard<RawRwLock, Vec<LogLine>>;
    /// Get a RwLock to the current searched log to avoid copying
    fn fetch_search(&self) -> RwLockReadGuard<RawRwLock, Vec<LogLine>>;
    /// Get a copy of a window of lines. Is safe to query out of bounds
    fn get_log_lines(&self, from: usize, to: usize) -> Vec<LogLine>;
    /// Get a copy of a window of search lines. Is safe to query out of bounds
    fn get_search_lines(&self, from: usize, to: usize) -> Vec<LogLine>;
    /// Get a window of `elements` number of lines centered around the target `line`
    ///
    /// Returns (list of lines, offset from start, index of target)
    fn get_log_lines_containing(
        &self,
        index: usize,
        elements: usize,
    ) -> (Vec<LogLine>, usize, usize);
    /// Get a window of `elements` number of lines centered around the target `line`
    ///
    /// Returns (list of lines, offset from start, index of target)
    fn get_search_lines_containing(
        &self,
        index: usize,
        elements: usize,
    ) -> (Vec<LogLine>, usize, usize);
    /// Count the total number of lines
    fn get_total_filtered_lines(&self) -> usize;
    /// Count the total number of search lines
    fn get_total_searched_lines(&self) -> usize;
}
pub struct InMemmoryAnalysisStore {
    log: RwLock<Vec<LogLine>>,
    search_query: RwLock<Option<String>>,
    search_log: RwLock<Vec<LogLine>>,
}

impl InMemmoryAnalysisStore {
    pub fn new() -> Self {
        Self {
            log: RwLock::new(Vec::new()),
            search_query: RwLock::new(None),
            search_log: RwLock::new(Vec::new()),
        }
    }
}

impl Default for InMemmoryAnalysisStore {
    fn default() -> Self {
        Self::new()
    }
}

impl AnalysisStore for InMemmoryAnalysisStore {
    fn add_lines(&self, lines: &[LogLine]) {
        let mut w = self.log.write();
        for line in lines {
            let index = w.len();

            let mut line = line.clone();
            line.index = index.to_string();

            w.push(line);
        }
    }

    fn add_search_lines(&self, lines: &[LogLine]) {
        let mut w = self.search_log.write();
        for line in lines {
            w.push(line.clone());
        }
    }

    fn add_search_query(&self, query: &str) {
        let mut w = self.search_query.write();
        *w = Some(query.to_string());
    }

    fn get_search_query(&self) -> Option<String> {
        let r = self.search_query.read();
        r.clone()
    }

    fn fetch_log(&self) -> RwLockReadGuard<RawRwLock, Vec<LogLine>> {
        self.log.read()
    }

    fn fetch_search(&self) -> RwLockReadGuard<RawRwLock, Vec<LogLine>> {
        self.search_log.read()
    }

    fn get_log_lines(&self, from: usize, to: usize) -> Vec<LogLine> {
        let log = self.log.read();
        log[from.min(log.len())..to.min(log.len())].to_vec()
    }

    fn get_search_lines(&self, from: usize, to: usize) -> Vec<LogLine> {
        let log = self.search_log.read();
        log[from.min(log.len())..to.min(log.len())].to_vec()
    }

    fn get_log_lines_containing(
        &self,
        index: usize,
        elements: usize,
    ) -> (Vec<LogLine>, usize, usize) {
        let log = self.log.read();
        InMemmoryAnalysisStore::find_rolling_window(&log, index, elements)
    }

    fn get_search_lines_containing(
        &self,
        index: usize,
        elements: usize,
    ) -> (Vec<LogLine>, usize, usize) {
        let search_log = self.search_log.read();
        InMemmoryAnalysisStore::find_rolling_window(&search_log, index, elements)
    }

    fn reset_log(&self) {
        let mut w = self.log.write();
        w.clear();
    }

    fn reset_search(&self) {
        let mut w = self.search_log.write();
        w.clear();
    }

    fn get_total_filtered_lines(&self) -> usize {
        self.log.read().len()
    }

    fn get_total_searched_lines(&self) -> usize {
        self.search_log.read().len()
    }
}

impl InMemmoryAnalysisStore {
    fn find_sorted_index(source: &[LogLine], index: usize) -> usize {
        match source.binary_search_by(|e| {
            e.index
                .parse::<usize>()
                .unwrap()
                .cmp(&index)
        }) {
            Ok(i) => i,
            Err(i) => i,
        }
    }

    /// Find a window of elements containing the target in the middle
    /// Returns (elements, offset, index)
    fn find_rolling_window(
        source: &[LogLine],
        index: usize,
        elements: usize,
    ) -> (Vec<LogLine>, usize, usize) {
        let closest = InMemmoryAnalysisStore::find_sorted_index(source, index);
        let from = if (elements / 2) < closest {
            closest - elements / 2
        } else {
            0
        };
        let to = (closest + elements / 2).min(source.len());

        let lines = source[from..to].to_vec();
        let index = InMemmoryAnalysisStore::find_sorted_index(&lines, index);
        (lines, from, index)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn log_line_with_index(index: usize) -> LogLine {
        LogLine {
            index: index.to_string(),
            ..Default::default()
        }
    }
}