forked from pophealth/popHealth
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathpatient_cache.rb
63 lines (53 loc) · 2.59 KB
/
patient_cache.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
class PatientCache
include Mongoid::Document
store_in collection: :patient_cache
field :first, type: String
field :last, type: String
field :patient_id, type: String
field :birthdate, type: Integer
field :gender, type: String
field :manual_exclusion, type: Boolean
scope :by_provider, ->(provider, effective_start_date, effective_date) { where({'value.provider_performances.provider_id' => provider.id, 'value.effective_date'=>effective_date, 'value.effective_start_date'=>effective_start_date}) }
scope :outliers, ->(patient) {where({'value.patient_id'=>patient.id})}
MATCH = {'$match' => {'value.measure_id' => "8A4D92B2-397A-48D2-0139-9BB3331F4C02", "value.sub_id" => "a"}}
SUM = {'$group' => {
"_id" => "$value.measure_id", # we don't really need this, but Mongo requires that we group
"population" => {"$sum" => "$value.population"},
"denominator" => {"$sum" => "$value.denominator"},
"numerator" => {"$sum" => "$value.numerator"},
"antinumerator" => {"$sum" => "$value.antinumerator"},
"exclusions" => {"$sum" => "$value.exclusions"},
"denexcep" => {"$sum" => "$value.denexcep"},
"considered" => {"$sum" => 1}
}}
REWIND = {'$group' => {"_id" => "$_id", "value" => {"$first" => "$value"}}}
def self.provider
aggregate({"$match"=>
{"value.measure_id"=>"8A4D92B2-3A00-2A25-013A-23015AD43373",
"value.sub_id"=>nil,
"value.effective_date"=>1293840000,
"value.test_id"=>nil,
"value.manual_exclusion"=>{"$in"=>[nil, false]}}},
{"$project"=>
{"value"=>1, "providers"=>"$value.provider_performances.provider_id"}},
{"$unwind"=>"$providers"},
{"$match"=>
{"$or"=>
[{"providers"=> BSON::ObjectId.from_string("50a64aa68898e5b4b2000001")},
{"providers"=> BSON::ObjectId.from_string("50a64aa68898e5b4b2000003")},
{"providers"=> BSON::ObjectId.from_string("50a55a8e8898e5d400000005")},
{"providers"=> BSON::ObjectId.from_string("50a64aa68898e5b4b2000007")},
{"providers"=> BSON::ObjectId.from_string("50a64aa68898e5b4b2000009")}]}})
end
def self.languages
# aggregate({'$project' => {'value' => 1, 'languages' => "$value.languages"}},
# {'$unwind' => "$languages"},
# {'$project' => {'value' => 1, 'languages' => {'$substr' => ['$languages', 0, 2]}}},
# {'$match' => {'$or' => [{'languages' => "en"}, {'languages' => "fr"}]}},
# REWIND}
# )
end
def self.aggregate(*pipeline)
Mongoid.default_client.command(aggregate: 'patient_cache', pipeline: pipeline, allowDiskUse:true)['result']
end
end