Exemple #1
0
async def test_query_client(app, client, mocked_influx):
    query_client = influx.get_client(app)
    retval = dict(key='val')
    mocked_influx.return_value.query.return_value = retval

    data = await query_client.query(database='db', query='gimme')

    assert data == retval
async def _do_with_handler(func: Awaitable, request: web.Request, default_args=...) -> web.Response:
    try:
        args = await request.json()
    except JSONDecodeError:
        if default_args is ...:
            raise
        args = default_args
    response = await func(influx.get_client(request.app), **args)
    return web.json_response(response)
Exemple #3
0
async def subscribe_values(request: web.Request) -> web.Response:
    """
    ---
    tags:
    - History
    summary: subscribe to InfluxDB updates
    operationId: history.sse.values
    produces:
    - application/json
    parameters:
    -
        in: query
        name: database
        schema:
            type: string
            required: false
            example: "brewblox"
    -
        in: query
        name: measurement
        schema:
            type: string
            required: true
            example: "spark"
    -
        in: query
        name: fields
        schema:
            type: list
            required: false
            example: ["*"]
    -
        in: query
        name: approx_points
        schema:
            type: int
            required: false
            example: 100
    -
        in: query
        name: start
        schema:
            type: string
            required: false
    -
        in: query
        name: duration
        schema:
            type: string
            required: false
    -
        in: query
        name: end
        schema:
            type: string
            required: false
    """
    client = influx.get_client(request.app)
    params = {
        k: request.query.get(k)
        for k in [
            'database',
            'measurement',
            'approx_points',
            'start',
            'duration',
            'end',
        ] if k in request.query
    }
    if 'fields' in request.query:
        params['fields'] = request.query.getall('fields')

    params = await queries.configure_params(client, **params)
    open_ended = _check_open_ended(params)
    alert: ShutdownAlert = features.get(request.app, ShutdownAlert)
    poll_interval = request.app['config']['poll_interval']

    def check_shutdown():
        if alert.shutdown_signal.is_set():
            raise asyncio.CancelledError()

    async with sse_response(request, headers=_cors_headers(request)) as resp:
        while True:
            try:
                check_shutdown()
                query = queries.build_query(params)
                data = await queries.run_query(client, query, params)

                if data.get('values'):
                    await resp.send(json.dumps(data))
                    # Reset time frame for subsequent updates
                    params['start'] = data['values'][-1][0] + 1
                    params.pop('duration', None)

                if not open_ended:
                    break

                check_shutdown()
                await asyncio.sleep(poll_interval)

            except asyncio.CancelledError:
                return resp

            except Exception as ex:
                msg = f'Exiting values SSE with error: {strex(ex)}'
                LOGGER.error(msg)
                break

    return resp
Exemple #4
0
async def subscribe_last_values(request: web.Request) -> web.Response:
    """
    ---
    tags:
    - History
    summary: Subscribe to updates of latest value in each field.
    operationId: history.sse.last_values
    produces:
    - application/json
    parameters:
    -
        in: query
        name: database
        schema:
            type: string
            required: false
            example: "brewblox"
    -
        in: query
        name: measurement
        schema:
            type: string
            required: true
            example: "sparkey"
    -
        in: query
        name: fields
        schema:
            type: list
            required: true
            example: ["actuator-1/value"]
    -
        in: query
        name: duration
        schema:
            type: string
            required: false
    """
    client = influx.get_client(request.app)
    params = {
        k: request.query.get(k)
        for k in [
            'database',
            'measurement',
            'duration',
        ]
    }
    params['fields'] = request.query.getall('fields')
    alert: ShutdownAlert = features.get(request.app, ShutdownAlert)
    poll_interval = request.app['config']['poll_interval']

    def check_shutdown():
        if alert.shutdown_signal.is_set():
            raise asyncio.CancelledError()

    async with sse_response(request, headers=_cors_headers(request)) as resp:
        while True:
            try:
                check_shutdown()
                data = await queries.select_last_values(client, **params)
                await resp.send(json.dumps(data))

                check_shutdown()
                await asyncio.sleep(poll_interval)

            except asyncio.CancelledError:
                return resp

            except Exception as ex:
                msg = f'Exiting last_values SSE with error: {strex(ex)}'
                LOGGER.error(msg)
                break

    return resp
Exemple #5
0
async def test_setup(app, client):
    assert influx.get_writer(app)
    assert influx.get_relay(app)
    assert influx.get_client(app)