Skip to content

Commit

Permalink
Merge pull request #80 from kaggis/webservice-mongo
Browse files Browse the repository at this point in the history
REC-101 Update webservice app to use mongodb as datastore
  • Loading branch information
nikosT authored Oct 27, 2022
2 parents abdc2ee + 80b8c85 commit efd8c5d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ dependencies:
- urllib3==1.26.9
- Werkzeug==2.1.2
- zipp==3.8.0
- flask-pymongo==2.3.0
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ surprise==0.1
urllib3==1.26.9
Werkzeug==2.1.2
zipp==3.8.0
flask-pymongo==2.3.0
2 changes: 1 addition & 1 deletion webservice/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
RS_EVALUATION_METRICS_FILE=../data/metrics.json
RS_EVALUATION_MONGO_URI=mongodb://localhost:27017/rsmetrics
RS_EVALUATION_METRIC_DESC_DIR=../metric_descriptions
36 changes: 13 additions & 23 deletions webservice/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import Flask, render_template, jsonify, abort
from flask_pymongo import PyMongo
import json, os, re
from dotenv import load_dotenv
import yaml
Expand All @@ -9,10 +10,9 @@
dotenv_path = os.path.join(app.instance_path, '.env')
load_dotenv(dotenv_path)



app.config['RS_EVALUATION_METRICS_FILE'] = os.environ.get('RS_EVALUATION_METRICS_FILE')
app.config['RS_EVALUATION_METRIC_DESC_DIR'] = os.environ.get('RS_EVALUATION_METRIC_DESC_DIR')
app.config["MONGO_URI"] = os.environ.get('RS_EVALUATION_MONGO_URI')
mongo = PyMongo(app)

def load_sidebar_info():
'''Reads the available metric description yaml files in metric description folder path
Expand All @@ -37,6 +37,10 @@ def load_sidebar_info():

app.sidebar_info = load_sidebar_info()

def db_get_metrics():
'''Get evaluated metric results from mongodb'''
return mongo.db.metrics.find_one({},{"_id":0})

@app.route("/", strict_slashes=False)
def html_index():
'''Serve the main page that constructs the report view'''
Expand Down Expand Up @@ -106,32 +110,24 @@ def html_metric_description(metric_name):




@app.route("/api")
def get_api_index():
'''Serve metrics and statistics as default api response'''
result = {}

with open(app.config['RS_EVALUATION_METRICS_FILE'], 'r') as f:
result = json.load(f)
result = db_get_metrics()
return jsonify(result)


@app.route("/api/metrics")
def get_metrics():
'''Serve the metrics data in json format'''
result = {}

with open(app.config['RS_EVALUATION_METRICS_FILE'], 'r') as f:
result = json.load(f)
result = db_get_metrics()
return jsonify(result['metrics'])

@app.route("/api/metrics/<string:metric_name>")
def get_metric(metric_name):
'''Serve specific metric data in json format'''
result = {}

with open(app.config['RS_EVALUATION_METRICS_FILE'], 'r') as f:
result = json.load(f)
result = db_get_metrics()

for metric in result['metrics']:
if metric['name'] == metric_name:
Expand All @@ -142,19 +138,13 @@ def get_metric(metric_name):
@app.route("/api/statistics")
def get_statistics():
'''Serve the statistics data in json format'''
result = {}

with open(app.config['RS_EVALUATION_METRICS_FILE'], 'r') as f:
result = json.load(f)
result = db_get_metrics()
return jsonify(result['statistics'])

@app.route("/api/statistics/<string:stat_name>")
def get_statistic(stat_name):
'''Serve specific statistic data in json format'''
result = {}

with open(app.config['RS_EVALUATION_METRICS_FILE'], 'r') as f:
result = json.load(f)
result = db_get_metrics()

for stat in result['statistics']:
if stat['name'] == stat_name:
Expand Down

0 comments on commit efd8c5d

Please sign in to comment.