micromegas-analytics 0.2.0

analytics module of micromegas
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::sync::Arc;

use anyhow::{Context, Result};
use bytes::Buf;
use bytes::BufMut;
use chrono::TimeDelta;
use datafusion::parquet::arrow::ArrowWriter;
use datafusion::parquet::basic::Compression;
use datafusion::parquet::file::properties::WriterProperties;
use datafusion::parquet::file::properties::WriterVersion;
use micromegas_ingestion::data_lake_connection::DataLakeConnection;
use serde::Deserialize;
use sqlx::types::chrono::{DateTime, FixedOffset};
use uuid::Uuid;

use crate::lakehouse::answer::Answer;
use crate::lakehouse::batch_update::materialize_partition_range;
use crate::lakehouse::partition_cache::LivePartitionProvider;
use crate::lakehouse::partition_cache::PartitionCache;
use crate::lakehouse::view_factory::ViewFactory;
use crate::lakehouse::write_partition::retire_partitions;
use crate::response_writer::ResponseWriter;
use crate::sql_arrow_bridge::rows_to_record_batch;
use crate::time::TimeRange;

#[derive(Clone)]
pub struct AnalyticsService {
    data_lake: Arc<DataLakeConnection>,
    view_factory: Arc<ViewFactory>,
}

#[derive(Debug, Deserialize)]
pub struct FindProcessRequest {
    #[serde(deserialize_with = "micromegas_transit::uuid_utils::uuid_from_string")]
    pub process_id: Uuid,
}

#[derive(Debug, Deserialize)]
pub struct FindStreamRequest {
    #[serde(deserialize_with = "micromegas_transit::uuid_utils::uuid_from_string")]
    pub stream_id: Uuid,
}

#[derive(Debug, Deserialize)]
pub struct QueryStreamsRequest {
    pub limit: i64,
    pub begin: Option<String>,
    pub end: Option<String>,
    pub tag_filter: Option<String>,
    #[serde(deserialize_with = "micromegas_transit::uuid_utils::opt_uuid_from_string")]
    pub process_id: Option<Uuid>,
}

#[derive(Debug, Deserialize)]
pub struct QueryBlocksRequest {
    #[serde(deserialize_with = "micromegas_transit::uuid_utils::uuid_from_string")]
    pub stream_id: Uuid,
}

#[derive(Debug, Deserialize)]
pub struct QueryViewRequest {
    pub view_set_name: String,
    pub view_instance_id: String,
    pub begin: Option<String>,
    pub end: Option<String>,
    pub sql: String,
}

#[derive(Debug, Deserialize)]
pub struct QueryRequest {
    pub sql: String,
    pub begin: Option<String>,
    pub end: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct MetarializePartitionsRequest {
    pub view_set_name: String,
    pub view_instance_id: String,
    pub begin: String,
    pub end: String,
    pub partition_delta_seconds: i64,
}

#[derive(Debug, Deserialize)]
pub struct MergePartitionsRequest {
    pub view_set_name: String,
    pub view_instance_id: String,
    pub begin: String,
    pub end: String,
    pub partition_delta_seconds: i64,
}

#[derive(Debug, Deserialize)]
pub struct RetirePartitionsRequest {
    pub view_set_name: String,
    pub view_instance_id: String,
    pub begin: String,
    pub end: String,
}

impl AnalyticsService {
    pub fn new(data_lake: Arc<DataLakeConnection>, view_factory: Arc<ViewFactory>) -> Self {
        Self {
            data_lake,
            view_factory,
        }
    }

    pub async fn find_process(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: FindProcessRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing FindProcessRequest")?;
        let rows = sqlx::query(
            "SELECT process_id,
                    exe,
                    username,
                    realname,
                    computer,
                    distro,
                    cpu_brand,
                    tsc_frequency,
                    start_time,
                    start_ticks,
                    insert_time,
                    parent_process_id,
                    properties
             FROM processes
             WHERE process_id = $1",
        )
        .bind(request.process_id)
        .fetch_all(&self.data_lake.db_pool)
        .await?;
        let record_batch =
            rows_to_record_batch(&rows).with_context(|| "converting rows to record batch")?;
        let answer = Answer::from_record_batch(record_batch);
        serialize_record_batches(&answer)
    }

    pub async fn find_stream(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: FindStreamRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing FindStreamRequest")?;
        let rows = sqlx::query(
            "SELECT stream_id, process_id, dependencies_metadata, objects_metadata, tags, properties
             FROM streams
             WHERE stream_id = $1",
        )
        .bind(request.stream_id)
        .fetch_all(&self.data_lake.db_pool)
        .await?;
        let record_batch =
            rows_to_record_batch(&rows).with_context(|| "converting rows to record batch")?;
        let answer = Answer::from_record_batch(record_batch);
        serialize_record_batches(&answer)
    }

    pub async fn query_streams(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryStreamsRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing QueryStreamsRequest")?;
        if (request.begin.is_none() || request.end.is_none()) && request.process_id.is_none() {
            anyhow::bail!("Time range or process_id have to be provided");
        }
        let mut conditions = vec![];
        let mut begin_time = None;
        if let Some(time_str) = &request.begin {
            begin_time = Some(
                DateTime::<FixedOffset>::parse_from_rfc3339(time_str)
                    .with_context(|| "parsing begin time range")?,
            );
            conditions.push(format!(
                "(insert_time >= {})",
                format_postgres_placeholder(conditions.len())
            ));
        }
        let mut end_time = None;
        if let Some(time_str) = &request.end {
            end_time = Some(
                DateTime::<FixedOffset>::parse_from_rfc3339(time_str)
                    .with_context(|| "parsing end time range")?,
            );
            conditions.push(format!(
                "(insert_time < {})",
                format_postgres_placeholder(conditions.len())
            ));
        }
        if request.process_id.is_some() {
            conditions.push(format!(
                "(process_id = {})",
                format_postgres_placeholder(conditions.len())
            ));
        }
        if request.tag_filter.is_some() {
            conditions.push(format!(
                "(array_position(tags, {}) is not NULL)",
                format_postgres_placeholder(conditions.len())
            ));
        }
        let limit_placeholder = format_postgres_placeholder(conditions.len());
        let joined_conditions = conditions.join(" AND ");
        let sql = format!(
            "SELECT stream_id,
                    process_id,
                    tags,
                    properties
             FROM streams
             WHERE {joined_conditions}
             ORDER BY insert_time
             LIMIT {limit_placeholder}"
        );

        let mut query = sqlx::query(&sql);
        if let Some(begin) = begin_time {
            query = query.bind(begin);
        }
        if let Some(end) = end_time {
            query = query.bind(end);
        }
        if request.process_id.is_some() {
            query = query.bind(request.process_id);
        }
        if request.tag_filter.is_some() {
            query = query.bind(request.tag_filter);
        }
        query = query.bind(request.limit);
        let rows = query.fetch_all(&self.data_lake.db_pool).await?;
        let record_batch =
            rows_to_record_batch(&rows).with_context(|| "converting rows to record batch")?;
        let answer = Answer::from_record_batch(record_batch);
        serialize_record_batches(&answer)
    }

    pub async fn query_blocks(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryBlocksRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing QueryBlocksRequest")?;
        let sql = "SELECT block_id,
                    stream_id,
                    process_id,
                    begin_time,
                    begin_ticks,
                    end_time,
                    end_ticks,
                    nb_objects,
                    object_offset,
                    payload_size
             FROM blocks
             WHERE stream_id = $1
             ORDER BY begin_time;";
        let rows = sqlx::query(sql)
            .bind(request.stream_id)
            .fetch_all(&self.data_lake.db_pool)
            .await?;
        let record_batch =
            rows_to_record_batch(&rows).with_context(|| "converting rows to record batch")?;
        let answer = Answer::from_record_batch(record_batch);
        serialize_record_batches(&answer)
    }

    pub async fn query_view(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryViewRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing QueryViewRequest")?;
        let time_range = if request.begin.is_some() || request.end.is_some() {
            if request.begin.is_none() || request.end.is_none() {
                anyhow::bail!("Either specify both begin & end or specify none");
            }
            let begin = DateTime::<FixedOffset>::parse_from_rfc3339(&request.begin.unwrap())
                .with_context(|| "parsing begin time range")?;
            let end = DateTime::<FixedOffset>::parse_from_rfc3339(&request.end.unwrap())
                .with_context(|| "parsing end time range")?;
            Some(TimeRange::new(begin.into(), end.into()))
        } else {
            None
        };
        let view = self
            .view_factory
            .make_view(&request.view_set_name, &request.view_instance_id)
            .with_context(|| "making view")?;
        let answer = crate::lakehouse::query::query_single_view(
            self.data_lake.clone(),
            Arc::new(LivePartitionProvider::new(self.data_lake.db_pool.clone())),
            time_range,
            &request.sql,
            view,
        )
        .await
        .with_context(|| "lakehouse::query::query")?;
        serialize_record_batches(&answer)
    }

    pub async fn query(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing QueryRequest")?;
        let time_range = if request.begin.is_some() || request.end.is_some() {
            if request.begin.is_none() || request.end.is_none() {
                anyhow::bail!("Either specify both begin & end or specify none");
            }
            let begin = DateTime::<FixedOffset>::parse_from_rfc3339(&request.begin.unwrap())
                .with_context(|| "parsing begin time range")?;
            let end = DateTime::<FixedOffset>::parse_from_rfc3339(&request.end.unwrap())
                .with_context(|| "parsing end time range")?;
            Some(TimeRange::new(begin.into(), end.into()))
        } else {
            None
        };
        let answer = crate::lakehouse::query::query(
            self.data_lake.clone(),
            Arc::new(LivePartitionProvider::new(self.data_lake.db_pool.clone())),
            time_range,
            &request.sql,
            self.view_factory.clone(),
        )
        .await
        .with_context(|| "lakehouse::query::query")?;
        serialize_record_batches(&answer)
    }

    pub async fn query_partitions(&self) -> Result<bytes::Bytes> {
        // if partitions are merged on a daily basis, there should not be that many
        let rows = sqlx::query(
            "SELECT *
             FROM lakehouse_partitions
             WHERE file_metadata IS NOT NULL
             ;",
        )
        .fetch_all(&self.data_lake.db_pool)
        .await?;
        let record_batch =
            rows_to_record_batch(&rows).with_context(|| "converting rows to record batch")?;
        serialize_record_batches(&Answer::from_record_batch(record_batch))
    }

    pub async fn materialize_partition_range(
        &self,
        body: bytes::Bytes,
        writer: Arc<ResponseWriter>,
    ) -> Result<()> {
        let request: MetarializePartitionsRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing MetarializePartitionsRequest")?;
        let begin = DateTime::<FixedOffset>::parse_from_rfc3339(&request.begin)
            .with_context(|| "parsing begin time range")?;
        let end = DateTime::<FixedOffset>::parse_from_rfc3339(&request.end)
            .with_context(|| "parsing end time range")?;
        let view = self
            .view_factory
            .make_view(&request.view_set_name, &request.view_instance_id)
            .with_context(|| "making view")?;
        let delta = TimeDelta::try_seconds(request.partition_delta_seconds)
            .with_context(|| "making time delta")?;
        let existing_partitions = Arc::new(
            PartitionCache::fetch_overlapping_insert_range(
                &self.data_lake.db_pool,
                begin.into(),
                end.into(),
            )
            .await?,
        );
        materialize_partition_range(
            existing_partitions,
            self.data_lake.clone(),
            view,
            begin.into(),
            end.into(),
            delta,
            writer,
        )
        .await?;
        Ok(())
    }

    pub async fn retire_partitions(
        &self,
        body: bytes::Bytes,
        writer: Arc<ResponseWriter>,
    ) -> Result<()> {
        let request: RetirePartitionsRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing RetirePartitionsRequest")?;
        let begin = DateTime::<FixedOffset>::parse_from_rfc3339(&request.begin)
            .with_context(|| "parsing begin time range")?;
        let end = DateTime::<FixedOffset>::parse_from_rfc3339(&request.end)
            .with_context(|| "parsing end time range")?;
        let mut tr = self.data_lake.db_pool.begin().await?;
        retire_partitions(
            &mut tr,
            &request.view_set_name,
            &request.view_instance_id,
            begin.into(),
            end.into(),
            writer,
        )
        .await?;
        tr.commit().await.with_context(|| "commit")?;
        Ok(())
    }
}

fn format_postgres_placeholder(index: usize) -> String {
    format!("${}", index + 1)
}

fn serialize_record_batches(answer: &Answer) -> Result<bytes::Bytes> {
    let mut buffer_writer = bytes::BytesMut::with_capacity(1024).writer();
    let props = WriterProperties::builder()
        .set_writer_version(WriterVersion::PARQUET_2_0)
        .set_compression(Compression::LZ4_RAW)
        .build();
    let mut arrow_writer =
        ArrowWriter::try_new(&mut buffer_writer, answer.schema.clone(), Some(props))?;
    for batch in &answer.record_batches {
        arrow_writer.write(batch)?;
    }
    arrow_writer.close()?;
    Ok(buffer_writer.into_inner().into())
}