Skip to content

Commit

Permalink
Error handling and table links
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Feb 4, 2016
1 parent 50d7d0f commit 9c47415
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
14 changes: 13 additions & 1 deletion panoramix/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ def sqlalchemy_uri_decrypted(self):
conn.password = self.password
return str(conn)

@property
def sql_url(self):
return '/panoramix/sql/{}/'.format(self.id)

@property
def sql_link(self):
return '<a href="/panoramix/sql/{}/">SQL</a>'.format(self.id)
return '<a href="{}">SQL</a>'.format(self.sql_url)


class SqlaTable(Model, Queryable, AuditMixinNullable):
Expand Down Expand Up @@ -449,6 +453,14 @@ def query_bkp(
return QueryResult(
df=df, duration=datetime.now() - qry_start_dttm, query=sql)

@property
def sql_url(self):
return self.database.sql_url + "?table_id=" + str(self.id)

@property
def sql_link(self):
return '<a href="{}">SQL</a>'.format(self.sql_url)

def query(
self, groupby, metrics,
granularity,
Expand Down
1 change: 1 addition & 0 deletions panoramix/static/panoramix.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ var px = (function() {
editor.setValue(msg);
});
});
$("#select_star").click();
$("#run").click(function() {
$('#results').hide(0);
$('#loading').show(0);
Expand Down
10 changes: 9 additions & 1 deletion panoramix/templates/panoramix/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
border-radius: 5px;
background-color: #EEE;
}
div.alert {
padding: 5px;
margin: 0px;
}
.metadata {
overflow: auto;
width: 300px;
Expand All @@ -36,6 +40,7 @@
#results {
overflow: auto;
font-size: 12px;
margin-bottom: 5px;
}
#results table tbody tr td{
padding: 2px 4px;
Expand All @@ -56,7 +61,10 @@ <h2>db: [{{ db }}]</h2>
<div class="col-xs-5 fillheight">
<select id="dbtable">
{% for table in db.tables %}
<option value="{{ table.id }}">{{ table }}</option>
<option value="{{ table.id }}"
{{ "selected" if table.id|string == table_id }}>
{{ table }}
</option>
{% endfor %}
</select>
<button class="btn btn-default" id="select_star">SELECT *</button>
Expand Down
26 changes: 17 additions & 9 deletions panoramix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def pre_update(self, db):

class TableView(PanoramixModelView, DeleteMixin):
datamodel = SQLAInterface(models.SqlaTable)
list_columns = ['table_link', 'database', 'changed_by', 'changed_on_']
list_columns = [
'table_link', 'database', 'sql_link', 'changed_by_', 'changed_on_']
add_columns = ['table_name', 'database', 'default_endpoint', 'offset']
edit_columns = [
'table_name', 'is_featured', 'database', 'description', 'owner',
Expand Down Expand Up @@ -278,7 +279,7 @@ class DatasourceModelView(PanoramixModelView, DeleteMixin):
list_columns = [
'datasource_link', 'cluster', 'owner',
'created_by', 'created_on',
'changed_by', 'changed_on',
'changed_by_', 'changed_on',
'offset']
related_views = [ColumnInlineView, MetricInlineView]
edit_columns = [
Expand Down Expand Up @@ -561,6 +562,7 @@ def sql(self, database_id):
return self.render_template(
"panoramix/sql.html",
database_id=database_id,
table_id=request.args.get('table_id'),
db=mydb)

@has_access
Expand All @@ -579,7 +581,7 @@ def select_star(self, table_id):
t = db.session.query(models.SqlaTable).filter_by(id=table_id).first()
fields = ", ".join(
[c.column_name for c in t.columns if not c.expression] or "*")
s = "SELECT\n{fields}\nFROM {t.table_name}\nLIMIT 1000".format(**locals())
s = "SELECT\n{fields}\nFROM {t.table_name}".format(**locals())
return self.render_template(
"panoramix/ajah.html",
content=s)
Expand Down Expand Up @@ -608,12 +610,18 @@ def runsql(self):
.limit(limit)
)
sql= str(qry.compile(eng, compile_kwargs={"literal_binds": True}))
df = read_sql_query(sql=sql, con=eng)
content = df.to_html(
index=False,
classes=(
"dataframe table table-striped table-bordered "
"table-condensed sql_results"))
try:
df = read_sql_query(sql=sql, con=eng)
content = df.to_html(
index=False,
classes=(
"dataframe table table-striped table-bordered "
"table-condensed sql_results"))
except Exception as e:
content = (
'<div class="alert alert-danger">'
"{}</div>"
).format(e.message)
session.commit()
return content

Expand Down

0 comments on commit 9c47415

Please sign in to comment.