Skip to content

Commit

Permalink
feat: update result of tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamadch91 committed Jan 1, 2025
1 parent 888f30d commit e6d5ec5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
6 changes: 3 additions & 3 deletions services/tasks/api/task-run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package api

import (
"github.com/opengovern/opencomply/services/tasks/db/models"
"time"
)

Expand All @@ -11,11 +10,12 @@ type TaskRun struct {
UpdatedAt time.Time `json:"updated_at"`
TaskID string `json:"task_id"`
Status string `json:"status"`
Result string `json:"result"`
Result map[string]any `json:"result"`
Params map[string]any `json:"params"`
FailureMessage string `json:"failure_message"`
}

type ListTaskRunsResponse struct {
TotalCount int `json:"total_count"`
Items []models.TaskRun `json:"items"`
Items []TaskRun `json:"items"`
}
6 changes: 3 additions & 3 deletions services/tasks/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func (db Database) GetTask(id string) (*models.Task, error) {
}

// GetTaskRunResult retrieves a task result by Task ID
func (db Database) GetTaskRunResult(id string) (*models.TaskRun, error) {
var task *models.TaskRun
func (db Database) GetTaskRunResult(id string) ([]models.TaskRun, error) {
var task []models.TaskRun
tx := db.Orm.Where("task_id = ?", id).
Order("created_at desc").
First(&task)
Find(&task)
if tx.Error != nil {
return nil, tx.Error
}
Expand Down
82 changes: 79 additions & 3 deletions services/tasks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,65 @@ func (r *httpRoutes) RunTask(ctx echo.Context) error {
// @Router /tasks/api/v1/tasks/run/:id [get]
func (r *httpRoutes) GetTaskRunResult(ctx echo.Context) error {
id := ctx.Param("id")
var cursor, perPage int64
taskResults, err := r.db.GetTaskRunResult(id)
if err != nil {
r.logger.Error("failed to get task results", zap.Error(err))
return ctx.JSON(http.StatusInternalServerError, "failed to get task results")
}
cursorStr := ctx.QueryParam("cursor")
if cursorStr != "" {
cursor, err = strconv.ParseInt(cursorStr, 10, 64)
if err != nil {
return err
}
}
perPageStr := ctx.QueryParam("per_page")
if perPageStr != "" {
perPage, err = strconv.ParseInt(perPageStr, 10, 64)
if err != nil {
return err
}
}
totalCount := len(taskResults)
if perPage != 0 {
if cursor == 0 {
taskResults = utils.Paginate(1, perPage, taskResults)
} else {
taskResults = utils.Paginate(cursor, perPage, taskResults)
}
}
var taskRunResponses []api.TaskRun
for _, task := range taskResults {
var params map[string]interface{}
err := json.Unmarshal(task.Params.Bytes, &params)
if err != nil {
r.logger.Error("failed to unmarshal params", zap.Error(err))
return ctx.JSON(http.StatusInternalServerError, "failed to unmarshal params")
}
var result map[string]interface{}
err = json.Unmarshal(task.Result.Bytes, &result)
if err != nil {
r.logger.Error("failed to unmarshal result", zap.Error(err))
return ctx.JSON(http.StatusInternalServerError, "failed to unmarshal result")
}
taskRunResponses = append(taskRunResponses, api.TaskRun{
ID: task.ID,
CreatedAt: task.CreatedAt,
UpdatedAt: task.UpdatedAt,
TaskID: task.TaskID,
Status: string(task.Status),
Result: result,
Params: params,
FailureMessage: task.FailureMessage,
})
}

return ctx.JSON(http.StatusOK, api.ListTaskRunsResponse{
TotalCount: totalCount,
Items: taskRunResponses,
})

return ctx.JSON(http.StatusOK, taskResults)
}

// ListTaskRunResult godoc
Expand Down Expand Up @@ -256,9 +308,33 @@ func (r *httpRoutes) ListTaskRunResult(ctx echo.Context) error {
items = utils.Paginate(cursor, perPage, items)
}
}

var taskRunResponses []api.TaskRun
for _, task := range items {
var params map[string]interface{}
err := json.Unmarshal(task.Params.Bytes, &params)
if err != nil {
r.logger.Error("failed to unmarshal params", zap.Error(err))
return ctx.JSON(http.StatusInternalServerError, "failed to unmarshal params")
}
var result map[string]interface{}
err = json.Unmarshal(task.Result.Bytes, &result)
if err != nil {
r.logger.Error("failed to unmarshal result", zap.Error(err))
return ctx.JSON(http.StatusInternalServerError, "failed to unmarshal result")
}
taskRunResponses = append(taskRunResponses, api.TaskRun{
ID: task.ID,
CreatedAt: task.CreatedAt,
UpdatedAt: task.UpdatedAt,
TaskID: task.TaskID,
Status: string(task.Status),
Result: result,
Params: params,
FailureMessage: task.FailureMessage,
})
}
return ctx.JSON(http.StatusOK, api.ListTaskRunsResponse{
TotalCount: totalCount,
Items: items,
Items: taskRunResponses,
})
}

0 comments on commit e6d5ec5

Please sign in to comment.