diff options
author | vrvarma <vikas.varma@att.com> | 2020-09-17 02:24:00 -0400 |
---|---|---|
committer | vrvarma <vikas.varma@att.com> | 2020-09-17 04:52:05 -0400 |
commit | a55cbabaef4975d5007363e59d4560cb30a855e3 (patch) | |
tree | c30f83a71fee88744acbfd8bf8447c7ccbf24395 /runtime | |
parent | 2024d8efab152980640b624402b6076da338ef7f (diff) |
Fix osdf code after upgrading to py38
Fix osdf logging to work with 3.8
Fix osdf code that broke after migration
Fix test cases after migration
Fixing pep8 violations
Change-Id: I11ca33959882c8b9010f00ff744d59c7eeb3c2f2
Signed-off-by: vrvarma <vikas.varma@att.com>
Issue-ID: OPTFRA-796
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/model_api.py | 30 | ||||
-rw-r--r-- | runtime/optim_engine.py | 11 | ||||
-rw-r--r-- | runtime/solvers/mzn/mzn_solver.py | 48 |
3 files changed, 63 insertions, 26 deletions
diff --git a/runtime/model_api.py b/runtime/model_api.py index fd87333..b0492f2 100644 --- a/runtime/model_api.py +++ b/runtime/model_api.py @@ -19,12 +19,16 @@ import json import traceback +from flask import Flask +from flask import g +from flask import Response import mysql.connector -from flask import g, Flask, Response from osdf.config.base import osdf_config -from osdf.logging.osdf_logging import debug_log, error_log +from osdf.logging.osdf_logging import debug_log +from osdf.logging.osdf_logging import error_log from osdf.operation.exceptions import BusinessException +from osdf.utils.data_conversion import decode_data def init_db(): @@ -33,20 +37,22 @@ def init_db(): def get_db(): - """Opens a new database connection if there is none yet for the - current application context. + """Opens a new database connection if there is none yet for the current application context. + """ if not hasattr(g, 'pg'): properties = osdf_config['deployment'] - host, db_port, db = properties["osdfDatabaseHost"], properties["osdfDatabasePort"], \ - properties.get("osdfDatabaseSchema") + host, db_port, db = properties["osdfDatabaseHost"], properties["osdfDatabasePort"], properties.get( + "osdfDatabaseSchema") user, password = properties["osdfDatabaseUsername"], properties["osdfDatabasePassword"] g.pg = mysql.connector.connect(host=host, port=db_port, user=user, password=password, database=db) return g.pg def close_db(): - """Closes the database again at the end of the request.""" + """Closes the database again at the end of the request. + + """ if hasattr(g, 'pg'): g.pg.close() @@ -109,7 +115,7 @@ def build_model_dict(resp_data, content_needed=True): resp = {'modelId': resp_data[0], 'description': resp_data[2] if resp_data[2] else '', 'solver': resp_data[3]} if content_needed: - resp.update({'modelContent': resp_data[1]}) + resp.update({'modelContent': decode_data(resp_data[1])}) return resp @@ -124,7 +130,6 @@ def delete_model_data(model_id): with app.app_context(): try: debug_log.debug("deleting model data given model_id = {}".format(model_id)) - d = dict(); connection = get_db() cursor = connection.cursor(buffered=True) query = "delete from optim_model_data WHERE model_id = %s" @@ -146,10 +151,11 @@ def get_model_data(model_id): with app.app_context(): try: debug_log.debug("getting model data given model_id = {}".format(model_id)) - d = dict(); + d = dict() connection = get_db() cursor = connection.cursor(buffered=True) - query = "SELECT model_id, model_content, description, solver_type FROM optim_model_data WHERE model_id = %s" + query = "SELECT model_id, model_content, description, " \ + "solver_type FROM optim_model_data WHERE model_id = %s" values = (model_id,) cursor.execute(query, values) if cursor is None: @@ -194,7 +200,7 @@ def get_all_models(): connection = get_db() cursor = connection.cursor(buffered=True) query = "SELECT model_id, model_content, description, solver_type FROM optim_model_data" - + cursor.execute(query) if cursor is None: return 400, "FAILED" diff --git a/runtime/optim_engine.py b/runtime/optim_engine.py index 4a8788e..b303bbf 100644 --- a/runtime/optim_engine.py +++ b/runtime/optim_engine.py @@ -19,10 +19,11 @@ from flask import Response from osdf.operation.exceptions import BusinessException -from .model_api import get_model_data -from .models.api.optim_request import OptimizationAPI -from .solvers.mzn.mzn_solver import solve as mzn_solve -from .solvers.py.py_solver import solve as py_solve +from osdf.utils.data_conversion import decode_data +from runtime.model_api import get_model_data +from runtime.models.api.optim_request import OptimizationAPI +from runtime.solvers.mzn.mzn_solver import solve as mzn_solve +from runtime.solvers.py.py_solver import solve as py_solve def is_valid_optim_request(request_json): @@ -69,7 +70,7 @@ def get_model_content(request_json): if model_id: status, data = get_model_data(model_id) if status == 200: - model_content = data[1] + model_content = decode_data(data[1]) solver = data[3] else: raise BusinessException('model_id [{}] not found in the model database'.format(model_id)) diff --git a/runtime/solvers/mzn/mzn_solver.py b/runtime/solvers/mzn/mzn_solver.py index cf002e7..f3daa2b 100644 --- a/runtime/solvers/mzn/mzn_solver.py +++ b/runtime/solvers/mzn/mzn_solver.py @@ -16,10 +16,15 @@ # ------------------------------------------------------------------------- # -import json from datetime import datetime +import json -from pymzn import Status, minizinc, cbc, gecode, chuffed, or_tools +from pymzn import cbc +from pymzn import chuffed +from pymzn import gecode +from pymzn import minizinc +from pymzn import or_tools +from pymzn import Status from osdf.utils.file_utils import delete_file_folder @@ -47,6 +52,10 @@ def map_status(status): def solve(request_json, mzn_content): + """Given the request and minizinc content. Translates the json request to the format minizinc understands + + return: returns the optimized solution. + """ req_info = request_json['requestInfo'] opt_info = request_json['optimInfo'] try: @@ -71,6 +80,9 @@ def solve(request_json, mzn_content): def mzn_solver(mzn_content, opt_info): + """Calls the minizinc optimizer. + + """ args = opt_info['solverArgs'] solver = get_mzn_solver(args.pop('solver')) mzn_opts = dict() @@ -85,18 +97,36 @@ def mzn_solver(mzn_content, opt_info): def persist_opt_data(opt_info): + """Persist the opt data, if included as part of the request. + + return: file_name path of the optim_data + returns None if no optData is part of the request + """ + file_name = None + if 'optData' in opt_info: + if opt_info['optData'].get('json'): + data_content = json.dumps(opt_info['optData']['json']) + file_name = '/tmp/optim_engine_{}.json'.format(datetime.timestamp(datetime.now())) + persist_data(data_content, file_name) + elif opt_info['optData'].get('text'): + data_content = opt_info['optData']['text'] + file_name = '/tmp/optim_engine_{}.dzn'.format(datetime.timestamp(datetime.now())) + persist_data(data_content, file_name) + return file_name + - if opt_info['optData'].get('json'): - data_content = json.dumps(opt_info['optData']['json']) - file_name = '/tmp/optim_engine_{}.json'.format(datetime.timestamp(datetime.now())) - elif opt_info['optData'].get('text'): - data_content = opt_info['optData']['text'] - file_name = '/tmp/optim_engine_{}.dzn'.format(datetime.timestamp(datetime.now())) +def persist_data(data_content, file_name): + """Save the dzn data into a file + """ with open(file_name, "wt") as data: data.write(data_content) - return file_name def get_mzn_solver(solver): + """Returns a solver type object for minizinc optimizers + + solver: solver that is part of the request + return: solver mapped object + """ return solver_dict.get(solver) |