All files / modules/75-ce/components/PerspectiveReportsAndBudget Cron.tsx

84.62% Statements 132/156
72.34% Branches 34/47
76.67% Functions 23/30
83.89% Lines 125/149

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404                                                3x 3x 3x 3x             3x 3x               3x 3x                             36x     3x 3x 21x     3x 3x 3x 93x 93x       9x 9x     6x 6x     6x 6x   72x 72x     93x           3x     3x 3x 3x 3x   3x 36x 36x   36x 36x     3x     3x 3x 3x 3x 3x   17x 3x 3x                 3x 3x 2x       1x 1x 1x   1x 5x 5x   4x 4x 4x           1x 6x 1x                 3x 3x                   3x 3x 3x   3x 3x           3x 3x 3x 3x 3x 3x                               3x               3x                                                       3x 3x 3x 3x 2x     1x 1x 1x 1x 1x 1x   1x                             3x 17x   3x   17x     17x 17x   17x   1x 1x 1x 1x 1x             16x 16x 16x 16x 16x                 17x                       3x 19x 17x   17x 3x     17x         17x       21x               17x       27x               17x       39x               17x       38x             17x             17x 17x     36x       35x         3x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
// *    *    *    *    *    *
// ┬    ┬    ┬    ┬    ┬    ┬
// │    │    │    │    │    |
// │    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
// │    │    │    │    └───── month (1 - 12)
// │    │    │    └────────── day of month (1 - 31, L)
// │    │    └─────────────── hour (0 - 23)
// │    └──────────────────── minute (0 - 59)
// └───────────────────────── second (0 - 59, optional)
 
// Rules:
// [Freq]  -> [components to render]
// Daily   -> hour, min
// Weekly  -> day of week, hour, min, day of month = '?'
// Monthly -> day of month, hour, min
// Yearly  -> month, day of month, hour, min
 
import React, { useReducer, useMemo, useEffect } from 'react'
import { Container, Select, Text } from '@wings-software/uicore'
import { useStrings } from 'framework/strings'
import css from './Cron.module.scss'
 
interface ISelectOption {
  label: string
  value: number
}
 
const getFrequencyOptions = () => {
  return [
    { label: 'Daily', value: 'day' },
    { label: 'Weekly', value: 'week' },
    { label: 'Monthly', value: 'month' },
    { label: 'Yearly', value: 'year' }
  ]
}
 
const getMonthOptions = (): ISelectOption[] => {
  const MONTHS = [
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
  ]
 
  return MONTHS.map((m, i) => ({ label: m, value: i + 1 }))
}
 
const getDaysOfWeekOptions = (): ISelectOption[] => {
  const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  return DAYS.map((d, i) => ({ label: d, value: i }))
}
 
const getDaysOfMonthOptions = () => {
  const DAYS_OF_MONTH_OPTIONS: ISelectOption[] = []
  for (let d = 1; d < 32; d++) {
    let suffix = ''
    switch (d) {
      case 1:
      case 21:
      case 31:
        suffix = 'st'
        break
      case 2:
      case 22:
        suffix = 'nd'
        break
      case 3:
      case 23:
        suffix = 'rd'
        break
      default:
        suffix = 'th'
        break
    }
 
    DAYS_OF_MONTH_OPTIONS.push({
      label: d + suffix,
      value: d
    })
  }
 
  return DAYS_OF_MONTH_OPTIONS
}
 
const getHourOptions = () => {
  const precision = [12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
  const am: ISelectOption[] = []
  const pm: ISelectOption[] = []
 
  precision.forEach((p, i) => {
    am.push({ label: `${p}:00 AM`, value: i }) // 12:00 AM
    am.push({ label: `${p}:30 AM`, value: i + 0.5 }) // 12:30 AM
 
    pm.push({ label: `${p}:00 PM`, value: i + 12 }) // 12:00 PM
    pm.push({ label: `${p}:30 PM`, value: i + 12 + 0.5 }) // 12:30 PM
  })
 
  return [...am, ...pm]
}
 
export const FREQUENCY_OPTIONS = getFrequencyOptions()
export const MONTH_OPTIONS = getMonthOptions()
export const DAYS_OF_WEEK_OPTIONS = getDaysOfWeekOptions()
export const DAYS_OF_MONTH_OPTIONS = getDaysOfMonthOptions()
export const HOUR_OPTIONS = getHourOptions()
 
const getMins = (h: number) => (h % Math.floor(h) > 0 ? 30 : 0)
const VALID_CRON_REGEX = /^((\d{1,2}|\*)\s){2}(\*|\?|\d{1,2})\s(\d{1,2}|\*)\s(\d{1,2}|\*|\?)$/
const CRON_TYPE: Record<string, RegExp> = {
  minute: /^(\*\s){4}\*$/, // "* * * * *"
  hour: /^\d{1,2}\s(\*\s){3}\*$/, // "? * * * *"
  day: /^(\d{1,2}\s){2}(\*|\?)\s(\*\s)(\*|\?)$/, // "a b * * ?"
  week: /^((\d{1,2}|\*)\s){2}(\*|\?)\s(\*\s)\d{1,2}$/, // "a b * * c"
  month: /^((\d{1,2}|\*)\s){2}\d{1,2}\s\*\s(\?|\*)$/, // "a b c * ?"
  year: /^(\d{1,2}\s){4}(\?|\*)$/ // "a b c d ?"
}
 
const getCronType = (cron: string): void | string => {
  if (typeof cron !== 'string' || !VALID_CRON_REGEX.test(cron)) {
    return
  }
 
  // Structure: [mins, hours, dayOfMonth, month, dayOfWeek]
  const minval = [0, 0, 1, 1, 0]
  const maxval = [59, 23, 31, 12, 6]
  const cronParts = cron.split(' ')
 
  for (let i = 0; i < cronParts.length; i++) {
    const unit = cronParts[i]
    if (unit === '*' || unit === '?') continue
 
    const value = parseInt(unit)
    Eif (value <= maxval[i] && value >= minval[i]) {
      continue
    }
 
    return
  }
 
  for (const type in CRON_TYPE) {
    if (CRON_TYPE[type].test(cron)) {
      return type
    }
  }
}
 
// check if dayOfWeek is in 'SUN', 'MON' format,
// If yes, convert it to 0, 1, etc. Our cron component doesn't
// support it. We support numbers only.
// This may be needed for legacy cron values/backward compat
const replaceDayOfWeek = (cron: string) => {
  const weekdayToNums: Record<string, string> = {
    SUN: '0',
    MON: '1',
    TUE: '2',
    WED: '3',
    THU: '4',
    FRI: '5',
    SAT: '6'
  }
 
  const weekdays = Object.keys(weekdayToNums)
  const parts = cron.split(' ')
  const dayOfWeek = parts.pop()
 
  Eif (!weekdays.includes(dayOfWeek!)) {
    return cron
  }
 
  return parts.concat(weekdayToNums[dayOfWeek!]).join(' ')
}
 
export enum ACTIONS {
  FREQUENCY,
  MONTH,
  DAY_OF_MONTH,
  DAY_OF_WEEK,
  HOURS
}
 
interface Action {
  type: ACTIONS
  data: unknown
}
 
interface IState {
  frequency: string // monthly, weekly, ..
  month: number // Jan(1), Feb(2), ..
  dayOfMonth: number // 1st(1), 2nd(2), ..
  dayOfWeek: number // Mon(0), Tue(1), ...
  hours: number // 12:00 AM (0), 5:30 PM (17.5), ..
}
 
const initialState: IState = {
  frequency: 'week',
  month: 1,
  dayOfWeek: 1,
  dayOfMonth: 1,
  hours: 0
}
 
const reducer = (state: IState, action: Action) => {
  const { type, data } = action
 
  switch (type) {
    case ACTIONS.FREQUENCY:
      return {
        ...state,
        ...initialState, // reset other values
        frequency: data as string
      }
 
    case ACTIONS.DAY_OF_WEEK:
      return { ...state, dayOfWeek: data as number }
 
    case ACTIONS.HOURS:
      return { ...state, hours: data as number }
 
    case ACTIONS.DAY_OF_MONTH:
      return { ...state, dayOfMonth: data as number }
 
    case ACTIONS.MONTH:
      return { ...state, month: data as number }
 
    default:
      return state
  }
}
 
const getInitialState = (cron: string): IState => {
  cron = replaceDayOfWeek(cron)
  const frequency = getCronType(cron)
  if (!cron || !frequency) {
    return initialState
  }
 
  const c = cron.split(' ')
  const mins = c[0],
    hrs = +c[1],
    dom = +c[2],
    month = +c[3],
    dow = +c[4]
 
  return {
    frequency,
    month: month as unknown as number,
    dayOfMonth: dom as unknown as number,
    dayOfWeek: dow as unknown as number,
    hours: mins === '30' ? hrs + 0.5 : hrs
  }
}
 
interface useCronReturnType {
  state: IState
  dispatch: React.Dispatch<Action>
  cron: string
}
 
export const useCron = (initialCron: string): useCronReturnType => {
  const [state, dispatch] = useReducer(
    reducer,
    useMemo(() => getInitialState(initialCron), [])
  )
  const { frequency, month, dayOfWeek, dayOfMonth, hours } = state
 
  let mins, hrs, day, mon, dow
  mins = hrs = day = mon = '*'
  dow = '?'
 
  switch (frequency) {
    case 'year':
      mins = getMins(hours)
      hrs = Math.floor(hours)
      day = dayOfMonth
      mon = month
      break
    case 'month':
      mins = getMins(hours)
      hrs = Math.floor(hours)
      day = dayOfMonth
      break
    case 'week':
      mins = getMins(hours)
      hrs = Math.floor(hours)
      dow = dayOfWeek
      day = '?'
      break
    case 'day':
      mins = getMins(hours)
      hrs = Math.floor(hours)
      break
    default:
      break
  }
 
  return {
    state,
    dispatch,
    cron: [mins, hrs, day, mon, dow].join(' ')
  }
}
 
interface CronProps {
  onChange: (cron: string) => void
  cron: string
}
 
const Cron = (props: CronProps) => {
  const { getString } = useStrings()
  const { state, dispatch, cron } = useCron(props.cron)
 
  useEffect(() => {
    props.onChange(cron)
  }, [cron])
 
  const handleChange = (actionType: ACTIONS, data: unknown) => {
    dispatch({ type: actionType, data })
  }
 
  const MonthComp = (
    <>
      <Text inline>{getString('common.in')} </Text>
      <Select
        className={css.selectBox}
        value={MONTH_OPTIONS.find(op => op.value === state.month)}
        onChange={({ value }) => handleChange(ACTIONS.MONTH, value)}
        items={MONTH_OPTIONS}
      />
    </>
  )
 
  const DaysOfMonthComp = (
    <>
      <Text inline>{getString('common.on')} </Text>
      <Select
        className={css.selectBox}
        value={DAYS_OF_MONTH_OPTIONS.find(op => op.value === state.dayOfMonth)}
        onChange={({ value }) => handleChange(ACTIONS.DAY_OF_MONTH, value)}
        items={DAYS_OF_MONTH_OPTIONS}
      />
    </>
  )
 
  const DaysOfWeekComp = (
    <>
      <Text inline>{getString('common.on')} </Text>
      <Select
        className={css.selectBox}
        value={DAYS_OF_WEEK_OPTIONS.find(op => op.value === state.dayOfWeek)}
        onChange={({ value }) => handleChange(ACTIONS.DAY_OF_WEEK, value)}
        items={DAYS_OF_WEEK_OPTIONS}
      />
    </>
  )
 
  const HoursComp = (
    <>
      <Text inline>{getString('common.at')} </Text>
      <Select
        className={css.selectBox}
        value={HOUR_OPTIONS.find(op => op.value === state.hours)}
        items={HOUR_OPTIONS}
        onChange={({ value }) => handleChange(ACTIONS.HOURS, value)}
      />
    </>
  )
 
  const frequencyToComponents: Record<string, React.ReactNode[]> = {
    year: [MonthComp, DaysOfMonthComp, HoursComp],
    month: [DaysOfMonthComp, HoursComp],
    week: [DaysOfWeekComp, HoursComp],
    day: [HoursComp]
  }
 
  const comps = frequencyToComponents[state.frequency] || [DaysOfWeekComp, HoursComp]
  return (
    <Container className={css.cron}>
      <Select
        value={FREQUENCY_OPTIONS.find(op => op.value === state.frequency)}
        onChange={({ value }) => handleChange(ACTIONS.FREQUENCY, value)}
        items={FREQUENCY_OPTIONS}
      />
      <div className={css.comps}>{comps.map(comp => comp)}</div>
    </Container>
  )
}
 
export default Cron