1use std::sync::Arc;
2
3use anyhow::{Context, Result};
4use chrono::DateTime;
5use datafusion::arrow::array::ArrayBuilder;
6use datafusion::arrow::array::PrimitiveBuilder;
7use datafusion::arrow::array::StringDictionaryBuilder;
8use datafusion::arrow::datatypes::DataType;
9use datafusion::arrow::datatypes::Field;
10use datafusion::arrow::datatypes::Int32Type;
11use datafusion::arrow::datatypes::Int64Type;
12use datafusion::arrow::datatypes::Schema;
13use datafusion::arrow::datatypes::TimeUnit;
14use datafusion::arrow::datatypes::TimestampNanosecondType;
15use datafusion::arrow::datatypes::UInt32Type;
16use datafusion::arrow::record_batch::RecordBatch;
17
18use crate::properties::property_set::PropertySet;
19use crate::properties::property_set_jsonb_dictionary_builder::PropertySetJsonbDictionaryBuilder;
20use crate::time::TimeRange;
21
22#[derive(Debug, Clone)]
28pub struct AsyncEventRecord<'a> {
29 pub stream_id: Arc<String>,
30 pub block_id: Arc<String>,
31 pub time: i64,
32 pub event_type: &'static str,
33 pub span_id: i64,
34 pub parent_span_id: i64,
35 pub depth: u32,
36 pub hash: u32,
37 pub name: &'a str,
38 pub filename: &'a str,
39 pub target: &'a str,
40 pub line: u32,
41 pub properties: PropertySet<'a>,
43}
44
45pub fn async_events_table_schema() -> Schema {
48 Schema::new(vec![
49 Field::new(
50 "stream_id",
51 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
52 false,
53 ),
54 Field::new(
55 "block_id",
56 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
57 false,
58 ),
59 Field::new(
60 "time",
61 DataType::Timestamp(TimeUnit::Nanosecond, Some("+00:00".into())),
62 false,
63 ),
64 Field::new(
65 "event_type",
66 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
67 false,
68 ),
69 Field::new("span_id", DataType::Int64, false),
70 Field::new("parent_span_id", DataType::Int64, false),
71 Field::new("depth", DataType::UInt32, false),
72 Field::new("hash", DataType::UInt32, false),
73 Field::new(
74 "name",
75 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
76 false,
77 ),
78 Field::new(
79 "filename",
80 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
81 false,
82 ),
83 Field::new(
84 "target",
85 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Utf8)),
86 false,
87 ),
88 Field::new("line", DataType::UInt32, false),
89 Field::new(
90 "properties",
91 DataType::Dictionary(Box::new(DataType::Int32), Box::new(DataType::Binary)),
92 false,
93 ),
94 ])
95}
96
97pub struct AsyncEventRecordBuilder {
99 stream_ids: StringDictionaryBuilder<Int32Type>,
100 block_ids: StringDictionaryBuilder<Int32Type>,
101 times: PrimitiveBuilder<TimestampNanosecondType>,
102 event_types: StringDictionaryBuilder<Int32Type>,
103 span_ids: PrimitiveBuilder<Int64Type>,
104 parent_span_ids: PrimitiveBuilder<Int64Type>,
105 depths: PrimitiveBuilder<UInt32Type>,
106 hashes: PrimitiveBuilder<UInt32Type>,
107 names: StringDictionaryBuilder<Int32Type>,
108 filenames: StringDictionaryBuilder<Int32Type>,
109 targets: StringDictionaryBuilder<Int32Type>,
110 lines: PrimitiveBuilder<UInt32Type>,
111 properties: PropertySetJsonbDictionaryBuilder,
112}
113
114impl AsyncEventRecordBuilder {
115 pub fn with_capacity(capacity: usize) -> Self {
116 Self {
117 stream_ids: StringDictionaryBuilder::new(),
118 block_ids: StringDictionaryBuilder::new(),
119 times: PrimitiveBuilder::with_capacity(capacity),
120 event_types: StringDictionaryBuilder::new(),
121 span_ids: PrimitiveBuilder::with_capacity(capacity),
122 parent_span_ids: PrimitiveBuilder::with_capacity(capacity),
123 depths: PrimitiveBuilder::with_capacity(capacity),
124 hashes: PrimitiveBuilder::with_capacity(capacity),
125 names: StringDictionaryBuilder::new(),
126 filenames: StringDictionaryBuilder::new(),
127 targets: StringDictionaryBuilder::new(),
128 lines: PrimitiveBuilder::with_capacity(capacity),
129 properties: PropertySetJsonbDictionaryBuilder::new(capacity),
130 }
131 }
132
133 pub fn get_time_range(&self) -> Option<TimeRange> {
134 if self.is_empty() {
135 return None;
136 }
137 let slice = self.times.values_slice();
139 Some(TimeRange::new(
140 DateTime::from_timestamp_nanos(slice[0]),
141 DateTime::from_timestamp_nanos(slice[slice.len() - 1]),
142 ))
143 }
144
145 pub fn len(&self) -> i64 {
146 self.times.len() as i64
147 }
148
149 pub fn is_empty(&self) -> bool {
150 self.times.len() == 0
151 }
152
153 pub fn append(&mut self, record: &AsyncEventRecord<'_>) -> Result<()> {
154 self.stream_ids.append(&*record.stream_id)?;
155 self.block_ids.append(&*record.block_id)?;
156 self.times.append_value(record.time);
157 self.event_types.append(record.event_type)?;
158 self.span_ids.append_value(record.span_id);
159 self.parent_span_ids.append_value(record.parent_span_id);
160 self.depths.append_value(record.depth);
161 self.hashes.append_value(record.hash);
162 self.names.append(record.name)?;
163 self.filenames.append(record.filename)?;
164 self.targets.append(record.target)?;
165 self.lines.append_value(record.line);
166 self.properties.append_property_set(&record.properties)?;
167 Ok(())
168 }
169
170 pub fn finish(mut self) -> Result<RecordBatch> {
171 RecordBatch::try_new(
172 Arc::new(async_events_table_schema()),
173 vec![
174 Arc::new(self.stream_ids.finish()),
175 Arc::new(self.block_ids.finish()),
176 Arc::new(self.times.finish().with_timezone_utc()),
177 Arc::new(self.event_types.finish()),
178 Arc::new(self.span_ids.finish()),
179 Arc::new(self.parent_span_ids.finish()),
180 Arc::new(self.depths.finish()),
181 Arc::new(self.hashes.finish()),
182 Arc::new(self.names.finish()),
183 Arc::new(self.filenames.finish()),
184 Arc::new(self.targets.finish()),
185 Arc::new(self.lines.finish()),
186 Arc::new(self.properties.finish()?),
187 ],
188 )
189 .with_context(|| "building record batch")
190 }
191}