Skip to content
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

Merged
merged 1 commit into from
Jul 3, 2017
Merged

add decimal_si_to_f #43

merged 1 commit into from
Jul 3, 2017

Conversation

@zeari zeari changed the title Decimal si add decimal_si_to_f Jun 5, 2017
@@ -0,0 +1,19 @@
module MoreCoreExtensions
module DECIMAL_SI
Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on both points

Copy link
Member

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
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member

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
Copy link
Contributor

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'

@simon3z
Copy link

simon3z commented Jun 5, 2017

@cben @zeari remember that Quantity is a fixed point variable, it would be nice to try and keep it fixed (vs current floating).

@moolitayer
Copy link

@cben @zeari remember that Quantity is a fixed point variable, it would be nice to try and keep it fixed (vs current floating).

In that case we should also check the column type this is eventually stored into

@simon3z
Copy link

simon3z commented Jun 6, 2017

In that case we should also check the column type this is eventually stored into

@moolitayer what do you mean by "check the column type"? when you do a to_f or a to_i how do you check the column type that you'll use for those?

@moolitayer
Copy link

@moolitayer what do you mean by "check the column type"? when you do a to_f or a to_i how do you check the column type that you'll use for those?

The numeric types of the columns these are stored into in Postrgres need not be real or double precision AFAIK

@simon3z
Copy link

simon3z commented Jun 7, 2017

The numeric types of the columns these are stored into in Postrgres need not be real or double precision AFAIK

@moolitayer yes, that's correct but it's out of scope here (generic lib).

@zeari
Copy link
Author

zeari commented Jun 8, 2017

@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

@zeari
Copy link
Author

zeari commented Jun 8, 2017

we can also go with something like string.decimal_si_to_f(desired_unit) and desired unit would be 1 of "m", "k", "M".... "none" and itll convert it to that unit

@@ -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
Copy link
Member

@Fryguy Fryguy Jun 13, 2017

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.

Copy link
Member

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.

@Fryguy
Copy link
Member

Fryguy commented Jun 13, 2017

we can also go with something like string.decimal_si_to_f(desired_unit) and desired unit would be 1 of "m", "k", "M".... "none" and itll convert it to that unit

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

@simon3z
Copy link

simon3z commented Jun 17, 2017

I saw good comments about style and generalization (that should be addressed).
Anyway ATM overall 👍 for the case we need to cover.
So feel free to merge when ready.

@simon3z
Copy link

simon3z commented Jun 17, 2017

@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

@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).

@zeari zeari force-pushed the decimal_si branch 4 times, most recently from dcd674a to 73e4a78 Compare June 18, 2017 10:51
@zeari
Copy link
Author

zeari commented Jun 18, 2017

Comments addressed. This also handles decimal exponents.

tests included

@zeari
Copy link
Author

zeari commented Jun 19, 2017

ManageIQ/manageiq-providers-kubernetes#22 is also coordinated with this

Copy link

@moolitayer moolitayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@moolitayer
Copy link

BTW method can be written as

float(to_i) *
{
  "m" => 1e-3, "k" => 1e3, "M" => 1e6, "G" => 1e9, "T" => 1e12, "P" => 1e15, "E" => 1e18, nil => 1
}[
  self[-1]
]

but that might be more confusing...

Copy link
Member

@bdunne bdunne left a 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
Copy link
Member

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)
Copy link
Member

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?

Copy link
Author

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)
Copy link
Member

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?
Copy link
Member

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

@zeari
Copy link
Author

zeari commented Jun 29, 2017

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 ?

Sure, lets get this in and do that in separate PR though.

@miq-bot
Copy link
Member

miq-bot commented Jun 29, 2017

Checked commit zeari@fd28b8b with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
3 files checked, 0 offenses detected
Everything looks fine. ⭐

@zeari
Copy link
Author

zeari commented Jun 29, 2017

@bdunne all comments addressed

@zeari
Copy link
Author

zeari commented Jul 2, 2017

ping @bdunne @Fryguy

Copy link
Member

@Fryguy Fryguy left a 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 bdunne merged commit b4bd8d3 into ManageIQ:master Jul 3, 2017
@zeari
Copy link
Author

zeari commented Jul 17, 2017

@bdunne When can this be available on master?

@Fryguy
Copy link
Member

Fryguy commented Jul 18, 2017

@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.

@bdunne
Copy link
Member

bdunne commented Jul 24, 2017

@zeari Version 3.3.0 has been released with these changes

@zeari
Copy link
Author

zeari commented Jul 24, 2017

Thanks @bdunne @Fryguy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants