Skip to content

Commit 98933f7

Browse files
authored
fix(frontend): avoid loading ui issue (#25)
* fix(frontend): avoid loading ui issue Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> * update Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> * update Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com> --------- Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
1 parent f7e1dab commit 98933f7

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

web/frontend/src/components/application/NodeJSMetric.jsx

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import colorSetLatency from '~/components/metrics/latency.module.css'
1717
function NodeJSMetric ({
1818
title = '',
1919
metricURL = '',
20-
initialLoading = true,
20+
initialLoading = false,
2121
dataValues = [],
2222
unit = '',
2323
options = [{ label: '', internalKey: '', unit: '' }],
@@ -35,33 +35,31 @@ function NodeJSMetric ({
3535
const colorStyles = metricURL === 'mem' ? colorSetMem : metricURL === 'cpu' ? colorSetCpu : colorSetLatency
3636

3737
useEffect(() => {
38-
if (!initialLoading) {
39-
setBorderexBoxClassName(`${styles.borderexBoxContainer}`)
40-
let newValues = []
41-
let lowerMaxY = 0
42-
if (dataValues.length > 0) {
43-
if (metricURL === 'latency') {
44-
newValues = dataValues.map(({ date, ...rest }) => ({
45-
time: new Date(date),
46-
...rest
47-
}))
48-
} else {
49-
dataValues.forEach(dataValue => {
50-
newValues.push({
51-
time: new Date(dataValue.date),
52-
values: [...options.map(option => dataValue[option.internalKey])]
53-
})
54-
lowerMaxY = Math.max(lowerMaxY, ...options.map(option => dataValue[option.internalKey]))
55-
})
56-
}
57-
setSeriesValues([...newValues])
58-
setShowNoResult(false)
59-
setLowerMaxY(lowerMaxY)
38+
setBorderexBoxClassName(`${styles.borderexBoxContainer}`)
39+
let newValues = []
40+
let lowerMaxY = 0
41+
if (dataValues.length > 0) {
42+
if (metricURL === 'latency') {
43+
newValues = dataValues.map(({ date, ...rest }) => ({
44+
time: new Date(date),
45+
...rest
46+
}))
6047
} else {
61-
setShowNoResult(true)
48+
dataValues.forEach(dataValue => {
49+
newValues.push({
50+
time: new Date(dataValue.date),
51+
values: [...options.map(option => dataValue[option.internalKey])]
52+
})
53+
lowerMaxY = Math.max(lowerMaxY, ...options.map(option => dataValue[option.internalKey]))
54+
})
6255
}
56+
setSeriesValues([...newValues])
57+
setShowNoResult(false)
58+
setLowerMaxY(lowerMaxY)
59+
} else {
60+
setShowNoResult(true)
6361
}
64-
}, [initialLoading])
62+
}, [])
6563

6664
const generateLegend = () => {
6765
return (

web/frontend/src/components/application/NodeJSMetrics.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ function NodeJSMetrics ({
2626

2727
useInterval(async () => {
2828
try {
29-
setInitialLoading(true)
3029
const data = await getApiMetricsPod(runtimePid)
3130
setAllData(data)
3231
setLatestRefreshDate(new Date())

web/frontend/src/components/metrics/LatencyChart.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ const LatencyChart = ({
130130
.attr('y', d => y(d[percentile] || 0))
131131
.attr('width', barWidth)
132132
.attr('height', d => h - marginOnY - y(d[percentile] || 0))
133+
.transition()
134+
.duration(1000)
133135
})
134136

135137
// Tooltip

web/frontend/src/components/metrics/NodeJSMetricChart.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ const NodeJSMetricChart = ({
129129
.attr('class', `${styles.line} ${colorStyles[`color-${i}`]}`)
130130
.datum(latestData)
131131
.attr('d', d3.line()
132-
.x(p => {
133-
return x(p.time)
134-
})
132+
.x(p => x(p.time))
135133
.y((p) => y(p.values[i]))
136134
.curve(d3.curveBasis)
137135
)
136+
.transition()
137+
.duration(1000)
138138
.node()
139139
)
140140

web/frontend/src/components/metrics/ServicesMetrics.jsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import PropTypes from 'prop-types'
33
import { TRANSPARENT, RICH_BLACK, OPACITY_30, WHITE } from '@platformatic/ui-components/src/components/constants'
44
import styles from './ServicesMetrics.module.css'
@@ -32,9 +32,8 @@ const ServicesMetrics = React.forwardRef(({
3232
const [latestRefreshDate, setLatestRefreshDate] = useState(new Date())
3333
const { runtimePid } = useAdminStore()
3434

35-
useInterval(async () => {
35+
const getData = async () => {
3636
try {
37-
setInitialLoading(true)
3837
setAllData(await getApiMetricsPod(runtimePid))
3938
setServiceData(await getApiMetricsPodService(runtimePid, serviceId))
4039
setLatestRefreshDate(new Date())
@@ -43,8 +42,16 @@ const ServicesMetrics = React.forwardRef(({
4342
} finally {
4443
setInitialLoading(false)
4544
}
45+
}
46+
47+
useInterval(() => {
48+
getData()
4649
}, REFRESH_INTERVAL_METRICS)
4750

51+
useEffect(() => {
52+
getData()
53+
}, [serviceId])
54+
4855
// FIXME: unify with NodeJSMetrics logic (since it's duplicated)
4956
function generateLegend (labels, colorStyles) {
5057
return (

0 commit comments

Comments
 (0)