256 lines
7.0 KiB
JavaScript
256 lines
7.0 KiB
JavaScript
/* global use, db */
|
|
// MongoDB Playground
|
|
// To disable this template go to Settings | MongoDB | Use Default Template For Playground.
|
|
// Make sure you are connected to enable completions and to be able to run a playground.
|
|
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
|
|
// The result of the last command run in a playground is shown on the results panel.
|
|
// By default the first 20 documents will be returned with a cursor.
|
|
// Use 'console.log()' to print to the debug output.
|
|
// For more documentation on playgrounds please refer to
|
|
// https://www.mongodb.com/docs/mongodb-vscode/playgrounds/
|
|
use("statistics");
|
|
|
|
let formattedDate = (date) => ({
|
|
unix: { $toLong: date },
|
|
iso: { $toString: date },
|
|
});
|
|
|
|
// Find the minimum timestamp and calculate the maximum timestamp (one hour later) for a collection
|
|
function getTimeWindow(collectionName) {
|
|
const minTimestamp = db.getCollection(collectionName).aggregate([
|
|
{ $sort: { timestamp: 1 } },
|
|
{ $limit: 1 },
|
|
{ $project: { _id: 0, timestamp: 1 } }
|
|
]).toArray()[0]?.timestamp;
|
|
|
|
if (!minTimestamp) return null;
|
|
|
|
const maxTimestamp = new Date(minTimestamp.getTime() + 60 * 60 * 1000);
|
|
return { minTimestamp, maxTimestamp };
|
|
}
|
|
|
|
// Get time windows for both collections
|
|
const highLatencyTimeWindow = getTimeWindow("peertube_hetzner_high_latency");
|
|
const defaultLatencyTimeWindow = getTimeWindow("peertube_hetzner_default_latency");
|
|
|
|
// Function to perform the aggregation for a given collection
|
|
function getAggregationResult(collectionName, timeWindow) {
|
|
if (!timeWindow) return [];
|
|
|
|
return db.getCollection(collectionName).aggregate([
|
|
// Filter documents within the collection's specific time window
|
|
{
|
|
$match: {
|
|
timestamp: {
|
|
$gte: timeWindow.minTimestamp,
|
|
$lt: timeWindow.maxTimestamp
|
|
}
|
|
}
|
|
},
|
|
{
|
|
$unwind: "$peers"
|
|
},
|
|
{
|
|
$match: {
|
|
"peers.iceConnectionState": "connected"
|
|
}
|
|
},
|
|
{
|
|
$unwind: "$peers.values"
|
|
},
|
|
{
|
|
$match: {
|
|
"peers.values.type": "data-channel",
|
|
}
|
|
},
|
|
{
|
|
$group: {
|
|
_id: "$peers.id",
|
|
maxBytesReceived: {
|
|
$max: "$peers.values.bytesReceived"
|
|
},
|
|
maxBytesSent: {
|
|
$max: "$peers.values.bytesSent"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
$group: {
|
|
_id: null,
|
|
totalMaxBytesReceived: {
|
|
$sum: "$maxBytesReceived"
|
|
},
|
|
totalMaxBytesSent: {
|
|
$sum: "$maxBytesSent"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
$project: {
|
|
_id: 0,
|
|
collection: collectionName,
|
|
totalBytesReceived: "$totalMaxBytesReceived",
|
|
totalBytesSent: "$totalMaxBytesSent",
|
|
formattedBytesReceived: {
|
|
$switch: {
|
|
branches: [
|
|
{
|
|
case: {
|
|
$gte: [
|
|
"$totalMaxBytesReceived",
|
|
1073741824 // 1024^3
|
|
]
|
|
},
|
|
then: {
|
|
$concat: [
|
|
{
|
|
$toString: {
|
|
$divide: [
|
|
"$totalMaxBytesReceived",
|
|
1073741824 // 1024^3
|
|
]
|
|
}
|
|
},
|
|
" GiB"
|
|
]
|
|
}
|
|
},
|
|
{
|
|
case: {
|
|
$gte: [
|
|
"$totalMaxBytesReceived",
|
|
1048576 // 1024^2
|
|
]
|
|
},
|
|
then: {
|
|
$concat: [
|
|
{
|
|
$toString: {
|
|
$divide: [
|
|
"$totalMaxBytesReceived",
|
|
1048576 // 1024^2
|
|
]
|
|
}
|
|
},
|
|
" MiB"
|
|
]
|
|
}
|
|
},
|
|
{
|
|
case: {
|
|
$gte: [
|
|
"$totalMaxBytesReceived",
|
|
1024 // 1024^1
|
|
]
|
|
},
|
|
then: {
|
|
$concat: [
|
|
{
|
|
$toString: {
|
|
$divide: [
|
|
"$totalMaxBytesReceived",
|
|
1024 // 1024^1
|
|
]
|
|
}
|
|
},
|
|
" KiB"
|
|
]
|
|
}
|
|
}
|
|
],
|
|
default: {
|
|
$concat: [
|
|
{
|
|
$toString:
|
|
"$totalMaxBytesReceived"
|
|
},
|
|
" bytes"
|
|
]
|
|
}
|
|
}
|
|
},
|
|
formattedBytesSent: {
|
|
$switch: {
|
|
branches: [
|
|
{
|
|
case: {
|
|
$gte: [
|
|
"$totalMaxBytesSent",
|
|
1073741824 // 1024^3
|
|
]
|
|
},
|
|
then: {
|
|
$concat: [
|
|
{
|
|
$toString: {
|
|
$divide: [
|
|
"$totalMaxBytesSent",
|
|
1073741824 // 1024^3
|
|
]
|
|
}
|
|
},
|
|
" GiB"
|
|
]
|
|
}
|
|
},
|
|
{
|
|
case: {
|
|
$gte: [
|
|
"$totalMaxBytesSent",
|
|
1048576 // 1024^2
|
|
]
|
|
},
|
|
then: {
|
|
$concat: [
|
|
{
|
|
$toString: {
|
|
$divide: [
|
|
"$totalMaxBytesSent",
|
|
1048576 // 1024^2
|
|
]
|
|
}
|
|
},
|
|
" MiB"
|
|
]
|
|
}
|
|
},
|
|
{
|
|
case: {
|
|
$gte: ["$totalMaxBytesSent", 1024] // 1024^1
|
|
},
|
|
then: {
|
|
$concat: [
|
|
{
|
|
$toString: {
|
|
$divide: [
|
|
"$totalMaxBytesSent",
|
|
1024 // 1024^1
|
|
]
|
|
}
|
|
},
|
|
" KiB"
|
|
]
|
|
}
|
|
}
|
|
],
|
|
default: {
|
|
$concat: [
|
|
{ $toString: "$totalMaxBytesSent" },
|
|
" bytes"
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]).toArray();
|
|
}
|
|
|
|
// Get results from both collections using their respective time windows
|
|
const highLatencyResults = getAggregationResult("peertube_hetzner_high_latency", highLatencyTimeWindow);
|
|
const defaultLatencyResults = getAggregationResult("peertube_hetzner_default_latency", defaultLatencyTimeWindow);
|
|
|
|
// Combine and return the results
|
|
const combinedResults = [...highLatencyResults, ...defaultLatencyResults];
|
|
combinedResults;
|