diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 96 |
1 files changed, 96 insertions, 0 deletions
| @@ -0,0 +1,96 @@ | |||
| 1 | package main | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "fmt" | ||
| 6 | "strconv" | ||
| 7 | "sync" | ||
| 8 | |||
| 9 | "flux/core" | ||
| 10 | "flux/core/operator" | ||
| 11 | "flux/runtime" | ||
| 12 | "flux/state" | ||
| 13 | "flux/transport" | ||
| 14 | ) | ||
| 15 | |||
| 16 | type Transaction struct { | ||
| 17 | User string | ||
| 18 | Amount int | ||
| 19 | Timestamp int64 | ||
| 20 | } | ||
| 21 | |||
| 22 | type TransactionSummary[IN Transaction, OUT Transaction] struct { | ||
| 23 | totalTransaction int | ||
| 24 | totalAmount int | ||
| 25 | average int | ||
| 26 | } | ||
| 27 | |||
| 28 | func (t *TransactionSummary[IN, OUT]) Apply(ctx context.Context, key string, window core.Window, in []Transaction, out transport.Emitter[Transaction]) error { | ||
| 29 | for _, v := range in { | ||
| 30 | t.totalAmount += v.Amount | ||
| 31 | out.Emit(ctx, core.StreamRecord[Transaction]{ | ||
| 32 | Value: v, | ||
| 33 | Type: core.RecordData, | ||
| 34 | Timestamp: v.Timestamp, | ||
| 35 | }) | ||
| 36 | } | ||
| 37 | |||
| 38 | return nil | ||
| 39 | } | ||
| 40 | |||
| 41 | func main() { | ||
| 42 | // init | ||
| 43 | ctx := context.Background() | ||
| 44 | |||
| 45 | backend := state.NewMemoryStateBackend(1) | ||
| 46 | |||
| 47 | ctx = core.InjectStateBackend(ctx, backend) | ||
| 48 | |||
| 49 | txList := []Transaction{ | ||
| 50 | { | ||
| 51 | User: "Sang", | ||
| 52 | Amount: 1, | ||
| 53 | Timestamp: 1000, | ||
| 54 | }, | ||
| 55 | { | ||
| 56 | User: "Sang", | ||
| 57 | Amount: 4, | ||
| 58 | Timestamp: 1200, | ||
| 59 | }, | ||
| 60 | { | ||
| 61 | User: "Sang", | ||
| 62 | Amount: 10, | ||
| 63 | Timestamp: 1600, | ||
| 64 | }, | ||
| 65 | } | ||
| 66 | |||
| 67 | ch1 := make(chan core.StreamRecord[Transaction], len(txList)) | ||
| 68 | ch2 := make(chan core.StreamRecord[Transaction], len(txList)) | ||
| 69 | |||
| 70 | wo := operator.NewWatermarkGeneratorOperator[Transaction](0, func(in Transaction) int64 { | ||
| 71 | return in.Timestamp | ||
| 72 | }) | ||
| 73 | |||
| 74 | var wg sync.WaitGroup | ||
| 75 | |||
| 76 | runtime.RunTask[Transaction, Transaction](ctx, transport.NewLocalReceiveChannel(ch1), transport.NewLocalEmitChannel(ch2), wo) | ||
| 77 | |||
| 78 | wg.Go(func() { | ||
| 79 | for i, tx := range txList { | ||
| 80 | ch1 <- core.NewDataRecord(strconv.Itoa(i), tx, tx.Timestamp) | ||
| 81 | } | ||
| 82 | |||
| 83 | close(ch1) | ||
| 84 | }) | ||
| 85 | |||
| 86 | wg.Go(func() { | ||
| 87 | for v := range ch2 { | ||
| 88 | if v.Type == core.RecordData { | ||
| 89 | fmt.Println("data: ", v.Value) | ||
| 90 | } else { | ||
| 91 | fmt.Println("watermark: ", v.Timestamp) | ||
| 92 | } | ||
| 93 | } | ||
| 94 | }) | ||
| 95 | wg.Wait() | ||
| 96 | } | ||
