diff --git a/connector/Include/analytics.py b/connector/Include/analytics.py new file mode 100644 index 00000000..25d2a159 --- /dev/null +++ b/connector/Include/analytics.py @@ -0,0 +1,116 @@ +from flask import Flask, request, jsonify +from flask_caching import Cache +import psycopg2 +import pandas +import json +from configparser import ConfigParser +from datetime import datetime + + +app = Flask(__name__) +cache = Cache(config={'CACHE_TYPE': 'SimpleCache'}) +db_connector = None +conn = None + + +@app.route("/api/get_number_of_ratings", methods=["GET"]) +@cache.cached(timeout=500) +def get_number_of_ratings(): + cursor = conn.cursor() + cursor.execute("select count(*) as num_of_ratings from ratings") + res = cursor.fetchall() + + cursor.close() + + return jsonify(res[0]), 200 + + +@app.route("/api/get_movie_ratings/", methods=["GET"]) +@cache.cached(timeout=50) +def get_movie_ratings(movie_id): + cursor = conn.cursor() + ratings = {} + rating_values = [5, 4, 3, 2, 1] + + for rating in rating_values: + cursor.execute(""" + SELECT COUNT(*) as count + FROM ratings + WHERE rating = %s AND movie_ID = %s; + """, (rating, movie_id)) + result = cursor.fetchone() + ratings[f'{rating}_star'] = result[0] + + cursor.close() + + return jsonify(ratings), 200 + + +@app.route("/api/get_users_number", methods=["GET"]) +@cache.cached(timeout=50) +def get_number_of_users(): + cursor = conn.cursor() + cursor.execute("select count(*) as num_of_users from users") + res = cursor.fetchall() + + cursor.close() + + return jsonify(res[0]), 200 + + +@app.route("/api/get_movie_rating_avg/", methods=["GET"]) +@cache.cached(timeout=50) +def get_movie_rating_avg(movie_id): + cursor = conn.cursor() + cursor.execute(""" + SELECT AVG(rating) as avg_rating + FROM ratings + WHERE movie_ID = %s; + """, (movie_id,)) + res = cursor.fetchall() + + cursor.close() + + return jsonify(res[0]), 200 + + +@app.route("/api/get_user_ratings/", methods=["GET"]) +@cache.cached(timeout=50) +def get_user_ratings(user_id): + cursor = conn.cursor() + cursor.execute(""" + SELECT * + FROM ratings + WHERE oauth_ID = %s; + """, (user_id,)) + res = cursor.fetchall() + + cursor.close() + + return jsonify(res), 200 + + +if __name__ == "__main__": + config = ConfigParser() + config.read("init_scripts/constants.ini") + + while True: + try: + conn = psycopg2.connect( + host=config["postgres"]["host"], + database=config["postgres"]["database"], + user=config["postgres"]["user"], + password=config["postgres"]["password"], + port=int(config["postgres"]["port"]) + ) + + except Exception: + print("Trying to connect with database") + continue + else: + break + + cache.init_app(app) + app.run(host="0.0.0.0", port=8090, debug=True) + + conn.close() diff --git a/connector/Include/frontend_AI_connector.py b/connector/Include/frontend_AI_connector.py index 304dff00..da3d60ae 100644 --- a/connector/Include/frontend_AI_connector.py +++ b/connector/Include/frontend_AI_connector.py @@ -1,4 +1,5 @@ from flask import Flask, request, jsonify +from flask_caching import Cache import psycopg2 import pandas import json @@ -13,6 +14,7 @@ db_connector = None conn = None movie_list = None + def error_decorator(fun): def inner1(*args, **kwargs): try: @@ -22,18 +24,21 @@ def error_decorator(fun): return inner1 + @app.route("/", methods=["GET"]) @cache.cached(timeout=69) def hello(): return jsonify({"response": "Hello there", "time": datetime.now()}), 200 -#endpoint do wyciągania danych o userze + +# endpoint do wyciągania danych o userze @app.route("/api/v3/get/", methods=["GET"]) def access_user(username): return jsonify({"us": "er"}), 200 -#endpoint służący do zapisu danych nowostworzonego użytkownika, podajemy mu -#id z oautha oraz login + +# endpoint służący do zapisu danych nowo stworzonego użytkownika, podajemy mu +# id z oautha oraz login @app.route("/api/v3/add//", methods=["POST"]) def add_user(oauth_ID, username): cursor = conn.cursor() @@ -50,11 +55,11 @@ def add_user(oauth_ID, username): conn.commit() cursor.close() - + return jsonify({"status": "success"}), 200 -#roboczy endpoint służący do wyciąganiu rekomendacji +# roboczy endpoint służący do wyciąganiu rekomendacji @app.route("/api/v3/ai/", methods=["GET"]) def get_recommendations(oauth_ID): cursor = conn.cursor() @@ -67,6 +72,7 @@ def get_recommendations(oauth_ID): headers={'Content-Type': 'application/json'}) return jsonify(response.json()), 200 + @app.route("/api/v3/get_movie/", methods=["GET"]) def get_movie(movie_ID): movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID] @@ -74,8 +80,8 @@ def get_movie(movie_ID): return jsonify({"status": "Movie with ID {} doesn't exist".format(movie_ID)} ), 500 - cast = json.loads(movie_info["cast"][0].replace('\\"','"')) - crew = json.loads(movie_info["crew"][0].replace('\\"','"')) + cast = json.loads(movie_info["cast"][0].replace('\\"', '"')) + crew = json.loads(movie_info["crew"][0].replace('\\"', '"')) output_json = {"movie_id": movie_ID, "title": movie_info["title"][0], @@ -84,6 +90,7 @@ def get_movie(movie_ID): return jsonify(output_json), 200 + @app.route("/api/v3/rate_movie///", methods=["POST"]) def rate_movie(uID, movie_ID, rating): movie_info = movie_list.loc[movie_list['movie_id'] == int(movie_ID)] @@ -120,7 +127,7 @@ def rate_movie(uID, movie_ID, rating): conn.commit() cursor.close() - + return jsonify({"status": "success"}), 200 @@ -146,7 +153,7 @@ if __name__ == "__main__": movie_list = pandas.read_csv(config["movie"]["csv_path"]) cache.init_app(app) - app.run(host="0.0.0.0",port=8090, debug=True) + app.run(host="localhost", port=8090, debug=True) conn.close() diff --git a/test_scripts/Test.http b/test_scripts/Test.http new file mode 100644 index 00000000..33636784 --- /dev/null +++ b/test_scripts/Test.http @@ -0,0 +1,24 @@ +### +GET http://127.0.0.1:8090/get/boop +### +POST http://127.0.0.1:8090/api/v3/AI_recommendations +Content-Type: application/json + +[ + 49026, + 155, + 312113 +] +### +POST http://127.0.0.1:8090/api/v3/add/1111/boop +### +POST http://127.0.0.1:8090/api/v3/rate_movie/1111/155/4 +### +GET http://localhost:8090/api/get_number_of_ratings +### +GET http://localhost:8090/api/get_movie_ratings/155 +### +GET http://localhost:8090/api/get_users_number +### +GET http://localhost:8090/api/get_movie_rating_avg/155 +###