Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 71 additions & 48 deletions graphql_server/mongodb_queries/grouped_nodes_timeseries.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,82 @@
const query = [
{
$group: {
_id: "$timestamp",
sumNodes: {
$sum: "$content.state.nodes"
}
module.exports = function(args) {
const pipeline = [];

// 1. Dynamic Match Stage (Filter by Date)
// If start or end dates are provided, we filter the 'timestamp' field first.
// This improves performance by reducing the amount of data processed in later stages.
const matchQuery = {};

if (args && (args.start || args.end)) {
matchQuery.timestamp = {};
if (args.start) {
matchQuery.timestamp.$gte = new Date(args.start);
}
},
{
$project: {
date: {
$dateToParts: {
date: "$_id"
}
},
sumNodes: 1
if (args.end) {
matchQuery.timestamp.$lte = new Date(args.end);
}
},
{
$group: {
_id: {
date: {
year: "$date.year",
month: "$date.month"
// Add the match stage to the start of the pipeline
pipeline.push({ $match: matchQuery });
}

// 2. Original Pipeline Stages
// These stages group the data by timestamp, calculate averages, and format the output.
pipeline.push(
{
$group: {
_id: "$timestamp",
sumNodes: {
$sum: "$content.state.nodes"
}
},
avgNodes: {
$avg: "$sumNodes"
}
}
},
{
$sort: {
_id: 1
}
},
{
$project: {
_id: 0,
date: {
$dateToString: {
format: "%Y-%m",
},
{
$project: {
date: {
$dateToParts: {
date: "$_id"
}
},
sumNodes: 1
}
},
{
$group: {
_id: {
date: {
$dateFromParts: {
year: "$_id.date.year",
month: "$_id.date.month"
year: "$date.year",
month: "$date.month"
}
},
avgNodes: {
$avg: "$sumNodes"
}
}
},
{
$sort: {
_id: 1
}
},
{
$project: {
_id: 0,
date: {
$dateToString: {
format: "%Y-%m",
date: {
$dateFromParts: {
year: "$_id.date.year",
month: "$_id.date.month"
}
}
}
},
avgNodes: {
$toInt: "$avgNodes"
}
},
avgNodes: {
$toInt: "$avgNodes"
}
}
}
];
);

module.exports = query;
return pipeline;
};
236 changes: 127 additions & 109 deletions graphql_server/mongodb_queries/routing_protocols.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,141 @@
const query = [
{
$match: {
"content.techDetails.routing": {
$exists: true,
$ne: ""
}
}
},
{
$project: {
_id: 0,
timestamp: {
$dateToParts: {
date: "$timestamp"
module.exports = function(args) {
const pipeline = [];

// 1. Dynamic Match Stage (Date Filter)
// We add this specific check for 'args' to ensure we only filter if the user asked for it.
const matchDate = {};
if (args && (args.start || args.end)) {
matchDate.timestamp = {};
if (args.start) matchDate.timestamp.$gte = new Date(args.start);
if (args.end) matchDate.timestamp.$lte = new Date(args.end);

// Push this stage first for performance (indexes work best here)
pipeline.push({ $match: matchDate });
}

// 2. Existing Pipeline Stages
// We push the rest of your original query logic into the pipeline array.
pipeline.push(
{
$match: {
"content.techDetails.routing": {
$exists: true,
$ne: ""
}
},
routing: "$content.techDetails.routing"
}
},
{
$unwind: {
path: "$routing"
}
},
{
$group: {
_id: {
timestamp: "$timestamp",
routing: "$routing"
},
seen: {
$sum: 1
}
}
},
{
$group: {
_id: {
date: {
year: "$_id.timestamp.year",
month: "$_id.timestamp.month"
},
{
$project: {
_id: 0,
timestamp: {
$dateToParts: {
date: "$timestamp"
}
},
routing: "$_id.routing"
},
seen: {
$avg: "$seen"
routing: "$content.techDetails.routing"
}
}
},
{
$project: {
_id: 0,
date: {
$dateToString: {
format: "%Y-%m",
},
{
$unwind: {
path: "$routing"
}
},
{
$group: {
_id: {
timestamp: "$timestamp",
routing: "$routing"
},
seen: {
$sum: 1
}
}
},
{
$group: {
_id: {
date: {
$dateFromParts: {
year: "$_id.date.year",
month: "$_id.date.month"
year: "$_id.timestamp.year",
month: "$_id.timestamp.month"
},
routing: "$_id.routing"
},
seen: {
$avg: "$seen"
}
}
},
{
$project: {
_id: 0,
date: {
$dateToString: {
format: "%Y-%m",
date: {
$dateFromParts: {
year: "$_id.date.year",
month: "$_id.date.month"
}
}
}
}
},
routingTech: {
$cond: {
if: {
$in: [
"$_id.routing",
[
"B.A.T.M.A.N. advanced",
"B.A.T.M.A.N advanced",
"BATMAN",
"B.A.T.M.A.N.-adv"
},
routingTech: {
$cond: {
if: {
$in: [
"$_id.routing",
[
"B.A.T.M.A.N. advanced",
"B.A.T.M.A.N advanced",
"BATMAN",
"B.A.T.M.A.N.-adv"
]
]
]
},
then: "batman-adv",
else: "$_id.routing"
},
then: "batman-adv",
else: "$_id.routing"
}
},
seen: {
$toInt: "$seen"
}
},
seen: {
$toInt: "$seen"
}
}
},
{
$group: {
_id: {
routingTech: "$routingTech",
date: "$date"
},
seen: {
$sum: "$seen"
},
{
$group: {
_id: {
routingTech: "$routingTech",
date: "$date"
},
seen: {
$sum: "$seen"
}
}
}
},
{
$project: {
date: "$_id.date",
routingTech: "$_id.routingTech",
seen: true,
_id: 0
}
},
{
$match: {
routingTech: {
$ne: ""
},
seen: {
$gt: 1
},
{
$project: {
date: "$_id.date",
routingTech: "$_id.routingTech",
seen: true,
_id: 0
}
},
{
$match: {
routingTech: {
$ne: ""
},
seen: {
$gt: 1
}
}
},
{
$sort: {
date: 1
}
}
},
{
$sort: {
date: 1
}
}
];
);

module.exports = query;
return pipeline;
};
Loading