mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-18 15:09:49 +02:00
surrealdb init
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
use tokio;
|
||||
use tracing::info;
|
||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||
use zettle_db::rabbitmq::{consumer::RabbitMQConsumer, RabbitMQConfig };
|
||||
|
||||
@@ -3,4 +3,5 @@ pub mod neo4j;
|
||||
pub mod rabbitmq;
|
||||
pub mod redis;
|
||||
pub mod routes;
|
||||
pub mod surrealdb;
|
||||
pub mod utils;
|
||||
|
||||
@@ -19,7 +19,7 @@ use crate::redis::client::{RedisClient, RedisClientTrait};
|
||||
/// Represents metadata and storage information for a file.
|
||||
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
||||
pub struct FileInfo {
|
||||
pub uuid: Uuid,
|
||||
pub uuid: String,
|
||||
pub sha256: String,
|
||||
pub path: String,
|
||||
pub mime_type: String,
|
||||
@@ -139,7 +139,7 @@ impl FileInfo {
|
||||
|
||||
// Construct the FileInfo object
|
||||
let file_info = FileInfo {
|
||||
uuid,
|
||||
uuid: uuid.to_string(),
|
||||
sha256: sha.clone(),
|
||||
path: persisted_path.to_string_lossy().to_string(),
|
||||
mime_type,
|
||||
@@ -212,7 +212,7 @@ impl FileInfo {
|
||||
|
||||
// Update FileInfo
|
||||
let updated_file_info = FileInfo {
|
||||
uuid,
|
||||
uuid: uuid.to_string(),
|
||||
sha256: new_sha.clone(),
|
||||
path: new_persisted_path.to_string_lossy().to_string(),
|
||||
mime_type: new_mime_type,
|
||||
|
||||
@@ -35,7 +35,7 @@ impl RabbitMQConsumer {
|
||||
Self::bind_queue(&common.channel, &config.exchange, &queue, config).await?;
|
||||
|
||||
// Initialize the consumer
|
||||
let consumer = Self::initialize_consumer(&common.channel, &config).await?;
|
||||
let consumer = Self::initialize_consumer(&common.channel, config).await?;
|
||||
|
||||
Ok(Self { common, queue, consumer })
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
models::file_info::{FileError, FileInfo},
|
||||
redis::client::RedisClient,
|
||||
redis::client::RedisClient, surrealdb::{document::set_file_info, SurrealDbClient},
|
||||
};
|
||||
|
||||
#[derive(Debug, TryFromMultipart)]
|
||||
@@ -45,6 +45,10 @@ pub async fn upload_handler(
|
||||
|
||||
info!("File uploaded successfully: {:?}", file_info);
|
||||
|
||||
let database = SurrealDbClient::new().await.map_err(|e| FileError::PersistError(e.to_string())).unwrap();
|
||||
|
||||
set_file_info(database.client, &file_info.sha256, file_info.clone()).await.unwrap();
|
||||
|
||||
// Return the response with HTTP 200
|
||||
Ok((axum::http::StatusCode::OK, Json(response)))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use axum::{
|
||||
extract::DefaultBodyLimit, routing::{delete, get, post, put}, Extension, Router
|
||||
};
|
||||
use tracing::info;
|
||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||
use zettle_db::{rabbitmq::{publisher::RabbitMQProducer, RabbitMQConfig}, routes::{file::{delete_file_handler, get_file_handler, update_file_handler, upload_handler}, ingress::ingress_handler, queue_length::queue_length_handler}};
|
||||
use zettle_db::{rabbitmq::{publisher::RabbitMQProducer, RabbitMQConfig}, routes::{file::{delete_file_handler, get_file_handler, update_file_handler, upload_handler}, ingress::ingress_handler, queue_length::queue_length_handler}, surrealdb::SurrealDbClient};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
||||
@@ -23,6 +24,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
};
|
||||
|
||||
let producer = Arc::new(RabbitMQProducer::new(&config).await?);
|
||||
|
||||
// let database = SurrealDbClient::new().await?;
|
||||
|
||||
// database.client.health().await?;
|
||||
// info!("Passed health check");
|
||||
|
||||
// Create Axum router
|
||||
let app = Router::new()
|
||||
|
||||
33
src/surrealdb/document.rs
Normal file
33
src/surrealdb/document.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use surrealdb::{engine::remote::ws::Client, RecordId, Surreal};
|
||||
use tracing::info;
|
||||
|
||||
use crate::models::file_info::FileInfo;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Record {
|
||||
id: RecordId,
|
||||
}
|
||||
|
||||
use super::SurrealError;
|
||||
|
||||
pub async fn set_file_info(client: Surreal<Client>, sha256: &str, file_info: FileInfo) -> Result<(), SurrealError> {
|
||||
info!("Creating in surrealdb");
|
||||
info!("{:?}, {:?}", sha256, file_info);
|
||||
|
||||
// Use create instead of upsert if you're sure the record doesn't exist
|
||||
let created: Option<Record> = client
|
||||
.create(("file", sha256))
|
||||
.content(file_info)
|
||||
.await?;
|
||||
|
||||
// If you want to update or create, use upsert instead
|
||||
// let created: Option<Record> = client
|
||||
// .upsert(("file", sha256))
|
||||
// .content(file_info)
|
||||
// .await?;
|
||||
|
||||
info!("{:?}", created);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
1
src/surrealdb/graph.rs
Normal file
1
src/surrealdb/graph.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
38
src/surrealdb/mod.rs
Normal file
38
src/surrealdb/mod.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use surrealdb::{engine::remote::ws::{Client, Ws}, opt::auth::Root, Surreal};
|
||||
use thiserror::Error;
|
||||
|
||||
pub mod document;
|
||||
pub mod graph;
|
||||
|
||||
pub struct SurrealDbClient {
|
||||
pub client: Surreal<Client>,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum SurrealError {
|
||||
#[error("SurrealDb error: {0}")]
|
||||
SurrealDbError(#[from] surrealdb::Error),
|
||||
|
||||
// Add more error variants as needed.
|
||||
}
|
||||
|
||||
|
||||
impl SurrealDbClient {
|
||||
/// # Initialize a new datbase client
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// # Returns
|
||||
/// * `SurrealDbClient` initialized
|
||||
pub async fn new() -> Result<Self, SurrealError> {
|
||||
let db = Surreal::new::<Ws>("127.0.0.1:8000").await?;
|
||||
|
||||
// Sign in to database
|
||||
db.signin(Root{username: "root_user", password: "root_password"}).await?;
|
||||
|
||||
// Set namespace
|
||||
db.use_ns("test").use_db("test").await?;
|
||||
|
||||
Ok(SurrealDbClient { client: db })
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ pub async fn create_json_ld(category: &str, instructions: &str, text: &str) -> R
|
||||
|
||||
// Send the request to OpenAI
|
||||
let response = client.chat().create(request).await.map_err(|e| {
|
||||
ProcessingError::LLMError(format!("OpenAI API request failed: {}", e.to_string()))
|
||||
ProcessingError::LLMError(format!("OpenAI API request failed: {}", e))
|
||||
})?;
|
||||
|
||||
info!("{:?}", response);
|
||||
@@ -85,7 +85,7 @@ pub async fn create_json_ld(category: &str, instructions: &str, text: &str) -> R
|
||||
let analysis: AnalysisResult = serde_json::from_str(&content).map_err(|e| {
|
||||
ProcessingError::LLMError(format!(
|
||||
"Failed to parse LLM response into LLMAnalysis: {}",
|
||||
e.to_string()
|
||||
e
|
||||
))
|
||||
})?;
|
||||
return Ok(analysis);
|
||||
|
||||
Reference in New Issue
Block a user