Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to "rollup" values in a time series #554

Merged
merged 16 commits into from
Mar 19, 2024
Merged

Conversation

zachgoll
Copy link
Collaborator

@zachgoll zachgoll commented Mar 18, 2024

Another prerequisite PR for #543 that aims to make it much easier to work with time series and aggregated data.

Overview

This PR introduces two important domain concepts:

  • TimeSeries + TimeSeries::Value
  • ValueGroup

Time Series Data

Maybe will be presenting user data in a time series format often. The TimeSeries model accepts an array of TimeSeries::Value objects, which have a date and value. This is a generic domain concept that works with numeric and money values:

money_series = TimeSeries.new([ { date: 1.day.ago.to_date, value: Money.new(100) }, { date: Date.current, value: Money.new(200) } ])

regular_numeric_series = TimeSeries.new([{ date: 1.day.ago.to_date, value: 100 }])

It also works with collections via the from_collection class method:

balance_series = TimeSeries.from_collection @account.balances, :balance_money

Value Groups

The purpose of value groups is to recursively "roll up" both individual values and time series arrays based on an arbitrary hierarchy. For example, we can create a hierarchy of assets:

  • assets
    • depositories
      • checking account
      • savings account
    • other assets
      • collectable account
# See value_group_test.rb
setup do
    checking = accounts(:checking)
    savings = accounts(:savings_with_valuation_overrides)
    collectable = accounts(:collectable)

    # Level 1
    @assets = ValueGroup.new("Assets")

    # Level 2
    @depositories = @assets.add_child_node("Depositories")
    @other_assets = @assets.add_child_node("Other Assets")

    # Level 3 (leaf/value nodes)
    @checking_node = @depositories.add_value_node(checking)
    @savings_node = @depositories.add_value_node(savings)
    @collectable_node = @other_assets.add_value_node(collectable)

    # Can attach time series to one or more nodes (which will also be "rolled up")
    @checking_node.attach_series(TimeSeries.from_collection(@checking.series, :balance_money))
end

@zachgoll zachgoll merged commit f904d9d into main Mar 19, 2024
4 checks passed
@zachgoll zachgoll deleted the account-rollups branch March 19, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant