-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REF] pivot: extract groupable fields logic
- Loading branch information
1 parent
66e50f9
commit 1dedc50
Showing
3 changed files
with
89 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { PivotCoreDimension, PivotField, PivotFields } from "../../types"; | ||
import { deepCopy } from "../misc"; | ||
import { isDateOrDatetimeField } from "./pivot_helpers"; | ||
|
||
export class GroupableFields { | ||
constructor( | ||
private fields: PivotFields, | ||
private dateGranularities: string[], | ||
private datetimeGranularities: string[], | ||
private isGroupable: (field: PivotField) => boolean | ||
) {} | ||
|
||
getUnusedGroupableFields(dimensions: PivotCoreDimension[]): PivotField[] { | ||
const groupableFields: PivotField[] = []; | ||
const fields = this.fields; | ||
for (const fieldName in fields) { | ||
const field = fields[fieldName]; | ||
if (!field) { | ||
continue; | ||
} | ||
if (this.isGroupable(field)) { | ||
groupableFields.push(field); | ||
} | ||
} | ||
const currentlyUsed = dimensions.map((field) => field.fieldName); | ||
const unusedGranularities = this.getUnusedGranularities(dimensions); | ||
return groupableFields | ||
.filter((field) => { | ||
if (isDateOrDatetimeField(field)) { | ||
return !currentlyUsed.includes(field.name) || unusedGranularities[field.name].size > 0; | ||
} | ||
return !currentlyUsed.includes(field.name); | ||
}) | ||
.sort((a, b) => a.string.localeCompare(b.string)); | ||
} | ||
|
||
getUnusedGranularities(dimensions: PivotCoreDimension[]): Record<string, Set<string>> { | ||
const dateFields = dimensions.filter((dimension) => { | ||
const fieldType = this.fields[dimension.fieldName]?.type; | ||
return fieldType === "date" || fieldType === "datetime"; | ||
}); | ||
const granularitiesPerFields = {}; | ||
for (const field of dateFields) { | ||
granularitiesPerFields[field.fieldName] = new Set( | ||
this.fields[field.fieldName]?.type === "date" | ||
? this.dateGranularities | ||
: this.datetimeGranularities | ||
); | ||
} | ||
for (const field of dateFields) { | ||
granularitiesPerFields[field.fieldName].delete(field.granularity); | ||
} | ||
return granularitiesPerFields; | ||
} | ||
|
||
addDefaultDateTimeGranularity(dimensions: PivotCoreDimension[]) { | ||
const dimensionsWithGranularity = deepCopy(dimensions); | ||
const unusedGranularities = this.getUnusedGranularities(dimensions); | ||
for (const dimension of dimensionsWithGranularity) { | ||
const fieldType = this.fields[dimension.fieldName]?.type; | ||
if ((fieldType === "date" || fieldType === "datetime") && !dimension.granularity) { | ||
const granularity = | ||
unusedGranularities[dimension.fieldName]?.values().next().value || "year"; | ||
unusedGranularities[dimension.fieldName]?.delete(granularity); | ||
dimension.granularity = granularity; | ||
} | ||
} | ||
return dimensionsWithGranularity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters