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

[nyarlathotep] Project accounts across time #247

Merged
merged 1 commit into from
Dec 16, 2023
Merged
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
19 changes: 15 additions & 4 deletions hosts/nyarlathotep/jobs/hledger-export-to-promscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ def preprocess_group_credits_debits(postings):
"""Group postings by date and work out the total debit / credit for
each account. This is then used to simplify other metrics.

Accounts are projected forwards and backwards in time.

Postings are applied to an account and all of its superaccounts.
"""

key = lambda account, currency: (("account", account), ("currency", currency))
all_keys = set()

# credits_debits_by_date :: date => key => {credit, debit}
credits_debits_by_date = {}
Expand All @@ -104,13 +107,23 @@ def preprocess_group_credits_debits(postings):
else:
account = f"{account}:{segment}"

old = credits_debits.get(key(account, currency), {"credit": 0, "debit": 0})
credits_debits[key(account, currency)] = {
k = key(account, currency)
all_keys.add(k)

old = credits_debits.get(k, {"credit": 0, "debit": 0})
credits_debits[k] = {
"credit": old["credit"] + credit,
"debit": old["debit"] + debit,
}
credits_debits_by_date[posting["date"]] = credits_debits

# Project accounts through all time
for timestamp in credits_debits_by_date.keys():
credits_debits = credits_debits_by_date[timestamp]
for k in all_keys:
credits_debits[k] = credits_debits.get(k, {"credit": 0, "debit": 0})
credits_debits_by_date[timestamp] = credits_debits

return credits_debits_by_date


Expand Down Expand Up @@ -172,8 +185,6 @@ def metric_hledger_balance(credits_debits):
Accounts are propagated forward in time: if an account is seen at
time T, then its balance will also be reported at time T+1, T+2,
etc.

Postings are applied to an account and all of its superaccounts.
"""

# deltas_by_timestamp :: timestamp => key => delta
Expand Down