# fake orders are not executed. fake: bool = False app = faust.App( "orders", cache="redis://*****:*****@orders.route("/init/{side}/", name="init") class OrderView(web.View): # First clients do a GET on /order/init/sell/ # This endpoint will then do a POST to /order/create/ async def get(self, request: web.Request, side: str) -> web.Response: order_id = uuid() user_id = uuid() side = side.lower() assert side in Order.VALID_SIDES # This will trigger our test_order case with 50% probability.
"""Web endpoint showing graph of running :pypi:`mode` services.""" import io from faust import web __all__ = ['Graph', 'blueprint'] blueprint = web.Blueprint('graph') @blueprint.route('/', name='detail') class Graph(web.View): """Render image from graph of running services.""" async def get(self, request: web.Request) -> web.Response: """Draw image of the services running in this worker.""" try: import pydot except ImportError: return self.text('Please install pydot: pip install pydot', status=500) o = io.StringIO() beacon = self.app.beacon.root or self.app.beacon beacon.as_graph().to_dot(o) graph, = pydot.graph_from_dot_data(o.getvalue()) return self.bytes(graph.create_png(), content_type='image/png')
"""HTTP endpoint showing statistics from the Faust monitor.""" from collections import defaultdict from typing import List, MutableMapping, Set from faust import web from faust.types.tuples import TP __all__ = ["Assignment", "Stats", "blueprint"] TPMap = MutableMapping[str, List[int]] blueprint = web.Blueprint("monitor") @blueprint.route("/", name="index") class Stats(web.View): """Monitor statistics.""" async def get(self, request: web.Request) -> web.Response: """Return JSON response with sensor information.""" return self.json( {f"Sensor{i}": s.asdict() for i, s in enumerate(self.app.sensors)}) @blueprint.route("/assignment/", name="assignment") class Assignment(web.View): """Cluster assignment information.""" @classmethod def _topic_grouped(cls, assignment: Set[TP]) -> TPMap: tps: MutableMapping[str, List[int]] = defaultdict(list) for tp in sorted(assignment):
def bp(self): return web.Blueprint("test")
"""HTTP endpoint showing partition routing destinations.""" from faust import web __all__ = ['TableList', 'TableDetail', 'TableKeyDetail', 'blueprint'] blueprint = web.Blueprint('router') @blueprint.route('/', name='list') class TableList(web.View): """List routes for all tables.""" async def get(self, request: web.Request) -> web.Response: router = self.app.router return self.json(router.tables_metadata()) @blueprint.route('/{name}/', name='detail') class TableDetail(web.View): """List route for specific table.""" async def get(self, request: web.Request, name: str) -> web.Response: router = self.app.router return self.json(router.table_metadata(name)) @blueprint.route('/{name}/{key}/', name='key-detail') class TableKeyDetail(web.View): """List information about key.""" async def get(self, request: web.Request, name: str, key: str) -> web.Response: router = self.app.router return self.json(str(router.key_store(name, key)))
def bp(self): return web.Blueprint('test')
"""HTTP endpoint showing partition routing destinations.""" from faust import web from faust.web.exceptions import ServiceUnavailable __all__ = ["TableList", "TableDetail", "TableKeyDetail", "blueprint"] blueprint = web.Blueprint("router") @blueprint.route("/", name="list") class TableList(web.View): """List routes for all tables.""" async def get(self, request: web.Request) -> web.Response: """Return JSON response with list of all table routes.""" router = self.app.router return self.json(router.tables_metadata()) @blueprint.route("/{name}/", name="detail") class TableDetail(web.View): """List route for specific table.""" async def get(self, request: web.Request, name: str) -> web.Response: """Return JSON response with table metadata.""" router = self.app.router return self.json(router.table_metadata(name)) @blueprint.route("/{name}/{key}/", name="key-detail") class TableKeyDetail(web.View): """List information about key.""" async def get(self, request: web.Request, name: str,
from faust import web from faust import uuid from ..app import app, livecheck from .livechecks import test_order from .models import Order from .topics import orders_topic blueprint = web.Blueprint('orders') SIDE_SELL = 'sell' SIDE_BUY = 'buy' VALID_SIDES = {SIDE_SELL, SIDE_BUY} @blueprint.route('/init/{side}/', name='init') class OrderView(web.View): # First clients do a GET on /order/init/sell/ # This endpoint will then do a POST to /order/create/ async def get(self, request: web.Request, side: str) -> web.Response: order_id = uuid() user_id = uuid() side = side.lower() assert side in VALID_SIDES # This will trigger our test_order case with 50% probability. # If executed we pass along LiveCheck-Test-* related headers # that can track the test as it progresses through the system. # All intermediate systems must pass along these headers, # be it through HTTP or Kafka.
"""HTTP endpoint showing partition routing destinations.""" from typing import Any, Mapping, cast from faust import web from faust.app.router import SameNode from faust.models import Record from faust.types import K, TableT, V __all__ = [ 'TableView', 'TableList', 'TableDetail', 'TableKeyDetail', 'blueprint', ] blueprint = web.Blueprint('tables') class TableInfo(Record, serializer='json', namespace='@TableInfo'): name: str help: str class TableView(web.View): """Base class for table related views.""" def table_json(self, table: TableT, **kwargs: Any) -> Mapping: """Return table info as JSON serializable object.""" return TableInfo(table.name, table.help).asdict() def get_table_or_404(self, name: str) -> TableT: """Find table by name, or raise NotFound if not found."""
from typing import Any, Mapping, cast from faust import web from faust.app.router import SameNode from faust.models import Record from faust.types import K, TableT, V __all__ = [ "TableView", "TableList", "TableDetail", "TableKeyDetail", "blueprint", ] blueprint = web.Blueprint("tables") class TableInfo(Record, serializer="json", namespace="@TableInfo"): name: str help: str class TableView(web.View): """Base class for table related views.""" def table_json(self, table: TableT, **kwargs: Any) -> Mapping: """Return table info as JSON serializable object.""" return TableInfo(table.name, table.help).asdict() def get_table_or_404(self, name: str) -> TableT: """Find table by name, or raise NotFound if not found."""
import faust import pytest from faust import web blueprint = web.Blueprint('test_views') class XModel(faust.Record): aint: int astr: str @blueprint.route('/takes/') class TakeView(web.View): @web.takes_model(XModel) @web.gives_model(XModel) async def post(self, request, obj): return obj @blueprint.route('/gives/') class GivesView(web.View): @web.gives_model(XModel) async def get(self, request): return XModel(11, 'world') @pytest.fixture() def inject_blueprint(app):
"""HTTP endpoint showing statistics from the Faust monitor.""" from typing import List, MutableMapping from faust import web __all__ = ['Index', 'blueprint'] TPMap = MutableMapping[str, List[int]] blueprint = web.Blueprint('production_index') @blueprint.route('/', name='index') class Index(web.View): """Dummy Index.""" async def get(self, request: web.Request) -> web.Response: """Simple status page.""" return self.json({'status': 'OK'})
import pytest import faust from faust import web blueprint = web.Blueprint("test_views") class XModel(faust.Record): aint: int astr: str @blueprint.route("/takes/") class TakeView(web.View): @web.takes_model(XModel) @web.gives_model(XModel) async def post(self, request, obj): return obj @blueprint.route("/gives/") class GivesView(web.View): @web.gives_model(XModel) async def get(self, request): return XModel(11, "world") @blueprint.route("/options/") class OptionsView(web.View): async def options(self, request):
"""HTTP endpoint showing statistics from the Faust monitor.""" from typing import List, MutableMapping from faust import web __all__ = ["Index", "blueprint"] TPMap = MutableMapping[str, List[int]] blueprint = web.Blueprint("production_index") @blueprint.route("/", name="index") class Index(web.View): """Dummy Index.""" async def get(self, request: web.Request) -> web.Response: """Simple status page.""" return self.json({"status": "OK"})
"""HTTP endpoint showing statistics from the Faust monitor.""" from collections import defaultdict from typing import List, MutableMapping, Set from faust import web from faust.types.tuples import TP __all__ = ['Assignment', 'Stats', 'blueprint'] TPMap = MutableMapping[str, List[int]] blueprint = web.Blueprint('monitor') @blueprint.route('/', name='index') class Stats(web.View): """Monitor statistics.""" async def get(self, request: web.Request) -> web.Response: """Return JSON response with sensor information.""" return self.json( {f'Sensor{i}': s.asdict() for i, s in enumerate(self.app.sensors)}) @blueprint.route('/assignment/', name='assignment') class Assignment(web.View): """Cluster assignment information.""" @classmethod def _topic_grouped(cls, assignment: Set[TP]) -> TPMap:
"""Web endpoint showing graph of running :pypi:`mode` services.""" import io from faust import web __all__ = ["Graph", "blueprint"] blueprint = web.Blueprint("graph") @blueprint.route("/", name="detail") class Graph(web.View): """ --- description: Render image from graph of running services. tags: - Faust produces: - application/json """ async def get(self, request: web.Request) -> web.Response: """Draw image of the services running in this worker.""" try: import pydot except ImportError: return self.text("Please install pydot: pip install pydot", status=500) o = io.StringIO() beacon = self.app.beacon.root or self.app.beacon beacon.as_graph().to_dot(o) (graph,) = pydot.graph_from_dot_data(o.getvalue())