-
Notifications
You must be signed in to change notification settings - Fork 1
/
cedar_api.rb
379 lines (333 loc) · 12.6 KB
/
cedar_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# frozen_string_literal: true
require 'cgi'
require 'csv'
require 'fhir_models'
require 'json'
require 'sinatra'
require 'sinatra/namespace'
require 'sinatra/cross_origin'
require 'sinatra/required_params'
require_relative 'util/cedar_logger'
require_relative 'database/models'
require_relative 'fhir/fhir_adapter'
require_relative 'util/citation_filter'
require_relative 'util/search_parser'
configure do
# Support cross-origin requests to allow JavaScript-based UIs hosted on different servers
enable :cross_origin
set :logger, CedarLogger.logger
end
get '/' do
"Artifact count: #{Artifact.count}"
end
get '/demo' do
content_type 'text/html'
<<~DEMO_FORM
<form action="fhir/Citation" method="get">
<label for="_content">Search Text:</label>
<input type="text" id="_content" name="_content">
<input type="hidden" id="artifact-current-state" name="artifact-current-state" value="active">
<button type="submit">Search</button>
</form>
<form action="fhir/Citation" method="get">
<label for="Last Updated">Last Updated:</label>
<input type="text" id="_lastUpdated" name="_lastUpdated">
<input type="hidden" id="artifact-current-state" name="artifact-current-state" value="active">
<button type="submit">Search</button>
</form>
<form action="fhir/Citation" method="get">
<label for="keyword">Search Keywords:</label>
<input type="text" id="classification:text" name="classification:text">
<input type="hidden" id="artifact-current-state" name="artifact-current-state" value="active">
<button type="submit">Search</button>
</form>
<form action="fhir/Citation" method="get">
<label for="keyword">Search Concepts:</label>
<input type="text" id="classification" name="classification">
<input type="hidden" id="artifact-current-state" name="artifact-current-state" value="active">
<button type="submit">Search</button>
</form>
<form action="fhir/Citation" method="get">
<label for="title">Search Title:</label>
<input type="text" id="title" name="title:contains">
<input type="hidden" id="artifact-current-state" name="artifact-current-state" value="active">
<button type="submit">Search</button>
</form>
DEMO_FORM
end
get '/suggestions' do
required_params :term
term = params[:term]
begin
result_names = if term.empty?
[]
else
MeshTreeNode.similar_to_name(term).collect { |r| r[:name] }
end
content_type 'application/json'
{ suggestions: result_names, term: term }.to_json
rescue StandardError => e
logger.error "Suggestions error: #{e.full_message}"
content_type 'text/plain'
status 500
return 'Error finding suggestions.'
end
end
get '/redirect/:id' do
id = params[:id]
artifact = Artifact.first(cedar_identifier: id)
if artifact.nil?
logger.info "Redirect for unknown artifact (#{id})"
halt(404)
elsif artifact.url.nil?
logger.info "Redirect for retracted artifact (#{id})"
halt(404)
end
search_log = SearchLog[params[:search]]
if search_log.nil?
logger.info "Redirect for artifact (#{id}) but search log #{params[:search]} not found"
else
search_log.link_clicks ||= []
search_log.link_clicks << { artifact_id: artifact.id, position: params[:result].to_i, referrer: request.referrer }
repo_result = search_log.repository_results[artifact.repository_id.to_s]
if repo_result.nil?
logger.info "Redirect for artifact (#{id}) but artifact repository not in search log #{params[:search]}"
else
repo_result['clicked'] ||= 0
repo_result['clicked'] += 1
end
search_log.save
end
redirect artifact.url
end
get '/csv' do
multiple_and_parameters = CGI.parse(request.query_string).select do |k, _v|
CitationFilter::MULTIPLE_AND_PARAMETERS.include?(k)
end
request_url = "#{request.scheme}://#{request.host}:#{request.port}#{request.path}"
redirect_base_url = uri('redirect').to_s
filter = CitationFilter.new(params: params.merge(multiple_and_parameters),
artifact_base_url: uri('fhir/Citation').to_s,
redirect_base_url: redirect_base_url,
request_url: request_url,
client_ip: request.ip,
client_id: request.env['HTTP_FROM'],
log_to_db: true)
begin
artifacts = filter.all_artifacts
rescue StandardError => e
logger.error "Search error: #{e.full_message}"
content_type 'text/plain'
status 500
return 'Error executing query.'
end
content_type 'text/csv'
attachment "cedar_#{Time.now.strftime('%Y%m%d-%H%M%S')}.csv"
stream do |out|
out << CSV.generate_line(
%w[Repository Title Description Keywords UMLS MeSH SNOMED-CT ICD10CM RXNORM Status Published Link]
)
artifacts.each_with_index do |artifact, result_index|
truncated_description = artifact.description
if truncated_description && truncated_description.size > 300
truncated_description = "#{truncated_description[..300]}..."
end
out << CSV.generate_line([artifact.repository.alias,
artifact.title,
truncated_description,
artifact.keywords.join('; '),
stringify(artifact.concepts, 'UMLS'),
stringify(artifact.concepts, 'MSH'),
stringify(artifact.concepts, 'SNOMEDCT_US'),
stringify(artifact.concepts, 'ICD10CM'),
stringify(artifact.concepts, 'RXNORM'),
artifact.artifact_status,
artifact.published_on,
if FHIRAdapter::ARTIFACT_URL_CLICK_LOGGING
"#{redirect_base_url}/#{artifact.cedar_identifier}?result=#{result_index}"
else
artifact.url
end])
end
end
end
def stringify(concepts, code_system)
if code_system == 'UMLS'
concepts.map { |concept| "#{concept.umls_cui} (#{concept.umls_description})" }.join('; ')
else
concept_codes = concepts.map do |concept|
codes = concept.codes.select do |code|
code['system'] == code_system
end
codes.map do |code|
"#{code['code']} (#{code['description']})"
end.join('; ')
end
concept_codes.select { |str| str&.size&.positive? }.join('; ')
end
end
not_found do
logger.info "Request for unknown URL (#{request.url})"
'Not found'
end
namespace '/fhir' do
before do
content_type 'application/fhir+json; charset=utf-8'
response.headers['Access-Control-Allow-Origin'] = '*'
end
get '/metadata' do
json = File.read('resources/capabilitystatement.json')
cs = FHIR.from_contents(json)
cs.to_json
end
get '/SearchParameter/?:id?' do
read_resource_from_file('SearchParameter', params)
end
get '/OperationDefinition/?:id?' do
read_resource_from_file('OperationDefinition', params)
end
get '/StructureDefinition/?:id?' do
read_resource_from_file('StructureDefinition', params)
end
get '/Organization' do
bundle = FHIRAdapter.create_organization_bundle(Repository.all)
uri = Addressable::URI.parse("#{request.scheme}://#{request.host}:#{request.port}#{request.path}")
bundle.link << FHIR::Bundle::Link.new(
{
relation: 'self',
url: uri.normalize.to_str
}
)
bundle.to_json
end
get '/Organization/:id' do
id = params[:id]
repository = Repository.first(fhir_id: id)
if repository.nil?
logger.info "Request for unknown repository id (#{id})"
halt(404)
end
citation = FHIRAdapter.create_organization(repository)
citation.to_json
end
get '/Citation/$get-artifact-types' do
output = Artifact.exclude(artifact_type: '').order(:artifact_type).distinct(:artifact_type).select(:artifact_type)
output = FHIRAdapter.create_artifact_types_output(output)
output.to_json
end
get '/Citation/:id' do
id = params[:id]
artifact = Artifact.first(cedar_identifier: id)
if artifact.nil?
logger.info "Request for unknown artifact id (#{id})"
halt(404)
end
citation = FHIRAdapter.create_citation(artifact, uri('fhir/Citation'), uri('redirect'),
artifact.public_version_history.count + 1)
citation.to_json
end
# Return a particular historical version of an artifact. If an artifact has N versions, we can return one of
# the N - 1 historical versions retrieved via Artifact#public_version_history or we can return the current version
get '/Citation/:id/_history/:version_id' do
id = params[:id]
version_id = params[:version_id].to_i
if version_id < 1
logger.info "Request for invalid artifact (#{id}) version id (#{version_id})"
halt(404)
end
base_artifact = Artifact.first(cedar_identifier: id)
if base_artifact.nil?
logger.info "Request for unknown artifact id (#{id})"
halt(404)
end
version_history = base_artifact.public_version_history
if version_id > version_history.count + 1
logger.info "Request for invalid artifact (#{id}) version id (#{version_id})"
halt(404)
end
citation = nil
if version_id <= version_history.count
versioned_artifact = version_history[version_id - 1].build_artifact
citation = FHIRAdapter.create_citation(versioned_artifact, uri('fhir/Citation'), uri('redirect'),
version_id, skip_concept: true)
else
citation = FHIRAdapter.create_citation(base_artifact, uri('fhir/Citation'), uri('redirect'), version_id)
end
citation.to_json
end
get '/Citation' do
# artifact-current-state is required
unless params&.any? { |key, _value| key == 'artifact-current-state' }
logger.info 'Search request missing artifact-current-state'
oo = FHIR::OperationOutcome.new(
issue: [
{
severity: 'error',
code: 'required',
details: {
text: 'Required search parameter artifact-current-state is missing'
}
}
]
)
return oo.to_json
end
multiple_and_parameters = CGI.parse(request.query_string).select do |k, _v|
CitationFilter::MULTIPLE_AND_PARAMETERS.include?(k)
end
request_url = "#{request.scheme}://#{request.host}:#{request.port}#{request.path}"
filter = CitationFilter.new(params: params.merge(multiple_and_parameters),
artifact_base_url: uri('fhir/Citation').to_s,
redirect_base_url: uri('redirect').to_s,
request_url: request_url,
client_ip: request.ip,
client_id: request.env['HTTP_FROM'],
log_to_db: true)
begin
bundle = filter.citations
bundle.to_json
rescue FhirError => e
logger.error "Error creating FHIR Bundle: #{e.full_message}"
e.to_operation_outcome_json
rescue StandardError => e
logger.error "Search error: #{e.full_message}"
oo = FHIR::OperationOutcome.new(
issue: [
{
severity: 'error',
code: 'exception',
details: {
text: e.message
}
}
]
)
oo.to_json
end
end
get '/CodeSystem/$get-mesh-children' do
if params[:code].nil?
tree_nodes = MeshTreeNode.where(parent_id: nil).order(:name)
else
parent_node = MeshTreeNode.where(tree_number: params[:code]).first
return FHIR::Parameters.new.to_json if parent_node.nil?
tree_nodes = parent_node.children
end
output = FHIRAdapter.create_mesh_children_output(tree_nodes)
output.to_json
end
def read_resource_from_file(path, params)
# We do not return files based on file names built from user-supplied parameters to prevent users from
# reading arbitrary files; instead, we 1) get a listing of valid files 2) see if there's a file that
# matches the requested file, and 3) return the matching file if present
id = params[:id] || params[:url].split('/').last
valid_files = Dir['resources/*.json']
matching_file = valid_files.detect { |f| f == "resources/#{path}-#{id}.json" }
if matching_file && File.exist?(matching_file)
FHIR.from_contents(File.read(matching_file)).to_json
else
logger.info "Request for unknown resource: #{id}"
halt(404)
end
end
end