surrealdb init

This commit is contained in:
Per Stark
2024-10-13 20:20:46 +02:00
parent 13b8fce803
commit 83d8452316
14 changed files with 2144 additions and 28 deletions

2053
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -21,6 +21,7 @@ redis = { version = "0.27.2", features = ["aio", "tokio-comp"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
sha2 = "0.10.8"
surrealdb = "2.0.4"
tempfile = "3.12.0"
thiserror = "1.0.63"
tokio = { version = "1.40.0", features = ["full"] }

16
flake.lock generated
View File

@@ -511,17 +511,17 @@
},
"nixpkgs_3": {
"locked": {
"lastModified": 1716977621,
"narHash": "sha256-Q1UQzYcMJH4RscmpTkjlgqQDX5yi1tZL0O345Ri6vXQ=",
"owner": "cachix",
"repo": "devenv-nixpkgs",
"rev": "4267e705586473d3e5c8d50299e71503f16a6fb6",
"lastModified": 1728492678,
"narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7",
"type": "github"
},
"original": {
"owner": "cachix",
"ref": "rolling",
"repo": "devenv-nixpkgs",
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},

View File

@@ -1,6 +1,6 @@
{
inputs = {
nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
devenv.url = "github:cachix/devenv";
devenv.inputs.nixpkgs.follows = "nixpkgs";

View File

@@ -1,4 +1,3 @@
use tokio;
use tracing::info;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
use zettle_db::rabbitmq::{consumer::RabbitMQConsumer, RabbitMQConfig };

View File

@@ -3,4 +3,5 @@ pub mod neo4j;
pub mod rabbitmq;
pub mod redis;
pub mod routes;
pub mod surrealdb;
pub mod utils;

View File

@@ -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,

View File

@@ -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 })
}

View File

@@ -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)))
}

View File

@@ -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
View 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
View File

@@ -0,0 +1 @@

38
src/surrealdb/mod.rs Normal file
View 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 })
}
}

View File

@@ -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);