Skip to content

Commit

Permalink
fix valueset expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
niquola committed Mar 17, 2016
1 parent 07b4419 commit 613aaf4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/fhir/terminology.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
utils = require('../core/utils')
compat = require('../compat')
outcome = require('./outcome')

exports.fhir_expand_valueset = (plv8, query)->
Expand Down Expand Up @@ -124,6 +125,17 @@ exports.fhir_valueset_after_changed = (plv8, resource)->
for inc in ((resource.compose && resource.compose.include) || [])
for concept in (inc.concept || [])
_create_concept(plv8, acc, {valueset_id: resource.id, system: inc.system}, {}, concept)
if inc.system
plv8.debug = true
syst = utils.exec plv8,
select: [':*']
from: ':codesystem'
where: ['$eq', ":resource->>'url'", inc.system]
limit: 1
syst_res = (syst[0] && syst[0].resource && JSON.parse(syst[0].resource))
for concept in ((syst_res && syst_res.concept) || [])
_create_concept(plv8, acc, {valueset_id: resource.id, system: inc.system}, {}, concept)
plv8.debug = false

res = plv8.execute """
INSERT INTO _valueset_expansion
Expand Down
26 changes: 25 additions & 1 deletion test/fhir/terminology_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
term = require('../../src/fhir/terminology')
crud = require('../../src/fhir/crud')
schema = require('../../src/core/schema')
test = require('../helpers.coffee')
plv8 = require('../../plpl/src/plv8')

Expand All @@ -8,6 +9,19 @@ log = (x)-> console.log(JSON.stringify(x, null, " "))

expand = (q)-> term.fhir_expand_valueset(plv8, q)

TEST_CS = {
id: 'mytestvs'
url: '/mycodesystem'
resourceType: 'CodeSystem'
concept: [
{
code: 'b'
display: 'b'
concept: [{code: 'b1', display: 'b1'}]
}
]
}

TEST_VS = {
id: 'mytestvs'
resourceType: 'ValueSet'
Expand All @@ -34,18 +48,23 @@ TEST_VS = {
{code: 'a31', display: 'A31'}
{code: 'a32', display: 'A32'}]
}
{system: '/mycodesystem'}
]
}


big_valueset = require('./fixtures/valueset-ucum-common.json')
big_valueset.id = 'bigone'

describe "terminology", ->
@timeout(50000)

before (done)->
schema.fhir_create_storage(plv8, {resourceType: 'CodeSystem'})
crud.fhir_terminate_resource(plv8, {resourceType: 'ValueSet', id: TEST_VS.id})
crud.fhir_terminate_resource(plv8, {resourceType: 'ValueSet', id: big_valueset.id})
crud.fhir_terminate_resource(plv8, {resourceType: 'CodeSystem', id: TEST_CS.id})
crud.fhir_create_resource(plv8, {resourceType: 'CodeSystem', allowId: true, resource: TEST_CS})
crud.fhir_create_resource(plv8, {resourceType: 'ValueSet', allowId: true, resource: TEST_VS})
done()

Expand All @@ -61,7 +80,7 @@ describe "terminology", ->
it "custom vs", ->
vs = expand(id: "mytestvs")
res = vs.expansion.contains.map((x)-> x.code).sort()
assert.deepEqual([ 'a1', 'a21', 'a22', 'a31', 'a32', 'nested' ], res)
assert.deepEqual([ 'a1', 'a21', 'a22', 'a31', 'a32', 'b', 'b1', 'nested' ], res)

vs = expand(id: "mytestvs", filter: '32')
res = vs.expansion.contains
Expand All @@ -75,6 +94,11 @@ describe "terminology", ->
res = vs.expansion.contains
assert.equal(res.length, 1)

vs = expand(id: "mytestvs", filter: 'b1')
res = vs.expansion.contains
console.log(res)
assert.equal(res.length, 1)

crud.fhir_update_resource plv8,
resource:
resourceType: 'ValueSet'
Expand Down
3 changes: 2 additions & 1 deletion utils/generate_migrations.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ output schema.fhir_create_storage_sql(plv8, resourceType: 'SearchParameter')
output schema.fhir_create_storage_sql(plv8, resourceType: 'OperationDefinition')
output schema.fhir_create_storage_sql(plv8, resourceType: 'ValueSet')
output schema.fhir_create_storage_sql(plv8, resourceType: 'ConceptMap')
output schema.fhir_create_storage_sql(plv8, resourceType: 'CodeSystem')
output schema.fhir_create_storage_sql(plv8, resourceType: 'NamingSystem')

bundles = [
Expand Down Expand Up @@ -100,7 +101,7 @@ console.log """
);
"""

for tp in ['StructureDefinition', 'SearchParameter', 'OperationDefinition', 'ValueSet', 'ConceptMap', 'NamingSystem']
for tp in ['StructureDefinition', 'SearchParameter', 'OperationDefinition', 'ValueSet', 'ConceptMap', 'NamingSystem', 'CodeSystem']
console.log """
INSERT INTO #{tp.toLowerCase()} (id, version_id, resource)
SELECT m.resource->>'id', m.resource->>'id' || '-#{VERSION}' , resource
Expand Down
17 changes: 13 additions & 4 deletions utils/patch_3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ DO $JS$
for(var j = 0; j < (include.concept && include.concept.length) || 0; j++){
expand_concept(acc, props, {}, include.concept[j]);
}
var syst = plv8.execute("SELECT * FROM codesystem WHERE resource->>'url' = $1 LIMIT 1", include.system);
var syst_res = syst && syst[0] && syst[0].resource && parse(syst[0].resource);
/* if(syst_res && syst_res.concept.length > 0){ */
/* log("+ " + include.system + ' (' + syst_res.concept.length + ')'); */
/* } */
for(var j = 0; j < (syst_res && syst_res.concept.length) || 0; j++){
expand_concept(acc, props, {}, syst_res.concept[j]);
}
}
}
return acc;
Expand All @@ -74,15 +82,16 @@ DO $JS$
var cursor = plan.cursor();
var row = null;
while (row = cursor.fetch()) {
var res = expand(parse(row.resource))
if(res.length > 0){
log('expand valueset: ' + row.resource.id + ' (' + res.length + ')')
var res = parse(row.resource)
var items = expand(res);
if(items.length > 0){
log('expand valueset: ' + res.id + ' (' + items.length + ')')
plv8.execute(
"INSERT INTO _valueset_expansion" +
"(valueset_id, system, parent_code, code, display, abstract, definition, designation, extension) " +
"SELECT x->>'valueset_id', x->>'system', x->>'parent_code', x->>'code', x->>'display', (x->>'abstract')::boolean, x->>'definition', (x->'designation')::jsonb, (x->'extension')::jsonb " +
"FROM json_array_elements($1::json) x"
, JSON.stringify(res))
, JSON.stringify(items))
}
}
cursor.close();
Expand Down

0 comments on commit 613aaf4

Please sign in to comment.