Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
edb2dcf1
Commit
edb2dcf1
authored
Dec 12, 2017
by
David Kaltschmidt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extracted row matching function and added comments
parent
8d70f133
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
41 deletions
+50
-41
public/app/plugins/panel/table/transformers.ts
+50
-41
No files found.
public/app/plugins/panel/table/transformers.ts
View file @
edb2dcf1
...
@@ -145,8 +145,8 @@ transformers['table'] = {
...
@@ -145,8 +145,8 @@ transformers['table'] = {
const
columnNames
=
{};
const
columnNames
=
{};
// Union of all columns
// Union of all columns
const
columns
=
data
.
reduce
((
acc
,
d
,
i
)
=>
{
const
columns
=
data
.
reduce
((
acc
,
series
)
=>
{
d
.
columns
.
forEach
((
col
,
j
)
=>
{
series
.
columns
.
forEach
(
col
=>
{
const
{
text
}
=
col
;
const
{
text
}
=
col
;
if
(
columnNames
[
text
]
===
undefined
)
{
if
(
columnNames
[
text
]
===
undefined
)
{
columnNames
[
text
]
=
acc
.
length
;
columnNames
[
text
]
=
acc
.
length
;
...
@@ -175,76 +175,84 @@ transformers['table'] = {
...
@@ -175,76 +175,84 @@ transformers['table'] = {
return
;
return
;
}
}
// Track column indexes: name -> index
// Track column indexes
of union
: name -> index
const
columnNames
=
{};
const
columnNames
=
{};
const
columnIndexes
=
[];
// Union of all non-value columns
// Union of all non-value columns
const
columns
=
data
.
reduce
((
acc
,
d
,
i
)
=>
{
const
columnsUnion
=
data
.
reduce
((
acc
,
series
)
=>
{
const
indexes
=
[];
series
.
columns
.
forEach
(
col
=>
{
d
.
columns
.
forEach
((
col
,
j
)
=>
{
const
{
text
}
=
col
;
const
{
text
}
=
col
;
if
(
columnNames
[
text
]
===
undefined
)
{
if
(
columnNames
[
text
]
===
undefined
)
{
columnNames
[
text
]
=
acc
.
length
;
columnNames
[
text
]
=
acc
.
length
;
acc
.
push
(
col
);
acc
.
push
(
col
);
}
}
indexes
[
j
]
=
columnNames
[
text
];
});
});
columnIndexes
.
push
(
indexes
);
return
acc
;
return
acc
;
},
[]);
},
[]);
model
.
columns
=
columns
;
// Map old column index to union index per series, e.g.,
// given columnNames {A: 0, B: 1} and
// Adjust rows to new column indexes
// data [{columns: [{ text: 'A' }]}, {columns: [{ text: 'B' }]}] => [[0], [1]]
let
rows
=
data
.
reduce
((
acc
,
d
,
i
)
=>
{
const
columnIndexMapper
=
data
.
map
(
series
=>
const
indexes
=
columnIndexes
[
i
];
series
.
columns
.
map
(
col
=>
columnNames
[
col
.
text
])
d
.
rows
.
forEach
((
r
,
j
)
=>
{
);
// Flatten rows of all series and adjust new column indexes
const
flattenedRows
=
data
.
reduce
((
acc
,
series
,
seriesIndex
)
=>
{
const
mapper
=
columnIndexMapper
[
seriesIndex
];
series
.
rows
.
forEach
(
row
=>
{
const
alteredRow
=
[];
const
alteredRow
=
[];
indexes
.
forEach
((
to
,
from
)
=>
{
// Shifting entries according to index mapper
alteredRow
[
to
]
=
r
[
from
];
mapper
.
forEach
((
to
,
from
)
=>
{
alteredRow
[
to
]
=
row
[
from
];
});
});
acc
.
push
(
alteredRow
);
acc
.
push
(
alteredRow
);
});
});
return
acc
;
return
acc
;
},
[]);
},
[]);
// Returns true if both rows have matching non-empty fields as well as matching
// indexes where one field is empty and the other is not
function
areRowsMatching
(
columns
,
row
,
otherRow
)
{
let
foundFieldToMatch
=
false
;
for
(
let
columnIndex
=
0
;
columnIndex
<
columns
.
length
;
columnIndex
++
)
{
if
(
row
[
columnIndex
]
!==
undefined
&&
otherRow
[
columnIndex
]
!==
undefined
)
{
if
(
row
[
columnIndex
]
!==
otherRow
[
columnIndex
])
{
return
false
;
}
}
else
if
(
row
[
columnIndex
]
===
undefined
||
otherRow
[
columnIndex
]
===
undefined
)
{
foundFieldToMatch
=
true
;
}
}
return
foundFieldToMatch
;
}
// Merge rows that have same values for columns
// Merge rows that have same values for columns
const
mergedRows
=
{};
const
mergedRows
=
{};
rows
=
r
ows
.
reduce
((
acc
,
row
,
rowIndex
)
=>
{
const
compactedRows
=
flattenedR
ows
.
reduce
((
acc
,
row
,
rowIndex
)
=>
{
if
(
!
mergedRows
[
rowIndex
])
{
if
(
!
mergedRows
[
rowIndex
])
{
// Look from current row onwards
let
offset
=
rowIndex
+
1
;
let
offset
=
rowIndex
+
1
;
while
(
offset
<
rows
.
length
)
{
// More than one row can be merged into current row
// Find next row that has the same field values unless the respective field is undefined
while
(
offset
<
flattenedRows
.
length
)
{
const
match
=
_
.
findIndex
(
rows
,
(
otherRow
)
=>
{
// Find next row that could be merged
let
fieldsAreTheSame
=
true
;
const
match
=
_
.
findIndex
(
flattenedRows
,
let
foundFieldToMatch
=
false
;
otherRow
=>
areRowsMatching
(
columnsUnion
,
row
,
otherRow
),
for
(
let
columnIndex
=
0
;
columnIndex
<
columns
.
length
;
columnIndex
++
)
{
offset
);
if
(
row
[
columnIndex
]
!==
undefined
&&
otherRow
[
columnIndex
]
!==
undefined
)
{
if
(
row
[
columnIndex
]
!==
otherRow
[
columnIndex
])
{
fieldsAreTheSame
=
false
;
}
}
else
if
(
row
[
columnIndex
]
===
undefined
||
otherRow
[
columnIndex
]
===
undefined
)
{
foundFieldToMatch
=
true
;
}
if
(
!
fieldsAreTheSame
)
{
break
;
}
}
return
fieldsAreTheSame
&&
foundFieldToMatch
;
},
offset
);
if
(
match
>
-
1
)
{
if
(
match
>
-
1
)
{
const
matchedRow
=
r
ows
[
match
];
const
matchedRow
=
flattenedR
ows
[
match
];
// Merge values
into
current row
// Merge values
from match into current row if there is a gap in the
current row
for
(
let
columnIndex
=
0
;
columnIndex
<
columns
.
length
;
columnIndex
++
)
{
for
(
let
columnIndex
=
0
;
columnIndex
<
columns
Union
.
length
;
columnIndex
++
)
{
if
(
row
[
columnIndex
]
===
undefined
&&
matchedRow
[
columnIndex
]
!==
undefined
)
{
if
(
row
[
columnIndex
]
===
undefined
&&
matchedRow
[
columnIndex
]
!==
undefined
)
{
row
[
columnIndex
]
=
matchedRow
[
columnIndex
];
row
[
columnIndex
]
=
matchedRow
[
columnIndex
];
}
}
}
}
// Dont visit this row again
mergedRows
[
match
]
=
matchedRow
;
mergedRows
[
match
]
=
matchedRow
;
// Keep looking for more rows to merge
// Keep looking for more rows to merge
offset
=
match
+
1
;
offset
=
match
+
1
;
}
else
{
}
else
{
// No match found, stop looking
break
;
break
;
}
}
}
}
...
@@ -253,7 +261,8 @@ transformers['table'] = {
...
@@ -253,7 +261,8 @@ transformers['table'] = {
return
acc
;
return
acc
;
},
[]);
},
[]);
model
.
rows
=
rows
;
model
.
columns
=
columnsUnion
;
model
.
rows
=
compactedRows
;
}
}
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment