//Aggregation query for events to summarise the nodes by current state.
Code Block |
---|
|
db.getCollection('nodes').aggregate([
//get all nodes from the group HQDEV
{
$match: {group: "HQDev"}
},
//Get all the states for the matched nodes
{
$lookup: {
from: "state",
localField: "_id",
foreignField: "node",
as: "states"
}
},
//unwinds the states array creating a document per state with node config data
{
$unwind: {
path: "$states"
}
},
//We only want open states
{
$match: {'states.state': {$eq: 'open'}}
},
// group by the node, the state and the stateful element
{
$group : {
_id : {node: "$_id", state: "$states.state", stateful: "$states.stateful" },
total: {$sum: 1}
}
},
//Then group by node and collecting all the states for the node
{
$group : {
_id : "$_id.node",
states: { $push: { state: "$_id.state", stateful: "$_id.stateful", count: "$total"}}
}
},
]) |
...
Code Block |
---|
|
db.getCollection('events').aggregate([
{
$match: { "time": { "$gte": 1577836800 }, "acknowledged": { "$eq": 0 } }
},
{
$group: {
_id: { event: "$event", priority: "$priority" },
priorities: { $push: "$priority" },
total: { $sum: 1 }
}
},
{
$group: {
_id: "$_id.event",
priorities: { $push: { priority: "$_id.priority", count: "$total" } },
}
}
]) |
Priority sorted for all events which are not acknowledged
Code Block |
---|
|
db.getCollection('events').aggregate([
{
$match: { "time": { "$gte": 1577836800 }, "acknowledged": { "$eq": 0 } }
},
{
$group: {
_id: "$priority",
total: { $sum: 1 }
}
},
{$sort: {"_id": 1}}
]) |
Image Added