Skip to main content

swarm_engine_core/learn/record/
stream.rs

1//! RecordStream - Record のコレクション操作
2
3use super::{ActionRecord, DependencyGraphRecord, LlmCallRecord, Record};
4
5/// Record のストリームを操作するためのヘルパー
6pub struct RecordStream<'a> {
7    records: &'a [Record],
8}
9
10impl<'a> RecordStream<'a> {
11    pub fn new(records: &'a [Record]) -> Self {
12        Self { records }
13    }
14
15    /// Action Records のみ抽出
16    pub fn actions(&self) -> impl Iterator<Item = &ActionRecord> {
17        self.records.iter().filter_map(Record::as_action)
18    }
19
20    /// Llm Records のみ抽出
21    pub fn llm_calls(&self) -> impl Iterator<Item = &LlmCallRecord> {
22        self.records.iter().filter_map(Record::as_llm)
23    }
24
25    /// DependencyGraph Records のみ抽出
26    pub fn dependency_graphs(&self) -> impl Iterator<Item = &DependencyGraphRecord> {
27        self.records.iter().filter_map(Record::as_dependency_graph)
28    }
29
30    /// Worker ID でフィルタ
31    pub fn by_worker(&self, worker_id: usize) -> impl Iterator<Item = &Record> {
32        self.records
33            .iter()
34            .filter(move |r| r.worker_id() == Some(worker_id))
35    }
36
37    /// Worker ID ごとにグルーピング
38    pub fn group_by_worker(&self) -> std::collections::HashMap<usize, Vec<&Record>> {
39        let mut groups = std::collections::HashMap::new();
40        for record in self.records {
41            if let Some(worker_id) = record.worker_id() {
42                groups
43                    .entry(worker_id)
44                    .or_insert_with(Vec::new)
45                    .push(record);
46            }
47        }
48        groups
49    }
50
51    /// タイムスタンプでソートした Iterator
52    pub fn sorted_by_time(&self) -> Vec<&Record> {
53        let mut sorted: Vec<_> = self.records.iter().collect();
54        sorted.sort_by_key(|r| r.timestamp_ms());
55        sorted
56    }
57
58    /// Record 数
59    pub fn len(&self) -> usize {
60        self.records.len()
61    }
62
63    /// 空かどうか
64    pub fn is_empty(&self) -> bool {
65        self.records.is_empty()
66    }
67}