Automating console demos

#1

Here is some code that can be used to script demos in the console (tested with v7.6.1, Chrome), from data import (please be gentle by not loading GBs this way) to logging on the console to visualization.

The bricks:

function uploadFilePromise(ct, prompt) {
    return new Promise(function(ok, ko) {
        if (prompt)
            ct = window.prompt('Enter filename prefix', ct);
        var input = $('<input type="file" accept="' + ct + '*.*"/>');
        input.change(function(e) {
            var file = e.target.files[0];
            console.log('onchange', file.name, ct, file.name.startsWith(ct));
            if (file.name.startsWith(ct)) {
                var reader = new FileReader();
                reader.onload = function(e) {
                    console.log('onload', e);
                    var b = ContentValue.make({
                        content: e.target.result,
                        contentType: file.type,
                        contentLocation: 'client:/' + file.name,
                        contentLength: file.size
                    });
                    ok([b, ct]);
                };
                reader.readAsArrayBuffer(file);
            } else {
                var error = 'File name must start with ' + ct;
                window.alert(error);
                ko(error);
            }
        });
        console.log('prompt', input, ct, this);
        input.click();
    });
}

function importFile(tname) {
    return uploadFilePromise(tname)
        .then(logAs('Uploaded'))
        .then(importCsv)
        .then(logAs('Imported'))
        .catch(logAs('Caught while importing'));
}

var decoder = new TextDecoder('utf-8');

function importCsv(cvCt) {
    var typeRef = DeepRef.make({
        typeName: cvCt[1]
    }).toType();
    return typeRef.importData({
        content: {
            contentType: 'text/csv',
            content: decoder.decode(cvCt[0].content)
        }
    }).stats.targetTypeStats;
}

function logAs(prefix) {
    return function(v) {
        console.log(prefix, v);
        return v;
    };
}

function logAlert(text) {
    return function(v) {
        window.alert(text);
        console.log(text);
        return v;
    };
}

and an example of building a demo using the above bricks:

function demo_xyz() {
    var filename = [
        'CanonicalMeasurement0.csv',
        'CanonicalMeasurement1.csv'
    ];
    var p;
    Promise.resolve()
        .then(c3Viz('Demo XYZ'))
        .then(logAlert('Please load ' + filename[0]))
        .then(p = importFile('CanonicalMeasurement')); // 0.csv
    p.then(logAlert('Please load ' + filename[1]))
        .then(p = importFile('CanonicalMeasurement')); // 1.csv
    p.then(logAlert('Test: one week on, three off, one on (2016)\n' +
            'Raw measurements: initial 0 on 2015-07-23,\n' +
            'start on 2016-06-19, ' +
            'missing on 2016-06-22, 2016-06-26--2016-07-18, ' +
            'overflow on 2016-07-23, end on 2016-07-25'))
        .then(function() {
            return Facility.evalMetrics({
                ids: ['s00110-b001-gen001'],
                expressions: [
                    'GeneralGasVolumeConsumption',
                    'estimates(GeneralGasVolumeConsumption)*30',
                    'missing(GeneralGasVolumeConsumption)*30'
                ],
                start: '2016-06-15',
                end: '2016-07-31',
                interval: 'DAY'
                // unitId: 'litre'
            });
        })
        .then(logAs('Resulting timeseries'))
        .then(c3Viz)
        .then(function() {
            return PointMeasurement.removeAll(
                Filter.eq('parent.id', 'sappel_izar dp_30500147_volume')
                    .and.ge('start', new DateTime('2016-06-19'))
                    .and.le('start', new DateTime('2016-07-25')));
        })
        .then(logAs('PointMeasurement objects removed:'))
        .catch(logAs('Caught while loading ' +
            'CanonicalMeasurement{0,1}.csv'));
}

Note that the variable p is always set to the last importFilePromise's result, and that thunks are used to delay the execution of evalMetrics and removeAll.

In Chrome’s dev tools, you can go to Sources/Filesystem and open a local directory with your demo scripts.

You may also find the function below useful:

function pReduce(l, fn) {
    var p = _.reduce(
        l,
        function(pr, e) { return pr.then(fn(e)); },
        Promise.resolve());
    return p;
}

to import multiple csv files:

function loadReferentials() {
    var q = [
        'CanonicalSite',
        'CanonicalBuilding',
        'CanonicalZone',
        'CanonicalMeter',
        'CanonicalService'
    ];
    pReduce(q, importFile)
        .then(logAs('Please follow the above prompts in order'))
        .catch(logAs('Caught while loading referentials'));
}
0 Likes

Running Jasmine tests in C3 console