runnerd_core/api/jobs/log/
search.rs1use std::collections::VecDeque;
11use std::ops::Range;
12
13use serde::{Deserialize, Serialize};
14
15use super::{LINE_BYTES_MAX, LineRead, RUNS_MAX, Run, StoredLine, StyledLine};
16
17pub const SEARCH_QUERY_MAX: usize = 200;
19
20pub(crate) const MATCHES_DEFAULT: u32 = 50;
22
23pub(crate) const MATCHES_MAX: u32 = 200;
25
26pub(crate) const CONTEXT_MAX: u32 = 5;
28
29pub(crate) const SCAN_LINES: u64 = 50_000;
31
32pub(crate) const SCAN_BYTES: u64 = 4 << 20;
34
35const BATCH: u64 = 500;
37
38#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
40pub struct LogSearchQuery {
41 pub q: String,
44 pub from: Option<u64>,
46 pub limit: Option<u32>,
48 pub context: Option<u32>,
50 #[serde(default)]
52 pub follow: bool,
53}
54
55#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
57pub struct LogSearch {
58 pub matches: Vec<u64>,
60 pub lines: Vec<StyledLine>,
63 #[serde(default, skip_serializing_if = "Option::is_none")]
66 pub next: Option<u64>,
67}
68
69impl LogSearch {
70 pub fn found_words(&self, query: &str) -> String {
73 let unread = if self.next.is_some() {
74 " in the lines read"
75 } else {
76 ""
77 };
78 match self.matches.len() {
79 0 => format!("no line's message holds “{query}”{unread}"),
80 1 => format!("1 line holds “{query}”{unread}"),
81 count => format!("{count} lines hold “{query}”{unread}"),
82 }
83 }
84}
85
86#[derive(Debug, Clone)]
88pub struct LogNeedle {
89 lower: String,
91}
92
93impl LogNeedle {
94 pub fn new(query: &str) -> Result<LogNeedle, String> {
96 if query.is_empty() {
97 return Err("q is empty: say what to find".to_owned());
98 }
99 if query.len() > SEARCH_QUERY_MAX {
100 return Err(format!("q is longer than {SEARCH_QUERY_MAX} bytes"));
101 }
102 Ok(LogNeedle {
103 lower: query.to_ascii_lowercase(),
104 })
105 }
106
107 pub fn mark(&self, line: &mut StyledLine) -> bool {
110 let text = line.text();
111 let lower = text.to_ascii_lowercase();
113 let mut found: Vec<Range<usize>> = Vec::new();
114 for (at, matched) in lower.match_indices(self.lower.as_str()) {
115 match found.last_mut() {
116 Some(last) if last.end == at => last.end = at + matched.len(),
117 _ => found.push(at..at + matched.len()),
118 }
119 }
120 if found.is_empty() {
121 return false;
122 }
123 let mut runs: Vec<Run> = Vec::new();
124 let mut found = found.into_iter().peekable();
125 let mut offset = 0;
126 'runs: for run in &line.runs {
127 let end = offset + run.text.len();
128 let mut at = offset;
129 while at < end {
130 while found.next_if(|range| range.end <= at).is_some() {}
131 let (to, mark) = match found.peek() {
132 Some(range) if range.start <= at => (range.end.min(end), true),
133 Some(range) => (range.start.min(end), false),
134 None => (end, false),
135 };
136 let full = runs.len() >= RUNS_MAX;
137 match runs.last_mut() {
138 Some(last)
139 if (last.color, last.bold, last.mark) == (run.color, run.bold, mark) =>
140 {
141 last.text.push_str(&text[at..to]);
142 }
143 _ if full => {
144 line.omitted += (text.len() - at) as u64;
145 break 'runs;
146 }
147 _ => runs.push(Run {
148 text: text[at..to].to_owned(),
149 color: run.color,
150 bold: run.bold,
151 mark,
152 }),
153 }
154 at = to;
155 }
156 offset = end;
157 }
158 line.runs = runs;
159 true
160 }
161}
162
163#[derive(Debug)]
165enum Stage {
166 Scanning { next: u64 },
168 Trailing {
171 from: u64,
172 left: u32,
173 resume: Option<u64>,
174 },
175 Done { resume: Option<u64> },
177}
178
179#[derive(Debug)]
182pub struct Search {
183 needle: LogNeedle,
184 limit: usize,
185 context: u32,
186 found: LogSearch,
187 before: VecDeque<StyledLine>,
189 after: u32,
191 scanned_lines: u64,
192 scanned_bytes: u64,
193 stage: Stage,
194}
195
196impl Search {
197 pub fn new(query: &LogSearchQuery) -> Result<Search, String> {
199 if query.follow {
200 return Err("a search does not follow a log: search again as it grows".to_owned());
201 }
202 Ok(Search {
203 needle: LogNeedle::new(&query.q)?,
204 limit: usize::try_from(query.limit.unwrap_or(MATCHES_DEFAULT).clamp(1, MATCHES_MAX))
205 .unwrap_or(usize::MAX),
206 context: query.context.unwrap_or(0).min(CONTEXT_MAX),
207 found: LogSearch::default(),
208 before: VecDeque::new(),
209 after: 0,
210 scanned_lines: 0,
211 scanned_bytes: 0,
212 stage: Stage::Scanning {
213 next: query.from.unwrap_or(0),
214 },
215 })
216 }
217
218 pub fn wants(&self) -> Option<LineRead> {
221 match self.stage {
222 Stage::Scanning { next } => Some(LineRead {
223 from: next,
224 limit: BATCH.min(SCAN_LINES - self.scanned_lines),
225 prefix: LINE_BYTES_MAX,
226 bytes: SCAN_BYTES - self.scanned_bytes,
227 }),
228 Stage::Trailing { from, left, .. } => Some(LineRead {
229 from,
230 limit: u64::from(left),
231 prefix: LINE_BYTES_MAX,
232 bytes: u64::MAX,
233 }),
234 Stage::Done { .. } => None,
235 }
236 }
237
238 pub fn read(&mut self, lines: Vec<StoredLine>) {
240 let asked = self.wants();
241 let text: u64 = lines.iter().map(|line| line.text.len() as u64).sum();
242 let short =
243 asked.is_some_and(|read| (lines.len() as u64) < read.limit && text < read.bytes);
244 let mut lines = lines.into_iter();
245 if let Stage::Scanning { .. } = self.stage {
246 for stored in lines.by_ref() {
247 let number = stored.line;
248 self.scanned_lines += 1;
249 self.scanned_bytes += stored.text.len() as u64;
250 let mut line = stored.styled();
251 let matched = self.needle.mark(&mut line);
252 if matched {
253 self.found.matches.push(number);
254 self.found.lines.extend(self.before.drain(..));
255 self.found.lines.push(line);
256 self.after = self.context;
257 } else if self.after > 0 {
258 self.found.lines.push(line);
259 self.after -= 1;
260 } else if self.context > 0 {
261 self.before.push_back(line);
262 if self.before.len() > self.context as usize {
263 self.before.pop_front();
264 }
265 }
266 let full = matched && self.found.matches.len() >= self.limit;
267 if full || self.scanned_lines >= SCAN_LINES || self.scanned_bytes >= SCAN_BYTES {
268 self.stop(Some(number + 1));
269 break;
270 }
271 self.stage = Stage::Scanning { next: number + 1 };
272 }
273 if let Stage::Scanning { .. } = self.stage
274 && short
275 {
276 self.stop(None);
277 }
278 }
279 if let Stage::Trailing { resume, .. } = self.stage {
281 for stored in lines.by_ref().take(self.after as usize) {
282 let number = stored.line;
283 self.found.lines.push(stored.styled());
284 self.after -= 1;
285 self.stage = Stage::Trailing {
286 from: number + 1,
287 left: self.after,
288 resume,
289 };
290 }
291 if self.after == 0 || short {
292 self.stage = Stage::Done { resume };
293 }
294 }
295 }
296
297 fn stop(&mut self, resume: Option<u64>) {
298 self.stage = match (self.after, resume) {
299 (0, _) | (_, None) => Stage::Done { resume },
300 (left, Some(from)) => Stage::Trailing { from, left, resume },
301 };
302 }
303
304 pub fn finish(self) -> LogSearch {
306 let resume = match self.stage {
307 Stage::Scanning { next } => Some(next),
308 Stage::Trailing { resume, .. } | Stage::Done { resume } => resume,
309 };
310 LogSearch {
311 next: resume,
312 ..self.found
313 }
314 }
315}