diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 55 |
1 files changed, 37 insertions, 18 deletions
| @@ -19,24 +19,24 @@ type Transaction struct { | |||
| 19 | Timestamp int64 | 19 | Timestamp int64 |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | type TransactionSummary[IN Transaction, OUT Transaction] struct { | 22 | type TransactionSummary[IN Transaction, OUT string] struct { |
| 23 | totalTransaction int | 23 | totalTransaction int |
| 24 | totalAmount int | ||
| 25 | average int | 24 | average int |
| 26 | } | 25 | } |
| 27 | 26 | ||
| 28 | func (t *TransactionSummary[IN, OUT]) Apply(ctx context.Context, key string, window core.Window, in []Transaction, out transport.Emitter[Transaction]) error { | 27 | func (t *TransactionSummary[IN, OUT]) Apply(ctx context.Context, key string, window core.Window, in []Transaction, out operator.Emitter[string]) error { |
| 28 | totalAmount := 0 | ||
| 29 | for _, v := range in { | 29 | for _, v := range in { |
| 30 | t.totalAmount += v.Amount | 30 | totalAmount += v.Amount |
| 31 | out.Emit(ctx, core.StreamRecord[Transaction]{ | ||
| 32 | Value: v, | ||
| 33 | Type: core.RecordData, | ||
| 34 | Timestamp: v.Timestamp, | ||
| 35 | }) | ||
| 36 | } | 31 | } |
| 37 | 32 | ||
| 38 | return nil | 33 | msg := fmt.Sprintf("Khách: %s | Số đơn: %d | Tổng tiền: %d$", key, len(in), totalAmount) |
| 39 | } | 34 | |
| 35 | return out.Emit(ctx, core.StreamRecord[string]{ | ||
| 36 | Value: msg, | ||
| 37 | Type: core.RecordData, | ||
| 38 | Timestamp: window.MaxTimestamp(), | ||
| 39 | })} | ||
| 40 | 40 | ||
| 41 | func main() { | 41 | func main() { |
| 42 | // init | 42 | // init |
| @@ -55,25 +55,45 @@ func main() { | |||
| 55 | { | 55 | { |
| 56 | User: "Sang", | 56 | User: "Sang", |
| 57 | Amount: 4, | 57 | Amount: 4, |
| 58 | Timestamp: 1200, | 58 | Timestamp: 1500, |
| 59 | }, | ||
| 60 | { | ||
| 61 | User: "Sang", | ||
| 62 | Amount: 10, | ||
| 63 | Timestamp: 2000, | ||
| 64 | }, | ||
| 65 | { | ||
| 66 | User: "Sang", | ||
| 67 | Amount: 12, | ||
| 68 | Timestamp: 2500, | ||
| 59 | }, | 69 | }, |
| 60 | { | 70 | { |
| 61 | User: "Sang", | 71 | User: "Sang", |
| 62 | Amount: 10, | 72 | Amount: 10, |
| 63 | Timestamp: 1600, | 73 | Timestamp: 3000, |
| 64 | }, | 74 | }, |
| 65 | } | 75 | } |
| 66 | 76 | ||
| 67 | ch1 := make(chan core.StreamRecord[Transaction], len(txList)) | 77 | ch1 := make(chan core.StreamRecord[Transaction], len(txList)) |
| 68 | ch2 := make(chan core.StreamRecord[Transaction], len(txList)) | 78 | ch2 := make(chan core.StreamRecord[Transaction], len(txList)) |
| 79 | ch3 := make(chan core.StreamRecord[Transaction], len(txList)) | ||
| 80 | ch4 := make(chan core.StreamRecord[string], len(txList)) | ||
| 69 | 81 | ||
| 70 | wo := operator.NewWatermarkGeneratorOperator[Transaction](0, func(in Transaction) int64 { | 82 | wo := operator.NewWatermarkGeneratorOperator[Transaction](0, func(in Transaction) int64 { |
| 71 | return in.Timestamp | 83 | return in.Timestamp |
| 72 | }) | 84 | }) |
| 73 | 85 | ||
| 86 | ko := operator.NewKeyByOperator[Transaction](func(in Transaction) string { | ||
| 87 | return in.User | ||
| 88 | }) | ||
| 89 | |||
| 90 | wop := operator.NewWindowOperator[Transaction, string](core.NewTumblingWindows[Transaction](1000), &TransactionSummary[Transaction, string]{}) | ||
| 91 | |||
| 74 | var wg sync.WaitGroup | 92 | var wg sync.WaitGroup |
| 75 | 93 | ||
| 76 | runtime.RunTask[Transaction, Transaction](ctx, transport.NewLocalReceiveChannel(ch1), transport.NewLocalEmitChannel(ch2), wo) | 94 | runtime.RunTask(ctx, transport.NewLocalReceiveChannel(ch1), transport.NewLocalEmitChannel(ch2), wo) |
| 95 | runtime.RunTask(ctx, transport.NewLocalReceiveChannel(ch2), transport.NewLocalEmitChannel(ch3), ko) | ||
| 96 | runtime.RunTask(ctx, transport.NewLocalReceiveChannel(ch3), transport.NewLocalEmitChannel(ch4), wop) | ||
| 77 | 97 | ||
| 78 | wg.Go(func() { | 98 | wg.Go(func() { |
| 79 | for i, tx := range txList { | 99 | for i, tx := range txList { |
| @@ -84,13 +104,12 @@ func main() { | |||
| 84 | }) | 104 | }) |
| 85 | 105 | ||
| 86 | wg.Go(func() { | 106 | wg.Go(func() { |
| 87 | for v := range ch2 { | 107 | for v := range ch4 { |
| 88 | if v.Type == core.RecordData { | 108 | if v.Type == core.RecordData { |
| 89 | fmt.Println("data: ", v.Value) | 109 | fmt.Println("Total Amount after: ", v.Value) |
| 90 | } else { | ||
| 91 | fmt.Println("watermark: ", v.Timestamp) | ||
| 92 | } | 110 | } |
| 93 | } | 111 | } |
| 94 | }) | 112 | }) |
| 113 | |||
| 95 | wg.Wait() | 114 | wg.Wait() |
| 96 | } | 115 | } |
