toka toimiva vedos

This commit is contained in:
2026-04-01 23:52:39 +03:00
parent 02f6684378
commit 9a72d35081
1420 changed files with 79953 additions and 64 deletions

View File

@@ -9,6 +9,24 @@ impl NodeDb {
pub fn new(path: &str) -> Self {
let conn = Connection::open(path).expect("SQLite-tietokantaa ei voitu avata");
// Poista vanha tietokanta jos skeema on rikki — PoC, ei tuotantodata
let _ = conn.execute_batch("
CREATE TABLE IF NOT EXISTS _schema_version (version INTEGER);
");
let version: i64 = conn.query_row(
"SELECT COALESCE(MAX(version), 0) FROM _schema_version", [], |r| r.get(0)
).unwrap_or(0);
if version < 2 {
// Pudotetaan vanhat taulut ja luodaan uudet
let _ = conn.execute_batch("
DROP TABLE IF EXISTS node_sessions;
DROP TABLE IF EXISTS pair_results;
DELETE FROM _schema_version;
INSERT INTO _schema_version VALUES (2);
");
}
conn.execute_batch("
CREATE TABLE IF NOT EXISTS node_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -35,8 +53,9 @@ impl NodeDb {
gpu_temp_c INTEGER,
gpu_util_pct INTEGER,
-- Varaus
-- Varaus ja tehtävä
allocated_gb INTEGER,
selected_task TEXT DEFAULT 'tokenize',
-- WebGPU-tuki
has_webgpu BOOLEAN,
@@ -70,7 +89,7 @@ impl NodeDb {
node_type: &str,
auth_data: &serde_json::Value,
) -> i64 {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let now = chrono::Utc::now().to_rfc3339();
// Selainsolmun tiedot
@@ -78,6 +97,7 @@ impl NodeDb {
let cpu_cores = auth_data.get("cpu_cores").and_then(|v| v.as_u64());
let ram = auth_data.get("device_memory_gb").and_then(|v| v.as_f64()).map(|v| (v * 1024.0) as i64);
let allocated = auth_data.get("allocated_gb").and_then(|v| v.as_u64());
let selected_task = auth_data.get("selected_task").and_then(|v| v.as_str());
// GPU (selain)
let gpu_vendor = auth_data.get("gpu").and_then(|g| g.get("vendor")).and_then(|v| v.as_str());
@@ -108,8 +128,8 @@ impl NodeDb {
node_id, ip, node_type, connected_at,
platform, hostname, os, cpu_cores, cpu_model, ram_mb,
gpu_name, gpu_vendor, gpu_backend, vram_total_mb, vram_used_mb, gpu_temp_c, gpu_util_pct,
allocated_gb, has_webgpu
) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16,?17,?18,?19)",
allocated_gb, selected_task, has_webgpu
) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16,?17,?18,?19,?20)",
params![
node_id as i64, ip, node_type, now,
platform, hostname, os,
@@ -124,6 +144,7 @@ impl NodeDb {
gpu_temp.map(|v| v as i64),
gpu_util.map(|v| v as i64),
allocated.map(|v| v as i64),
selected_task,
has_webgpu,
],
).expect("Session insert epäonnistui");
@@ -132,7 +153,7 @@ impl NodeDb {
}
pub fn close_session(&self, node_id: u64) {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let now = chrono::Utc::now().to_rfc3339();
let _ = conn.execute(
"UPDATE node_sessions SET disconnected_at = ?1 WHERE node_id = ?2 AND disconnected_at IS NULL",
@@ -141,7 +162,7 @@ impl NodeDb {
}
pub fn increment_tasks(&self, node_id: u64) {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let _ = conn.execute(
"UPDATE node_sessions SET tasks_completed = tasks_completed + 1 WHERE node_id = ?1 AND disconnected_at IS NULL",
params![node_id as i64],
@@ -149,12 +170,12 @@ impl NodeDb {
}
pub fn get_sessions(&self, limit: u32) -> Vec<serde_json::Value> {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let mut stmt = conn.prepare(
"SELECT id, node_id, ip, node_type, connected_at, disconnected_at,
platform, hostname, os, cpu_cores, cpu_model, ram_mb,
gpu_name, gpu_vendor, gpu_backend, vram_total_mb, gpu_temp_c, gpu_util_pct,
allocated_gb, has_webgpu, tasks_completed
allocated_gb, selected_task, has_webgpu, tasks_completed
FROM node_sessions ORDER BY id DESC LIMIT ?1"
).unwrap();
@@ -179,14 +200,15 @@ impl NodeDb {
"gpu_temp_c": row.get::<_, Option<i64>>(16)?,
"gpu_util_pct": row.get::<_, Option<i64>>(17)?,
"allocated_gb": row.get::<_, Option<i64>>(18)?,
"has_webgpu": row.get::<_, Option<bool>>(19)?,
"tasks_completed": row.get::<_, i64>(20)?,
"selected_task": row.get::<_, Option<String>>(19)?,
"has_webgpu": row.get::<_, Option<bool>>(20)?,
"tasks_completed": row.get::<_, i64>(21)?,
}))
}).unwrap().filter_map(|r| r.ok()).collect()
}
pub fn get_pair_results(&self, limit: u32) -> Vec<serde_json::Value> {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let mut stmt = conn.prepare(
"SELECT id, node_id, created_at, en_text, fi_text,
en_tokens, fi_tokens, en_chars_per_token, fi_chars_per_token,
@@ -212,7 +234,7 @@ impl NodeDb {
}
pub fn get_stats(&self) -> serde_json::Value {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let total_sessions: i64 = conn.query_row("SELECT COUNT(*) FROM node_sessions", [], |r| r.get(0)).unwrap_or(0);
let active_sessions: i64 = conn.query_row("SELECT COUNT(*) FROM node_sessions WHERE disconnected_at IS NULL", [], |r| r.get(0)).unwrap_or(0);
@@ -247,7 +269,7 @@ impl NodeDb {
overhead: f64,
duration_ms: f64,
) {
let conn = self.conn.lock().unwrap();
let conn = self.conn.lock().unwrap_or_else(|e| e.into_inner());
let now = chrono::Utc::now().to_rfc3339();
let _ = conn.execute(
"INSERT INTO pair_results (