common_frontend/
slow_query_event.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16
17use api::v1::value::ValueData;
18use api::v1::{ColumnDataType, ColumnSchema, Row, SemanticType};
19use common_event_recorder::error::Result;
20use common_event_recorder::Event;
21use serde::Serialize;
22
23pub const SLOW_QUERY_TABLE_NAME: &str = "slow_queries";
24pub const SLOW_QUERY_TABLE_COST_COLUMN_NAME: &str = "cost";
25pub const SLOW_QUERY_TABLE_THRESHOLD_COLUMN_NAME: &str = "threshold";
26pub const SLOW_QUERY_TABLE_QUERY_COLUMN_NAME: &str = "query";
27pub const SLOW_QUERY_TABLE_TIMESTAMP_COLUMN_NAME: &str = "timestamp";
28pub const SLOW_QUERY_TABLE_IS_PROMQL_COLUMN_NAME: &str = "is_promql";
29pub const SLOW_QUERY_TABLE_PROMQL_START_COLUMN_NAME: &str = "promql_start";
30pub const SLOW_QUERY_TABLE_PROMQL_END_COLUMN_NAME: &str = "promql_end";
31pub const SLOW_QUERY_TABLE_PROMQL_RANGE_COLUMN_NAME: &str = "promql_range";
32pub const SLOW_QUERY_TABLE_PROMQL_STEP_COLUMN_NAME: &str = "promql_step";
33pub const SLOW_QUERY_EVENT_TYPE: &str = "slow_query";
34
35/// SlowQueryEvent is the event of slow query.
36#[derive(Debug, Serialize)]
37pub struct SlowQueryEvent {
38    pub cost: u64,
39    pub threshold: u64,
40    pub query: String,
41    pub is_promql: bool,
42    pub promql_range: Option<u64>,
43    pub promql_step: Option<u64>,
44    pub promql_start: Option<i64>,
45    pub promql_end: Option<i64>,
46}
47
48impl Event for SlowQueryEvent {
49    fn table_name(&self) -> &str {
50        SLOW_QUERY_TABLE_NAME
51    }
52
53    fn event_type(&self) -> &str {
54        SLOW_QUERY_EVENT_TYPE
55    }
56
57    fn extra_schema(&self) -> Vec<ColumnSchema> {
58        vec![
59            ColumnSchema {
60                column_name: SLOW_QUERY_TABLE_COST_COLUMN_NAME.to_string(),
61                datatype: ColumnDataType::Uint64.into(),
62                semantic_type: SemanticType::Field.into(),
63                ..Default::default()
64            },
65            ColumnSchema {
66                column_name: SLOW_QUERY_TABLE_THRESHOLD_COLUMN_NAME.to_string(),
67                datatype: ColumnDataType::Uint64.into(),
68                semantic_type: SemanticType::Field.into(),
69                ..Default::default()
70            },
71            ColumnSchema {
72                column_name: SLOW_QUERY_TABLE_QUERY_COLUMN_NAME.to_string(),
73                datatype: ColumnDataType::String.into(),
74                semantic_type: SemanticType::Field.into(),
75                ..Default::default()
76            },
77            ColumnSchema {
78                column_name: SLOW_QUERY_TABLE_IS_PROMQL_COLUMN_NAME.to_string(),
79                datatype: ColumnDataType::Boolean.into(),
80                semantic_type: SemanticType::Field.into(),
81                ..Default::default()
82            },
83            ColumnSchema {
84                column_name: SLOW_QUERY_TABLE_PROMQL_RANGE_COLUMN_NAME.to_string(),
85                datatype: ColumnDataType::Uint64.into(),
86                semantic_type: SemanticType::Field.into(),
87                ..Default::default()
88            },
89            ColumnSchema {
90                column_name: SLOW_QUERY_TABLE_PROMQL_STEP_COLUMN_NAME.to_string(),
91                datatype: ColumnDataType::Uint64.into(),
92                semantic_type: SemanticType::Field.into(),
93                ..Default::default()
94            },
95            ColumnSchema {
96                column_name: SLOW_QUERY_TABLE_PROMQL_START_COLUMN_NAME.to_string(),
97                datatype: ColumnDataType::TimestampMillisecond.into(),
98                semantic_type: SemanticType::Field.into(),
99                ..Default::default()
100            },
101            ColumnSchema {
102                column_name: SLOW_QUERY_TABLE_PROMQL_END_COLUMN_NAME.to_string(),
103                datatype: ColumnDataType::TimestampMillisecond.into(),
104                semantic_type: SemanticType::Field.into(),
105                ..Default::default()
106            },
107        ]
108    }
109
110    fn extra_row(&self) -> Result<Row> {
111        Ok(Row {
112            values: vec![
113                ValueData::U64Value(self.cost).into(),
114                ValueData::U64Value(self.threshold).into(),
115                ValueData::StringValue(self.query.to_string()).into(),
116                ValueData::BoolValue(self.is_promql).into(),
117                ValueData::U64Value(self.promql_range.unwrap_or(0)).into(),
118                ValueData::U64Value(self.promql_step.unwrap_or(0)).into(),
119                ValueData::TimestampMillisecondValue(self.promql_start.unwrap_or(0)).into(),
120                ValueData::TimestampMillisecondValue(self.promql_end.unwrap_or(0)).into(),
121            ],
122        })
123    }
124
125    fn json_payload(&self) -> Result<String> {
126        Ok("".to_string())
127    }
128
129    fn as_any(&self) -> &dyn Any {
130        self
131    }
132}