micromegas-analytics 0.1.6

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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
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::create_or_update_partitions;
use crate::lakehouse::merge::merge_partitions;
use crate::lakehouse::partition::retire_partitions;
use crate::lakehouse::view_factory::ViewFactory;
use crate::response_writer::ResponseWriter;
use crate::sql_arrow_bridge::rows_to_record_batch;

#[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 QueryProcessesRequest {
    pub limit: i64,
    pub begin: String,
    pub end: String,
}

#[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 QuerySpansRequest {
    pub limit: i64,
    pub begin: String,
    pub end: String,
    #[serde(deserialize_with = "micromegas_transit::uuid_utils::uuid_from_string")]
    pub stream_id: Uuid,
}

#[derive(Debug, Deserialize)]
pub struct QueryThreadEventsRequest {
    pub limit: i64,
    pub begin: String,
    pub end: String,
    #[serde(deserialize_with = "micromegas_transit::uuid_utils::uuid_from_string")]
    pub stream_id: Uuid,
}

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

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

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

#[derive(Debug, Deserialize)]
pub struct CreateOrUpdatePartitionsRequest {
    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 mut connection = self.data_lake.db_pool.acquire().await?;
        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(&mut *connection)
        .await?;
        drop(connection);
        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_processes(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryProcessesRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing QueryProcessesRequest")?;

        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 connection = self.data_lake.db_pool.acquire().await?;
        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 start_time >= $1
             AND start_time < $2
             ORDER BY start_time
             LIMIT $3",
        )
        .bind(begin)
        .bind(end)
        .bind(request.limit)
        .fetch_all(&mut *connection)
        .await?;
        drop(connection);
        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 mut connection = self.data_lake.db_pool.acquire().await?;
        let rows = query.fetch_all(&mut *connection).await?;
        drop(connection);
        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 mut connection = self.data_lake.db_pool.acquire().await?;
        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(&mut *connection)
            .await?;
        drop(connection);
        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_spans(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QuerySpansRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing QuerySpansRequest")?;
        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 record_batch = crate::query_spans::query_spans(
            &self.data_lake,
            request.limit,
            request.stream_id,
            begin.into(),
            end.into(),
        )
        .await
        .with_context(|| "query_spans")?;
        let answer = Answer::from_record_batch(record_batch);
        serialize_record_batches(&answer)
    }

    pub async fn query_thread_events(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryThreadEventsRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing QueryThreadEventsRequest")?;
        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 record_batch = crate::query_thread_events::query_thread_events(
            &self.data_lake,
            request.limit,
            request.stream_id,
            begin.into(),
            end.into(),
        )
        .await
        .with_context(|| "query_thread_events")?;
        let answer = Answer::from_record_batch(record_batch);
        serialize_record_batches(&answer)
    }

    pub async fn query_log_entries(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryLogEntriesRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing QueryLogEntriesRequest")?;
        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 answer = if let Some(stream_id) = request.stream_id {
            if request.limit.is_none() {
                anyhow::bail!("limit is required for stream-specific queries");
            }
            let record_batch = crate::query_log_entries::query_log_entries(
                &self.data_lake,
                stream_id,
                begin.into(),
                end.into(),
                request.limit.unwrap(),
            )
            .await
            .with_context(|| "query_log_entries")?;
            Answer::from_record_batch(record_batch)
        } else {
            if request.sql.is_none() {
                anyhow::bail!("sql is required for lakehouse queries");
            }
            if request.limit.is_some() {
                anyhow::bail!("limit must be included in the sql statement for lakehouse queries");
            }
            let view = self.view_factory.make_view("log_entries", "global")?;
            crate::lakehouse::query::query(
                &self.data_lake,
                begin.into(),
                end.into(),
                &request.sql.unwrap(),
                view,
            )
            .await
            .with_context(|| "lakehouse::query::query")?
        };
        serialize_record_batches(&answer)
    }

    pub async fn query_metrics(&self, body: bytes::Bytes) -> Result<bytes::Bytes> {
        let request: QueryMetricsRequest =
            ciborium::from_reader(body.reader()).with_context(|| "parsing QueryMetricsRequest")?;
        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 answer = if let Some(stream_id) = request.stream_id {
            if request.limit.is_none() {
                anyhow::bail!("limit is required for stream-specific queries");
            }
            let record_batch = crate::query_metrics::query_metrics(
                &self.data_lake,
                request.limit.unwrap(),
                stream_id,
                begin.into(),
                end.into(),
            )
            .await
            .with_context(|| "query_metrics")?;
            Answer::from_record_batch(record_batch)
        } else {
            if request.sql.is_none() {
                anyhow::bail!("sql is required for lakehouse queries");
            }
            if request.limit.is_some() {
                anyhow::bail!("limit must be included in the sql statement for lakehouse queries");
            }
            let view = self.view_factory.make_view("measures", "global")?;
            crate::lakehouse::query::query(
                &self.data_lake,
                begin.into(),
                end.into(),
                &request.sql.unwrap(),
                view,
            )
            .await
            .with_context(|| "lakehouse::query::query")?
        };
        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 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 answer = crate::lakehouse::query::query(
            &self.data_lake,
            begin.into(),
            end.into(),
            &request.sql,
            view,
        )
        .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")
            .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 create_or_update_partitions(
        &self,
        body: bytes::Bytes,
        writer: Arc<ResponseWriter>,
    ) -> Result<()> {
        let request: CreateOrUpdatePartitionsRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing CreateOrUpdatePartitionsRequest")?;
        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")?;
        create_or_update_partitions(
            self.data_lake.clone(),
            view,
            begin.into(),
            end.into(),
            delta,
            writer,
        )
        .await?;
        Ok(())
    }

    pub async fn merge_partitions(
        &self,
        body: bytes::Bytes,
        writer: Arc<ResponseWriter>,
    ) -> Result<()> {
        let request: MergePartitionsRequest = ciborium::from_reader(body.reader())
            .with_context(|| "parsing MergePartitionsRequest")?;
        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")?;
        merge_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())
}