Groovy web console

subscribe to the feed Subscribe
to this
site
day_cal (via #groovywebconsole)
tweet this snippet Tweet
this
script

day_cal

Published 3 months ago by jvk with tags cals
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
config_optimal_move_in_period = 7
config_optimal_move_in_period_boost = 240000
one_day_in_seconds = 86400
config_outdated_ll_move_in_date_boost = 300000
config_before_move_in_window_boost = 350000
config_outside_gap_booking_boost = 200000
one_year_in_seconds = 31556926
min_dt = 2629743
for_script_field = 'yes'

move_in_ts=[1506816000]
move_out_ts=[1509408000]
today_dt = 1478822400
desired_dt = 1478822400
desired_odt = 0

gap_day_tolerance_days = 3
static_gap_decay = 0

selected_i_dt=null
final_boost = null
min_diff=one_year_in_seconds

//if(doc['gap_days'].value == null) {
//  gap=365*one_day_in_seconds
//} else {
//  gap=doc['gap_days'].value * one_day_in_seconds
//}

gap = 30* one_day_in_seconds
//joined_dt=[doc['move_in_ts'], doc['move_out_ts']].transpose()
joined_dt=[move_in_ts, move_out_ts].transpose();

if (joined_dt.size() > 0) {
  joined_dt.each { i, o -> 
    if (i < today_dt) { i = today_dt } 

    if (o-i >= min_dt && o >= desired_odt) {

      // Outside gap decay
      if (i < today_dt+(gap_day_tolerance_days*one_day_in_seconds) ) {
        window_start = today_dt+(gap_day_tolerance_days*one_day_in_seconds)
        window_end = window_start+gap
      } else {
        window_start = today_dt+(gap_day_tolerance_days*one_day_in_seconds)
        window_end = window_start+gap
      }

      if (window_start <= desired_dt && desired_dt <= window_end) {
        static_gap_decay = config_outside_gap_booking_boost
      }

      // Optimal move-in (begining and end both are exclusive)
      if (desired_dt >= i && desired_dt <= i + config_optimal_move_in_period*one_day_in_seconds) {
        final_boost = static_gap_decay + ( config_optimal_move_in_period_boost - ( (config_optimal_move_in_period_boost/config_optimal_move_in_period)*((desired_dt - i)/one_day_in_seconds) ) )
        selected_i_dt = i
      }

      // Outdated (begining is exclusive and end is inclusive)
      if (desired_dt > (i + config_optimal_move_in_period*one_day_in_seconds)) {
        final_boost = static_gap_decay + ( config_outdated_ll_move_in_date_boost * ( (desired_dt - (i + (config_optimal_move_in_period*one_day_in_seconds)))/one_day_in_seconds ) )
        selected_i_dt = i
      }

      // before date (begining is inclusive and end is exclusive)
      if (desired_dt < i ) {
        final_boost = static_gap_decay + (config_before_move_in_window_boost * ((i - desired_dt)/one_day_in_seconds) )
        selected_i_dt = i
      }

      if(for_script_field == 'yes') {
        print selected_i_dt
      } else {
        print final_boost
      }
    } else {
      return 0
    }
  }
}