|
1 | | -from io import BytesIO |
2 | | -import json |
3 | | -from os import path |
4 | | -import subprocess |
5 | | -import tempfile |
6 | | -import zipfile |
7 | | - |
8 | | -from testsuite.validate_sch import validate_schematron |
9 | | - |
10 | | -from flask import Flask, jsonify, send_file, request |
11 | | - |
12 | | - |
13 | | -app = Flask(__name__) |
14 | | -R_SCRIPT_PATH = "/usr/src/app/bsyncr_server/lib/bsyncRunner.r" |
15 | | -SCHEMATRON_FILE_PATH = "/usr/src/schematron/bsyncr_schematron.sch" |
16 | | -INPUT_FILE_PATH = "/tmp/input.xml" |
17 | | -OUTPUT_FILENAMES = ["result.xml", "plot.png"] |
18 | | -ERROR_FILENAME = "error.json" |
19 | | -MODEL_CHOICES = ["SLR", "3PC", "3PH", "4P"] |
20 | | - |
21 | | - |
22 | | -def json_error(status_code, detail): |
23 | | - return {"errors": [{"status": str(status_code), "detail": detail}]}, status_code |
24 | | - |
25 | | - |
26 | | -@app.route("/", methods=["POST"]) |
27 | | -def root(): |
28 | | - if "file" not in request.files: |
29 | | - return json_error(400, "No file in request") |
30 | | - uploaded_file = request.files["file"] |
31 | | - |
32 | | - uploaded_file.save(INPUT_FILE_PATH) |
33 | | - try: |
34 | | - errors = validate_schematron(SCHEMATRON_FILE_PATH, INPUT_FILE_PATH) |
35 | | - except Exception as e: |
36 | | - print(e) |
37 | | - return json_error(400, "Failed to run schematron. Is your XML well-formed?") |
38 | | - |
39 | | - if errors: |
40 | | - |
41 | | - def format_failure(failure): |
42 | | - return f"line {failure.line}: element {failure.element}: {failure.message}" |
43 | | - |
44 | | - json_errors = [ |
45 | | - {"detail": format_failure(error), "status": "400"} for error in errors |
46 | | - ] |
47 | | - return {"errors": json_errors}, 400 |
48 | | - |
49 | | - model_type = request.args.get("model_type") |
50 | | - if model_type is None: |
51 | | - return json_error( |
52 | | - 400, |
53 | | - f"Invalid value for `model_type` query parameter. " |
54 | | - f"Must provide one of the following: {', '.join(MODEL_CHOICES)}", |
55 | | - ) |
56 | | - |
57 | | - with tempfile.TemporaryDirectory() as tmpdirname: |
58 | | - completed_process = subprocess.run( |
59 | | - ["Rscript", R_SCRIPT_PATH, INPUT_FILE_PATH, model_type, tmpdirname], |
60 | | - ) |
61 | | - |
62 | | - if completed_process.returncode != 0: |
63 | | - error_filepath = f"{tmpdirname}/{ERROR_FILENAME}" |
64 | | - if not path.exists(error_filepath): |
65 | | - raise Exception(f'Expected to find error json at "{error_filepath}"') |
66 | | - with open(error_filepath, "r") as f: |
67 | | - r_error = json.load(f) |
68 | | - return json_error( |
69 | | - 500, f"Unexpected error from bsyncr script: {r_error['message']}" |
70 | | - ) |
71 | | - |
72 | | - zip_file = BytesIO() |
73 | | - with zipfile.ZipFile(zip_file, "w") as zf: |
74 | | - for filename in OUTPUT_FILENAMES: |
75 | | - zf.write(f"{tmpdirname}/{filename}", arcname=filename) |
76 | | - |
77 | | - zip_file.seek(0) |
78 | | - return send_file(zip_file, attachment_filename="bsyncr.zip") |
79 | | - |
80 | | - |
81 | | -@app.route("/health", methods=["GET"]) |
82 | | -def health_check(): |
83 | | - return jsonify({"status": "healthy"}), 200 |
0 commit comments