Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem 'jbuilder', '~> 2.5' # Build JSON APIs with ease. Read more: https://
# gem 'redis', '~> 4.0' # Use Redis adapter to run Action Cable in production
# gem 'bcrypt', '~> 3.1.7' # Use ActiveModel has_secure_password
# gem 'mini_magick', '~> 4.8' # Use ActiveStorage variant

gem 'dalli' # Memcache client
gem 'zlib' , '>= 1.0.0'
gem 'bootsnap', '>= 1.1.0', require: false # Reduces boot times through caching; required in config/boot.rb

Expand Down
109 changes: 86 additions & 23 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
################################################################################

class ApplicationController < ActionController::Base
require 'dalli'
before_action :setup_dalli, :connect_to_server

@@plansbyid = {}

def self.plansbyid
decompress_hash(session[:plansbyid])
get_cache("plansbyid")
end

#-----------------------------------------------------------------------------
Expand All @@ -20,13 +23,12 @@ def self.plansbyid

def coverage_plans
# Read all of the coverage plans from the server
cp_profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-CoveragePlan"
reply = @client.read(FHIR::List, nil, nil, cp_profile).resource
#cp_profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-CoveragePlan"
reply = @client.read(FHIR::List, nil, nil, nil).resource
@plansbyid = build_coverage_plans(reply)
options = build_coverage_plan_options(reply)
session[:plansbyid] = compress_hash(@plansbyid.to_json)
session[:cp_options] = compress_hash(options)
options
@cp_options = build_coverage_plan_options(reply)
set_cache("plansbyid",@plansbyid)
set_cache("cp_options", @cp_options)
rescue => exception
puts "coverage_plans fails: not connected"
options = [["N/A (Must connect first)", "-"]]
Expand All @@ -35,11 +37,11 @@ def coverage_plans
#-----------------------------------------------------------------------------

def get_plansbyid
if session[:plansbyid]
@plansbyid = JSON.parse(decompress_hash(session[:plansbyid])).deep_symbolize_keys
@cp_options = decompress_hash(session[:cp_options])
if get_cache("plansbyid")
@plansbyid = get_cache("plansbyid")
@cp_options = get_cache("cp_options")
else
puts "get_plansbyid: session[:plansbyid] is nil, calling coverage_plans "
puts "get_plansbyid: get_cache(\"plansbyid\") is nil, calling coverage_plans "
@plansbyid = nil
@cp_options = [["N/A (Must connect first)", "-"]]
coverage_plans
Expand All @@ -48,18 +50,6 @@ def get_plansbyid

#-----------------------------------------------------------------------------

def compress_hash(h)
zh = Base64.encode64(Zlib::Deflate.deflate(h.to_json))
end

#-----------------------------------------------------------------------------

def decompress_hash(zh)
h = JSON.parse(Zlib::Inflate.inflate(Base64.decode64(zh)))
end

#-----------------------------------------------------------------------------

def build_coverage_plan_options(fhir_list_reply)
options = fhir_list_reply.entry.collect do |entry|
[entry.resource.title, entry.resource.identifier.first.value]
Expand All @@ -73,7 +63,80 @@ def build_coverage_plans (fhir_list_reply)
coverageplans = fhir_list_reply.entry.each_with_object({}) do | entry, planhashbyid |
planhashbyid[entry.resource.identifier.first.value] = CoveragePlan.new(entry.resource)
end
coverageplans.deep_symbolize_keys
#binding.pry
#coverageplans.deep_symbolize_keys
end

def setup_dalli
options = { :namespace => "formulary", :compress => true }
@dalli_client = Dalli::Client.new('localhost:11211', options)
end

# Utility accessors that reference session data

def set_cache(variable,value)
puts "dalli: set #{variable}-#{session.id.public_id}"
@dalli_client.set("#{variable}-#{session.id.public_id}",value)
end

def get_cache(variable)
puts "dalli: get #{variable}-#{session.id.public_id}"
@dalli_client.get("#{variable}-#{session.id.public_id}")
end

def get_iss_url
@dalli_client.get("iss_url-#{session.id.public_id}")
end
def set_iss_url(url)
@dalli_client.set("iss_url-#{session.id.public_id}",url)
end

def get_client_id
@dalli_client.get("client_id-#{session.id.public_id}")
#session[:client_id]
end
def set_client_id(id)
@dalli_client.set("client_id-#{session.id.public_id}",id)
#session[:client_id]
end

# Get the FHIR server url
def server_url
url = (params[:server_url] || session[:server_url])
url = url.strip if url
end

def connect_to_server
puts "==>connect_to_server"
# if client_id.length == 0
# @client = FHIR::Client.new(iss_url)
# @client.use_r4
# return # We do not have authentication
# end
if session.empty?
err = "Session Expired"
# binding.pry
redirect_to root_path, alert: err
end
if server_url.present?
@client = FHIR::Client.new(server_url)
@client.use_r4
@client.use_r4
@client.additional_headers = { 'Accept-Encoding' => 'identity' } #
@client.set_basic_auth("fhiruser","change-password")
# token_expires_in = token_expiration - Time.now.to_i
# if token_expires_in.to_i < 10 # if we are less than 10s from an expiration, refresh
# get_new_token
# end
# @client.set_bearer_token(access_token)
cookies[:server_url] = server_url
session[:server_url] = server_url
end
rescue StandardError => exception
reset_session
err = "Failed to connect: " + exception.message
redirect_to root_path, alert: err
end


end
43 changes: 19 additions & 24 deletions app/controllers/compare_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class CompareController < ApplicationController

before_action :check_server_connection, only: [ :index ]
before_action :connect_to_server, only: [ :index ]

attr_accessor :drugname, :codes

Expand All @@ -26,7 +26,7 @@ def index
@codes = params[:code].strip.split(',').map(&:strip).join(',')
set_cache
set_table
@cache_nil = ClientConnections.cache_nil?(session.id.public_id)
#@cache_nil = ClientConnections.cache_nil?(session.id.public_id)
else
redirect_to root_path, flash: { error: "Please specify a (partial) drug name, or at least one rxnorm code" }
end
Expand All @@ -36,17 +36,6 @@ def index
private
#-----------------------------------------------------------------------------

# Check that this session has an established FHIR client connection.
# Specifically, sets @client and redirects home if nil.

def check_server_connection
session[:foo] = "bar" unless session.id
raise "session.id is nil" unless session.id

unless @client = ClientConnections.get(session.id.public_id)
redirect_to root_path, flash: { error: "Please connect to a formulary server" }
end
end

#-----------------------------------------------------------------------------

Expand All @@ -59,34 +48,38 @@ def set_cache
searchParams = {:_count => 200}
searchParams[:code] = @codes if @codes and @codes.length > 0
searchParams["DrugName:contains"] = @drugname if @drugname and @drugname.length>0
profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-FormularyDrug"
searchParams[:_profile] = profile

#profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-FormularyDrug"
#searchParams[:_profile] = profile
@cache[:fds] = get_all(FHIR::MedicationKnowledge, searchParams)
ClientConnections.cache(session.id.public_id, @cache) unless params[:search].present?
#ClientConnections.cache(session.id.public_id, @cache) unless params[:search].present?
# end
end

#-----------------------------------------------------------------------------

# Gets all instances of klass from server
# first call get_all_bundles to get all pages of response
# Then iterate through the responses,and pull out the resources returned

def get_all(klass = nil, search_params = {})
replies = get_all_bundles(klass, search_params)
return nil unless replies

resources = []
replies.each do |reply|
# Create an array of resources from each bundle and insert it into resources
replies.each do |reply|
resources.push(reply.entry.collect{ |singleEntry| singleEntry.resource })
end

resources.compact!
resources.flatten(1)
# At this point resources is an array of arrays
resources.compact!
# Now resources is an array.
resources.flatten(1)
end

#-----------------------------------------------------------------------------

# Gets all bundles from server when querying for klass
# replies is an array of all of the bundles

def get_all_bundles(klass = nil, search_params = {})
return nil unless klass
Expand All @@ -98,7 +91,7 @@ def get_all_bundles(klass = nil, search_params = {})
replies.push(replies.last.next_bundle)
end

replies.compact!
replies.compact! # eliminates nulls
replies.present? ? replies : nil
end

Expand All @@ -108,7 +101,8 @@ def get_all_bundles(klass = nil, search_params = {})

def set_table
@table_header = @cache[:cps].collect{ |cp| CoveragePlanOld.new(cp.title , cp.identifier.first.value) }
chosen = sift_fds
#chosen = sift_fds # not convinced this is necessary
chosen = @cache[:fds].clone
@table_rows = Hash.new

chosen.collect!{ |fd| FormularyDrug.new(fd, @plansbyid) }
Expand All @@ -124,7 +118,8 @@ def set_table
# Sifts through formulary drugs based on search term, returns chosen fds

def sift_fds
return @cache[:fds].clone if params[:search].blank? || ClientConnections.cache_nil?(session.id.public_id)
return @cache[:fds].clone if params[:search].blank?
binding.pry
@cache[:fds].select{ |fd| fd.code.coding.first.display.upcase.include?(params[:search].upcase) }
end

Expand Down
6 changes: 4 additions & 2 deletions app/controllers/coverageplans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CoverageplansController < ApplicationController

before_action :check_server_connection, only: [ :index, :show ]
before_action :connect_to_server, only: [ :index, :show ]

#-----------------------------------------------------------------------------

Expand All @@ -19,6 +19,7 @@ class CoverageplansController < ApplicationController
def index
get_plansbyid
@coverageplans = @plansbyid.values

end

#-----------------------------------------------------------------------------
Expand All @@ -27,7 +28,8 @@ def index

def show
get_plansbyid
@plandata = @plansbyid[params[:id].to_sym]
binding.pry
@plandata = @plansbyid[params[:id]]
end

#-----------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/formularies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class FormulariesController < ApplicationController

before_action :check_server_connection, only: [ :index, :show ]
before_action :connect_to_server

#-----------------------------------------------------------------------------

Expand All @@ -20,8 +20,8 @@ def index
if params[:page].present?
@@bundle = update_page(params[:page], @@bundle)
else
profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-FormularyDrug"
search = { parameters: { _profile: profile } }
#profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-FormularyDrug"
search = { parameters: { } }
search[:parameters][:DrugTier] = params[:drug_tier] if params[:drug_tier].present?
search[:parameters][:DrugPlan] = params[:coverage] if params[:coverage].present?
search[:parameters][:code] = params[:code] if params[:code].present?
Expand Down
48 changes: 29 additions & 19 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class WelcomeController < ApplicationController
def index
# solution from https://stackoverflow.com/questions/30772737/rails-4-session-id-occasionally-nil
session[:foo] = "bar" unless session.id
setup_dalli

@client = ClientConnections.get(session.id.public_id)
#@client = ClientConnections.get(session.id.public_id)
connect_to_server
@count = formulary_count
@cp_count = coverageplan_count
@cp_options = coverage_plans
@cache_nil = ClientConnections.cache_nil?(session.id.public_id)
# @cache_nil = ClientConnections.cache_nil?(session.id.public_id)

get_plansbyid
end
Expand All @@ -30,19 +32,27 @@ def index
# Connect the FHIR client with the specified server and save the connection
# for future requests.

def connect_to_server
session[:foo] = "bar" unless session.id
raise "session.id is nil" unless session.id
if params[:server_url].present? && !ClientConnections.set(session.id.public_id, params[:server_url])
err = "Connection failed: Ensure provided url points to a valid FHIR server"
err += " that holds at least one Formulary"
redirect_to root_path, flash: { error: err }
session[:plansbyid] = nil
session[:cp_options] = [["N/A (Must connect first)", "-"]]
return nil
end
cookies[:server_url] = params[:server_url] if params[:server_url].present?
end

# Connect the FHIR client with the specified server and save the connection
# for future requests.
# If token is expired or within 10s of expiration, refresh the token




# def connect_to_server
# session[:foo] = "bar" unless session.id
# raise "session.id is nil" unless session.id
# if params[:server_url].present? && !ClientConnections.set(session.id.public_id, params[:server_url])
# err = "Connection failed: Ensure provided url points to a valid FHIR server"
# err += " that holds at least one Formulary"
# redirect_to root_path, flash: { error: err }
# session[:plansbyid] = nil
# session[:cp_options] = [["N/A (Must connect first)", "-"]]
# return nil
# end
# cookies[:server_url] = params[:server_url] if params[:server_url].present?
# end


#-----------------------------------------------------------------------------
Expand All @@ -51,8 +61,8 @@ def connect_to_server

def formulary_count
begin
profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-FormularyDrug"
search = { parameters: { _profile: profile, _summary: "count" } }
#profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-FormularyDrug"
search = { parameters: { _summary: "count" } }
count = @client.search(FHIR::MedicationKnowledge, search: search ).resource.total
rescue => exception
count = 0
Expand All @@ -64,8 +74,8 @@ def formulary_count

def coverageplan_count
begin
profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-CoveragePlan"
search = { parameters: { _profile: profile, _summary: "count" } }
#profile = "http://hl7.org/fhir/us/davinci-drug-formulary/StructureDefinition/usdf-CoveragePlan"
search = { parameters: { _summary: "count" } }
count = @client.search(FHIR::List, search: search ).resource.total
rescue => exception
count = 0
Expand Down
Loading