mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Add the ability to "rollup" values in a time series (#554)
* Clean up time series models * Add value group rollup class for summarizing hierarchical data * Integrate new classes * Update UI to use new patterns * Update D3 charts to expect new data format * Clean up account model * More cleanup * Money improvements * Use new money fields * Remove invalid fixture data to avoid orphaned accountables * Update time series to work better with collections * Fix tests and UI bugs
This commit is contained in:
parent
0a8518506c
commit
f904d9d062
34 changed files with 687 additions and 391 deletions
48
app/models/time_series/trend.rb
Normal file
48
app/models/time_series/trend.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
class TimeSeries::Trend
|
||||
attr_reader :type
|
||||
|
||||
# Tells us whether an increasing/decreasing trend is good or bad (i.e. a liability decreasing is good)
|
||||
TYPES = %i[normal inverse].freeze
|
||||
|
||||
def initialize(current: nil, previous: nil, type: :normal)
|
||||
validate_data_types(current, previous)
|
||||
validate_type(type)
|
||||
@current = current
|
||||
@previous = previous
|
||||
@type = type
|
||||
end
|
||||
|
||||
def direction
|
||||
return "flat" if @previous.nil? || @current == @previous
|
||||
return "up" if @current && @current > @previous
|
||||
"down"
|
||||
end
|
||||
|
||||
def value
|
||||
return Money.new(0) if @previous.nil? && @current.is_a?(Money)
|
||||
return 0 if @previous.nil?
|
||||
@current - @previous
|
||||
end
|
||||
|
||||
def percent
|
||||
return 0.0 if @previous.nil?
|
||||
return Float::INFINITY if @previous == 0
|
||||
((extract_numeric(@current) - extract_numeric(@previous)).abs / extract_numeric(@previous).abs.to_f * 100).round(1).to_f
|
||||
end
|
||||
|
||||
private
|
||||
def validate_type(type)
|
||||
raise ArgumentError, "Invalid type" unless TYPES.include?(type)
|
||||
end
|
||||
|
||||
def validate_data_types(current, previous)
|
||||
return if previous.nil? || current.nil?
|
||||
raise ArgumentError, "Current and previous values must be of the same type" unless current.class == previous.class
|
||||
raise ArgumentError, "Current and previous values must be of type Money or Numeric" unless current.is_a?(Money) || current.is_a?(Numeric)
|
||||
end
|
||||
|
||||
def extract_numeric(obj)
|
||||
return obj.amount if obj.is_a? Money
|
||||
obj
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue