Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def c_stats(campaign_class=["dynamic",'static','event'])
- # puts "Listing campaigns of type: #{campaign_class}"
- campaigns_info = campaigns.in(type: campaign_class).only(:_id, :name, :type, :status )
- ids = campaigns_info.map { |c| c._id}
- # puts "Ids: #{ids}"
- pipeline = [
- # {"$match" => {project_id: self._id} },
- {"$match" => {'campaign_id' => {'$in' => ids}} },
- {"$project" => {
- campaign_id: '$campaign_id',
- non_opened: {"$ifNull" => [ '$action', 1 ]},
- opened: {"$cond" => [{"$eq" => ['$action', 0]}, 1, 0]},
- engaged: {"$cond" => [{"$eq" => ['$action', 1]}, 1, 0]}
- }},
- { "$group" => {
- _id: '$campaign_id',
- non_opened: {"$sum" => '$non_opened'},
- opened: {"$sum" => '$opened'},
- engaged: {"$sum" => '$engaged'}
- }}
- ]
- hashed_campaigns = Hash[campaigns_info.map { |u| [u._id, u] }]
- aggregation = Notification.collection.aggregate(pipeline)
- hashed_aggregation = Hash[aggregation.map { |u| [u['_id'], u] }]
- final_data = []
- new_data = hashed_campaigns.as_json.each do |k,v|
- v["engaged"] = hashed_aggregation.as_json.key?(k) ? hashed_aggregation.as_json[k]["engaged"] : 0
- v["non_opened"] = hashed_aggregation.as_json.key?(k) ? hashed_aggregation.as_json[k]["non_opened"] : 0
- v["opened"] = hashed_aggregation.as_json.key?(k) ? hashed_aggregation.as_json[k]["opened"] : 0
- end
- return new_data.map { |key,value| value }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement