Skip to content

Commit

Permalink
Add task type as a filterable field (flyteorg#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
katrogan authored Mar 18, 2020
1 parent d3bb1ad commit fc4a9fe
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/repositories/config/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ var Migrations = []*gormigrate.Migration{
return tx.DropTable("resources").Error
},
},
// Add Type to Task model.
{
ID: "2020-03-17-task-type",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Task{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE tasks DROP COLUMN IF EXISTS type").Error
},
},
}
9 changes: 9 additions & 0 deletions pkg/repositories/gormimpl/task_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/stretchr/testify/assert"
)

const pythonTestTaskType = "python-task"

func TestCreateTask(t *testing.T) {
taskRepo := NewTaskRepo(GetDbForTest(t), errors.NewTestErrorTransformer(), mockScope.NewTestScope())
err := taskRepo.Create(context.Background(), models.Task{
Expand All @@ -26,6 +28,7 @@ func TestCreateTask(t *testing.T) {
Version: version,
},
Closure: []byte{1, 2},
Type: pythonTestTaskType,
})
assert.NoError(t, err)
}
Expand All @@ -37,6 +40,7 @@ func getMockTaskResponseFromDb(version string, spec []byte) map[string]interface
task["name"] = name
task["version"] = version
task["closure"] = spec
task["type"] = pythonTestTaskType
return task
}

Expand Down Expand Up @@ -66,6 +70,7 @@ func TestGetTask(t *testing.T) {
assert.Equal(t, name, output.Name)
assert.Equal(t, version, output.Version)
assert.Equal(t, []byte{1, 2}, output.Closure)
assert.Equal(t, pythonTestTaskType, output.Type)
}

func TestListTasks(t *testing.T) {
Expand Down Expand Up @@ -100,6 +105,7 @@ func TestListTasks(t *testing.T) {
assert.Equal(t, name, task.Name)
assert.Contains(t, versions, task.Version)
assert.Equal(t, spec, task.Closure)
assert.Equal(t, pythonTestTaskType, task.Type)
}
}

Expand Down Expand Up @@ -135,6 +141,7 @@ func TestListTasks_Pagination(t *testing.T) {
assert.Equal(t, name, task.Name)
assert.Equal(t, versions[idx], task.Version)
assert.Equal(t, spec, task.Closure)
assert.Equal(t, pythonTestTaskType, task.Type)
}
}

Expand Down Expand Up @@ -167,6 +174,7 @@ func TestListTasks_Filters(t *testing.T) {
assert.Equal(t, name, collection.Tasks[0].Name)
assert.Equal(t, "ABC", collection.Tasks[0].Version)
assert.Equal(t, []byte{1, 2}, collection.Tasks[0].Closure)
assert.Equal(t, pythonTestTaskType, collection.Tasks[0].Type)
}

func TestListTasks_Order(t *testing.T) {
Expand Down Expand Up @@ -246,6 +254,7 @@ func TestListTaskIds(t *testing.T) {
assert.Equal(t, name, task.Name)
assert.Equal(t, versions[idx], task.Version)
assert.Equal(t, spec, task.Closure)
assert.Equal(t, pythonTestTaskType, task.Type)
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/repositories/models/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ type Task struct {
Closure []byte `gorm:"not null"`
// Hash of the compiled task closure
Digest []byte
// Task type (also stored in the closure put promoted as a column for filtering).
Type string
}
5 changes: 5 additions & 0 deletions pkg/repositories/transformers/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func CreateTaskModel(
if err != nil {
return models.Task{}, errors.NewFlyteAdminError(codes.Internal, "Failed to serialize task closure")
}
var taskType string
if taskClosure.CompiledTask != nil && taskClosure.CompiledTask.Template != nil {
taskType = taskClosure.CompiledTask.Template.Type
}
return models.Task{
TaskKey: models.TaskKey{
Project: request.Id.Project,
Expand All @@ -29,6 +33,7 @@ func CreateTaskModel(
},
Closure: closureBytes,
Digest: digest,
Type: taskType,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/repositories/transformers/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestCreateTask(t *testing.T) {
assert.Equal(t, "version", task.Version)
assert.Equal(t, testutils.GetTaskClosureBytes(), task.Closure)
assert.Equal(t, taskDigest, task.Digest)
assert.Equal(t, "type", task.Type)
}

func TestFromTaskModel(t *testing.T) {
Expand Down

0 comments on commit fc4a9fe

Please sign in to comment.