Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
1e82a8f9
Commit
1e82a8f9
authored
6 years ago
by
Tom Robinson
Browse files
Options
Downloads
Patches
Plain Diff
Cleanup pivot sort logic
parent
7df7e66f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/metabase/lib/data_grid.js
+65
-54
65 additions, 54 deletions
frontend/src/metabase/lib/data_grid.js
with
65 additions
and
54 deletions
frontend/src/metabase/lib/data_grid.js
+
65
−
54
View file @
1e82a8f9
...
...
@@ -3,21 +3,21 @@ import { formatValue } from "metabase/lib/formatting";
import
_
from
"
underscore
"
;
export
function
pivot
(
data
,
normalCol
,
pivotCol
,
cellCol
)
{
const
{
pivot
Col
Values
,
normal
Col
Values
}
=
distinctValuesSorted
(
const
{
pivotValues
,
normalValues
}
=
distinctValuesSorted
(
data
.
rows
,
pivotCol
,
normalCol
,
);
// make sure that the first element in the pivoted column list is null which makes room for the label of the other column
pivot
Col
Values
.
unshift
(
data
.
cols
[
normalCol
].
display_name
);
pivotValues
.
unshift
(
data
.
cols
[
normalCol
].
display_name
);
// start with an empty grid that we'll fill with the appropriate values
const
pivotedRows
=
normal
Col
Values
.
map
((
normal
Col
Values
,
index
)
=>
{
const
row
=
pivot
Col
Values
.
map
(()
=>
null
);
const
pivotedRows
=
normalValues
.
map
((
normalValues
,
index
)
=>
{
const
row
=
pivotValues
.
map
(()
=>
null
);
// for onVisualizationClick:
row
.
_dimension
=
{
value
:
normal
Col
Values
,
value
:
normalValues
,
column
:
data
.
cols
[
normalCol
],
};
return
row
;
...
...
@@ -25,8 +25,8 @@ export function pivot(data, normalCol, pivotCol, cellCol) {
// fill it up with the data
for
(
let
j
=
0
;
j
<
data
.
rows
.
length
;
j
++
)
{
let
normalColIdx
=
normal
Col
Values
.
lastIndexOf
(
data
.
rows
[
j
][
normalCol
]);
let
pivotColIdx
=
pivot
Col
Values
.
lastIndexOf
(
data
.
rows
[
j
][
pivotCol
]);
let
normalColIdx
=
normalValues
.
lastIndexOf
(
data
.
rows
[
j
][
normalCol
]);
let
pivotColIdx
=
pivotValues
.
lastIndexOf
(
data
.
rows
[
j
][
pivotCol
]);
pivotedRows
[
normalColIdx
][
0
]
=
data
.
rows
[
j
][
normalCol
];
// NOTE: we are hard coding the expectation that the metric is in the 3rd column
...
...
@@ -34,7 +34,7 @@ export function pivot(data, normalCol, pivotCol, cellCol) {
}
// provide some column metadata to maintain consistency
const
cols
=
pivot
Col
Values
.
map
(
function
(
value
,
idx
)
{
const
cols
=
pivotValues
.
map
(
function
(
value
,
idx
)
{
if
(
idx
===
0
)
{
// first column is always the coldef of the normal column
return
data
.
cols
[
normalCol
];
...
...
@@ -55,77 +55,88 @@ export function pivot(data, normalCol, pivotCol, cellCol) {
return
{
cols
:
cols
,
columns
:
pivot
Col
Values
,
columns
:
pivotValues
,
rows
:
pivotedRows
,
};
}
export
function
distinctValuesSorted
(
rows
,
pivotColIdx
,
normalColIdx
)
{
const
normalSet
=
new
Set
();
const
pivotSet
=
new
Set
();
const
normalSortState
=
new
SortState
();
const
pivotSortState
=
new
SortState
();
for
(
const
row
of
rows
)
{
const
pivotValue
=
row
[
pivotColIdx
];
const
normalValue
=
row
[
normalColIdx
];
normalSet
.
add
(
normalValue
);
pivotSet
.
add
(
pivotValue
);
normalSortState
.
update
(
normalValue
,
pivotValue
);
pivotSortState
.
update
(
pivotValue
,
normalValue
);
}
const
normalValues
=
Array
.
from
(
normalSet
);
const
pivotValues
=
Array
.
from
(
pivotSet
);
normalSortState
.
sort
(
normalValues
);
pivotSortState
.
sort
(
pivotValues
);
return
{
normalValues
,
pivotValues
};
}
// This should work for both strings and numbers
const
DEFAULT_COMPARE
=
(
a
,
b
)
=>
(
a
<
b
?
-
1
:
a
>
b
?
1
:
0
);
class
SortState
{
constructor
(
compare
=
DEFAULT_COMPARE
)
{
this
.
compare
=
compare
;
this
.
asc
=
true
;
this
.
desc
=
true
;
this
.
lastValue
=
undefined
;
this
.
isGrouped
=
false
;
this
.
groupAsc
=
true
;
this
.
groupDesc
=
true
;
this
.
lastGroupKey
=
undefined
;
this
.
compare
=
compare
;
this
.
isGrouped
=
false
;
}
update
(
value
,
groupKey
)
{
// skip the first value since there's nothing to compare it to
if
(
this
.
lastValue
!==
undefined
)
{
this
.
asc
=
this
.
asc
&&
value
>=
this
.
lastValue
;
this
.
desc
=
this
.
desc
&&
value
<=
this
.
lastValue
;
if
(
this
.
lastGroupKey
!==
undefined
&&
this
.
lastGroupKey
===
groupKey
)
{
this
.
groupAsc
=
this
.
groupAsc
&&
value
>=
this
.
lastValue
;
this
.
groupDesc
=
this
.
groupDesc
&&
value
<=
this
.
lastValue
;
// compare the current value with the previous value
const
result
=
this
.
compare
(
value
,
this
.
lastValue
);
// update global sort state
this
.
asc
=
this
.
asc
&&
result
>=
0
;
this
.
desc
=
this
.
desc
&&
result
<=
0
;
if
(
// if current and last values are different
result
!==
0
&&
// and current and last group are same
this
.
lastGroupKey
!==
undefined
&&
this
.
lastGroupKey
===
groupKey
)
{
// update grouped sort state
this
.
groupAsc
=
this
.
groupAsc
&&
result
>=
0
;
this
.
groupDesc
=
this
.
groupDesc
&&
result
<=
0
;
this
.
isGrouped
=
true
;
}
}
// update last value and group key
this
.
lastValue
=
value
;
this
.
lastGroupKey
=
groupKey
;
}
sort
(
array
)
{
if
(
!
this
.
isGrouped
)
{
console
.
log
(
"
Not grouped
"
);
}
else
if
(
this
.
groupAsc
&&
this
.
groupDesc
)
{
console
.
warn
(
"
This shouldn't happen
"
);
}
else
if
(
this
.
groupAsc
&&
!
this
.
asc
)
{
console
.
log
(
"
Sorting ascending
"
);
array
.
sort
(
this
.
compare
);
}
else
if
(
this
.
groupDesc
&&
!
this
.
desc
)
{
console
.
log
(
"
Sorting descending
"
);
array
.
sort
((
a
,
b
)
=>
this
.
compare
(
b
,
a
));
if
(
this
.
isGrouped
)
{
if
(
this
.
groupAsc
&&
this
.
groupDesc
)
{
console
.
warn
(
"
This shouldn't happen
"
);
}
else
if
(
this
.
groupAsc
&&
!
this
.
asc
)
{
array
.
sort
(
this
.
compare
);
}
else
if
(
this
.
groupDesc
&&
!
this
.
desc
)
{
array
.
sort
((
a
,
b
)
=>
this
.
compare
(
b
,
a
));
}
}
}
}
export
function
distinctValuesSorted
(
rows
,
pivotColIdx
,
normalColIdx
)
{
const
normalSet
=
new
Set
();
const
pivotSet
=
new
Set
();
const
normalSortState
=
new
SortState
();
const
pivotSortState
=
new
SortState
();
for
(
const
row
of
rows
)
{
const
pivotValue
=
row
[
pivotColIdx
];
const
normalValue
=
row
[
normalColIdx
];
normalSet
.
add
(
normalValue
);
pivotSet
.
add
(
pivotValue
);
normalSortState
.
update
(
normalValue
,
pivotValue
);
pivotSortState
.
update
(
pivotValue
,
normalValue
);
}
const
normalColValues
=
Array
.
from
(
normalSet
);
const
pivotColValues
=
Array
.
from
(
pivotSet
);
normalSortState
.
sort
(
normalColValues
);
pivotSortState
.
sort
(
pivotColValues
);
return
{
normalColValues
,
pivotColValues
};
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment