Skip to content

Commit

Permalink
Parse args for features / datasources of interest
Browse files Browse the repository at this point in the history
 * A start on #101
  • Loading branch information
arlolra committed Mar 16, 2016
1 parent c74af51 commit d927f01
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
3 changes: 2 additions & 1 deletion ores/score_processors/score_processor.py
Expand Up @@ -161,7 +161,8 @@ def _score(self, context, model, rev_ids, caches=None):
try:
score = self._process(context, model, cache)
scores[rev_id] = score
self._store(context, model, rev_id, score)
if caches is None:
self._store(context, model, rev_id, score)
except Exception as error:
scores[rev_id] = {'error': jsonify_error(error)}

Expand Down
30 changes: 17 additions & 13 deletions ores/wsgi/routes/v1/scores.py
Expand Up @@ -5,7 +5,7 @@

from ... import responses
from .... import errors
from ...util import ParamError, read_bar_split_param
from ...util import ParamError, read_bar_split_param, parse_features


def configure(config, bp, score_processor):
Expand Down Expand Up @@ -108,28 +108,32 @@ def score_revisions(context, model):
return jsonify(scores)

# /scores/enwiki/reverted/4567890
@bp.route("/scores/<context>/<model>/<int:rev_id>/", methods=["GET"])
@bp.route("/v1/scores/<context>/<model>/<int:rev_id>/", methods=["GET"])
@bp.route("/scores/<context>/<model>/<int:rev_id>/", methods=["GET", "POST"])
@bp.route("/v1/scores/<context>/<model>/<int:rev_id>/", methods=["GET", "POST"])
def score_revision(context, model, rev_id):

# Check to see if we have the context available in our score_processor
if context not in score_processor:
return responses.not_found("No models available for {0}"
.format(context))

precache = "precache" in request.args

# If the model exists, score revisions with it and return the result
if model not in score_processor[context]:
return responses.not_found("Model '{0}' not available for {1}."
.format(model, context))
else:
try:
scores = score_processor.score(context, model, [rev_id],
precache=precache)
except errors.ScoreProcessorOverloaded:
return responses.server_overloaded()

return jsonify(scores)
e, caches = parse_features(request)
if e is not None:
return responses.bad_request("Unabled to parse params: {0}"
.format(e))

precache = ("precache" in request.args) and (len(caches) == 0)

try:
scores = score_processor.score(context, model, [rev_id],
caches=caches, precache=precache)
except errors.ScoreProcessorOverloaded:
return responses.server_overloaded()

return jsonify(scores)

return bp
15 changes: 11 additions & 4 deletions ores/wsgi/routes/v2/scores.py
Expand Up @@ -5,7 +5,7 @@

from ... import responses
from .... import errors
from ...util import ParamError, format_output, read_bar_split_param
from ...util import ParamError, format_output, read_bar_split_param, parse_features


def configure(config, bp, score_processor):
Expand Down Expand Up @@ -136,15 +136,14 @@ def score_revisions_v2(context, model):
return format_output(context, scores, model_info)

# /v2/scores/enwiki/reverted/4567890
@bp.route("/v2/scores/<context>/<model>/<int:rev_id>/", methods=["GET"])
@bp.route("/v2/scores/<context>/<model>/<int:rev_id>/", methods=["GET", "POST"])
def score_revision_v2(context, model, rev_id):

# Check to see if we have the context available in our score_processor
if context not in score_processor:
return responses.not_found("No scorers available for {0}"
.format(context))

precache = "precache" in request.args
model_object = score_processor[context][model]
model_info = {model: {'version': model_object.version}}
scores = {}
Expand All @@ -167,9 +166,17 @@ def score_revision_v2(context, model, rev_id):
return responses.bad_request(
"Model '{0}' has no attribute {1}.".format(
model, req))

e, caches = parse_features(request)
if e is not None:
return responses.bad_request("Unabled to parse params: {0}"
.format(e))

precache = ("precache" in request.args) and (len(caches) == 0)

try:
scores = {model: score_processor.score(
context, model, [rev_id], precache=precache)}
context, model, [rev_id], caches=caches, precache=precache)}
except errors.ScoreProcessorOverloaded:
return responses.server_overloaded()

Expand Down
17 changes: 17 additions & 0 deletions ores/wsgi/util.py
Expand Up @@ -2,6 +2,8 @@

from flask.ext.jsonpify import jsonify

import json


class ParamError(Exception):
pass
Expand Down Expand Up @@ -61,3 +63,18 @@ def format_output(context, scores, model_info, warning=None, notice=None):
for rev_id in scores[model]:
output['scores'][context][model]['scores'][rev_id] = scores[model][rev_id]
return jsonify(output)


def parse_features(request):
"""Parse values for features / datasources of interest."""
caches = {}
try:
for k, v in request.values.items():
if k == "caches":
caches = json.loads(v)
break
elif k.startswith(("feature.", "datasource.")):
caches[k] = json.loads(v)
except Exception as e:
return e, None
return None, caches

0 comments on commit d927f01

Please sign in to comment.