This commit is contained in:
2025-03-16 09:23:51 +01:00
parent 6a555a5fe3
commit 6e7843f368
10 changed files with 105 additions and 61 deletions

View File

@@ -1,10 +1,10 @@
module actionprocessor
import freeflowuniverse.herolib.circles.core.db
import freeflowuniverse.herolib.circles.mcc.db
import freeflowuniverse.herolib.circles.actions.db
import freeflowuniverse.herolib.circles.base
import freeflowuniverse.herolib.circles.core.db as core_db
import freeflowuniverse.herolib.circles.mcc.db as mcc_db
import freeflowuniverse.herolib.circles.actions.db as actions_db
import freeflowuniverse.herolib.circles.base { SessionState }
import freeflowuniverse.herolib.core.texttools
__global (
@@ -18,13 +18,13 @@ __global (
pub struct CircleCoordinator {
pub mut:
name string //is a unique name on planetary scale is a dns name
agents &db.AgentDB
circles &db.CircleDB
names &db.NameDB
mails &db.MailDB
calendar &db.CalendarDB
jobs &db.JobDB
session_state base.SessionState
agents &core_db.AgentDB
circles &core_db.CircleDB
names &core_db.NameDB
mails &mcc_db.MailDB
calendar &mcc_db.CalendarDB
jobs &actions_db.JobDB
session_state SessionState
}
@@ -47,7 +47,12 @@ pub fn new(args_ CircleCoordinatorArgs) !&CircleCoordinator {
return c
}
mut session_state:=models.new_session(name: args.name, pubkey: args.pubkey, addr: args.addr, path: args.path)!
mut session_state := base.new_session(base.StateArgs{
name: args.name
pubkey: args.pubkey
addr: args.addr
path: args.path
})!
// os.mkdir_all(mypath)!
// Create the directories if they don't exist// SHOULD BE AUTOMATIC
@@ -58,12 +63,12 @@ pub fn new(args_ CircleCoordinatorArgs) !&CircleCoordinator {
// Initialize the db handlers with proper ourdb instances
mut agent_db := core.new_agentdb(session_state)!
mut circle_db := core.new_circledb(session_state)!
mut name_db := core.new_namedb(session_state)!
mut mail_db := mcc.new_maildb(session_state)!
mut calendar_db := mcc.new_calendardb(session_state)!
mut job_db := actions.new_jobdb(session_state)!
mut agent_db := core_db.new_agentdb(session_state) or { return error('Failed to initialize agent_db: ${err}') }
mut circle_db := core_db.new_circledb(session_state) or { return error('Failed to initialize circle_db: ${err}') }
mut name_db := core_db.new_namedb(session_state) or { return error('Failed to initialize name_db: ${err}') }
mut mail_db := mcc_db.new_maildb(session_state) or { return error('Failed to initialize mail_db: ${err}') }
mut calendar_db := mcc_db.new_calendardb(session_state) or { return error('Failed to initialize calendar_db: ${err}') }
mut job_db := actions_db.new_jobdb(session_state) or { return error('Failed to initialize job_db: ${err}') }
mut cm := &CircleCoordinator{
agents: &agent_db

View File

@@ -64,19 +64,19 @@ pub fn (mut m JobDB) delete_by_guid(guid string) ! {
// get_by_actor retrieves all jobs for a specific actor
pub fn (mut m JobDB) get_by_actor(actor string) ![]Job {
// Get all jobs with this actor
// Get all jobs with this actor using the fixed getall_by_prefix method
return m.db.getall_by_prefix('actor', actor)!
}
// get_by_circle retrieves all jobs for a specific circle
pub fn (mut m JobDB) get_by_circle(circle string) ![]Job {
// Get all jobs with this circle
// Get all jobs with this circle using the fixed getall_by_prefix method
return m.db.getall_by_prefix('circle', circle)!
}
// get_by_context retrieves all jobs for a specific context
pub fn (mut m JobDB) get_by_context(context string) ![]Job {
// Get all jobs with this context
// Get all jobs with this context using the fixed getall_by_prefix method
return m.db.getall_by_prefix('context', context)!
}

View File

@@ -3,7 +3,7 @@ module db
import os
import rand
import freeflowuniverse.herolib.circles.actionprocessor
import freeflowuniverse.herolib.circles.actions.models
import freeflowuniverse.herolib.circles.actions.models { Status, JobStatus }
import freeflowuniverse.herolib.data.ourtime
fn test_job_db() {
@@ -67,12 +67,9 @@ fn test_job_db() {
println('Adding job 1')
job1 = runner.jobs.set(job1)!
// Explicitly set different IDs for each job to avoid overwriting
job2.id = 1 // Set a different ID for job2
println('Adding job 2')
job2 = runner.jobs.set(job2)!
job3.id = 2 // Set a different ID for job3
println('Adding job 3')
job3 = runner.jobs.set(job3)!
@@ -124,7 +121,31 @@ fn test_job_db() {
// Test get_by_actor method
println('Testing get_by_actor method')
// Debug: Print all jobs and their actors
all_jobs_debug := runner.jobs.getall()!
println('Debug - All jobs:')
for job in all_jobs_debug {
println('Job ID: ${job.id}, GUID: ${job.guid}, Actor: ${job.actor}')
}
// Debug: Print the index keys for job1 and job2
println('Debug - Index keys for job1:')
for k, v in job1.index_keys() {
println('${k}: ${v}')
}
println('Debug - Index keys for job2:')
for k, v in job2.index_keys() {
println('${k}: ${v}')
}
vm_manager_jobs := runner.jobs.get_by_actor('vm_manager')!
println('Found ${vm_manager_jobs.len} jobs with actor "vm_manager"')
for i, job in vm_manager_jobs {
println('VM Manager Job ${i}: ID=${job.id}, GUID=${job.guid}')
}
// Now we should find both jobs with actor "vm_manager"
assert vm_manager_jobs.len == 2
assert vm_manager_jobs[0].guid in ['job-1', 'job-2']
assert vm_manager_jobs[1].guid in ['job-1', 'job-2']
@@ -151,12 +172,12 @@ fn test_job_db() {
// Test update_job_status method
println('Testing update_job_status method')
updated_job1 := runner.jobs.update_job_status('job-1', .running)!
assert updated_job1.status.status == .running
updated_job1 := runner.jobs.update_job_status('job-1', JobStatus{status: Status.running})!
assert updated_job1.status.status == Status.running
// Verify the status was updated in the database
status_updated_job1 := runner.jobs.get_by_guid('job-1')!
assert status_updated_job1.status.status == .running
assert status_updated_job1.status.status == Status.running
// Test delete functionality
println('Testing delete functionality')

View File

@@ -172,10 +172,10 @@ pub fn job_loads(data []u8) !Job {
job.ignore_error = d.get_bool()!
// Decode ignore_error_codes array
error_codes_len := d.get_u8()!
error_codes_len := d.get_u16()!
job.ignore_error_codes = []u16{len: int(error_codes_len)}
for i in 0 .. error_codes_len {
job.ignore_error_codes[i] = d.get_u8()!
job.ignore_error_codes[i] = d.get_u16()!
}
job.debug = d.get_bool()!

View File

@@ -1,8 +1,8 @@
module base
import freeflowuniverse.herolib.circles.core.models { agent_loads, Agent, circle_loads, Circle, name_loads, Name }
import freeflowuniverse.herolib.circles.mcc.models { Email, email_loads, CalendarEvent, calendar_event_loads }
import freeflowuniverse.herolib.circles.actions.models { Job, job_loads }
import freeflowuniverse.herolib.circles.core.models as core_models
import freeflowuniverse.herolib.circles.mcc.models as mcc_models
import freeflowuniverse.herolib.circles.actions.models as actions_models
pub struct DBHandler[T] {
pub mut:
@@ -43,28 +43,28 @@ pub fn (mut m DBHandler[T]) get(id u32) !T {
}
//THIS IS SUPER ANNOYING AND NOT NICE
$if T is Agent {
mut o:= agent_loads(item_data)!
$if T is core_models.Agent {
mut o:= core_models.agent_loads(item_data)!
o.id = id
return o
} $else $if T is Circle {
mut o:= circle_loads(item_data)!
} $else $if T is core_models.Circle {
mut o:= core_models.circle_loads(item_data)!
o.id = id
return o
} $else $if T is Name {
mut o:= name_loads(item_data)!
} $else $if T is core_models.Name {
mut o:= core_models.name_loads(item_data)!
o.id = id
return o
} $else $if T is Email {
mut o:= email_loads(item_data)!
} $else $if T is mcc_models.Email {
mut o:= mcc_models.email_loads(item_data)!
o.id = id
return o
} $else $if T is CalendarEvent {
mut o:= calendar_event_loads(item_data)!
} $else $if T is mcc_models.CalendarEvent {
mut o:= mcc_models.calendar_event_loads(item_data)!
o.id = id
return o
} $else $if T is Job {
mut o:= job_loads(item_data)!
} $else $if T is actions_models.Job {
mut o:= actions_models.job_loads(item_data)!
o.id = id
return o
} $else {
@@ -168,28 +168,46 @@ pub fn (mut m DBHandler[T]) getall() ![]T {
pub fn (mut m DBHandler[T]) list_by_prefix(key_field string, prefix_value string) ![]u32 {
// Create the prefix for the radix tree
prefix := '${m.prefix}:${key_field}:${prefix_value}'
println('DEBUG: Searching with prefix: ${prefix}')
// Use RadixTree's list method to get all keys with this prefix
keys := m. session_state.dbs.db_meta_core.list(prefix)!
keys := m.session_state.dbs.db_meta_core.list(prefix)!
println('DEBUG: Found ${keys.len} keys matching prefix')
for i, key in keys {
println('DEBUG: Key ${i}: ${key}')
}
// Extract IDs from the values stored in these keys
mut ids := []u32{}
mut seen := map[u32]bool{}
for key in keys {
if id_bytes := m.session_state.dbs.db_meta_core.get(key) {
id_str := id_bytes.bytestr()
if id_str.len > 0 {
ids << id_str.u32()
id := id_str.u32()
println('DEBUG: Found ID ${id} for key ${key}')
// Only add the ID if we haven't seen it before
if !seen[id] {
ids << id
seen[id] = true
}
}
}
}
println('DEBUG: Returning ${ids.len} unique IDs')
return ids
}
// getall_by_prefix returns all items that match a specific prefix pattern
pub fn (mut m DBHandler[T]) getall_by_prefix(key_field string, prefix_value string) ![]T {
// Get all IDs that match the prefix
ids := m.list_by_prefix(key_field, prefix_value)!
// Get all items with these IDs
mut items := []T{}
for id in m.list_by_prefix(key_field, prefix_value)! {
for id in ids {
items << m.get(id)!
}
return items

View File

@@ -1,7 +1,7 @@
module dbmodule core
module db
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState }
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState, new_dbhandler }
import freeflowuniverse.herolib.circles.core.models { Agent, AgentService, AgentServiceAction, AgentState }
@@ -13,7 +13,7 @@ pub mut:
pub fn new_agentdb(session_state SessionState) !AgentDB {
return AgentDB{
db:models.new_dbhandler[Agent]('agent', session_state)
db: new_dbhandler[Agent]('agent', session_state)
}
}

View File

@@ -1,7 +1,7 @@
module db
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState }
import freeflowuniverse.herolib.circles.core.models { Circle, Member }
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState, new_dbhandler }
import freeflowuniverse.herolib.circles.core.models { Circle, Member, Role }
@[heap]
pub struct CircleDB {
@@ -11,7 +11,7 @@ pub mut:
pub fn new_circledb(session_state SessionState) !CircleDB {
return CircleDB{
db: models.new_dbhandler[Circle]('circle', session_state)
db: new_dbhandler[Circle]('circle', session_state)
}
}

View File

@@ -1,6 +1,6 @@
module dbmodule core
module db
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState }
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState, new_dbhandler }
import freeflowuniverse.herolib.circles.core.models { Name, Record, RecordType }
@[heap]
@@ -11,7 +11,7 @@ pub mut:
pub fn new_namedb(session_state SessionState) !NameDB {
return NameDB{
db: models.new_dbhandler[Name]('name', session_state)
db: new_dbhandler[Name]('name', session_state)
}
}

View File

@@ -1,6 +1,6 @@
module db
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState }
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState, new_dbhandler }
import freeflowuniverse.herolib.circles.mcc.models { CalendarEvent, calendar_event_loads }
@[heap]
@@ -11,7 +11,7 @@ pub mut:
pub fn new_calendardb(session_state SessionState) !CalendarDB {
return CalendarDB{
db: models.new_dbhandler[CalendarEvent]('calendar', session_state)
db: new_dbhandler[CalendarEvent]('calendar', session_state)
}
}

View File

@@ -1,6 +1,6 @@
module db
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState }
import freeflowuniverse.herolib.circles.base { DBHandler, SessionState, new_dbhandler }
import freeflowuniverse.herolib.circles.mcc.models { Email, email_loads }
@[heap]
@@ -11,7 +11,7 @@ pub mut:
pub fn new_maildb(session_state SessionState) !MailDB {
return MailDB{
db: models.new_dbhandler[Email]('mail', session_state)
db: new_dbhandler[Email]('mail', session_state)
}
}