FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
waveapps/histogram/histogram-main.go at main · wavetermdev/waveapps · GitHub
wavetermdev
/
waveapps
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
8
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
waveapps
/
histogram
/
histogram-main.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
377 lines (339 loc) · 9.04 KB
Breadcrumbs
waveapps
/
histogram
/
histogram-main.go
Copy path
File metadata and controls
377 lines (339 loc) · 9.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package
main
import
(
"bufio"
"context"
_
"embed"
"flag"
"fmt"
"math"
"os"
"sort"
"strconv"
"strings"
"github.com/wavetermdev/waveterm/pkg/vdom"
"github.com/wavetermdev/waveterm/pkg/waveapp"
)
//go:embed style.css
var
styleCSS
[]
byte
// CLI flags
var
(
numBuckets
=
flag
.
Int
(
"b"
,
10
,
"initial number of buckets"
)
minValue
=
flag
.
Float64
(
"min"
,
math
.
NaN
(),
"minimum value (auto if not specified)"
)
maxValue
=
flag
.
Float64
(
"max"
,
math
.
NaN
(),
"maximum value (auto if not specified)"
)
)
var
AppClient
=
waveapp
.
MakeClient
(waveapp.
AppOpts
{
CloseOnCtrlC
:
true
,
GlobalStyles
:
styleCSS
,
})
type
HistogramProps
struct
{
Values
[]
float64
`json:"values"`
NumBuckets
int
`json:"numBuckets"`
MinValue
*
float64
`json:"minValue"`
MaxValue
*
float64
`json:"maxValue"`
}
type
HistogramBucket
struct
{
Start
float64
End
float64
Count
int
Height
int
}
func
calcStats
(
values
[]
float64
) (
mean
,
median
,
stddev
float64
) {
if
len
(
values
)
==
0
{
return
0
,
0
,
0
}
// Calculate mean
sum
:=
0.0
for
_
,
v
:=
range
values
{
sum
+=
v
}
mean
=
sum
/
float64
(
len
(
values
))
// Calculate median
sorted
:=
make
([]
float64
,
len
(
values
))
copy
(
sorted
,
values
)
sort
.
Float64s
(
sorted
)
if
len
(
sorted
)
%
2
==
0
{
median
=
(
sorted
[
len
(
sorted
)
/
2
-
1
]
+
sorted
[
len
(
sorted
)
/
2
])
/
2
}
else
{
median
=
sorted
[
len
(
sorted
)
/
2
]
}
// Calculate standard deviation
sumSquares
:=
0.0
for
_
,
v
:=
range
values
{
diff
:=
v
-
mean
sumSquares
+=
diff
*
diff
}
stddev
=
math
.
Sqrt
(
sumSquares
/
float64
(
len
(
values
)))
return
mean
,
median
,
stddev
}
var
Histogram
=
waveapp
.
DefineComponent
[
HistogramProps
](
AppClient
,
"Histogram"
,
func
(
ctx
context.
Context
,
props
HistogramProps
)
any
{
if
len
(
props
.
Values
)
==
0
{
return
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"histogram-empty"
,
},
"Waiting for data..."
)
}
// Calculate data range and stats
min
,
max
:=
props
.
Values
[
0
],
props
.
Values
[
0
]
for
_
,
v
:=
range
props
.
Values
{
if
v
<
min
{
min
=
v
}
if
v
>
max
{
max
=
v
}
}
mean
,
median
,
stddev
:=
calcStats
(
props
.
Values
)
// Use provided min/max if set
bucketMin
:=
min
if
props
.
MinValue
!=
nil
{
bucketMin
=
*
props
.
MinValue
}
bucketMax
:=
max
if
props
.
MaxValue
!=
nil
{
bucketMax
=
*
props
.
MaxValue
}
// Create evenly spaced buckets
bucketSize
:=
(
bucketMax
-
bucketMin
)
/
float64
(
props
.
NumBuckets
)
buckets
:=
make
([]
HistogramBucket
,
props
.
NumBuckets
)
// Initialize bucket ranges
for
i
:=
range
buckets
{
buckets
[
i
].
Start
=
bucketMin
+
float64
(
i
)
*
bucketSize
buckets
[
i
].
End
=
buckets
[
i
].
Start
+
bucketSize
}
// Fill buckets
maxCount
:=
0
for
_
,
v
:=
range
props
.
Values
{
bucketValue
:=
v
if
props
.
MinValue
!=
nil
&&
v
<
*
props
.
MinValue
{
bucketValue
=
*
props
.
MinValue
}
if
props
.
MaxValue
!=
nil
&&
v
>
*
props
.
MaxValue
{
bucketValue
=
*
props
.
MaxValue
}
bucketIdx
:=
int
((
bucketValue
-
bucketMin
)
/
bucketSize
)
if
bucketIdx
>=
len
(
buckets
) {
bucketIdx
=
len
(
buckets
)
-
1
}
if
bucketIdx
>=
0
&&
bucketIdx
<
len
(
buckets
) {
buckets
[
bucketIdx
].
Count
++
if
buckets
[
bucketIdx
].
Count
>
maxCount
{
maxCount
=
buckets
[
bucketIdx
].
Count
}
}
}
// Normalize heights
const
maxHeight
=
20
for
i
:=
range
buckets
{
if
maxCount
>
0
{
buckets
[
i
].
Height
=
(
buckets
[
i
].
Count
*
maxHeight
)
/
maxCount
}
}
return
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"histogram"
,
},
// Stats header
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"histogram-stats"
,
},
"Count: "
,
len
(
props
.
Values
),
" | Range: "
,
strconv
.
FormatFloat
(
min
,
'f'
,
2
,
64
),
" - "
,
strconv
.
FormatFloat
(
max
,
'f'
,
2
,
64
),
" | Mean: "
,
strconv
.
FormatFloat
(
mean
,
'f'
,
2
,
64
),
" | Median: "
,
strconv
.
FormatFloat
(
median
,
'f'
,
2
,
64
),
" | StdDev: "
,
strconv
.
FormatFloat
(
stddev
,
'f'
,
2
,
64
),
),
// Single scrolling container for both bars and labels
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"histogram-scroll-container"
,
},
// Container for all columns and final label
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"histogram-content"
,
},
// Generate columns (bars + labels)
vdom
.
ForEachIdx
(
buckets
,
func
(
bucket
HistogramBucket
,
idx
int
)
any
{
return
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"key"
:
idx
,
"className"
:
"histogram-column"
,
},
// Count label (visible on hover)
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"count-label"
,
},
strconv
.
Itoa
(
bucket
.
Count
)),
// Bar
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
vdom
.
Classes
(
"bar"
,
vdom
.
If
(
bucket
.
Count
==
0
,
"empty"
),
),
"style"
:
map
[
string
]
any
{
"height"
:
func
()
string
{
if
bucket
.
Count
==
0
{
return
"1px"
}
return
fmt
.
Sprintf
(
"%dpx"
,
bucket
.
Height
*
8
)
}(),
},
}),
// Label for left boundary
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"x-label"
,
},
strconv
.
FormatFloat
(
bucket
.
Start
,
'f'
,
1
,
64
)),
)
}),
// Final label for rightmost boundary
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"histogram-final-label"
,
},
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"x-label"
,
},
strconv
.
FormatFloat
(
bucketMax
,
'f'
,
1
,
64
)),
),
),
),
)
},
)
var
App
=
waveapp
.
DefineComponent
(
AppClient
,
"App"
,
func
(
ctx
context.
Context
,
_
any
)
any
{
// Initialize with CLI values
initialMin
:=
(
*
float64
)(
nil
)
if
!
math
.
IsNaN
(
*
minValue
) {
initialMin
=
minValue
}
initialMax
:=
(
*
float64
)(
nil
)
if
!
math
.
IsNaN
(
*
maxValue
) {
initialMax
=
maxValue
}
values
,
_
,
setValuesFn
:=
vdom
.
UseStateWithFn
(
ctx
, []
float64
{})
numBuckets
,
setNumBuckets
:=
vdom
.
UseState
(
ctx
,
*
numBuckets
)
minValue
,
setMinValue
:=
vdom
.
UseState
(
ctx
, (
*
float64
)(
initialMin
))
maxValue
,
setMaxValue
:=
vdom
.
UseState
(
ctx
, (
*
float64
)(
initialMax
))
vdom
.
UseEffect
(
ctx
,
func
()
func
() {
done
:=
make
(
chan
bool
)
go
func
() {
defer
close
(
done
)
scanner
:=
bufio
.
NewScanner
(
os
.
Stdin
)
for
scanner
.
Scan
() {
line
:=
strings
.
TrimSpace
(
scanner
.
Text
())
if
line
==
""
{
continue
}
if
num
,
err
:=
strconv
.
ParseFloat
(
line
,
64
);
err
==
nil
{
setValuesFn
(
func
(
currentValues
[]
float64
) []
float64
{
newValues
:=
make
([]
float64
,
len
(
currentValues
)
+
1
)
copy
(
newValues
,
currentValues
)
newValues
[
len
(
currentValues
)]
=
num
return
newValues
})
AppClient
.
SendAsyncInitiation
()
}
}
}()
return
func
() {
<-
done
}
}, []
any
{})
return
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"app"
,
},
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"controls"
,
},
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"control-group"
,
},
vdom
.
H
(
"label"
,
nil
,
"Number of buckets: "
),
vdom
.
H
(
"input"
,
map
[
string
]
any
{
"type"
:
"text"
,
"value"
:
numBuckets
,
"onChange"
:
func
(
e
vdom.
VDomEvent
) {
if
n
,
err
:=
strconv
.
Atoi
(
e
.
TargetValue
);
err
==
nil
&&
n
>=
2
&&
n
<=
100
{
setNumBuckets
(
n
)
}
else
{
setNumBuckets
(
10
)
}
},
}),
),
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"control-group"
,
},
vdom
.
H
(
"label"
,
nil
,
"Min value: "
),
vdom
.
H
(
"input"
,
map
[
string
]
any
{
"type"
:
"text"
,
"value"
:
func
()
string
{
if
minValue
!=
nil
{
return
strconv
.
FormatFloat
(
*
minValue
,
'f'
,
-
1
,
64
)
}
return
""
}(),
"placeholder"
:
"auto"
,
"onChange"
:
func
(
e
vdom.
VDomEvent
) {
if
e
.
TargetValue
==
""
{
setMinValue
(
nil
)
}
else
if
val
,
err
:=
strconv
.
ParseFloat
(
e
.
TargetValue
,
64
);
err
==
nil
{
setMinValue
(
&
val
)
}
},
}),
vdom
.
If
(
minValue
!=
nil
,
vdom
.
H
(
"button"
,
map
[
string
]
any
{
"onClick"
:
func
() {
setMinValue
(
nil
) },
"className"
:
"clear-btn"
,
},
"×"
),
),
),
vdom
.
H
(
"div"
,
map
[
string
]
any
{
"className"
:
"control-group"
,
},
vdom
.
H
(
"label"
,
nil
,
"Max value: "
),
vdom
.
H
(
"input"
,
map
[
string
]
any
{
"type"
:
"text"
,
"value"
:
func
()
string
{
if
maxValue
!=
nil
{
return
strconv
.
FormatFloat
(
*
maxValue
,
'f'
,
-
1
,
64
)
}
return
""
}(),
"placeholder"
:
"auto"
,
"onChange"
:
func
(
e
vdom.
VDomEvent
) {
if
e
.
TargetValue
==
""
{
setMaxValue
(
nil
)
}
else
if
val
,
err
:=
strconv
.
ParseFloat
(
e
.
TargetValue
,
64
);
err
==
nil
{
setMaxValue
(
&
val
)
}
},
}),
vdom
.
If
(
maxValue
!=
nil
,
vdom
.
H
(
"button"
,
map
[
string
]
any
{
"onClick"
:
func
() {
setMaxValue
(
nil
) },
"className"
:
"clear-btn"
,
},
"×"
),
),
),
),
Histogram
(
HistogramProps
{
Values
:
values
,
NumBuckets
:
numBuckets
,
MinValue
:
minValue
,
MaxValue
:
maxValue
,
}),
)
},
)
func
main
() {
// Register WaveApp's flags first
AppClient
.
RegisterDefaultFlags
()
// Parse our flags
flag
.
Parse
()
if
*
numBuckets
<
2
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Number of buckets must be at least 2
\n
"
)
os
.
Exit
(
1
)
}
if
*
numBuckets
>
100
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Number of buckets must be at most 100
\n
"
)
os
.
Exit
(
1
)
}
AppClient
.
RunMain
()
}
Back
|
FazBrowse Home
|
New Git URL