mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-23 10:40:13 +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
|
|
@ -552,15 +552,18 @@ class StatsController < ApplicationController
|
|||
end
|
||||
|
||||
def get_stats_tags
|
||||
cloud = Stats::TagCloud.new(current_user, @cut_off_3months)
|
||||
cloud = Stats::TagCloud.new(current_user)
|
||||
cloud.compute
|
||||
|
||||
@tags_for_cloud = cloud.tags
|
||||
@tags_min = cloud.min
|
||||
@tags_divisor = cloud.divisor
|
||||
@tags_for_cloud_90days = cloud.tags_90days
|
||||
@tags_min_90days = cloud.min_90days
|
||||
@tags_divisor_90days = cloud.divisor_90days
|
||||
|
||||
cloud = Stats::TagCloud.new(current_user, @cut_off_3months)
|
||||
cloud.compute
|
||||
@tags_for_cloud_90days = cloud.tags
|
||||
@tags_min_90days = cloud.min
|
||||
@tags_divisor_90days = cloud.divisor
|
||||
end
|
||||
|
||||
def get_ids_from (actions, week_from, week_to, at_end)
|
||||
|
|
|
|||
|
|
@ -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 "
|
||||
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