-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pci.savings-plan): view percentage
ref: TAPC-1779 Signed-off-by: Eric Ciccotti <[email protected]>
- Loading branch information
Eric Ciccotti
committed
Jan 31, 2025
1 parent
ebbcb15
commit 1edad29
Showing
7 changed files
with
124 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/manager/apps/pci-savings-plan/src/utils/kpi/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { calculateAverageUsage } from './utils'; | ||
import { SavingsPlanConsumption } from '@/types/savingsPlanConsumption.type'; | ||
|
||
describe('calculateAverageUsage', () => { | ||
const testCases = [ | ||
{ | ||
description: 'should return null when no periods exist in any flavor', | ||
input: { flavors: [{ periods: [] }, { periods: [] }] }, | ||
expected: null, | ||
}, | ||
{ | ||
description: | ||
'should correctly calculate the average utilization across multiple periods', | ||
input: { | ||
flavors: [ | ||
{ periods: [{ utilization: '40%' }, { utilization: '60%' }] }, | ||
{ periods: [{ utilization: '80%' }] }, | ||
], | ||
}, | ||
expected: 60.0, | ||
}, | ||
{ | ||
description: 'should handle a single period correctly', | ||
input: { flavors: [{ periods: [{ utilization: '75%' }] }] }, | ||
expected: 75.0, | ||
}, | ||
{ | ||
description: 'should ignore invalid values and calculate correctly', | ||
input: { | ||
flavors: [ | ||
{ periods: [{ utilization: '50%' }, { utilization: 'N/A' }] }, | ||
], | ||
}, | ||
expected: 50.0, | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ description, input, expected }) => { | ||
it(description, () => { | ||
expect(calculateAverageUsage(input as SavingsPlanConsumption)).toBe( | ||
expected, | ||
); | ||
}); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
packages/manager/apps/pci-savings-plan/src/utils/kpi/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { SavingsPlanConsumption } from '@/types/savingsPlanConsumption.type'; | ||
|
||
export const calculateAverageUsage = ( | ||
consumption: SavingsPlanConsumption | null, | ||
): number | null => { | ||
if (!consumption.flavors.some(({ periods }) => periods.length > 0)) { | ||
return null; | ||
} | ||
|
||
const utilizationValues = consumption.flavors.flatMap(({ periods }) => | ||
periods | ||
.map(({ utilization }) => parseFloat(utilization.replace('%', ''))) | ||
.filter((value) => !Number.isNaN(value)), | ||
); | ||
|
||
if (!utilizationValues.length) { | ||
return null; | ||
} | ||
|
||
const averageUtilization = | ||
utilizationValues.reduce((sum, value) => sum + value, 0) / | ||
utilizationValues.length; | ||
|
||
return averageUtilization; | ||
}; |