I would like to sort my links using an algorithm, all the information needed can be derived from values in my Links table. How can I return items sorted by the item's calculated Score?
Schema https://gist.github.com/1326044
Index Action https://gist.github.com/1326045
Time Calculation https://gist.github.com/1326050
Algorithm
# Score = (P-1) / (T+2)^G
# P = points of an item (and -1 is to negate submitters vote)
# T = time since submission (in hours)
# G = Gravity, defaults to 1.8
I'd do as follows:
First, in your model:
after_initialize :calculate_score
attr_accessor :score
def calculate_score
unless self.new_record?
time_elapsed = (Time.zone.now - self.created_at) / 3600
self.score = (self.points-1) / (time_elapsed+2)^G # I don't know how you retrieve G
end
end
In your controller:
#links = Link.all.sort_by(&:score)
Related
I work with rails 5 / audited 4.6.5
I have batch actions on more than 2000 items on once.
To make it usable, I need to use the updatre_all function.
Then, I would like to create the needed audited in one time
What I would like to do is something like that :
Followup.where(id: ids).in_batches(of: 500) do |group|
#quick update for user responsiveness
group.update_all(step_id: 1)
group.delay.newAudits(step_id: 1)
end
But the audited gem looks to be to basic for that.
I'm sure a lot of poeple faced issue like that before
after a lot of iteration, I manage to create an optimized query.
I put it in an AditBatch class and also add delay
class AuditBatch
############################################
## Init a batch update from a list of ids
## the aim is to avoid instantiations in controllers
## #param string class name (eq: 'Followup')
## #param int[] ids of object to update
## #param changet hash of changes {key1: newval, key2: newval}
############################################
def self.init_batch_creation(auditable_type, auditable_ids, changes)
obj = Object.const_get(auditable_type)
group = obj.where(id: auditable_ids)
AuditBatch.delay.batch_creation(group, changes, false)
end
############################################
## insert a long list of audits in one time
## #param group array array of auditable objects
## #param changet hash of changes {key1: newval, key2: newval}
############################################
def self.batch_creation(group, changes, delayed = true)
sql = 'INSERT INTO audits ("action", "audited_changes", "auditable_id", "auditable_type", "created_at", "version", "request_uuid")
VALUES '
total = group.size
group.each_with_index do |g, index|
parameters = 'json_build_object('
length = changes.size
i=1
changes.each do |key, val|
parameters += "'#{key}',"
parameters += "json_build_array("
parameters += "(SELECT ((audited_changes -> '#{key}')::json->>1) FROM audits WHERE auditable_id = #{g.id} order by id desc limit 1),"
parameters += val.is_a?(String) ? "'#{val.to_s}'" : val.to_s
parameters += ')'
parameters += ',' if i < length
i +=1
end
parameters += ')'
sql += "('update', #{parameters}, #{g.id}, '#{g.class.name}', '#{Time.now}', (SELECT max(version) FROM audits where auditable_id= #{g.id})+1, '#{SecureRandom.uuid}')"
sql += ", " if (index+1) < total
end
if delayed==true
AuditBatch.delay.execute_delayed_sql(sql)
else
ActiveRecord::Base.connection.execute(sql)
end
end
def self.execute_delayed_sql(sql)
ActiveRecord::Base.connection.execute(sql)
end
end
With group.update_all your callbacks are skipped and it doesn't end up recording changes in new audit.
You cannot manually create audits for those records, and even if you can create that audit changes manually, you will need the reference of "what changed?" (goes in audited_changes). But those changes are already lost when you did update_all on group earlier.
(`action`, `audited_changes`, `auditable_id`, `auditable_type`, `created_at`, `version`, `request_uuid`)
It is also documented in this audited issue - https://github.com/collectiveidea/audited/issues/352
paper_trail, another such gem which retains change_logs, also has this issue: https://github.com/airblade/paper_trail/issues/337
So I have a User model and a Post model
Post belongs to a User
Post has a field in the database called score
In the Post model I have a method called score which gives the post a score based on the fields (needs to be done this way):
def score
score = 0
if self.title.present?
score += 5
end
if self.author.present?
score += 5
end
if self.body.present?
score += 5
end
score
end
The Question:
There are loads of Users and loads of Posts. So What I'm trying to do is after the score is worked out, I want to save it to the Post score field in the database for each Post. The score should be updated if the user updates the Post.
I have looked at using after_update :score! but don't understand how to apply the logic
It looks a little like you are trying to re-invent the wheel ActiveRecord provides you.
If you have a database field score, then ActiveRecord will automatically provide an attribute_reader and attribute_writer for score and you should not override these unless you have a really really good reason for it, e.g. you need to add some other resources or some serious business logic into it.
There is a way easier way to solve it, by using the before_save hook, which will kick in before any #create or #update:
class Post
attribute_accessible :score # if you have Rails 4.x you can omit this line
before_save :update_score
private
def update_score
new_score = 0
self.score = [:title, :author, :body].each do |field|
new_score += 5 if send(field).present?
end
self.score = new_score
end
This way, ActiveRecord will handle the saving for you and your score will always up to date. Additionally Post#score will always return the real value currently saved in the database
You can do it like this
after_update :score!
def score!
score = 0
if self.title.present?
score += 5
end
if self.author.present?
score += 5
end
if self.body.present?
score += 5
end
self.update_column(:score, score)
end
This is to be done in your Post model.
You can do it using update_column method. Like:
def score
score = 0
if self.title.present?
score += 5
end
if self.author.present?
score += 5
end
if self.body.present?
score += 5
end
self.update_column(:score, score)
end
You need to override the setter method in the Post model
attr_accessible :score
def score=(value)
score = 0
if self.title.present?
score += 5
end
if self.author.present?
score += 5
end
if self.body.present?
score += 5
end
write_attribute(:score, score)
end
Pretty new to RoR. Wonder if anyone can help me with this issue.
I got a gem called "business_time" which calculates the business days between two dates. I have set up a method in the model which does all the calculations.
I have a field called "credit" which should hold the number of business days. Here's what I have:
MODEL
def self.calculate(from_date,to_date)
days = 0
date_1 = Date.parse(from_date)
date 2 = Date.parse(to_date)
days = date_1.business_days_until(date2)
days
end
CONTROLLER
def new
#vacation = current_user.vacations.build
#vacations = Vacation.calculate(:from_date, :to_date)
end
I got an error referencing something about a string.
Furthermore, how do I go about storing the data from the method into the field called "credit"?
Thanks guys.
I think there is no need for an extra method, since all attributes (from_date, end_date and credit) are stored in the same model.
I would just set from_date and end_date in the initializer and calculate credit with a callback before validation:
# in the model
before_validation :calculate_credit
private
def calculate_credit
if from_date && to_date
# `+ 1` because the user takes off both days (`from_date` and `to_date`),
# but `business_days_until` doesn't count the `from_day`.
self.credit = from_date.business_days_until(to_date) + 1
end
end
# in the controller
def new
#vacation = current_user.vacations.build
end
def create
#vacation = current_user.vacations.build(vacation_params)
if #vacation.save
# #vacation.credit would return the calculated credit at this point
else
# ...
end
end
private
def vacation_params
params.require(:vacation).permit(:from_date, :to_date)
end
What you need here is pass String objects instead of Symbol objects.
So instead of #vacations = Vacation.calculate(:from_date, :to_date), you probably need to pass params[:from_date] and params[:to_date] which should be strings like 20/01/2016, etc...
Your code should be
#vacations = Vacation.calculate(params[:from_date], params[:to_date])
I have this "heavy_rotation" filter I'm working on. Basically it grabs tracks from our database based on certain parameters (a mixture of listens_count, staff_pick, purchase_count, to name a few)
An xhr request is made to the filter_tracks controller action. In there I have a flag to check if it's "heavy_rotation". I will likely move this to the model (cos this controller is getting fat)... Anyway, how can I ensure (in a efficient way) to not have it pull the same records? I've considered an offset, but than I have to keep track of the offset for every query. Or maybe store track.id's to compare against for each query? Any ideas? I'm having trouble thinking of an elegant way to do this.
Maybe it should be noted that a limit of 14 is set via Javascript, and when a user hits "view more" to paginate, it sends another request to filter_tracks.
Any help appreciated! Thanks!
def filter_tracks
params[:limit] ||= 50
params[:offset] ||= 0
params[:order] ||= 'heavy_rotation'
# heavy rotation filter flag
heavy_rotation ||= (params[:order] == 'heavy_rotation')
#result_offset = params[:offset]
#tracks = Track.ready.with_artist
params[:order] = "tracks.#{params[:order]}" unless heavy_rotation
if params[:order]
order = params[:order]
order.match(/artist.*/){|m|
params[:order] = params[:order].sub /tracks\./, ''
}
order.match(/title.*/){|m|
params[:order] = params[:order].sub /tracks.(title)(.*)/i, 'LOWER(\1)\2'
}
end
searched = params[:q] && params[:q][:search].present?
#tracks = parse_params(params[:q], #tracks)
#tracks = #tracks.offset(params[:offset])
#result_count = #tracks.count
#tracks = #tracks.order(params[:order], 'tracks.updated_at DESC').limit(params[:limit]) unless heavy_rotation
# structure heavy rotation results
if heavy_rotation
puts "*" * 300
week_ago = Time.now - 7.days
two_weeks_ago = Time.now - 14.days
three_months_ago = Time.now - 3.months
# mix in top licensed tracks within last 3 months
t = Track.top_licensed
tracks_top_licensed = t.where(
"tracks.updated_at >= :top",
top: three_months_ago).limit(5)
# mix top listened to tracks within last two weeks
tracks_top_listens = #tracks.order('tracks.listens_count DESC').where(
"tracks.updated_at >= :top",
top: two_weeks_ago)
.limit(3)
# mix top downloaded tracks within last two weeks
tracks_top_downloaded = #tracks.order("tracks.downloads_count DESC").where(
"tracks.updated_at >= :top",
top: two_weeks_ago)
.limit(2)
# mix in 25% of staff picks added within 3 months
tracks_staff_picks = Track.ready.staff_picks.
includes(:artist).order("tracks.created_at DESC").where(
"tracks.updated_at >= :top",
top: three_months_ago)
.limit(4)
#tracks = tracks_top_licensed + tracks_top_listens + tracks_top_downloaded + tracks_staff_picks
end
render partial: "shared/results"
end
I think seeking an "elegant" solution is going to yield many diverse opinions, so I'll offer one approach and my reasoning. In my design decision, I feel that in this case it's optimal and elegant to enforce uniqueness on query intersections by filtering the returned record objects instead of trying to restrict the query to only yield unique results. As for getting contiguous results for pagination, on the other hand, I would store offsets from each query and use it as the starting point for the next query using instance variables or sessions, depending on how the data needs to be persisted.
Here's a gist to my refactored version of your code with a solution implemented and comments explaining why I chose to use certain logic or data structures: https://gist.github.com/femmestem/2b539abe92e9813c02da
#filter_tracks holds a hash map #tracks_offset which the other methods can access and update; each of the query methods holds the responsibility of adding its own offset key to #tracks_offset.
#filter_tracks also holds a collection of track id's for tracks that already appear in the results.
If you need persistence, make #tracks_offset and #track_ids sessions/cookies instead of instance variables. The logic should be the same. If you use sessions to store the offsets and id's from results, remember to clear them when your user is done interacting with this feature.
See below. Note, I refactored your #filter_tracks method to separate the responsibilities into 9 different methods: #filter_tracks, #heavy_rotation, #order_by_params, #heavy_rotation?, #validate_and_return_top_results, and #tracks_top_licensed... #tracks_top_<whatever>. This will make my notes easier to follow and your code more maintainable.
def filter_tracks
# Does this need to be so high when JavaScript limits display to 14?
#limit ||= 50
#tracks_offset ||= {}
#tracks_offset[:default] ||= 0
#result_track_ids ||= []
#order ||= params[:order] || 'heavy_rotation'
tracks = Track.ready.with_artist
tracks = parse_params(params[:q], tracks)
#result_count = tracks.count
# Checks for heavy_rotation filter flag
if heavy_rotation? #order
#tracks = heavy_rotation
else
#tracks = order_by_params
end
render partial: "shared/results"
end
All #heavy_rotation does is call the various query methods. This makes it easy to add, modify, or delete any one of the query methods as criteria changes without affecting any other method.
def heavy_rotation
week_ago = Time.now - 7.days
two_weeks_ago = Time.now - 14.days
three_months_ago = Time.now - 3.months
tracks_top_licensed(date_range: three_months_ago, max_results: 5) +
tracks_top_listens(date_range: two_weeks_ago, max_results: 3) +
tracks_top_downloaded(date_range: two_weeks_ago, max_results: 2) +
tracks_staff_picks(date_range: three_months_ago, max_results: 4)
end
Here's what one of the query methods looks like. They're all basically the same, but with custom SQL/ORM queries. You'll notice that I'm not setting the :limit parameter to the number of results that I want the query method to return. This would create a problem if one of the records returned is duplicated by another query method, like if the same track was returned by staff_picks and top_downloaded. Then I would have to make an additional query to get another record. That's not a wrong decision, just one I didn't decide to do.
def tracks_top_licensed(args = {})
args = #default.merge args
max = args[:max_results]
date_range = args[:date_range]
# Adds own offset key to #filter_tracks hash map => #tracks_offset
#tracks_offset[:top_licensed] ||= 0
unfiltered_results = Track.top_licensed
.where("tracks.updated_at >= :date_range", date_range: date_range)
.limit(#limit)
.offset(#tracks_offset[:top_licensed])
top_tracks = validate_and_return_top_results(unfiltered_results, max)
# Add offset of your most recent query to the cumulative offset
# so triggering 'view more'/pagination returns contiguous results
#tracks_offset[:top_licensed] += top_tracks[:offset]
top_tracks[:top_results]
end
In each query method, I'm cleaning the record objects through a custom method #validate_and_return_top_results. My validator checks through the record objects for duplicates against the #track_ids collection in its ancestor method #filter_tracks. It then returns the number of records specified by its caller.
def validate_and_return_top_results(collection, max = 1)
top_results = []
i = 0 # offset incrementer
until top_results.count >= max do
# Checks if track has already appeared in the results
unless #result_track_ids.include? collection[i].id
# this will be returned to the caller
top_results << collection[i]
# this is the point of reference to validate your query method results
#result_track_ids << collection[i].id
end
i += 1
end
{ top_results: top_results, offset: i }
end
Why do I get empty when I execute this? Asumming User's point is 2500. It should return 83
posts_controller
#user = User.find(params[:id])
#user.percentage = count_percentage(#user)
#user.save
application_controller
def count_percentage(user)
if user
if user.point > 2999
percentage = ((user.point / 5000.0) * 100 ).to_i
elsif user.point > 1999
percentage = ((user.point / 3000.0) * 100 ).to_i
end
return percentage
end
end
It looks like there is some lack of basic understanding, so I try to focus on a few points here.
count_percentage belongs to your model. It is a method which does things that are tied to your User records. In your example, the code can only be used with a User record. Therefore it belongs to your User class!
class User < ActiveRecord::Base
def percentage
if self.point > 2999
return ((self.point / 5000.0) * 100 ).to_i
elsif user.point > 1999
return ((self.point / 3000.0) * 100 ).to_i
else
# personally, I'd add a case for points range 0...3000
end
end
As you said, you want to put that value into your "side menu", so this allows to e.g #user.percentage there which gives you the desired percentage.
According to your description (if I understood it the right way) you want to store the calculated value in your user model. And here we go again: Model logic belongs into your model class. If you want to keep your percentage value up to date, you'd add a callback, like:
class User < ActiveRecord::Base
before_save :store_percentage
def precentage
# your code here
end
private
def store_percentage
self.my_percentage_field_from_my_database = self.percentage
end
end
To your actual problem: user.point may be NULL (db) / nil (ruby), so you should convert it to_i before actually calculating with it. Also that's why I've suggested an else block in your condition; To return a value wether or not your user.point is bigger than 1999 (if it even is an integer.. which I doubt in this case).
To answer on this question you should try to examine user.point, percentage value in the count_percentage method.
I strongly recommend pry. Because it's awesome.