distribution

package
v0.0.0-...-3770506 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assignment

type Assignment struct {
	LeadID     string    `json:"lead_id"`
	BrokerID   string    `json:"broker_id"`
	TenantID   string    `json:"tenant_id"`
	Strategy   Strategy  `json:"strategy"`
	Score      float64   `json:"score"`
	Reason     string    `json:"reason"`
	AssignedAt time.Time `json:"assigned_at"`
	Lead       *Lead     `json:"lead,omitempty"`
	Broker     *Broker   `json:"broker,omitempty"`
}

Assignment represents a lead-broker assignment

type Broker

type Broker struct {
	ID           string    `json:"id"`
	TenantID     string    `json:"tenant_id"`
	Name         string    `json:"name"`
	Status       string    `json:"status"`
	Capacity     int       `json:"capacity"`
	CurrentLoad  int       `json:"current_load"`
	Performance  float64   `json:"performance_score"`
	Specialties  []string  `json:"specialties"`
	Regions      []string  `json:"regions"`
	Tier         string    `json:"tier"`
	LastActiveAt time.Time `json:"last_active_at"`
	Availability bool      `json:"availability"`
}

Broker represents a broker in the distribution system

type Config

type Config struct {
	DefaultStrategy Strategy
	MaxQueueSize    int
	MaxWaitTime     time.Duration
	Enabled         bool
}

Config holds distribution service configuration

type DistributionStrategy

type DistributionStrategy interface {
	Name() Strategy
	Assign(ctx context.Context, lead *Lead, brokers []*Broker) (*Assignment, error)
	Priority() int
	Enabled() bool
}

DistributionStrategy interface for different assignment strategies

type Lead

type Lead struct {
	ID         string                 `json:"id"`
	TenantID   string                 `json:"tenant_id"`
	Name       string                 `json:"name"`
	Email      string                 `json:"email"`
	Phone      string                 `json:"phone"`
	Source     string                 `json:"source"`
	Priority   int                    `json:"priority"`
	Score      int                    `json:"score"`
	Properties map[string]interface{} `json:"properties"`
	Tags       []string               `json:"tags"`
	CreatedAt  time.Time              `json:"created_at"`
	Region     string                 `json:"region"`
	Urgency    string                 `json:"urgency"`
	Budget     float64                `json:"budget"`
	Interests  []string               `json:"interests"`
}

Lead represents a lead to be distributed

type MLOptimizedStrategy

type MLOptimizedStrategy struct {
	// contains filtered or unexported fields
}

MLOptimizedStrategy uses machine learning to optimize assignments

func (*MLOptimizedStrategy) Assign

func (s *MLOptimizedStrategy) Assign(ctx context.Context, lead *Lead, brokers []*Broker) (*Assignment, error)

func (*MLOptimizedStrategy) Enabled

func (s *MLOptimizedStrategy) Enabled() bool

func (*MLOptimizedStrategy) Name

func (s *MLOptimizedStrategy) Name() Strategy

func (*MLOptimizedStrategy) Priority

func (s *MLOptimizedStrategy) Priority() int

type PerformanceBasedStrategy

type PerformanceBasedStrategy struct{}

PerformanceBasedStrategy assigns leads based on broker performance scores

func (*PerformanceBasedStrategy) Assign

func (s *PerformanceBasedStrategy) Assign(ctx context.Context, lead *Lead, brokers []*Broker) (*Assignment, error)

func (*PerformanceBasedStrategy) Enabled

func (s *PerformanceBasedStrategy) Enabled() bool

func (*PerformanceBasedStrategy) Name

func (*PerformanceBasedStrategy) Priority

func (s *PerformanceBasedStrategy) Priority() int

type PriorityQueue

type PriorityQueue []*QueueItem

PriorityQueue implements heap.Interface for lead prioritization

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue manages the lead distribution queue with priority support

func NewQueue

func NewQueue(maxSize int) *Queue

NewQueue creates a new lead distribution queue

func (*Queue) Clear

func (q *Queue) Clear()

Clear removes all leads from the queue

func (*Queue) Dequeue

func (q *Queue) Dequeue() (*Lead, error)

Dequeue removes and returns the highest priority lead from the queue

func (*Queue) Enqueue

func (q *Queue) Enqueue(lead *Lead) error

Enqueue adds a lead to the queue with appropriate priority

func (*Queue) GetLeadsByPriority

func (q *Queue) GetLeadsByPriority() map[int][]*Lead

GetLeadsByPriority returns all leads grouped by priority level

func (*Queue) GetMetrics

func (q *Queue) GetMetrics() *QueueMetrics

GetMetrics returns queue performance metrics

func (*Queue) GetQueueSnapshot

func (q *Queue) GetQueueSnapshot() *QueueSnapshot

GetQueueSnapshot returns a snapshot of the current queue state

func (*Queue) GetWaitTimes

func (q *Queue) GetWaitTimes() []time.Duration

GetWaitTimes returns wait times for all leads currently in queue

func (*Queue) IsEmpty

func (q *Queue) IsEmpty() bool

IsEmpty returns true if the queue is empty

func (*Queue) IsFull

func (q *Queue) IsFull() bool

IsFull returns true if the queue is at maximum capacity

func (*Queue) Peek

func (q *Queue) Peek() (*Lead, error)

Peek returns the highest priority lead without removing it

func (*Queue) RemoveLead

func (q *Queue) RemoveLead(leadID string) (*Lead, error)

RemoveLead removes a specific lead from the queue (for cancellations)

func (*Queue) Size

func (q *Queue) Size() int

Size returns the current number of leads in the queue

func (*Queue) UpdatePriority

func (q *Queue) UpdatePriority(leadID string, newPriority int) error

UpdatePriority updates the priority of a lead in the queue

type QueueItem

type QueueItem struct {
	Lead       *Lead
	Priority   int
	Index      int
	EnqueuedAt time.Time
}

QueueItem represents an item in the priority queue

type QueueMetrics

type QueueMetrics struct {
	TotalEnqueued   int64
	TotalDequeued   int64
	TotalRejected   int64
	AverageWaitTime time.Duration
	PeakSize        int
	CreatedAt       time.Time
}

QueueMetrics tracks queue performance metrics

type QueueSnapshot

type QueueSnapshot struct {
	Size            int           `json:"size"`
	MaxSize         int           `json:"max_size"`
	Utilization     float64       `json:"utilization"`
	Metrics         *QueueMetrics `json:"metrics"`
	LeadsByPriority map[int]int   `json:"leads_by_priority"`
	AverageWaitTime time.Duration `json:"average_wait_time"`
	OldestLeadAge   time.Duration `json:"oldest_lead_age"`
}

QueueSnapshot represents a point-in-time view of the queue

type RegionalStrategy

type RegionalStrategy struct{}

RegionalStrategy assigns leads based on geographical proximity

func (*RegionalStrategy) Assign

func (s *RegionalStrategy) Assign(ctx context.Context, lead *Lead, brokers []*Broker) (*Assignment, error)

func (*RegionalStrategy) Enabled

func (s *RegionalStrategy) Enabled() bool

func (*RegionalStrategy) Name

func (s *RegionalStrategy) Name() Strategy

func (*RegionalStrategy) Priority

func (s *RegionalStrategy) Priority() int

type RoundRobinStrategy

type RoundRobinStrategy struct {
	// contains filtered or unexported fields
}

RoundRobinStrategy distributes leads evenly across available brokers

func (*RoundRobinStrategy) Assign

func (s *RoundRobinStrategy) Assign(ctx context.Context, lead *Lead, brokers []*Broker) (*Assignment, error)

func (*RoundRobinStrategy) Enabled

func (s *RoundRobinStrategy) Enabled() bool

func (*RoundRobinStrategy) Name

func (s *RoundRobinStrategy) Name() Strategy

func (*RoundRobinStrategy) Priority

func (s *RoundRobinStrategy) Priority() int

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service handles lead distribution logic

func NewService

func NewService(config *Config, mlSvc *ml.Service, slaManager *sla.Manager, antiAbuse *antiabuse.Service, eventBus *events.Bus) (*Service, error)

NewService creates a new distribution service

func (*Service) AssignLead

func (s *Service) AssignLead(ctx context.Context, leadID, brokerID string, force bool) (*Assignment, error)

AssignLead manually assigns a lead to a broker

func (*Service) DistributeLead

func (s *Service) DistributeLead(ctx context.Context, lead *Lead) error

DistributeLead adds a lead to the distribution queue

func (*Service) GetQueueStats

func (s *Service) GetQueueStats() map[string]interface{}

GetQueueStats returns current queue statistics

func (*Service) IsReady

func (s *Service) IsReady() bool

IsReady returns whether the service is ready to process leads

func (*Service) Rebalance

func (s *Service) Rebalance(ctx context.Context, strategy string, params map[string]interface{}) error

Rebalance triggers queue rebalancing

func (*Service) StartQueueProcessor

func (s *Service) StartQueueProcessor(ctx context.Context)

StartQueueProcessor starts the background queue processing

type SpecialtyBasedStrategy

type SpecialtyBasedStrategy struct{}

SpecialtyBasedStrategy matches leads with brokers based on specializations

func (*SpecialtyBasedStrategy) Assign

func (s *SpecialtyBasedStrategy) Assign(ctx context.Context, lead *Lead, brokers []*Broker) (*Assignment, error)

func (*SpecialtyBasedStrategy) Enabled

func (s *SpecialtyBasedStrategy) Enabled() bool

func (*SpecialtyBasedStrategy) Name

func (s *SpecialtyBasedStrategy) Name() Strategy

func (*SpecialtyBasedStrategy) Priority

func (s *SpecialtyBasedStrategy) Priority() int

type Strategy

type Strategy string

Strategy represents different lead distribution strategies

const (
	RoundRobin       Strategy = "round_robin"
	PerformanceBased Strategy = "performance_based"
	MLOptimized      Strategy = "ml_optimized"
	Regional         Strategy = "regional"
	SpecialtyBased   Strategy = "specialty_based"
)

type StrategySelector

type StrategySelector struct {
	// contains filtered or unexported fields
}

StrategySelector selects the best strategy for a given lead

func NewStrategySelector

func NewStrategySelector(strategies map[Strategy]DistributionStrategy) *StrategySelector

func (*StrategySelector) SelectBestStrategy

func (ss *StrategySelector) SelectBestStrategy(lead *Lead, brokers []*Broker) DistributionStrategy

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL