Skip to content

Instantly share code, notes, and snippets.

@maggiben
Last active August 29, 2015 14:08
Show Gist options
  • Save maggiben/bec49b923c08351dbc45 to your computer and use it in GitHub Desktop.
Save maggiben/bec49b923c08351dbc45 to your computer and use it in GitHub Desktop.
mongo logs by intervals
var until = new Date(2015,1,1);
var since = new Date(2010,1,1);
var digestor = '535b97507899a672c49dd490'
db.logs.aggregate([
{
$match: {
time: {
$gt: 0
},
date: {
$gte: new Date(since),
$lt: new Date(until)
},
status: {
$gte: 100
},
digestor: ObjectId(digestor)
}
},
{
$project: {
_id: 0, // let's remove bson id's from request's result
status: 1,
success: { $cond: [{$lt: ['$status', 400]}, 1, 0]}, // add 1
error: { $cond: [{$gte: ['$status', 400]}, 1, 0]},
cache: { $cond: [{$eq: ['$status', 304]}, 1, 0]},
bytesOut: {$ifNull: ['$responseHeaders.content-length', 0]},
//date: {year: {$year: '$date'}, month: {$month: '$date'}, day: {$dayOfMonth: '$date'}}
//timestamp: {day: {$dayOfMonth: '$date'}},
timestamp: {year: { $year: '$date' }, day: {$dayOfMonth: '$date'}, month: { $month: '$date' }, hour: { $hour: '$date'}, minute: { $minute: '$date'}},
date: '$date',
index: { $const:[0,1] },
method: 1
//date: { hour: { $hour: '$date' } }
}
},
{
$group: {
//_id: {c: '$country', n: '$name' },
_id: { method:'$method', minute: '$timestamp.minute'},
error: { $sum: '$error'},
success: { $sum: '$success' },
cache: { $sum: '$cache' },
bytesOut: { $sum: '$bytesOut' },
total: { $sum: 1 },
}
},
{
$project: {
_id: 0,
method: '$_id.method',
ts: '$_id.minute',
dataset: {
ts: '$_id.minute',
out: '$bytesOut'
}
}
},
{
$group: {
_id: '$method',
dataset: {
$push: {
ts: '$dataset.ts',
out: '$dataset.out',
}
}
}
},
{
$project: {
_id: 1,
ts: '$dataset.out'
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment