-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
259 lines (212 loc) · 5.01 KB
/
parser.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
term "github.com/buger/goterm"
"github.com/fatih/color"
)
type Parser struct {
last map[string]float64
titleColors map[string]color.Attribute
lastColors map[string]color.Attribute
unch map[string]int
ids []string
pairs string
countUnchanged int
countShouldPause int
sleep bool
shouldClear bool
start time.Time
count int
t30 time.Time
t30Count int64
t30Avg time.Duration
symbols []string
termArea int
}
func newParser() *Parser {
p := &Parser{
last: make(map[string]float64),
titleColors: make(map[string]color.Attribute),
lastColors: make(map[string]color.Attribute),
unch: make(map[string]int),
start: time.Now(),
t30: time.Now(),
termArea: termArea(),
}
for _, sym := range defaultPairsSlice {
if id, ok := Symbols[sym]; ok {
sym = id
}
if sym == "0000" {
continue
}
p.ids = append(p.ids, sym)
}
p.pairs = strings.Join(p.ids, ",")
return p
}
func (p *Parser) getLast(sym string) float64 {
pl, _ := p.last[sym]
return pl
}
func (p *Parser) getLastColor(sym string) color.Attribute {
if lc, ok := p.lastColors[sym]; ok {
return lc
}
return color.Concealed
}
func (p *Parser) getLastTitleColor(sym string) color.Attribute {
if lc, ok := p.titleColors[sym]; ok {
return lc
}
return color.Concealed
}
func (p *Parser) header(titlePad, lastPad, changePctPad, changePad int) {
var (
hsym = "Symbol"
hlast = "Last"
hpct = "%"
hchg = "+/-"
hma = "MA"
hti = "Technical"
)
head := fmt.Sprintf("%s%s%s%s%s%s%s%s %s%s%s",
hsym,
padding(hsym, titlePad),
hlast,
padding(hlast, lastPad),
hpct,
padding(hpct, changePctPad),
hchg,
padding(hchg, changePad),
hma,
padding(hma, 18),
hti,
)
head = head + padding(head, 87)
head = color.New(color.BgBlack).Set().Add(color.FgWhite).SprintFunc()(head)
term.Println(head)
}
func (p *Parser) status() {
now := time.Now()
avg := time.Duration(now.Sub(p.start).Milliseconds()/int64(p.count)) * time.Millisecond
p.t30Count++
if p.count%30 == 0 {
p.t30Avg = time.Duration(now.Sub(p.t30).Milliseconds()/p.t30Count) * time.Millisecond
p.t30Count = 0
p.t30 = time.Now()
}
if p.count == 20000 {
p.start = now.Add(-time.Duration(avg * 1000))
p.count = 1000
}
status := fmt.Sprintf("\nTarget: 180ms Avg: %s%s Trailing 30: %s%s github.com/zpkg/ticker-term",
avg,
padding(avg.String(), 6),
p.t30Avg,
padding(p.t30Avg.String(), 17),
)
status = status + padding(status, 88)
status = color.New(color.BgBlack).Set().Add(color.FgWhite).SprintFunc()(status)
status = toColor(status, color.Concealed, color.Concealed)
term.Println(status)
}
func (p *Parser) getColors(sym string, lastf float64, imap InstrumentMap) (current, title, defaultc color.Attribute) {
prev := p.getLast(sym)
p.last[sym] = lastf
switch {
case prev == 0:
current, title = color.Concealed, color.Concealed
p.lastColors[sym], p.titleColors[sym] = current, title
case lastf > prev:
current, title = color.Reset, color.Reset
p.lastColors[sym], p.titleColors[sym] = color.FgGreen, title
p.zeroUnch(sym)
case lastf < prev:
current, title = color.Reset, color.Reset
p.lastColors[sym], p.titleColors[sym] = color.FgRed, title
p.zeroUnch(sym)
default:
current = p.getLastColor(sym)
title = p.getLastTitleColor(sym)
p.lastColors[sym], p.titleColors[sym] = current, title
p.incrUnch(sym)
}
defaultc = imap.Color(sym)
return
}
///////////////////////////////
func (p *Parser) incrUnch(sym string) {
p.unch[sym] = p.unch[sym] + 1
if p.unch[sym] > 1000 {
p.lastColors[sym], p.titleColors[sym] = color.Concealed, color.Concealed
}
p.countUnchanged++
if p.countUnchanged >= len(p.symbols) {
p.countShouldPause++
}
}
func (p *Parser) zeroUnch(sym string) {
p.unch[sym] = 0
p.countShouldPause = 0
}
func (p *Parser) pause() {
p.countShouldPause = 0
term.Println(" ")
term.Println("The last 250 requests returned the same value for each symbol.")
term.Println("Switching to sleep mode. Will check for updated results every 60 seconds...")
p.sleep = true
for k, _ := range p.lastColors {
p.lastColors[k] = color.Concealed
p.titleColors[k] = color.Concealed
}
}
func (p *Parser) checkTermArea() {
if area := termArea(); area != p.termArea {
p.termArea = area
p.shouldClear = true
}
}
func (p *Parser) flush() {
p.checkTermArea()
if p.countShouldPause > 250 {
p.pause()
}
if p.shouldClear {
p.shouldClear = false
term.Clear()
}
term.Flush()
if p.sleep {
time.Sleep(time.Second * 60)
term.Clear()
}
p.countUnchanged = 0
}
/////////////////////////////
func unwrap(res *http.Response) InstrumentMap {
wrap := map[string]interface{}{}
out, err := ioutil.ReadAll(res.Body)
if err != nil {
fail(err)
}
err = json.Unmarshal(out, &wrap)
if err != nil {
fail(err)
}
delete(wrap, "time")
out, err = json.Marshal(&wrap)
if err != nil {
fail(err)
}
imap, err := newInstrumentMap(out)
if err != nil {
fail(err)
}
return imap
}