Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the tests in the carapal. #1023

Merged
merged 3 commits into from
Aug 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions caravel/assets/javascripts/SqlLab/components/Timer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ class Timer extends React.Component {
}
stopwatch() {
if (this.props && this.props.query) {
let fromDttm = this.props.query.endDttm || new Date();
fromDttm = moment(fromDttm);
let duration = fromDttm - moment(this.props.query.startDttm).valueOf();
duration = moment.utc(duration);
let fromDttm = (this.props.query.endDttm) ? this.props.query.endDttm : new Date().getTime();
const since = (this.props.query.endDttm) ? this.props.query.endDttm : new Date().getTime();
const duration = moment.utc(since - this.props.query.startDttm);
const clockStr = duration.format('HH:mm:ss.SS');
this.setState({ clockStr });
if (this.props.query.state !== 'running') {
Expand Down
4 changes: 2 additions & 2 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,9 +1796,9 @@ class Query(Model):
executed_sql = Column(Text)
# Could be configured in the caravel config.
limit = Column(Integer)
limit_used = Column(Boolean)
limit_used = Column(Boolean, default=False)
select_as_cta = Column(Boolean)
select_as_cta_used = Column(Boolean)
select_as_cta_used = Column(Boolean, default=False)

# 1..100
progress = Column(Integer) # TODO should be float
Expand Down
12 changes: 7 additions & 5 deletions caravel/sql_lab.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import celery
from caravel import models, app, utils
from datetime import datetime
import pandas as pd
import logging
import sqlparse
from caravel import db, models, config
from caravel import app, db, models, utils


celery_app = celery.Celery(config_source=app.config.get('CELERY_CONFIG'))
Expand Down Expand Up @@ -55,8 +53,10 @@ def get_sql_results(query_id):
query.user_id,
query.start_time.strftime('%Y_%m_%d_%H_%M_%S'))
executed_sql = create_table_as(executed_sql, query.tmp_table_name)
query.select_as_cta_used = True
elif query.limit:
executed_sql = database.wrap_sql_limit(executed_sql, query.limit)
query.limit_used = True
engine = database.get_sqla_engine(schema=query.schema)
try:
query.executed_sql = executed_sql
Expand All @@ -66,6 +66,7 @@ def get_sql_results(query_id):
query.error_message = utils.error_msg_from_exception(e)
query.status = models.QueryStatus.FAILED
query.tmp_table_name = None
db.session.commit()
raise Exception(query.error_message)

cursor = result_proxy.cursor
Expand Down Expand Up @@ -106,7 +107,8 @@ def get_sql_results(query_id):

# CTAs queries result in 1 cell having the # of the added rows.
if query.select_as_cta:
query.select_sql = database.select_star(query.tmp_table_name, query.limit)
query.select_sql = '{}'.format(database.select_star(
query.tmp_table_name, limit=query.limit))

query.end_time = datetime.now()
db.session.commit()
Expand All @@ -119,5 +121,5 @@ def get_sql_results(query_id):
payload['data'] = data
payload['columns'] = columns
else:
payload['error'] = query.error_message
payload['error'] = query.error_message
return payload
26 changes: 9 additions & 17 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,6 @@ def json_error_response(msg, status=None):
limit=int(app.config.get('SQL_MAX_ROW', None)),
sql=sql,
schema=request.form.get('schema'),
# TODO(bkyryliuk): consider it being DB property.
select_as_cta=request.form.get('select_as_cta') == 'true',
start_time=datetime.now(),
status=models.QueryStatus.IN_PROGRESS,
Expand All @@ -1458,37 +1457,30 @@ def json_error_response(msg, status=None):
mimetype="application/json")

# Sync request.
data = {}
try:
data = sql_lab.get_sql_results(query_id)
except Exception as e:
logging.exception(e)
return Response(
json.dumps({'error': "{}".format(e)}),
status=500,
mimetype="application/json"
)

return Response(json.dumps({'error': "{}".format(e)}),
status=500,
mimetype = "application/json")
data['query'] = query.to_dict()

return Response(
json.dumps(data, default=utils.json_int_dttm_ser, allow_nan=False),
status=200,
mimetype="application/json")
mimetype = "application/json")

@has_access
@expose("/csv/<query_id>")
@log_this
def csv(self, query_id):
"""Get the updated queries."""
"""Download the query results as csv."""
s = db.session()
query = s.query(models.Query).filter_by(id=int(query_id)).first()

if not (self.can_access('all_datasource_access', 'all_datasource_access') or
self.can_access('database_access', query.database.perm)):
flash(_(
"SQL Lab requires the `all_datasource_access` "
"or specific DB permission"))
"SQL Lab requires the `all_datasource_access` or specific DB permission"))
redirect('/')

sql = query.select_sql or query.sql
Expand All @@ -1512,19 +1504,19 @@ def queries(self, last_updated_ms):
mimetype="application/json")

# Unix time, milliseconds.
last_updated_ms = last_updated_ms or 0
last_updated_ms_int = int(last_updated_ms) if last_updated_ms else 0

# Local date time, DO NOT USE IT.
# last_updated_dt = datetime.fromtimestamp(int(last_updated_ms) / 1000)

# UTC date time, same that is stored in the DB.
last_updated_dt = utils.EPOCH + timedelta(
seconds=int(last_updated_ms) / 1000)
seconds=last_updated_ms_int / 1000)

sql_queries = (
db.session.query(models.Query)
.filter(
models.Query.user_id == g.user.get_id() and
models.Query.user_id == g.user.get_id() or
models.Query.changed_on >= last_updated_dt
)
.all()
Expand Down