Skip to content

Commit 1943696

Browse files
committed
fix: metric formatting to proper unit
1 parent 131e7b3 commit 1943696

File tree

1 file changed

+50
-19
lines changed
  • assets/src/dashboard/parts/connected/dashboard

1 file changed

+50
-19
lines changed

assets/src/dashboard/parts/connected/dashboard/index.js

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,28 +137,55 @@ const Dashboard = () => {
137137

138138
const visitorsLimitPercent = ( ( userData.visitors / userData.visitors_limit ) * 100 ).toFixed( 0 );
139139

140-
const getFormattedMetric = ( metric ) => {
141-
let metricValue = userData[ metric ];
140+
const formatMetric = ( type, value ) => {
141+
let formattedValue = 0;
142+
let unit = '';
142143

143144
// Fallback for missing data
144-
if ( undefined === metricValue ) {
145-
metricValue = 'saved_size' === metric ?
146-
Math.floor( Math.random() * 2500 ) + 500 :
147-
Math.floor( Math.random() * 40 ) + 10;
145+
if ( undefined === value ) {
146+
value = 'saved_size' === type ?
147+
Math.floor( Math.random() * 2500000 ) + 500000 : // Mock KB
148+
'traffic' === type ?
149+
Math.floor( Math.random() * 2500 ) + 500 : // Mock MB
150+
Math.floor( Math.random() * 40 ) + 10; // Mock Percentage
148151
}
149152

150-
return metricValue;
151-
};
153+
switch ( type ) {
154+
case 'compression_percentage':
155+
formattedValue = parseFloat( value ).toFixed( 2 );
156+
unit = '%';
157+
break;
158+
case 'saved_size': // Assuming value is in KB
159+
const sizeInMB = value / 1000;
160+
if ( 1000 > sizeInMB ) {
161+
formattedValue = sizeInMB.toFixed( 2 );
162+
unit = 'MB';
163+
} else if ( 1000000 > sizeInMB ) {
164+
formattedValue = ( sizeInMB / 1000 ).toFixed( 2 );
165+
unit = 'GB';
166+
} else {
167+
formattedValue = ( sizeInMB / 1000000 ).toFixed( 2 );
168+
unit = 'TB';
169+
}
170+
break;
171+
case 'traffic': // Assuming value is in MB
172+
if ( 1000 > value ) {
173+
formattedValue = parseFloat( value ).toFixed( 2 );
174+
unit = 'MB';
175+
} else if ( 1000000 > value ) {
176+
formattedValue = ( value / 1000 ).toFixed( 2 );
177+
unit = 'GB';
178+
} else {
179+
formattedValue = ( value / 1000000 ).toFixed( 2 );
180+
unit = 'TB';
181+
}
182+
break;
183+
default:
184+
formattedValue = parseFloat( value ).toFixed( 2 );
185+
unit = '';
186+
}
152187

153-
const formatMetricValue = metric => {
154-
const value = getFormattedMetric( metric );
155-
const calcValue = 'saved_size' === metric ? ( value / 1000 ).toFixed( 2 ) : value.toFixed( 2 );
156-
return (
157-
<div className='flex items-end gap-1'>
158-
<span className='text-2xl text-black font-bold'>{calcValue}</span>
159-
<span className='text-sm text-gray-500'>{ 'compression_percentage' === metric ? '%' : 'MB' }</span>
160-
</div>
161-
);
188+
return { formattedValue, unit };
162189
};
163190

164191
return (
@@ -209,6 +236,9 @@ const Dashboard = () => {
209236

210237
<div className="flex pt-5 gap-5 flex-col md:flex-row">
211238
{ metrics.map( metric => {
239+
const rawValue = userData[ metric.value ];
240+
const { formattedValue, unit } = formatMetric( metric.value, rawValue );
241+
212242
return (
213243
<div
214244
key={ metric.value }
@@ -221,8 +251,9 @@ const Dashboard = () => {
221251
{ metric.label }
222252
</div>
223253

224-
<div>
225-
{ formatMetricValue( metric.value ) }
254+
<div className='flex items-end gap-1'>
255+
<span className='text-2xl text-black font-bold'>{formattedValue}</span>
256+
<span className='text-sm text-gray-500'>{unit}</span>
226257
</div>
227258

228259
<div className="font-normal text-gray-600">

0 commit comments

Comments
 (0)