Skip to content

Commit 280d1c6

Browse files
committed
refactor: share rate chart history helpers
1 parent 2bcf0f0 commit 280d1c6

3 files changed

Lines changed: 13 additions & 20 deletions

File tree

web/src/components/case-runner.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ import { Button } from '@blueprintjs/core'
33
import { ChannelsContext, ConfigContext, I18nContext } from '../context'
44
import { css } from '@emotion/react'
55
import { SpeedIndicator } from './speed-indicator'
6-
import { RateCurve } from './rate-curve'
6+
import { appendRatePoint, RateCurve } from './rate-curve'
77
import { Subscription, zip } from 'rxjs'
88

9-
const MAX_RATE_POINTS = 60
10-
11-
function appendRate(rates: number[], rate: number) {
12-
return [...rates.slice(-(MAX_RATE_POINTS - 1)), rate]
13-
}
14-
159
export function CaseRunner(props: { name: 'upload' | 'download'; title: string }) {
1610
const createChannels = useContext(ChannelsContext)
1711
const t = useContext(I18nContext)
@@ -64,7 +58,7 @@ export function CaseRunner(props: { name: 'upload' | 'download'; title: string }
6458
}
6559
const nextRate = rate.reduce((a, b) => a + b, 0)
6660
setRate(nextRate)
67-
setRates((prevRates) => appendRate(prevRates, nextRate))
61+
setRates((prevRates) => appendRatePoint(prevRates, nextRate))
6862
},
6963
error(e) {
7064
console.error(e)

web/src/components/rate-curve.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const WIDTH = 320
77
const HEIGHT = 116
88
const PADDING_X = 12
99
const PADDING_Y = 14
10-
const MAX_POINTS = 60
10+
export const MAX_RATE_POINTS = 60
11+
12+
export function appendRatePoint(rates: number[], rate: number) {
13+
return [...rates.slice(-(MAX_RATE_POINTS - 1)), rate]
14+
}
1115

1216
export const RateCurve = memo(function RateCurve({
1317
rates,
@@ -22,7 +26,7 @@ export const RateCurve = memo(function RateCurve({
2226
const { unit } = useContext(ConfigContext)
2327
const t = useContext(I18nContext)
2428
const formatter = formatValue ?? rateFormatters[unit]
25-
const points = rates.slice(-MAX_POINTS)
29+
const points = rates.slice(-MAX_RATE_POINTS)
2630
const maxRate = Math.max(...points, 0)
2731
const { linePath, areaPath } = useMemo(() => {
2832
if (points.length < 2 || maxRate <= 0) {
@@ -69,7 +73,7 @@ export const RateCurve = memo(function RateCurve({
6973
css={css`
7074
display: block;
7175
width: 100%;
72-
height: 116px;
76+
height: ${HEIGHT}px;
7377
color: currentColor;
7478
`}
7579
>

web/src/components/run-case-once.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SpeedIndicator } from './speed-indicator'
2-
import { RateCurve } from './rate-curve'
2+
import { appendRatePoint, RateCurve } from './rate-curve'
33
import styled from '@emotion/styled'
44
import { useState, useContext, useEffect, useRef } from 'react'
55
import { useRates } from '../hooks'
@@ -40,11 +40,6 @@ type ChartItem = {
4040
}
4141

4242
const chartKeys: ChartKey[] = ['ping', 'download', 'upload']
43-
const MAX_RATE_POINTS = 60
44-
45-
function appendRate(rates: number[], rate: number) {
46-
return [...rates.slice(-(MAX_RATE_POINTS - 1)), rate]
47-
}
4843

4944
enum RunningStep {
5045
NONE = 1,
@@ -130,7 +125,7 @@ export function RunCaseOnce() {
130125
),
131126
(v) => {
132127
pushTTL(v)
133-
setPingRates((prevRates) => appendRate(prevRates, v))
128+
setPingRates((prevRates) => appendRatePoint(prevRates, v))
134129
},
135130
runId,
136131
)
@@ -158,7 +153,7 @@ export function RunCaseOnce() {
158153
(v) => {
159154
const nextRate = v.reduce((a, b) => a + b, 0)
160155
setDlRate(nextRate)
161-
setDlRates((prevRates) => appendRate(prevRates, nextRate))
156+
setDlRates((prevRates) => appendRatePoint(prevRates, nextRate))
162157
},
163158
runId,
164159
)
@@ -183,7 +178,7 @@ export function RunCaseOnce() {
183178
(v) => {
184179
const nextRate = v.reduce((a, b) => a + b, 0)
185180
setUlRate(nextRate)
186-
setUlRates((prevRates) => appendRate(prevRates, nextRate))
181+
setUlRates((prevRates) => appendRatePoint(prevRates, nextRate))
187182
},
188183
runId,
189184
)

0 commit comments

Comments
 (0)