summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_graph_lookup.js
blob: 29786acb8fb2334a766905b7082b60c2ae0b3b26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * Verifies that time-series bucket collections work as expected with $graphLookup.
 *
 *
 * @tags: [
 *     assumes_unsharded_collection,
 *     assumes_against_mongod_not_mongos,
 *     requires_timeseries,
 *     requires_fcv_49,
 *     sbe_incompatible,
 * ]
 */
(function() {
"use strict";

load("jstests/core/timeseries/libs/timeseries.js");

if (!TimeseriesTest.timeseriesCollectionsEnabled(db.getMongo())) {
    jsTestLog("Skipping test because the time-series collection feature flag is disabled");
    return;
}

const testDB = db.getSiblingDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());
const timeFieldName = "time";
const hostIdFieldName = "hostid";
const nonTimeseriesCollOption = null;
const timeseriesCollOption = {
    timeseries: {timeField: timeFieldName, metaField: hostIdFieldName}
};
const numHosts = 10;
const numDocs = 200;

Random.setRandomSeed();
const hosts = TimeseriesTest.generateHosts(numHosts);

let testFunc = function(collAOption, collBOption) {
    // Prepares two collections. Each collection can be either a time-series or a non time-series
    // collection, depending on collAOption/collBOption.
    const collA = testDB.getCollection("a");
    const collB = testDB.getCollection("b");
    collA.drop();
    collB.drop();
    assert.commandWorked(testDB.createCollection(collA.getName(), collAOption));
    assert.commandWorked(testDB.createCollection(collB.getName(), collBOption));
    let entryCountPerHost = new Array(numHosts).fill(0);
    let entryCountOver80AndExistsIdlePerHost = new Array(numHosts).fill(0);

    // Inserts into collA, one entry per host.
    for (let i = 0; i < numHosts; i++) {
        let host = hosts[i];
        assert.commandWorked(collA.insert({time: ISODate(), hostid: host.tags.hostid}));
    }

    // Inserts some random documents to collB. The 'idle' measurement is inserted only when usage is
    // odd.
    for (let i = 0; i < numDocs; i++) {
        let host = TimeseriesTest.getRandomElem(hosts);
        let usage = TimeseriesTest.getRandomUsage();
        if (usage % 2) {
            assert.commandWorked(collB.insert(
                {time: ISODate(), hostid: host.tags.hostid, cpu: usage, idle: 100 - usage}));
        } else {
            assert.commandWorked(
                collB.insert({time: ISODate(), hostid: host.tags.hostid, cpu: usage}));
        }

        // These counts are to test metaField match.
        entryCountPerHost[host.tags.hostid]++;

        // These counts are to test measurement fields match which are specified by $graphLookup's
        // restrictSearchWithMatch.
        if (usage > 80 && usage % 2) {
            entryCountOver80AndExistsIdlePerHost[host.tags.hostid]++;
        }
    }

    // Verifies that a meta field "hostid" works with $graphLookup.
    let results = collA.aggregate([
            {
                $graphLookup: {
                    from: collB.getName(),
                    startWith: "$hostid",
                    connectFromField: "hostid",
                    connectToField: "hostid",
                    as: "matchedB",
                    maxDepth: 0
                }
            }, {
                $project: {
                    _id: 0,
                    hostid: 1,
                    matchedB: {
                        $size: "$matchedB"
                    }
                }
            },
            {$sort: {hostid: 1}}
        ]).toArray();

    assert.eq(numHosts, results.length, results);

    for (let i = 0; i < numHosts; i++) {
        assert.eq({hostid: i, matchedB: entryCountPerHost[i]}, results[i], results);
    }

    // Verifies that measurement fields "cpu" and "idle" work with $graphLookup as expected.
    results = collA.aggregate([
        {
            $graphLookup: {
                from: collB.getName(),
                startWith: "$hostid",
                connectFromField: "hostid",
                connectToField: "hostid",
                as: "matchedB",
                maxDepth: 0,
                restrictSearchWithMatch: {
                    cpu: {$gt: 80},             // Tests measurement "cpu".
                    idle: {$exists: true}       // Tests the existence of measurement "idle".
                }
            }
        }, {
            $project: {
                _id: 0,
                hostid: 1,
                matchedB: {
                    $size: "$matchedB"
                }
            }
        },
        {$sort: {hostid: 1}}
    ]).toArray();

    assert.eq(numHosts, results.length, results);

    for (let i = 0; i < numHosts; i++) {
        let expectedCount = entryCountOver80AndExistsIdlePerHost[i];
        assert.eq(
            {hostid: i, matchedB: expectedCount}, results[i], entryCountOver80AndExistsIdlePerHost);
    }
};

// Tests case #1: collA: non time-series, collB: time-series
var collAOption = nonTimeseriesCollOption;
var collBOption = timeseriesCollOption;
testFunc(collAOption, collBOption);

// Tests case #2: collA: time-series, collB: non time-series
collAOption = timeseriesCollOption;
collBOption = nonTimeseriesCollOption;
testFunc(collAOption, collBOption);

// Tests case #3: collA: time-series, collB: time-series
collAOption = timeseriesCollOption;
collBOption = timeseriesCollOption;
testFunc(collAOption, collBOption);
})();