diff --git a/api/Server2_1.py b/api/Server2_1.py index 136a728..978b91e 100644 --- a/api/Server2_1.py +++ b/api/Server2_1.py @@ -1,11 +1,39 @@ #!usr/bin/python3 from flask import Flask, request, jsonify +from flask.json.provider import JSONProvider import psycopg2 from configparser import ConfigParser from argparse import ArgumentParser import decimal +from json import JSONEncoder +import json +from datetime import datetime, time, date +import sys + +class CustomJSONifier(JSONEncoder): + """ + A simple class overriding the default method of JSONEncoder, so it can + encode types like TIME + """ + def default(self, obj): + if isinstance(obj, (datetime, time, date)): + return obj.isoformat() + return super().default(obj) + +class CustomJSONProvider(JSONProvider): + """ + Simple JSON provider modifications, that allow for usage of custom jsonifier. + """ + def dumps(self, obj, **kwargs): + return json.dumps(obj, **kwargs, cls=CustomJSONifier) + def loads(self, s : str | bytes, **kwargs): + return json.loads(s, **kwargs) + + app = Flask(__name__) +app.json = CustomJSONProvider(app) + args_parser = ArgumentParser() @@ -28,6 +56,8 @@ ("affiliation", "organisation") : ("organisation_id", "organisation_id") } +SPEECH_TIME_COLUMNS = ["time_start", "time_end", "earliest_timestamp", "latest_timestamp"] + def determine_joins(columns, conditions, group_by): """ Determine the necessary joins to perform to satisfy the filtering part of @@ -67,9 +97,11 @@ def determine_joins(columns, conditions, group_by): def connect_to_database(db_ini_path=args.db): parser = ConfigParser() + print(db_ini_path) parser.read(db_ini_path) config = {} if parser.has_section("postgresql"): + print("here") parameters = parser.items("postgresql") for parameter in parameters: config[parameter[0]] = parameter[1] @@ -157,24 +189,25 @@ def query(): target_databases = json_query["target_databases"] res = [] for database in target_databases: - step_results = {} - connection = connect_to_database(f"../DatabaseCommunication/{database}.ini") - cursor = connection.cursor() - try: - for step in json_query['steps']: - sql_query, params = SQLBuilder(step, step_results) - cursor.execute(sql_query, params) - step_results[step['goal']] = cursor.fetchall() - except ValueError as e: - return jsonify({"error": str(e)}), 400 - cursor.close() - connection.close() - final_step_name = json_query['steps'][-1]['goal'] - results = step_results[final_step_name] - columns = [col.split(' AS ')[-1] for col in json_query['steps'][-1]['filtering']['columns']] - response = [dict(zip(columns, row)) for row in results] + if (database) == "databaseCS": + step_results = {} + connection = connect_to_database(f"{args.db}{database}.ini") + cursor = connection.cursor() + try: + for step in json_query['steps']: + sql_query, params = SQLBuilder(step, step_results) + cursor.execute(sql_query, params) + step_results[step['goal']] = cursor.fetchall() + except ValueError as e: + return jsonify({"error": str(e)}), 400 + cursor.close() + connection.close() + final_step_name = json_query['steps'][-1]['goal'] + results = step_results[final_step_name] + columns = [col.split(' AS ')[-1] for col in json_query['steps'][-1]['filtering']['columns']] + response = [dict(zip(columns, row)) for row in results] - res.append(format_output(response)) + res.append(format_output(response)) return jsonify(res) if __name__ == "__main__": app.run(debug=True) diff --git a/api/client2.py b/api/client2.py index c6c0a04..0368f5d 100644 --- a/api/client2.py +++ b/api/client2.py @@ -104,10 +104,10 @@ def run(self): print(self.__adjust_results(result)) else: - print(f"Result for {query_file}:") - print(self.__adjust_results(result)) + #print(f"Result for {query_file}:") + #print(self.__adjust_results(result)) if self.target_dir: - with open(f"{self.target_dir}{filename[:-5]}_result.txt", 'a') as file: + with open(f"{self.target_dir}{filename[:-5]}_result.txt", 'w') as file: print(result, file=file) print(file=file) res = self.__adjust_results(result)