Skip to content

Commit

Permalink
fix farm free ips & node free resources calculations
Browse files Browse the repository at this point in the history
Signed-off-by: mariobassem <[email protected]>
  • Loading branch information
MarioBassem committed Sep 13, 2023
1 parent 5ca1f5a commit 4ca6b15
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/mappings/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ export async function nodeContractCanceled(
await ctx.store.save<Contract>(savedContract)

let savedPublicIPs: PublicIp[] = await ctx.store.find(PublicIp, { where: { contractId: contractID }, relations: { farm: true } })
savedPublicIPs = await Promise.all(savedPublicIPs.map(async (ip) => {
ip.contractId = BigInt(0)
Promise.all(savedPublicIPs.map(async (ip) => {
ip.contractId = null
const farm = await ctx.store.get(Farm, { where: { farmID: ip.farm.farmID } })
if (farm) {
farm.freeIps += 1
await ctx.store.save<Farm>(farm)
}

return ip
await ctx.store.save<PublicIp>(ip)
}))

if (savedContract.nodeID) {
Expand Down
7 changes: 4 additions & 3 deletions src/mappings/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ export async function farmStored(
return ctx.store.save<PublicIp>(newIP)
})

newFarm.freeIps = newFarm.totalIps

await Promise.all(ipPromises)

newFarm.freeIps = newFarm.totalIps
await ctx.store.save<Farm>(newFarm)
}

Expand Down Expand Up @@ -127,7 +128,7 @@ export async function farmUpdated(
savedFarm.freeIps = 0
let eventPublicIPs = farmUpdatedEventParsed.publicIps

await farmUpdatedEventParsed.publicIps.forEach(async ip => {
await Promise.all(farmUpdatedEventParsed.publicIps.map(async ip => {
if (ip.ip.toString().indexOf('\x00') >= 0) {
return
}
Expand Down Expand Up @@ -157,7 +158,7 @@ export async function farmUpdated(
savedFarm.publicIPs.push(newIP)
savedFarm.freeIps += 1
}
})
}))

const publicIPsOfFarm = await ctx.store.find<PublicIp>(PublicIp, { where: { farm: { id: savedFarm.id } }, relations: { farm: true } })
publicIPsOfFarm.forEach(async ip => {
Expand Down
9 changes: 5 additions & 4 deletions src/mappings/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,16 @@ export async function nodeUpdated(
// calculate free resources difference
let prevMRUPercentage = savedNode.totalMRU / BigInt(10)
let prevZOSUsedMRU = savedNode.totalMRU - (prevMRUPercentage > BigInt(ZOSMinUsedMemory) ? prevMRUPercentage : BigInt(ZOSMinUsedMemory))

let curMRUPercentage = nodeEvent.resources.mru / BigInt(10)
let curZOSUsedMRU = nodeEvent.resources.mru - (curMRUPercentage > BigInt(ZOSMinUsedMemory) ? curMRUPercentage : BigInt(ZOSMinUsedMemory))

let mruDiff = curZOSUsedMRU - prevZOSUsedMRU
let contractsUsedMRU = savedNode.totalMRU - savedNode.freeMRU - prevZOSUsedMRU
let newFreeMRU = nodeEvent.resources.mru - contractsUsedMRU - curZOSUsedMRU

let hruDiff = nodeEvent.resources.hru - savedNode.totalHRU
let sruDiff = (nodeEvent.resources.sru - BigInt(ZOSUsedSRU)) - savedNode.totalSRU
let sruDiff = nodeEvent.resources.sru - savedNode.totalSRU

savedNode.freeMRU += mruDiff
savedNode.freeMRU = newFreeMRU
savedNode.freeHRU += hruDiff
savedNode.freeSRU += sruDiff

Expand Down

0 comments on commit 4ca6b15

Please sign in to comment.