129 lines
4.4 KiB
Python
129 lines
4.4 KiB
Python
import os
|
|
import glob
|
|
from flask import render_template, Blueprint, current_app, request
|
|
|
|
from app.forms import ScrapingForm
|
|
from app.util import get_size
|
|
from app.config import load_config
|
|
from app.api import scraper as scraper# Import the scraper instance
|
|
from app.logging_config import get_logger
|
|
from app.analysis import load_data, generate_statistics, plot_activity_distribution
|
|
|
|
|
|
from app.state import log_file_name
|
|
|
|
print(f"A imported log_file_name: {log_file_name}")
|
|
|
|
config = load_config()
|
|
logger = get_logger()
|
|
|
|
views_bp = Blueprint("views", __name__)
|
|
|
|
def register_views(app):
|
|
@app.route('/')
|
|
def index():
|
|
form = ScrapingForm()
|
|
return render_template('index.html', form=form)
|
|
|
|
@app.route('/results')
|
|
def results():
|
|
return render_template('results.html')
|
|
|
|
@app.route('/analyze')
|
|
def analyze():
|
|
return render_template('analyze.html')
|
|
|
|
@app.route('/log_viewer')
|
|
def log_viewer():
|
|
return render_template('log_viewer.html')
|
|
|
|
@app.route('/download_results')
|
|
def download_results():
|
|
log_file_name = os.path.abspath(app.config['LOG_FILE_NAME'])
|
|
scraper = app.config.get('SCRAPER')
|
|
|
|
if scraper:
|
|
print(scraper.data_file_name)
|
|
if not scraper:
|
|
print("Scraper not initialized")
|
|
|
|
data_dir = os.path.abspath(config['DATA']['DATA_DIR'])
|
|
log_dir = os.path.abspath(config['LOGGING']['LOG_DIR'])
|
|
|
|
data_files = glob.glob(os.path.join(data_dir, "*.csv"))
|
|
log_files = glob.glob(os.path.join(log_dir, "*.log"))
|
|
|
|
def get_file_info(file_path):
|
|
return {
|
|
"name": file_path,
|
|
"name_display": os.path.basename(file_path),
|
|
"last_modified": os.path.getmtime(file_path),
|
|
"created": os.path.getctime(file_path),
|
|
"size": get_size(file_path)
|
|
}
|
|
|
|
data_files_info = [get_file_info(file) for file in data_files]
|
|
log_files_info = [get_file_info(file) for file in log_files]
|
|
|
|
if scraper and scraper.scraping_active:
|
|
for data_file in data_files_info:
|
|
if os.path.abspath(scraper.data_file_name) == data_file['name']:
|
|
data_file['active'] = True
|
|
else:
|
|
data_file['active'] = False
|
|
|
|
for log_file in log_files_info:
|
|
if log_file_name == os.path.abspath(log_file['name']):
|
|
log_file['active'] = True
|
|
else:
|
|
log_file['active'] = False
|
|
|
|
data_files_info.sort(key=lambda x: x['last_modified'], reverse=True)
|
|
log_files_info.sort(key=lambda x: x['last_modified'], reverse=True)
|
|
|
|
files = {"data": data_files_info, "log": log_files_info}
|
|
|
|
return render_template('download_results.html', files=files)
|
|
|
|
views_bp = Blueprint("views", __name__)
|
|
|
|
@views_bp.route("/data-visualization", methods=["GET", "POST"])
|
|
def data_visualization():
|
|
"""Route to display activity statistics with a visualization."""
|
|
data_dir = current_app.config["DATA"]["DATA_DIR"]
|
|
|
|
# Find all available CSV files
|
|
data_files = sorted(
|
|
glob.glob(os.path.join(data_dir, "*.csv")),
|
|
key=os.path.getmtime,
|
|
reverse=True
|
|
)
|
|
|
|
if not data_files:
|
|
return render_template("data_visualization.html", error="No data files found.", data_files=[])
|
|
|
|
# Get the selected file from the dropdown (default to the latest file)
|
|
selected_file = request.form.get("data_file", data_files[0] if data_files else None)
|
|
|
|
if selected_file and os.path.exists(selected_file):
|
|
df = load_data(selected_file)
|
|
statistics = generate_statistics(df)
|
|
|
|
# ✅ Generate the plot and get the correct URL path
|
|
# remove app/ from the base URL
|
|
plot_url = plot_activity_distribution(df).replace("app/", "")
|
|
|
|
else:
|
|
return render_template("data_visualization.html", error="Invalid file selection.", data_files=data_files)
|
|
|
|
return render_template(
|
|
"data_visualization.html",
|
|
plot_url=plot_url,
|
|
statistics=statistics.to_dict(),
|
|
data_files=data_files,
|
|
selected_file=selected_file
|
|
)
|
|
|
|
|
|
app.register_blueprint(views_bp)
|