models

package
v0.0.0-...-ab91138 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Page        = 1    // 当前页数
	PageSize    = 20   // 每页多少条数据
	MaxPageSize = 1000 // 每次最多取多少条
)
View Source
const (
	SlackCode        = "slack"
	SlackUrlKey      = "url"
	SlackTemplateKey = "template"
	SlackChannelKey  = "channel"
)
View Source
const (
	MailCode        = "mail"
	MailTemplateKey = "template"
	MailServerKey   = "server"
	MailUserKey     = "user"
)
View Source
const (
	WebhookCode        = "webhook"
	WebhookTemplateKey = "template"
	WebhookUrlKey      = "url"
)
View Source
const DefaultTimeFormat = "2006-01-02 15:04:05"
View Source
const PasswordSaltLength = 6

Variables

View Source
var TablePrefix = ""

Functions

func CreateDb

func CreateDb() *xorm.Engine

创建Db

func CreateTmpDb

func CreateTmpDb(setting *setting.Setting) (*xorm.Engine, error)

创建临时数据库连接

Types

type BaseModel

type BaseModel struct {
	Page     int `xorm:"-"`
	PageSize int `xorm:"-"`
}

type Channel

type Channel struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

type CommonMap

type CommonMap map[string]interface{}

type Host

type Host struct {
	Id        int16  `json:"id" xorm:"smallint pk autoincr"`
	Name      string `json:"name" xorm:"varchar(64) notnull"`                // 主机名称
	Alias     string `json:"alias" xorm:"varchar(32) notnull default '' "`   // 主机别名
	Port      int    `json:"port" xorm:"notnull default 5921"`               // 主机端口
	Remark    string `json:"remark" xorm:"varchar(100) notnull default '' "` // 备注
	BaseModel `json:"-" xorm:"-"`
	Selected  bool `json:"-" xorm:"-"`
}

主机

func (*Host) AllList

func (host *Host) AllList() ([]Host, error)

func (*Host) Create

func (host *Host) Create() (insertId int16, err error)

新增

func (*Host) Delete

func (host *Host) Delete(id int) (int64, error)

删除

func (*Host) Find

func (host *Host) Find(id int) error

func (*Host) List

func (host *Host) List(params CommonMap) ([]Host, error)

func (*Host) NameExists

func (host *Host) NameExists(name string, id int16) (bool, error)

func (*Host) Total

func (host *Host) Total(params CommonMap) (int64, error)

func (*Host) Update

func (host *Host) Update(id int, data CommonMap) (int64, error)

更新

func (*Host) UpdateBean

func (host *Host) UpdateBean(id int16) (int64, error)

type LoginLog

type LoginLog struct {
	Id        int       `json:"id" xorm:"pk autoincr notnull "`
	Username  string    `json:"username" xorm:"varchar(32) notnull"`
	Ip        string    `json:"ip" xorm:"varchar(15) not null"`
	Created   time.Time `json:"created" xorm:"datetime notnull created"`
	BaseModel `json:"-" xorm:"-"`
}

func (*LoginLog) Create

func (log *LoginLog) Create() (insertId int, err error)

func (*LoginLog) List

func (log *LoginLog) List(params CommonMap) ([]LoginLog, error)

func (*LoginLog) Total

func (log *LoginLog) Total() (int64, error)

type Mail

type Mail struct {
	Host      string     `json:"host"`
	Port      int        `json:"port"`
	User      string     `json:"user"`
	Password  string     `json:"password"`
	MailUsers []MailUser `json:"mail_users"`
	Template  string     `json:"template"`
}

type MailUser

type MailUser struct {
	Id       int    `json:"id"`
	Username string `json:"username"`
	Email    string `json:"email"`
}

type Migration

type Migration struct{}

func (*Migration) Install

func (migration *Migration) Install(dbName string) error

首次安装, 创建数据库表

func (*Migration) Upgrade

func (migration *Migration) Upgrade(oldVersionId int)

迭代升级数据库, 新建表、新增字段等

type Setting

type Setting struct {
	Id    int    `xorm:"int pk autoincr"`
	Code  string `xorm:"varchar(32) notnull"`
	Key   string `xorm:"varchar(64) notnull"`
	Value string `xorm:"varchar(4096) notnull default '' "`
}

func (*Setting) CreateChannel

func (setting *Setting) CreateChannel(channel string) (int64, error)

创建slack渠道

func (*Setting) CreateMailUser

func (setting *Setting) CreateMailUser(username, email string) (int64, error)

func (*Setting) InitBasicField

func (setting *Setting) InitBasicField()

初始化基本字段 邮件、slack等

func (*Setting) IsChannelExist

func (setting *Setting) IsChannelExist(channel string) bool

func (*Setting) Mail

func (setting *Setting) Mail() (Mail, error)

region 邮件配置

func (*Setting) RemoveChannel

func (setting *Setting) RemoveChannel(id int) (int64, error)

删除slack渠道

func (*Setting) RemoveMailUser

func (setting *Setting) RemoveMailUser(id int) (int64, error)

func (*Setting) Slack

func (setting *Setting) Slack() (Slack, error)

func (*Setting) UpdateMail

func (setting *Setting) UpdateMail(config, template string) error

func (*Setting) UpdateSlack

func (setting *Setting) UpdateSlack(url, template string) error

func (*Setting) UpdateWebHook

func (setting *Setting) UpdateWebHook(url, template string) error

func (*Setting) Webhook

func (setting *Setting) Webhook() (WebHook, error)

type Slack

type Slack struct {
	Url      string    `json:"url"`
	Channels []Channel `json:"channels"`
	Template string    `json:"template"`
}

type Status

type Status int8
const (
	Disabled Status = 0 // 禁用
	Failure  Status = 0 // 失败
	Enabled  Status = 1 // 启用
	Running  Status = 1 // 运行中
	Finish   Status = 2 // 完成
	Cancel   Status = 3 // 取消
)

type Task

type Task struct {
	Id               int                  `json:"id" xorm:"int pk autoincr"`
	Name             string               `json:"name" xorm:"varchar(32) notnull"`                            // 任务名称
	Level            TaskLevel            `json:"level" xorm:"tinyint notnull index default 1"`               // 任务等级 1: 主任务 2: 依赖任务
	DependencyTaskId string               `json:"dependency_task_id" xorm:"varchar(64) notnull default ''"`   // 依赖任务ID,多个ID逗号分隔
	DependencyStatus TaskDependencyStatus `json:"dependency_status" xorm:"tinyint notnull default 1"`         // 依赖关系 1:强依赖 主任务执行成功, 依赖任务才会被执行 2:弱依赖
	Spec             string               `json:"spec" xorm:"varchar(64) notnull"`                            // crontab
	Protocol         TaskProtocol         `json:"protocol" xorm:"tinyint notnull index"`                      // 协议 1:http 2:系统命令
	Command          string               `json:"command" xorm:"varchar(256) notnull"`                        // URL地址或shell命令
	HttpMethod       TaskHTTPMethod       `json:"http_method" xorm:"tinyint notnull default 1"`               // http请求方法
	Timeout          int                  `json:"timeout" xorm:"mediumint notnull default 0"`                 // 任务执行超时时间(单位秒),0不限制
	Multi            int8                 `json:"multi" xorm:"tinyint notnull default 1"`                     // 是否允许多实例运行
	RetryTimes       int8                 `json:"retry_times" xorm:"tinyint notnull default 0"`               // 重试次数
	RetryInterval    int16                `json:"retry_interval" xorm:"smallint notnull default 0"`           // 重试间隔时间
	NotifyStatus     int8                 `json:"notify_status" xorm:"tinyint notnull default 1"`             // 任务执行结束是否通知 0: 不通知 1: 失败通知 2: 执行结束通知 3: 任务执行结果关键字匹配通知
	NotifyType       int8                 `json:"notify_type" xorm:"tinyint notnull default 0"`               // 通知类型 1: 邮件 2: slack 3: webhook
	NotifyReceiverId string               `json:"notify_receiver_id" xorm:"varchar(256) notnull default '' "` // 通知接受者ID, setting表主键ID,多个ID逗号分隔
	NotifyKeyword    string               `json:"notify_keyword" xorm:"varchar(128) notnull default '' "`
	Tag              string               `json:"tag" xorm:"varchar(32) notnull default ''"`
	Remark           string               `json:"remark" xorm:"varchar(100) notnull default ''"` // 备注
	Status           Status               `json:"status" xorm:"tinyint notnull index default 0"` // 状态 1:正常 0:停止
	Created          time.Time            `json:"created" xorm:"datetime notnull created"`       // 创建时间
	Deleted          time.Time            `json:"deleted" xorm:"datetime deleted"`               // 删除时间
	BaseModel        `json:"-" xorm:"-"`
	Hosts            []TaskHostDetail `json:"hosts" xorm:"-"`
	NextRunTime      time.Time        `json:"next_run_time" xorm:"-"`
}

任务

func (*Task) ActiveList

func (task *Task) ActiveList(page, pageSize int) ([]Task, error)

获取所有激活任务

func (*Task) ActiveListByHostId

func (task *Task) ActiveListByHostId(hostId int16) ([]Task, error)

获取某个主机下的所有激活任务

func (*Task) Create

func (task *Task) Create() (insertId int, err error)

新增

func (*Task) Delete

func (task *Task) Delete(id int) (int64, error)

删除

func (*Task) Detail

func (task *Task) Detail(id int) (Task, error)

func (*Task) Disable

func (task *Task) Disable(id int) (int64, error)

禁用

func (*Task) Enable

func (task *Task) Enable(id int) (int64, error)

激活

func (*Task) GetDependencyTaskList

func (task *Task) GetDependencyTaskList(ids string) ([]Task, error)

获取依赖任务列表

func (*Task) GetStatus

func (task *Task) GetStatus(id int) (Status, error)

func (*Task) List

func (task *Task) List(params CommonMap) ([]Task, error)

func (*Task) NameExist

func (task *Task) NameExist(name string, id int) (bool, error)

判断任务名称是否存在

func (*Task) Total

func (task *Task) Total(params CommonMap) (int64, error)

func (*Task) Update

func (task *Task) Update(id int, data CommonMap) (int64, error)

更新

func (*Task) UpdateBean

func (task *Task) UpdateBean(id int) (int64, error)

type TaskDependencyStatus

type TaskDependencyStatus int8
const (
	TaskDependencyStatusStrong TaskDependencyStatus = 1 // 强依赖
	TaskDependencyStatusWeak   TaskDependencyStatus = 2 // 弱依赖
)

type TaskHTTPMethod

type TaskHTTPMethod int8
const (
	TaskHTTPMethodGet  TaskHTTPMethod = 1
	TaskHttpMethodPost TaskHTTPMethod = 2
)

type TaskHost

type TaskHost struct {
	Id     int   `json:"id" xorm:"int pk autoincr"`
	TaskId int   `json:"task_id" xorm:"int not null index"`
	HostId int16 `json:"host_id" xorm:"smallint not null index"`
}

func (*TaskHost) Add

func (th *TaskHost) Add(taskId int, hostIds []int) error

func (*TaskHost) GetHostIdsByTaskId

func (th *TaskHost) GetHostIdsByTaskId(taskId int) ([]TaskHostDetail, error)

func (*TaskHost) GetTaskIdsByHostId

func (th *TaskHost) GetTaskIdsByHostId(hostId int16) ([]interface{}, error)

func (*TaskHost) HostIdExist

func (th *TaskHost) HostIdExist(hostId int16) (bool, error)

判断主机id是否有引用

func (*TaskHost) Remove

func (th *TaskHost) Remove(taskId int) error

type TaskHostDetail

type TaskHostDetail struct {
	TaskHost `xorm:"extends"`
	Name     string `json:"name"`
	Port     int    `json:"port"`
	Alias    string `json:"alias"`
}

func (TaskHostDetail) TableName

func (TaskHostDetail) TableName() string

type TaskLevel

type TaskLevel int8
const (
	TaskLevelParent TaskLevel = 1 // 父任务
	TaskLevelChild  TaskLevel = 2 // 子任务(依赖任务)
)

type TaskLog

type TaskLog struct {
	Id         int64        `json:"id" xorm:"bigint pk autoincr"`
	TaskId     int          `json:"task_id" xorm:"int notnull index default 0"`       // 任务id
	Name       string       `json:"name" xorm:"varchar(32) notnull"`                  // 任务名称
	Spec       string       `json:"spec" xorm:"varchar(64) notnull"`                  // crontab
	Protocol   TaskProtocol `json:"protocol" xorm:"tinyint notnull index"`            // 协议 1:http 2:RPC
	Command    string       `json:"command" xorm:"varchar(256) notnull"`              // URL地址或shell命令
	Timeout    int          `json:"timeout" xorm:"mediumint notnull default 0"`       // 任务执行超时时间(单位秒),0不限制
	RetryTimes int8         `json:"retry_times" xorm:"tinyint notnull default 0"`     // 任务重试次数
	Hostname   string       `json:"hostname" xorm:"varchar(128) notnull default '' "` // RPC主机名,逗号分隔
	StartTime  time.Time    `json:"start_time" xorm:"datetime created"`               // 开始执行时间
	EndTime    time.Time    `json:"end_time" xorm:"datetime updated"`                 // 执行完成(失败)时间
	Status     Status       `json:"status" xorm:"tinyint notnull index default 1"`    // 状态 0:执行失败 1:执行中  2:执行完毕 3:任务取消(上次任务未执行完成) 4:异步执行
	Result     string       `json:"result" xorm:"mediumtext notnull "`                // 执行结果
	TotalTime  int          `json:"total_time" xorm:"-"`                              // 执行总时长
	BaseModel  `json:"-" xorm:"-"`
}

任务执行日志

func (*TaskLog) Clear

func (taskLog *TaskLog) Clear() (int64, error)

清空表

func (*TaskLog) Create

func (taskLog *TaskLog) Create() (insertId int64, err error)

func (*TaskLog) List

func (taskLog *TaskLog) List(params CommonMap) ([]TaskLog, error)

func (*TaskLog) Remove

func (taskLog *TaskLog) Remove(id int) (int64, error)

删除N个月前的日志

func (*TaskLog) Total

func (taskLog *TaskLog) Total(params CommonMap) (int64, error)

func (*TaskLog) Update

func (taskLog *TaskLog) Update(id int64, data CommonMap) (int64, error)

更新

type TaskProtocol

type TaskProtocol int8
const (
	TaskHTTP TaskProtocol = iota + 1 // HTTP协议
	TaskRPC                          // RPC方式执行命令
)

type TaskType

type TaskType int8

type User

type User struct {
	Id        int       `json:"id" xorm:"pk autoincr notnull "`
	Name      string    `json:"name" xorm:"varchar(32) notnull unique"`              // 用户名
	Password  string    `json:"-" xorm:"char(32) notnull "`                          // 密码
	Salt      string    `json:"-" xorm:"char(6) notnull "`                           // 密码盐值
	Email     string    `json:"email" xorm:"varchar(50) notnull unique default '' "` // 邮箱
	Created   time.Time `json:"created" xorm:"datetime notnull created"`
	Updated   time.Time `json:"updated" xorm:"datetime updated"`
	IsAdmin   int8      `json:"is_admin" xorm:"tinyint notnull default 0"` // 是否是管理员 1:管理员 0:普通用户
	Status    Status    `json:"status" xorm:"tinyint notnull default 1"`   // 1: 正常 0:禁用
	BaseModel `json:"-" xorm:"-"`
}

用户model

func (*User) Create

func (user *User) Create() (insertId int, err error)

新增

func (*User) Delete

func (user *User) Delete(id int) (int64, error)

删除

func (*User) Disable

func (user *User) Disable(id int) (int64, error)

禁用

func (*User) EmailExists

func (user *User) EmailExists(email string, uid int) (int64, error)

邮箱地址是否存在

func (*User) Enable

func (user *User) Enable(id int) (int64, error)

激活

func (*User) Find

func (user *User) Find(id int) error

获取用户详情

func (*User) List

func (user *User) List(params CommonMap) ([]User, error)

func (*User) Match

func (user *User) Match(username, password string) bool

验证用户名和密码

func (*User) Total

func (user *User) Total() (int64, error)

func (*User) Update

func (user *User) Update(id int, data CommonMap) (int64, error)

更新

func (*User) UpdatePassword

func (user *User) UpdatePassword(id int, password string) (int64, error)

func (*User) UsernameExists

func (user *User) UsernameExists(username string, uid int) (int64, error)

用户名是否存在

type WebHook

type WebHook struct {
	Url      string `json:"url"`
	Template string `json:"template"`
}

Jump to

Keyboard shortcuts

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