import powerbi from "powerbi-visuals-api" ; import ILocalizationManager = powerbi . extensibility . ILocalizationManager ; import { DurationUnits } from "./gantt" ; const GanttDurationUnitType = [ "second" , "minute" , "hour" , "day" , ] ; const HoursInADay = 24 ; const MinutesInAHour = 60 ; const SecondsInAMinute = 60 ; const MinutesInADay = 24 * MinutesInAHour ; const SecondsInADay = 60 * MinutesInADay ; const SecondsInAHour = MinutesInAHour * SecondsInAMinute ; export class DurationHelper { public static getNewUnitByFloorDuration ( durationUnitTypeIndex , duration ) { if ( ! durationUnitTypeIndex ) return GanttDurationUnitType [ 0 ] ; switch ( durationUnitTypeIndex ) { case GanttDurationUnitType . indexOf ( "day" ) : duration = duration * HoursInADay ; break ; case GanttDurationUnitType . indexOf ( "hour" ) : duration = duration * MinutesInAHour ; break ; case GanttDurationUnitType . indexOf ( "minute" ) : duration = duration * SecondsInAMinute ; break ; } if ( ( duration - Math . floor ( duration ) !== 0 ) && durationUnitTypeIndex > 1 ) { return DurationHelper . getNewUnitByFloorDuration ( durationUnitTypeIndex - 1 , duration ) ; } else { return GanttDurationUnitType [ durationUnitTypeIndex - 1 ] ; } } public static downgradeDurationUnit ( durationUnit , duration ) { let durationUnitTypeIndex = GanttDurationUnitType . indexOf ( durationUnit ) ; durationUnit = DurationHelper . getNewUnitByFloorDuration ( durationUnitTypeIndex , duration ) ; return durationUnit ; } public static transformExtraDuration ( durationUnit , duration ) { switch ( durationUnit ) { case DurationUnits . Hour : return HoursInADay * duration ; case DurationUnits . Minute : return MinutesInADay * duration ; case DurationUnits . Second : return SecondsInADay * duration ; default : return duration ; } } public static transformDuration ( duration , newDurationUnit , stepDurationTransformation ) { if ( stepDurationTransformation === null || typeof stepDurationTransformation === "undefined" ) { return Math . floor ( duration ) ; } let transformedDuration = duration ; switch ( newDurationUnit ) { case DurationUnits . Hour : transformedDuration = duration * HoursInADay ; break ; case DurationUnits . Minute : transformedDuration = duration * ( stepDurationTransformation === 2 ? MinutesInADay : MinutesInAHour ) ; break ; case DurationUnits . Second : transformedDuration = duration * ( stepDurationTransformation === 3 ? SecondsInADay : stepDurationTransformation === 2 ? SecondsInAHour : SecondsInAMinute ) ; break ; } return Math . floor ( transformedDuration ) ; } param duration param durationUnit public static generateLabelForDuration ( duration , durationUnit , localizationManager ) { let oneDayDuration = HoursInADay ; let oneHourDuration = MinutesInAHour ; let oneMinuteDuration = 1 ; switch ( durationUnit ) { case DurationUnits . Hour : oneHourDuration = 1 ; break ; case DurationUnits . Minute : oneDayDuration = MinutesInADay ; break ; case DurationUnits . Second : oneDayDuration = SecondsInADay ; oneHourDuration = SecondsInAHour ; oneMinuteDuration = SecondsInAMinute ; break ; } let label = "" ; const days = Math . floor ( duration / oneDayDuration ) ; label += days ? `${ days } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Days" ) } ` : `` ; if ( durationUnit === DurationUnits . Day ) { return `${ duration } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Days" ) } ` ; } let timeDelta = days * oneDayDuration ; const hours = Math . floor ( ( duration - timeDelta ) / oneHourDuration ) ; label += hours ? `${ hours } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Hours" ) } ` : `` ; if ( durationUnit === DurationUnits . Hour ) { return duration >= 24 ? label : `${ duration } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Hours" ) }` ; } timeDelta = ( days * oneDayDuration ) + ( hours * oneHourDuration ) ; const minutes = Math . floor ( ( duration - timeDelta ) / oneMinuteDuration ) ; label += minutes ? `${ minutes } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Minutes" ) } ` : `` ; if ( durationUnit === DurationUnits . Minute ) { return duration >= 60 ? label : `${ duration } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Minutes" ) } ` ; } timeDelta = ( days * oneDayDuration ) + ( hours * oneHourDuration ) + ( minutes * oneMinuteDuration ) ; const seconds = Math . floor ( duration - timeDelta ) ; label += seconds ? `${ seconds } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Seconds" ) } ` : `` ; if ( durationUnit === DurationUnits . Second ) { return duration >= 60 ? label : `${ duration } ${ localizationManager . getDisplayName ( "Visual_DurationUnit_Seconds" ) } ` ; } } }