Skip to content

Commit

Permalink
wip: fixing for upstream updates from fava 1.25+
Browse files Browse the repository at this point in the history
Everything works except for the Fava version of asset allocation by
class, which does not render the tree correctly, and does not render a
graph at all
  • Loading branch information
redstreet committed Dec 13, 2023
1 parent 6e29dd7 commit 79a10f4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions fava_investor/common/beancountinvestorapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

from beancount.core import convert
from beancount import loader
from beancount.core import getters
from beancount.core import prices
Expand All @@ -14,6 +15,7 @@ class AccAPI:
def __init__(self, beancount_file, options):
self.entries, _, self.options_map = loader.load_file(beancount_file)
self.options = options
self.convert_position = convert.convert_position

def end_date(self):
return None # Only used in fava (UI selection context)
Expand Down
19 changes: 12 additions & 7 deletions fava_investor/common/favainvestorapi.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from beancount.core import getters
from fava.template_filters import cost_or_value
# from fava.template_filters import cost_or_value
from fava import __version__ as fava_version
from packaging import version
from fava.context import g
from fava.core.conversion import convert_position
from beancount.core import realization


class FavaInvestorAPI:
def __init__(self):
self.convert_position = convert_position

def build_price_map(self):
return g.ledger.price_map
return g.ledger.prices

def build_filtered_price_map(self, pcur, base_currency):
"""pcur and base_currency are currency strings"""
Expand All @@ -20,7 +25,7 @@ def get_commodity_directives(self):
return {entry.currency: entry for entry in g.filtered.ledger.all_entries_by_type.Commodity}

def realize(self):
return g.filtered.root_account
return realization.realize(g.filtered.entries)

def root_tree(self):
return g.filtered.root_tree
Expand Down Expand Up @@ -48,7 +53,7 @@ def get_account_open(self):
# TODO: below is probably fava only, and needs to be made beancount friendly
return g.ledger.all_entries_by_type.Open

def cost_or_value(self, node, date, include_children):
if include_children:
return cost_or_value(node.balance_children, date)
return cost_or_value(node.balance, date)
# def cost_or_value(self, node, date, include_children):
# if include_children:
# return cost_or_value(node.balance_children, date)
# return cost_or_value(node.balance, date)
5 changes: 3 additions & 2 deletions fava_investor/common/libinvestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import collections
import decimal
from beancount.core.inventory import Inventory
from beancount.core import convert
from beancount.core import convert # noqa: F401
from fava.core.conversion import convert_position


class Node(object):
Expand Down Expand Up @@ -93,7 +94,7 @@ def sum_inventories(invs):
total = ''
if t == Inventory:
total = sum_inventories([getattr(r, label) for r in rows])
total = total.reduce(convert.convert_position, accapi.get_operating_currencies()[0],
total = total.reduce(convert_position, accapi.get_operating_currencies()[0],
accapi.build_price_map())
elif t == decimal.Decimal:
total = sum([getattr(r, label) for r in rows])
Expand Down
3 changes: 2 additions & 1 deletion fava_investor/modules/assetalloc_class/libassetalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import re

# from fava.core.conversion import convert_position
from beancount.core import convert
from beancount.core import inventory
from beancount.core import position
Expand Down Expand Up @@ -100,7 +101,7 @@ def bucketize(vbalance, accapi):

# what we want is the conversion to be done on the end date, or on a date
# closest to it, either earlier or later. convert_position does this via bisect
amount = convert.convert_position(pos, base_currency, price_map, date=end_date)
amount = accapi.convert_position(pos, base_currency, price_map, date=end_date)
if amount.currency == pos.units.currency and amount.currency != base_currency:
# Ideally, we would automatically figure out the currency to hop via, based on the cost
# currency of the position. However, with vbalance, cost currency info is not
Expand Down
3 changes: 2 additions & 1 deletion fava_investor/modules/summarizer/libsummarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from beancount.core.data import Close
from beancount.core import realization
from beancount.core import convert
from fava.core.conversion import convert_position
from fava_investor.common.libinvestor import build_table_footer


Expand Down Expand Up @@ -167,7 +168,7 @@ def get_balance(realacc, account, pm, currency):
subtree = realization.get(realacc, account)
balance = realization.compute_balance(subtree)
vbalance = balance.reduce(convert.get_units)
market_value = vbalance.reduce(convert.convert_position, currency, pm)
market_value = vbalance.reduce(convert_position, currency, pm)
val = libinvestor.val(market_value)
return val
# return int(val)
Expand Down
10 changes: 4 additions & 6 deletions fava_investor/templates/Investor.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% import "_query_table.html" as querytable with context %}
{% import "_charts.html" as charts with context %}
{% set new_querytable = extension.use_new_querytable() %}

{% set module = request.args.get('module') %}
Expand All @@ -11,7 +10,7 @@
('summarizer', _('Summarizer')),
('minimizegains', _('Gains Minimizer'))
] %}
<h3><b>{% if not (module == key) %}<a href="{{ url_for('extension_report', report_name='Investor', module=key) }}">{{ label }}</a>{% else %} {{ label }}{% endif %}</b></h3>
<h3><b>{% if not (module == key) %}<a href="{{ url_for('extension_report', extension_name='Investor', module=key) }}">{{ label }}</a>{% else %} {{ label }}{% endif %}</b></h3>
{% endfor %}
</div>

Expand All @@ -23,8 +22,6 @@ <h3><b>{% if not (module == key) %}<a href="{{ url_for('extension_report', repor
</i>
{% endif %}

<svelte-component type="charts"></svelte-component>

<!-- -------------------------------------------------------------------------------- -->
{% macro table_list_renderer(title, tables) -%}
<h2>{{ title }}</h2>
Expand Down Expand Up @@ -206,7 +203,7 @@ <h3>What not to buy</h3>
{% endmacro %}

{% macro asset_allocation_hierarchy(serialised_tree, label='Asset Allocation') %}
{% do charts.chart_data.append({
{% do chart_data.append({
'type': 'hierarchy',
'label': label,
'data': {
Expand All @@ -219,10 +216,11 @@ <h3>What not to buy</h3>
{% if (module == 'aa_class') %}
<h2>Portfolio: Asset Allocation by Class</h2>

{% set chart_data = [] %}
{% set results = extension.build_assetalloc_by_class() %}

{{ asset_allocation_hierarchy(results[0].serialise(results[0]['currency']), label='Asset Allocation') }}
<svelte-component type="charts"><script type="application/json">{{ charts.chart_data|tojson }}</script></svelte-component>
<svelte-component type="charts"><script type="application/json">{{ chart_data|tojson }}</script></svelte-component>

{{ asset_tree(results[0]) }}

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ click_aliases == 1.0.1
# fava_investor/fava_investor/__init__.py: 3,4
# fava_investor/fava_investor/common/beancountinvestorapi.py: 29
# fava_investor/fava_investor/common/favainvestorapi.py: 2,3
fava>=1.23,<1.25
fava>=1.25

# fava_investor/.eggs/setuptools_scm-7.0.4-py3.8.egg/setuptools_scm/_entrypoints.py: 77
importlib_metadata == 1.5.0
Expand Down

0 comments on commit 79a10f4

Please sign in to comment.