Skip to content

Commit

Permalink
Better counting of migrated vms
Browse files Browse the repository at this point in the history
Signed-off-by: yaacov <yzamir@redhat.com>
  • Loading branch information
yaacov committed Jul 18, 2023
1 parent 288379b commit 6b466c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function getMigrationCounts(migrations: V1beta1Migration[]): { [key: stri
Total: 0,
Running: 0,
Failed: 0,
Canceled: 0,
Succeeded: 0,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { V1beta1Migration } from '@kubev2v/types';

// Helper function to process 'True' vm conditions
function processVmConditions(vm) {
if (!('conditions' in vm)) return [];

return vm.conditions.reduce((acc: string[], condition) => {
if (condition.status === 'True') acc.push(condition.type);
return acc;
}, []);
}

// Helper function to increment vmCounts based on conditions
function incrementCounts(conditions: string[], vm, vmCounts: { [key: string]: number }) {
vmCounts['Total']++;

const isRunning =
vm?.phase !== 'Completed' && !conditions.includes('Failed') && !conditions.includes('Canceled');
const isCompleted = vm?.phase === 'Completed';

if (isRunning) {
vmCounts['Running']++;
}

if (isCompleted) {
conditions.forEach((condition) => {
if (condition in vmCounts) vmCounts[condition]++;
});
}
}

/**
* This function gets the number of 'Running', 'Failed', and 'Succeeded' VMs in the migrations.
* @param {V1beta1Migration[]} migrations - The array of migration objects to inspect.
Expand All @@ -10,22 +39,15 @@ export function getVmCounts(migrations: V1beta1Migration[]): { [key: string]: nu
Total: 0,
Running: 0,
Failed: 0,
Canceled: 0,
Succeeded: 0,
};

for (const migration of migrations || []) {
if ('vms' in migration.status) {
for (const vm of migration.status.vms) {
vmCounts['Total']++;
if ('conditions' in vm) {
for (const condition of vm.conditions) {
if (condition.status == 'True') {
if (condition.type in vmCounts) {
vmCounts[condition.type]++;
}
}
}
}
const conditions = processVmConditions(vm);
incrementCounts(conditions, vm, vmCounts);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const MigrationsCard: React.FC<MigrationsCardProps> = () => {
/>
</Flex>

<Text component={TextVariants.small}>{t('{{Canceled}} canceled', count)}</Text>
<Divider />
</CardBody>

Expand All @@ -95,6 +96,7 @@ export const MigrationsCard: React.FC<MigrationsCardProps> = () => {
/>
</Flex>

<Text component={TextVariants.small}>{t('{{Canceled}} canceled', vmCount)}</Text>
<Divider />
</CardBody>
</Card>
Expand Down

0 comments on commit 6b466c2

Please sign in to comment.