Convert utils/groupByTimeSeriesTransform
parent
1876c91bdc
commit
30b7e92c47
|
@ -12,6 +12,24 @@ import {
|
||||||
DecimalPlaces,
|
DecimalPlaces,
|
||||||
} from 'src/types/dashboard'
|
} from 'src/types/dashboard'
|
||||||
|
|
||||||
|
interface ColumnWidths {
|
||||||
|
totalWidths: number
|
||||||
|
widths: {[x: string]: number}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SortedLabel {
|
||||||
|
label: string
|
||||||
|
responseIndex: number
|
||||||
|
seriesIndex: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TransformTableDataReturnType {
|
||||||
|
transformedData: DbData[][]
|
||||||
|
sortedTimeVals: DbData[]
|
||||||
|
columnWidths: {[x: string]: number}
|
||||||
|
totalWidths: number
|
||||||
|
}
|
||||||
|
|
||||||
const calculateTimeColumnWidth = (timeFormat: string): number => {
|
const calculateTimeColumnWidth = (timeFormat: string): number => {
|
||||||
// Force usage of longest format names for ideal measurement
|
// Force usage of longest format names for ideal measurement
|
||||||
timeFormat = _.replace(timeFormat, 'MMMM', 'September')
|
timeFormat = _.replace(timeFormat, 'MMMM', 'September')
|
||||||
|
@ -29,10 +47,6 @@ const calculateTimeColumnWidth = (timeFormat: string): number => {
|
||||||
return width + CELL_HORIZONTAL_PADDING
|
return width + CELL_HORIZONTAL_PADDING
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ColumnWidths {
|
|
||||||
totalWidths: number
|
|
||||||
widths: {[x: string]: number}
|
|
||||||
}
|
|
||||||
const updateMaxWidths = (
|
const updateMaxWidths = (
|
||||||
row: DbData[],
|
row: DbData[],
|
||||||
maxColumnWidths: ColumnWidths,
|
maxColumnWidths: ColumnWidths,
|
||||||
|
@ -79,7 +93,6 @@ const updateMaxWidths = (
|
||||||
fontWeight: 'bold',
|
fontWeight: 'bold',
|
||||||
}).width + CELL_HORIZONTAL_PADDING
|
}).width + CELL_HORIZONTAL_PADDING
|
||||||
|
|
||||||
const {widths: maxWidths} = maxColumnWidths
|
|
||||||
const maxWidth = _.get(maxWidths, `${columnLabel}`, 0)
|
const maxWidth = _.get(maxWidths, `${columnLabel}`, 0)
|
||||||
|
|
||||||
if (isTopRow || currentWidth > maxWidth) {
|
if (isTopRow || currentWidth > maxWidth) {
|
||||||
|
@ -93,11 +106,6 @@ const updateMaxWidths = (
|
||||||
return maxWidths
|
return maxWidths
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SortedLabel {
|
|
||||||
label: string
|
|
||||||
responseIndex: number
|
|
||||||
seriesIndex: number
|
|
||||||
}
|
|
||||||
export const computeFieldOptions = (
|
export const computeFieldOptions = (
|
||||||
existingFieldOptions: FieldOption[],
|
existingFieldOptions: FieldOption[],
|
||||||
sortedLabels: SortedLabel[]
|
sortedLabels: SortedLabel[]
|
||||||
|
@ -189,12 +197,20 @@ export const orderTableColumns = (
|
||||||
return orderedData[0].length ? orderedData : [[]]
|
return orderedData[0].length ? orderedData : [[]]
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TransformTableDataReturnType {
|
export const sortTableData = (
|
||||||
transformedData: DbData[][]
|
data: DbData[][],
|
||||||
sortedTimeVals: number[]
|
sort: Sort
|
||||||
columnWidths: ColumnWidths
|
): {sortedData: DbData[][]; sortedTimeVals: DbData[]} => {
|
||||||
totalWidths: number
|
const sortIndex = _.indexOf(data[0], sort.field)
|
||||||
|
const dataValues = _.drop(data, 1)
|
||||||
|
const sortedData = [
|
||||||
|
data[0],
|
||||||
|
..._.orderBy<DbData[]>(dataValues, sortIndex, [sort.direction]),
|
||||||
|
]
|
||||||
|
const sortedTimeVals = map(sortedData, r => r[0])
|
||||||
|
return {sortedData, sortedTimeVals}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const transformTableData = (
|
export const transformTableData = (
|
||||||
data: DbData[][],
|
data: DbData[][],
|
||||||
sort: Sort,
|
sort: Sort,
|
||||||
|
@ -204,12 +220,8 @@ export const transformTableData = (
|
||||||
decimalPlaces: DecimalPlaces
|
decimalPlaces: DecimalPlaces
|
||||||
): TransformTableDataReturnType => {
|
): TransformTableDataReturnType => {
|
||||||
const {verticalTimeAxis} = tableOptions
|
const {verticalTimeAxis} = tableOptions
|
||||||
const sortIndex = _.indexOf(data[0], sort.field)
|
|
||||||
const sortedData = [
|
const {sortedData, sortedTimeVals} = sortTableData(data, sort)
|
||||||
data[0],
|
|
||||||
..._.orderBy(_.drop(data, 1), sortIndex, [sort.direction]),
|
|
||||||
]
|
|
||||||
const sortedTimeVals = map(sortedData, r => r[0])
|
|
||||||
const filteredData = filterTableColumns(sortedData, fieldOptions)
|
const filteredData = filterTableColumns(sortedData, fieldOptions)
|
||||||
const orderedData = orderTableColumns(filteredData, fieldOptions)
|
const orderedData = orderTableColumns(filteredData, fieldOptions)
|
||||||
const transformedData = verticalTimeAxis ? orderedData : _.unzip(orderedData)
|
const transformedData = verticalTimeAxis ? orderedData : _.unzip(orderedData)
|
||||||
|
@ -220,5 +232,6 @@ export const transformTableData = (
|
||||||
verticalTimeAxis,
|
verticalTimeAxis,
|
||||||
decimalPlaces
|
decimalPlaces
|
||||||
)
|
)
|
||||||
|
|
||||||
return {transformedData, sortedTimeVals, columnWidths, totalWidths}
|
return {transformedData, sortedTimeVals, columnWidths, totalWidths}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
import {shiftDate} from 'shared/query/helpers'
|
import {shiftDate} from 'src/shared/query/helpers'
|
||||||
import {map, reduce, forEach, concat, clone} from 'fast.js'
|
import {map, reduce, forEach, concat, clone} from 'fast.js'
|
||||||
|
import {TimeSeriesServerResponse} from 'src/types/series'
|
||||||
|
import {DbData} from '../types/dashboard'
|
||||||
|
|
||||||
|
// interface Results {}
|
||||||
|
// interface Serieses {}
|
||||||
|
// interface Cells {}
|
||||||
|
// interface sortedLabels {}
|
||||||
|
// interface seriesLabels {}
|
||||||
|
|
||||||
const flattenGroupBySeries = (results, responseIndex, tags) => {
|
const flattenGroupBySeries = (results, responseIndex, tags) => {
|
||||||
if (_.isEmpty(results)) {
|
if (_.isEmpty(results)) {
|
||||||
|
@ -42,7 +50,7 @@ const flattenGroupBySeries = (results, responseIndex, tags) => {
|
||||||
return flattenedSeries
|
return flattenedSeries
|
||||||
}
|
}
|
||||||
|
|
||||||
const constructResults = (raw, isTable) => {
|
const constructResults = (raw: TimeSeriesServerResponse, isTable: boolean) => {
|
||||||
return _.flatten(
|
return _.flatten(
|
||||||
map(raw, (response, index) => {
|
map(raw, (response, index) => {
|
||||||
const results = _.get(response, 'response.results', [])
|
const results = _.get(response, 'response.results', [])
|
||||||
|
@ -252,7 +260,10 @@ const constructTimeSeries = (serieses, cells, sortedLabels, seriesLabels) => {
|
||||||
return _.sortBy(timeSeries, 'time')
|
return _.sortBy(timeSeries, 'time')
|
||||||
}
|
}
|
||||||
|
|
||||||
export const groupByTimeSeriesTransform = (raw, isTable) => {
|
export const groupByTimeSeriesTransform = (
|
||||||
|
raw: TimeSeriesServerResponse,
|
||||||
|
isTable: boolean
|
||||||
|
): {sortedLabels: string[]; sortedTimeSeries: DbData[][]} => {
|
||||||
const results = constructResults(raw, isTable)
|
const results = constructResults(raw, isTable)
|
||||||
const serieses = constructSerieses(results)
|
const serieses = constructSerieses(results)
|
||||||
const {cells, sortedLabels, seriesLabels} = constructCells(serieses)
|
const {cells, sortedLabels, seriesLabels} = constructCells(serieses)
|
Loading…
Reference in New Issue