service

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExists  = errors.New("not exist")
	ErrBadRequest = errors.New("bad request")
)

Functions

func ContextWithUser

func ContextWithUser(ctx context.Context, user string) context.Context

func DataToMap added in v0.1.7

func DataToMap(columns []string, data [][]any) []map[string]any

func UserContext

func UserContext(ctx context.Context) string

Types

type Cell

type Cell struct {
	ID          string                 `json:"id"`
	DBType      string                 `json:"db_type"`
	Content     string                 `json:"content"`
	Limit       int64                  `json:"limit"`
	Mode        types.Null[Mode]       `json:"mode,omitzero"`
	Description types.Null[string]     `json:"description,omitzero"`
	Collapsed   types.Null[bool]       `json:"collapsed,omitzero"`
	Enabled     types.Null[bool]       `json:"enabled,omitzero"`
	Result      types.Null[bool]       `json:"result,omitzero"`
	Template    Template               `json:"template"`
	Path        types.Null[string]     `json:"path,omitzero"`
	Dependency  types.Null[Dependency] `json:"dependency,omitzero"`
}

type CellResult added in v0.2.5

type CellResult struct {
	Columns []string
	Rows    [][]any
}

CellResult holds query result data for a single cell execution. Returned to the HTTP caller but NOT stored in the process log.

type ColumnType

type ColumnType struct {
	Type     string `json:"type"`
	Nullable bool   `json:"nullable"`
}

type ColumnTypeTemplate

type ColumnTypeTemplate struct {
	Type     string      `json:"type"`
	Nullable bool        `json:"nullable"`
	Template EnableValue `json:"template"`
	Encoding Encoding    `json:"encoding"`
}

type Content

type Content struct {
	Cells []Cell `json:"cells"`
}

type ContextKey

type ContextKey string
const (
	UserContextKey ContextKey = "USER"
)

type Database

type Database interface {
	DatabaseList() []string
	DatabaseDriverType(name string) string

	Query(ctx context.Context, name, query string, limit int64) (Result, error)
	Exec(ctx context.Context, name, query string) (Result, error)

	IterGet(ctx context.Context, name, query string, mapType MapType) ([]string, iter.Seq2[[]any, error], error)
	IterSet(ctx context.Context, name, table string, wipe bool, skipError SkipError, mapType MapType, batch int, columns []string, rows iter.Seq2[[]any, error]) (Result, error)
}

type Dependency added in v0.1.7

type Dependency struct {
	Enabled bool     `json:"enabled"`
	Names   []string `json:"names"`
}

type EnableValue

type EnableValue struct {
	Enabled bool   `json:"enabled"`
	Value   string `json:"value"`
}

type Encoding

type Encoding struct {
	Enabled bool   `json:"enabled"`
	Coding  string `json:"coding"`
}

type IDName

type IDName struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

type MapType

type MapType struct {
	Enabled     bool                          `json:"enabled"`
	Column      map[string]ColumnType         `json:"column"`
	Destination map[string]ColumnTypeTemplate `json:"destination"`
}

type Mode

type Mode struct {
	Enabled   bool      `json:"enabled"`
	Name      string    `json:"name"`
	DBType    string    `json:"db_type"`
	Table     string    `json:"table"`
	Wipe      bool      `json:"wipe"`
	SkipError SkipError `json:"skip_error"`
	MapType   MapType   `json:"map_type"`
	Batch     int       `json:"batch"`
}

type Note

type Note struct {
	ID        string                 `json:"id"`
	Name      string                 `json:"name"`
	Path      string                 `json:"path"`
	Content   Content                `json:"content"`
	UpdatedBy types.Null[string]     `json:"updated_by"`
	CreatedAt types.Null[types.Time] `json:"created_at"`
	UpdatedAt types.Null[types.Time] `json:"updated_at"`
}

type Process added in v0.2.0

type Process struct {
	ID        string             `json:"id"`
	Status    ProcessStatus      `json:"status"`
	Info      ProcessInfo        `json:"info"`
	User      types.Null[string] `json:"user,omitzero"`
	CreatedAt types.Time         `json:"created_at"`
	UpdatedAt types.Time         `json:"updated_at"`
}

type ProcessAction added in v0.2.0

type ProcessAction string
const (
	ProcessActionTerminate ProcessAction = "terminate"
)

type ProcessActionRequest added in v0.2.0

type ProcessActionRequest struct {
	Action ProcessAction `json:"action"`
}

type ProcessCellInfo added in v0.2.0

type ProcessCellInfo struct {
	Description  string `json:"description,omitempty"`
	Query        string `json:"query,omitempty"`
	Database     string `json:"database,omitempty"`
	Driver       string `json:"driver,omitempty"`
	Status       string `json:"status"`
	Duration     string `json:"duration,omitempty"`
	Error        string `json:"error,omitempty"`
	RowsAffected int64  `json:"rows_affected,omitempty"`
}

type ProcessInfo added in v0.2.0

type ProcessInfo struct {
	Note         string            `json:"note,omitempty"`
	Query        string            `json:"query,omitempty"`
	Description  string            `json:"description,omitempty"`
	Database     string            `json:"database,omitempty"`
	Driver       string            `json:"driver,omitempty"`
	RowsAffected int64             `json:"rows_affected,omitempty"`
	Error        string            `json:"error,omitempty"`
	Duration     string            `json:"duration,omitempty"`
	Cells        []ProcessCellInfo `json:"cells,omitempty"`
}

type ProcessStatus added in v0.2.0

type ProcessStatus string
const (
	ProcessStatusRunning    ProcessStatus = "running"
	ProcessStatusCompleted  ProcessStatus = "completed"
	ProcessStatusFailed     ProcessStatus = "failed"
	ProcessStatusTerminated ProcessStatus = "terminated"
)

type Result

type Result interface {
	Columns() []string
	Rows() [][]any
	RowsAffected() int64
	Duration() time.Duration
}

type Service

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

func New

func New(db Database, store Storer, cfg *alan.Config) (*Service, error)

func (*Service) ActionProcessID added in v0.2.0

func (s *Service) ActionProcessID(ctx context.Context, pid string, action ProcessActionRequest) error

func (*Service) CancelProcess added in v0.2.0

func (s *Service) CancelProcess(pid string) bool

CancelProcess invokes and removes the cancel function for a process ID. If not found locally and alan is configured, broadcasts to peers. Returns true if a cancel function was found and invoked (locally or on a peer).

func (*Service) CleanupStaleProcesses added in v0.2.0

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

CleanupStaleProcesses marks all running processes as failed on startup.

func (*Service) CompleteProcess added in v0.2.0

func (s *Service) CompleteProcess(ctx context.Context, pid string, rowsAffected int64, duration time.Duration)

CompleteProcess updates a process to completed status with result info.

func (*Service) CompleteProcessWithCells added in v0.2.0

func (s *Service) CompleteProcessWithCells(ctx context.Context, pid string, rowsAffected int64, duration time.Duration, cells []ProcessCellInfo)

CompleteProcessWithCells updates a process to completed status with result info and cell details.

func (*Service) CreateProcess added in v0.2.0

func (s *Service) CreateProcess(ctx context.Context, info ProcessInfo, cancel context.CancelFunc) (string, error)

CreateProcess creates a new process record with running status, registers the cancel function, and returns the process ID.

func (*Service) DatabaseDriverType added in v0.2.5

func (s *Service) DatabaseDriverType(name string) string

func (*Service) DatabaseList

func (s *Service) DatabaseList() []string

func (*Service) DeleteNote

func (s *Service) DeleteNote(ctx context.Context, id string) error

func (*Service) FailProcess added in v0.2.0

func (s *Service) FailProcess(ctx context.Context, pid string, processErr error, duration time.Duration)

FailProcess updates a process to failed status with error info.

func (*Service) FailProcessWithCells added in v0.2.0

func (s *Service) FailProcessWithCells(ctx context.Context, pid string, processErr error, duration time.Duration, cells []ProcessCellInfo)

FailProcessWithCells updates a process to failed status with error info and cell details.

func (*Service) GetNote

func (s *Service) GetNote(ctx context.Context, id string) (*Note, error)

func (*Service) GetNoteCellDBInfo added in v0.2.5

func (s *Service) GetNoteCellDBInfo(ctx context.Context, notePath string, cellPath string) (database string, driver string)

GetNoteCellDBInfo returns the database name and driver type for a specific cell in a note.

func (*Service) GetNotes

func (s *Service) GetNotes(ctx context.Context) ([]IDName, error)

func (*Service) GetProcess added in v0.2.0

func (s *Service) GetProcess(ctx context.Context, q *query.Query) ([]Process, error)

func (*Service) GetProcessID added in v0.2.0

func (s *Service) GetProcessID(ctx context.Context, pid string) (*Process, error)

func (*Service) RegisterCancel added in v0.2.0

func (s *Service) RegisterCancel(pid string, cancel context.CancelFunc)

RegisterCancel stores a cancel function for a process ID.

func (*Service) RemoveCancel added in v0.2.0

func (s *Service) RemoveCancel(pid string)

RemoveCancel removes the cancel function for a process ID without invoking it.

func (*Service) Run

func (s *Service) Run(ctx context.Context, cell *Cell, values map[string]any, dependency map[string]struct{}) (result Result, err error)

func (*Service) RunNote

func (s *Service) RunNote(ctx context.Context, notePath string, values map[string]any, withResults bool) (cellInfos []ProcessCellInfo, cellResults []CellResult, err error)

func (*Service) RunNoteCell added in v0.1.4

func (s *Service) RunNoteCell(ctx context.Context, notePath string, cellPath string, values map[string]any) (result Result, err error)

func (*Service) SaveNote

func (s *Service) SaveNote(ctx context.Context, note *Note) error

func (*Service) StartAlan added in v0.2.0

func (s *Service) StartAlan(ctx context.Context) error

StartAlan starts the alan peer communication. Blocks until ctx is cancelled. Returns nil immediately if alan is not configured.

func (*Service) StartProcessCleanup added in v0.2.2

func (s *Service) StartProcessCleanup(ctx context.Context, retention, interval time.Duration)

StartProcessCleanup runs periodic cleanup of old process records. If alan is configured, a distributed lock ensures only one instance runs the cleanup loop. Other instances block on Lock until the holder exits or crashes, then one takes over automatically. Blocks until ctx is cancelled.

func (*Service) StopAlan added in v0.2.0

func (s *Service) StopAlan() error

StopAlan gracefully stops alan peer communication.

type SkipError

type SkipError struct {
	Enabled bool   `json:"enabled"`
	Message string `json:"message"`
}

type Storer

type Storer interface {
	Get(ctx context.Context, id string) (*Note, error)
	GetWithPath(ctx context.Context, path string) (*Note, error)
	GetNotes(ctx context.Context) ([]IDName, error)
	Save(ctx context.Context, note *Note) error
	Delete(ctx context.Context, id string) error

	GetProcess(ctx context.Context, q *query.Query) ([]Process, error)
	SaveProcess(ctx context.Context, process *Process) error
	DeleteProcessBefore(ctx context.Context, before time.Time) (int64, error)
}

type Template

type Template struct {
	Enabled bool `json:"enabled"`
}

Jump to

Keyboard shortcuts

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