repository

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package repository provides database abstraction for the perf-analysis service.

Package repository provides database abstraction for the perf-analysis service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoMigrateModels

func AutoMigrateModels(db *gorm.DB) error

AutoMigrateModels runs GORM AutoMigrate for all known models. AutoMigrate only adds missing columns and indexes; it does NOT drop columns or change existing column types, so it is safe for production use.

func NewGormDB

func NewGormDB(cfg *DBConfig) (*gorm.DB, error)

NewGormDB creates a new GORM database connection based on configuration.

Types

type AnalysisSuggestion

type AnalysisSuggestion struct {
	ID           int64     `gorm:"column:id;primaryKey;autoIncrement"`
	TID          string    `gorm:"column:tid;type:varchar(64);index"`
	Namespace    string    `gorm:"column:namespace;type:varchar(256)"`
	Suggestion   string    `gorm:"column:suggestion;type:text"`
	Func         string    `gorm:"column:func;type:varchar(512)"`
	CallStack    JSONField `gorm:"column:call_stack;type:json"`
	AISuggestion string    `gorm:"column:ai_suggestion;type:text"`
	CreatedAt    time.Time `gorm:"column:created_at;autoCreateTime"`
	UpdatedAt    time.Time `gorm:"column:updated_at;autoUpdateTime"`
}

AnalysisSuggestion represents the analysis_suggestions table.

func (AnalysisSuggestion) TableName

func (AnalysisSuggestion) TableName() string

TableName returns the table name for AnalysisSuggestion.

func (*AnalysisSuggestion) ToModel

func (s *AnalysisSuggestion) ToModel() model.Suggestion

ToModel converts AnalysisSuggestion to model.Suggestion.

type AnalysisSuggestionRule

type AnalysisSuggestionRule struct {
	ID                int64   `gorm:"column:id;primaryKey;autoIncrement"`
	Type              string  `gorm:"column:type;type:varchar(64)"`
	Operation         string  `gorm:"column:operation;type:varchar(64)"`
	Target            string  `gorm:"column:target;type:varchar(512)"`
	TargetType        string  `gorm:"column:target_type;type:varchar(64)"`
	Threshold         float64 `gorm:"column:threshold"`
	SuggestionContent string  `gorm:"column:suggestion_content;type:text"`
	Deleted           *int64  `gorm:"column:deleted"`
}

AnalysisSuggestionRule represents the analysis_suggestion_rules table.

func (AnalysisSuggestionRule) TableName

func (AnalysisSuggestionRule) TableName() string

TableName returns the table name for AnalysisSuggestionRule.

func (*AnalysisSuggestionRule) ToModel

ToModel converts AnalysisSuggestionRule to model.SuggestionRule.

type DBConfig

type DBConfig struct {
	Type     string `mapstructure:"type"` // postgres or mysql
	Host     string `mapstructure:"host"`
	Port     int    `mapstructure:"port"`
	Database string `mapstructure:"database"`
	User     string `mapstructure:"user"`
	Password string `mapstructure:"password"`
	MaxConns int    `mapstructure:"max_conns"`
}

DBConfig holds database configuration.

type DBType

type DBType string

DBType represents the database type.

const (
	DBTypePostgres DBType = "postgres"
	DBTypeMySQL    DBType = "mysql"
)

type GeneralAnalysisResult

type GeneralAnalysisResult struct {
	ID             int64     `gorm:"column:id;primaryKey;autoIncrement"`
	TID            string    `gorm:"column:tid;type:varchar(64);uniqueIndex"`
	ContainersInfo JSONField `gorm:"column:containers_info;type:json"`
	Result         JSONField `gorm:"column:result;type:json"`
	Version        string    `gorm:"column:version;type:varchar(32)"`
}

GeneralAnalysisResult represents the general_analysis_results table.

func (GeneralAnalysisResult) TableName

func (GeneralAnalysisResult) TableName() string

TableName returns the table name for GeneralAnalysisResult.

func (*GeneralAnalysisResult) ToModel

ToModel converts GeneralAnalysisResult to model.AnalysisResult.

type GormMasterTaskRepository

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

GormMasterTaskRepository implements MasterTaskRepository using GORM.

func NewGormMasterTaskRepository

func NewGormMasterTaskRepository(db *gorm.DB) *GormMasterTaskRepository

NewGormMasterTaskRepository creates a new GormMasterTaskRepository.

func (*GormMasterTaskRepository) CheckAndCompleteIfReady

func (r *GormMasterTaskRepository) CheckAndCompleteIfReady(ctx context.Context, masterTID string) error

CheckAndCompleteIfReady checks if all sub-tasks are done and updates master task status.

func (*GormMasterTaskRepository) GetIncompleteSubTaskCount

func (r *GormMasterTaskRepository) GetIncompleteSubTaskCount(ctx context.Context, masterTID string) (int, error)

GetIncompleteSubTaskCount returns the count of incomplete sub-tasks.

func (*GormMasterTaskRepository) GetMasterTask

func (r *GormMasterTaskRepository) GetMasterTask(ctx context.Context, masterTID string) (*MasterTask, error)

GetMasterTask retrieves a master task by its UUID.

func (*GormMasterTaskRepository) UpdateMasterTaskStatus

func (r *GormMasterTaskRepository) UpdateMasterTaskStatus(ctx context.Context, masterTID string, status model.AnalysisStatus) error

UpdateMasterTaskStatus updates the analysis status of a master task.

func (*GormMasterTaskRepository) UpdateMasterTaskSuggestions

func (r *GormMasterTaskRepository) UpdateMasterTaskSuggestions(ctx context.Context, masterTID string, resourceType string, suggestions *model.SuggestionGroup) error

UpdateMasterTaskSuggestions updates the suggestions for a master task atomically.

type GormResultRepository

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

GormResultRepository implements ResultRepository using GORM.

func NewGormResultRepository

func NewGormResultRepository(db *gorm.DB, version string) *GormResultRepository

NewGormResultRepository creates a new GormResultRepository.

func (*GormResultRepository) GetResultByTaskUUID

func (r *GormResultRepository) GetResultByTaskUUID(ctx context.Context, taskUUID string) (*model.AnalysisResult, error)

GetResultByTaskUUID retrieves the analysis result for a task.

func (*GormResultRepository) SaveResult

func (r *GormResultRepository) SaveResult(ctx context.Context, result *model.AnalysisResult) error

SaveResult saves an analysis result to the database.

func (*GormResultRepository) UpdateResult

func (r *GormResultRepository) UpdateResult(ctx context.Context, result *model.AnalysisResult) error

UpdateResult updates an existing analysis result.

type GormSuggestionRepository

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

GormSuggestionRepository implements SuggestionRepository using GORM.

func NewGormSuggestionRepository

func NewGormSuggestionRepository(db *gorm.DB) *GormSuggestionRepository

NewGormSuggestionRepository creates a new GormSuggestionRepository.

func (*GormSuggestionRepository) GetAnalysisRules

func (r *GormSuggestionRepository) GetAnalysisRules(ctx context.Context) ([]model.SuggestionRule, error)

GetAnalysisRules retrieves all active analysis rules.

func (*GormSuggestionRepository) GetSuggestionsByTaskUUID

func (r *GormSuggestionRepository) GetSuggestionsByTaskUUID(ctx context.Context, taskUUID string) ([]model.Suggestion, error)

GetSuggestionsByTaskUUID retrieves suggestions for a task.

func (*GormSuggestionRepository) SaveSuggestions

func (r *GormSuggestionRepository) SaveSuggestions(ctx context.Context, suggestions []model.Suggestion) error

SaveSuggestions saves multiple suggestions to the database.

type GormTaskRepository

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

GormTaskRepository implements TaskRepository using GORM.

func NewGormTaskRepository

func NewGormTaskRepository(db *gorm.DB) *GormTaskRepository

NewGormTaskRepository creates a new GormTaskRepository.

func (*GormTaskRepository) CreateTask

func (r *GormTaskRepository) CreateTask(ctx context.Context, task *model.Task) error

CreateTask creates a new task record in the database.

func (*GormTaskRepository) GetCompletedTasks

func (r *GormTaskRepository) GetCompletedTasks(ctx context.Context) ([]model.Task, error)

GetCompletedTasks retrieves tasks that have completed analysis.

func (*GormTaskRepository) GetPendingTasks

func (r *GormTaskRepository) GetPendingTasks(ctx context.Context, limit int) ([]*model.Task, error)

GetPendingTasks retrieves tasks that are pending analysis.

func (*GormTaskRepository) GetTaskByID

func (r *GormTaskRepository) GetTaskByID(ctx context.Context, id int64) (*model.Task, error)

GetTaskByID retrieves a task by its ID.

func (*GormTaskRepository) GetTaskByUUID

func (r *GormTaskRepository) GetTaskByUUID(ctx context.Context, uuid string) (*model.Task, error)

GetTaskByUUID retrieves a task by its UUID.

func (*GormTaskRepository) LockTaskForAnalysis

func (r *GormTaskRepository) LockTaskForAnalysis(ctx context.Context, id int64) (bool, error)

LockTaskForAnalysis attempts to lock a task for analysis using FOR UPDATE.

func (*GormTaskRepository) UpdateAnalysisStatus

func (r *GormTaskRepository) UpdateAnalysisStatus(ctx context.Context, id int64, status model.AnalysisStatus) error

UpdateAnalysisStatus updates the analysis status of a task.

func (*GormTaskRepository) UpdateAnalysisStatusWithInfo

func (r *GormTaskRepository) UpdateAnalysisStatusWithInfo(ctx context.Context, id int64, status model.AnalysisStatus, info string) error

UpdateAnalysisStatusWithInfo updates the analysis status with additional info.

type HotmethodTask

type HotmethodTask struct {
	ID             int64                `gorm:"column:id;primaryKey;autoIncrement"`
	TID            string               `gorm:"column:tid;type:varchar(64);uniqueIndex"`
	Profiler       model.Profiler       `gorm:"column:profiler;type:varchar(32)"`
	Event          model.EventType      `gorm:"column:event;type:varchar(32)"`
	Mode           string               `gorm:"column:mode;type:varchar(64)"`
	Status         model.TaskStatus     `gorm:"column:status"`
	AnalysisStatus model.AnalysisStatus `gorm:"column:analysis_status"`
	StatusInfo     string               `gorm:"column:status_info;type:text"`
	ResultFile     string               `gorm:"column:result_file;type:varchar(512)"`
	UserName       string               `gorm:"column:user_name;type:varchar(128)"`
	MasterTaskTID  *string              `gorm:"column:mastertask_tid;type:varchar(64)"`
	COSBucket      string               `gorm:"column:cos_bucket;type:varchar(128)"`
	CallbackURL    string               `gorm:"column:callback_url;type:varchar(512)"`
	RequestParams  JSONField            `gorm:"column:request_params;type:json"`
	Metadata       JSONField            `gorm:"column:metadata;type:json"`
	CreateTime     time.Time            `gorm:"column:create_time;autoCreateTime"`
	BeginTime      *time.Time           `gorm:"column:begin_time"`
	EndTime        *time.Time           `gorm:"column:end_time"`
}

HotmethodTask represents the hotmethod_task table.

func NewHotmethodTaskFromModel

func NewHotmethodTaskFromModel(task *model.Task) *HotmethodTask

NewHotmethodTaskFromModel converts model.Task to HotmethodTask for database insertion.

func (HotmethodTask) TableName

func (HotmethodTask) TableName() string

TableName returns the table name for HotmethodTask.

func (*HotmethodTask) ToModel

func (t *HotmethodTask) ToModel() *model.Task

ToModel converts HotmethodTask to model.Task.

type JSONField

type JSONField []byte

JSONField is a custom type for handling JSON fields in GORM.

func (JSONField) MarshalJSON

func (j JSONField) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*JSONField) Scan

func (j *JSONField) Scan(value interface{}) error

Scan implements sql.Scanner interface.

func (*JSONField) UnmarshalJSON

func (j *JSONField) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface.

func (JSONField) Value

func (j JSONField) Value() (driver.Value, error)

Value implements driver.Valuer interface.

type MasterTask

type MasterTask struct {
	TID                 string                       `json:"tid" db:"tid"`
	SubTIDs             []string                     `json:"sub_tids" db:"sub_tids"`
	AnalysisSuggestions *model.MasterTaskSuggestions `json:"analysis_suggestions" db:"analysis_suggestions"`
	AnalysisStatus      model.AnalysisStatus         `json:"analysis_status" db:"analysis_status"`
}

MasterTask represents a master task that may have sub-tasks.

type MasterTaskRepository

type MasterTaskRepository interface {
	// GetMasterTask retrieves a master task by its UUID.
	GetMasterTask(ctx context.Context, masterTID string) (*MasterTask, error)

	// UpdateMasterTaskSuggestions updates the suggestions for a master task.
	UpdateMasterTaskSuggestions(ctx context.Context, masterTID string, resourceType string, suggestions *model.SuggestionGroup) error

	// UpdateMasterTaskStatus updates the analysis status of a master task.
	UpdateMasterTaskStatus(ctx context.Context, masterTID string, status model.AnalysisStatus) error

	// GetIncompleteSubTaskCount returns the count of incomplete sub-tasks.
	GetIncompleteSubTaskCount(ctx context.Context, masterTID string) (int, error)

	// CheckAndCompleteIfReady checks if all sub-tasks are done and updates status.
	CheckAndCompleteIfReady(ctx context.Context, masterTID string) error
}

MasterTaskRepository defines the interface for master task operations.

type MultipleTask

type MultipleTask struct {
	TID                 string               `gorm:"column:tid;type:varchar(64);primaryKey"`
	SubTIDs             JSONField            `gorm:"column:sub_tids;type:json"`
	AnalysisSuggestions JSONField            `gorm:"column:analysis_suggestions;type:json"`
	AnalysisStatus      model.AnalysisStatus `gorm:"column:analysis_status"`
	EndTime             *time.Time           `gorm:"column:end_time"`
}

MultipleTask represents the multiple_task table for master tasks.

func (MultipleTask) TableName

func (MultipleTask) TableName() string

TableName returns the table name for MultipleTask.

func (*MultipleTask) ToMasterTask

func (m *MultipleTask) ToMasterTask() (*MasterTask, error)

ToMasterTask converts MultipleTask to MasterTask.

type Repositories

type Repositories struct {
	Task       TaskRepository
	Result     ResultRepository
	Suggestion SuggestionRepository
	MasterTask MasterTaskRepository
	// contains filtered or unexported fields
}

Repositories holds all repository instances.

func NewRepositories

func NewRepositories(gormDB *gorm.DB, dbType string, version string) *Repositories

NewRepositories creates all repositories using GORM.

func (*Repositories) Close

func (r *Repositories) Close() error

Close closes the database connection.

func (*Repositories) DB

func (r *Repositories) DB() *sql.DB

DB returns the underlying sql.DB connection.

func (*Repositories) GormDB

func (r *Repositories) GormDB() *gorm.DB

GormDB returns the underlying GORM DB instance.

func (*Repositories) HealthCheck

func (r *Repositories) HealthCheck(ctx context.Context) error

HealthCheck verifies the database connection is still alive.

type ResultRepository

type ResultRepository interface {
	// SaveResult saves an analysis result to the database.
	SaveResult(ctx context.Context, result *model.AnalysisResult) error

	// GetResultByTaskUUID retrieves the analysis result for a task.
	GetResultByTaskUUID(ctx context.Context, taskUUID string) (*model.AnalysisResult, error)

	// UpdateResult updates an existing analysis result.
	UpdateResult(ctx context.Context, result *model.AnalysisResult) error
}

ResultRepository defines the interface for analysis result operations.

type SuggestionRepository

type SuggestionRepository interface {
	// SaveSuggestions saves multiple suggestions to the database.
	SaveSuggestions(ctx context.Context, suggestions []model.Suggestion) error

	// GetSuggestionsByTaskUUID retrieves suggestions for a task.
	GetSuggestionsByTaskUUID(ctx context.Context, taskUUID string) ([]model.Suggestion, error)

	// GetAnalysisRules retrieves all active analysis rules.
	GetAnalysisRules(ctx context.Context) ([]model.SuggestionRule, error)
}

SuggestionRepository defines the interface for suggestion operations.

type TaskRepository

type TaskRepository interface {
	// CreateTask creates a new task record in the database.
	CreateTask(ctx context.Context, task *model.Task) error

	// GetPendingTasks retrieves tasks that are pending analysis.
	GetPendingTasks(ctx context.Context, limit int) ([]*model.Task, error)

	// GetTaskByID retrieves a task by its ID.
	GetTaskByID(ctx context.Context, id int64) (*model.Task, error)

	// GetTaskByUUID retrieves a task by its UUID.
	GetTaskByUUID(ctx context.Context, uuid string) (*model.Task, error)

	// UpdateAnalysisStatus updates the analysis status of a task.
	UpdateAnalysisStatus(ctx context.Context, id int64, status model.AnalysisStatus) error

	// UpdateAnalysisStatusWithInfo updates the analysis status with additional info.
	UpdateAnalysisStatusWithInfo(ctx context.Context, id int64, status model.AnalysisStatus, info string) error

	// LockTaskForAnalysis attempts to lock a task for analysis (prevents concurrent processing).
	LockTaskForAnalysis(ctx context.Context, id int64) (bool, error)

	// GetCompletedTasks retrieves tasks that have completed analysis (for cleanup purposes).
	GetCompletedTasks(ctx context.Context) ([]model.Task, error)
}

TaskRepository defines the interface for task-related database operations.

Jump to

Keyboard shortcuts

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