Skip to content

Commit

Permalink
stream controller - get all streams
Browse files Browse the repository at this point in the history
  • Loading branch information
wkrzywiec committed Feb 14, 2025
1 parent 3e05f1f commit 8f3cebd
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 28 deletions.
27 changes: 26 additions & 1 deletion services/mankkoo/mankkoo/account/account_db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from apiflask import Schema
from apiflask.fields import String, Boolean, Float, File

import mankkoo.database as db

from mankkoo.base_logger import log
from mankkoo.controller.account_controller import Account, AccountOperation
from mankkoo.account.models import Bank

class Account(Schema):
id = String()
name = String()
number = String()
alias = String()
type = String()
importer = String()
active = Boolean()
bankName = String()
bankUrl = String()
hidden = Boolean()
openedAt = String()

def load_all_accounts() -> list[Account]:
log.info("Loading all accounts...")
Expand Down Expand Up @@ -104,6 +118,17 @@ def get_account_balance(account_id: str) -> float:
return float(balance)


class AccountOperation(Schema):
id = String()
date = String()
title = String()
details = String()
operation = Float()
balance = Float()
currency = String()
comment = String()


def load_operations_for_account(stream_id: str) -> list[AccountOperation]:
query = f"""
SELECT
Expand Down
2 changes: 2 additions & 0 deletions services/mankkoo/mankkoo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from mankkoo.controller.main_controller import main_endpoints
from mankkoo.controller.account_controller import account_endpoints
from mankkoo.controller.stream_controller import stream_endpoints
from mankkoo.base_logger import log
from mankkoo.config import ProdConfig, DevConfig

Expand Down Expand Up @@ -49,6 +50,7 @@ def create_app(app_config=None):

app.register_blueprint(main_endpoints, url_prefix='/api/main')
app.register_blueprint(account_endpoints, url_prefix='/api/accounts')
app.register_blueprint(stream_endpoints, url_prefix='/api/streams')

db.init_db()

Expand Down
28 changes: 2 additions & 26 deletions services/mankkoo/mankkoo/controller/account_controller.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import traceback
from apiflask import APIBlueprint, Schema
from apiflask.fields import String, Boolean, Float, File

from mankkoo.base_logger import log
from mankkoo.account import account_db as database
from mankkoo.account.account_db import Account, AccountOperation
from mankkoo.account import account

account_endpoints = APIBlueprint('account_endpoints', __name__, tag='Account')


class Account(Schema):
id = String()
name = String()
number = String()
alias = String()
type = String()
importer = String()
active = Boolean()
bankName = String()
bankUrl = String()
hidden = Boolean()
openedAt = String()


class AccountOperationResult(Schema):
result = String()
details = String()
Expand All @@ -33,21 +21,9 @@ class AccountOperationResult(Schema):
def accounts():
log.info('Fetching account info...')
accounts = database.load_all_accounts()

return accounts


class AccountOperation(Schema):
id = String()
date = String()
title = String()
details = String()
operation = Float()
balance = Float()
currency = String()
comment = String()


@account_endpoints.route("/<account_id>/operations")
@account_endpoints.output(AccountOperation(many=True), status_code=200)
@account_endpoints.doc(
Expand Down
43 changes: 43 additions & 0 deletions services/mankkoo/mankkoo/controller/stream_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from apiflask import APIBlueprint, Schema
from apiflask.fields import String, Boolean, Integer

from mankkoo.stream import stream_db as database
from mankkoo.stream.stream_db import Stream, StreamDetails
from mankkoo.base_logger import log

stream_endpoints = APIBlueprint('stream_endpoints', __name__, tag='Stream')


class StreamsQuery(Schema):
active = Boolean()
type = String()

@stream_endpoints.route("", methods=['GET'])
@stream_endpoints.input(StreamsQuery, location='query')
@stream_endpoints.output(Stream(many=True), status_code=200)
@stream_endpoints.doc(summary='List of streams', description='Get list of streams')
def streams(query_params):
log.info(f"Fetching all streams. Query: {str(query_params)}...")

active: bool = None
type: str = None
if hasattr(query_params, "active"):
active = query_params["active"]

if hasattr(query_params, "type"):
type = query_params["type"]


streams = database.load_streams(active, type)
return streams



@stream_endpoints.route("/<stream_id>")
@stream_endpoints.output(StreamDetails, status_code=200)
@stream_endpoints.doc(
summary='Stream by id',
description='Get detailed information about the Stream'
)
def stream_by_id(stream_id):
pass
1 change: 0 additions & 1 deletion services/mankkoo/mankkoo/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import psycopg2

from mankkoo.base_logger import log
from mankkoo.account import account_db

log.basicConfig(level=log.DEBUG)

Expand Down
Empty file.
65 changes: 65 additions & 0 deletions services/mankkoo/mankkoo/stream/stream_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from apiflask import Schema
from apiflask.fields import String, Integer

import mankkoo.database as db
from mankkoo.base_logger import log

class Stream(Schema):
id = String()
type = String()
name = String()

def load_streams(active: bool, type: str) -> list[Stream]:
log.info(f"Loading stream... Params: active={active}, type={type}")
conditions = []

# if active is not None:
# conditions.append(f"metadata->>'active' = '{active}'")

# if type is not None:
# conditions.append(f"type = '{type}'")

# where_clause = ""

# if len(conditions) > 0:
# where_clause = f"WHERE {conditions[0]}"

# if len(conditions) > 1:
# and_cond = " AND ".join()

query = f"""
SELECT
id, type,
CASE
WHEN type = 'account' THEN CONCAT(metadata->>'bankName', ' - ', metadata->>'alias')
WHEN type = 'investment' THEN metadata->>'investmentName'
WHEN type = 'retirement' THEN metadata->>'alias'
WHEN type = 'stocks' AND metadata->>'type' = 'ETF' THEN metadata->>'etfName'
ELSE 'Unknown'
END AS name
FROM
streams
;
"""

result = []
with db.get_connection() as conn:
with conn.cursor() as cur:
cur.execute(query)
rows = cur.fetchall()

for row in rows:
stream = Stream()
stream.id = row[0]
stream.type = row[1]
stream.name = row[2]

result.append(stream)
return result


class StreamDetails(Schema):
id = String()
type = String()
version = Integer()

0 comments on commit 8f3cebd

Please sign in to comment.