use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use bytes::BytesMut;
use jiff::Timestamp;
use tokio::sync::mpsc;
use crate::errors::TaleError;
use crate::logpatterns::{PrettyPrintable, Printable, SourcedLine};
#[derive(Debug, Clone)]
pub struct BatchedLine {
pub rendered: BytesMut,
pub timestamp: Option<Timestamp>,
pub received_at: Instant,
}
impl BatchedLine {
pub fn new(line: String, source_file: PathBuf, line_number: usize) -> Self {
let mut rendered = BytesMut::with_capacity(2048);
let timestamp = match serde_json::from_str::<Printable<'_>>(&line) {
Ok(parsed) => {
let sourced = SourcedLine::new(parsed, source_file, line_number);
let ts = sourced.timestamp().copied();
sourced.write(&mut rendered);
ts
}
Err(_) => {
let sourced = SourcedLine::new(Printable::Text(line), source_file, line_number);
sourced.write(&mut rendered);
None
}
};
rendered.extend_from_slice(b"\n");
Self {
rendered,
timestamp,
received_at: Instant::now(),
}
}
pub fn sort_key(&self) -> SortKey {
match &self.timestamp {
Some(ts) => SortKey::Timestamp(*ts),
None => SortKey::ReceivedAt(self.received_at),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SortKey {
Timestamp(Timestamp),
ReceivedAt(Instant),
}
impl PartialOrd for SortKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SortKey {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(SortKey::Timestamp(a), SortKey::Timestamp(b)) => a.cmp(b),
(SortKey::ReceivedAt(a), SortKey::ReceivedAt(b)) => a.cmp(b),
(SortKey::Timestamp(_), SortKey::ReceivedAt(_)) => Ordering::Less,
(SortKey::ReceivedAt(_), SortKey::Timestamp(_)) => Ordering::Greater,
}
}
}
#[derive(Debug)]
struct MinHeapLine(BatchedLine);
impl PartialEq for MinHeapLine {
fn eq(&self, other: &Self) -> bool {
self.0.sort_key() == other.0.sort_key()
}
}
impl Eq for MinHeapLine {}
impl PartialOrd for MinHeapLine {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for MinHeapLine {
fn cmp(&self, other: &Self) -> Ordering {
other.0.sort_key().cmp(&self.0.sort_key())
}
}
#[derive(Debug, Clone)]
pub struct BatchConfig {
pub batch_window: Duration,
pub max_batch_size: usize,
}
impl Default for BatchConfig {
fn default() -> Self {
Self {
batch_window: Duration::from_millis(250),
max_batch_size: 200,
}
}
}
pub struct BatchProcessor {
config: BatchConfig,
pending_lines: BinaryHeap<MinHeapLine>,
receiver: Option<mpsc::UnboundedReceiver<BatchedLine>>,
sender: Option<mpsc::UnboundedSender<Vec<BatchedLine>>>,
window_start: Option<Instant>,
}
impl BatchProcessor {
pub fn new(config: BatchConfig) -> Self {
Self {
config,
pending_lines: BinaryHeap::new(),
receiver: None,
sender: None,
window_start: None,
}
}
pub async fn start(
&mut self,
) -> Result<
(
mpsc::UnboundedSender<BatchedLine>,
mpsc::UnboundedReceiver<Vec<BatchedLine>>,
),
TaleError,
> {
let (line_sender, line_receiver) = mpsc::unbounded_channel();
let (batch_sender, batch_receiver) = mpsc::unbounded_channel();
let mut processor = BatchProcessor {
config: self.config.clone(),
pending_lines: BinaryHeap::new(),
receiver: Some(line_receiver),
sender: Some(batch_sender),
window_start: None,
};
tokio::spawn(async move {
if let Err(e) = processor.process_loop().await {
eprintln!("Batch processor error: {e}");
}
});
Ok((line_sender, batch_receiver))
}
async fn process_loop(&mut self) -> Result<(), TaleError> {
let Some(mut line_receiver) = self.receiver.take() else {
return Err(TaleError::LineReceiver);
};
let Some(batch_sender) = self.sender.take() else {
return Err(TaleError::BatchSender);
};
let batch_timeout = tokio::time::interval(self.config.batch_window);
tokio::pin!(batch_timeout);
loop {
tokio::select! {
line_opt = line_receiver.recv() => {
match line_opt {
Some(line) => {
self.pending_lines.push(MinHeapLine(line));
if self.window_start.is_none() {
self.window_start = Some(Instant::now());
}
if self.pending_lines.len() >= self.config.max_batch_size {
self.emit_batch(&batch_sender)?;
}
}
None => {
self.emit_batch(&batch_sender)?;
break;
}
}
}
_ = batch_timeout.tick() => {
if !self.pending_lines.is_empty() {
self.emit_batch(&batch_sender)?;
}
}
}
}
Ok(())
}
fn emit_batch(&mut self, batch_sender: &mpsc::UnboundedSender<Vec<BatchedLine>>) -> Result<(), TaleError> {
if self.pending_lines.is_empty() {
return Ok(());
}
let mut sorted_lines = Vec::with_capacity(self.pending_lines.len());
while let Some(min_line) = self.pending_lines.pop() {
sorted_lines.push(min_line.0);
}
self.window_start = None;
batch_sender
.send(sorted_lines)
.map_err(|_| TaleError::BatchedLineVecSender)
}
}
pub fn batched_with_config(config: BatchConfig) -> BatchProcessor {
BatchProcessor::new(config)
}