-
Notifications
You must be signed in to change notification settings - Fork 7
/
bookwatcher.go
153 lines (126 loc) · 3.92 KB
/
bookwatcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Package main implements the bookwatcher for terminal and JSON output from demo applications
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
"time"
"github.com/flomesh-io/fsm/demo/cmd/common"
)
const (
bookBuyerPort = 8080
bookStoreV1Port = 8081
bookStoreV2Port = 8082
bookThiefPort = 8083
)
func clearScreen() {
fmt.Print("\033[H\033[2J")
}
func printGreenln(msg string) {
fmt.Printf("\033[32m%s\033[0m\n", msg)
}
func printYellowln(msg string) {
fmt.Printf("\033[33m%s\033[0m\n", msg)
}
func printRedln(msg string) {
fmt.Printf("\033[31m%s\033[0m\n", msg)
}
func getBookData(dest interface{}, port int, wg *sync.WaitGroup, errc chan<- error) {
defer wg.Done()
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/raw", port))
if err != nil {
errc <- fmt.Errorf("error fetching data (port %d): %w", port, err)
return
}
defer func() {
if err := resp.Body.Close(); err != nil {
errc <- fmt.Errorf("error closing response (port %d): %w", port, err)
}
}()
output, err := ioutil.ReadAll(resp.Body)
if err != nil {
errc <- fmt.Errorf("error reading data (port %d): %w", port, err)
return
}
err = json.Unmarshal(output, dest)
if err != nil {
errc <- fmt.Errorf("error unmarshalling data (port %d): %w", port, err)
}
}
func main() {
bookBuyerPurchases := &common.BookBuyerPurchases{}
bookThiefThievery := &common.BookThiefThievery{}
bookStorePurchasesV1 := &common.BookStorePurchases{}
bookStorePurchasesV2 := &common.BookStorePurchases{}
wg := &sync.WaitGroup{}
errc := make(chan error, 4)
for {
clearScreen()
bookBuyerPurchasesTemp := *bookBuyerPurchases
wg.Add(1)
go getBookData(bookBuyerPurchases, bookBuyerPort, wg, errc)
bookThiefThieveryTemp := *bookThiefThievery
wg.Add(1)
go getBookData(bookThiefThievery, bookThiefPort, wg, errc)
bookStorePurchasesV1Temp := *bookStorePurchasesV1
wg.Add(1)
go getBookData(bookStorePurchasesV1, bookStoreV1Port, wg, errc)
bookStorePurchasesV2Temp := *bookStorePurchasesV2
wg.Add(1)
go getBookData(bookStorePurchasesV2, bookStoreV2Port, wg, errc)
complete := make(chan bool)
go func() {
wg.Wait()
close(complete)
}()
select {
case err := <-errc:
wg.Wait()
close(errc)
log.Fatal(err)
case <-complete:
}
bookBuyerHasChanged := bookBuyerPurchases.BooksBought-bookBuyerPurchasesTemp.BooksBought != 0 ||
bookBuyerPurchases.BooksBoughtV1-bookBuyerPurchasesTemp.BooksBoughtV1 != 0 ||
bookBuyerPurchases.BooksBoughtV2-bookBuyerPurchasesTemp.BooksBoughtV2 != 0
bookThiefHasChanged := bookThiefThievery.BooksStolen-bookThiefThieveryTemp.BooksStolen != 0 ||
bookThiefThievery.BooksStolenV1-bookThiefThieveryTemp.BooksStolenV1 != 0 ||
bookThiefThievery.BooksStolenV2-bookThiefThieveryTemp.BooksStolenV2 != 0
bookStoreV1HasChanged := bookStorePurchasesV1.BooksSold-bookStorePurchasesV1Temp.BooksSold != 0
bookStoreV2HasChanged := bookStorePurchasesV2.BooksSold-bookStorePurchasesV2Temp.BooksSold != 0
printFunc := printYellowln
if bookBuyerHasChanged {
printFunc = printGreenln
}
printFunc(fmt.Sprintf(
"bookbuyer Books bought: %d V1 books bought: %d V2 books bought: %d",
bookBuyerPurchases.BooksBought,
bookBuyerPurchases.BooksBoughtV1,
bookBuyerPurchases.BooksBoughtV2,
))
printFunc = printYellowln
if bookThiefHasChanged {
printFunc = printRedln
}
printFunc(fmt.Sprintf(
"bookthief Books stolen: %d V1 books stolen: %d V2 books stolen: %d",
bookThiefThievery.BooksStolen,
bookThiefThievery.BooksStolenV1,
bookThiefThievery.BooksStolenV2,
))
printFunc = printYellowln
if bookStoreV1HasChanged {
printFunc = printGreenln
}
printFunc(fmt.Sprintf("bookstore v1 Books sold: %d", bookStorePurchasesV1.BooksSold))
printFunc = printYellowln
if bookStoreV2HasChanged {
printFunc = printGreenln
}
printFunc(fmt.Sprintf("bookstore v2 Books sold: %d", bookStorePurchasesV2.BooksSold))
time.Sleep(1 * time.Second)
}
}