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

feat(accumulative balance chart): Add accumulative balance chart for account #33

Merged
merged 2 commits into from
Aug 28, 2023

Conversation

VladislavSokov
Copy link
Contributor

@VladislavSokov VladislavSokov commented Aug 28, 2023

No description provided.

@VladislavSokov VladislavSokov changed the title feat(accumulative balance chart): Add accumulative balance chart for … feat(accumulative balance chart): Add accumulative balance chart for account Aug 28, 2023
@ka8725 ka8725 merged commit c36c0a8 into master Aug 28, 2023
2 checks passed
@ka8725 ka8725 deleted the add-accumulative-balance-chart branch August 28, 2023 19:35
@@ -31,4 +31,8 @@ class Account < ApplicationRecord
def transactions
Transaction.where(to_account: self).or(Transaction.where(from_account: self))
end

def accumulative_balance_data
transactions.group_by_day(:created_at).sum(:amount)
Copy link
Member

@ka8725 ka8725 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VladislavSokov it doesn't produce accumulative balance:

  1. Sum by date does sum only for the date and nothing more, it doesn't "remember" the sum of the previous date. But the task is supposed to produce the accumulative sum
  2. Seems the gem won't help here

Here is the SQL that produces correct result:

with trs as (select id, created_at::date as date, case when to_account_id = 1 then amount else -amount end as amount from transactions where to_account_id = 1 or from_account_id = 1 order by created_at::date) select date, sum(amount) over (order by date) from trs order by date;

This is the result I got on my account:

    date    |   sum
------------+---------
 2021-10-22 |  135.00
 2021-10-22 |  135.00
 2021-10-22 |  135.00
 2021-10-22 |  135.00
 2021-10-24 |  100.00
 2021-10-29 |  110.00
 2021-10-29 |  110.00
 2021-11-05 |  134.00
 2021-11-05 |  134.00
 2021-11-06 |   90.00
 2021-11-12 |  125.00
 2021-11-19 |  160.00
 2021-11-20 |  -25.00
 2021-11-26 |   10.00
 2021-12-03 |   45.00
 2021-12-04 |    0.00
 2021-12-10 |   35.00
 2021-12-17 |   70.00
 2021-12-24 |  105.00
 2021-12-31 |  140.00
 2022-01-07 |  175.00
 2022-01-08 |   70.00
 2022-01-14 |  105.00
 2022-01-15 |   25.00
 2022-01-21 |   60.00
 2022-01-24 |   65.00
 2022-01-28 |  100.00
 2022-02-04 |  135.00
 2022-02-06 |  105.00
 2022-02-09 |   95.00
 2022-02-10 |  105.00
 2022-02-11 |  140.00
 2022-02-18 |  175.00
 2022-02-22 |  160.00
 2022-02-25 |  120.00
 2022-02-25 |  120.00
 2022-02-25 |  120.00
 2022-03-01 |    0.00
 2022-03-04 |   35.00
 2022-03-11 |   70.00
 2022-03-18 |  105.00
 2022-03-19 |   55.00
 2022-03-20 |   50.00
 2022-03-25 |   85.00
 2022-03-29 |   15.00
 2022-04-01 |   50.00
 2022-04-04 |    5.00
 2022-04-06 |    0.50
 2022-04-08 |   35.50
 2022-04-12 |   15.50
 2022-04-12 |   15.50
 2022-04-15 |   35.50
 2022-04-15 |   35.50
 2022-04-22 |   -4.50
 2022-04-22 |   -4.50
 2022-04-29 |   30.50
 2022-05-06 |   40.50
 2022-05-06 |   40.50
 2022-05-12 |   15.50
 2022-05-13 |   25.50
 2022-05-13 |   25.50
 2022-05-17 |    0.50
 2022-05-20 |    5.50
 2022-05-20 |    5.50
 2022-05-27 |   40.50
 2022-05-29 |   40.70
 2022-06-03 |   75.70
 2022-06-10 |  110.70
 2022-06-15 |  160.70
 2022-06-17 |  195.70
 2022-06-18 |   19.05
 2022-06-19 |   -5.00
 2022-06-19 |   -5.00
 2022-06-19 |   -5.00
 2022-06-20 |  -15.00
 2022-06-20 |  -15.00
 2022-06-24 |   20.00
 2022-06-26 |    6.00
 2022-07-01 |   41.00
 2022-07-04 |   16.00
 2022-07-08 |   51.00
 2022-07-09 |   21.00
 2022-07-15 |   31.00
 2022-07-15 |   31.00
 2022-07-16 |    6.00
 2022-07-18 |   16.00
 2022-07-19 |   26.00
 2022-07-20 |   22.00
 2022-07-20 |   22.00
 2022-07-22 |   62.00
 2022-07-22 |   62.00
 2022-07-23 |   12.00
 2022-07-26 |   14.00
 2022-07-29 |   49.00
 2022-07-30 |   -4.00
 2022-07-30 |   -4.00
 2022-08-05 |   -4.00
 2022-08-05 |   -4.00
 2022-08-10 |    1.00
 2022-08-12 |   -4.00
 2022-08-12 |   -4.00
 2022-08-15 |    6.00
 2022-08-19 |   41.00
 2022-08-22 |    6.00
 2022-08-24 |   -4.00
 2022-08-24 |   -4.00
 2022-08-26 |   23.00
 2022-08-26 |   23.00
 2022-08-26 |   23.00
 2022-09-02 |   63.00
 2022-09-02 |   63.00
 2022-09-08 |   38.00
 2022-09-09 |   73.00
 2022-09-14 |   78.00
 2022-09-16 |  113.00
 2022-09-23 |   63.00
 2022-09-23 |   63.00
 2022-09-23 |   63.00
 2022-09-30 |   98.00
 2022-10-07 |  133.00
 2022-10-14 |  168.00
 2022-10-21 |  203.00
 2022-10-28 |   98.00
 2022-10-28 |   98.00
 2022-11-01 |   62.00
 2022-11-02 |    2.00
 2022-11-03 |    6.00
 2022-11-03 |    6.00
 2022-11-03 |    6.00
 2022-11-04 |   41.00
 2022-11-05 |   11.00
 2022-11-11 |   46.00
 2022-11-18 |   18.00
 2022-11-18 |   18.00
 2022-11-25 |   53.00
 2022-11-26 |   10.00
 2022-12-02 |   45.00
 2022-12-03 |    0.00
 2022-12-09 |   35.00
 2022-12-16 |   70.00
 2022-12-18 | -105.00
 2022-12-18 | -105.00
 2022-12-20 |   35.00
 2022-12-23 |   53.00
 2022-12-23 |   53.00
 2022-12-30 |   88.00
 2023-01-06 |  123.00
 2023-01-07 |  -27.00
 2023-01-13 |    8.00
 2023-01-20 |   43.00
 2023-01-21 |   53.00
 2023-01-27 |   88.00
 2023-02-03 |  123.00
 2023-02-10 |  158.00
 2023-02-17 |  193.00
 2023-02-19 |  103.00
 2023-02-24 |  138.00
 2023-02-25 |  -12.00
 2023-02-25 |  -12.00
 2023-02-25 |  -12.00
 2023-03-01 |  -17.00
 2023-03-03 |   18.00
 2023-03-07 |   30.00
 2023-03-10 |   15.00
 2023-03-10 |   15.00
 2023-03-17 |   50.00
 2023-03-18 |    0.00
 2023-03-24 |   35.00
 2023-03-31 |   35.00
 2023-03-31 |   35.00
 2023-04-07 |   70.00
 2023-04-11 |   -7.00
 2023-04-14 |   28.00
 2023-04-21 |   44.00
 2023-04-21 |   44.00
 2023-04-24 |    5.00
 2023-04-25 | -135.00
 2023-04-28 | -100.00
 2023-05-05 |  -65.00
 2023-05-12 |  -30.00
 2023-05-19 |    5.00
 2023-05-26 |   40.00
 2023-05-29 |   10.00
 2023-06-11 |   80.00
 2023-06-16 |  115.00
 2023-06-23 |  150.00
 2023-06-27 |   20.00
 2023-06-30 |   55.00
 2023-07-07 |   90.00
 2023-07-14 |  125.00
 2023-07-15 |   15.00
 2023-07-21 |   50.00
 2023-07-25 |    0.00
 2023-07-28 |   35.00
 2023-08-02 |    0.00
 2023-08-04 |   35.00
 2023-08-10 |   16.00
 2023-08-11 |   51.00
 2023-08-15 |   16.00
 2023-08-17 |   -9.00
 2023-08-18 |   26.00
 2023-08-25 |   61.00
(202 rows)

This is not even close to what the current implementation does:

image

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.

2 participants