diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..085c4fe --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*.db diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/create_tables.py b/create_tables.py new file mode 100644 index 0000000..a3d3e4d --- /dev/null +++ b/create_tables.py @@ -0,0 +1,3 @@ +from systemview import db + +db.create_all() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ea840eb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +Flask==0.10.1 +Flask-SQLAlchemy==1.0 +Jinja2==2.7.2 +MarkupSafe==0.18 +MySQL-python==1.2.5 +SQLAlchemy==0.9.2 +Werkzeug==0.9.4 +argparse==1.2.1 +itsdangerous==0.23 diff --git a/systemview.py b/systemview.py index 0b3a8c9..e458a3a 100644 --- a/systemview.py +++ b/systemview.py @@ -1,6 +1,7 @@ # all the imports from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash +from flask.ext.sqlalchemy import SQLAlchemy import subprocess import sys import argparse @@ -18,6 +19,28 @@ # create the application app = Flask(__name__) +# tell the app where our db is. +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://vagrant:password@localhost/systemview' + +# set up the db through SQLAlchemy +db = SQLAlchemy(app) +db.init_app(app) + + +# declare our Search model to hold previous search terms. +class Search(db.Model): + # tell the model what to name the table + __tablename__ = 'searches' + + # this is where we declare the types of our database columns + # and relationships between tables + term = db.Column(db.String(100), primary_key=True) + + # this defines what to do, and which parameters to provide when + # creating a Search object. + def __init__(self, term): + self.term = term + """ @app.route is a decorator - it tells the app what URL to respond to @@ -33,18 +56,22 @@ def show_form(): # first, let's get the host name of this vm to display on the form hostname = get_hostname() + # Now let's get all the previous searches + searches = Search.query.all() + # render the search template and send it the hostname we found - return render_template('proc_search.html', hostname=hostname) + return render_template('proc_search.html', hostname=hostname, + searches=searches) @app.route('/procs/', methods=['POST']) -def show_procs(): +def show_procs(term=None): # this method gets the system processes matching the search term # let's get the host name of this vm to display on the page hostname = get_hostname() - # first, get the search term out of the POST data + # get the search term out of the POST data term = request.form['term'] # now lets get all the system processes @@ -60,8 +87,23 @@ def show_procs(): procs.append(line) count += 1 + # check to see if the term is already in the db + search = Search.query.filter_by(term=term).first() + + # check to see that we entered a term, and it's not in the db + if term and not search: + + # create an object from our search term + search = Search(term) + + # add and commit it to the db + db.session.add(search) + db.session.commit() + + searches = Search.query.all() + # render the search template, this time with our data - return render_template('proc_search.html', + return render_template('proc_search.html', searches=searches, procs=procs, count=count, hostname=hostname) diff --git a/templates/proc_search.html b/templates/proc_search.html index f1120af..cd8c55d 100644 --- a/templates/proc_search.html +++ b/templates/proc_search.html @@ -21,4 +21,17 @@