use std::collections::VecDeque;
use serde_json::{Value, json};
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
Session(String),
Text(String),
Answer(String),
Observed(Value),
Failure(String),
}
pub const HEAD_BYTES: usize = 64 * 1024;
pub const TAIL_BYTES: usize = 192 * 1024;
pub const OUTPUT_BYTES: usize = 16 * 1024;
pub const FIELD_BYTES: usize = 400;
pub const ERROR_BYTES: usize = 4 * 1024;
pub const EVENTS_KEPT: usize = 256;
pub const EVENTS_PER_ANSWER: usize = 32;
pub fn head(text: &str, limit: usize) -> &str {
if text.len() <= limit {
return text;
}
let mut end = limit;
while !text.is_char_boundary(end) {
end -= 1;
}
&text[..end]
}
pub fn tail(text: &str, limit: usize) -> &str {
if text.len() <= limit {
return text;
}
let mut start = text.len() - limit;
while !text.is_char_boundary(start) {
start += 1;
}
&text[start..]
}
pub fn observed(kind: &str, fields: &[(&str, Value)]) -> Event {
let mut entry = serde_json::Map::new();
entry.insert("kind".into(), json!(kind));
for (name, value) in fields {
let value = match value {
Value::String(text) => json!(head(text, FIELD_BYTES)),
other => other.clone(),
};
entry.insert((*name).to_string(), value);
}
Event::Observed(Value::Object(entry))
}
#[derive(Default)]
pub struct Text {
head: String,
tail: VecDeque<String>,
tail_bytes: usize,
total: usize,
}
impl Text {
pub fn push(&mut self, text: &str) {
self.total += text.len();
if self.tail.is_empty() && self.head.len() + text.len() <= HEAD_BYTES {
self.head.push_str(text);
return;
}
let rest = if self.tail.is_empty() {
let room = head(text, HEAD_BYTES - self.head.len());
self.head.push_str(room);
&text[room.len()..]
} else {
text
};
self.tail.push_back(rest.to_string());
self.tail_bytes += rest.len();
while self.tail_bytes > TAIL_BYTES {
let front = self.tail.front_mut().expect("bytes over the bound");
let excess = self.tail_bytes - TAIL_BYTES;
if front.len() <= excess {
self.tail_bytes -= front.len();
self.tail.pop_front();
} else {
let kept = tail(front, front.len() - excess).to_string();
self.tail_bytes -= front.len() - kept.len();
*front = kept;
}
}
}
pub fn total(&self) -> usize {
self.total
}
pub fn since(&self, from: usize, limit: usize) -> (String, usize) {
let from = from.min(self.total);
let tail_start = self.total - self.tail_bytes;
let mut held = String::new();
if from < self.head.len() {
held.push_str(&self.head[from..]);
}
let mut at = tail_start;
for piece in &self.tail {
let end = at + piece.len();
if end > from {
held.push_str(&piece[from.saturating_sub(at)..]);
}
at = end;
}
let shown = tail(&held, limit).to_string();
let skipped = self.total - from - shown.len();
(shown, skipped)
}
}
#[derive(Default)]
pub struct Log {
entries: VecDeque<Value>,
total: usize,
}
impl Log {
pub fn push(&mut self, entry: Value) {
self.entries.push_back(entry);
self.total += 1;
if self.entries.len() > EVENTS_KEPT {
self.entries.pop_front();
}
}
pub fn total(&self) -> usize {
self.total
}
pub fn since(&self, from: usize, limit: usize) -> (Vec<Value>, usize) {
let first = self.total - self.entries.len();
let held: Vec<&Value> = self
.entries
.iter()
.enumerate()
.filter(|(index, _)| first + index >= from)
.map(|(_, entry)| entry)
.collect();
let shown: Vec<Value> = held[held.len().saturating_sub(limit)..]
.iter()
.map(|entry| (*entry).clone())
.collect();
let skipped = self.total - from.min(self.total) - shown.len();
(shown, skipped)
}
}
#[derive(Default)]
pub struct Tail(String);
impl Tail {
pub fn push(&mut self, text: &str) {
self.0.push_str(text);
if self.0.len() > 2 * ERROR_BYTES {
let keep = tail(&self.0, ERROR_BYTES).len();
self.0.drain(..self.0.len() - keep);
}
}
pub fn text(&self) -> &str {
tail(&self.0, ERROR_BYTES)
}
pub fn replace(&mut self, text: &str) {
self.0 = text.to_string();
}
}
#[cfg(test)]
mod tests;