all files / common/models/measure/ measure.js

90.72% Statements 88/97
78.05% Branches 32/41
85% Functions 17/20
90.63% Lines 87/96
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178  68×           68× 68× 68× 68× 68× 68×   68× 68× 68×   68× 68×   33×   89× 33× 56× 61×             12×                     12×           18× 18× 18× 18×                                   10× 10× 10×         10×         45× 45×                         59×         59×   59×       28×                              
"use strict";
var immutable_class_1 = require('immutable-class');
var numeral = require('numeral');
var plywood_1 = require('plywood');
var general_1 = require('../../utils/general/general');
function formatFnFactory(format) {
    return function (n) {
        if (isNaN(n) || !isFinite(n))
            return '-';
        return numeral(n).format(format);
    };
}
var check;
var Measure = (function () {
    function Measure(parameters) {
        var name = parameters.name;
        general_1.verifyUrlSafeName(name);
        this.name = name;
        this.title = parameters.title || general_1.makeTitle(name);
        var expression = parameters.expression;
        Iif (!expression)
            throw new Error('measure must have expression');
        this.expression = expression;
        var format = parameters.format || Measure.DEFAULT_FORMAT;
        Iif (format[0] === '(')
            throw new Error('can not have format that uses ( )');
        this.format = format;
        this.formatFn = formatFnFactory(format);
    }
    Measure.isMeasure = function (candidate) {
        return immutable_class_1.isInstanceOf(candidate, Measure);
    };
    Measure.getMeasure = function (measures, measureName) {
        if (!measureName)
            return null;
        measureName = measureName.toLowerCase(); // Case insensitive
        return measures.find(function (measure) { return measure.name.toLowerCase() === measureName; });
    };
    /**
     * Look for all instances of aggregateAction($blah) and return the blahs
     * @param ex
     * @returns {string[]}
     */
    Measure.getAggregateReferences = function (ex) {
        var references = [];
        ex.forEach(function (ex) {
            if (ex instanceof plywood_1.ChainExpression) {
                var actions = ex.actions;
                for (var _i = 0, actions_1 = actions; _i < actions_1.length; _i++) {
                    var action = actions_1[_i];
                    if (action.isAggregate()) {
                        references = references.concat(action.getFreeReferences());
                    }
                }
            }
        });
        return plywood_1.helper.deduplicateSort(references);
    };
    /**
     * Look for all instances of countDistinct($blah) and return the blahs
     * @param ex
     * @returns {string[]}
     */
    Measure.getCountDistinctReferences = function (ex) {
        var references = [];
        ex.forEach(function (ex) {
            if (ex instanceof plywood_1.ChainExpression) {
                var actions = ex.actions;
                for (var _i = 0, actions_2 = actions; _i < actions_2.length; _i++) {
                    var action = actions_2[_i];
                    if (action.action === 'countDistinct') {
                        references = references.concat(action.getFreeReferences());
                    }
                }
            }
        });
        return plywood_1.helper.deduplicateSort(references);
    };
    Measure.measuresFromAttributeInfo = function (attribute) {
        var name = attribute.name, special = attribute.special;
        var $main = plywood_1.$('main');
        var ref = plywood_1.$(name);
        if (special) {
            if (special === 'unique') {
                return [
                    new Measure({
                        name: general_1.makeUrlSafeName(name),
                        expression: $main.countDistinct(ref)
                    })
                ];
            }
            else Eif (special === 'histogram') {
                return [
                    new Measure({
                        name: general_1.makeUrlSafeName(name + '_p95'),
                        expression: $main.quantile(ref, 0.95)
                    }),
                    new Measure({
                        name: general_1.makeUrlSafeName(name + '_p99'),
                        expression: $main.quantile(ref, 0.99)
                    })
                ];
            }
        }
        var expression = $main.sum(ref);
        var makerAction = attribute.makerAction;
        if (makerAction) {
            switch (makerAction.action) {
                case 'min':
                    expression = $main.min(ref);
                    break;
                case 'max':
                    expression = $main.max(ref);
                    break;
            }
        }
        return [new Measure({
                name: general_1.makeUrlSafeName(name),
                expression: expression
            })];
    };
    Measure.fromJS = function (parameters) {
        var name = parameters.name;
        return new Measure({
            name: name,
            title: parameters.title,
            expression: parameters.expression ? plywood_1.Expression.fromJSLoose(parameters.expression) : plywood_1.$('main').sum(plywood_1.$(name)),
            format: parameters.format
        });
    };
    Measure.prototype.valueOf = function () {
        return {
            name: this.name,
            title: this.title,
            expression: this.expression,
            format: this.format
        };
    };
    Measure.prototype.toJS = function () {
        var js = {
            name: this.name,
            title: this.title,
            expression: this.expression.toJS()
        };
        Iif (this.format !== Measure.DEFAULT_FORMAT)
            js.format = this.format;
        return js;
    };
    Measure.prototype.toJSON = function () {
        return this.toJS();
    };
    Measure.prototype.toString = function () {
        return "[Measure: " + this.name + "]";
    };
    Measure.prototype.equals = function (other) {
        return Measure.isMeasure(other) &&
            this.name === other.name &&
            this.title === other.title &&
            this.expression.equals(other.expression) &&
            this.format === other.format;
    };
    Measure.prototype.toApplyAction = function () {
        var _a = this, name = _a.name, expression = _a.expression;
        return new plywood_1.ApplyAction({
            name: name,
            expression: expression
        });
    };
    Measure.prototype.formatDatum = function (datum) {
        return this.formatFn(datum[this.name]);
    };
    Measure.DEFAULT_FORMAT = '0,0.0 a';
    Measure.INTEGER_FORMAT = '0,0 a';
    return Measure;
}());
exports.Measure = Measure;
check = Measure;