1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 20:15:22 +02:00

Improve chart performance and gapfilling (#2306)
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

This commit is contained in:
Zach Gollwitzer 2025-05-25 20:40:18 -04:00 committed by GitHub
parent e1b81ef879
commit 6e202bd7ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 382 additions and 196 deletions

View file

@ -1,9 +1,21 @@
class Series
attr_reader :start_date, :end_date, :interval, :trend, :values
# Behave like an Array whose elements are the `Value` structs stored in `values`
include Enumerable
# Forward any undefined method calls (e.g. `first`, `[]`, `map`) to `values`
delegate_missing_to :values
# Enumerable needs `#each`
def each(&block)
values.each(&block)
end
attr_reader :start_date, :end_date, :interval, :trend, :values, :favorable_direction
Value = Struct.new(
:date,
:date_formatted,
:value,
:trend,
keyword_init: true
)
@ -29,6 +41,7 @@ class Series
Value.new(
date: curr_value[:date],
date_formatted: I18n.l(curr_value[:date], format: :long),
value: curr_value[:value],
trend: Trend.new(
current: curr_value[:value],
previous: prev_value&.[](:value)
@ -39,19 +52,29 @@ class Series
end
end
def initialize(start_date:, end_date:, interval:, trend:, values:)
def initialize(start_date:, end_date:, interval:, values:, favorable_direction: "up")
@start_date = start_date
@end_date = end_date
@interval = interval
@trend = trend
@values = values
@favorable_direction = favorable_direction
end
def current
values.last.trend.current
def trend
@trend ||= Trend.new(
current: values.last&.value,
previous: values.first&.value,
favorable_direction: favorable_direction
)
end
def any?
values.any?
def as_json
{
start_date: start_date,
end_date: end_date,
interval: interval,
trend: trend,
values: values.map { |v| { date: v.date, date_formatted: v.date_formatted, value: v.value, trend: v.trend } }
}
end
end