diff --git a/data_quality_tool/compose.yaml b/data_quality_tool/compose.yaml index 57b36ec..f09c42a 100644 --- a/data_quality_tool/compose.yaml +++ b/data_quality_tool/compose.yaml @@ -17,6 +17,3 @@ services: dockerfile: Dockerfile ports: - "80:80" - environment: - - FLASK_ENV=development - - FLASK_DEBUG=1 diff --git a/data_quality_tool/controller.py b/data_quality_tool/controller.py index 1a5ded7..1d61490 100644 --- a/data_quality_tool/controller.py +++ b/data_quality_tool/controller.py @@ -1,18 +1,16 @@ +from flask import Flask, request, jsonify, send_file import json import logging -from flask import Flask, request, jsonify, send_file -from flask_cors import CORS -import pandas as pd from io import BytesIO +import pandas as pd +from flask_cors import CORS from converter.excel_to_json import convert_excel_to_json from converter.json_to_excel import convert_json_to_excel from validator import json_validator, excel_validator app = Flask(__name__) -# Allow all origins for all routes CORS(app, resources={r"/*": {"origins": "*"}}) -# Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @@ -32,54 +30,62 @@ def excel_to_json(): logger.error("No selected file") return jsonify({"error": "No selected file"}), 400 if file: - logger.info(f"Processing file: {file.filename}") - file_stream = BytesIO(file.read()) - df = pd.read_excel(file_stream, engine="openpyxl") - logger.info("Excel file read into DataFrame") - excel_validator.validate_excel(df) - logger.info("Excel file validated") - json_data = convert_excel_to_json(df) - logger.info("Excel file converted to JSON") - pretty_json_data = json.dumps(json_data, indent=4) - logger.info("JSON data pretty-printed") - response = app.response_class( - response=pretty_json_data, - status=200, - mimetype='application/json' - ) - return response + try: + logger.info(f"Processing file: {file.filename}") + file_stream = BytesIO(file.read()) + df = pd.read_excel(file_stream, engine="openpyxl") + logger.info("Excel file read into DataFrame") + excel_validator.validate_excel(df) + logger.info("Excel file validated") + json_data = convert_excel_to_json(df) + logger.info("Excel file converted to JSON") + pretty_json_data = json.dumps(json_data, indent=4) + logger.info("JSON data pretty-printed") + response = app.response_class( + response=pretty_json_data, + status=200, + mimetype='application/json' + ) + return response + except Exception as e: + logger.error(f"Error processing file: {str(e)}") + return jsonify({"error": str(e)}), 500 @app.route("/json-to-excel", methods=["POST"]) def json_to_excel(): logger.info("json_to_excel endpoint accessed") if not request.json: logger.error("No JSON provided in request") - return "Please provide the json", 400 + return jsonify({"error": "No JSON provided"}), 400 json_data = request.json - logger.info(f"Processing JSON data: {json_data}") - json_validator.validate_json(json_data) - logger.info("JSON data validated") - df = convert_json_to_excel(json_data) - logger.info("JSON data converted to Excel") - output = BytesIO() - with pd.ExcelWriter(output, engine="xlsxwriter") as writer: - df.to_excel(writer, index=False) - output.seek(0) - logger.info("Excel file created and saved to buffer") - return send_file( - output, - as_attachment=True, - download_name="output.xlsx", - mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", - ) + try: + logger.info(f"Processing JSON data: {json_data}") + json_validator.validate_json(json_data) + logger.info("JSON data validated") + df = convert_json_to_excel(json_data) + logger.info("JSON data converted to Excel") + output = BytesIO() + with pd.ExcelWriter(output, engine="xlsxwriter") as writer: + df.to_excel(writer, index=False) + output.seek(0) + logger.info("Excel file created and saved to buffer") + return send_file( + output, + as_attachment=True, + download_name="output.xlsx", + mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ) + except Exception as e: + logger.error(f"Error processing JSON: {str(e)}") + return jsonify({"error": str(e)}), 500 @app.route("/validate-json", methods=["POST"]) def validate_json(): logger.info("validate_json endpoint accessed") - json_data = request.json if not request.json: logger.error("No JSON provided in request") - return "Please provide the json", 400 + return jsonify({"error": "No JSON provided"}), 400 + json_data = request.json try: json_validator.validate_json(json_data) logger.info("JSON data is valid") @@ -99,11 +105,11 @@ def validate_excel(): logger.error("No selected file") return jsonify({"error": "No selected file"}), 400 if file: - logger.info(f"Processing file: {file.filename}") - file_stream = BytesIO(file.read()) - df = pd.read_excel(file_stream, engine="openpyxl") - logger.info("Excel file read into DataFrame") try: + logger.info(f"Processing file: {file.filename}") + file_stream = BytesIO(file.read()) + df = pd.read_excel(file_stream, engine="openpyxl") + logger.info("Excel file read into DataFrame") excel_validator.validate_excel(df) logger.info("Excel file is valid") return jsonify({"message": "Data model is valid."}) diff --git a/data_quality_tool/frontend/index.html b/data_quality_tool/frontend/index.html index 5790ca1..965f841 100644 --- a/data_quality_tool/frontend/index.html +++ b/data_quality_tool/frontend/index.html @@ -24,6 +24,8 @@

Convert JSON to Excel

+ + diff --git a/data_quality_tool/frontend/script.js b/data_quality_tool/frontend/script.js index ffb1cd0..41c57fb 100644 --- a/data_quality_tool/frontend/script.js +++ b/data_quality_tool/frontend/script.js @@ -1,57 +1,75 @@ +function displayError(message) { + const errorMessage = document.getElementById('error-message'); + errorMessage.textContent = message; + errorMessage.style.display = 'block'; +} + async function convertExcelToJson() { const excelFile = document.getElementById('excelFile').files[0]; if (!excelFile) { - alert("Please select an Excel file first."); + displayError("Please select an Excel file first."); return; } const formData = new FormData(); formData.append('file', excelFile); - const response = await fetch('http://127.0.0.1:8000/excel-to-json', { - method: 'POST', - body: formData - }); - - if (response.ok) { - const blob = await response.blob(); - const url = window.URL.createObjectURL(blob); - const downloadLink = document.getElementById('downloadJson'); - downloadLink.href = url; - downloadLink.download = 'data.json'; - downloadLink.style.display = 'block'; - downloadLink.textContent = 'Download JSON'; - } else { - alert("Failed to convert Excel to JSON."); + try { + const response = await fetch('http://127.0.0.1:8000/excel-to-json', { + method: 'POST', + body: formData + }); + + if (response.ok) { + const blob = await response.blob(); + const url = window.URL.createObjectURL(blob); + const downloadLink = document.getElementById('downloadJson'); + downloadLink.href = url; + downloadLink.download = 'data.json'; + downloadLink.style.display = 'block'; + downloadLink.textContent = 'Download JSON'; + displayError(""); // Clear previous error message + } else { + const errorData = await response.json(); + displayError(`Failed to convert Excel to JSON: ${errorData.error}`); + } + } catch (error) { + displayError(`An error occurred: ${error.message}`); } } async function convertJsonToExcel() { const jsonFile = document.getElementById('jsonFile').files[0]; if (!jsonFile) { - alert("Please select a JSON file first."); + displayError("Please select a JSON file first."); return; } const fileContent = await jsonFile.text(); - const response = await fetch('http://127.0.0.1:8000/json-to-excel', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: fileContent - }); - - if (response.ok) { - const blob = await response.blob(); - const url = window.URL.createObjectURL(blob); - const downloadLink = document.getElementById('downloadExcel'); - downloadLink.href = url; - downloadLink.download = 'data.xlsx'; - downloadLink.style.display = 'block'; - downloadLink.textContent = 'Download Excel'; - } else { - alert("Failed to convert JSON to Excel."); + try { + const response = await fetch('http://127.0.0.1:8000/json-to-excel', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: fileContent + }); + + if (response.ok) { + const blob = await response.blob(); + const url = window.URL.createObjectURL(blob); + const downloadLink = document.getElementById('downloadExcel'); + downloadLink.href = url; + downloadLink.download = 'data.xlsx'; + downloadLink.style.display = 'block'; + downloadLink.textContent = 'Download Excel'; + displayError(""); // Clear previous error message + } else { + const errorData = await response.json(); + displayError(`Failed to convert JSON to Excel: ${errorData.error}`); + } + } catch (error) { + displayError(`An error occurred: ${error.message}`); } } diff --git a/data_quality_tool/frontend/style.css b/data_quality_tool/frontend/style.css index 57ec6dc..dc4654d 100644 --- a/data_quality_tool/frontend/style.css +++ b/data_quality_tool/frontend/style.css @@ -39,3 +39,7 @@ a { margin-top: 10px; color: #007bff; } + +#error-message { + margin-top: 20px; +}