-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0f4fba
commit 264df0f
Showing
7 changed files
with
84 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ Rake::TestTask.new do |t| | |
end | ||
|
||
desc "Run tests" | ||
task default: :test | ||
task default: :test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative "counter" | ||
|
||
module Statisfy | ||
module Aggregate | ||
def self.included(klass) | ||
klass.extend(ClassMethods) | ||
klass.class_eval do | ||
include Counter | ||
end | ||
end | ||
|
||
module ClassMethods | ||
# | ||
# Simply a shortcut for declaring an aggregation type counter | ||
# | ||
def aggregate(args = {}) | ||
raise ArgumentError, "You must provide the value to aggregate" if args[:value].blank? | ||
|
||
count(args.merge(type: :aggregate)) | ||
end | ||
|
||
# | ||
# Average type counters ret | ||
# | ||
# @param scope: the scope of the counter (an Organisation or a Department) | ||
# @param month: the month for which you want the value of the counter (optional) | ||
# | ||
def value(scope: nil, month: nil) | ||
p "HEIN???" | ||
month = month&.strftime("%Y-%m") if month.present? | ||
average(scope:, month:) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Statisfy | ||
module Monthly | ||
def self.included(klass) | ||
klass.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
# | ||
# Returns a hash of values grouped by month: | ||
# { | ||
# "01/2024" => 33.3, | ||
# "02/2024" => 36.6, | ||
# "03/2024" => 38.2, | ||
# } | ||
# | ||
# @param scope: the scope of the counter (an Organisation or a Department) | ||
# @param start_at: the date from which you want to start counting (optional) | ||
# @param stop_at: the date at which you want to stop counting (optional) | ||
# | ||
def values_grouped_by_month(scope: nil, start_at: nil, stop_at: nil) | ||
n_months = 24 | ||
|
||
if start_at.present? || scope&.created_at.present? | ||
start_at ||= scope.created_at | ||
n_months = (Time.zone.today.year + Time.zone.today.month) - (start_at.year + start_at.month) | ||
end | ||
|
||
relevant_months = (0..n_months).map do |i| | ||
(n_months - i).months.ago.beginning_of_month | ||
end | ||
|
||
relevant_months | ||
.filter { |month| stop_at.blank? || month < stop_at } | ||
.to_h do |month| | ||
[month.strftime("%m/%Y"), value(scope:, month:).round(2)] | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters