feat: tasks for BoltDB

This commit is contained in:
Denis Gukov 2021-06-24 22:45:28 +05:00
parent c1853fa5de
commit 482d24422b
5 changed files with 71 additions and 53 deletions

View File

@ -6,8 +6,6 @@ import (
"time"
)
const databaseTimeFormat = "2006-01-02T15:04:05:99Z"
// GetParsedTime returns the timestamp as it will retrieved from the database
@ -172,45 +170,45 @@ var RepositoryProps = ObjectProperties{
}
var TemplateProps = ObjectProperties{
TableName: "project__template",
SortableColumns: []string{"name"},
TableName: "project__template",
SortableColumns: []string{"name"},
PrimaryColumnName: "id",
}
var ProjectUserProps = ObjectProperties{
TableName: "project__user",
TableName: "project__user",
PrimaryColumnName: "user_id",
}
var ProjectProps = ObjectProperties{
TableName: "project",
IsGlobal: true,
TableName: "project",
IsGlobal: true,
PrimaryColumnName: "id",
}
var UserProps = ObjectProperties{
TableName: "user",
IsGlobal: true,
TableName: "user",
IsGlobal: true,
PrimaryColumnName: "id",
}
var SessionProps = ObjectProperties{
TableName: "session",
TableName: "session",
PrimaryColumnName: "id",
}
var TokenProps = ObjectProperties{
TableName: "user__token",
TableName: "user__token",
PrimaryColumnName: "id",
}
var TaskProps = ObjectProperties{
TableName: "task",
IsGlobal: true,
TableName: "task",
IsGlobal: true,
PrimaryColumnName: "id",
}
var TaskOutputProps = ObjectProperties{
TableName: "task__output",
PrimaryColumnName: "id",
TableName: "task__output",
PrimaryColumnName: "",
}

View File

@ -26,6 +26,7 @@ type Task struct {
End *time.Time `db:"end" json:"end"`
}
// TaskWithTpl is the task data with additional fields
type TaskWithTpl struct {
Task
TemplatePlaybook string `db:"tpl_playbook" json:"tpl_playbook"`

View File

@ -502,46 +502,56 @@ func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object in
tmpObj := reflect.New(objPtr.Elem().Type()).Elem()
tmpObj.Set(objPtr.Elem())
idFieldName, err := getFieldNameByTag(reflect.TypeOf(object), "db", props.PrimaryColumnName)
if err != nil {
return err
}
idValue := tmpObj.FieldByName(idFieldName)
var objectID objectID
switch idValue.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
if idValue.Int() == 0 {
if props.PrimaryColumnName != "" {
idFieldName, err := getFieldNameByTag(reflect.TypeOf(object), "db", props.PrimaryColumnName)
if err != nil {
return err
}
idValue := tmpObj.FieldByName(idFieldName)
switch idValue.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
if idValue.Int() == 0 {
id, err2 := b.NextSequence()
if err2 != nil {
return err2
}
idValue.SetInt(int64(id))
}
objectID = intObjectID(idValue.Int())
case reflect.String:
if idValue.String() == "" {
return fmt.Errorf("object ID can not be empty string")
}
objectID = strObjectID(idValue.String())
case reflect.Invalid:
id, err2 := b.NextSequence()
if err2 != nil {
return err2
}
idValue.SetInt(int64(id))
objectID = intObjectID(id)
default:
return fmt.Errorf("unsupported ID type")
}
objectID = intObjectID(idValue.Int())
case reflect.String:
if idValue.String() == "" {
return fmt.Errorf("object ID can not be empty string")
}
objectID = strObjectID(idValue.String())
case reflect.Invalid:
} else {
id, err2 := b.NextSequence()
if err2 != nil {
return err2
}
objectID = intObjectID(id)
default:
return fmt.Errorf("unsupported ID type")
}
if objectID == nil {

View File

@ -6,6 +6,7 @@ import (
func (d *BoltDb) GetRepository(projectID int, repositoryID int) (repository db.Repository, err error) {
err = d.getObject(projectID, db.RepositoryProps, intObjectID(repositoryID), &repository)
repository.SSHKey, err = d.GetAccessKey(projectID, repository.SSHKeyID)
return
}

View File

@ -8,7 +8,11 @@ import (
func (d *BoltDb) CreateTask(task db.Task) (newTask db.Task, err error) {
task.Created = time.Now()
err = d.getObject(0, db.TaskProps, intObjectID(task.ID), &newTask)
res, err := d.createObject(0, db.TaskProps, task)
if err != nil {
return
}
newTask = res.(db.Task)
return
}
@ -24,9 +28,11 @@ func (d *BoltDb) CreateTaskOutput(output db.TaskOutput) (db.TaskOutput, error) {
return newOutput.(db.TaskOutput), nil
}
func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQueryParams) (tasks []db.TaskWithTpl, err error) {
err = d.getObjects(0, db.TaskProps, params, func (tsk interface{}) bool {
task := tsk.(db.TaskWithTpl)
func (d *BoltDb) getTasks(projectID int, templateID *int, params db.RetrieveQueryParams) (tasksWithTpl []db.TaskWithTpl, err error) {
var tasks []db.Task
err = d.getObjects(0, db.TaskProps, params, func(tsk interface{}) bool {
task := tsk.(db.Task)
if task.ProjectID != projectID {
return false
@ -42,7 +48,8 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer
var templates = make(map[int]db.Template)
var users = make(map[int]db.User)
for _, task := range tasks {
tasksWithTpl = make([]db.TaskWithTpl, len(tasks))
for i, task := range tasks {
tpl, ok := templates[task.TemplateID]
if !ok {
tpl, err = d.GetTemplate(task.ProjectID, task.TemplateID)
@ -51,8 +58,9 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer
}
templates[task.TemplateID] = tpl
}
task.TemplatePlaybook = tpl.Playbook
task.TemplateAlias = tpl.Alias
tasksWithTpl[i] = db.TaskWithTpl{Task: task}
tasksWithTpl[i].TemplatePlaybook = tpl.Playbook
tasksWithTpl[i].TemplateAlias = tpl.Alias
if task.UserID != nil {
usr, ok := users[*task.UserID]
if !ok {
@ -62,7 +70,7 @@ func (d *BoltDb) getTasks(projectID int, templateID* int, params db.RetrieveQuer
}
users[*task.UserID] = usr
}
task.UserName = &usr.Name
tasksWithTpl[i].UserName = &usr.Name
}
}
@ -102,7 +110,7 @@ func (d *BoltDb) DeleteTaskWithOutputs(projectID int, taskID int) (err error) {
return
}
_ = d.db.Update(func (tx *bbolt.Tx) error {
_ = d.db.Update(func(tx *bbolt.Tx) error {
return tx.DeleteBucket(makeBucketId(db.TaskOutputProps, taskID))
})