Skip to content

Commit

Permalink
add inject framework, refactor rpc and services
Browse files Browse the repository at this point in the history
  • Loading branch information
dzakharchuk committed Jan 4, 2024
1 parent aeec040 commit 7b4c3bf
Show file tree
Hide file tree
Showing 29 changed files with 795 additions and 702 deletions.
1 change: 1 addition & 0 deletions configs/local.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
debug: true
secret: 023517f06f444118986877e636b4a226
test_environ: true

app:
port: 8000
Expand Down
2 changes: 1 addition & 1 deletion configs/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
debug: true
secret: 023517f06f444118986877e636b4a226
test_environ: false
test_environ: true

app:
port: 8000
Expand Down
47 changes: 47 additions & 0 deletions featureflags/container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import Literal

from dependency_injector import containers, providers
from hiku.engine import Engine
from hiku.executors.asyncio import AsyncIOExecutor

from featureflags.config import config
from featureflags.services.db import init_db_engine
from featureflags.services.ldap import LDAP, DummyLDAP


def select_main_or_testing_dependency() -> Literal["main", "testing"]:
return "testing" if config.test_environ else "main"


class Container(containers.DeclarativeContainer):
"""
Container with app dependencies.
"""

wiring_config = containers.WiringConfiguration(
packages=[
"featureflags.services",
"featureflags.web",
"featureflags.rpc",
]
)

graph_engine = providers.Factory(
Engine,
providers.Callable(AsyncIOExecutor),
)

db_engine = providers.Resource(init_db_engine)

ldap_service = providers.Selector(
selector=select_main_or_testing_dependency,
main=providers.Factory(
LDAP,
host=config.ldap.host,
base_dn=config.ldap.base_dn,
),
testing=providers.Factory(
DummyLDAP,
bound=True,
),
)
132 changes: 0 additions & 132 deletions featureflags/feedback.py

This file was deleted.

18 changes: 9 additions & 9 deletions featureflags/actions.py → featureflags/graph/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from sqlalchemy.dialects.postgresql import insert

from featureflags import metrics
from featureflags.utils import sel_scalar
from featureflags.schema import Operator, Project, Variable, AuthSession, AuthUser
from featureflags.schema import Flag, Check, LocalIdMap, Condition, Action, Changelog
from featureflags.utils import select_scalar
from featureflags.models import Operator, Project, Variable, AuthSession, AuthUser
from featureflags.models import Flag, Check, LocalIdMap, Condition, Action, Changelog


action_time = Histogram(
Expand Down Expand Up @@ -109,7 +109,7 @@ def __hash__(self):
async def gen_id(local_id: LocalId, *, db: SAConnection):
assert local_id.scope and local_id.value, local_id

id_ = await sel_scalar(
id_ = await select_scalar(
db,
(
insert(LocalIdMap.__table__)
Expand All @@ -126,7 +126,7 @@ async def gen_id(local_id: LocalId, *, db: SAConnection):
),
)
if id_ is None:
id_ = await sel_scalar(
id_ = await select_scalar(
db,
(
select([LocalIdMap.id]).where(
Expand All @@ -142,9 +142,9 @@ async def gen_id(local_id: LocalId, *, db: SAConnection):

async def get_auth_user(username, *, db):
user_id_select = select([AuthUser.id]).where(AuthUser.username == username)
user_id = await sel_scalar(db, user_id_select)
user_id = await select_scalar(db, user_id_select)
if user_id is None:
user_id = await sel_scalar(
user_id = await select_scalar(
db,
(
insert(AuthUser.__table__)
Expand All @@ -159,7 +159,7 @@ async def get_auth_user(username, *, db):
),
)
if user_id is None:
user_id = await sel_scalar(db, user_id_select)
user_id = await select_scalar(db, user_id_select)
assert user_id is not None
return user_id

Expand Down Expand Up @@ -377,7 +377,7 @@ async def disable_condition(condition_id, *, db, dirty, changes):
assert condition_id, "Condition id is required"

condition_id = UUID(hex=condition_id)
flag_id = await sel_scalar(
flag_id = await select_scalar(
db,
(
Condition.__table__.delete()
Expand Down
20 changes: 10 additions & 10 deletions featureflags/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@
from hiku.telemetry.prometheus import AsyncGraphMetrics

from featureflags import metrics
from featureflags import actions
from featureflags.actions import (
from featureflags.graph import actions
from featureflags.graph.actions import (
AddCheckOp,
AddConditionOp,
postprocess,
update_changelog,
)
from featureflags.utils import (
exec_expr,
exec_expression,
exec_scalar,
)
from featureflags.schema import (
from featureflags.models import (
Project,
Variable,
Flag,
Condition,
Check,
Changelog,
)
from featureflags.schema import AuthUser
from featureflags.models import AuthUser

graph_pull_time = Histogram(
"graph_pull_time",
Expand Down Expand Up @@ -129,7 +129,7 @@ async def root_flags(ctx, options):
)
)
)
return await exec_expr(ctx[SA_ENGINE], expr)
return await exec_expression(ctx[SA_ENGINE], expr)


@pass_context
Expand All @@ -140,7 +140,7 @@ async def root_flags_by_ids(ctx, options):
if len(ids) == 0:
return []
else:
return await exec_expr(
return await exec_expression(
ctx[SA_ENGINE], select([Flag.id]).where(Flag.id.in_(ids))
)

Expand All @@ -150,7 +150,7 @@ async def root_projects(ctx):
if not ctx[SESSION].is_authenticated:
return []
else:
return await exec_expr(ctx[SA_ENGINE], select([Project.id]))
return await exec_expression(ctx[SA_ENGINE], select([Project.id]))


@pass_context
Expand All @@ -166,7 +166,7 @@ async def root_changes(ctx, options):
Flag.__table__, Changelog.flag == Flag.id
)
sel = sel.select_from(join).where(Flag.project.in_(project_ids))
return await exec_expr(
return await exec_expression(
ctx[SA_ENGINE], sel.order_by(Changelog.timestamp.desc())
)

Expand Down Expand Up @@ -718,7 +718,7 @@ async def delete_flag(ctx, options):

@metrics.wrap(graph_pull_time.time())
@metrics.wrap(graph_pull_errors.count_exceptions())
async def pull(engine, query, *, sa, session):
async def exec_graph(engine, query, *, sa, session):
return await engine.execute(
GRAPH,
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def value_set(self, obj, value):
}


def populate(result, result_proto):
def populate_result_proto(result, result_proto):
binding = BINDINGS["Root"]
for name, value in result.__idx__.root.items():
getattr(binding, name.partition("[")[0])(result_proto.Root, value)
Expand Down
Loading

0 comments on commit 7b4c3bf

Please sign in to comment.