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

tickets/dm-2095 #1

Merged
merged 6 commits into from
Mar 4, 2015
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
16 changes: 10 additions & 6 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ sudo aptitude install python-flask
python bin/dbServer.py

# and run some queries:
curl http://localhost:5000/db
curl http://localhost:5000/db/v0
curl http://localhost:5000/db/v0/query
curl http://localhost:5000/db/v0/query?sql='show+databases+like+"%Stripe%"'
curl http://localhost:5000/db/v0/query?sql=SELECT+COUNT\(*\)+FROM+jacek_1mRows.Source
curl http://localhost:5000/db/v0/query?sql=SELECT+deepSourceId+FROM+jacek_1mRows.Source+limit+10
curl 'http://localhost:5000/db'
curl 'http://localhost:5000/db/v0'
curl 'http://localhost:5000/db/v0/query'
curl 'http://localhost:5000/db/v0/query?sql=SHOW+DATABASES+LIKE+"%Stripe%"'
curl 'http://localhost:5000/db/v0/query?sql=SHOW+TABLES+IN+DC_W13_Stripe82'
curl 'http://localhost:5000/db/v0/query?sql=DESCRIBE+DC_W13_Stripe82.DeepForcedSource'
curl 'http://localhost:5000/db/v0/query?sql=DESCRIBE+DC_W13_Stripe82.Science_Ccd_Exposure'
curl 'http://localhost:5000/db/v0/query?sql=SELECT+deepForcedSourceId,scienceCcdExposureId+FROM+DC_W13_Stripe82.DeepForcedSource+LIMIT+10'
curl 'http://localhost:5000/db/v0/query?sql=SELECT+ra,decl,filterName+FROM+DC_W13_Stripe82.Science_Ccd_Exposure+WHERE+scienceCcdExposureId=125230127'
curl 'http://localhost:5000/image/v0/raw/cutout?ra=7.90481567257&dec=-0.299951669961&filter=r&width=30.0&height=45.0'
9 changes: 8 additions & 1 deletion bin/dbServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
"""

from flask import Flask, request
from lsst.dbserv import dbREST_v0
import json
import logging as log
import sys

from lsst.dbserv import dbREST_v0

app = Flask(__name__)

@app.route('/')
Expand All @@ -53,6 +55,11 @@ def getDb():
app.register_blueprint(dbREST_v0.dbREST, url_prefix='/db/v0')

if __name__ == '__main__':
log.basicConfig(
format='%(asctime)s %(name)s %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %I:%M:%S',
level=log.DEBUG)

try:
app.run(debug=True)
except Exception, e:
Expand Down
52 changes: 15 additions & 37 deletions python/lsst/dbserv/dbREST_v0.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,18 @@

from flask import Blueprint, request
import json
import MySQLdb

from lsst.db.utils import readCredentialFile
import lsst.log as log
from lsst.db.db import Db


dbREST = Blueprint('dbREST', __name__, template_folder='dbserv')

# connect to the database
creds = readCredentialFile("~/.lsst/dbAuth-dbServ.txt", log)
try:
con = MySQLdb.connect(host=creds['host'],
port=int(creds['port']),
user=creds['user'],
passwd=creds['passwd'])
db = Db(read_default_file="~/.lsst/dbAuth-dbServ.txt")

except MySQLdb.Error as err:
log.info("ERROR MySQL %s", err)
raise

def runDbQuery1(query, optTuple=None):
'''Runs query that returns one row.'''
cursor = con.cursor()
if optTuple:
cursor.execute(query, optTuple)
else:
cursor.execute(query)
row = cursor.fetchone()
def runDbQuery1(query, optParams=None):
'''Runs query that returns one row. Returns properly formatted result.'''
row = db.execCommand1(query, optParams)
fmt = request.accept_mimetypes.best_match(['application/json', 'text/html'])
retStr = ''
if row:
Expand All @@ -72,26 +56,21 @@ def runDbQuery1(query, optTuple=None):
retStr += "%s:%s " % (cursor.description[x][0], row[x])
if fmt == "application/json":
retStr = json.dumps(retStr)
cursor.close()
return retStr

def runDbQueryM(query, optTuple=None):
'''Runs query that returns many rows.'''
cursor = con.cursor()
if optTuple:
cursor.execute(query, optTuple)
else:
cursor.execute(query)
rows = cursor.fetchall()
def runDbQueryM(query, optParams=None):
'''Runs query that returns many rows. Returns properly formatted result.'''
rows = db.execCommandN(query, optParams)
fmt = request.accept_mimetypes.best_match(['application/json', 'text/html'])
retStr = ''
if len(rows) > 0:
if fmt == 'text/html':
retStr = "<br />".join(str(r[0]) for r in rows)
for row in rows:
for c in row:
retStr += "%s " % c
retStr += "<br><br>"
else: # default format is application/json
ret = " ".join(str(r[0]) for r in rows)
retStr = json.dumps(ret)
cursor.close()
retStr = json.dumps(rows)
return retStr

@dbREST.route('/', methods=['GET'])
Expand All @@ -107,9 +86,8 @@ def getQuery():
'''If sql is not passed, it lists quries running for a given user.
If sql is passed, it runs a given query.'''
if 'sql' in request.args:
sql = request.args.get('sql')
# I'm sure we will want to do some validation here, etc, this is
# a very first version
sql = request.args.get('sql').encode('utf8')
# TODO: query validation. See DM-2138
return runDbQueryM(sql)
else:
return "Listing queries is not implemented."