Add state history for task state

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-01-30 15:58:33 -08:00
parent 48d6f3c7fb
commit 31f750cf52
12 changed files with 1271 additions and 41 deletions

View File

@@ -192,6 +192,13 @@ type Task implements Node {
description: String!
state: TaskState!
stateTransisions(
first: Int
after: CursorKey
last: Int
before: CursorKey
): TaskStateTransitionConnection! @goField(forceResolver: true)
evidences(
first: Int
after: CursorKey
@@ -203,6 +210,25 @@ type Task implements Node {
updatedAt: Datetime!
}
type TaskStateTransitionConnection {
edges: [TaskStateTransitionEdge!]!
pageInfo: PageInfo!
}
type TaskStateTransitionEdge {
cursor: CursorKey!
node: TaskStateTransition!
}
type TaskStateTransition {
id: ID!
fromState: TaskState
toState: TaskState!
reason: String
createdAt: Datetime!
updatedAt: Datetime!
}
type EvidenceConnection {
edges: [EvidenceEdge!]!
pageInfo: PageInfo!

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,59 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package types
import (
"github.com/getprobo/probo/pkg/probo/coredata"
"github.com/getprobo/probo/pkg/probo/coredata/page"
)
func NewTaskStateTransitionConnection(
p *page.Page[*coredata.TaskStateTransition],
) *TaskStateTransitionConnection {
var edges = make([]*TaskStateTransitionEdge, len(p.Data))
for i := range edges {
edges[i] = NewTaskStateTransitionEdge(p.Data[i])
}
return &TaskStateTransitionConnection{
Edges: edges,
PageInfo: NewPageInfo(p),
}
}
func NewTaskStateTransitionEdge(tst *coredata.TaskStateTransition) *TaskStateTransitionEdge {
return &TaskStateTransitionEdge{
Cursor: tst.CursorKey(),
Node: NewTaskStateTransition(tst),
}
}
func NewTaskStateTransition(tst *coredata.TaskStateTransition) *TaskStateTransition {
var fromState *TaskState
if tst.FromState != nil {
val := TaskState((*tst.FromState).String())
fromState = &val
}
return &TaskStateTransition{
ID: tst.ID,
FromState: fromState,
ToState: TaskState(tst.ToState.String()),
Reason: tst.Reason,
CreatedAt: tst.CreatedAt,
UpdatedAt: tst.UpdatedAt,
}
}

View File

@@ -148,13 +148,14 @@ type Query struct {
}
type Task struct {
ID gid.GID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
State TaskState `json:"state"`
Evidences *EvidenceConnection `json:"evidences"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID gid.GID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
State TaskState `json:"state"`
StateTransisions *TaskStateTransitionConnection `json:"stateTransisions"`
Evidences *EvidenceConnection `json:"evidences"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (Task) IsNode() {}
@@ -170,6 +171,25 @@ type TaskEdge struct {
Node *Task `json:"node"`
}
type TaskStateTransition struct {
ID gid.GID `json:"id"`
FromState *TaskState `json:"fromState,omitempty"`
ToState TaskState `json:"toState"`
Reason *string `json:"reason,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type TaskStateTransitionConnection struct {
Edges []*TaskStateTransitionEdge `json:"edges"`
PageInfo *PageInfo `json:"pageInfo"`
}
type TaskStateTransitionEdge struct {
Cursor page.CursorKey `json:"cursor"`
Node *TaskStateTransition `json:"node"`
}
type Vendor struct {
ID gid.GID `json:"id"`
Name string `json:"name"`

View File

@@ -104,6 +104,18 @@ func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error
return nil, gqlerror.Errorf("node %q not found", id)
}
// StateTransisions is the resolver for the stateTransisions field.
func (r *taskResolver) StateTransisions(ctx context.Context, obj *types.Task, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.TaskStateTransitionConnection, error) {
cursor := types.NewCursor(first, after, last, before)
page, err := r.svc.ListTaskStateTransitions(ctx, obj.ID, cursor)
if err != nil {
return nil, fmt.Errorf("cannot list control tasks: %w", err)
}
return types.NewTaskStateTransitionConnection(page), nil
}
// Evidences is the resolver for the evidences field.
func (r *taskResolver) Evidences(ctx context.Context, obj *types.Task, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.EvidenceConnection, error) {
cursor := types.NewCursor(first, after, last, before)

View File

@@ -18,7 +18,6 @@ import (
"context"
"fmt"
"maps"
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/getprobo/probo/pkg/probo/coredata/page"
@@ -29,13 +28,9 @@ import (
type (
ControlStateTransition struct {
ID gid.GID
StateTransition[ControlState]
ControlID gid.GID
FromState *ControlState
ToState ControlState
Reason *string
CreatedAt time.Time
UpdatedAt time.Time
}
ControlStateTransitions []*ControlStateTransition

View File

@@ -21,4 +21,7 @@ const (
TaskEntityType
EvidenceEntityType
ControlStateTransitionEntityType
TaskStateTransitionEntityType
VendorEntityType
PeopleEntityType
)

View File

@@ -0,0 +1,11 @@
CREATE TABLE task_state_transitions (
id TEXT PRIMARY KEY,
task_id TEXT REFERENCES tasks(id) NOT NULL,
from_state task_state,
to_state task_state NOT NULL,
reason TEXT,
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE
);
ALTER TABLE tasks DROP COLUMN state;

View File

@@ -0,0 +1,31 @@
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"time"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
)
type (
StateTransition[T any] struct {
ID gid.GID
FromState *T
ToState T
Reason *string
CreatedAt time.Time
UpdatedAt time.Time
}
)

View File

@@ -68,36 +68,49 @@ func (t *Tasks) LoadByControlID(
cursor *page.Cursor,
) error {
q := `
WITH control_tasks AS (
SELECT
t.id,
@control_id AS control_id,
t.name,
t.description,
t.state,
t.content_ref,
t.created_at,
t.updated_at
FROM
tasks t
INNER JOIN
controls_tasks ct ON
ct.task_id = t.id
AND ct.control_id = @control_id
WHERE
%s
)
WITH
control_tasks AS (
SELECT
t.id,
@control_id AS control_id,
t.name,
t.description,
t.content_ref,
t.created_at,
t.updated_at
FROM
tasks t
INNER JOIN
controls_tasks ct ON
ct.task_id = t.id
AND ct.control_id = @control_id
WHERE
%s
),
task_states AS (
SELECT
task_id,
to_state AS state,
reason,
RANK() OVER w
FROM
task_state_transitions
WINDOW
w AS (PARTITION BY task_id ORDER BY created_at DESC)
)
SELECT
id,
control_id,
name,
description,
state,
ts.state AS state,
content_ref,
created_at,
updated_at
FROM
control_tasks
INNER JOIN
task_states ts ON ts.task_id = control_tasks.id
WHERE
%s
`

View File

@@ -0,0 +1,107 @@
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"context"
"fmt"
"maps"
"github.com/getprobo/probo/pkg/probo/coredata/gid"
"github.com/getprobo/probo/pkg/probo/coredata/page"
"github.com/jackc/pgx/v5"
"go.gearno.de/crypto/uuid"
"go.gearno.de/kit/pg"
)
type (
TaskStateTransition struct {
TaskID gid.GID
StateTransition[TaskState]
}
TaskStateTransitions []*TaskStateTransition
)
func (tst TaskStateTransition) CursorKey() page.CursorKey {
return page.NewCursorKey(uuid.UUID(tst.ID), tst.CreatedAt)
}
func (tst *TaskStateTransition) scan(r pgx.Row) error {
return r.Scan(
&tst.ID,
&tst.TaskID,
&tst.FromState,
&tst.ToState,
&tst.Reason,
&tst.CreatedAt,
&tst.UpdatedAt,
)
}
func (tst *TaskStateTransitions) LoadByTaskID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
taskID gid.GID,
cursor *page.Cursor,
) error {
q := `
SELECT
id,
task_id,
from_state,
to_state,
reason,
created_at,
updated_at
FROM
task_state_transitions
WHERE
%s
AND task_id = @task_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.NamedArgs{"task_id": taskID}
maps.Copy(args, scope.SQLArguments())
r, err := conn.Query(ctx, q, args)
if err != nil {
return err
}
defer r.Close()
taskStateTransitions := TaskStateTransitions{}
for r.Next() {
taskStateTransition := &TaskStateTransition{}
if err := taskStateTransition.scan(r); err != nil {
return err
}
taskStateTransitions = append(taskStateTransitions, taskStateTransition)
}
if err := r.Err(); err != nil {
return err
}
*tst = taskStateTransitions
return nil
}

View File

@@ -257,3 +257,30 @@ func (s Service) ListOrganizationPeoples(
return page.NewPage(peoples, cursor), nil
}
func (s Service) ListTaskStateTransitions(
ctx context.Context,
taskID gid.GID,
cursor *page.Cursor,
) (*page.Page[*coredata.TaskStateTransition], error) {
var taskStateTransitions coredata.TaskStateTransitions
err := s.pg.WithConn(
ctx,
func(conn pg.Conn) error {
return taskStateTransitions.LoadByTaskID(
ctx,
conn,
s.scope,
taskID,
cursor,
)
},
)
if err != nil {
return nil, err
}
return page.NewPage(taskStateTransitions, cursor), nil
}