Skip to content

Commit 5a590db

Browse files
committed
Use gear id instead of name (alg) in rules
1 parent 81fa7fb commit 5a590db

File tree

11 files changed

+62
-85
lines changed

11 files changed

+62
-85
lines changed

api/jobs/gears.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import datetime
99
from jsonschema import Draft4Validator, ValidationError
1010
import gears as gear_tools
11-
import pymongo
1211

1312
from .. import config
1413
from .jobs import Job
@@ -39,17 +38,10 @@ def get_gears():
3938
return map(lambda x: x['original'], cursor)
4039

4140
def get_gear(_id):
42-
return config.db.gears.find_one({'_id': bson.ObjectId(_id)})
43-
44-
def get_gear_by_name(name):
45-
46-
# Find a gear from the list by name
47-
gear_doc = list(config.db.gears.find({'gear.name': name}).sort('created', pymongo.DESCENDING))
48-
49-
if len(gear_doc) == 0 :
50-
raise APINotFoundException('Unknown gear ' + name)
51-
52-
return gear_doc[0]
41+
gear = config.db.gears.find_one({'_id': bson.ObjectId(_id)})
42+
if gear is None:
43+
raise APINotFoundException('Cannot find gear {}'.format(_id))
44+
return gear
5345

5446
def get_invocation_schema(gear):
5547
return gear_tools.derive_invocation_schema(gear['gear'])

api/jobs/handlers.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from ..auth.apikeys import JobApiKey
2626

27-
from .gears import validate_gear_config, get_gears, get_gear, get_invocation_schema, remove_gear, upsert_gear, suggest_container, get_gear_by_name, check_for_gear_insertion
27+
from .gears import validate_gear_config, get_gears, get_gear, get_invocation_schema, remove_gear, upsert_gear, suggest_container, check_for_gear_insertion
2828
from .jobs import Job, Logs
2929
from .batch import check_state, update
3030
from .queue import Queue
@@ -181,10 +181,7 @@ def post(self, cid):
181181

182182
validate_data(doc, 'rule-new.json', 'input', 'POST', optional=True)
183183
validate_regexes(doc)
184-
try:
185-
get_gear_by_name(doc['alg'])
186-
except APINotFoundException:
187-
self.abort(400, 'Cannot find gear for alg {}, alg not valid'.format(doc['alg']))
184+
get_gear(doc['gear_id'])
188185

189186
doc['project_id'] = cid
190187

@@ -234,11 +231,8 @@ def put(self, cid, rid):
234231
updates = self.request.json
235232
validate_data(updates, 'rule-update.json', 'input', 'POST', optional=True)
236233
validate_regexes(updates)
237-
if updates.get('alg'):
238-
try:
239-
get_gear_by_name(updates['alg'])
240-
except APINotFoundException:
241-
self.abort(400, 'Cannot find gear for alg {}, alg not valid'.format(updates['alg']))
234+
if updates.get('gear_id'):
235+
get_gear(updates['gear_id'])
242236

243237
doc.update(updates)
244238
config.db.project_rules.replace_one({'_id': bson.ObjectId(rid)}, doc)

api/jobs/rules.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,25 @@ def eval_rule(rule, file_, container):
138138

139139
return True
140140

141-
def queue_job_legacy(algorithm_id, input_):
141+
def queue_job_legacy(gear_id, input_):
142142
"""
143143
Tie together logic used from the no-manifest, single-file era.
144144
Takes a single FileReference instead of a map.
145145
"""
146146

147-
gear = gears.get_gear_by_name(algorithm_id)
147+
gear = gears.get_gear(gear_id)
148148

149149
if len(gear['gear']['inputs']) != 1:
150-
raise Exception("Legacy gear enqueue attempt of " + algorithm_id + " failed: must have exactly 1 input in manifest")
150+
raise Exception("Legacy gear enqueue attempt of " + gear_id + " failed: must have exactly 1 input in manifest")
151151

152152
input_name = gear['gear']['inputs'].keys()[0]
153153

154154
inputs = {
155155
input_name: input_
156156
}
157157

158-
job = Job(str(gear['_id']), inputs, tags=['auto', algorithm_id])
158+
gear_tag = gear['gear']['name'] + '-' + gear_id
159+
job = Job(str(gear['_id']), inputs, tags=['auto', gear_tag])
159160
return job
160161

161162
def find_type_in_container(container, type_):
@@ -184,22 +185,23 @@ def create_potential_jobs(db, container, container_type, file_):
184185

185186
if 'from_failed_job' not in file_ and eval_rule(rule, file_, container):
186187

187-
alg_name = rule['alg']
188+
gear_id = rule['gear_id']
189+
gear = gears.get_gear(gear_id)
190+
gear_tag = gear['gear']['name'] + '-' + gear_id
188191

189192
if rule.get('match') is None:
190193
input_ = FileReference(type=container_type, id=str(container['_id']), name=file_['name'])
191-
job = queue_job_legacy(alg_name, input_)
194+
job = queue_job_legacy(gear_id, input_)
192195
else:
193196
inputs = { }
194197

195198
for input_name, match_type in rule['match'].iteritems():
196199
match = find_type_in_container(container, match_type)
197200
if match is None:
198-
raise Exception("No type " + match_type + " found for alg rule " + alg_name + " that should have been satisfied")
201+
raise Exception("No type " + match_type + " found for alg rule " + gear_tag + " that should have been satisfied")
199202
inputs[input_name] = FileReference(type=container_type, id=str(container['_id']), name=match['name'])
200203

201-
gear = gears.get_gear_by_name(alg_name)
202-
job = Job(str(gear['_id']), inputs, tags=['auto', alg_name])
204+
job = Job(str(gear['_id']), inputs, tags=['auto', gear_tag])
203205

204206
potential_jobs.append({
205207
'job': job,
@@ -248,7 +250,7 @@ def create_jobs(db, container_before, container_after, container_type):
248250
job_map = pj['job'].map()
249251
Queue.enqueue_job(job_map, origin)
250252

251-
spawned_jobs.append(pj['rule']['alg'])
253+
spawned_jobs.append(pj['rule']['gear_id'])
252254

253255
return spawned_jobs
254256

raml/examples/input/rule-new.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"_id": "5a12f2923306be0016179f47",
33
"project_id": "57e452791cff88b85f9f9c97",
4-
"alg": "dcm2niix",
4+
"gear_id": "580925ce9e512c57dc8a103c",
55
"name": "dcm2niix",
66
"all": [
77
{

raml/examples/output/rule-list.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"alg": "dcm2niix",
3+
"gear_id": "580925ce9e512c57dc8a103b",
44
"all": [
55
{
66
"regex": true,
@@ -17,7 +17,7 @@
1717
"any": []
1818
},
1919
{
20-
"alg": "dicom-mr-classifier",
20+
"gear_id": "580925ce9e512c57dc8a103c",
2121
"all": [
2222
{
2323
"type": "file.type",
@@ -34,7 +34,7 @@
3434
"any": []
3535
},
3636
{
37-
"alg": "mriqc",
37+
"gear_id": "580925ce9e512c57dc8a103d",
3838
"all": [
3939
{
4040
"type": "file.type",

raml/examples/rules_list.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"alg" : "dicom_mr_classifier",
3+
"gear_id": "580925ce9e512c57dc8a103c",
44
"all" : [
55
[
66
"file.type",

raml/schemas/definitions/rule.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"properties": {
3030
"_id": { "type": "string" },
3131
"project_id": { "type": "string" },
32-
"alg": { "type": "string" },
32+
"gear_id": { "type": "string" },
3333
"name": { "type": "string" },
3434
"any": { "$ref": "#/definitions/rule-items" },
3535
"all": { "$ref": "#/definitions/rule-items" },
@@ -42,7 +42,7 @@
4242
"type": "object",
4343
"properties": {
4444
"_id": { "type": "string" },
45-
"alg": { "type": "string" },
45+
"gear_id": { "type": "string" },
4646
"name": { "type": "string" },
4747
"any": { "$ref": "#/definitions/rule-items" },
4848
"all": { "$ref": "#/definitions/rule-items" },

raml/schemas/input/rule-new.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"title": "Rule",
44
"type": "object",
55
"allOf": [{"$ref": "../definitions/rule.json#/definitions/rule-input"}],
6-
"required": ["alg", "name", "any", "all"]
6+
"required": ["gear_id", "name", "any", "all"]
77
}

tests/integration_tests/python/test_jobs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_jobs(data_builder, default_payload, as_public, as_user, as_admin, as_ro
6060
job0 = copy.deepcopy(job_data)
6161
job0['gear_id'] = '000000000000000000000000'
6262
r = as_admin.post('/jobs/add', json=job0)
63-
assert r.status_code == 400
63+
assert r.status_code == 404
6464

6565
# add job with explicit destination
6666
r = as_admin.post('/jobs/add', json=job_data)
@@ -353,15 +353,14 @@ def test_failed_job_output(data_builder, default_payload, as_user, as_admin, as_
353353
}
354354
gear = data_builder.create_gear(gear=gear_doc)
355355
gear2 = data_builder.create_gear()
356-
gear2_name = as_admin.get('/gears/' + gear2).json()['gear']['name']
357356
project = data_builder.create_project()
358357
session = data_builder.create_session()
359358
acquisition = data_builder.create_acquisition()
360359
assert as_admin.post('/acquisitions/' + acquisition + '/files', files=file_form('test.zip')).ok
361360

362361
# create rule for text files
363362
r = as_admin.post('/projects/' + project + '/rules', json={
364-
'alg': gear2_name,
363+
'gear_id': gear2,
365364
'name': 'text-trigger',
366365
'any': [],
367366
'all': [{'type': 'file.type', 'value': 'text'}]

0 commit comments

Comments
 (0)