Skip to content

Commit

Permalink
Logging slice and dash views
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Dec 4, 2015
1 parent aed2f46 commit f882ff1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
30 changes: 30 additions & 0 deletions panoramix/migrations/versions/315b3f4da9b0_adding_log_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""adding log model
Revision ID: 315b3f4da9b0
Revises: 1a48a5411020
Create Date: 2015-12-04 11:16:58.226984
"""

# revision identifiers, used by Alembic.
revision = '315b3f4da9b0'
down_revision = '1a48a5411020'

from alembic import op
import sqlalchemy as sa

def upgrade():
op.create_table('logs',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('action', sa.String(length=512), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('json', sa.Text(), nullable=True),
sa.Column('dttm', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['ab_user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.drop_table('birth_names')


def downgrade():
op.drop_table('logs')
13 changes: 12 additions & 1 deletion panoramix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sqlalchemy as sqla
from sqlalchemy import (
Column, Integer, String, ForeignKey, Text, Boolean, DateTime,
Table, create_engine, MetaData, desc, select, and_)
Table, create_engine, MetaData, desc, select, and_, func)
from sqlalchemy.orm import relationship
from sqlalchemy.sql import table, literal_column, text
from sqlalchemy.sql.elements import ColumnClause
Expand Down Expand Up @@ -885,6 +885,17 @@ def query(
duration=datetime.now() - qry_start_dttm)


class Log(Model):
__tablename__ = 'logs'

id = Column(Integer, primary_key=True)
action = Column(String(512))
user_id = Column(Integer, ForeignKey('ab_user.id'))
json = Column(Text)
user = relationship('User', backref='logs', foreign_keys=[user_id])
dttm = Column(DateTime, default=func.now())


class Metric(Model):
__tablename__ = 'metrics'
id = Column(Integer, primary_key=True)
Expand Down
24 changes: 24 additions & 0 deletions panoramix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hashlib
from sqlalchemy.types import TypeDecorator, TEXT
import json
from flask import g, request
import parsedatetime
import functools
from panoramix import db
Expand Down Expand Up @@ -178,3 +179,26 @@ def init():
table.perm for table in session.query(models.Datasource).all()]
for table_perm in table_perms:
merge_perm(sm, 'datasource_access', table.perm)


def log_this(f):
'''
Decorator to log user actions
'''
@functools.wraps(f)
def wrapper(*args, **kwargs):
user_id = None
if g.user:
user_id = g.user.id
from panoramix import models
log = models.Log(
action=f.__name__,
json=json.dumps(request.args.to_dict()),
user_id=user_id)

db.session.add(log)
db.session.commit()

return f(*args, **kwargs)

return wrapper
18 changes: 18 additions & 0 deletions panoramix/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
import json
import logging
import re
import traceback

from flask import request, redirect, flash, Response, render_template
Expand Down Expand Up @@ -228,6 +229,8 @@ class DashboardModelView(PanoramixModelView, DeleteMixin):
}
def pre_add(self, obj):
obj.slug = obj.slug.strip() or None
obj.slug = obj.slug.replace(" ", "-")
obj.slug = re.sub(r'\W+', '', obj.slug)

def pre_update(self, obj):
self.pre_add(obj)
Expand All @@ -241,6 +244,19 @@ def pre_update(self, obj):
category_icon='',)


class LogModelView(PanoramixModelView):
datamodel = SQLAInterface(models.Log)
list_columns = ('user', 'action', 'dttm')
edit_columns = ('user', 'action', 'dttm', 'json')
base_order = ('dttm','desc')

appbuilder.add_view(
LogModelView,
"Action Log",
category="Security",
icon="fa-list-ol")


class DatasourceModelView(PanoramixModelView, DeleteMixin):
datamodel = SQLAInterface(models.Datasource)
list_columns = [
Expand Down Expand Up @@ -285,6 +301,7 @@ class Panoramix(BaseView):
@has_access
@expose("/explore/<datasource_type>/<datasource_id>/")
@expose("/datasource/<datasource_type>/<datasource_id>/") # Legacy url
@utils.log_this
def explore(self, datasource_type, datasource_id):
if datasource_type == "table":
datasource = (
Expand Down Expand Up @@ -432,6 +449,7 @@ def testconn(self):

@has_access
@expose("/dashboard/<identifier>/")
@utils.log_this
def dashboard(self, identifier):
session = db.session()
qry = session.query(models.Dashboard)
Expand Down

0 comments on commit f882ff1

Please sign in to comment.