-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add decimal_si_to_f #43
Conversation
@@ -0,0 +1,19 @@ | |||
module MoreCoreExtensions | |||
module DECIMAL_SI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why capital? Following IEC60027_2 example? I think that's because IEC is an abbreviation, so this would be DecimalSI
.
But module & filename should match, so module DecimalSuffix
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 on both points
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, name the file decimal_si.rb
module MoreCoreExtensions | ||
module DECIMAL_SI | ||
# made to handle https://github.com/kubernetes/kubernetes/blob/4def5add114b651c26fe576b7315f8029bfce46a/vendor/github.com/appc/spec/schema/types/resource/quantity.go#L49 | ||
DECIMAL_SUFFIXES = %w(k M G T P E).freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a hash {"m" => 1e-3, "k" => 1e3, "M" => 1e6, ...}
might make the logic simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for this...I prefer the Hash notation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, if you use the Hash, you can use a Hash default of 1
to handle when there is no suffix (the elsif suffix_index.nil?
condition below).
@@ -0,0 +1,12 @@ | |||
describe String do | |||
it '#iec60027_2' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it 'decimal_si_to_f'
@moolitayer what do you mean by "check the column type"? when you do a |
The numeric types of the columns these are stored into in Postrgres need not be |
@moolitayer yes, that's correct but it's out of scope here (generic lib). |
@moolitayer @simon3z IIUC that means i would have to convert it to the smallest possible unit which is "m" = mili. is that what we want here? because that would be weird for bytes |
we can also go with something like |
@@ -0,0 +1,19 @@ | |||
module MoreCoreExtensions | |||
module DECIMAL_SI | |||
# made to handle https://github.com/kubernetes/kubernetes/blob/4def5add114b651c26fe576b7315f8029bfce46a/vendor/github.com/appc/spec/schema/types/resource/quantity.go#L49 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need a kubernetes reference comment for something that is supposed to be generic. This comment might be appropriate in the body of the commit message, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, the reference is pointing to a vendored package within kubernetes, so it might be better to link the original package instead.
ActiveSupport doesn't have these reverse converters, instead choosing to implement them at the presentation layer via the NumberHelper view helper. We implemented more of these inManageIQ's NumberHelper as well. I'm not sure what is the "right" approach for these reverse converters. EDIT: It seems ActiveSupport patches to_s in order to expose the NumberHelper methods. https://api.rubyonrails.org/classes/ActiveSupport/NumericWithFormat.html This is really interesting and probably something we might want to extend for ManageIQ's additional number helpers |
I saw good comments about style and generalization (that should be addressed). |
@zeari yes for our use-case I would have preferred to go that way (use milli as unit). But that seems very specific to our use-case. Anyway either we use something very specific (in the kubernetes parser) or we go with something more generic as this. I understand the pros/cons. I don't think we should hold this PR on the above discussion (that maybe we can continue somewhere else). |
dcd674a
to
73e4a78
Compare
Comments addressed. This also handles decimal exponents. tests included |
ManageIQ/manageiq-providers-kubernetes#22 is also coordinated with this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
BTW method can be written as
but that might be more confusing... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this extension and iec60027_2
, it feels like it would be nicer to have one method that handled all of the cases. Maybe something like string_to_numeric
. What do you think @zeari ?
@@ -0,0 +1,15 @@ | |||
module MoreCoreExtensions | |||
module DecimalSI | |||
DECIMAL_SUFFIXES = {"m" => 1e-3, "k" => 1e3, "M" => 1e6, "G" => 1e9, "T" => 1e12, "P" => 1e15, "E" => 1e18}.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add all of the suffixes?
https://en.wikipedia.org/wiki/Metric_prefix
expect("1T".decimal_si_to_f).to eq(1_000_000_000_000.0) | ||
expect("1P".decimal_si_to_f).to eq(1_000_000_000_000_000.0) | ||
expect("1E".decimal_si_to_f).to eq(1_000_000_000_000_000_000.0) | ||
expect("1e9".decimal_si_to_f).to eq(1_000_000_000.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this test added to verify that it's not broken?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. Since I depend on this method to do both.
expect("1P".decimal_si_to_f).to eq(1_000_000_000_000_000.0) | ||
expect("1E".decimal_si_to_f).to eq(1_000_000_000_000_000_000.0) | ||
expect("1e9".decimal_si_to_f).to eq(1_000_000_000.0) | ||
expect("1e-9".decimal_si_to_f).to eq(0.000_000_001) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
DECIMAL_SUFFIXES = {"m" => 1e-3, "k" => 1e3, "M" => 1e6, "G" => 1e9, "T" => 1e12, "P" => 1e15, "E" => 1e18}.freeze | ||
def decimal_si_to_f | ||
suffix = self[-1] | ||
if DECIMAL_SUFFIXES[suffix].nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It reads nicer in the positive case.
multiplier = DECIMAL_SUFFIXES[suffix]
if multiplier
Float(self[0..-2]) * multiplier
else
Float(self)
end
Sure, lets get this in and do that in separate PR though. |
Checked commit zeari@fd28b8b with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
@bdunne all comments addressed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM @bdunne Please merge if you are good.
@bdunne When can this be available on master? |
@zeari We are just getting in a few more extractions from ManageIQ, and then we plan to cut a release. I'm hoping in the next couple of days. |
@zeari Version 3.3.0 has been released with these changes |
Needed in order to handle https://github.com/kubernetes/kubernetes/blob/4def5add114b651c26fe576b7315f8029bfce46a/vendor/github.com/appc/spec/schema/types/resource/quantity.go#L49
@cben @moolitayer @zakiva Please review
cc @simon3z