table_model.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import _ from 'lodash';
  2. import { Column, TableData } from '@grafana/ui';
  3. /**
  4. * Extends the standard Column class with variables that get
  5. * mutated in the angular table panel.
  6. */
  7. interface MutableColumn extends Column {
  8. title?: string;
  9. sort?: boolean;
  10. desc?: boolean;
  11. }
  12. export default class TableModel implements TableData {
  13. columns: MutableColumn[];
  14. rows: any[];
  15. type: string;
  16. columnMap: any;
  17. constructor(table?: any) {
  18. this.columns = [];
  19. this.columnMap = {};
  20. this.rows = [];
  21. this.type = 'table';
  22. if (table) {
  23. if (table.columns) {
  24. table.columns.forEach(col => this.addColumn(col));
  25. }
  26. if (table.rows) {
  27. table.rows.forEach(row => this.addRow(row));
  28. }
  29. }
  30. }
  31. sort(options) {
  32. if (options.col === null || this.columns.length <= options.col) {
  33. return;
  34. }
  35. this.rows.sort((a, b) => {
  36. a = a[options.col];
  37. b = b[options.col];
  38. // Sort null or undefined separately from comparable values
  39. return +(a == null) - +(b == null) || +(a > b) || -(a < b);
  40. });
  41. if (options.desc) {
  42. this.rows.reverse();
  43. }
  44. this.columns[options.col].sort = true;
  45. this.columns[options.col].desc = options.desc;
  46. }
  47. addColumn(col) {
  48. if (!this.columnMap[col.text]) {
  49. this.columns.push(col);
  50. this.columnMap[col.text] = col;
  51. }
  52. }
  53. addRow(row) {
  54. this.rows.push(row);
  55. }
  56. }
  57. // Returns true if both rows have matching non-empty fields as well as matching
  58. // indexes where one field is empty and the other is not
  59. function areRowsMatching(columns, row, otherRow) {
  60. let foundFieldToMatch = false;
  61. for (let columnIndex = 0; columnIndex < columns.length; columnIndex++) {
  62. if (row[columnIndex] !== undefined && otherRow[columnIndex] !== undefined) {
  63. if (row[columnIndex] !== otherRow[columnIndex]) {
  64. return false;
  65. }
  66. } else if (row[columnIndex] === undefined || otherRow[columnIndex] === undefined) {
  67. foundFieldToMatch = true;
  68. }
  69. }
  70. return foundFieldToMatch;
  71. }
  72. export function mergeTablesIntoModel(dst?: TableModel, ...tables: TableModel[]): TableModel {
  73. const model = dst || new TableModel();
  74. if (arguments.length === 1) {
  75. return model;
  76. }
  77. // Single query returns data columns and rows as is
  78. if (arguments.length === 2) {
  79. model.columns = tables[0].hasOwnProperty('columns') ? [...tables[0].columns] : [];
  80. model.rows = tables[0].hasOwnProperty('rows') ? [...tables[0].rows] : [];
  81. return model;
  82. }
  83. // Track column indexes of union: name -> index
  84. const columnNames = {};
  85. // Union of all non-value columns
  86. const columnsUnion = tables.slice().reduce((acc, series) => {
  87. series.columns.forEach(col => {
  88. const { text } = col;
  89. if (columnNames[text] === undefined) {
  90. columnNames[text] = acc.length;
  91. acc.push(col);
  92. }
  93. });
  94. return acc;
  95. }, []);
  96. // Map old column index to union index per series, e.g.,
  97. // given columnNames {A: 0, B: 1} and
  98. // data [{columns: [{ text: 'A' }]}, {columns: [{ text: 'B' }]}] => [[0], [1]]
  99. const columnIndexMapper = tables.map(series => series.columns.map(col => columnNames[col.text]));
  100. // Flatten rows of all series and adjust new column indexes
  101. const flattenedRows = tables.reduce((acc, series, seriesIndex) => {
  102. const mapper = columnIndexMapper[seriesIndex];
  103. series.rows.forEach(row => {
  104. const alteredRow = [];
  105. // Shifting entries according to index mapper
  106. mapper.forEach((to, from) => {
  107. alteredRow[to] = row[from];
  108. });
  109. acc.push(alteredRow);
  110. });
  111. return acc;
  112. }, []);
  113. // Merge rows that have same values for columns
  114. const mergedRows = {};
  115. const compactedRows = flattenedRows.reduce((acc, row, rowIndex) => {
  116. if (!mergedRows[rowIndex]) {
  117. // Look from current row onwards
  118. let offset = rowIndex + 1;
  119. // More than one row can be merged into current row
  120. while (offset < flattenedRows.length) {
  121. // Find next row that could be merged
  122. const match = _.findIndex(flattenedRows, otherRow => areRowsMatching(columnsUnion, row, otherRow), offset);
  123. if (match > -1) {
  124. const matchedRow = flattenedRows[match];
  125. // Merge values from match into current row if there is a gap in the current row
  126. for (let columnIndex = 0; columnIndex < columnsUnion.length; columnIndex++) {
  127. if (row[columnIndex] === undefined && matchedRow[columnIndex] !== undefined) {
  128. row[columnIndex] = matchedRow[columnIndex];
  129. }
  130. }
  131. // Don't visit this row again
  132. mergedRows[match] = matchedRow;
  133. // Keep looking for more rows to merge
  134. offset = match + 1;
  135. } else {
  136. // No match found, stop looking
  137. break;
  138. }
  139. }
  140. acc.push(row);
  141. }
  142. return acc;
  143. }, []);
  144. model.columns = columnsUnion;
  145. model.rows = compactedRows;
  146. return model;
  147. }