2023-04-14 12:40:34 +00:00
|
|
|
package output
|
2023-03-21 15:25:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
)
|
|
|
|
|
2023-05-08 14:35:47 +00:00
|
|
|
type TaskOutput struct {
|
|
|
|
TaskKey string
|
|
|
|
Output RunOutput
|
|
|
|
EndTime int64
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:25:18 +00:00
|
|
|
type JobOutput struct {
|
|
|
|
// output for tasks with a non empty output
|
2023-05-08 14:35:47 +00:00
|
|
|
TaskOutputs []TaskOutput `json:"task_outputs"`
|
2023-03-21 15:25:18 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 14:35:47 +00:00
|
|
|
// Returns tasks output in text form sorted in execution order based on task end time
|
2023-03-21 15:25:18 +00:00
|
|
|
func (out *JobOutput) String() (string, error) {
|
|
|
|
if len(out.TaskOutputs) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
// When only one task, just return that output without any formatting
|
|
|
|
if len(out.TaskOutputs) == 1 {
|
|
|
|
for _, v := range out.TaskOutputs {
|
2023-05-08 14:35:47 +00:00
|
|
|
return v.Output.String()
|
2023-03-21 15:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result := strings.Builder{}
|
2023-04-18 12:40:45 +00:00
|
|
|
result.WriteString("Output:\n")
|
2023-05-08 14:35:47 +00:00
|
|
|
sort.Slice(out.TaskOutputs, func(i, j int) bool {
|
|
|
|
return out.TaskOutputs[i].EndTime < out.TaskOutputs[j].EndTime
|
|
|
|
})
|
|
|
|
for _, v := range out.TaskOutputs {
|
|
|
|
if v.Output == nil {
|
2023-04-05 13:55:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-08 14:35:47 +00:00
|
|
|
taskString, err := v.Output.String()
|
2023-03-21 15:25:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
result.WriteString("=======\n")
|
2023-05-08 14:35:47 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Task %s:\n", v.TaskKey))
|
2023-03-21 15:25:18 +00:00
|
|
|
result.WriteString(fmt.Sprintf("%s\n", taskString))
|
|
|
|
}
|
|
|
|
return result.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-04-14 12:40:34 +00:00
|
|
|
func GetJobOutput(ctx context.Context, w *databricks.WorkspaceClient, runId int64) (*JobOutput, error) {
|
2023-04-21 08:30:20 +00:00
|
|
|
jobRun, err := w.Jobs.GetRun(ctx, jobs.GetRunRequest{
|
2023-03-21 15:25:18 +00:00
|
|
|
RunId: runId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := &JobOutput{
|
2023-05-08 14:35:47 +00:00
|
|
|
TaskOutputs: make([]TaskOutput, len(jobRun.Tasks)),
|
2023-03-21 15:25:18 +00:00
|
|
|
}
|
|
|
|
for _, task := range jobRun.Tasks {
|
2023-04-21 08:30:20 +00:00
|
|
|
jobRunOutput, err := w.Jobs.GetRunOutput(ctx, jobs.GetRunOutputRequest{
|
2023-03-21 15:25:18 +00:00
|
|
|
RunId: task.RunId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-08 14:35:47 +00:00
|
|
|
task := TaskOutput{TaskKey: task.TaskKey, Output: toRunOutput(jobRunOutput), EndTime: task.EndTime}
|
|
|
|
result.TaskOutputs = append(result.TaskOutputs, task)
|
2023-03-21 15:25:18 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|