mirror of
https://github.com/perstarkse/minne.git
synced 2026-04-25 10:18:38 +02:00
surrealdb init
This commit is contained in:
2053
Cargo.lock
generated
2053
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -21,6 +21,7 @@ redis = { version = "0.27.2", features = ["aio", "tokio-comp"] }
|
|||||||
serde = { version = "1.0.210", features = ["derive"] }
|
serde = { version = "1.0.210", features = ["derive"] }
|
||||||
serde_json = "1.0.128"
|
serde_json = "1.0.128"
|
||||||
sha2 = "0.10.8"
|
sha2 = "0.10.8"
|
||||||
|
surrealdb = "2.0.4"
|
||||||
tempfile = "3.12.0"
|
tempfile = "3.12.0"
|
||||||
thiserror = "1.0.63"
|
thiserror = "1.0.63"
|
||||||
tokio = { version = "1.40.0", features = ["full"] }
|
tokio = { version = "1.40.0", features = ["full"] }
|
||||||
|
|||||||
16
flake.lock
generated
16
flake.lock
generated
@@ -511,17 +511,17 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs_3": {
|
"nixpkgs_3": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1716977621,
|
"lastModified": 1728492678,
|
||||||
"narHash": "sha256-Q1UQzYcMJH4RscmpTkjlgqQDX5yi1tZL0O345Ri6vXQ=",
|
"narHash": "sha256-9UTxR8eukdg+XZeHgxW5hQA9fIKHsKCdOIUycTryeVw=",
|
||||||
"owner": "cachix",
|
"owner": "nixos",
|
||||||
"repo": "devenv-nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "4267e705586473d3e5c8d50299e71503f16a6fb6",
|
"rev": "5633bcff0c6162b9e4b5f1264264611e950c8ec7",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "cachix",
|
"owner": "nixos",
|
||||||
"ref": "rolling",
|
"ref": "nixos-unstable",
|
||||||
"repo": "devenv-nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
|
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
systems.url = "github:nix-systems/default";
|
systems.url = "github:nix-systems/default";
|
||||||
devenv.url = "github:cachix/devenv";
|
devenv.url = "github:cachix/devenv";
|
||||||
devenv.inputs.nixpkgs.follows = "nixpkgs";
|
devenv.inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use tokio;
|
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||||
use zettle_db::rabbitmq::{consumer::RabbitMQConsumer, RabbitMQConfig };
|
use zettle_db::rabbitmq::{consumer::RabbitMQConsumer, RabbitMQConfig };
|
||||||
|
|||||||
@@ -3,4 +3,5 @@ pub mod neo4j;
|
|||||||
pub mod rabbitmq;
|
pub mod rabbitmq;
|
||||||
pub mod redis;
|
pub mod redis;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
|
pub mod surrealdb;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ use crate::redis::client::{RedisClient, RedisClientTrait};
|
|||||||
/// Represents metadata and storage information for a file.
|
/// Represents metadata and storage information for a file.
|
||||||
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
|
||||||
pub struct FileInfo {
|
pub struct FileInfo {
|
||||||
pub uuid: Uuid,
|
pub uuid: String,
|
||||||
pub sha256: String,
|
pub sha256: String,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub mime_type: String,
|
pub mime_type: String,
|
||||||
@@ -139,7 +139,7 @@ impl FileInfo {
|
|||||||
|
|
||||||
// Construct the FileInfo object
|
// Construct the FileInfo object
|
||||||
let file_info = FileInfo {
|
let file_info = FileInfo {
|
||||||
uuid,
|
uuid: uuid.to_string(),
|
||||||
sha256: sha.clone(),
|
sha256: sha.clone(),
|
||||||
path: persisted_path.to_string_lossy().to_string(),
|
path: persisted_path.to_string_lossy().to_string(),
|
||||||
mime_type,
|
mime_type,
|
||||||
@@ -212,7 +212,7 @@ impl FileInfo {
|
|||||||
|
|
||||||
// Update FileInfo
|
// Update FileInfo
|
||||||
let updated_file_info = FileInfo {
|
let updated_file_info = FileInfo {
|
||||||
uuid,
|
uuid: uuid.to_string(),
|
||||||
sha256: new_sha.clone(),
|
sha256: new_sha.clone(),
|
||||||
path: new_persisted_path.to_string_lossy().to_string(),
|
path: new_persisted_path.to_string_lossy().to_string(),
|
||||||
mime_type: new_mime_type,
|
mime_type: new_mime_type,
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ impl RabbitMQConsumer {
|
|||||||
Self::bind_queue(&common.channel, &config.exchange, &queue, config).await?;
|
Self::bind_queue(&common.channel, &config.exchange, &queue, config).await?;
|
||||||
|
|
||||||
// Initialize the consumer
|
// 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 })
|
Ok(Self { common, queue, consumer })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
models::file_info::{FileError, FileInfo},
|
models::file_info::{FileError, FileInfo},
|
||||||
redis::client::RedisClient,
|
redis::client::RedisClient, surrealdb::{document::set_file_info, SurrealDbClient},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, TryFromMultipart)]
|
#[derive(Debug, TryFromMultipart)]
|
||||||
@@ -45,6 +45,10 @@ pub async fn upload_handler(
|
|||||||
|
|
||||||
info!("File uploaded successfully: {:?}", file_info);
|
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
|
// Return the response with HTTP 200
|
||||||
Ok((axum::http::StatusCode::OK, Json(response)))
|
Ok((axum::http::StatusCode::OK, Json(response)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
extract::DefaultBodyLimit, routing::{delete, get, post, put}, Extension, Router
|
extract::DefaultBodyLimit, routing::{delete, get, post, put}, Extension, Router
|
||||||
};
|
};
|
||||||
|
use tracing::info;
|
||||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
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;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
#[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 producer = Arc::new(RabbitMQProducer::new(&config).await?);
|
||||||
|
|
||||||
|
// let database = SurrealDbClient::new().await?;
|
||||||
|
|
||||||
|
// database.client.health().await?;
|
||||||
|
// info!("Passed health check");
|
||||||
|
|
||||||
// Create Axum router
|
// Create Axum router
|
||||||
let app = Router::new()
|
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
|
// Send the request to OpenAI
|
||||||
let response = client.chat().create(request).await.map_err(|e| {
|
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);
|
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| {
|
let analysis: AnalysisResult = serde_json::from_str(&content).map_err(|e| {
|
||||||
ProcessingError::LLMError(format!(
|
ProcessingError::LLMError(format!(
|
||||||
"Failed to parse LLM response into LLMAnalysis: {}",
|
"Failed to parse LLM response into LLMAnalysis: {}",
|
||||||
e.to_string()
|
e
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
return Ok(analysis);
|
return Ok(analysis);
|
||||||
|
|||||||
Reference in New Issue
Block a user