Result
This commit is contained in:
53
src/index.ts
53
src/index.ts
@@ -1,5 +1,52 @@
|
||||
export class SampleClass {
|
||||
add(a: number, b: number): number {
|
||||
return a+b;
|
||||
export interface VM {
|
||||
cpu: number,
|
||||
memory: number,
|
||||
network: number,
|
||||
}
|
||||
|
||||
export interface ServerCapacity {
|
||||
limitCpu: number,
|
||||
limitMemory: number,
|
||||
limitNetwork: number,
|
||||
}
|
||||
|
||||
export type Server = Set<VM>;
|
||||
|
||||
|
||||
export class ServerConsolidation {
|
||||
doesVmFitOnServer(vm: VM, capacity: ServerCapacity): boolean {
|
||||
return vm.cpu <= capacity.limitCpu &&
|
||||
vm.memory <= capacity.limitMemory &&
|
||||
vm.network <= capacity.limitNetwork;
|
||||
}
|
||||
|
||||
reduceCapacity(vm: VM, capacity: ServerCapacity): void {
|
||||
capacity.limitCpu -= vm.cpu;
|
||||
capacity.limitMemory -= vm.memory;
|
||||
capacity.limitNetwork -= vm.network;
|
||||
}
|
||||
|
||||
fit(vms: VM[], capacity: ServerCapacity): Server[] {
|
||||
const servers: Server[] = [];
|
||||
const capacitiesLeft: ServerCapacity[] = [];
|
||||
for (const vm of vms) {
|
||||
let found = false;
|
||||
for (const [idx, server] of servers.entries()) {
|
||||
const capacity = capacitiesLeft[idx];
|
||||
if (this.doesVmFitOnServer(vm, capacity)) {
|
||||
found = true;
|
||||
server.add(vm);
|
||||
this.reduceCapacity(vm, capacity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
servers.push(new Set([vm]));
|
||||
const newCapacity = {...capacity};
|
||||
this.reduceCapacity(vm, newCapacity);
|
||||
capacitiesLeft.push(newCapacity);
|
||||
}
|
||||
}
|
||||
return servers;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user