@@ -19,6 +19,8 @@ import { Box, Text } from "ink"
19
19
20
20
import type { GridSpec , Worker } from "./types.js"
21
21
22
+ import { avg } from "./stats.js"
23
+
22
24
type Props = {
23
25
gridModels : GridSpec [ ]
24
26
workers : Worker [ ] [ ]
@@ -51,63 +53,12 @@ export default class Timeline extends React.PureComponent<Props> {
51
53
} , 0 )
52
54
}
53
55
54
- /** @return the accumulated `total` and count `N` across a set of `workers` for the given `timeIdx` */
55
- private accum ( workers : Worker [ ] , timeIdx : number , field : "valueTotal" | "metricIdxTotal" ) {
56
- return workers . reduce (
57
- ( A , worker ) => {
58
- const history = worker . metricHistory
59
- if ( history [ timeIdx ] ) {
60
- A . total += history [ timeIdx ] [ field ]
61
- A . N += history [ timeIdx ] . N
62
- }
63
- return A
64
- } ,
65
- { total : 0 , N : 0 }
66
- )
67
- }
68
-
69
- /** @return average metric value across a set of `workers` for the given `timeIdx` */
70
- private avg ( workers : Worker [ ] , timeIdx : number , field : "valueTotal" | "metricIdxTotal" ) : number {
71
- const { total, N } = this . accum ( workers , timeIdx , field )
72
- if ( N === 0 ) {
73
- if ( timeIdx === 0 ) return 0
74
- else {
75
- for ( let t = timeIdx - 1 ; t >= 0 ; t -- ) {
76
- const { total, N } = this . accum ( workers , t , field )
77
- if ( N !== 0 ) {
78
- return Math . round ( total / N )
79
- }
80
- }
81
- return 0
82
- }
83
- }
84
-
85
- return Math . round ( total / N )
86
- }
87
-
88
- /** @return long-term average, averaged over time and across a set of `workers` */
89
- private longTermAvg ( workers : Worker [ ] , nTimes : number ) {
90
- const { total, N } = Array ( nTimes )
91
- . fill ( 0 )
92
- . map ( ( _ , timeIdx ) => this . accum ( workers , timeIdx , "valueTotal" ) )
93
- . reduce (
94
- ( A , { total, N } ) => {
95
- A . total += total
96
- A . N += N
97
- return A
98
- } ,
99
- { total : 0 , N : 0 }
100
- )
101
-
102
- return Math . round ( total / N )
103
- }
104
-
105
56
/**
106
57
* Render one cell to represent the average over the given `workers`
107
58
* for the given grid, for the given time.
108
59
*/
109
60
private cell ( workers : Worker [ ] , spec : GridSpec , timeIdx : number , isLatest : boolean ) {
110
- const metricIdx = this . avg ( workers , timeIdx , "metricIdxTotal" )
61
+ const metricIdx = avg ( workers , "metricIdxTotal" , timeIdx )
111
62
const style = spec . states [ metricIdx ] ? spec . states [ metricIdx ] . style : { color : "gray" , dimColor : true }
112
63
113
64
return (
@@ -135,12 +86,6 @@ export default class Timeline extends React.PureComponent<Props> {
135
86
< Text > { spec . title . padStart ( this . maxLabelLength ) } </ Text >
136
87
</ Box >
137
88
< Box marginLeft = { 1 } > { this . cells ( workers , spec , nTimes , timeStartIdx ) } </ Box >
138
- < Text >
139
- { Math . round ( this . avg ( workers , nTimes - 1 , "valueTotal" ) )
140
- . toFixed ( )
141
- . padStart ( 3 ) + "%" }
142
- </ Text >
143
- < Text color = "yellow" > μ={ Math . round ( this . longTermAvg ( workers , nTimes ) ) + "%" } </ Text >
144
89
</ React . Fragment >
145
90
)
146
91
}
@@ -155,7 +100,7 @@ export default class Timeline extends React.PureComponent<Props> {
155
100
156
101
// to help us compute whether we are about to overflow terminal width
157
102
const maxLabelLength = this . props . gridModels . reduce ( ( N , spec ) => {
158
- return Math . max ( N , "100% μ=100%" . length + spec . title . length )
103
+ return Math . max ( N , spec . title . length )
159
104
} , 0 )
160
105
161
106
// once we overflow, display the suffix of history information, starting at this index
0 commit comments