Module: Auth::Concerns::ActivityConcern::ClassMethods

Defined in:
app/models/auth/concerns/activity_concern.rb

Instance Method Summary collapse

Instance Method Details

#activities_fields(query) ⇒ Object

defaults for only. if it is empty or nil, then it becomes all attributes otherwise it becomes the intersect of all attributes and the ones specified in the only created_at had to be added here, because otherwise it throws an error saying missing_attribute in the only. I think this has something to do with the fact that it is used in the query, so it will be included in the result. @used_in: get_in_range @param : the provided query, expected to be of the structure: => [array],,,other key value pairs @return query : returns the query with the default values for the fields to be returned



42
43
44
45
46
47
48
# File 'app/models/auth/concerns/activity_concern.rb', line 42

def activities_fields(query)
	defaults = {"only" => Object.const_get(name).fields.keys}
	query = defaults.deep_merge(query)
	only = ((Object.const_get(name).fields.keys & query["only"]) + ["created_at"])
	query["only"] = only
	return query
end

#activities_from_to(query, default_from, default_to) ⇒ Object

the default “from” is the beginning of the current month, and the default “to” is the current time. @param query: the “from”,“to” provided in the query if at all, otherwise nil, assumed that query has two keys : “from”, “to”, under a key called “range” @param default_from : the default_from for the particular function that is firing this query, it is an epoch @param default_to : the default_to for the particular function that is firing this query, it is an epoch @return : default values for “from”, “to”



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/auth/concerns/activity_concern.rb', line 20

def activities_from_to(query,default_from,default_to)
	defaults = {"range" => {"from" => default_from, "to" => default_to}}
	query = defaults.deep_merge(query)
	##default from and to assigned here.
	from = query["range"]["from"].to_i
	to = query["range"]["to"].to_i
	if from >= to
		query["range"]["from"] = default_from
		query["range"]["to"] = default_to
	end
	return query
end

#get_in_range(query_params) ⇒ Object

@param query_params: : {“from” : unix_epoch_as_string, “to” => unix_epoch_as_string, “user_id”: string, “only”: [array_of_attributes_required]} “range” => optional,if nil or empty, “from” and “to” will be automatically assigned to beginning_of_current_month and current_time respectively “user_id” => required, will return empty hash if absent. “only” => optional, will default to all attributes of the activity model. @return: timestamp => activity_object hashified.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/auth/concerns/activity_concern.rb', line 90

def get_in_range(query_params)
	
	## return empty hash if there is no user_id
	return {} unless query_params[:user_id]
	query_params = Object.const_get(name).activities_from_to(query_params, Time.now.beginning_of_month.to_i, Time.now.to_i)
	query_params = Object.const_get(name).activities_fields(query_params)

	##make the mongoid range call here.
	activities = Object.const_get(name).where(:created_at.gte => query_params["range"]["from"], :created_at.lte => query_params["range"]["to"], :user_id => query_params["user_id"]).only(query_params["only"])

	activities_hash = Hash[activities.entries.map{|c| c.created_at.to_i}.zip(activities.entries.map{|c| c.as_json})]

	puts JSON.pretty_generate(activities_hash)
	
	return activities_hash

end

#last_n_months(query_params) ⇒ Object

@param query_params: : {“from” : unix_epoch_as_string, “to” => unix_epoch_as_string, “user_id”: string, “only”: [array_of_attributes_required]} “range” => optional,if nil or empty, “from” and “to” will be automatically assigned to beginning_of_current_month and current_time respectively “user_id” => required, will return empty hash if absent. @return agg_hash: the aggregation hash



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
# File 'app/models/auth/concerns/activity_concern.rb', line 54

def last_n_months(query_params)
	return {} unless query_params[:user_id]
	query_params = Object.const_get(name).activities_from_to(query_params, Time.now.to_i = 12.months, Time.now.to_i)
	
	agg_hash = Object.const_get(name).collection.aggregate([
			{
				"$match" => Object.const_get(name).where(:created_at.gte => query_params["range"]["from"], :created_at.lte => query_params["range"]["to"], :user_id => query_params[:user_id]).selector
			},
			{
				"$project" => {
					month: {
						"$month" => "$created_at"
					}
				}
			},
			{
				"$group" => {
					"_id" => {
						month: "$month"
					},
					count: {
						"$sum" => 1
					}
				}
			}]
		)

	return agg_hash

end