0.5.1
This commit is contained in:
133
kb_lib/src/db/queries/db_runtime_event.rs
Normal file
133
kb_lib/src/db/queries/db_runtime_event.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
// file: kb_lib/src/db/queries/db_runtime_event.rs
|
||||
|
||||
//! Queries for `kb_db_runtime_events`.
|
||||
|
||||
/// Inserts one runtime event row and returns its numeric id.
|
||||
pub async fn insert_db_runtime_event(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbDbRuntimeEventDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_db_runtime_events (
|
||||
event_kind,
|
||||
level,
|
||||
source,
|
||||
message,
|
||||
created_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
"#,
|
||||
)
|
||||
.bind(dto.event_kind.clone())
|
||||
.bind(dto.level.to_i16())
|
||||
.bind(dto.source.clone())
|
||||
.bind(dto.message.clone())
|
||||
.bind(dto.created_at.to_rfc3339())
|
||||
.execute(pool)
|
||||
.await;
|
||||
let query_result = match query_result {
|
||||
Ok(query_result) => query_result,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot insert kb_db_runtime_events on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(query_result.last_insert_rowid())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists recent runtime events ordered from newest to oldest.
|
||||
pub async fn list_recent_db_runtime_events(
|
||||
database: &crate::KbDatabase,
|
||||
limit: u32,
|
||||
) -> Result<std::vec::Vec<crate::KbDbRuntimeEventDto>, crate::KbError> {
|
||||
if limit == 0 {
|
||||
return Ok(std::vec::Vec::new());
|
||||
}
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbDbRuntimeEventEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
event_kind,
|
||||
level,
|
||||
source,
|
||||
message,
|
||||
created_at
|
||||
FROM kb_db_runtime_events
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
"#,
|
||||
)
|
||||
.bind(i64::from(limit))
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list runtime events on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbDbRuntimeEventDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[tokio::test]
|
||||
async fn runtime_event_roundtrip_works() {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("runtime_event.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database = crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed");
|
||||
let dto = crate::KbDbRuntimeEventDto::new(
|
||||
"http_request".to_string(),
|
||||
crate::KbDbRuntimeEventLevel::Info,
|
||||
"demo_http".to_string(),
|
||||
"getHealth executed".to_string(),
|
||||
);
|
||||
let inserted_id = crate::insert_db_runtime_event(&database, &dto)
|
||||
.await
|
||||
.expect("insert must succeed");
|
||||
assert!(inserted_id > 0);
|
||||
let listed = crate::list_recent_db_runtime_events(&database, 10)
|
||||
.await
|
||||
.expect("list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
assert_eq!(listed[0].event_kind, "http_request");
|
||||
assert_eq!(listed[0].level, crate::KbDbRuntimeEventLevel::Info);
|
||||
}
|
||||
}
|
||||
195
kb_lib/src/db/queries/known_http_endpoint.rs
Normal file
195
kb_lib/src/db/queries/known_http_endpoint.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
// file: kb_lib/src/db/queries/known_http_endpoint.rs
|
||||
|
||||
//! Queries for `kb_known_http_endpoints`.
|
||||
|
||||
/// Inserts or updates one known HTTP endpoint row.
|
||||
pub async fn upsert_known_http_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbKnownHttpEndpointDto,
|
||||
) -> Result<(), crate::KbError> {
|
||||
let entity_result = crate::KbKnownHttpEndpointEntity::try_from(dto.clone());
|
||||
let entity = match entity_result {
|
||||
Ok(entity) => entity,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_known_http_endpoints (
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
provider = excluded.provider,
|
||||
url = excluded.url,
|
||||
enabled = excluded.enabled,
|
||||
roles_json = excluded.roles_json,
|
||||
last_seen_at = excluded.last_seen_at,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(entity.name)
|
||||
.bind(entity.provider)
|
||||
.bind(entity.url)
|
||||
.bind(entity.enabled)
|
||||
.bind(entity.roles_json)
|
||||
.bind(entity.last_seen_at)
|
||||
.bind(entity.updated_at)
|
||||
.execute(pool)
|
||||
.await;
|
||||
match query_result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_known_http_endpoints on sqlite: {}",
|
||||
error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads one known HTTP endpoint by name.
|
||||
pub async fn get_known_http_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
name: &str,
|
||||
) -> Result<std::option::Option<crate::KbKnownHttpEndpointDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownHttpEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_http_endpoints
|
||||
WHERE name = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(name)
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
let entity_option = match query_result {
|
||||
Ok(entity_option) => entity_option,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot read known http endpoint '{}' on sqlite: {}",
|
||||
name, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => {
|
||||
let dto_result = crate::KbKnownHttpEndpointDto::try_from(entity);
|
||||
match dto_result {
|
||||
Ok(dto) => Ok(Some(dto)),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists all known HTTP endpoints.
|
||||
pub async fn list_known_http_endpoints(
|
||||
database: &crate::KbDatabase,
|
||||
) -> Result<std::vec::Vec<crate::KbKnownHttpEndpointDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownHttpEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_http_endpoints
|
||||
ORDER BY name ASC
|
||||
"#,
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list known http endpoints on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbKnownHttpEndpointDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[tokio::test]
|
||||
async fn known_http_endpoint_roundtrip_works() {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("known_http_endpoint.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database = crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed");
|
||||
let dto = crate::KbKnownHttpEndpointDto::new(
|
||||
"helius_primary_http".to_string(),
|
||||
"helius".to_string(),
|
||||
"https://mainnet.helius-rpc.com".to_string(),
|
||||
true,
|
||||
vec!["http_queries".to_string(), "http_transactions".to_string()],
|
||||
);
|
||||
crate::upsert_known_http_endpoint(&database, &dto)
|
||||
.await
|
||||
.expect("upsert must succeed");
|
||||
let fetched = crate::get_known_http_endpoint(&database, "helius_primary_http")
|
||||
.await
|
||||
.expect("fetch must succeed");
|
||||
assert!(fetched.is_some());
|
||||
let fetched = fetched.expect("endpoint must exist");
|
||||
assert_eq!(fetched.provider, "helius");
|
||||
assert_eq!(fetched.roles.len(), 2);
|
||||
let listed = crate::list_known_http_endpoints(&database)
|
||||
.await
|
||||
.expect("list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
}
|
||||
}
|
||||
195
kb_lib/src/db/queries/known_ws_endpoint.rs
Normal file
195
kb_lib/src/db/queries/known_ws_endpoint.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
// file: kb_lib/src/db/queries/known_ws_endpoint.rs
|
||||
|
||||
//! Queries for `kb_known_ws_endpoints`.
|
||||
|
||||
/// Inserts or updates one known WS endpoint row.
|
||||
pub async fn upsert_known_ws_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbKnownWsEndpointDto,
|
||||
) -> Result<(), crate::KbError> {
|
||||
let entity_result = crate::KbKnownWsEndpointEntity::try_from(dto.clone());
|
||||
let entity = match entity_result {
|
||||
Ok(entity) => entity,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_known_ws_endpoints (
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
provider = excluded.provider,
|
||||
url = excluded.url,
|
||||
enabled = excluded.enabled,
|
||||
roles_json = excluded.roles_json,
|
||||
last_seen_at = excluded.last_seen_at,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(entity.name)
|
||||
.bind(entity.provider)
|
||||
.bind(entity.url)
|
||||
.bind(entity.enabled)
|
||||
.bind(entity.roles_json)
|
||||
.bind(entity.last_seen_at)
|
||||
.bind(entity.updated_at)
|
||||
.execute(pool)
|
||||
.await;
|
||||
match query_result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_known_ws_endpoints on sqlite: {}",
|
||||
error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads one known WS endpoint by name.
|
||||
pub async fn get_known_ws_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
name: &str,
|
||||
) -> Result<std::option::Option<crate::KbKnownWsEndpointDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownWsEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_ws_endpoints
|
||||
WHERE name = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(name)
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
let entity_option = match query_result {
|
||||
Ok(entity_option) => entity_option,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot read known ws endpoint '{}' on sqlite: {}",
|
||||
name, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => {
|
||||
let dto_result = crate::KbKnownWsEndpointDto::try_from(entity);
|
||||
match dto_result {
|
||||
Ok(dto) => Ok(Some(dto)),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists all known WS endpoints.
|
||||
pub async fn list_known_ws_endpoints(
|
||||
database: &crate::KbDatabase,
|
||||
) -> Result<std::vec::Vec<crate::KbKnownWsEndpointDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownWsEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_ws_endpoints
|
||||
ORDER BY name ASC
|
||||
"#,
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list known ws endpoints on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbKnownWsEndpointDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[tokio::test]
|
||||
async fn known_ws_endpoint_roundtrip_works() {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("known_ws_endpoint.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database = crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed");
|
||||
let dto = crate::KbKnownWsEndpointDto::new(
|
||||
"mainnet_public_ws_slots".to_string(),
|
||||
"solana".to_string(),
|
||||
"wss://api.mainnet.solana.com".to_string(),
|
||||
true,
|
||||
vec!["ws_slots".to_string(), "ws_subscriptions".to_string()],
|
||||
);
|
||||
crate::upsert_known_ws_endpoint(&database, &dto)
|
||||
.await
|
||||
.expect("upsert must succeed");
|
||||
let fetched = crate::get_known_ws_endpoint(&database, "mainnet_public_ws_slots")
|
||||
.await
|
||||
.expect("fetch must succeed");
|
||||
assert!(fetched.is_some());
|
||||
let fetched = fetched.expect("endpoint must exist");
|
||||
assert_eq!(fetched.provider, "solana");
|
||||
assert_eq!(fetched.roles.len(), 2);
|
||||
let listed = crate::list_known_ws_endpoints(&database)
|
||||
.await
|
||||
.expect("list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user