Code style fixes

This commit is contained in:
Jyri-Petteri Paloposki 2020-10-10 02:27:42 +03:00
parent c6bbc67dab
commit d8acf60049
72 changed files with 458 additions and 594 deletions

View file

@ -1,9 +1,9 @@
module Stats
class Actions
SECONDS_PER_DAY = 86400;
SECONDS_PER_DAY = 86_400
attr_reader :user
def initialize(user)
@user = user
@ -58,17 +58,17 @@ module Stats
@interpolated_actions_created_this_month = interpolate_avg_for_current_month(@actions_created_last12months_array)
@interpolated_actions_done_this_month = interpolate_avg_for_current_month(@actions_done_last12months_array)
@created_count_array = Array.new(13, actions_last12months.created_after(@cut_off_year).count(:all)/12.0)
@done_count_array = Array.new(13, actions_last12months.completed_after(@cut_off_year).count(:all)/12.0)
@created_count_array = Array.new(13, actions_last12months.created_after(@cut_off_year).count(:all) / 12.0)
@done_count_array = Array.new(13, actions_last12months.completed_after(@cut_off_year).count(:all) / 12.0)
return {
datasets: [
{label: I18n.t('stats.labels.avg_created'), data: @created_count_array, type: "line"},
{label: I18n.t('stats.labels.avg_completed'), data: @done_count_array, type: "line"},
{label: I18n.t('stats.labels.month_avg_completed', :months => 3), data: @actions_done_avg_last12months_array, type: "line"},
{label: I18n.t('stats.labels.month_avg_created', :months => 3), data: @actions_created_avg_last12months_array, type: "line"},
{label: I18n.t('stats.labels.created'), data: @actions_created_last12months_array},
{label: I18n.t('stats.labels.completed'), data: @actions_done_last12months_array},
{ label: I18n.t('stats.labels.avg_created'), data: @created_count_array, type: "line" },
{ label: I18n.t('stats.labels.avg_completed'), data: @done_count_array, type: "line" },
{ label: I18n.t('stats.labels.month_avg_completed', :months => 3), data: @actions_done_avg_last12months_array, type: "line" },
{ label: I18n.t('stats.labels.month_avg_created', :months => 3), data: @actions_created_avg_last12months_array, type: "line" },
{ label: I18n.t('stats.labels.created'), data: @actions_created_last12months_array },
{ label: I18n.t('stats.labels.completed'), data: @actions_done_last12months_array },
],
labels: array_of_month_labels(@done_count_array.size),
}
@ -86,17 +86,17 @@ module Stats
# find max for graph in both hashes
@max = [@actions_done_last30days_array.max, @actions_created_last30days_array.max].max
created_count_array = Array.new(30){ |i| @actions_created_last30days.size/30.0 }
done_count_array = Array.new(30){ |i| @actions_done_last30days.size/30.0 }
created_count_array = Array.new(30) { |i| @actions_created_last30days.size / 30.0 }
done_count_array = Array.new(30) { |i| @actions_done_last30days.size / 30.0 }
# TODO: make the strftime i18n proof
time_labels = Array.new(30){ |i| I18n.l(Time.zone.now-i.days, :format => :stats) }
time_labels = Array.new(30) { |i| I18n.l(Time.zone.now-i.days, :format => :stats) }
return {
datasets: [
{label: I18n.t('stats.labels.avg_created'), data: created_count_array, type: "line"},
{label: I18n.t('stats.labels.avg_completed'), data: done_count_array, type: "line"},
{label: I18n.t('stats.labels.created'), data: @actions_created_last30days_array},
{label: I18n.t('stats.labels.completed'), data: @actions_done_last30days_array},
{ label: I18n.t('stats.labels.avg_created'), data: created_count_array, type: "line" },
{ label: I18n.t('stats.labels.avg_completed'), data: done_count_array, type: "line" },
{ label: I18n.t('stats.labels.created'), data: @actions_created_last30days_array },
{ label: I18n.t('stats.labels.completed'), data: @actions_done_last30days_array },
],
labels: time_labels,
}
@ -119,14 +119,14 @@ module Stats
# get percentage done cumulative
@cum_percent_done = convert_to_cumulative_array(@actions_completion_time_array, @actions_completion_time.count(:all))
time_labels = Array.new(@count){ |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count) { |i| "#{i}-#{i+1}" }
time_labels[0] = I18n.t('stats.within_one')
time_labels[@count] = "> #{@count}"
return {
datasets: [
{label: I18n.t('stats.legend.percentage'), data: @cum_percent_done, type: "line"},
{label: I18n.t('stats.legend.actions'), data: @actions_completion_time_array},
{ label: I18n.t('stats.legend.percentage'), data: @cum_percent_done, type: "line" },
{ label: I18n.t('stats.legend.actions'), data: @actions_completion_time_array },
],
labels: time_labels,
}
@ -149,14 +149,14 @@ module Stats
# get percentage done cumulative
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count )
time_labels = Array.new(@count){ |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count) { |i| "#{i}-#{i+1}" }
time_labels[0] = "< 1"
time_labels[@count] = "> #{@count}"
return {
datasets: [
{label: I18n.t('stats.running_time_all_legend.percentage'), data: @cum_percent_done, type: "line"},
{label: I18n.t('stats.running_time_all_legend.actions'), data: @actions_running_time_array},
{ label: I18n.t('stats.running_time_all_legend.percentage'), data: @cum_percent_done, type: "line" },
{ label: I18n.t('stats.running_time_all_legend.actions'), data: @actions_running_time_array },
],
labels: time_labels,
}
@ -171,12 +171,12 @@ module Stats
# - actions not deferred (show_from must be null)
# - actions not pending/blocked
@actions_running_time = @user.todos.not_completed.not_hidden.not_deferred_or_blocked.
select("todos.created_at").
reorder("todos.created_at DESC")
@actions_running_time = @user.todos.not_completed.not_hidden.not_deferred_or_blocked
.select("todos.created_at")
.reorder("todos.created_at DESC")
@max_weeks = difference_in_weeks(@today, @actions_running_time.last.created_at)
@actions_running_per_week_array = convert_to_weeks_from_today_array(@actions_running_time, @max_weeks+1, :created_at)
@actions_running_per_week_array = convert_to_weeks_from_today_array(@actions_running_time, @max_weeks + 1, :created_at)
# cut off chart at 52 weeks = one year
@count = [52, @max_weeks].min
@ -188,23 +188,23 @@ module Stats
# get percentage done cumulative
@cum_percent_done = convert_to_cumulative_array(@actions_running_time_array, @actions_running_time.count )
time_labels = Array.new(@count){ |i| "#{i}-#{i+1}" }
time_labels = Array.new(@count) { |i| "#{i}-#{i+1}" }
time_labels[0] = "< 1"
time_labels[@count] = "> #{@count}"
return {
datasets: [
{label: I18n.t('stats.running_time_legend.percentage'), data: @cum_percent_done, type: "line"},
{label: I18n.t('stats.running_time_legend.actions'), data: @actions_running_time_array},
{ label: I18n.t('stats.running_time_legend.percentage'), data: @cum_percent_done, type: "line" },
{ label: I18n.t('stats.running_time_legend.actions'), data: @actions_running_time_array },
],
labels: time_labels,
}
end
def open_per_week_data
@actions_started = @user.todos.created_after(@today-53.weeks).
select("todos.created_at, todos.completed_at").
reorder("todos.created_at DESC")
@actions_started = @user.todos.created_after(@today - 53.weeks)
.select("todos.created_at, todos.completed_at")
.reorder("todos.created_at DESC")
@max_weeks = difference_in_weeks(@today, @actions_started.last.created_at)
@ -214,12 +214,12 @@ module Stats
@actions_open_per_week_array = convert_to_weeks_running_from_today_array(@actions_started, @max_weeks+1)
@actions_open_per_week_array = cut_off_array(@actions_open_per_week_array, @count)
time_labels = Array.new(@count+1){ |i| "#{i}-#{i+1}" }
time_labels[0] = "< 1"
time_labels = Array.new(@count+1) { |i| "#{i}-#{i+1}" }
time_labels[0] = "< 1"
return {
datasets: [
{label: I18n.t('stats.open_per_week_legend.actions'), data: @actions_open_per_week_array},
{ label: I18n.t('stats.open_per_week_legend.actions'), data: @actions_open_per_week_array },
],
labels: time_labels,
}
@ -230,18 +230,18 @@ module Stats
@actions_completion_day = @user.todos.completed.select("completed_at")
# convert to array and fill in non-existing days
@actions_creation_day_array = Array.new(7) { |i| 0}
@actions_creation_day.each { |t| @actions_creation_day_array[ t.created_at.wday ] += 1 }
@actions_creation_day_array = Array.new(7) { |i| 0 }
@actions_creation_day.each { |t| @actions_creation_day_array[t.created_at.wday] += 1 }
@max = @actions_creation_day_array.max
# convert to array and fill in non-existing days
@actions_completion_day_array = Array.new(7) { |i| 0}
@actions_completion_day.each { |t| @actions_completion_day_array[ t.completed_at.wday ] += 1 }
@actions_completion_day_array = Array.new(7) { |i| 0 }
@actions_completion_day.each { |t| @actions_completion_day_array[t.completed_at.wday] += 1 }
return {
datasets: [
{label: I18n.t('stats.labels.created'), data: @actions_creation_day_array},
{label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array},
{ label: I18n.t('stats.labels.created'), data: @actions_creation_day_array },
{ label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array },
],
labels: I18n.t('date.day_names'),
}
@ -253,17 +253,17 @@ module Stats
# convert to hash to be able to fill in non-existing days
@max=0
@actions_creation_day_array = Array.new(7) { |i| 0}
@actions_creation_day.each { |r| @actions_creation_day_array[ r.created_at.wday ] += 1 }
@actions_creation_day_array = Array.new(7) { |i| 0 }
@actions_creation_day.each { |r| @actions_creation_day_array[r.created_at.wday] += 1 }
# convert to hash to be able to fill in non-existing days
@actions_completion_day_array = Array.new(7) { |i| 0}
@actions_completion_day_array = Array.new(7) { |i| 0 }
@actions_completion_day.each { |r| @actions_completion_day_array[r.completed_at.wday] += 1 }
return {
datasets: [
{label: I18n.t('stats.labels.created'), data: @actions_creation_day_array},
{label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array},
{ label: I18n.t('stats.labels.created'), data: @actions_creation_day_array },
{ label: I18n.t('stats.labels.completed'), data: @actions_completion_day_array },
],
labels: I18n.t('date.day_names'),
}
@ -274,17 +274,17 @@ module Stats
@actions_completion_hour = @user.todos.completed.select("completed_at")
# convert to hash to be able to fill in non-existing days
@actions_creation_hour_array = Array.new(24) { |i| 0}
@actions_creation_hour_array = Array.new(24) { |i| 0 }
@actions_creation_hour.each{|r| @actions_creation_hour_array[r.created_at.hour] += 1 }
# convert to hash to be able to fill in non-existing days
@actions_completion_hour_array = Array.new(24) { |i| 0}
@actions_completion_hour_array = Array.new(24) { |i| 0 }
@actions_completion_hour.each{|r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
return {
datasets: [
{label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array},
{label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array},
{ label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array },
{ label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array },
],
labels: @actions_creation_hour_array.each_with_index.map { |total, hour| [hour] },
}
@ -295,17 +295,17 @@ module Stats
@actions_completion_hour = @user.todos.completed_after(@cut_off_month).select("completed_at")
# convert to hash to be able to fill in non-existing days
@actions_creation_hour_array = Array.new(24) { |i| 0}
@actions_creation_hour_array = Array.new(24) { |i| 0 }
@actions_creation_hour.each{|r| @actions_creation_hour_array[r.created_at.hour] += 1 }
# convert to hash to be able to fill in non-existing days
@actions_completion_hour_array = Array.new(24) { |i| 0}
@actions_completion_hour_array = Array.new(24) { |i| 0 }
@actions_completion_hour.each{|r| @actions_completion_hour_array[r.completed_at.hour] += 1 }
return {
datasets: [
{label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array},
{label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array},
{ label: I18n.t('stats.labels.created'), data: @actions_creation_hour_array },
{ label: I18n.t('stats.labels.completed'), data: @actions_completion_hour_array },
],
labels: @actions_creation_hour_array.each_with_index.map { |total, hour| [hour] },
}
@ -334,7 +334,7 @@ module Stats
end
def interpolate_avg_for_current_month(set)
(set[0]*(1/percent_of_month) + set[1] + set[2]) / 3.0
(set[0] * (1 / percent_of_month) + set[1] + set[2]) / 3.0
end
def percent_of_month
@ -350,15 +350,15 @@ module Stats
end
def put_events_into_month_buckets(records, array_size, date_method_on_todo)
convert_to_array(records.select { |x| x.send(date_method_on_todo) }, array_size) { |r| [difference_in_months(@today, r.send(date_method_on_todo))]}
convert_to_array(records.select { |x| x.send(date_method_on_todo) }, array_size) { |r| [difference_in_months(@today, r.send(date_method_on_todo))] }
end
def convert_to_days_from_today_array(records, array_size, date_method_on_todo)
return convert_to_array(records, array_size){ |r| [difference_in_days(@today, r.send(date_method_on_todo))]}
return convert_to_array(records, array_size) { |r| [difference_in_days(@today, r.send(date_method_on_todo))] }
end
def convert_to_weeks_from_today_array(records, array_size, date_method_on_todo)
return convert_to_array(records, array_size) { |r| [difference_in_weeks(@today, r.send(date_method_on_todo))]}
return convert_to_array(records, array_size) { |r| [difference_in_weeks(@today, r.send(date_method_on_todo))] }
end
def convert_to_weeks_running_array(records, array_size)
@ -373,37 +373,37 @@ module Stats
a = []
start_week = difference_in_weeks(@today, record.created_at)
end_week = record.completed_at ? difference_in_weeks(@today, record.completed_at) : 0
end_week.upto(start_week) { |i| a << i };
end_week.upto(start_week) { |i| a << i }
return a
end
def cut_off_array_with_sum(array, cut_off)
# +1 to hold sum of rest
a = Array.new(cut_off+1){|i| array[i]||0}
a = Array.new(cut_off + 1) { |i| array[i] || 0 }
# add rest of array to last elem
a[cut_off] += array.inject(:+) - a.inject(:+)
return a
end
def cut_off_array(array, cut_off)
return Array.new(cut_off){|i| array[i]||0}
return Array.new(cut_off) { |i| array[i] || 0 }
end
def convert_to_cumulative_array(array, max)
# calculate fractions
a = Array.new(array.size){|i| array[i]*100.0/max}
a = Array.new(array.size) {|i| array[i] * 100.0 / max}
# make cumulative
1.upto(array.size-1){ |i| a[i] += a[i-1] }
1.upto(array.size-1) { |i| a[i] += a[i - 1] }
return a
end
def difference_in_months(date1, date2)
return (date1.utc.year - date2.utc.year)*12 + (date1.utc.month - date2.utc.month)
return (date1.utc.year - date2.utc.year) * 12 + (date1.utc.month - date2.utc.month)
end
# assumes date1 > date2
def difference_in_days(date1, date2)
return ((date1.utc.at_midnight-date2.utc.at_midnight)/SECONDS_PER_DAY).to_i
return ((date1.utc.at_midnight - date2.utc.at_midnight) / SECONDS_PER_DAY).to_i
end
# assumes date1 > date2
@ -412,23 +412,23 @@ module Stats
end
def three_month_avg(set, i)
(set.fetch(i){ 0 } + set.fetch(i+1){ 0 } + set.fetch(i+2){ 0 }) / 3.0
(set.fetch(i) { 0 } + set.fetch(i+1) { 0 } + set.fetch(i + 2) { 0 }) / 3.0
end
def set_three_month_avg(set,upper_bound)
(0..upper_bound-1).map { |i| three_month_avg(set, i) }
def set_three_month_avg(set, upper_bound)
(0..upper_bound - 1).map { |i| three_month_avg(set, i) }
end
def compute_running_avg_array(set, upper_bound)
result = set_three_month_avg(set, upper_bound)
result[upper_bound-1] = result[upper_bound-1] * 3 if upper_bound == set.length
result[upper_bound-2] = result[upper_bound-2] * 3 / 2 if upper_bound > 1 and upper_bound == set.length
result[upper_bound - 1] = result[upper_bound-1] * 3 if upper_bound == set.length
result[upper_bound - 2] = result[upper_bound-2] * 3 / 2 if upper_bound > 1 and upper_bound == set.length
result[0] = "null"
result
end # unsolved, not triggered, edge case for set.length == upper_bound + 1
def month_label(i)
I18n.t('date.month_names')[ (Time.zone.now.mon - i -1 ) % 12 + 1 ]
I18n.t('date.month_names')[(Time.zone.now.mon - i -1 ) % 12 + 1]
end
def array_of_month_labels(count)

View file

@ -1,8 +1,7 @@
module Stats
class Chart
attr_reader :action, :height, :width
def initialize(action, dimensions = {})
@action = action
@height = dimensions.fetch(:height) { 250 }
@ -12,7 +11,5 @@ module Stats
def dimensions
"#{width}x#{height}"
end
end
end

View file

@ -1,7 +1,7 @@
module Stats
class Contexts
attr_reader :user
def initialize(user)
@user = user
end

View file

@ -1,7 +1,7 @@
module Stats
class Projects
attr_reader :user
def initialize(user)
@user = user
end
@ -24,6 +24,5 @@ module Stats
projects = user.projects.order('created_at ASC')
projects.sort_by{ |p| p.running_time }.reverse.take(10)
end
end
end

View file

@ -2,8 +2,8 @@
# http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/
module Stats
class TagCloud
attr_reader :levels, :tags
def initialize(tags)
@levels = 10
@tags = tags.sort_by { |tag| tag.name.downcase }
@ -32,7 +32,7 @@ module Stats
end
def counts
@counts ||= tags.map {|t| t.count}
@counts ||= tags.map { |t| t.count }
end
end
end

View file

@ -1,7 +1,7 @@
module Stats
class TagCloudQuery
attr_reader :user, :cutoff
def initialize(user, cutoff = nil)
@user = user
@cutoff = cutoff
@ -19,20 +19,19 @@ module Stats
def sql
# TODO: parameterize limit
query = "SELECT tags.id, tags.name AS name, count(*) AS count"
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.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 << " ORDER BY count DESC, name "
query << " LIMIT 100"
end
end
end

View file

@ -1,9 +1,9 @@
module Stats
class TimeToComplete
SECONDS_PER_DAY = 86400;
SECONDS_PER_DAY = 86_400
attr_reader :actions
def initialize(actions)
@actions = actions
end
@ -52,12 +52,11 @@ module Stats
end
def sum
@sum ||= durations.inject(0) {|sum, d| sum + d}
@sum ||= durations.inject(0) { |sum, d| sum + d }
end
def arbitrary_day
@arbitrary_day ||= Time.utc(2000, 1, 1, 0, 0)
end
end
end

View file

@ -3,8 +3,8 @@
# and visible contexts will be included.
module Stats
class TopContextsQuery
attr_reader :user, :running, :limit
def initialize(user, options = {})
@user = user
@running = options.fetch(:running) { false }

View file

@ -3,8 +3,8 @@
# or completed since that cutoff will be included.
module Stats
class TopProjectsQuery
attr_reader :user, :cutoff
def initialize(user, cutoff = nil)
@user = user
@cutoff = cutoff
@ -34,6 +34,5 @@ module Stats
query << "ORDER BY count DESC "
query << "LIMIT 10"
end
end
end

View file

@ -1,7 +1,7 @@
module Stats
class Totals
attr_reader :user
def initialize(user)
@user = user
end
@ -83,6 +83,5 @@ module Stats
def tag_ids
@tag_ids ||= Stats::UserTagsQuery.new(user).result.map(&:id)
end
end
end

View file

@ -1,7 +1,7 @@
module Stats
class UserStats
attr_reader :user
def initialize(user)
@user = user
end
@ -38,6 +38,5 @@ module Stats
end
@tag_cloud_90days
end
end
end

View file

@ -1,7 +1,7 @@
module Stats
class UserTagsQuery
attr_reader :user
def initialize(user)
@user = user
end
@ -21,6 +21,5 @@ module Stats
AND todos.user_id = ?
SQL
end
end
end