Commit ad4b3d61 authored by Yohan Boniface's avatar Yohan Boniface

Naive "simulate" explorer view

parent 451a17f5
......@@ -53,8 +53,7 @@ def given_set_false(context, key):
@when('je demande un calcul de financement')
async def when_simulate(context):
context.passed = [f for f in await simulate(**
if f.get('eligible')]
context.passed = [f for f in await simulate( if f['eligible']]
@then(r"il y a (?P<expected>\d+) financements? proposés?")
import os
import sys
from io import StringIO
import urllib.request
......@@ -137,13 +140,34 @@ def ssh_keys():
def access_logs():
"""See the nginx access logs."""
run('tail -F /var/log/nginx/access.log')
with sudo():
run('tail -F /var/log/nginx/access.log')
def error_logs():
"""See the nginx error logs."""
run('tail -F /var/log/nginx/error.log')
with sudo():
run('tail -F /var/log/nginx/error.log')
def upload_env():
"""Upload environment vars to the server.
Use those to deal with info not commitable.
keys = ['LBF_CHARMAP']
content = ''
for key in keys:
content += '{}={}\n'.format(key, os.environ[key])
except KeyError as e:
sys.exit('The {} environment variable does not exist.'.format(e))
path = '/srv/trefle/env'
if exists(path):
run(f'cat {path}')
put(StringIO(content), path)
......@@ -7,6 +7,7 @@ Type=simple
ExecStart=/srv/trefle/venv/bin/gunicorn trefle.api:app --config gunicorn.conf
import logging
import pkg_resources
from http import HTTPStatus
from pathlib import Path
from roll import Roll, HttpError
from roll import HttpError, Roll
from roll.extensions import cors
from .config import RAW_RULES, SCHEMA
from .core import simulate
from .debugging import data_from_lbf_url, make_feature
from .openapis import OPENAPI
from .config import SCHEMA, RAW_RULES
logger = logging.getLogger('trefle')
......@@ -39,8 +39,9 @@ async def json_error_response(request, response, error):
@app.route('/financement', methods=['POST'])
async def simulate_(request, response):
context = request.json
financements = await simulate(**request.json)
financements = await simulate(context)
except ValueError as err:
raise HttpError(HTTPStatus.UNPROCESSABLE_ENTITY, err.args[0])
......@@ -49,7 +50,15 @@ async def simulate_(request, response):
financements = [f for f in financements if f['eligible'] == eligible]
for tag in request.query.list('tags', []):
financements = [f for f in financements if tag in f['tags']]
response.json = {'financements': financements}
body = {'financements': financements}
del context['financements']
if request.query.bool('context', False):
body['context'] = {k: {'label': SCHEMA[k]['label'], 'value': v}
for k, v in context.items()
if k in SCHEMA and 'label' in SCHEMA[k]}
if request.query.bool('scenario', False):
body['scenario'] = make_feature(context, financements)
response.json = body
......@@ -65,3 +74,8 @@ async def explore_schema(request, response):
async def explore_rules(request, response):
response.json = RAW_RULES
async def decode_lbf_url(request, response):
response.json = data_from_lbf_url(request.query.get('url'))
......@@ -3,7 +3,7 @@ from pathlib import Path
import ujson as json
from minicli import cli, run
from roll.extensions import simple_server, static
from roll.extensions import simple_server, static, traceback
from .api import app
......@@ -42,7 +42,7 @@ async def cli_simulate(*args, data: json.loads={}, url=None, trace=False,
if url:
data = await data_from_lbf_url(url)
data = data_from_lbf_url(url)
if args:
if 'formation.numero' in data:
......@@ -68,7 +68,7 @@ async def cli_simulate(*args, data: json.loads={}, url=None, trace=False,
# We already processed the formation, prevent to process it twice.
del data['formation.numero']
start = time.perf_counter()
financements = await simulate(**data)
financements = await simulate(data)
duration = (time.perf_counter() - start)
print('*' * 105)
eligibles = [f for f in financements if f['eligible']]
......@@ -131,6 +131,7 @@ def serve():
"""Run a web server (for development only)."""
# Debug only.
static(app, '/explorer/', Path(__file__).parent / 'explorer')