All files / datamodel/src/operator group-by.js

90.14% Statements 64/71
69.05% Branches 29/42
100% Functions 14/14
90.14% Lines 64/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          18x 18x 18x 18x   18x 38x 36x 16x     2x       18x 36x                   18x                     18x 18x 18x 18x 18x 18x     18x 36x 3x   36x 32x   36x   18x                         15x 15x 15x 15x 15x 15x 15x 15x 15x 15x     15x 62x 42x 42x 30x 12x 12x         15x 15x 50x 50x 40x   50x 29x 29x 29x 26x   29x 58x   29x   21x 42x         15x 29x 29x 58x     15x         15x   15x        
import { extend2 } from '../utils';
import { rowDiffsetIterator } from './row-diffset-iterator';
import DataModel from '../export';
import reducerStore from '../utils/reducer-store';
 
/**
 * This function sanitize the user given field and return a common Array structure field
 * list
 * @param  {DataModel} dataModel the dataModel operating on
 * @param  {Array} fieldArr  user input of field Array
 * @return {Array}           arrays of field name
 */
function getFieldArr (dataModel, fieldArr) {
    const retArr = [];
    const fieldStore = dataModel.getPartialFieldspace();
    const dimensions = fieldStore.getDimension();
    const measures = fieldStore.getMeasure();
 
    Object.entries(dimensions).forEach(([key]) => {
        if (fieldArr && fieldArr.length) {
            if (fieldArr.indexOf(key) !== -1) {
                retArr.push(key);
            }
        } else {
            retArr.push(key);
        }
    });
 
    Object.entries(measures).forEach(([key]) => {
        Iif (measures[key].subType() === 'discrete') {
            if (fieldArr && fieldArr.length) {
                if (fieldArr.indexOf(key) !== -1) {
                    retArr.push(key);
                }
            } else {
                retArr.push(key);
            }
        }
    });
    return retArr;
}
 
/**
 * This sanitize the reducer provide by the user and create a common type of object.
 * user can give function Also
 * @param  {DataModel} dataModel     dataModel to worked on
 * @param  {Object|function} [reducers={}] reducer provided by the users
 * @return {Object}               object containing reducer function for every measure
 */
function getReducerObj (dataModel, reducers = {}) {
    const retObj = {};
    const pReducers = reducers;
    const fieldStore = dataModel.getPartialFieldspace();
    const measures = fieldStore.getMeasure();
    let reducer = reducerStore.defaultReducer();
    Iif (typeof reducers === 'function') {
        reducer = reducers;
    }
    Object.entries(measures).forEach(([key]) => {
        if (typeof reducers[key] === 'string') {
            pReducers[key] = reducerStore.resolve(pReducers[key]) ? reducerStore.resolve(pReducers[key]) : reducer;
        }
        if (typeof reducers[key] !== 'function') {
            pReducers[key] = undefined;
        }
        retObj[key] = pReducers[key] || reducerStore.resolve(measures[key].defAggFn()) || reducer;
    });
    return retObj;
}
 
/**
 * main function which perform the group-by operations which reduce the measures value is the
 * fields are common according to the reducer function provided
 * @param  {DataModel} dataModel the dataModel to worked
 * @param  {Array} fieldArr  fields according to which the groupby should be worked
 * @param  {Object|Function} reducers  reducers function
 * @param {DataModel} existingDataModel Existing datamodel instance
 * @return {DataModel} new dataModel with the group by
 */
function groupBy (dataModel, fieldArr, reducers, existingDataModel) {
    const sFieldArr = getFieldArr(dataModel, fieldArr);
    const reducerObj = getReducerObj(dataModel, reducers);
    const fieldStore = dataModel.getPartialFieldspace();
    const fieldStoreObj = fieldStore.fieldsObj();
    const dbName = fieldStore.name;
    const dimensionArr = [];
    const measureArr = [];
    const schema = [];
    const hashMap = {};
    const data = [];
    let newDataModel;
    // Prepare the schema
    Object.entries(fieldStoreObj).forEach(([key, value]) => {
        if (sFieldArr.indexOf(key) !== -1 || reducerObj[key]) {
            schema.push(extend2({}, value.schema));
            if (value.schema.type === 'measure' && value.schema.subtype !== 'discrete') {
                measureArr.push(key);
            } else Eif (value.schema.type === 'dimension' || value.schema.subtype === 'discrete') {
                dimensionArr.push(key);
            }
        }
    });
    // Prepare the data
    let rowCount = 0;
    rowDiffsetIterator(dataModel._rowDiffset, (i) => {
        let hash = '';
        dimensionArr.forEach((_) => {
            hash = `${hash}-${fieldStoreObj[_].data[i]}`;
        });
        if (hashMap[hash] === undefined) {
            hashMap[hash] = rowCount;
            data.push({});
            dimensionArr.forEach((_) => {
                data[rowCount][_] = fieldStoreObj[_].data[i];
            });
            measureArr.forEach((_) => {
                data[rowCount][_] = [fieldStoreObj[_].data[i]];
            });
            rowCount += 1;
        } else {
            measureArr.forEach((_) => {
                data[hashMap[hash]][_].push(fieldStoreObj[_].data[i]);
            });
        }
    });
    // reduction
    data.forEach((row) => {
        const tuple = row;
        measureArr.forEach((_) => {
            tuple[_] = reducerObj[_](row[_]);
        });
    });
    Iif (existingDataModel) {
        existingDataModel.__calculateFieldspace();
        newDataModel = existingDataModel;
    }
    else {
        newDataModel = new DataModel(data, schema, { name: dbName });
    }
    return newDataModel;
}
 
export { groupBy, getFieldArr, getReducerObj };