示例#1
0
    # 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.
示例#2
0
文件: graph.py 项目: bralicea/Trades
"""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')
示例#3
0
"""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):
示例#4
0
 def bp(self):
     return web.Blueprint("test")
示例#5
0
"""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)))
示例#6
0
 def bp(self):
     return web.Blueprint('test')
示例#7
0
"""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,
示例#8
0
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.
示例#9
0
文件: tables.py 项目: zcsh/faust
"""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."""
示例#10
0
文件: tables.py 项目: taybin/faust
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."""
示例#11
0
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):
示例#12
0
"""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'})
示例#13
0
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):
示例#14
0
"""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"})
示例#15
0
文件: stats.py 项目: bralicea/Trades
"""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:
示例#16
0
文件: graph.py 项目: taybin/faust
"""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())