mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
refactor(data): limit query abstraction to postgres and sqlite
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use async_trait::async_trait;
|
||||
use sqlx::{mysql::MySqlRow, MySql, QueryBuilder, Row};
|
||||
use sqlx::{mysql::MySqlRow, Row};
|
||||
|
||||
use super::types::{
|
||||
bucket_start_unix_secs, build_tunnel_error_event_detail, build_tunnel_metrics_sample,
|
||||
@@ -14,7 +14,6 @@ use super::types::{
|
||||
use crate::driver::mysql::MysqlPool;
|
||||
use crate::error::SqlResultExt;
|
||||
use crate::DataLayerError;
|
||||
use aether_data_query::{push_eq, push_limit, WhereClause};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MysqlProxyNodeReadRepository {
|
||||
@@ -338,23 +337,13 @@ SELECT
|
||||
FROM proxy_nodes
|
||||
"#;
|
||||
|
||||
const PROXY_NODE_EVENT_COLUMNS: &str = r#"
|
||||
SELECT
|
||||
id,
|
||||
node_id,
|
||||
event_type,
|
||||
detail,
|
||||
event_metadata,
|
||||
created_at AS created_at_unix_ms
|
||||
FROM proxy_node_events
|
||||
"#;
|
||||
|
||||
#[async_trait]
|
||||
impl ProxyNodeReadRepository for MysqlProxyNodeReadRepository {
|
||||
async fn list_proxy_nodes(&self) -> Result<Vec<StoredProxyNode>, DataLayerError> {
|
||||
let mut builder = QueryBuilder::<MySql>::new(PROXY_NODE_COLUMNS);
|
||||
builder.push(" ORDER BY name ASC, id ASC");
|
||||
let rows = builder.build().fetch_all(&self.pool).await.map_sql_err()?;
|
||||
let rows = sqlx::query(&format!("{PROXY_NODE_COLUMNS} ORDER BY name ASC, id ASC"))
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_sql_err()?;
|
||||
rows.iter().map(map_proxy_node_row).collect()
|
||||
}
|
||||
|
||||
@@ -362,12 +351,8 @@ impl ProxyNodeReadRepository for MysqlProxyNodeReadRepository {
|
||||
&self,
|
||||
node_id: &str,
|
||||
) -> Result<Option<StoredProxyNode>, DataLayerError> {
|
||||
let mut builder = QueryBuilder::<MySql>::new(PROXY_NODE_COLUMNS);
|
||||
let mut where_clause = WhereClause::new();
|
||||
push_eq(&mut builder, &mut where_clause, "id", node_id.to_string());
|
||||
push_limit(&mut builder, 1);
|
||||
let row = builder
|
||||
.build()
|
||||
let row = sqlx::query(&format!("{PROXY_NODE_COLUMNS} WHERE id = ? LIMIT 1"))
|
||||
.bind(node_id)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_sql_err()?;
|
||||
@@ -379,17 +364,26 @@ impl ProxyNodeReadRepository for MysqlProxyNodeReadRepository {
|
||||
node_id: &str,
|
||||
limit: usize,
|
||||
) -> Result<Vec<StoredProxyNodeEvent>, DataLayerError> {
|
||||
let mut builder = QueryBuilder::<MySql>::new(PROXY_NODE_EVENT_COLUMNS);
|
||||
let mut where_clause = WhereClause::new();
|
||||
push_eq(
|
||||
&mut builder,
|
||||
&mut where_clause,
|
||||
"node_id",
|
||||
node_id.to_string(),
|
||||
);
|
||||
builder.push(" ORDER BY created_at DESC, id DESC");
|
||||
push_limit(&mut builder, i64::try_from(limit).unwrap_or(i64::MAX));
|
||||
let rows = builder.build().fetch_all(&self.pool).await.map_sql_err()?;
|
||||
let rows = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
node_id,
|
||||
event_type,
|
||||
detail,
|
||||
event_metadata,
|
||||
created_at AS created_at_unix_ms
|
||||
FROM proxy_node_events
|
||||
WHERE node_id = ?
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT ?
|
||||
"#,
|
||||
)
|
||||
.bind(node_id)
|
||||
.bind(i64::try_from(limit).unwrap_or(i64::MAX))
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_sql_err()?;
|
||||
rows.iter().map(map_proxy_node_event_row).collect()
|
||||
}
|
||||
|
||||
@@ -398,36 +392,51 @@ impl ProxyNodeReadRepository for MysqlProxyNodeReadRepository {
|
||||
node_id: &str,
|
||||
query: &ProxyNodeEventQuery,
|
||||
) -> Result<Vec<StoredProxyNodeEvent>, DataLayerError> {
|
||||
let mut builder = QueryBuilder::<MySql>::new(PROXY_NODE_EVENT_COLUMNS);
|
||||
let mut where_clause = WhereClause::new();
|
||||
push_eq(
|
||||
&mut builder,
|
||||
&mut where_clause,
|
||||
"node_id",
|
||||
node_id.to_string(),
|
||||
);
|
||||
if let Some(from_unix_secs) = query.from_unix_secs {
|
||||
where_clause.push_next(&mut builder);
|
||||
builder
|
||||
.push("created_at >= ")
|
||||
.push_bind(i64::try_from(from_unix_secs).unwrap_or(i64::MAX));
|
||||
}
|
||||
if let Some(to_unix_secs) = query.to_unix_secs {
|
||||
where_clause.push_next(&mut builder);
|
||||
builder
|
||||
.push("created_at <= ")
|
||||
.push_bind(i64::try_from(to_unix_secs).unwrap_or(i64::MAX));
|
||||
}
|
||||
if let Some(event_type) = query.event_type.as_deref() {
|
||||
where_clause.push_next(&mut builder);
|
||||
builder
|
||||
.push("LOWER(event_type) = LOWER(")
|
||||
.push_bind(event_type.to_string())
|
||||
.push(")");
|
||||
}
|
||||
builder.push(" ORDER BY created_at DESC, id DESC");
|
||||
push_limit(&mut builder, i64::try_from(query.limit).unwrap_or(i64::MAX));
|
||||
let rows = builder.build().fetch_all(&self.pool).await.map_sql_err()?;
|
||||
let rows = sqlx::query(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
node_id,
|
||||
event_type,
|
||||
detail,
|
||||
event_metadata,
|
||||
created_at AS created_at_unix_ms
|
||||
FROM proxy_node_events
|
||||
WHERE node_id = ?
|
||||
AND (? IS NULL OR created_at >= ?)
|
||||
AND (? IS NULL OR created_at <= ?)
|
||||
AND (? IS NULL OR LOWER(event_type) = LOWER(?))
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT ?
|
||||
"#,
|
||||
)
|
||||
.bind(node_id)
|
||||
.bind(
|
||||
query
|
||||
.from_unix_secs
|
||||
.map(|v| i64::try_from(v).unwrap_or(i64::MAX)),
|
||||
)
|
||||
.bind(
|
||||
query
|
||||
.from_unix_secs
|
||||
.map(|v| i64::try_from(v).unwrap_or(i64::MAX)),
|
||||
)
|
||||
.bind(
|
||||
query
|
||||
.to_unix_secs
|
||||
.map(|v| i64::try_from(v).unwrap_or(i64::MAX)),
|
||||
)
|
||||
.bind(
|
||||
query
|
||||
.to_unix_secs
|
||||
.map(|v| i64::try_from(v).unwrap_or(i64::MAX)),
|
||||
)
|
||||
.bind(query.event_type.as_deref())
|
||||
.bind(query.event_type.as_deref())
|
||||
.bind(i64::try_from(query.limit).unwrap_or(i64::MAX))
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_sql_err()?;
|
||||
rows.iter().map(map_proxy_node_event_row).collect()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user