summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_simple.js
blob: ab20a90d0964d1507c324638671f6ec8e392bdde (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
/**
 * Tests inserting sample data into the time-series buckets collection.
 * This test is for the simple case of only one measurement per bucket.
 * @tags: [
 *   does_not_support_stepdowns,
 *   does_not_support_transactions,
 *   requires_getmore,
 * ]
 */
(function() {
"use strict";

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

TimeseriesTest.run((insert) => {
    const coll = db.timeseries_simple;
    const bucketsColl = db.getCollection('system.buckets.' + coll.getName());

    coll.drop();

    const timeFieldName = 'time';
    assert.commandWorked(
        db.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}}));
    assert.contains(bucketsColl.getName(), db.getCollectionNames());

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

    /**
     * Updates min and max values in expected control document in bucket with most recent
     * measurement. Use bsonWoCompare() to handle non-numerical values (such as ObjectId() for _id).
     */
    function updateControlDoc(controlDoc, key, newVal) {
        if (!controlDoc.min.hasOwnProperty(key)) {
            if (key === timeFieldName) {
                // Time field must be rounded down to nearest minute.
                controlDoc.min[key] = new Date(newVal - (newVal % 60000));
            } else {
                controlDoc.min[key] = newVal;
            }
        } else if (bsonWoCompare(newVal, controlDoc.min[key]) < 0) {
            controlDoc.min[key] = newVal;
        }
        if (!controlDoc.max.hasOwnProperty(key)) {
            controlDoc.max[key] = newVal;
        } else if (bsonWoCompare(newVal, controlDoc.max[key]) > 0) {
            controlDoc.max[key] = newVal;
        }
    }

    const controlVersion = 1;
    const numDocs = 100;
    const expectedBucketDoc = {
        control: {
            version: controlVersion,
            min: {},
            max: {},
        },
        // no 'meta' field defined.
        data: {},
    };

    for (let i = 0; i < numDocs; i++) {
        const host = TimeseriesTest.getRandomElem(hosts);
        TimeseriesTest.updateUsages(host.fields);

        // Ignore host.tags because we did not provide 'metaField' during collection creation.
        const t = ISODate();
        const doc = Object.assign({_id: i, [timeFieldName]: t}, host.fields);

        jsTestLog('Inserting doc into time-series collection: ' + i + ': ' + tojson(doc));
        let start = new Date();
        assert.commandWorked(insert(coll, doc));
        jsTestLog('Insertion took ' + ((new Date()).getTime() - start.getTime()) +
                  ' ms. Retrieving doc from view: ' + i);
        start = new Date();
        const docFromView = coll.findOne({_id: doc._id});
        assert(docFromView,
               'inserted doc missing from time-series view: ' + i + ': ' + tojson(doc));
        jsTestLog('Doc retrieval took ' + ((new Date()).getTime() - start.getTime()) +
                  ' ms. Doc fetched from view: ' + i + ': ' + tojson(docFromView));
        assert.docEq(doc, docFromView, 'Invalid document retrieved from view: ' + i);

        // Update expected control min/max and data in bucket.
        Object.keys(doc).forEach((key) => {
            updateControlDoc(expectedBucketDoc.control, key, doc[key]);
        });
        Object.keys(doc).forEach((key) => {
            if (!expectedBucketDoc.data.hasOwnProperty(key)) {
                expectedBucketDoc.data[key] = {};
            }
            expectedBucketDoc.data[key][i] = doc[key];
        });
    }

    // Check view.
    const viewDocs = coll.find().toArray();
    assert.eq(numDocs, viewDocs.length, viewDocs);

    // Check bucket collection.
    const bucketDocs = bucketsColl.find().toArray();
    assert.eq(1, bucketDocs.length, bucketDocs);
    const bucketDoc = bucketDocs[0];
    jsTestLog('Bucket collection document: ' + tojson(bucketDoc));
    assert.docEq(expectedBucketDoc.control.min,
                 bucketDoc.control.min,
                 'invalid min in bucket: ' + tojson(bucketDoc));
    assert.docEq(expectedBucketDoc.control.max,
                 bucketDoc.control.max,
                 'invalid max in bucket: ' + tojson(bucketDoc));
    Object.keys(expectedBucketDoc.data).forEach((key) => {
        assert.docEq(expectedBucketDoc.data[key],
                     bucketDoc.data[key],
                     'invalid bucket data for field ' + key + ': ' + tojson(bucketDoc));
    });
});
})();