"""PSLA - Perforce Server Log Analyzer """ import os from flask import Flask, request, redirect, url_for, render_template, flash from flask_wtf.csrf import CSRFProtect from werkzeug import secure_filename from config import Config from forms import UploadForm, AccessLog import logging import pandas as pd import sqlite3 import flask_login from app.utils import generatePassword, createDbFolderName, getDatabaseFolders from app import app from app.log2sql import Log2sql UPLOAD_FOLDER = '/logs' ALLOWED_EXTENSIONS = set(['txt']) app.config.from_object(Config) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER csrf = CSRFProtect(app) login_manager = flask_login.LoginManager() login_manager.init_app(app) # Valid users in system - loaded from directories in /logs dir users = {} def allowed_file(filename): return True return '.' in filename and \ filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS class MyOptions(): def __init__(self, dbname, logfile, sql=False, verbosity=logging.INFO, outlog=None): self.dbname = dbname self.logfile = logfile self.sql = sql self.verbosity = verbosity self.interval = 10 self.outlog = outlog self.output = None class User(flask_login.UserMixin): def __init__(self, email, access_key): self.id = "%s|%s" % (email, access_key) self.email = email self.access_key = access_key def __repr__(self): return "%s/%s" % (self.email, self.access_key) @login_manager.user_loader def user_loader(id): app.logger.debug("Loading user: %s" % id) if id not in users: return return users[id] @app.route('/') def index(): """Front page for application""" return render_template('index.html') def initUsers(): databases = getDatabaseFolders(app.config['UPLOAD_FOLDER']) for d in databases: user = User(d['email'], d['access_key']) users[user.id] = user @app.route('/uploadLog', methods=['GET', 'POST']) def uploadLog(): "Upload log file" form = UploadForm() app.logger.debug("uploadLog: %s" % form.errors) app.logger.debug('------ {0}'.format(request.form)) if form.is_submitted(): app.logger.debug(form.errors) if form.validate(): app.logger.debug(form.errors) app.logger.debug(form.errors) if form.validate_on_submit(): filename = secure_filename(form.uploadFile.data.filename) email = form.email.data access_key = generatePassword(8, 3) user = User(email, access_key) users[user.id] = user app.logger.debug("user %s, access_key %s" % (email, access_key)) dbFolder = createDbFolderName(filename, email, access_key) absDbFolder = os.path.join(app.config['UPLOAD_FOLDER'], dbFolder) app.logger.debug("Saving in: %s" % absDbFolder) os.makedirs(absDbFolder) file_path = os.path.join(absDbFolder, filename) form.uploadFile.data.save(file_path) os.chdir(absDbFolder) optionsSQL = MyOptions("testdb", [file_path], sql=True, outlog='log.out') dbname = "%s.db" % optionsSQL.dbname if os.path.exists(dbname): os.remove(dbname) log2sql = Log2sql(optionsSQL) log2sql.processLog() msgs = [] return redirect(url_for('accessLog')) return render_template('uploadLog.html', title='Upload Log', form=form) @app.route('/analyzeLog') @flask_login.login_required def analyzeLog(): "Access previously uploaded log" user = flask_login.current_user app.logger.debug("Current logged in user: %s" % str(user)) databases = getDatabaseFolders(app.config['UPLOAD_FOLDER']) app.logger.debug("Databases: %s" % str(databases)) dbname = os.path.join(databases[0]['folderName'], "testdb.db") if not os.path.exists(dbname): flash('Database does not exist', 'error') return render_template('loginError.html', title='Database error') try: conn = sqlite3.connect(dbname) data = pd.read_sql_query(""" select cmd, count(cmd) from process group by cmd """, conn) return render_template('analyzeLog.html', tables=[data.to_html()], titles = ['na', 'Main SQL'], LogFile='t1.log') except: flash('Error executing SQL Query', 'error') return render_template('loginError.html', title='Database SQL error') @app.route('/accessLog', methods=['GET', 'POST']) def accessLog(): form = AccessLog() app.logger.debug(form.errors) app.logger.debug('------ {0}'.format(request.form)) if form.is_submitted(): app.logger.debug(form.errors) app.logger.debug(form.errors) if form.validate_on_submit(): initUsers() email = form.email.data access_key = form.access_key.data user = User(email, access_key) if user.id not in users: flash('Unrecognised details', 'error') return render_template('loginError.html', title='Login Error') # if access_key in users[email]: # user = User(email, access_key) flask_login.login_user(user) return redirect(url_for('analyzeLog')) flash('Unknown access key', 'error') return render_template('loginError.html', title='Login Error') return render_template('accessLog.html', title='Access Log', form=form) @login_manager.unauthorized_handler def unauthorized_handler(): return render_template('loginError.html', title='Unauthorized User Error')
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#11 | 23765 | Robert Cowham | Moved things down one level to psla dir to make it easier to see what belongs | ||
#10 | 23762 | Robert Cowham | New graph working | ||
#9 | 23761 | Robert Cowham | Create a parameterised loop for graphs | ||
#8 | 23760 | Robert Cowham | Simple basics with Altair chart | ||
#7 | 23723 | Robert Cowham | First basic chart with plotly | ||
#6 | 23718 | Robert Cowham |
New statements. Time query execution |
||
#5 | 23714 | Robert Cowham | New queries | ||
#4 | 23712 | Robert Cowham | Proper formatting of table data | ||
#3 | 23711 | Robert Cowham | More or less working with multiple SQL statements | ||
#2 | 23705 | Robert Cowham | Basics working in simplified form | ||
#1 | 23704 | Robert Cowham | Save before simplifying |