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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ gem 'devise'
gem "bulma-rails", "~> 0.9.2"
gem 'slim'
gem "font-awesome-rails"
gem 'chartkick'
gem 'groupdate'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
chartkick (5.0.4)
childprocess (4.1.0)
coderay (1.1.3)
concurrent-ruby (1.1.9)
Expand Down Expand Up @@ -110,6 +111,8 @@ GEM
railties (>= 3.2, < 7)
globalid (0.5.2)
activesupport (>= 5.0)
groupdate (6.2.1)
activesupport (>= 5.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
Expand Down Expand Up @@ -299,12 +302,14 @@ DEPENDENCIES
bulma-rails (~> 0.9.2)
byebug
capybara (>= 2.15)
chartkick
database_cleaner
devise
dry-initializer
factory_bot_rails (~> 6.2)
ffaker
font-awesome-rails
groupdate
jbuilder (~> 2.7)
letter_opener
listen (>= 3.0.5, < 3.2)
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

import "chartkick/chart.js"

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
Expand Down
4 changes: 4 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

end
end
2 changes: 2 additions & 0 deletions app/views/accounts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@

.card-footer
= link_to '+ Add', new_account_account_automatic_topup_config_path(account), class: 'card-footer-item'
= line_chart account.accumulative_balance_data, xtitle: 'Date', ytitle: 'Accumulative Balance'

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "^5.4.3",
"babel-loader": "^8.2.2",
"chart.js": "^4.4.0",
"chartkick": "^5.0.1",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
Expand Down
16 changes: 16 additions & 0 deletions spec/models/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Account, type: :model do
describe '#accumulative_balance_data' do
let(:parent) { create(:account, :parent) }
let(:child) { create(:account, :children, parent: parent) }
let!(:first_transaction) { create(:transaction, to_account: child, from_account: parent, amount: 100, created_at: 1.day.ago) }
let!(:second_transaction) { create(:transaction, to_account: child, from_account: parent, amount: 50, created_at: Time.current) }

it 'calculates accumulative balance data' do
expect(parent.accumulative_balance_data).to eq({ first_transaction.created_at.to_date => 100, second_transaction.created_at.to_date => 50 })
end
end
end
45 changes: 45 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,13 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.21.0":
version "7.22.11"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.11.tgz#7a9ba3bbe406ad6f9e8dd4da2ece453eb23a77a4"
integrity sha512-ee7jVNlWN09+KftVOu9n7S8gQzD/Z6hN/I8VBRXW4P1+Xe7kJGXMwu8vds4aGIMHZnNbdpSWCfZZtinytpcAvA==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.15.4":
version "7.15.4"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.15.4.tgz#51898d35dcf3faa670c4ee6afcfd517ee139f194"
Expand Down Expand Up @@ -895,6 +902,11 @@
resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.2.tgz#30aa825f11d438671d585bd44e7fd564535fc210"
integrity sha512-82cpyJyKRoQoRi+14ibCeGPu0CwypgtBAdBhq1WfvagpCZNKqwXbKwXllYSMG91DhmG4jt9gN8eP6lGOtozuaw==

"@kurkle/color@^0.3.0":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@kurkle/color/-/color-0.3.2.tgz#5acd38242e8bde4f9986e7913c8fdf49d3aa199f"
integrity sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -1954,6 +1966,27 @@ chalk@^2.0, chalk@^2.0.0, chalk@^2.4.1, chalk@^2.4.2:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chart.js@4, chart.js@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-4.4.0.tgz#df843fdd9ec6bd88d7f07e2b95348d221bd2698c"
integrity sha512-vQEj6d+z0dcsKLlQvbKIMYFHd3t8W/7L2vfJIbYcfyPcRx92CsHqECpueN8qVGNlKyDcr5wBrYAYKnfu/9Q1hQ==
dependencies:
"@kurkle/color" "^0.3.0"

chartjs-adapter-date-fns@>=3:
version "3.0.0"
resolved "https://registry.yarnpkg.com/chartjs-adapter-date-fns/-/chartjs-adapter-date-fns-3.0.0.tgz#c25f63c7f317c1f96f9a7c44bd45eeedb8a478e5"
integrity sha512-Rs3iEB3Q5pJ973J93OBTpnP7qoGwvq3nUnoMdtxO+9aoJof7UFcRbWcIDteXuYd1fgAvct/32T9qaLyLuZVwCg==

chartkick@^5.0.1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/chartkick/-/chartkick-5.0.1.tgz#f557ff8560f974343dc65c7fc34ce1e8326d8ee7"
integrity sha512-4F3tWI3eBQgnjCYZIZ+fHOaJuNyxeyhDE2Tm+voOWB19hDjSJceys/spzN52DOn8bWepNESGXvPVTGU1jeFsbA==
optionalDependencies:
chart.js "4"
chartjs-adapter-date-fns ">=3"
date-fns ">=2"

"chokidar@>=3.0.0 <4.0.0", chokidar@^3.4.1, chokidar@^3.5.1:
version "3.5.2"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75"
Expand Down Expand Up @@ -2518,6 +2551,13 @@ cyclist@^1.0.1:
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=

date-fns@>=2:
version "2.30.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0"
integrity sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==
dependencies:
"@babel/runtime" "^7.21.0"

[email protected], debug@^2.2.0, debug@^2.3.3:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -5962,6 +6002,11 @@ regenerator-runtime@^0.13.4, regenerator-runtime@^0.13.9:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==

regenerator-runtime@^0.14.0:
version "0.14.0"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz#5e19d68eb12d486f797e15a3c6a918f7cec5eb45"
integrity sha512-srw17NI0TUWHuGa5CFGGmhfNIeja30WMBfbslPNhf6JrqQlLN5gcrvig1oqPxiVaXb0oW0XRKtH6Nngs5lKCIA==

regenerator-transform@^0.14.2:
version "0.14.5"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.14.5.tgz#c98da154683671c9c4dcb16ece736517e1b7feb4"
Expand Down