Skip to content

Commit

Permalink
Added new file; changing Malaria Dashboard from sql to couchdb
Browse files Browse the repository at this point in the history
  • Loading branch information
phundi committed Mar 22, 2017
1 parent 92614de commit 9c5eec7
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 2 deletions.
21 changes: 21 additions & 0 deletions config/couchdb.yml.example
@@ -0,0 +1,21 @@
development: &development
protocol: 'http'
host: localhost
port: 5984
database: malaria_dash
username: root
password: password
site_code: MC DHO
site_name: Mchinji District Hospital
district_code: MC
district_name: Mchinji
sync_url: http://0.0.0.0:5984/central_dashboard

test:
<<: *development
suffix: test

production:
<<: *development
protocol: 'http'
suffix:
7 changes: 7 additions & 0 deletions db/couchdb_views.json
@@ -0,0 +1,7 @@
{
"views": {
"by_date": {
"map": "function(doc) { emit(doc['date']) }"
}
}
}
2 changes: 1 addition & 1 deletion app/models/api.rb → lib/api.rb
@@ -1,4 +1,4 @@
class API
module API
def self.malaria_observations(start_date, end_date)
outpatient_encounter_type_id = EncounterType.find_by_name("OUTPATIENT DIAGNOSIS").encounter_type_id
malaria_concept_id = Concept.find_by_name("MALARIA").concept_id
Expand Down
2 changes: 1 addition & 1 deletion lib/dashboard_service.rb
Expand Up @@ -40,7 +40,7 @@ def self.push_to_dashboard(enc_params)
patient_id,age,gender)
hash[:symptoms] = pull_symptoms(patient_id)
end
push_to_couch(hash)
push_to_couch(hash) rescue nil
end

def self.pull_diagnoses(observation,obs_date,national_id,patient_id,age,gender)
Expand Down
88 changes: 88 additions & 0 deletions lib/tasks/dashboard.rake
@@ -0,0 +1,88 @@
require "rest-client"
namespace :dashboard do
desc "Pull data from openmrs and save to couch database"
task :pull_data => :environment do
configs = YAML.load_file("#{RAILS_ROOT}/config/couchdb.yml")[RAILS_ENV] rescue nil

if configs.present?
database = configs["database"]
username = configs["username"]
password = configs["password"]
port = configs["port"]
host = configs["host"]

url = "http://#{username}:#{password}@#{host}:#{port}/#{database}"
`curl -X PUT #{url}`
`cd #{RAILS_ROOT}/db && curl -X PUT -d @couchdb_views.json #{url}/_design/query`

cur_date = params[:date].to_date rescue Date.today

#Usage:
#{"key" => "api method to call"}
data_sources = {
"reported_cases" => "malaria_observations",
"microscopy_orders" => "microscopy_orders",
"microscopy_positives" => "microscopy_positives",
"microscopy_negatives" => "microscopy_negatives",
"microscopy_unknown" => "microscopy_unknown",
"mRDT_orders" => "mRDT_orders",
"mRDT_positives" => "mRDT_positives",
"mRDT_negatives" => "mRDT_negatives",
"mRDT_unknown" => "mRDT_unknown",
"under_five_males" => "under_five_malaria_cases_male",
"under_five_females" => "under_five_malaria_cases_female",
"treated_negatives" => "treated_negatives",
"all_dispensations" => "all_dispensations",
"first_line_dispensations" => "first_line_dispensations",
"malaria_in_pregnancy" => "malaria_in_pregnancy"
}

date_ranges = {
"Today" => [cur_date, cur_date],
"This Month" => [cur_date.beginning_of_month, cur_date.end_of_month],
"This Quarter" => [cur_date.beginning_of_quarter, cur_date.end_of_quarter],
"This Year" => [cur_date.beginning_of_year, cur_date.end_of_year]
}

results = {}

date_ranges.each do |category, date_range|

results[category] = {}

data_sources.each do |indicator, query|
start = Time.now
results[category][indicator] = eval("API.#{query}('#{date_range[0].to_s}', '#{date_range[1].to_s}').count")
end
end

results['dispensation_trends'] = API.dispensation_trends((cur_date - 1.year), cur_date)

date = cur_date.to_date.strftime("%Y%m%d")
data = {
"type" => "PullTracker",
"date" => date,
"district_code" => configs['district_code'],
"district_name" => configs['district_name'],
"site_code" => configs['site_code'],
"site_name" => configs['site_name'],
"data" => results
}

info = JSON.parse(`curl -X GET http://#{username}:#{password}@#{host}:#{port}/#{database}/_design/query/_view/by_date?key=\\\"#{date}\\\"`)
uuid = info['rows'].first['id'] rescue nil
doc = JSON.parse(`curl -X GET http://#{username}:#{password}@#{host}:#{port}/#{database}/#{uuid}`) rescue nil

if doc && !doc['date'].blank?
doc['data'] = results
RestClient.post("http://#{username}:#{password}@#{host}:#{port}/#{database}", doc.to_json, :content_type => "application/json")
else
url = "http://#{username}:#{password}@#{host}:#{port}/#{database}/"
RestClient.post(url, data.to_json, :content_type => "application/json")
end

`curl -X POST http://127.0.0.1:5984/_replicate -d '{"source":"#{url}","target":"#{configs['sync_url']}", "continuous":false}' -H "Content-Type: application/json"`

end
end
end
Binary file removed vendor/cache/domain_name-0.5.20161129.gem
Binary file not shown.
Binary file removed vendor/cache/http-cookie-1.0.3.gem
Binary file not shown.
Binary file removed vendor/cache/mysql2-0.4.5.gem
Binary file not shown.
Binary file removed vendor/cache/rest-client-1.6.9.gem
Binary file not shown.
Binary file removed vendor/cache/unf_ext-0.0.7.2.gem
Binary file not shown.
Binary file removed vendor/cache/webrobots-0.1.2.gem
Binary file not shown.

0 comments on commit 9c5eec7

Please sign in to comment.