@@ -5,7 +5,7 @@ import { Tooltip } from 'antd';
55import { PowerCell } from './TableCells' ;
66import { fixed , color } from '../../utils/common' ;
77import { State } from '../ComponentsLib' ;
8- import { api , PATCH , GET } from '../../utils/serverAPI' ;
8+ import { api , PATCH , GET } from '../../utils/serverAPI' ;
99import '../style/PowerSummaryTable.css' ;
1010
1111function PowerSummaryTableToolTip ( { title, statusColor } ) {
@@ -18,8 +18,13 @@ function PowerSummaryTableToolTip({ title, statusColor }) {
1818 ) ;
1919}
2020
21+ PowerSummaryTableToolTip . propTypes = {
22+ title : PropTypes . string . isRequired ,
23+ statusColor : PropTypes . string . isRequired ,
24+ } ;
25+
2126function PowerSummaryTable ( {
22- title, data = [ ] , total = 0 , percent = 0 , deviceId = 'MPW1'
27+ title, data = [ ] , total = 0 , percent = 0 , deviceId = 'MPW1' ,
2328} ) {
2429 const [ thermalData , setThermalData ] = useState ( {
2530 ambientTypical : 25 ,
@@ -28,7 +33,7 @@ function PowerSummaryTable({
2833 } ) ;
2934
3035 const [ powerData , setPowerData ] = useState ( {
31- powerBudget : 1.00 ,
36+ powerBudget : 1.0 ,
3237 fpgaScaling : 25 ,
3338 pcScaling : 25 ,
3439 } ) ;
@@ -43,23 +48,23 @@ function PowerSummaryTable({
4348 useEffect ( ( ) => {
4449 const fetchData = async ( ) => {
4550 try {
46- GET ( api . consumption ( 'consumption' , deviceId ) , ( result ) => {
47- if ( result && result . specification ) {
48- const { specification } = result ;
49- setThermalData ( {
50- ambientTypical : specification . thermal . ambient . typical ,
51- ambientWorstCase : specification . thermal . ambient . worstcase ,
52- thetaJa : specification . thermal . theta_ja ,
53- } ) ;
54- setPowerData ( {
55- powerBudget : specification . power . budget ,
56- fpgaScaling : specification . power . typical_dynamic_scaling . fpga_complex * 100 ,
57- pcScaling : specification . power . typical_dynamic_scaling . processing_complex * 100 ,
58- } ) ;
59- }
60- } ) ;
51+ const result = await GET ( api . consumption ( 'consumption' , deviceId ) ) ;
52+ if ( result && result . specification ) {
53+ const { specification } = result ;
54+ setThermalData ( {
55+ ambientTypical : specification . thermal . ambient . typical ,
56+ ambientWorstCase : specification . thermal . ambient . worstcase ,
57+ thetaJa : specification . thermal . theta_ja ,
58+ } ) ;
59+ setPowerData ( {
60+ powerBudget : specification . power . budget ,
61+ fpgaScaling : specification . power . typical_dynamic_scaling . fpga_complex * 100 ,
62+ pcScaling : specification . power . typical_dynamic_scaling . processing_complex * 100 ,
63+ } ) ;
64+ }
6165 } catch ( error ) {
62- console . error ( "Error fetching data:" , error ) ;
66+ // Log error for debugging purposes
67+ console . error ( 'Error fetching data:' , error . message ) ;
6368 }
6469 } ;
6570
@@ -68,12 +73,10 @@ function PowerSummaryTable({
6873
6974 const updateBackend = async ( updatedData ) => {
7075 try {
71- PATCH ( api . deviceInfo ( deviceId ) , updatedData , ( ) => {
72- console . log ( 'Backend updated successfully' ) ;
73- fetchData ( ) ; // refetching to sync UI with backend
74- } ) ;
76+ await PATCH ( api . deviceInfo ( deviceId ) , updatedData ) ;
7577 } catch ( error ) {
76- console . error ( "Error updating backend:" , error ) ;
78+ // Log error for debugging purposes
79+ console . error ( 'Error updating backend:' , error . message ) ;
7780 }
7881 } ;
7982
@@ -96,39 +99,49 @@ function PowerSummaryTable({
9699 power : updatedPowerData ,
97100 } ,
98101 } ;
99-
100102 updateBackend ( updatedData ) ;
101103 } ;
102104
103105 const enforceNumericInput = ( e ) => {
104- const value = e . target . value ;
105- const valid = / ^ - ? \d * \. ? \d * % ? $ / . test ( value ) ;
106- if ( ! valid ) {
106+ const { value } = e . target ;
107+ if ( ! / ^ - ? \d * \. ? \d * % ? $ / . test ( value ) ) {
107108 e . target . value = value . slice ( 0 , - 1 ) ;
108109 }
109110 } ;
110111
111112 const handleKeyDown = ( e , nextFieldRef ) => {
112- if ( e . key === 'Enter' && nextFieldRef && nextFieldRef . current ) {
113+ if ( e . key === 'Enter' && nextFieldRef ? .current ) {
113114 nextFieldRef . current . focus ( ) ;
114115 }
115116 } ;
116117
117- const getErrors = ( messages ) => messages ?. filter ( ( item ) => item . some ( ( inner ) => inner . type === 'error' ) ) || [ ] ;
118- const getWarnings = ( messages ) => messages ?. filter ( ( item ) => item . some ( ( inner ) => inner . type === 'warn' ) ) || [ ] ;
118+ const getErrors = ( messages ) => (
119+ messages ?. filter ( ( item ) => item . some ( ( inner ) => inner . type === 'error' ) ) || [ ]
120+ ) ;
121+ const getWarnings = ( messages ) => (
122+ messages ?. filter ( ( item ) => item . some ( ( inner ) => inner . type === 'warn' ) ) || [ ]
123+ ) ;
119124
120- const buildMessage = ( messages ) =>
121- messages . reduce ( ( acc , item , currentIndex ) => {
122- item . forEach ( ( i , index ) => acc . push ( < span key = { `${ currentIndex } +${ index } ` } > { i . text } < br /> </ span > ) ) ;
123- return acc ;
124- } , [ ] ) ;
125+ const buildMessage = ( messages ) => messages . reduce ( ( acc , item , currentIndex ) => {
126+ item . forEach ( ( i , index ) => acc . push (
127+ // <span key={`${currentIndex}-${index}`}>
128+ < span key = { i . id || `${ currentIndex } -${ index } ` } >
129+ { i . text }
130+ < br />
131+ </ span > ,
132+ ) ) ;
133+ return acc ;
134+ } , [ ] ) ;
125135
126136 const message = ( messages ) => {
127137 const errors = getErrors ( messages ) ;
128138 return errors . length > 0 ? buildMessage ( errors ) : buildMessage ( getWarnings ( messages ) ) ;
129139 } ;
130140
131- const statusColor = ( messages ) => color ( getErrors ( messages ) . length > 0 , getWarnings ( messages ) . length > 0 ) ;
141+ const statusColor = ( messages ) => color (
142+ getErrors ( messages ) . length > 0 ,
143+ getWarnings ( messages ) . length > 0 ,
144+ ) ;
132145
133146 return (
134147 < div className = "pst-container main-border" >
@@ -138,7 +151,7 @@ function PowerSummaryTable({
138151 < table className = "spec-table" >
139152 < thead >
140153 < tr >
141- < th > </ th >
154+ < th / >
142155 < th className = "typical-header" > Typical</ th >
143156 < th className = "worst-header" > Worst-Case</ th >
144157 </ tr >
@@ -154,7 +167,8 @@ function PowerSummaryTable({
154167 onInput = { enforceNumericInput }
155168 ref = { ambientTypicalRef }
156169 onKeyDown = { ( e ) => handleKeyDown ( e , ambientWorstCaseRef ) }
157- /> °C
170+ />
171+ °C
158172 </ td >
159173 < td className = "value-cell" >
160174 < input
@@ -164,7 +178,8 @@ function PowerSummaryTable({
164178 onInput = { enforceNumericInput }
165179 ref = { ambientWorstCaseRef }
166180 onKeyDown = { ( e ) => handleKeyDown ( e , thetaJaRef ) }
167- /> °C
181+ />
182+ °C
168183 </ td >
169184 </ tr >
170185 < tr className = "theta-row" >
@@ -177,12 +192,12 @@ function PowerSummaryTable({
177192 onInput = { enforceNumericInput }
178193 ref = { thetaJaRef }
179194 onKeyDown = { ( e ) => handleKeyDown ( e , powerBudgetRef ) }
180- /> °C/W
195+ />
196+ °C/W
181197 </ td >
182198 </ tr >
183199 </ tbody >
184200 </ table >
185-
186201 < div className = "spec-header" > Power Specification</ div >
187202 < table className = "power-spec-table" >
188203 < tbody >
@@ -196,7 +211,8 @@ function PowerSummaryTable({
196211 onInput = { enforceNumericInput }
197212 ref = { powerBudgetRef }
198213 onKeyDown = { ( e ) => handleKeyDown ( e , fpgaScalingRef ) }
199- /> W
214+ />
215+ W
200216 </ td >
201217 </ tr >
202218 < tr >
@@ -210,7 +226,8 @@ function PowerSummaryTable({
210226 onInput = { enforceNumericInput }
211227 ref = { fpgaScalingRef }
212228 onKeyDown = { ( e ) => handleKeyDown ( e , pcScalingRef ) }
213- /> %
229+ />
230+ %
214231 </ td >
215232 < td className = "scaling-cell" >
216233 PC:
@@ -220,28 +237,29 @@ function PowerSummaryTable({
220237 onChange = { ( e ) => handleFieldUpdate ( 'pcScaling' , e . target . value ) }
221238 onInput = { enforceNumericInput }
222239 ref = { pcScalingRef }
223- /> %
240+ />
241+ %
224242 </ td >
225243 </ tr >
226244 </ tbody >
227245 </ table >
228246 </ div >
229247 ) }
230-
231248 < div className = "no-wrap bold-text-title" > { title || 'FPGA Complex and Core Power' } </ div >
232249 < div >
233250 < table className = "pst-table" >
234251 < tbody >
235- { data . map ( ( item , index ) => (
236- < tr key = { index } >
252+ { data . map ( ( item ) => (
253+ < tr key = { item . uniqueId } >
237254 < td className = "dot-td" > < State messages = { item . messages } baseClass = "dot" /> </ td >
238255 < td className = "no-wrap" > { item . text || 'N/A' } </ td >
239256 < PowerCell val = { item . power || 0 } />
240257 < td className = "no-wrap" style = { { textAlign : 'right' } } >
241258 { `${ fixed ( item . percent || 0 , 0 ) } %` }
242259 </ td >
243260 < td className = "fixed-col" >
244- { ( getErrors ( item . messages ) . length > 0 || getWarnings ( item . messages ) . length > 0 ) && (
261+ { ( getErrors ( item . messages ) . length > 0
262+ || getWarnings ( item . messages ) . length > 0 ) && (
245263 < PowerSummaryTableToolTip
246264 title = { message ( item . messages ) }
247265 statusColor = { statusColor ( item . messages ) }
@@ -253,7 +271,6 @@ function PowerSummaryTable({
253271 </ tbody >
254272 </ table >
255273 </ div >
256-
257274 < div className = "spacer" />
258275 < div className = "pst-bottom" >
259276 < div className = "pst-bottom-progress" >
@@ -273,7 +290,16 @@ function PowerSummaryTable({
273290
274291PowerSummaryTable . propTypes = {
275292 title : PropTypes . string . isRequired ,
276- data : PropTypes . array . isRequired ,
293+ data : PropTypes . arrayOf ( PropTypes . shape ( {
294+ uniqueId : PropTypes . string . isRequired ,
295+ text : PropTypes . string ,
296+ power : PropTypes . number ,
297+ percent : PropTypes . number ,
298+ messages : PropTypes . arrayOf ( PropTypes . shape ( {
299+ type : PropTypes . string ,
300+ text : PropTypes . string ,
301+ } ) ) ,
302+ } ) ) . isRequired ,
277303 total : PropTypes . number . isRequired ,
278304 percent : PropTypes . number . isRequired ,
279305 deviceId : PropTypes . string . isRequired ,
0 commit comments