diff --git a/.gitignore b/.gitignore index 1943617..edae3d6 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ wheels/ # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec +instance # Installer logs pip-log.txt @@ -243,4 +244,6 @@ secrets/ *.service temp/ -.env \ No newline at end of file +.env + +cba_server/run_sections* diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..945c9b4 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/cba_server/__init__.py b/cba_server/__init__.py new file mode 100644 index 0000000..871b067 --- /dev/null +++ b/cba_server/__init__.py @@ -0,0 +1,31 @@ +import os +from flask import Flask +import cba_server.db as db +import cba_server.api as api + + +def create_app(test_config=None): + # create and configure the app + app = Flask(__name__, instance_relative_config=True) + app.config.from_mapping( + SECRET_KEY="dev", + DATABASE=os.path.join(app.instance_path, "flaskr.sqlite"), + ) + + if test_config is None: + # load the instance config, if it exists, when not testing + app.config.from_pyfile("config.py", silent=True) + else: + # load the test config if passed in + app.config.from_mapping(test_config) + + # ensure the instance folder exists + try: + os.makedirs(app.instance_path) + except OSError: + pass + + db.init_app(app) + app.register_blueprint(api.bp) + + return app diff --git a/cba_server/api.py b/cba_server/api.py new file mode 100644 index 0000000..b2e3e3d --- /dev/null +++ b/cba_server/api.py @@ -0,0 +1,71 @@ +from flask import ( + Blueprint, + flash, + g, + redirect, + render_template, + request, + url_for, + jsonify, +) +from werkzeug.exceptions import abort +import pandas as pd +import json +import sys +import math + +from cba_server.db import get_db +from cba_server.request_logging import log_request, log_response +from roads_cba_py.section import Section, InvalidSection, parse_section +from roads_cba_py.cba import CostBenefitAnalysisModel +from roads_cba_py.utils import flatten, split_on_condition +from roads_cba_py.config import Config + + +bp = Blueprint("api", __name__) + + +@bp.route("/run_sections", methods=["POST"]) +def run_sections(): + request_id = log_request(request) + config = Config.parse_obj(request.json["config"]) + assets = request.json["assets"] + valid_sections, invalid_sections, stats = split_assets_by_validity(assets) + + cba = CostBenefitAnalysisModel(config) + results = [cba.compute_cba_for_section(s) for s in valid_sections] + results = sorted(results, key=lambda x: (x.work_year, -x.npv_cost)) + + problems = flatten([s.invalid_reason() for s in invalid_sections]) + invalid_reasons = pd.DataFrame(data=problems, columns=["reason"]) + invalid_reasons = invalid_reasons["reason"].value_counts().to_dict() + + response = jsonify( + { + "stats": stats, + "invalids": invalid_reasons, + "data": [s.dict() for s in results], + } + ) + log_response(response, request_id) + + return response + + +@bp.route("/evaluate_assets", methods=["POST"]) +def evaluate_assets(): + _valid_sections, invalid_sections, stats = split_assets_by_validity(request.json) + + problems = flatten([s.invalid_reason() for s in invalid_sections]) + invalid_reasons = pd.DataFrame(data=problems, columns=["reason"]) + invalid_reasons = invalid_reasons["reason"].value_counts().to_dict() + invalid_way_ids = [int(s.original_data["orma_way_id"]) for s in invalid_sections] + return jsonify({"stats": stats, "invalids": invalid_reasons, "invalid_way_ids": invalid_way_ids}) + + +def split_assets_by_validity(data): + sections = [parse_section(s) for s in data] + valid_sections, invalid_sections = split_on_condition(sections, lambda s: s.invalid_reason() is None) + + stats = {"valid": len(valid_sections), "invalid": len(invalid_sections)} + return (valid_sections, invalid_sections, stats) diff --git a/cba_server/db.py b/cba_server/db.py new file mode 100644 index 0000000..885e1f3 --- /dev/null +++ b/cba_server/db.py @@ -0,0 +1,42 @@ +import sqlite3 + +import click +from flask import current_app, g +from flask.cli import with_appcontext + + +def get_db(): + if 'db' not in g: + g.db = sqlite3.connect( + current_app.config['DATABASE'], + detect_types=sqlite3.PARSE_DECLTYPES + ) + g.db.row_factory = sqlite3.Row + + return g.db + + +def close_db(e=None): + db = g.pop('db', None) + + if db is not None: + db.close() + +def init_db(): + db = get_db() + + with current_app.open_resource('schema.sql') as f: + db.executescript(f.read().decode('utf8')) + + +@click.command('init-db') +@with_appcontext +def init_db_command(): + """Clear the existing data and create new tables.""" + init_db() + click.echo('Initialized the database.') + + +def init_app(app): + app.teardown_appcontext(close_db) + app.cli.add_command(init_db_command) diff --git a/cba_server/request_logging.py b/cba_server/request_logging.py new file mode 100644 index 0000000..d4b6985 --- /dev/null +++ b/cba_server/request_logging.py @@ -0,0 +1,36 @@ +import os +import json +import datetime +from os.path import join, abspath, dirname + + +def get_log_dir(): + return abspath(join(dirname(__file__), "..", "log")) + + +def get_request_dir(): + remote_dir = join(get_log_dir(), "flask_requests") + if os.path.isdir(remote_dir): + return remote_dir + return None + + +def log_request(request): + request_dir = get_request_dir() + if request_dir is None: + return None + + timestamp = datetime.datetime.now().replace(microsecond=0).isoformat() + filename = join(request_dir, f"run_sections_request_{timestamp}") + with open(filename, "w") as f: + f.write(json.dumps(request.json)) + return timestamp + + +def log_response(response, id): + request_dir = get_request_dir() + if request_dir is None: + return None + filename = join(request_dir, f"run_sections_response_{id}") + with open(filename, "w") as f: + f.write(json.dumps(response.json)) diff --git a/cba_server/schema.sql b/cba_server/schema.sql new file mode 100644 index 0000000..be76d7e --- /dev/null +++ b/cba_server/schema.sql @@ -0,0 +1,17 @@ +DROP TABLE IF EXISTS user; +DROP TABLE IF EXISTS post; + +CREATE TABLE user ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + password TEXT NOT NULL +); + +CREATE TABLE post ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + author_id INTEGER NOT NULL, + created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + title TEXT NOT NULL, + body TEXT NOT NULL, + FOREIGN KEY (author_id) REFERENCES user (id) +); diff --git a/cba_server/wsgi.py b/cba_server/wsgi.py new file mode 100644 index 0000000..df6f430 --- /dev/null +++ b/cba_server/wsgi.py @@ -0,0 +1,9 @@ +import sys + +sys.path.append(".") +from cba_server import create_app + +app = create_app() + +if __name__ == "__main__": + app.run() diff --git a/Pipfile b/old_django_implementation/Pipfile similarity index 100% rename from Pipfile rename to old_django_implementation/Pipfile diff --git a/Pipfile.lock b/old_django_implementation/Pipfile.lock similarity index 100% rename from Pipfile.lock rename to old_django_implementation/Pipfile.lock diff --git a/README.md b/old_django_implementation/README.md similarity index 100% rename from README.md rename to old_django_implementation/README.md diff --git a/cba/administrations/__init__.py b/old_django_implementation/cba/administrations/__init__.py similarity index 100% rename from cba/administrations/__init__.py rename to old_django_implementation/cba/administrations/__init__.py diff --git a/cba/administrations/admin.py b/old_django_implementation/cba/administrations/admin.py similarity index 100% rename from cba/administrations/admin.py rename to old_django_implementation/cba/administrations/admin.py diff --git a/cba/administrations/apps.py b/old_django_implementation/cba/administrations/apps.py similarity index 100% rename from cba/administrations/apps.py rename to old_django_implementation/cba/administrations/apps.py diff --git a/cba/administrations/exceptions.py b/old_django_implementation/cba/administrations/exceptions.py similarity index 100% rename from cba/administrations/exceptions.py rename to old_django_implementation/cba/administrations/exceptions.py diff --git a/cba/administrations/management/__init__.py b/old_django_implementation/cba/administrations/management/__init__.py similarity index 100% rename from cba/administrations/management/__init__.py rename to old_django_implementation/cba/administrations/management/__init__.py diff --git a/cba/administrations/management/commands/__init__.py b/old_django_implementation/cba/administrations/management/commands/__init__.py similarity index 100% rename from cba/administrations/management/commands/__init__.py rename to old_django_implementation/cba/administrations/management/commands/__init__.py diff --git a/cba/administrations/management/commands/import_provinces.py b/old_django_implementation/cba/administrations/management/commands/import_provinces.py similarity index 100% rename from cba/administrations/management/commands/import_provinces.py rename to old_django_implementation/cba/administrations/management/commands/import_provinces.py diff --git a/cba/administrations/managers.py b/old_django_implementation/cba/administrations/managers.py similarity index 100% rename from cba/administrations/managers.py rename to old_django_implementation/cba/administrations/managers.py diff --git a/cba/administrations/migrations/0001_initial.py b/old_django_implementation/cba/administrations/migrations/0001_initial.py similarity index 100% rename from cba/administrations/migrations/0001_initial.py rename to old_django_implementation/cba/administrations/migrations/0001_initial.py diff --git a/cba/administrations/migrations/__init__.py b/old_django_implementation/cba/administrations/migrations/__init__.py similarity index 100% rename from cba/administrations/migrations/__init__.py rename to old_django_implementation/cba/administrations/migrations/__init__.py diff --git a/cba/administrations/models.py b/old_django_implementation/cba/administrations/models.py similarity index 100% rename from cba/administrations/models.py rename to old_django_implementation/cba/administrations/models.py diff --git a/cba/administrations/serializers.py b/old_django_implementation/cba/administrations/serializers.py similarity index 100% rename from cba/administrations/serializers.py rename to old_django_implementation/cba/administrations/serializers.py diff --git a/cba/administrations/tests.py b/old_django_implementation/cba/administrations/tests.py similarity index 100% rename from cba/administrations/tests.py rename to old_django_implementation/cba/administrations/tests.py diff --git a/cba/administrations/urls.py b/old_django_implementation/cba/administrations/urls.py similarity index 100% rename from cba/administrations/urls.py rename to old_django_implementation/cba/administrations/urls.py diff --git a/cba/administrations/utils.py b/old_django_implementation/cba/administrations/utils.py similarity index 100% rename from cba/administrations/utils.py rename to old_django_implementation/cba/administrations/utils.py diff --git a/cba/administrations/views.py b/old_django_implementation/cba/administrations/views.py similarity index 100% rename from cba/administrations/views.py rename to old_django_implementation/cba/administrations/views.py diff --git a/cba/configs/__init__.py b/old_django_implementation/cba/configs/__init__.py similarity index 100% rename from cba/configs/__init__.py rename to old_django_implementation/cba/configs/__init__.py diff --git a/cba/configs/settings/__init__.py b/old_django_implementation/cba/configs/settings/__init__.py similarity index 100% rename from cba/configs/settings/__init__.py rename to old_django_implementation/cba/configs/settings/__init__.py diff --git a/cba/configs/settings/base.py b/old_django_implementation/cba/configs/settings/base.py similarity index 100% rename from cba/configs/settings/base.py rename to old_django_implementation/cba/configs/settings/base.py diff --git a/cba/configs/settings/development.py b/old_django_implementation/cba/configs/settings/development.py similarity index 100% rename from cba/configs/settings/development.py rename to old_django_implementation/cba/configs/settings/development.py diff --git a/cba/configs/urls.py b/old_django_implementation/cba/configs/urls.py similarity index 100% rename from cba/configs/urls.py rename to old_django_implementation/cba/configs/urls.py diff --git a/cba/configs/wsgi.py b/old_django_implementation/cba/configs/wsgi.py similarity index 100% rename from cba/configs/wsgi.py rename to old_django_implementation/cba/configs/wsgi.py diff --git a/cba/core/__init__.py b/old_django_implementation/cba/core/__init__.py similarity index 100% rename from cba/core/__init__.py rename to old_django_implementation/cba/core/__init__.py diff --git a/cba/core/admin.py b/old_django_implementation/cba/core/admin.py similarity index 100% rename from cba/core/admin.py rename to old_django_implementation/cba/core/admin.py diff --git a/cba/core/apps.py b/old_django_implementation/cba/core/apps.py similarity index 100% rename from cba/core/apps.py rename to old_django_implementation/cba/core/apps.py diff --git a/cba/core/cba.py b/old_django_implementation/cba/core/cba.py similarity index 53% rename from cba/core/cba.py rename to old_django_implementation/cba/core/cba.py index 4476fe0..587e1e4 100644 --- a/cba/core/cba.py +++ b/old_django_implementation/cba/core/cba.py @@ -1,15 +1,28 @@ import numpy as np from .default import ( - dDiscount_Rate, dEconomic_Factor, dGrowth, dTrafficLevels, dVehicleFleet, - iSurfaceDefaults, dWidthDefaults, dConditionData, dRoadWorks, - dRecurrent, dRecMult, dWorkEvaluated, dm_coeff, dVOC, dSPEED, dRoadDet, - iri_cc_df, get_cc_from_iri + dDiscount_Rate, + dEconomic_Factor, + dGrowth, + dTrafficLevels, + dVehicleFleet, + iSurfaceDefaults, + dWidthDefaults, + dConditionData, + dRoadWorks, + dRecurrent, + dRecMult, + dWorkEvaluated, + dm_coeff, + dVOC, + dSPEED, + dRoadDet, + iri_cc_df, + get_cc_from_iri, ) class CostBenefitAnalysisModel: - def __init__(self): self.dDiscount_Rate = dDiscount_Rate self.dEconomic_Factor = dEconomic_Factor @@ -83,15 +96,15 @@ def compute_cba_for_section(self, section): ######################## # Output variables ######################## - sRoadCode = np.empty((13, 20), dtype=' 0: - iCondLanes[ia, iy] =self.dRoadWorks[iTheWork - 1, 7] - dCondWidth[ia, iy] =self.dRoadWorks[iTheWork - 1, 8] - iCondSurface[ia, iy] =self.dRoadWorks[iTheWork - 1, 9] - iYearLanes =self.dRoadWorks[iTheWork - 1, 7] - dYearWidth =self.dRoadWorks[iTheWork, 8] - iYearSurface =self.dRoadWorks[iTheWork, 9] + iCondLanes[ia, iy] = self.dRoadWorks[iTheWork - 1, 7] + dCondWidth[ia, iy] = self.dRoadWorks[iTheWork - 1, 8] + iCondSurface[ia, iy] = self.dRoadWorks[iTheWork - 1, 9] + iYearLanes = self.dRoadWorks[iTheWork - 1, 7] + dYearWidth = self.dRoadWorks[iTheWork, 8] + iYearSurface = self.dRoadWorks[iTheWork, 9] # Srtructural number after periodic maintenance for bituminous roads if self.dRoadWorks[iTheWork - 1, 10] is not None and self.dRoadWorks[iTheWork - 1, 10] > 0: - dCondSNC[ia, iy] = dCondSNC[ia, iy] + self.dRoadWorks[iTheWork - 1, 10] * self.dRoadWorks[iTheWork - 1, 11] * 0.0393701 + dCondSNC[ia, iy] = ( + dCondSNC[ia, iy] + + self.dRoadWorks[iTheWork - 1, 10] * self.dRoadWorks[iTheWork - 1, 11] * 0.0393701 + ) dYearSNC = dCondSNC[ia, iy] # Structural number after rehabiliation for bituminous roads if self.dRoadWorks[iTheWork - 1, 12] is not None and self.dRoadWorks[iTheWork - 1, 12] > 0: - dCondSNC[ia, iy] =self.dRoadWorks[iTheWork - 1, 12] + dCondSNC[ia, iy] = self.dRoadWorks[iTheWork - 1, 12] dYearSNC = dCondSNC[ia, iy] # Capital work costs if iTerrain == 1: - dCostCapitalFin[ia, iy] =self.dRoadWorks[iTheWork - 1, 3] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 * dCostFactor - dCostCapitalEco[ia, iy] =self.dRoadWorks[iTheWork - 1, 3] * dCondLength[ia, iy] * dCondWidth[ia, iy] * self.dEconomic_Factor / 1000.0 * dCostFactor + dCostCapitalFin[ia, iy] = ( + self.dRoadWorks[iTheWork - 1, 3] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + / 1000.0 + * dCostFactor + ) + dCostCapitalEco[ia, iy] = ( + self.dRoadWorks[iTheWork - 1, 3] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + * self.dEconomic_Factor + / 1000.0 + * dCostFactor + ) elif iTerrain == 2: - dCostCapitalFin[ia, iy] =self.dRoadWorks[iTheWork - 1, 4] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 * dCostFactor - dCostCapitalEco[ia, iy] =self.dRoadWorks[iTheWork - 1, 4] * dCondLength[ia, iy] * dCondWidth[ia, iy] * self.dEconomic_Factor / 1000.0 * dCostFactor + dCostCapitalFin[ia, iy] = ( + self.dRoadWorks[iTheWork - 1, 4] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + / 1000.0 + * dCostFactor + ) + dCostCapitalEco[ia, iy] = ( + self.dRoadWorks[iTheWork - 1, 4] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + * self.dEconomic_Factor + / 1000.0 + * dCostFactor + ) elif iTerrain == 3: - dCostCapitalFin[ia, iy] =self.dRoadWorks[iTheWork - 1, 5] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 * dCostFactor - dCostCapitalEco[ia, iy] =self.dRoadWorks[iTheWork - 1, 5] * dCondLength[ia, iy] * dCondWidth[ia, iy] * self.dEconomic_Factor / 1000.0 * dCostFactor - sRoadCode[ia, iy] =self.dRoadWorks[iTheWork - 1, 1] - sSolName[ia] =self.dRoadWorks[iTheWork - 1, 0] - sSolCode[ia] =self.dRoadWorks[iTheWork - 1, 1] - sSolClass[ia] =self.dRoadWorks[iTheWork - 1, 2] - iSolYear[ia] = iy + 1 # Since iy is counted from 0 and year order starts from 1 + dCostCapitalFin[ia, iy] = ( + self.dRoadWorks[iTheWork - 1, 5] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + / 1000.0 + * dCostFactor + ) + dCostCapitalEco[ia, iy] = ( + self.dRoadWorks[iTheWork - 1, 5] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + * self.dEconomic_Factor + / 1000.0 + * dCostFactor + ) + sRoadCode[ia, iy] = self.dRoadWorks[iTheWork - 1, 1] + sSolName[ia] = self.dRoadWorks[iTheWork - 1, 0] + sSolCode[ia] = self.dRoadWorks[iTheWork - 1, 1] + sSolClass[ia] = self.dRoadWorks[iTheWork - 1, 2] + iSolYear[ia] = iy + 1 # Since iy is counted from 0 and year order starts from 1 dSolCost[ia] = dCostCapitalFin[ia, iy] dSolCostkm[ia] = dSolCost[ia] / dCondLength[ia, iy] # repair road work - if (iy == iTheRepairY1 - 1) or (iy == iTheRepairY2 - 1) or (iy == iTheRepairY3 - 1) or (iy == iTheRepairY4 - 1): - sRoadCode[ia, iy] =self.dRoadWorks[iTheRepair - 1, 1] + if ( + (iy == iTheRepairY1 - 1) + or (iy == iTheRepairY2 - 1) + or (iy == iTheRepairY3 - 1) + or (iy == iTheRepairY4 - 1) + ): + sRoadCode[ia, iy] = self.dRoadWorks[iTheRepair - 1, 1] if self.dRoadWorks[iTheRepair - 1, 7] is not None and self.dRoadWorks[iTheRepair - 1, 7] > 0: - iCondLanes[ia, iy] =self.dRoadWorks[iTheRepair - 1, 7] - dCondWidth[ia, iy] =self.dRoadWorks[iTheRepair - 1, 8] - iCondSurface[ia, iy] =self.dRoadWorks[iTheRepair - 1, 9] - iYearLanes =self.dRoadWorks[iTheRepair, 7] - dYearWidth =self.dRoadWorks[iTheRepair, 8] - iYearSurface =self.dRoadWorks[iTheRepair, 9] + iCondLanes[ia, iy] = self.dRoadWorks[iTheRepair - 1, 7] + dCondWidth[ia, iy] = self.dRoadWorks[iTheRepair - 1, 8] + iCondSurface[ia, iy] = self.dRoadWorks[iTheRepair - 1, 9] + iYearLanes = self.dRoadWorks[iTheRepair, 7] + dYearWidth = self.dRoadWorks[iTheRepair, 8] + iYearSurface = self.dRoadWorks[iTheRepair, 9] # structural number if self.dRoadWorks[iTheRepair - 1, 12] is not None and self.dRoadWorks[iTheRepair - 1, 12] > 0: - dCondSNC[ia, iy] =self.dRoadWorks[iTheRepair - 1, 12] - dYearSNC =self.dRoadWorks[iTheRepair - 1, 12] + dCondSNC[ia, iy] = self.dRoadWorks[iTheRepair - 1, 12] + dYearSNC = self.dRoadWorks[iTheRepair - 1, 12] # repair work costs if iTerrain == 1: - dCostRepairFin[ia, iy] =self.dRoadWorks[iTheRepair-1, 3] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 - dCostRepairEco[ia, iy] =self.dRoadWorks[iTheRepair-1, 3] * dCondLength[ia, iy] * dCondWidth[ia, iy] * self.dEconomic_Factor / 1000.0 + dCostRepairFin[ia, iy] = ( + self.dRoadWorks[iTheRepair - 1, 3] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 + ) + dCostRepairEco[ia, iy] = ( + self.dRoadWorks[iTheRepair - 1, 3] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + * self.dEconomic_Factor + / 1000.0 + ) elif iTerrain == 2: - dCostRepairFin[ia, iy] =self.dRoadWorks[iTheRepair-1, 4] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 - dCostRepairEco[ia, iy] =self.dRoadWorks[iTheRepair-1, 4] * dCondLength[ia, iy] * dCondWidth[ia, iy] * self.dEconomic_Factor / 1000.0 + dCostRepairFin[ia, iy] = ( + self.dRoadWorks[iTheRepair - 1, 4] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 + ) + dCostRepairEco[ia, iy] = ( + self.dRoadWorks[iTheRepair - 1, 4] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + * self.dEconomic_Factor + / 1000.0 + ) elif iTerrain == 3: - dCostRepairFin[ia, iy] =self.dRoadWorks[iTheRepair-1, 5] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 - dCostRepairEco[ia, iy] =self.dRoadWorks[iTheRepair-1, 5] * dCondLength[ia, iy] * dCondWidth[ia, iy] * self.dEconomic_Factor / 1000.0 - + dCostRepairFin[ia, iy] = ( + self.dRoadWorks[iTheRepair - 1, 5] * dCondLength[ia, iy] * dCondWidth[ia, iy] / 1000.0 + ) + dCostRepairEco[ia, iy] = ( + self.dRoadWorks[iTheRepair - 1, 5] + * dCondLength[ia, iy] + * dCondWidth[ia, iy] + * self.dEconomic_Factor + / 1000.0 + ) + # recurrent road work without recurrent maintenance condition multipliers - dCostRecurrentFin[ia, iy] = self.dRecurrent[iCondSurface[ia, iy]-1, iCondLanes[ia, iy]-1] * dCondLength[ia, iy] / 1000000.0 - dCostRecurrentEco[ia, iy] = self.dRecurrent[iCondSurface[ia, iy]-1, iCondLanes[ia, iy]-1] * dCondLength[ia, iy] * self.dEconomic_Factor / 1000000.0 - + dCostRecurrentFin[ia, iy] = ( + self.dRecurrent[iCondSurface[ia, iy] - 1, iCondLanes[ia, iy] - 1] * dCondLength[ia, iy] / 1000000.0 + ) + dCostRecurrentEco[ia, iy] = ( + self.dRecurrent[iCondSurface[ia, iy] - 1, iCondLanes[ia, iy] - 1] + * dCondLength[ia, iy] + * self.dEconomic_Factor + / 1000000.0 + ) + # Rouhgness if iy > 0: """ @@ -241,32 +332,41 @@ def compute_cba_for_section(self, section): dYearRoughness = dYearRoughness * (1 + self.dRoadDet[0, 1]) if dYearRoughness > 16: dYearRoughness = 16 - + iYearAge = iYearAge + 1 - + elif iCondSurface[ia, iy] == 2: if self.dRoadDet[1, 0] == float(1): # Constant Factor Increase, dRoadDet[1, 1] is currently None dYearRoughness = dYearRoughness * (1 + self.dRoadDet[1, 1]) iYearAge = iYearAge + 1 - elif self.dRoadDet[1, 0] == float(2): + elif self.dRoadDet[1, 0] == float(2): # HDM-4 Simplified Equation: # RIb = RIa + Kgp * (a0 * Exp (Kgm * m * AGE3) * [(1 + SNC * a1)]-5 * YE4 # + a2 * AGE3) + (Kgm *m * RIa) dYearRoughness = dYearRoughness + ( - self.dRoadDet[1, 2] * ( - self.dRoadDet[1, 4] * np.exp(self.dRoadDet[1, 3] * self.dm_coeff[iTemperature-1, iMoisture-1] * iYearAge) * \ - np.power(1 + dCondSNC[ia, iy] * self.dRoadDet[1, 5], -5) * dESATotal[iy] + self.dRoadDet[1, 6] * iYearAge - ) + ( - self.dRoadDet[1, 3] * self.dm_coeff[iTemperature-1, iMoisture-1] * dYearRoughness + self.dRoadDet[1, 2] + * ( + self.dRoadDet[1, 4] + * np.exp( + self.dRoadDet[1, 3] * self.dm_coeff[iTemperature - 1, iMoisture - 1] * iYearAge + ) + * np.power(1 + dCondSNC[ia, iy] * self.dRoadDet[1, 5], -5) + * dESATotal[iy] + + self.dRoadDet[1, 6] * iYearAge + ) + + ( + self.dRoadDet[1, 3] + * self.dm_coeff[iTemperature - 1, iMoisture - 1] + * dYearRoughness ) ) iYearAge = iYearAge + 1 - elif self.dRoadDet[1, 0] == float(3): + elif self.dRoadDet[1, 0] == float(3): # Climate Related dYearRoughness = dYearRoughness * (1 + self.dm_coeff[iTemperature - 1, iMoisture - 1]) iYearAge = iYearAge + 1 - + if dYearRoughness > 16: dYearRoughness = 16 @@ -280,16 +380,26 @@ def compute_cba_for_section(self, section): # RIb = RIa + Kgp * (a0 * Exp (Kgm * m * AGE3) * [(1 + SNC * a1)]-5 * YE4 # + a2 * AGE3) + (Kgm *m * RIa) dYearRoughness = dYearRoughness + ( - self.dRoadDet[2, 2] * ( - self.dRoadDet[2, 4] * np.exp(self.dRoadDet[2, 3] * self.dm_coeff[iTemperature - 1, iMoisture - 1] * iYearAge) * np.power((1 + dCondSNC[ia, iy] * self.dRoadDet[2, 5]), -5) * dESATotal[iy] + self.dRoadDet[2, 6] * iYearAge - ) + ( - self.dRoadDet[2, 3] * self.dm_coeff[iTemperature-1, iMoisture-1] * dYearRoughness + self.dRoadDet[2, 2] + * ( + self.dRoadDet[2, 4] + * np.exp( + self.dRoadDet[2, 3] * self.dm_coeff[iTemperature - 1, iMoisture - 1] * iYearAge + ) + * np.power((1 + dCondSNC[ia, iy] * self.dRoadDet[2, 5]), -5) + * dESATotal[iy] + + self.dRoadDet[2, 6] * iYearAge + ) + + ( + self.dRoadDet[2, 3] + * self.dm_coeff[iTemperature - 1, iMoisture - 1] + * dYearRoughness ) ) iYearAge = iYearAge + 1 elif self.dRoadDet[2, 0] == float(3): # Climate Related Only - dYearRoughness = dYearRoughness * (1 + self.dm_coeff[iTemperature-1, iMoisture-1]) + dYearRoughness = dYearRoughness * (1 + self.dm_coeff[iTemperature - 1, iMoisture - 1]) iYearAge = iYearAge + 1 if dYearRoughness > 16: @@ -298,28 +408,28 @@ def compute_cba_for_section(self, section): elif iCondSurface[ia, iy] == 4: dYearRoughness = dYearRoughness * (1 + self.dRoadDet[3, 1]) iYearAge = iYearAge + 1 - + if dYearRoughness > 25: dYearRoughness = 25 elif iCondSurface[ia, iy] == 5: dYearRoughness = dYearRoughness * (1 + self.dRoadDet[4, 1]) iYearAge = iYearAge + 1 - + if dYearRoughness > 25: dYearRoughness = 25 elif iCondSurface[ia, iy] == 6: dYearRoughness = dYearRoughness * (1 + self.dRoadDet[5, 1]) iYearAge = iYearAge + 1 - + if dYearRoughness > 16: dYearRoughness = 16 elif iCondSurface[ia, iy] == 7: dYearRoughness = dYearRoughness * (1 + self.dRoadDet[6, 1]) iYearAge = iYearAge + 1 - + if dYearRoughness > 16: dYearRoughness = 16 @@ -327,78 +437,103 @@ def compute_cba_for_section(self, section): """ Rougnesss effect function of road work type """ - dYearRoughness =self.dRoadWorks[iTheWork - 1, 6] + dYearRoughness = self.dRoadWorks[iTheWork - 1, 6] iYearAge = 1 - if (iy == iTheRepairY1 - 1) or (iy == iTheRepairY2 - 1) or (iy == iTheRepairY3 - 1) or (iy == iTheRepairY4 - 1): - dYearRoughness =self.dRoadWorks[iTheRepair - 1, 6] + if ( + (iy == iTheRepairY1 - 1) + or (iy == iTheRepairY2 - 1) + or (iy == iTheRepairY3 - 1) + or (iy == iTheRepairY4 - 1) + ): + dYearRoughness = self.dRoadWorks[iTheRepair - 1, 6] iYearAge = 1 - + dCondIRI[ia, iy] = dYearRoughness iCondAge[ia, iy] = iYearAge - + # VOC dCostVOC[ia, iy] = 0 - for iv in range(12): # 12 types of vehicles + for iv in range(12): # 12 types of vehicles if self.dVOC[iCondLanes[ia, iy] - 1, iTerrain - 1, 0, iv] != float(0): - dCostVOC[ia, iy] = dCostVOC[ia, iy] + ( - self.dVOC[iCondLanes[ia, iy]-1, iTerrain-1, 0, iv] + \ - (self.dVOC[iCondLanes[ia, iy]-1, iTerrain-1, 1, iv] * dCondIRI[ia, iy]) + \ - (self.dVOC[iCondLanes[ia, iy]-1, iTerrain-1, 2, iv] * np.power(dCondIRI[ia, iy], 2)) + \ - (self.dVOC[iCondLanes[ia, iy]-1, iTerrain-1, 3, iv] * np.power(dCondIRI[ia, iy], 3)) - ) * dAADT[iv, iy] + dCostVOC[ia, iy] = ( + dCostVOC[ia, iy] + + ( + self.dVOC[iCondLanes[ia, iy] - 1, iTerrain - 1, 0, iv] + + (self.dVOC[iCondLanes[ia, iy] - 1, iTerrain - 1, 1, iv] * dCondIRI[ia, iy]) + + ( + self.dVOC[iCondLanes[ia, iy] - 1, iTerrain - 1, 2, iv] + * np.power(dCondIRI[ia, iy], 2) + ) + + ( + self.dVOC[iCondLanes[ia, iy] - 1, iTerrain - 1, 3, iv] + * np.power(dCondIRI[ia, iy], 3) + ) + ) + * dAADT[iv, iy] + ) dCostVOC[ia, iy] = dCostVOC[ia, iy] * dCondLength[ia, iy] * 365 / 1000000 # Speed dCondSpeedAve[ia, iy] = 0 ii = 0 - for iv in range (0, 12): + for iv in range(0, 12): if self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 0, iv] != float(0): - dCondSpeed[ia, iy, iv] = \ - self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 0, iv] + \ - (self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 1, iv] * dCondIRI[ia, iy]) + \ - (self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 2, iv] * np.power(dCondIRI[ia, iy], 2)) + \ - (self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 3, iv] * np.power(dCondIRI[ia, iy], 3)) - + dCondSpeed[ia, iy, iv] = ( + self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 0, iv] + + (self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 1, iv] * dCondIRI[ia, iy]) + + (self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 2, iv] * np.power(dCondIRI[ia, iy], 2)) + + (self.dSPEED[iCondLanes[ia, iy] - 1, iTerrain - 1, 3, iv] * np.power(dCondIRI[ia, iy], 3)) + ) + dCondSpeedAve[ia, iy] = dCondSpeedAve[ia, iy] + dCondSpeed[ia, iy, iv] ii = ii + 1 dCondSpeedAve[ia, iy] = dCondSpeedAve[ia, iy] / ii - + # Time dCostTime[ia, iy] = 0 for iv in range(12): if dCondSpeed[ia, iy, iv] > float(0): - dCostTime[ia, iy] = dCostTime[ia, iy] + \ - 1 / dCondSpeed[ia, iy, iv] * dCondLength[ia, iy] * self.dVehicleFleet[iv, 1] * self.dVehicleFleet[iv, 2] * dAADT[iv, iy] * 365 / 1000000 - + dCostTime[ia, iy] = ( + dCostTime[ia, iy] + + 1 + / dCondSpeed[ia, iy, iv] + * dCondLength[ia, iy] + * self.dVehicleFleet[iv, 1] + * self.dVehicleFleet[iv, 2] + * dAADT[iv, iy] + * 365 + / 1000000 + ) + # Pavement Condition Class function of rougness dCondCON[ia, iy] = get_cc_from_iri(self.iri_cc_df, dCondIRI[ia, iy], iSurfaceType) dCostRecurrentFin[ia, iy] = dCostRecurrentFin[ia, iy] * dRecMult[dCondCON[ia, iy] - 1] dCostRecurrentEco[ia, iy] = dCostRecurrentEco[ia, iy] * dRecMult[dCondCON[ia, iy] - 1] - + # road agency costs dCostAgencyFin[ia, iy] = dCostCapitalFin[ia, iy] + dCostRepairFin[ia, iy] + dCostRecurrentFin[ia, iy] dCostAgencyEco[ia, iy] = dCostCapitalEco[ia, iy] + dCostRepairEco[ia, iy] + dCostRecurrentEco[ia, iy] - + # Users and Total dCostUsers[ia, iy] = dCostVOC[ia, iy] + dCostTime[ia, iy] dCostTotal[ia, iy] = dCostAgencyEco[ia, iy] + dCostUsers[ia, iy] - + # Net Benefits dNetTotal[ia, iy] = dCostTotal[0, iy] - dCostTotal[ia, iy] - + # NPV # Serious Note: raise to the power iy not (iy - 1) in the following equation dSolNPV[ia] = dSolNPV[ia] + dNetTotal[ia, iy] / ((1 + self.dDiscount_Rate) ** (iy)) dSolNPVKm[ia] = dSolNPV[ia] / dCondLength[ia, 0] - + if dSolCost[ia] > 0: dSolNPVCost[ia] = dSolNPV[ia] / dSolCost[ia] else: dSolNPVCost[ia] = 0 # End loop iy - + if ia > 1: # add computation of IRR here if needed dSolIRR[ia] = 0 @@ -411,26 +546,38 @@ def compute_cba_for_section(self, section): # Get the output results ########################################################### results = { - 'work_class': sSolClass[iTheSelected], - 'work_type': sSolCode[iTheSelected], - 'work_name': sSolName[iTheSelected], - 'work_cost': dSolCost[iTheSelected], - 'work_cost_km': dSolCostkm[iTheSelected], - 'work_year': int(iSolYear[iTheSelected]), - 'npv': dSolNPV[iTheSelected], - 'npv_km': dSolNPVKm[iTheSelected], - 'npv_cost': dSolNPVCost[iTheSelected], - 'eirr': dSolIRR[iTheSelected], - 'aadt': dAADT[12].tolist(), - 'truck_percent': dTRucks, - 'vehicle_utilization': dUtilization, - 'esa_loading': dESATotal[1], - 'iri_projection': dCondIRI[iTheSelected,].tolist(), - 'iri_base': dCondIRI[0,].tolist(), - 'con_projection': dCondCON[iTheSelected,].tolist(), - 'con_base': dCondCON[0,].tolist(), - 'financial_recurrent_cost': dCostRecurrentFin[iTheSelected,].tolist(), - 'net_benefits': dNetTotal[iTheSelected,].tolist() + "work_class": sSolClass[iTheSelected], + "work_type": sSolCode[iTheSelected], + "work_name": sSolName[iTheSelected], + "work_cost": dSolCost[iTheSelected], + "work_cost_km": dSolCostkm[iTheSelected], + "work_year": int(iSolYear[iTheSelected]), + "npv": dSolNPV[iTheSelected], + "npv_km": dSolNPVKm[iTheSelected], + "npv_cost": dSolNPVCost[iTheSelected], + "eirr": dSolIRR[iTheSelected], + "aadt": dAADT[12].tolist(), + "truck_percent": dTRucks, + "vehicle_utilization": dUtilization, + "esa_loading": dESATotal[1], + "iri_projection": dCondIRI[ + iTheSelected, + ].tolist(), + "iri_base": dCondIRI[ + 0, + ].tolist(), + "con_projection": dCondCON[ + iTheSelected, + ].tolist(), + "con_base": dCondCON[ + 0, + ].tolist(), + "financial_recurrent_cost": dCostRecurrentFin[ + iTheSelected, + ].tolist(), + "net_benefits": dNetTotal[ + iTheSelected, + ].tolist(), } return results @@ -438,23 +585,25 @@ def compute_alternatives(self, iSurfaceType, iRoadClass, iConditionClass): iNoAlernatives = 0 dAlternatives = np.zeros((13, 2), dtype=np.float64) + idx = (iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1 + year, number, x, y = self.dWorkEvaluated[idx, :] # Get initial road work for the 13 alternatives - dAlternatives[0, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 1]) - dAlternatives[1, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 2]) - dAlternatives[2, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 2]) - dAlternatives[3, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 2]) - dAlternatives[4, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 2]) - dAlternatives[5, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 2]) - dAlternatives[6, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 2]) - dAlternatives[7, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 3]) - dAlternatives[8, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 3]) - dAlternatives[9, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 3]) - dAlternatives[10, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 3]) - dAlternatives[11, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 3]) - dAlternatives[12, 0] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 3]) + dAlternatives[0, 0] = number + dAlternatives[1, 0] = x + dAlternatives[2, 0] = x + dAlternatives[3, 0] = x + dAlternatives[4, 0] = x + dAlternatives[5, 0] = x + dAlternatives[6, 0] = x + dAlternatives[7, 0] = y + dAlternatives[8, 0] = y + dAlternatives[9, 0] = y + dAlternatives[10, 0] = y + dAlternatives[11, 0] = y + dAlternatives[12, 0] = y # Define years of initial works for 13 alternatives - dAlternatives[0, 1] = int(self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 0]) + dAlternatives[0, 1] = year if dAlternatives[1, 0] > 0: # first road work defined: evaluate at least 7 alternatives dAlternatives[1, 1] = 1 @@ -474,11 +623,11 @@ def compute_alternatives(self, iSurfaceType, iRoadClass, iConditionClass): dAlternatives[12, 1] = 6 iNoAlernatives = 13 - if dAlternatives[1, 0] == 0 and dAlternatives[7, 0] == 0: # no road works defined: evaluate 2 base alternatives - dAlternatives[1, 0] = self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 1] - dAlternatives[1, 1] = self.dWorkEvaluated[(iSurfaceType - 1) * 50 + (iRoadClass - 1) * 5 + iConditionClass - 1, 0] + if dAlternatives[1, 0] == 0 and dAlternatives[7, 0] == 0: # no road works defined: evaluate 2 base alternatives + dAlternatives[1, 0] = number + dAlternatives[1, 1] = year iNoAlernatives = 2 - + return iNoAlernatives, dAlternatives def compute_cost_factor(self, iSurfaceType, iRoadClass, iConditionClass): @@ -493,7 +642,7 @@ def compute_annual_traffic(self, dAADT, iGrowthScenario): dAADT[12, iy] = 0 for iv in range(12): dAADT[12, iy] = dAADT[12, iy] + dAADT[iv, iy] - + return dAADT def compute_esa_loading(self, dAADT, iLanes): @@ -502,7 +651,10 @@ def compute_esa_loading(self, dAADT, iLanes): for iy in range(0, 20): dESATotal[iy] = 0 for iv in range(0, 12): - dESATotal[iy] = dESATotal[iy] + dAADT[iv, iy] * self.dVehicleFleet[iv, 0] / 1000000 * 365 / self.dWidthDefaults[iLanes - 1, 1] + dESATotal[iy] = ( + dESATotal[iy] + + dAADT[iv, iy] * self.dVehicleFleet[iv, 0] / 1000000 * 365 / self.dWidthDefaults[iLanes - 1, 1] + ) return dESATotal @@ -513,4 +665,4 @@ def compute_vehicle_utilization(self, dAADT, dLength): dUtilization = 0 for iv in range(0, 12): dUtilization = dUtilization + dAADT[iv, 0] * dLength * 365 / 1000000 - return dUtilization \ No newline at end of file + return dUtilization diff --git a/cba/core/default.py b/old_django_implementation/cba/core/default.py similarity index 100% rename from cba/core/default.py rename to old_django_implementation/cba/core/default.py diff --git a/cba/core/migrations/0001_initial.py b/old_django_implementation/cba/core/migrations/0001_initial.py similarity index 100% rename from cba/core/migrations/0001_initial.py rename to old_django_implementation/cba/core/migrations/0001_initial.py diff --git a/cba/core/migrations/0002_cbaresult_section.py b/old_django_implementation/cba/core/migrations/0002_cbaresult_section.py similarity index 100% rename from cba/core/migrations/0002_cbaresult_section.py rename to old_django_implementation/cba/core/migrations/0002_cbaresult_section.py diff --git a/cba/core/migrations/0003_auto_20190608_0918.py b/old_django_implementation/cba/core/migrations/0003_auto_20190608_0918.py similarity index 100% rename from cba/core/migrations/0003_auto_20190608_0918.py rename to old_django_implementation/cba/core/migrations/0003_auto_20190608_0918.py diff --git a/cba/core/migrations/__init__.py b/old_django_implementation/cba/core/migrations/__init__.py similarity index 100% rename from cba/core/migrations/__init__.py rename to old_django_implementation/cba/core/migrations/__init__.py diff --git a/cba/core/models.py b/old_django_implementation/cba/core/models.py similarity index 100% rename from cba/core/models.py rename to old_django_implementation/cba/core/models.py diff --git a/cba/core/tests.py b/old_django_implementation/cba/core/tests.py similarity index 100% rename from cba/core/tests.py rename to old_django_implementation/cba/core/tests.py diff --git a/cba/core/views.py b/old_django_implementation/cba/core/views.py similarity index 100% rename from cba/core/views.py rename to old_django_implementation/cba/core/views.py diff --git a/cba/fixtures/pavement_condition_class.json b/old_django_implementation/cba/fixtures/pavement_condition_class.json similarity index 100% rename from cba/fixtures/pavement_condition_class.json rename to old_django_implementation/cba/fixtures/pavement_condition_class.json diff --git a/cba/fixtures/provinces_districts.json b/old_django_implementation/cba/fixtures/provinces_districts.json similarity index 100% rename from cba/fixtures/provinces_districts.json rename to old_django_implementation/cba/fixtures/provinces_districts.json diff --git a/cba/fixtures/road_sections_thanhhoa.csv b/old_django_implementation/cba/fixtures/road_sections_thanhhoa.csv similarity index 100% rename from cba/fixtures/road_sections_thanhhoa.csv rename to old_django_implementation/cba/fixtures/road_sections_thanhhoa.csv diff --git a/cba/fixtures/surface_type.json b/old_django_implementation/cba/fixtures/surface_type.json similarity index 100% rename from cba/fixtures/surface_type.json rename to old_django_implementation/cba/fixtures/surface_type.json diff --git a/cba/fixtures/traffic_level.json b/old_django_implementation/cba/fixtures/traffic_level.json similarity index 100% rename from cba/fixtures/traffic_level.json rename to old_django_implementation/cba/fixtures/traffic_level.json diff --git a/cba/manage.py b/old_django_implementation/cba/manage.py similarity index 100% rename from cba/manage.py rename to old_django_implementation/cba/manage.py diff --git a/cba/roads/__init__.py b/old_django_implementation/cba/roads/__init__.py similarity index 100% rename from cba/roads/__init__.py rename to old_django_implementation/cba/roads/__init__.py diff --git a/cba/roads/adapters.py b/old_django_implementation/cba/roads/adapters.py similarity index 100% rename from cba/roads/adapters.py rename to old_django_implementation/cba/roads/adapters.py diff --git a/cba/roads/admin.py b/old_django_implementation/cba/roads/admin.py similarity index 100% rename from cba/roads/admin.py rename to old_django_implementation/cba/roads/admin.py diff --git a/cba/roads/apps.py b/old_django_implementation/cba/roads/apps.py similarity index 100% rename from cba/roads/apps.py rename to old_django_implementation/cba/roads/apps.py diff --git a/cba/roads/constants.py b/old_django_implementation/cba/roads/constants.py similarity index 100% rename from cba/roads/constants.py rename to old_django_implementation/cba/roads/constants.py diff --git a/cba/roads/management/__init__.py b/old_django_implementation/cba/roads/management/__init__.py similarity index 100% rename from cba/roads/management/__init__.py rename to old_django_implementation/cba/roads/management/__init__.py diff --git a/cba/roads/management/commands/__init__.py b/old_django_implementation/cba/roads/management/commands/__init__.py similarity index 100% rename from cba/roads/management/commands/__init__.py rename to old_django_implementation/cba/roads/management/commands/__init__.py diff --git a/cba/roads/management/commands/import_section_data.py b/old_django_implementation/cba/roads/management/commands/import_section_data.py similarity index 100% rename from cba/roads/management/commands/import_section_data.py rename to old_django_implementation/cba/roads/management/commands/import_section_data.py diff --git a/cba/roads/migrations/0001_initial.py b/old_django_implementation/cba/roads/migrations/0001_initial.py similarity index 100% rename from cba/roads/migrations/0001_initial.py rename to old_django_implementation/cba/roads/migrations/0001_initial.py diff --git a/cba/roads/migrations/0002_auto_20181016_1913.py b/old_django_implementation/cba/roads/migrations/0002_auto_20181016_1913.py similarity index 100% rename from cba/roads/migrations/0002_auto_20181016_1913.py rename to old_django_implementation/cba/roads/migrations/0002_auto_20181016_1913.py diff --git a/cba/roads/migrations/0003_auto_20181220_1404.py b/old_django_implementation/cba/roads/migrations/0003_auto_20181220_1404.py similarity index 100% rename from cba/roads/migrations/0003_auto_20181220_1404.py rename to old_django_implementation/cba/roads/migrations/0003_auto_20181220_1404.py diff --git a/cba/roads/migrations/0004_managementclass_moistureclass_pavementconditionclass_pavementtype_roadclass_surfacetype_temperaturec.py b/old_django_implementation/cba/roads/migrations/0004_managementclass_moistureclass_pavementconditionclass_pavementtype_roadclass_surfacetype_temperaturec.py similarity index 100% rename from cba/roads/migrations/0004_managementclass_moistureclass_pavementconditionclass_pavementtype_roadclass_surfacetype_temperaturec.py rename to old_django_implementation/cba/roads/migrations/0004_managementclass_moistureclass_pavementconditionclass_pavementtype_roadclass_surfacetype_temperaturec.py diff --git a/cba/roads/migrations/0005_auto_20190607_1321.py b/old_django_implementation/cba/roads/migrations/0005_auto_20190607_1321.py similarity index 100% rename from cba/roads/migrations/0005_auto_20190607_1321.py rename to old_django_implementation/cba/roads/migrations/0005_auto_20190607_1321.py diff --git a/cba/roads/migrations/0006_auto_20190607_1351.py b/old_django_implementation/cba/roads/migrations/0006_auto_20190607_1351.py similarity index 100% rename from cba/roads/migrations/0006_auto_20190607_1351.py rename to old_django_implementation/cba/roads/migrations/0006_auto_20190607_1351.py diff --git a/cba/roads/migrations/0007_laneclass.py b/old_django_implementation/cba/roads/migrations/0007_laneclass.py similarity index 100% rename from cba/roads/migrations/0007_laneclass.py rename to old_django_implementation/cba/roads/migrations/0007_laneclass.py diff --git a/cba/roads/migrations/0008_auto_20190607_1645.py b/old_django_implementation/cba/roads/migrations/0008_auto_20190607_1645.py similarity index 100% rename from cba/roads/migrations/0008_auto_20190607_1645.py rename to old_django_implementation/cba/roads/migrations/0008_auto_20190607_1645.py diff --git a/cba/roads/migrations/0009_auto_20190607_1657.py b/old_django_implementation/cba/roads/migrations/0009_auto_20190607_1657.py similarity index 100% rename from cba/roads/migrations/0009_auto_20190607_1657.py rename to old_django_implementation/cba/roads/migrations/0009_auto_20190607_1657.py diff --git a/cba/roads/migrations/0010_auto_20190607_1716.py b/old_django_implementation/cba/roads/migrations/0010_auto_20190607_1716.py similarity index 100% rename from cba/roads/migrations/0010_auto_20190607_1716.py rename to old_django_implementation/cba/roads/migrations/0010_auto_20190607_1716.py diff --git a/cba/roads/migrations/__init__.py b/old_django_implementation/cba/roads/migrations/__init__.py similarity index 100% rename from cba/roads/migrations/__init__.py rename to old_django_implementation/cba/roads/migrations/__init__.py diff --git a/cba/roads/models.py b/old_django_implementation/cba/roads/models.py similarity index 100% rename from cba/roads/models.py rename to old_django_implementation/cba/roads/models.py diff --git a/cba/roads/property_classes.py b/old_django_implementation/cba/roads/property_classes.py similarity index 100% rename from cba/roads/property_classes.py rename to old_django_implementation/cba/roads/property_classes.py diff --git a/cba/roads/serializers.py b/old_django_implementation/cba/roads/serializers.py similarity index 100% rename from cba/roads/serializers.py rename to old_django_implementation/cba/roads/serializers.py diff --git a/cba/roads/tests.py b/old_django_implementation/cba/roads/tests.py similarity index 100% rename from cba/roads/tests.py rename to old_django_implementation/cba/roads/tests.py diff --git a/cba/roads/urls.py b/old_django_implementation/cba/roads/urls.py similarity index 100% rename from cba/roads/urls.py rename to old_django_implementation/cba/roads/urls.py diff --git a/cba/roads/views.py b/old_django_implementation/cba/roads/views.py similarity index 100% rename from cba/roads/views.py rename to old_django_implementation/cba/roads/views.py diff --git a/docs/.gitkeep b/old_django_implementation/docs/.gitkeep similarity index 100% rename from docs/.gitkeep rename to old_django_implementation/docs/.gitkeep diff --git a/docs/environment_variables.md b/old_django_implementation/docs/environment_variables.md similarity index 100% rename from docs/environment_variables.md rename to old_django_implementation/docs/environment_variables.md diff --git a/envs/.gitkeep b/old_django_implementation/envs/.gitkeep similarity index 100% rename from envs/.gitkeep rename to old_django_implementation/envs/.gitkeep diff --git a/scripts/insert_traffic_level.sql b/old_django_implementation/scripts/insert_traffic_level.sql similarity index 100% rename from scripts/insert_traffic_level.sql rename to old_django_implementation/scripts/insert_traffic_level.sql diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..be64197 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +flask +# sqlite3 +click +-e ../roads-cba-py diff --git a/run_dev_server.sh b/run_dev_server.sh new file mode 100755 index 0000000..f5c185d --- /dev/null +++ b/run_dev_server.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -eu + +main() { + source venv/bin/activate + FLASK_APP=cba_server FLASK_ENV=development flask run +} + +main "$@" diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..907b308 --- /dev/null +++ b/setup.py @@ -0,0 +1 @@ +blah diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..8bfe8f4 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,13 @@ +import unittest +from os.path import join, dirname +import sys + +import os + +sys.path.append(".") + + +class TestApi(unittest.TestCase): + def test_simple(self): + s = {"id": 7} + self.assertEqual("635950_304", s.id)