mirror of
https://github.com/TracksApp/tracks.git
synced 2026-02-05 23:41:48 +01:00
Collapse duplication in tag cloud model
This commit is contained in:
parent
edc793e703
commit
447178bd7d
2 changed files with 27 additions and 39 deletions
|
|
@ -3,62 +3,47 @@
|
|||
module Stats
|
||||
class TagCloud
|
||||
|
||||
attr_reader :user, :cutoff,
|
||||
:tags, :min, :divisor,
|
||||
:tags_90days, :min_90days, :divisor_90days
|
||||
def initialize(user, cutoff)
|
||||
attr_reader :user, :cutoff, :levels,
|
||||
:tags, :min, :divisor
|
||||
def initialize(user, cutoff = nil)
|
||||
@user = user
|
||||
@cutoff = cutoff
|
||||
@levels = 10
|
||||
end
|
||||
|
||||
def compute
|
||||
levels=10
|
||||
# TODO: parameterize limit
|
||||
|
||||
# Get the tag cloud for all tags for actions
|
||||
query = "SELECT tags.id, name, count(*) AS count"
|
||||
query << " FROM taggings, tags, todos"
|
||||
query << " WHERE tags.id = tag_id"
|
||||
query << " AND taggings.taggable_id = todos.id"
|
||||
query << " AND todos.user_id="+user.id.to_s+" "
|
||||
query << " AND taggings.taggable_type='Todo' "
|
||||
query << " GROUP BY tags.id, tags.name"
|
||||
query << " ORDER BY count DESC, name"
|
||||
query << " LIMIT 100"
|
||||
@tags = Tag.find_by_sql(query).sort_by { |tag| tag.name.downcase }
|
||||
|
||||
@tags = top_tags
|
||||
max, @min = 0, 0
|
||||
@tags.each { |t|
|
||||
max = [t.count.to_i, max].max
|
||||
@min = [t.count.to_i, @min].min
|
||||
}
|
||||
|
||||
@divisor = ((max - @min) / levels) + 1
|
||||
end
|
||||
|
||||
# Get the tag cloud for all tags for actions
|
||||
private
|
||||
|
||||
def top_tags
|
||||
params = [sql, user.id]
|
||||
params += [cutoff, cutoff] if cutoff
|
||||
Tag.find_by_sql(params).sort_by { |tag| tag.name.downcase }
|
||||
end
|
||||
|
||||
def sql
|
||||
# TODO: parameterize limit
|
||||
query = "SELECT tags.id, tags.name AS name, count(*) AS count"
|
||||
query << " FROM taggings, tags, todos"
|
||||
query << " WHERE tags.id = tag_id"
|
||||
query << " AND todos.user_id=? "
|
||||
query << " AND taggings.taggable_type='Todo' "
|
||||
query << " AND taggings.taggable_id=todos.id "
|
||||
query << " AND (todos.created_at > ? OR "
|
||||
query << " todos.completed_at > ?) "
|
||||
if cutoff
|
||||
query << " AND (todos.created_at > ? OR "
|
||||
query << " todos.completed_at > ?) "
|
||||
end
|
||||
query << " GROUP BY tags.id, tags.name"
|
||||
query << " ORDER BY count DESC, name"
|
||||
query << " LIMIT 100"
|
||||
@tags_90days = Tag.find_by_sql(
|
||||
[query, user.id, cutoff, cutoff]
|
||||
).sort_by { |tag| tag.name.downcase }
|
||||
|
||||
max_90days, @min_90days = 0, 0
|
||||
@tags_90days.each { |t|
|
||||
max_90days = [t.count.to_i, max_90days].max
|
||||
@min_90days = [t.count.to_i, @min_90days].min
|
||||
}
|
||||
|
||||
@divisor_90days = ((max_90days - @min_90days) / levels) + 1
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue