Fix cpuallocated value in findHostsForMIgration api#4220
Conversation
The findHostsForMigration api displays 0% always for cpuallocated field which is wrong
GabrielBrascher
left a comment
There was a problem hiding this comment.
I hope to be helpful with the raised code enhancement suggestion.
Thanks for the PR @ravening.
| Float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId())); | ||
| String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)).toString() + "%"; | ||
| float cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId())); | ||
| String cpuAlloc = decimalFormat.format(((float)cpu / cpuWithOverprovisioning * 100f)) + "%"; |
There was a problem hiding this comment.
I see 100f a couple of times on the code, there is also 100.0f.
What do you think of extracting these magic 100f/100.0f into a constant?
Another approach that would be even nicer would be to have these pieces extracted to a method like calculateResourceAllocatedPercentage, and add a few unit test case methods.
public String calculateResourceAllocatedPercentage(float resource, float resourceWithOverprovisioning) {
return decimalFormat.format(((float)resource / resourceWithOverprovisioning * 100f)) + **"%"**
}
There was a problem hiding this comment.
@GabrielBrascher I made the changes. Also can you look into the issue #4221 ?
There was a problem hiding this comment.
@GabrielBrascher I didn't make changes in line 314 because it has different behavior compared to line 165.
I have mentioned the same in issue #4221 also. list hosts api returns absolute value where as findhostsformigration returns percentage. If we decided what to return then I can make changes in these two places also
There was a problem hiding this comment.
@ravening sorry for the delay. I think that I was not clear about my point.
-
the method that you extract L464:
decimalFormat.format(((float)resource / resourceWithOverProvision * 100.0f)) + "%" -
line 314 that I mentioned:
decimalFormat.format((float) mem / memWithOverprovisioning * 100.0f) +"%"
That is why I thought that it could also benefit from calculateResourceAllocatedPercentage.
|
@blueorangutan package |
|
@rhtyd a Jenkins job has been kicked to build packages. I'll keep you posted as I make progress. |
|
Packaging result: ✔centos7 ✔debian. JID-1623 |
|
@blueorangutan test |
|
@DaanHoogland a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests |
|
(tests failed, re-run after the weekend due to weekend maintenance) |
|
@blueorangutan test |
|
@rhtyd a Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests |
@rhtyd @DaanHoogland its not yet ready for merge. Im waiting for @GabrielBrascher reply for my question. |
|
@ravening if your question is whether a percentage or an absolute number is to be returned i would really not care. I don't think that the two API have to return the same type of value. I do think that the difference should be clear from the name of the parameter and hence both could be returned if so desired. |
|
@DaanHoogland @ravening I am good with the code as it is, I just saw that maybe the method could be also used in the other context considering how similar those lines are. Note that even |
|
Trillian test result (tid-2253)
|
|
@ravening sorry I missed your comment, and merged based on @GabrielBrascher 's |
Since it's ok for them that two functions returns different value, it's fine with me as well |
|
Alright I'll not revert it, thnx for replying. |
Description
Fixes #4221
The findHostsForMigration api displays 0% always for
cpuallocated field which is wrong
Types of changes
Screenshots (if appropriate):
How Has This Been Tested?
Tested from cloudmonkey api
Before the fix
After the fix