generated from IBM/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from IBM/8-generate-web-application
8 generate web application
- Loading branch information
Showing
9 changed files
with
245 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# | ||
# Copyright IBM Corp. 2024 - 2024 | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
import locale | ||
import logging | ||
import os | ||
import re | ||
import shutil | ||
import sys | ||
import tempfile | ||
import time | ||
from pathlib import Path | ||
|
||
from flask import Flask, render_template, request, send_from_directory, redirect | ||
|
||
import javacore_analyzer | ||
import logging_utils | ||
from constants import DEFAULT_REPORTS_DIR, DEFAULT_PORT | ||
|
||
""" | ||
To run the application from cmd type: | ||
export REPORTS_DIR=/tmp/reports | ||
flask --app javacore_analyser_web run | ||
""" | ||
app = Flask(__name__) | ||
with app.app_context(): | ||
logging_utils.create_console_logging() | ||
logging.info("Javacore analyser") | ||
logging.info("Python version: " + sys.version) | ||
logging.info("Preferred encoding: " + locale.getpreferredencoding()) | ||
reports_dir = os.getenv("REPORTS_DIR", DEFAULT_REPORTS_DIR) | ||
logging.info("Reports directory: " + reports_dir) | ||
logging_utils.create_file_logging(reports_dir) | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
reports = [{"name": Path(f).name, "date": time.ctime(os.path.getctime(f)), "timestamp": os.path.getctime(f)} | ||
for f in os.scandir(reports_dir) if f.is_dir()] | ||
reports.sort(key=lambda item: item["timestamp"], reverse=True) | ||
return render_template('index.html', reports=reports) | ||
|
||
|
||
@app.route('/reports/<path:path>') | ||
def dir_listing(path): | ||
return send_from_directory(reports_dir, path) | ||
|
||
|
||
@app.route('/zip/<path:path>') | ||
def compress(path): | ||
try: | ||
temp_zip_dir = tempfile.TemporaryDirectory() | ||
temp_zip_dir_name = temp_zip_dir.name | ||
zip_filename = path + ".zip" | ||
report_location = os.path.join(reports_dir, path) | ||
shutil.make_archive(os.path.join(temp_zip_dir_name, path), 'zip', report_location) | ||
logging.debug("Generated zip file location:" + os.path.join(temp_zip_dir_name, zip_filename)) | ||
logging.debug("Temp zip dir name: " + temp_zip_dir_name) | ||
logging.debug("Zip filename: " + zip_filename) | ||
return send_from_directory(temp_zip_dir_name, zip_filename, as_attachment=True) | ||
finally: | ||
temp_zip_dir.cleanup() | ||
|
||
|
||
@app.route('/delete/<path:path>') | ||
def delete(path): | ||
# Checking if the report exists. This is to prevent attempt to delete any data by deleting any file outside | ||
# report dir if you prepare path variable. | ||
reports_list = os.listdir(reports_dir) | ||
report_location = os.path.normpath(os.path.join(reports_dir, path)) | ||
if not report_location.startswith(reports_dir): | ||
logging.error("Deleted report in report list. Not deleting") | ||
return "Cannot delete the report.", 503 | ||
shutil.rmtree(report_location) | ||
|
||
return redirect("/") | ||
|
||
|
||
# Assisted by WCA@IBM | ||
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2 | ||
@app.route('/upload', methods=['POST']) | ||
def upload_file(): | ||
try: | ||
# Create a temporary directory to store uploaded files | ||
javacores_temp_dir = tempfile.TemporaryDirectory() | ||
javacores_temp_dir_name = javacores_temp_dir.name | ||
|
||
# Get the list of files from webpage | ||
files = request.files.getlist("files") | ||
|
||
input_files = [] | ||
# Iterate for each file in the files List, and Save them | ||
for file in files: | ||
file_name = os.path.join(javacores_temp_dir_name, file.filename) | ||
file.save(file_name) | ||
input_files.append(file_name) | ||
|
||
report_name = request.values.get("report_name") | ||
report_name = re.sub(r'[^a-zA-Z0-9]', '_', report_name) | ||
|
||
# Process the uploaded file | ||
report_output_dir = reports_dir + '/' + report_name | ||
javacore_analyzer.process_javacores_and_generate_report_data(input_files, report_output_dir) | ||
|
||
return redirect("/reports/" + report_name + "/index.html") | ||
finally: | ||
javacores_temp_dir.cleanup() | ||
|
||
|
||
if __name__ == '__main__': | ||
""" | ||
The application passes the following environmental variables: | ||
DEBUG (default: False) - defines if we should run an app in debug mode | ||
PORT - application port | ||
REPORTS_DIR - the directory when the reports are stored as default | ||
""" | ||
app.run(debug=os.getenv("DEBUG", False), port=os.getenv("PORT", DEFAULT_PORT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# | ||
# Copyright IBM Corp. 2024 - 2024 | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
import logging | ||
import sys | ||
from pathlib import Path | ||
|
||
LOGGING_FORMAT = '%(asctime)s [thread: %(thread)d][%(levelname)s][%(filename)s:%(lineno)s] %(message)s' | ||
|
||
|
||
def create_file_logging(logging_file_dir): | ||
logging_file = logging_file_dir + "/wait2-debug.log" | ||
Path(logging_file_dir).mkdir(parents=True, exist_ok=True) # Sometimes the folder of logging might not exist | ||
file_handler = logging.FileHandler(logging_file, mode='w') | ||
file_handler.setLevel(logging.DEBUG) | ||
file_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) | ||
logging.getLogger().addHandler(file_handler) | ||
|
||
|
||
def create_console_logging(): | ||
logging.getLogger().setLevel(logging.NOTSET) | ||
console_handler = logging.StreamHandler(sys.stdout) | ||
console_handler.setLevel(logging.INFO) | ||
console_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) | ||
logging.getLogger().addHandler(console_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ dicttoxml | |
py7zr | ||
lxml | ||
pyana | ||
flask # WSGI server for development the code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
# Copyright IBM Corp. 2024 - 2024 | ||
# SPDX-License-Identifier: Apache-2.0 | ||
--> | ||
|
||
<!-- | ||
// Assisted by WCA@IBM | ||
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2 | ||
--> | ||
<html xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html"> | ||
<head> | ||
<title>Javacore Analyser</title> | ||
</head> | ||
<body> | ||
<h1>Javacore Analyser</h1> | ||
|
||
<h2>Generate report:</h2> | ||
<form action="/upload" method="post" enctype="multipart/form-data"> | ||
<br>Report Name: <input type="text" id="report_name" name="report_name" required></br> | ||
<br>Archive file or multiple javacore files: <input type="file" name="files" multiple required> </br> | ||
<strong> | ||
NOTE: The report generation is expensive operation and might take even few minutes. Please be patient | ||
</strong> | ||
<br><input type="submit" value="Upload"></br> | ||
</form> | ||
<br></br> | ||
|
||
|
||
<h2>List of generated reports:</h2> | ||
<table> | ||
<tr><th> Name </th><th> Creation Date </th><th> Download </th><th> Delete </th></tr> | ||
{% for report in reports %} | ||
{% set name = report['name'] %} | ||
{% set date = report['date'] %} | ||
<tr> | ||
<td><a href="reports/{{ name }}/index.html">{{ name }}</a></td> | ||
<td> {{ date }} </td> | ||
<td><a href="zip/{{ name }}" > download </a></td> | ||
<td> | ||
<a href="delete/{{ name }}" onclick="return confirm('Do you want to delete report {{ name }}?')"> | ||
delete | ||
</a> | ||
</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
</body> | ||
</html> |