Skip to content

Instantly share code, notes, and snippets.

@cpdean
Last active December 23, 2015 16:29
Show Gist options
  • Save cpdean/6662448 to your computer and use it in GitHub Desktop.
Save cpdean/6662448 to your computer and use it in GitHub Desktop.
for an stream of pings, use the mongodb aggregation pipeline to find which urls do better in the morning than in the evening
// The following is a function defined in the mongo shell
// use transfer # switch to the transfer db
// t = transfer.test # save the important collection off to this temp
// unfortunately this is stuck to only work on july 28, 2013
function show_daily_improvers(){
return t.aggregate(
{$match:{time:{$gte:ISODate("2013-07-28"),
$lt:ISODate("2013-07-29")}}
},
{$group:
{_id:
{url: "$url",
year: {$year:"$time"},
month: {$month:"$time"},
day: {$dayOfMonth:"$time"},
morning: {$cmp: [ISODate("2013-07-28T12"), "$time"]}},
mean_ping: {$avg:"$duration"} }},
{$project:{mean_ping: {$multiply: ["$_id.morning", "$mean_ping"]}}},
{$group:
{_id:{url: "$_id.url"},
morning_performance:{$sum:"$mean_ping"}} },
{$sort:{morning_performance:-1}},
{$limit: 5}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment