Skip to content

Instantly share code, notes, and snippets.

{"_id"=>BSON::ObjectId('52cee7c830040e8281f81b72'),
"api_id"=>"2013-12-25 17:25:32 UTC_user",
"date"=>2013-12-25 17:25:32 UTC,
"text"=>"",
"link"=>"",
"metrics"=>{"place"=>1, "user"=>1, "lat"=>40.9112317301, "lon"=>-73.9011385182, "id"=>15103893, "name"=>"Obrien"},
"source_id"=>BSON::ObjectId('52cee7c030040e8281f8196c'), "summary"=>false, "simhash"=>nil,
"__metric_keys"=>["place", "user", "lat", "lon", "id", "name"]}
{"_id"=>BSON::ObjectId('52cee7c830040e8281f81b72'), "api_id"=>"2013-12-25 17:25:32 UTC_user", "date"=>2013-12-25 17:25:32 UTC, "text"=>"", "link"=>"", "metrics"=>{"place"=>1, "user"=>1, "lat"=>40.9112317301, "lon"=>-73.9011385182, "id"=>15103893, "name"=>"Obrien"}, "source_id"=>BSON::ObjectId('52cee7c030040e8281f8196c'), "summary"=>false, "simhash"=>nil, "__metric_keys"=>["place", "user", "lat", "lon", "id", "name"]}
aggregates = d.map {|a| a['_id'] || 0}
# get min/max and range
min = aggregates.min.to_f
max = aggregates.max.to_f
range = (max - min) / 8
index = 0
copied = d.clone
bins = (min..max).step(range).inject([]){|memo, num| memo << {"_id"=> num, metric_name => []}}
new_range = bins.map do |item|
more = item['_id']
def source_available_metrics
{
"friends" => {'cumulative'=>true, 'name'=>"Friends", 'primary'=>'true'},
"likes" => {'cumulative'=>false, 'name'=>"Likes Count"},
"posts" => {'cumulative'=>false, 'name'=>"Posts", 'primary'=>'true'},
"comments" => {'cumulative'=>false, 'name'=>"Comments", 'primary'=>'true'},
"post_comment_count" => {'cumulative'=>false, 'name'=>"Post Comments"}
}
end
0: Object
d: 26
last_id: "52c2406330042462861c1ae5"
v: 1
__proto__: Object
1: Object
d: 11
last_id: "527bcc423004f951123b7c1d"
v: 1
__proto__: Object
[{:d=>11, :v=>1, :last_id=>"527bcc423004f951123b7c1d"}, {:d=>9, :v=>1, :last_id=>"524afa4d30040d96248b828d"}, {:d=>5, :v=>1, :last_id=>"52bc9aec300441a506054092"}, {:d=>4, :v=>1, :last_id=>"527bcc453004f951123b7c80"}, {:d=>3, :v=>1, :last_id=>"520a30b33004882ce9feece7"}, {:d=>2, :v=>2, :last_id=>"52bc9aed300441a50605409b"}, {:d=>1, :v=>5, :last_id=>"524afa4e30040d96248b82ae"}, {:d=>0, :v=>15, :last_id=>"52bc9aee300441a506054100"}]
the params
[11, 9, 5, 4, 3, 2, 1, 0]
the result
[0.0,5.5], [6.0, 2.0]
#the query:
#"Show me all posts, group by post_comments_count"
#the raw data
[{:d=>11, :v=>1, :last_id=>"527bcc423004f951123b7c1d"}, {:d=>9, :v=>1, :last_id=>"524afa4d30040d96248b828d"}, {:d=>5, :v=>1, :last_id=>"52bc9aec300441a506054092"}, {:d=>4, :v=>1, :last_id=>"527bcc453004f951123b7c80"}, {:d=>3, :v=>1, :last_id=>"520a30b33004882ce9feece7"}, {:d=>2, :v=>2, :last_id=>"52bc9aed300441a50605409b"}, {:d=>1, :v=>5, :last_id=>"524afa4e30040d96248b82ae"}, {:d=>0, :v=>15, :last_id=>"52bc9aee300441a506054100"}]
#the meaning
#" i have 1 posts with 11 comments, i have 1 post with 9 comments, i have 1 post with 5 comments, i have 1 post with 4 comments, # i have 1 post with 3 comments, i have 2 posts with 2 comments, i have 5 with one comment, i have 15 posts with 0 comments
## these are the values of # of posts and no post_count taken into consideration
{"result"=>[{"_id"=>3, "posts"=>1, "last_id"=>BSON::ObjectId('520a30b33004882ce9feece7')}, {"_id"=>1, "posts"=>5, "last_id"=>BSON::ObjectId('524afa4e30040d96248b82ae')}, {"_id"=>4, "posts"=>1, "last_id"=>BSON::ObjectId('527bcc453004f951123b7c80')}, {"_id"=>9, "posts"=>1, "last_id"=>BSON::ObjectId('524afa4d30040d96248b828d')}, {"_id"=>5, "posts"=>1, "last_id"=>BSON::ObjectId('52bc9aec300441a506054092')}, {"_id"=>nil, "posts"=>1, "last_id"=>BSON::ObjectId('51ed7656300491ccd611aa29')}, {"_id"=>11, "posts"=>1, "last_id"=>BSON::ObjectId('527bcc423004f951123b7c1d')}, {"_id"=>2, "posts"=>2, "last_id"=>BSON::ObjectId('52bc9aed300441a50605409b')}, {"_id"=>0, "posts"=>15, "last_id"=>BSON::ObjectId('52bc9aee300441a506054100')}], "ok"=>1.0}
{"since":1369872000,"until":1388436795.964,"accounts":[{"id":"51cbf37e3004a7209841b572","name":"John Kealy","type":"Facebook","line_item_id":"0a8c1d74939af6095d476ce9c3795d9d","metrics":[{"id":"posts","original_name":"Posts","formatter":null,"name":"newpost","cumulative":false,"primary":"true","composite":null,"type":null,"value":"posts","filters":[{"metric":{"id":"post_comment_count","original_name":"Post Comments","formatter":null,"name":"Post Comments","cumulative":false,"primary":null,"composite":null,"type":null,"value":"post_comment_count"},"operator":{"symbol":"exists","string":"with"},"value":""}],"color":"#5CA8FA"}]}],"aggregate":"metric","aggregate_metric":"post_comment_count"}:
# new group
{"_id"=>"$metrics.post_comment_count", "posts"=>{"$sum"=>"$metrics.posts"}}
# new query
{"__metric_keys"=>{"$all"=>["posts", "post_comment_count"]}, "date"=>{"$gt"=>2013-05-29 20:00:00 -0400, "$lt"=>2013-12-30 15:56:45 -0500}, "source_id"=>BSON::ObjectId('51cbf37e3004a7209841b572'), "metrics.post_comment_count"=>{"$exists"=>true}}