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

wheight by created_at #30

Open
pywebdesign opened this issue Oct 20, 2014 · 6 comments
Open

wheight by created_at #30

pywebdesign opened this issue Oct 20, 2014 · 6 comments

Comments

@pywebdesign
Copy link

Hi,
since created_at is not a directly considered numeric but still very numeric in nature, it would be great and probably not so hard to implement wheight random that work on date time.

could someone help me on that?

thank you very much

@pywebdesign
Copy link
Author

Here is some idea:
select created_at, extract(epoch from created_at) from somethings limit 3;
created_at | date_part
----------------------------+------------------
2014-09-08 20:56:13.320441 | 1410209773.32044
2014-09-23 02:20:22.571723 | 1411438822.57172
2014-09-26 14:28:09.277595 | 1411741689.2776
(3 rows)

to be put somehow here

def random_weighted_order_clause(ranking_column, opts={})
    connection = opts[:connection]
    if connection.adapter_name =~ /sqlite/i
        # computer multiplication is faster than division I was once taught...so translate here
        max_int = 9223372036854775807.0
        multiplier = 1.0 / max_int
        "(#{ranking_column} * ABS(#{random_for(opts)} * #{multiplier}) ) DESC"
    else
        "(#{ranking_column} * #{random_for(opts)}) DESC"
    end
end

@spilliton
Copy link
Owner

I haven't had much time to devote to randumb as of late. If you implement this feel free to make a pull request and I can consider merging it in :)

I'm curious what your use case is for needing this. You may be able to achieve what you need by using a where clause to omit old records:

User.where(['created_at > ?', 1.year.ago]).order_by_rand.limit(3).to_a

@pywebdesign
Copy link
Author

thank, I am a little bit idiot I think! ;-)

I don't feel I can implement it, it seams I would need to check if it is numeric or not and if not do something but I don't understand so much how randumb work and where I should do that right now!

It is slightly different though,

User.where(['created_at > ?', 1.year.ago]).order_by_rand.limit(3).to_a 
!= , (just slightly similar)
User.order_by_rand_wheight(created_at) 

since the second should theorically probably return something older than 1 year ago no?

thank you

@spilliton
Copy link
Owner

Yes you are correct, I'm just trying to understand your use case (ie why you need things weighted by created date). For me when I have needed to do things like that I have just opted to not show things older than a certain date and randomize the things after that date. This performs well because you can cut out a large number of records to perform the rand() on.

If you further complicate the function is has to perform on every record, it will be even slower on large result sets.

@pywebdesign
Copy link
Author

yes this is true, I may as well do like you, but it is still only better than nothing! I have no real use case right now since I will be creating a numeric score value shortly, better just do as you said.

but even:

User.where(['created_at > ?', 1.year.ago]).order_by_rand_wheight(created_at) 
!= (but even more similar)
User.order_by_rand_wheight(created_at) 

If we think about distribution, there will be a flat distribution in time from the second and a poisson distribution or something similar from the first which again seams to me way better.

what do you think?

@spilliton
Copy link
Owner

Feel free to give it a go if you want to build it and make a pull request, but as mentioned earlier, I unfortunately do not have time at the moment to implement any real features like this myself.

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

No branches or pull requests

2 participants