Skip to content

Commit

Permalink
fix template context on column total: timeRange, totalHits, colTotal
Browse files Browse the repository at this point in the history
  • Loading branch information
fbaligand committed Feb 20, 2022
1 parent 95961ac commit bf9ab5d
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions public/enhanced-table-vis-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function EnhancedTableVisController ($scope, Private, config) {
const createTemplateContext = function (table, column, row, totalHits, timeRange, computedColsPerSplitCol, splitColIndex) {

// inject column value references
const templateContext = { totalHits, timeRange };
const templateContext = { total: totalHits, totalHits, timeRange };
_.forEach(column.template.paramsCols, function (templateParamCol) {
templateContext[`col${templateParamCol}`] = row[templateParamCol].value;
});
Expand Down Expand Up @@ -419,7 +419,7 @@ function EnhancedTableVisController ($scope, Private, config) {
};

/** create a new data table column for specified computed column */
const createColumn = function (computedColumn, index, totalHits, splitColIndex, columns, showTotal, totalFunc, aggs, computedColsPerSplitCol) {
const createColumn = function (computedColumn, index, splitColIndex, columns, showTotal, totalFunc, aggs, computedColsPerSplitCol) {

const fieldFormats = npStart.plugins.data.fieldFormats;
const FieldFormat = fieldFormats.getType(computedColumn.format);
Expand All @@ -438,6 +438,7 @@ function EnhancedTableVisController ($scope, Private, config) {
aggConfig: new AggConfig(aggs, { schema: aggSchema, type: aggType }),
title: computedColumn.label,
fieldFormatter: new FieldFormat(fieldFormatParams, getConfig),
applyTemplateOnTotal: computedColumn.applyTemplate && computedColumn.applyTemplateOnTotal,
dataAlignmentClass: `text-${computedColumn.alignment}`,
formula: createFormula(computedColumn.formula, 'computed column', splitColIndex, columns, totalFunc, computedColsPerSplitCol),
template: createTemplate(computedColumn, splitColIndex, columns, totalFunc),
Expand Down Expand Up @@ -516,38 +517,59 @@ function EnhancedTableVisController ($scope, Private, config) {
return newCell;
};

const addComputedColumnToTables = function (tables, newColumn, customColumnPosition, totalHits, timeRange, computedColsPerSplitCol, splitColIndex) {
_.forEach(tables, function (table) {
if (table.tables) {
addComputedColumnToTables(table.tables, newColumn, customColumnPosition, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
return;
}
const createTotalFormatter = function (table, column, row, totalHits, timeRange, computedColsPerSplitCol, splitColIndex) {
return function (contentType) {
return function (value) {
const self = { value, column };
if (column.applyTemplateOnTotal) {
self.templateContext = createTemplateContext(table, column, row, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
}
return renderCell.call(self, contentType);
};
};
};

const addComputedColumnToTables = function (table, newColumn, customColumnPosition, totalHits, timeRange, computedColsPerSplitCol, splitColIndex) {

// recursive call
if (table.tables) {
table.tables.forEach(function (subTable) {
addComputedColumnToTables(subTable, newColumn, customColumnPosition, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
});
return;
}

// add new computed column
newColumn = _.clone(newColumn);
if (customColumnPosition || customColumnPosition === 0) {
table.columns.splice(customColumnPosition, 0, newColumn);
}
else {
table.columns.push(newColumn);
}

// add new computed column and its cells
newColumn = _.clone(newColumn);
// add "total" formatter function
if (!computedColsPerSplitCol) {
newColumn.totalFormatter = createTotalFormatter(table, newColumn, undefined, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
}

// add new computed column cells
_.forEach(table.rows, function (row) {
const newCell = createComputedCell(table, newColumn, row, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
if (customColumnPosition || customColumnPosition === 0) {
table.columns.splice(customColumnPosition, 0, newColumn);
row.splice(customColumnPosition, 0, newCell);
}
else {
table.columns.push(newColumn);
row.push(newCell);
}
_.forEach(table.rows, function (row) {
const newCell = createComputedCell(table, newColumn, row, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
if (customColumnPosition || customColumnPosition === 0) {
row.splice(customColumnPosition, 0, newCell);
}
else {
row.push(newCell);
}
row[newColumn.id] = newCell.value;
});
row[newColumn.id] = newCell.value;
});

// compute total if totalFormula is present
if (newColumn.totalFormula && !computedColsPerSplitCol) {
newColumn.total = computeFormulaValue(newColumn.totalFormula, table, null, totalHits, timeRange);
}
// compute total if totalFormula is present
if (newColumn.totalFormula && !computedColsPerSplitCol) {
newColumn.total = computeFormulaValue(newColumn.totalFormula, table, null, totalHits, timeRange);
}

});
};

const processLinesComputedFilter = function (tables, linesComputedFilterFormula, totalHits, timeRange) {
Expand Down Expand Up @@ -768,6 +790,9 @@ function EnhancedTableVisController ($scope, Private, config) {
if (computedColsPerSplitCol && newCol.totalFormula !== undefined) {
newCol.total = computeFormulaValue(newCol.totalFormula, table, row, totalHits, timeRange, undefined, computedColsPerSplitCol, splitColIndex);
}
if (computedColsPerSplitCol && newCol.template !== undefined) {
newCol.totalFormatter = createTotalFormatter(_.clone(table), newCol, row, totalHits, timeRange, computedColsPerSplitCol, splitColIndex);
}
newCols.push(newCol);
let newColDefaultMetric;
if (newCol.formula === undefined) {
Expand Down Expand Up @@ -1036,8 +1061,8 @@ function EnhancedTableVisController ($scope, Private, config) {
// add computed columns
_.forEach(params.computedColumns, function (computedColumn, index) {
if (computedColumn.enabled) {
const newColumn = createColumn(computedColumn, index, totalHits, splitColIndex, firstTable.columns, params.showTotal, params.totalFunc, vis.aggs, splitColIndex !== -1 && params.computedColsPerSplitCol);
addComputedColumnToTables(tableGroups.tables, newColumn, computedColumn.customColumnPosition, totalHits, timeRange, splitColIndex !== -1 && params.computedColsPerSplitCol, splitColIndex);
const newColumn = createColumn(computedColumn, index, splitColIndex, firstTable.columns, params.showTotal, params.totalFunc, vis.aggs, splitColIndex !== -1 && params.computedColsPerSplitCol);
addComputedColumnToTables(tableGroups, newColumn, computedColumn.customColumnPosition, totalHits, timeRange, splitColIndex !== -1 && params.computedColsPerSplitCol, splitColIndex);
}
});

Expand Down

0 comments on commit bf9ab5d

Please sign in to comment.