Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dashboard position row data #5131

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
scan position data and fix rows
  • Loading branch information
Grace Guo committed Jun 6, 2018
commit bd9344e4d93f693c1ef5738de59b01b05bc4c980
70 changes: 68 additions & 2 deletions superset/assets/src/dashboard/util/dashboardLayoutConverter.js
Original file line number Diff line number Diff line change
@@ -410,20 +410,86 @@ export function convertToLayout(positions) {
return root;
}

function mergePosition(position, bottomLine, maxColumn) {
const { col, size_x, size_y } = position;
const endColumn = col + size_x > maxColumn ? bottomLine.length : col + size_x;
const nextSectionStart =
bottomLine.slice(col).findIndex(value => value > bottomLine[col]) + 1;

const currentBottom =
nextSectionStart > 0 && nextSectionStart < size_x
? Math.max.apply(null, bottomLine.slice(col, col + size_x + 1))
: bottomLine[col];
bottomLine.fill(currentBottom + size_y, col, endColumn);
}

function scanDashboardPositionsData(positions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a comment about what this is doing? hard to wrap your head around when just reading.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

const bottomLine = new Array(49).fill(0);
bottomLine[0] = Number.MAX_VALUE;
const maxColumn = Math.max.apply(
null,
positions.slice().map(position => position.col),
);

const positionsByRowId = {};
positions
.slice()
.sort(sortByRowId)
.forEach(position => {
const { row } = position;
if (positionsByRowId[row] === undefined) {
positionsByRowId[row] = [];
}
positionsByRowId[row].push(position);
});
const rawPositions = Object.values(positionsByRowId);
const updatedPositions = [];

while (rawPositions.length) {
const nextRow = rawPositions.shift();
let nextCol = 1;
while (nextRow.length) {
// special treatment for duplicated positions: display wider one first
const availableIndexByColumn = nextRow
.filter(position => position.col === nextCol)
.map((position, index) => index);
if (availableIndexByColumn.length) {
const idx =
availableIndexByColumn.length > 1
? availableIndexByColumn.sort(
(idx1, idx2) => nextRow[idx2].size_x - nextRow[idx1].size_x,
)[0]
: availableIndexByColumn[0];

const nextPosition = nextRow.splice(idx, 1)[0];
mergePosition(nextPosition, bottomLine, maxColumn + 1);
nextPosition.row = bottomLine[nextPosition.col] - nextPosition.size_y;
updatedPositions.push(nextPosition);
nextCol += nextPosition.size_x;
} else {
nextCol = nextRow[0].col;
}
}
}

return updatedPositions;
}

export default function(dashboard) {
const positions = [];

// position data clean up. some dashboard didn't have position_json
let { position_json } = dashboard;
const positionDict = {};
if (Array.isArray(position_json)) {
// scan and fix positions data: extra spaces, dup rows, .etc
position_json = scanDashboardPositionsData(position_json);
position_json.forEach(position => {
positionDict[position.slice_id] = position;
});
} else {
position_json = [];
}

// position data clean up. some dashboard didn't have position_json
const lastRowId = Math.max(
0,
Math.max.apply(null, position_json.map(pos => pos.row + pos.size_y)),