{"size": 6661, "ext": "js", "max_stars_repo_path": "test/runtime/adapter-read-timeseries.spec.js", "max_stars_repo_name": "mysky528/juttle", "max_stars_repo_stars_event_min_datetime": "2015-12-18T22:53:10.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-27T11:17:46.000Z", "max_issues_repo_path": "test/runtime/adapter-read-timeseries.spec.js", "max_issues_repo_name": "zhouzhou77/juttle", "max_issues_repo_issues_event_min_datetime": "2015-12-18T23:40:56.000Z", "max_issues_repo_issues_event_max_datetime": "2017-10-10T12:16:31.000Z", "max_forks_repo_path": "test/runtime/adapter-read-timeseries.spec.js", "max_forks_repo_name": "zhouzhou77/juttle", "max_forks_repo_forks_event_min_datetime": "2015-12-18T22:57:35.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-09T09:10:56.000Z"}
{"max_stars_count": 338, "max_issues_count": 654, "max_forks_count": 32, "avg_line_length": 39.6488095238, "max_line_length": 116, "alphanum_fraction": 0.5143371866}
'use strict';

var juttle_test_utils = require('./specs/juttle-test-utils');
var check_juttle = juttle_test_utils.check_juttle;
var expect = require('chai').expect;
var JuttleMoment = require('../../lib/runtime/types/juttle-moment');

describe('read testTimeseries', function () {
    it('requires -from or -to', function() {
        return check_juttle({
            program: 'read testTimeseries'
        })
        .then(function(result) {
            throw new Error('unexpected success');
        })
        .catch(function(err) {
            expect(err.code).equal('MISSING-TIME-RANGE');
            expect(err.message).equal('One of -from, -to, or -last must be specified to define a query time range');
        });
    });

    it('requires -from to be before -to', function() {
        return check_juttle({
            program: 'read testTimeseries -from :2014-01-01: -to :2014-01-01:'
        })
        .then(function(result) {
            throw new Error('unexpected success');
        })
        .catch(function(err) {
            expect(err.code).equal('TO-BEFORE-FROM-MOMENT-ERROR');
            expect(err.message).equal('-from must be before -to');
        });
    });

    it('handles pure historical mode', function() {
        return check_juttle({
            program: 'read testTimeseries -from :2015-01-01: -to :2015-01-05: -every :1d:'
        })
        .then(function(result) {
            var expected = [
                { time: '2015-01-01T00:00:00.000Z', count: 0 },
                { time: '2015-01-02T00:00:00.000Z', count: 1 },
                { time: '2015-01-03T00:00:00.000Z', count: 2 },
                { time: '2015-01-04T00:00:00.000Z', count: 3 }
            ];
            expect(result.sinks.table).deep.equal(expected);
        });
    });

    it('handles pure live mode with a default lag', function() {
        return check_juttle({
            program: 'read testTimeseries -to :end: ' +
                '| put dtProg = Math.floor(Duration.seconds(time - :now:)) ' +
                '| put dtReal = Math.floor(Duration.seconds(Date.time() - time)) ' +
                '| keep count, dtProg, dtReal',
            realtime: true,
            deactivateAfter: 3000
        })
        .then(function(result) {
            expect(result.graph.readEvery.milliseconds()).equal(500);
            expect(result.graph.lag.milliseconds()).equal(1000);
            var expected = [
                { count: 0, dtProg: 0, dtReal: 1 },
                { count: 1, dtProg: 0, dtReal: 1 },
                { count: 2, dtProg: 1, dtReal: 1 },
                { count: 3, dtProg: 1, dtReal: 1 }
            ];
            expect(result.sinks.table).deep.equal(expected);
        });
    });

    it('handles pure live mode with a configurable lag', function() {
        this.timeout(10000);
        return check_juttle({
            program: 'read testTimeseries -to :end: -lag :2s: -every :1s:' +
                '| put dtProg = Math.floor(Duration.seconds(time - :now:)) ' +
                '| put dtReal = Math.floor(Duration.seconds(Date.time() - time)) ' +
                '| keep count, dtProg, dtReal',
            realtime: true,
            deactivateAfter: 5000
        })
        .then(function(result) {
            var expected = [
                { count: 0, dtProg: 0, dtReal: 3 },
                { count: 1, dtProg: 1, dtReal: 3 },
                { count: 2, dtProg: 2, dtReal: 3 }
            ];
            expect(result.sinks.table).deep.equal(expected);
        });
    });

    it('properly emits ticks in live mode', function() {
        this.timeout(10000);
        return check_juttle({
            program: 'read testTimeseries -to :end: -every :2s: -lag :0s:' +
                '| put dtProg = Math.floor(Duration.seconds(time - :now:)) ' +
                '| put dtReal = Math.floor(Duration.seconds(Date.time() - time)) ' +
                '| keep count, dtProg, dtReal' +
                '| view result -ticks true -dt true',
            realtime: true,
            deactivateAfter: 4000
        })
        .then(function(result) {
            var expected = [
                { count: 0, dtProg: 0, dtReal: 2 },
                { tick: true, dt: '00:00:01.000'},
                { count: 1, dtProg: 2, dtReal: 2 },
                { tick: true, dt: '00:00:03.000'}
            ];
            expect(result.sinks.result).deep.equal(expected);
        });
    });

    it('handles mixed historical and live mode', function() {
        this.timeout(10000);
        return check_juttle({
            program: 'read testTimeseries -from :-2s: -to :end: -every :1s: -lag :0s:' +
                '| put dtProg = Math.floor(Duration.seconds(time - :now:)) ' +
                '| put dtReal = Math.floor(Duration.seconds(Date.time() - time)) ' +
                '| keep count, dtProg, dtReal',
            realtime: true,
            deactivateAfter: 3000
        })
        .then(function(result) {
            var expected = [
                { count: 0, dtProg: -2, dtReal: 2 },
                { count: 1, dtProg: -1, dtReal: 1 },
                { count: 2, dtProg: 0, dtReal: 1 },
                { count: 3, dtProg: 1, dtReal: 1 },
                { count: 4, dtProg: 2, dtReal: 1 }
            ];
            expect(result.sinks.table).deep.equal(expected);
        });
    });

    it('optimizes a live read -every', function() {
        return check_juttle({
            program: 'read testTimeseries -to :end: | reduce -every :s: count()',
            realtime: true,
            deactivateAfter: 3000
        })
        .then(function(result) {
            var second = JuttleMoment.duration(1);
            result.prog.graph.adapter.time_ranges.forEach(function(range, i) {
                var from = range.from;
                var to = range.to;
                expect(JuttleMoment.quantize(to, second).milliseconds()).equal(to.milliseconds());
                if (i > 0) {
                    expect(JuttleMoment.quantize(from, second).milliseconds()).equal(from.milliseconds());
                    expect(JuttleMoment.add(from, second).eq(to)).equal(true);
                }
            });

            expect(result.graph.readEvery.milliseconds()).equal(1000);
            expect(result.graph.lag.milliseconds()).equal(1000);
        });
    });

    it('rejects read -every | reduce -every', function() {
        var failing_read = check_juttle({
            program: 'read testTimeseries -every :s: | reduce -every :m: count()'
        });

        var message = 'cannot read -every | reduce -every';
        return juttle_test_utils.expect_to_fail(failing_read, message);
    });
});
