Help us shape the future of Go! Take the 2026 Go Developer Survey

idle

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package idle provides advanced idle detection and policy management

Package idle provides advanced idle detection and policy management

Package idle provides advanced idle detection and policy management

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AWSInstanceManager

type AWSInstanceManager interface {
	HibernateInstance(name string) error
	ResumeInstance(name string) error
	StopInstance(name string) error
	StartInstance(name string) error
	GetInstanceNames() ([]string, error)
	GetInstanceID(name string) (string, error) // Get AWS instance ID from instance name
}

AWSInstanceManager defines the interface for AWS instance operations needed by the scheduler

type AWSManagerAdapter

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

AWSManagerAdapter adapts an AWS Manager to the AWSInstanceManager interface This allows the scheduler to work with the real AWS manager without direct dependencies

func NewAWSManagerAdapter

func NewAWSManagerAdapter(
	hibernateFn func(string) error,
	resumeFn func(string) error,
	stopFn func(string) error,
	startFn func(string) error,
	getInstanceNamesFn func() ([]string, error),
	getInstanceIDFn func(string) (string, error),
) *AWSManagerAdapter

NewAWSManagerAdapter creates an adapter for an AWS manager

func (*AWSManagerAdapter) GetInstanceID

func (a *AWSManagerAdapter) GetInstanceID(name string) (string, error)

func (*AWSManagerAdapter) GetInstanceNames

func (a *AWSManagerAdapter) GetInstanceNames() ([]string, error)

func (*AWSManagerAdapter) HibernateInstance

func (a *AWSManagerAdapter) HibernateInstance(name string) error

func (*AWSManagerAdapter) ResumeInstance

func (a *AWSManagerAdapter) ResumeInstance(name string) error

func (*AWSManagerAdapter) StartInstance

func (a *AWSManagerAdapter) StartInstance(name string) error

func (*AWSManagerAdapter) StopInstance

func (a *AWSManagerAdapter) StopInstance(name string) error

type DailySaving

type DailySaving struct {
	Date             time.Time `json:"date"`
	HibernationHours float64   `json:"hibernation_hours"`
	ActiveHours      float64   `json:"active_hours"`
	Saved            float64   `json:"saved"`
	InstanceCount    int       `json:"instance_count"`
}

DailySaving represents savings for a single day

type DayOfWeek

type DayOfWeek string

DayOfWeek represents a day of the week

const (
	Monday    DayOfWeek = "monday"
	Tuesday   DayOfWeek = "tuesday"
	Wednesday DayOfWeek = "wednesday"
	Thursday  DayOfWeek = "thursday"
	Friday    DayOfWeek = "friday"
	Saturday  DayOfWeek = "saturday"
	Sunday    DayOfWeek = "sunday"
)

type HibernationEvent

type HibernationEvent struct {
	StartTime    time.Time `json:"start_time"`
	EndTime      time.Time `json:"end_time"`
	Duration     float64   `json:"duration_hours"`
	SavedAmount  float64   `json:"saved_amount"`
	TriggerType  string    `json:"trigger_type"` // schedule, idle, manual
	ScheduleName string    `json:"schedule_name,omitempty"`
}

HibernationEvent represents a single hibernation event

type InstanceMetrics

type InstanceMetrics struct {
	InstanceID        string
	AverageCPU        float64
	AverageNetworkBPS float64
	Period            time.Duration
	CollectedAt       time.Time
}

InstanceMetrics represents collected instance metrics

type InstanceSaving

type InstanceSaving struct {
	InstanceID       string  `json:"instance_id"`
	InstanceName     string  `json:"instance_name"`
	InstanceType     string  `json:"instance_type"`
	HourlyRate       float64 `json:"hourly_rate"`
	HibernationHours float64 `json:"hibernation_hours"`
	ActiveHours      float64 `json:"active_hours"`
	TotalSaved       float64 `json:"total_saved"`
	SavingsPercent   float64 `json:"savings_percent"`

	// Hibernation events
	HibernationEvents []HibernationEvent `json:"hibernation_events"`
}

InstanceSaving represents savings for a specific instance

type MetricsCollector

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

MetricsCollector collects and analyzes CloudWatch metrics for idle detection

func NewMetricsCollector

func NewMetricsCollector(awsConfig aws.Config) *MetricsCollector

NewMetricsCollector creates a new metrics collector

func (*MetricsCollector) GetInstanceMetrics

func (mc *MetricsCollector) GetInstanceMetrics(ctx context.Context, instanceID string, duration time.Duration) (*InstanceMetrics, error)

GetInstanceMetrics retrieves comprehensive metrics for an instance

func (*MetricsCollector) IsInstanceIdle

func (mc *MetricsCollector) IsInstanceIdle(ctx context.Context, instanceID string, schedule *Schedule) (bool, error)

IsInstanceIdle checks if an instance has been idle for the specified duration based on CPU, network, and other metrics

type MonthlySaving

type MonthlySaving struct {
	Month            string  `json:"month"` // YYYY-MM
	HibernationHours float64 `json:"hibernation_hours"`
	ActiveHours      float64 `json:"active_hours"`
	Saved            float64 `json:"saved"`
	ProjectedSaving  float64 `json:"projected_saving"`
}

MonthlySaving represents savings for a month

type Period

type Period struct {
	Start time.Time `json:"start"`
	End   time.Time `json:"end"`
	Days  int       `json:"days"`
}

Period represents the reporting period

type PolicyCategory

type PolicyCategory string

PolicyCategory categorizes policy templates

const (
	CategoryAggressive   PolicyCategory = "aggressive"
	CategoryBalanced     PolicyCategory = "balanced"
	CategoryConservative PolicyCategory = "conservative"
	CategoryDevelopment  PolicyCategory = "development"
	CategoryProduction   PolicyCategory = "production"
	CategoryResearch     PolicyCategory = "research"
	CategoryCustom       PolicyCategory = "custom"
)

type PolicyManager

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

PolicyManager manages hibernation policies

func NewPolicyManager

func NewPolicyManager() *PolicyManager

NewPolicyManager creates a new policy manager with default templates

func (*PolicyManager) ApplyTemplate

func (pm *PolicyManager) ApplyTemplate(instanceID string, templateID string) error

ApplyTemplate applies a policy template to an instance

func (*PolicyManager) CreateCustomTemplate

func (pm *PolicyManager) CreateCustomTemplate(name, description string, schedules []Schedule) (*PolicyTemplate, error)

CreateCustomTemplate creates a custom policy template

func (*PolicyManager) GetAppliedTemplates

func (pm *PolicyManager) GetAppliedTemplates(instanceID string) ([]*PolicyTemplate, error)

GetAppliedTemplates returns templates applied to an instance

func (*PolicyManager) GetTemplate

func (pm *PolicyManager) GetTemplate(id string) (*PolicyTemplate, error)

GetTemplate retrieves a policy template

func (*PolicyManager) ListTemplates

func (pm *PolicyManager) ListTemplates() []*PolicyTemplate

ListTemplates returns all available policy templates

func (*PolicyManager) ListTemplatesByCategory

func (pm *PolicyManager) ListTemplatesByCategory(category PolicyCategory) []*PolicyTemplate

ListTemplatesByCategory returns templates filtered by category

func (*PolicyManager) RecommendTemplate

func (pm *PolicyManager) RecommendTemplate(instanceType string, tags map[string]string) (*PolicyTemplate, error)

RecommendTemplate recommends a policy template based on instance characteristics

func (*PolicyManager) RemoveTemplate

func (pm *PolicyManager) RemoveTemplate(instanceID string, templateID string) error

RemoveTemplate removes a policy template from an instance

func (*PolicyManager) SetScheduler

func (pm *PolicyManager) SetScheduler(scheduler *Scheduler)

SetScheduler sets the scheduler for automated policy execution

type PolicyTemplate

type PolicyTemplate struct {
	ID          string            `json:"id"`
	Name        string            `json:"name"`
	Description string            `json:"description"`
	Category    PolicyCategory    `json:"category"`
	Schedules   []Schedule        `json:"schedules"`
	Tags        map[string]string `json:"tags"`

	// Estimated savings
	EstimatedSavingsPercent float64  `json:"estimated_savings_percent"`
	SuitableFor             []string `json:"suitable_for"`

	// Configuration
	AutoApply bool     `json:"auto_apply"`
	Priority  int      `json:"priority"`
	Conflicts []string `json:"conflicts"` // IDs of conflicting templates
}

PolicyTemplate represents a pre-configured hibernation policy

type Recommendation

type Recommendation struct {
	Type        string  `json:"type"`
	Priority    string  `json:"priority"` // high, medium, low
	Description string  `json:"description"`
	Impact      float64 `json:"estimated_monthly_impact"`
	Action      string  `json:"recommended_action"`
}

Recommendation suggests optimization opportunities

type SavingsCalculator

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

SavingsCalculator calculates hibernation savings

func NewSavingsCalculator

func NewSavingsCalculator() *SavingsCalculator

NewSavingsCalculator creates a new savings calculator

func (*SavingsCalculator) AddEvent

func (sc *SavingsCalculator) AddEvent(event HibernationEvent)

AddEvent adds a hibernation event for tracking

func (*SavingsCalculator) GenerateReport

func (sc *SavingsCalculator) GenerateReport(period Period) (*SavingsReport, error)

GenerateReport generates a comprehensive savings report

type SavingsReport

type SavingsReport struct {
	ReportID    string    `json:"report_id"`
	GeneratedAt time.Time `json:"generated_at"`
	Period      Period    `json:"period"`

	// Overall savings
	TotalSaved        float64 `json:"total_saved"`
	ProjectedSavings  float64 `json:"projected_savings"`
	HibernationHours  float64 `json:"hibernation_hours"`
	ActiveHours       float64 `json:"active_hours"`
	SavingsPercentage float64 `json:"savings_percentage"`

	// Instance breakdown
	InstanceSavings []InstanceSaving `json:"instance_savings"`

	// Schedule effectiveness
	SchedulePerformance []SchedulePerformance `json:"schedule_performance"`

	// Recommendations
	Recommendations []Recommendation `json:"recommendations"`

	// Trends
	DailySavings   []DailySaving   `json:"daily_savings"`
	WeeklySavings  []WeeklySaving  `json:"weekly_savings"`
	MonthlySavings []MonthlySaving `json:"monthly_savings"`
}

SavingsReport represents hibernation cost savings analysis

type Schedule

type Schedule struct {
	ID          string       `json:"id"`
	Name        string       `json:"name"`
	Description string       `json:"description"`
	Type        ScheduleType `json:"type"`
	Enabled     bool         `json:"enabled"`
	PolicyID    string       `json:"policy_id,omitempty"` // Track which policy created this schedule

	// Target instances
	TargetInstances []string `json:"target_instances,omitempty"` // Specific instance names, empty means all

	// Time-based scheduling
	StartTime  string      `json:"start_time,omitempty"` // HH:MM format
	EndTime    string      `json:"end_time,omitempty"`   // HH:MM format
	DaysOfWeek []DayOfWeek `json:"days_of_week,omitempty"`
	Timezone   string      `json:"timezone,omitempty"`

	// Idle-based scheduling
	IdleMinutes      int     `json:"idle_minutes,omitempty"`
	CPUThreshold     float64 `json:"cpu_threshold,omitempty"`
	MemoryThreshold  float64 `json:"memory_threshold,omitempty"`
	NetworkThreshold float64 `json:"network_threshold,omitempty"`

	// Actions
	HibernateAction string `json:"hibernate_action"` // hibernate, stop, terminate
	WakeAction      string `json:"wake_action"`      // resume, start, none

	// Advanced options
	GracePeriodMinutes int      `json:"grace_period_minutes"`
	IgnoreTags         []string `json:"ignore_tags"`
	RequireTags        []string `json:"require_tags"`

	// Cost tracking
	EstimatedMonthlySavings float64   `json:"estimated_monthly_savings"`
	LastExecuted            time.Time `json:"last_executed"`
	TotalSavings            float64   `json:"total_savings"`
}

Schedule represents a hibernation schedule

type ScheduleExecution

type ScheduleExecution struct {
	ScheduleID string
	StartTime  time.Time
	NextRun    time.Time
	IsActive   bool
}

ScheduleExecution tracks active schedule execution

type SchedulePerformance

type SchedulePerformance struct {
	ScheduleID        string  `json:"schedule_id"`
	ScheduleName      string  `json:"schedule_name"`
	ExecutionCount    int     `json:"execution_count"`
	SuccessRate       float64 `json:"success_rate"`
	TotalSaved        float64 `json:"total_saved"`
	AverageSaving     float64 `json:"average_saving"`
	EstimatedVsActual float64 `json:"estimated_vs_actual"` // Percentage difference
}

SchedulePerformance tracks how well schedules are performing

type ScheduleType

type ScheduleType string

ScheduleType defines the type of hibernation schedule

const (
	ScheduleTypeDaily     ScheduleType = "daily"
	ScheduleTypeWeekly    ScheduleType = "weekly"
	ScheduleTypeWorkHours ScheduleType = "work_hours"
	ScheduleTypeCustom    ScheduleType = "custom"
	ScheduleTypeIdle      ScheduleType = "idle"
)

type Scheduler

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

Scheduler manages hibernation schedules

func NewScheduler

func NewScheduler(awsManager AWSInstanceManager, metricsCollector *MetricsCollector) *Scheduler

NewScheduler creates a new hibernation scheduler

func (*Scheduler) AddSchedule

func (s *Scheduler) AddSchedule(schedule *Schedule) error

AddSchedule adds a new schedule

func (*Scheduler) AssignScheduleToInstance

func (s *Scheduler) AssignScheduleToInstance(scheduleID, instanceName string) error

AssignScheduleToInstance assigns a schedule to a specific instance

func (*Scheduler) DeleteSchedule

func (s *Scheduler) DeleteSchedule(id string) error

DeleteSchedule removes a schedule

func (*Scheduler) GetInstanceSchedules

func (s *Scheduler) GetInstanceSchedules(instanceName string) []*Schedule

GetInstanceSchedules returns all schedules assigned to an instance

func (*Scheduler) GetSchedule

func (s *Scheduler) GetSchedule(id string) (*Schedule, error)

GetSchedule retrieves a schedule

func (*Scheduler) ListSchedules

func (s *Scheduler) ListSchedules() []*Schedule

ListSchedules returns all schedules

func (*Scheduler) RemoveScheduleFromInstance

func (s *Scheduler) RemoveScheduleFromInstance(scheduleID, instanceName string) error

RemoveScheduleFromInstance removes a schedule from a specific instance

func (*Scheduler) Start

func (s *Scheduler) Start()

Start begins the scheduler

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the scheduler

func (*Scheduler) UpdateSchedule

func (s *Scheduler) UpdateSchedule(id string, updates *Schedule) error

UpdateSchedule updates an existing schedule

type WeeklySaving

type WeeklySaving struct {
	WeekStart        time.Time `json:"week_start"`
	HibernationHours float64   `json:"hibernation_hours"`
	ActiveHours      float64   `json:"active_hours"`
	Saved            float64   `json:"saved"`
	Trend            string    `json:"trend"` // increasing, decreasing, stable
}

WeeklySaving represents savings for a week

Jump to

Keyboard shortcuts

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