@@ -4,6 +4,11 @@ package chasm
44
55import (
66 "context"
7+ "reflect"
8+ "time"
9+
10+ commonpb "go.temporal.io/api/common/v1"
11+ "google.golang.org/protobuf/proto"
712)
813
914// NoValue is a sentinel type representing no value.
@@ -46,6 +51,83 @@ type Engine interface {
4651 func (MutableContext , Component , any ) error ,
4752 ... TransitionOption ,
4853 ) ([]byte , error )
54+
55+ ListExecutions (
56+ context.Context ,
57+ reflect.Type ,
58+ * ListChasmExecutionsRequest ,
59+ ... ListChasmExecutionsOption ,
60+ ) (* ListChasmExecutionsResponse , error )
61+
62+ CountExecutions (
63+ context.Context ,
64+ reflect.Type ,
65+ * CountChasmExecutionsRequest ,
66+ ) (* CountChasmExecutionsResponse , error )
67+ }
68+
69+ type ChasmExecutionInfo struct {
70+ BusinessID string
71+ RunID string
72+ StartTime time.Time
73+ CloseTime time.Time
74+ HistoryLength int64
75+ HistorySizeBytes int64
76+ StateTransitionCount int64
77+ ChasmSearchAttributes SearchAttributesMap
78+ CustomSearchAttributes map [string ]* commonpb.Payload
79+ Memo * commonpb.Memo
80+ ChasmMemo * commonpb.Payload
81+ }
82+
83+ type ListChasmExecutionsRequest struct {
84+ NamespaceID string
85+ NamespaceName string
86+ Query string
87+ }
88+
89+ type ListChasmExecutionsOptions struct {
90+ PageSize int
91+ NextPageToken []byte
92+ }
93+
94+ type ListChasmExecutionsOption func (* ListChasmExecutionsOptions )
95+
96+ func WithPagination (
97+ pageSize int ,
98+ nextPageToken []byte ,
99+ ) ListChasmExecutionsOption {
100+ return func (req * ListChasmExecutionsOptions ) {
101+ req .PageSize = pageSize
102+ req .NextPageToken = nextPageToken
103+ }
104+ }
105+
106+ type ListChasmExecutionsResponse struct {
107+ Executions []* ChasmExecutionInfo
108+ NextPageToken []byte
109+ }
110+
111+ type CountChasmExecutionsRequest struct {
112+ NamespaceID string
113+ NamespaceName string
114+ Query string
115+ }
116+
117+ type CountChasmExecutionsResponse struct {
118+ Count int64
119+ }
120+
121+ // TypedChasmRunInfo provides type-safe access to ChasmMemo
122+ type TypedChasmExecutionInfo [M proto.Message ] struct {
123+ * ChasmExecutionInfo
124+ TypedChasmMemo M
125+ }
126+
127+ // TypedListChasmRunsResponse provides type-safe response with unmarshaled memos
128+ type TypedListChasmExecutionsResponse [M proto.Message ] struct {
129+ Executions []* TypedChasmExecutionInfo [M ]
130+ NextPageToken []byte
49131}
50132
51133type BusinessIDReusePolicy int
@@ -302,3 +384,44 @@ func engineFromContext(
302384 }
303385 return e
304386}
387+
388+ func ListExecutions [C Component , M proto.Message ](
389+ ctx context.Context ,
390+ request * ListChasmExecutionsRequest ,
391+ opts ... ListChasmExecutionsOption ,
392+ ) (* TypedListChasmExecutionsResponse [M ], error ) {
393+ archetypeType := reflect .TypeFor [C ]()
394+ response , err := engineFromContext (ctx ).ListExecutions (ctx , archetypeType , request , opts ... )
395+ if err != nil {
396+ return nil , err
397+ }
398+
399+ // Convert response, unmarshaling ChasmMemo to type M
400+ typedExecutions := make ([]* TypedChasmExecutionInfo [M ], len (response .Executions ))
401+ for i , execution := range response .Executions {
402+ var typedMemo M
403+ if len (execution .ChasmMemo .Data ) > 0 {
404+ msg := reflect .New (reflect .TypeFor [M ]()).Interface ().(M )
405+ if err := proto .Unmarshal (execution .ChasmMemo .Data , msg ); err == nil {
406+ typedMemo = msg
407+ }
408+ }
409+ typedExecutions [i ] = & TypedChasmExecutionInfo [M ]{
410+ ChasmExecutionInfo : execution ,
411+ TypedChasmMemo : typedMemo ,
412+ }
413+ }
414+
415+ return & TypedListChasmExecutionsResponse [M ]{
416+ Executions : typedExecutions ,
417+ NextPageToken : response .NextPageToken ,
418+ }, nil
419+ }
420+
421+ func CountExecutions [C Component ](
422+ ctx context.Context ,
423+ request * CountChasmExecutionsRequest ,
424+ ) (* CountChasmExecutionsResponse , error ) {
425+ archetypeType := reflect .TypeFor [C ]()
426+ return engineFromContext (ctx ).CountExecutions (ctx , archetypeType , request )
427+ }
0 commit comments