Class: Pacing::Pacer

Inherits:
Object
  • Object
show all
Defined in:
lib/pacing/pacer.rb

Overview

two modes(strict: use start dates strictly in calculating pacing)

Constant Summary collapse

COMMON_YEAR_DAYS_IN_MONTH =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(school_plan:, date:, non_business_days:, state: :us_tn, mode: :liberal, summer_holidays: []) ⇒ Pacer

Returns a new instance of Pacer.

Raises:

  • (ArgumentError)


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
# File 'lib/pacing/pacer.rb', line 10

def initialize(school_plan:, date:, non_business_days:, state: :us_tn, mode: :liberal, summer_holidays: [])
  @school_plan = school_plan
  @non_business_days = non_business_days
  @date = date
  @state = state
  @mode = [:strict, :liberal].include?(mode) ? mode : :liberal

  raise ArgumentError.new("You must pass in at least one school plan") if @school_plan.nil?
  raise TypeError.new("School plan must be a hash") if @school_plan.class != Hash
  
  raise ArgumentError.new('You must pass in a date') if @date.nil?
  raise TypeError.new("The date should be formatted as a string in the format mm-dd-yyyy") if @date.class != String || !/(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d/.match?(@date)
  raise ArgumentError.new('Date must be within the interval range of the school plan') if !date_within_range
  
  @non_business_days.each do |non_business_day|
    raise TypeError.new('"Non business days" dates should be formatted as a string in the format mm-dd-yyyy') if non_business_day.class != String || !/(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d/.match?(non_business_day)
  end

  @school_plan[:school_plan_services].each do |school_plan_service|
    raise TypeError.new("School plan type must be a string and cannot be nil") if school_plan_service[:school_plan_type].class != String || school_plan_service[:school_plan_type].nil?

    raise ArgumentError.new("School plan services start and end dates can not be nil") if school_plan_service[:start_date].nil? || school_plan_service[:end_date].nil?

    raise TypeError.new("School plan services start and end dates should be formatted as a string in the format mm-dd-yyyy") if school_plan_service[:start_date].class != String || !/(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d/.match?(school_plan_service[:start_date])

    raise TypeError.new("School plan services start and end dates should be formatted as a string in the format mm-dd-yyyy") if school_plan_service[:end_date].class != String || !/(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])-(19|20)\d\d/.match?(school_plan_service[:end_date])

    raise TypeError.new("Type of service must be a string and cannot be nil") if school_plan_service[:type_of_service].class != String || school_plan_service[:type_of_service].nil?

    raise TypeError.new("Frequency must be an integer and cannot be nil") if school_plan_service[:frequency].class != Integer || school_plan_service[:frequency].nil?

    raise TypeError.new("Interval must be a string and cannot be nil") if school_plan_service[:interval].class != String || school_plan_service[:interval].nil?

    raise TypeError.new("Time per session in minutes must be an integer and cannot be nil") if school_plan_service[:time_per_session_in_minutes].class != Integer || school_plan_service[:time_per_session_in_minutes].nil?

    raise TypeError.new("Completed visits for current interval must be an integer and cannot be nil") if school_plan_service[:completed_visits_for_current_interval].class != Integer || school_plan_service[:completed_visits_for_current_interval].nil?

    raise TypeError.new("Extra sessions allowable must be an integer and cannot be nil") if school_plan_service[:extra_sessions_allowable].class != Integer || school_plan_service[:extra_sessions_allowable].nil?

    raise TypeError.new("Interval for extra sessions allowable must be a string and cannot be nil") if school_plan_service[:interval_for_extra_sessions_allowable].class != String || school_plan_service[:interval_for_extra_sessions_allowable].nil?
  end

  @summer_holidays = summer_holidays.empty? ? parse_summer_holiday_dates : [parse_date(summer_holidays[0]), parse_date(summer_holidays[1])]
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def date
  @date
end

#intervalObject (readonly)

Returns the value of attribute interval.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def interval
  @interval
end

#modeObject (readonly)

Returns the value of attribute mode.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def mode
  @mode
end

#non_business_daysObject (readonly)

Returns the value of attribute non_business_days.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def non_business_days
  @non_business_days
end

#school_planObject (readonly)

Returns the value of attribute school_plan.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def school_plan
  @school_plan
end

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def state
  @state
end

#summer_holidaysObject (readonly)

Returns the value of attribute summer_holidays.



8
9
10
# File 'lib/pacing/pacer.rb', line 8

def summer_holidays
  @summer_holidays
end

Instance Method Details

#business_days(start_date, end_date) ⇒ Object

days on which a session can hold



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pacing/pacer.rb', line 158

def business_days(start_date, end_date)
  # remove saturdays and sundays
  # remove holidays(array from Ambiki)
  # remove school holidays/non business days
  # should we remove today?(datetime call?)
  summer = get_working_days(summer_holidays[0], summer_holidays[1])

  working_days = get_working_days(start_date, end_date)

  holidays = get_holidays(start_date, end_date)

  working_days.sort - parsed_non_business_days.sort - holidays.sort - summer.sort
end

#calculateObject



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
# File 'lib/pacing/pacer.rb', line 55

def calculate
  # filter out services that haven't started or whose time is passed
  services = @school_plan[:school_plan_services].filter do |school_plan_service|
    within = true
    if !(parse_date(school_plan_service[:start_date]) <= parse_date(@date) && parse_date(@date) <= parse_date(school_plan_service[:end_date]))
      within = false
    end

    within
  end

  services = services.map do |service|
    discipline = {}

    expected = expected_visits(start_date: parse_date(service[:start_date]), end_date: parse_date(service[:end_date]), frequency: service[:frequency], interval: service[:interval])

    discipline[:pace] = pace(service[:completed_visits_for_current_interval], expected)

    discipline[:reset_date] = reset_date(start_date: service[:start_date], interval: service[:interval])

    discipline[:reset_date] = parse_date(discipline[:reset_date]) > parse_date(service[:end_date]) && service[:interval] == "yearly" ? service[:end_date] : discipline[:reset_date]

    discipline[:remaining_visits] = remaining_visits(completed_visits: service[:completed_visits_for_current_interval], required_visits: service[:frequency] + service[:extra_sessions_allowable])

    discipline[:pace_indicator] = pace_indicator(discipline[:pace])

    discipline[:used_visits] =  service[:completed_visits_for_current_interval]

    discipline[:suggested_rate] = suggested_rate(remaining_visits: discipline[:remaining_visits], start_date: parse_date(service[:start_date]), interval: service[:interval])

    discipline[:expected_visits_at_date] = expected

    discipline[:type_of_service] = service[:type_of_service]

    discipline
  end

  disciplines_cleaner ([speech_discipline(services), occupational_discipline(services), physical_discipline(services), feeding_discipline(services)])
end

#date_within_rangeObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/pacing/pacer.rb', line 275

def date_within_range
  valid_range_or_exceptions = false

  begin
    @school_plan[:school_plan_services].each do |school_plan_service|
      if (parse_date(school_plan_service[:start_date]) <= parse_date(@date) && parse_date(@date) <= parse_date(school_plan_service[:end_date]))
        valid_range_or_exceptions = true
      end
    end
  rescue => exception
    valid_range_or_exceptions = true
  end
  
  valid_range_or_exceptions
end

#discipline_data(services, discipline) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/pacing/pacer.rb', line 375

def discipline_data(services, discipline)
  services.each do |service|
    discipline[:pace] = discipline[:pace] ? discipline[:pace].to_i + service[:pace].to_i : service[:pace]

    discipline[:remaining_visits] = discipline[:remaining_visits] ? discipline[:remaining_visits].to_i + service[:remaining_visits].to_i : service[:remaining_visits]

    discipline[:used_visits] = discipline[:used_visits] ? discipline[:used_visits].to_i + service[:used_visits].to_i : service[:used_visits]

    discipline[:expected_visits_at_date] = discipline[:expected_visits_at_date] ? discipline[:expected_visits_at_date].to_i + service[:expected_visits_at_date].to_i : service[:expected_visits_at_date]

    discipline[:suggested_rate] = discipline[:suggested_rate] ? discipline[:suggested_rate].to_f + service[:suggested_rate].to_f : service[:suggested_rate].to_f

    discipline[:reset_date] = (!discipline[:reset_date].nil? && parse_date(service[:reset_date]) < parse_date(discipline[:reset_date])) ? discipline[:reset_date] : service[:reset_date]
  end

  discipline[:pace_indicator] = pace_indicator(discipline[:pace])
  discipline[:pace_suggestion] = readable_suggestion(rate: discipline[:suggested_rate]) 

  discipline.delete(:suggested_rate)
  discipline
end

#disciplines_cleaner(disciplines) ⇒ Object



432
433
434
435
# File 'lib/pacing/pacer.rb', line 432

def disciplines_cleaner(disciplines)
  # use the fake arbitrary reset date to remove unrequired disciplines
  disciplines.filter { |discipline| !discipline.empty? }
end

#end_of_treatment_date(reset_start, interval) ⇒ Object



257
258
259
# File 'lib/pacing/pacer.rb', line 257

def end_of_treatment_date(reset_start, interval)
  reset_start + interval_days(interval)
end

#expected_visits(start_date:, end_date:, frequency:, interval:) ⇒ Object

get a spreadout of visit dates over an interval by using simple proportion.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pacing/pacer.rb', line 96

def expected_visits(start_date:, end_date:, frequency:, interval:)
  reset_start = start_of_treatment_date(start_date, interval)

  reset_end = end_of_treatment_date(reset_start, interval)

  days_between = business_days(reset_start, reset_end).count

  days_passed = 0

  visits = 0

  if parse_date(@date) > reset_start
    days_passed = business_days(reset_start, parse_date(@date)).count
  end

  if days_between != 0
    visits = ((frequency/days_between.to_f) * days_passed).round
  end

  return visits
end

#feeding_discipline(services) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/pacing/pacer.rb', line 354

def feeding_discipline(services)
  discipline = {
    :discipline=>"Feeding Therapy",
    :remaining_visits=>0,
    :used_visits=>0,
    :pace=>0,
    :pace_indicator=>"🐢",
    :pace_suggestion=>"once a day",
    :suggested_rate => 0,
    :expected_visits_at_date=>0,
    :reset_date=> nil } # some arbitrarity date in the past

  discipline_services = services.filter do |service|
    ["Feeding Therapy"].include? service[:type_of_service]
  end

  return {} if discipline_services.empty?

  discipline_data(discipline_services, discipline)
end

#get_holidays(start_date, end_date) ⇒ Object



209
210
211
212
213
214
# File 'lib/pacing/pacer.rb', line 209

def get_holidays(start_date, end_date)
  begin
    Holidays.between(start_date, end_date, @state).map { |holiday| holiday[:date] }
  rescue => exception
  end
end

#get_working_days(start_date, end_date) ⇒ Object



203
204
205
206
207
# File 'lib/pacing/pacer.rb', line 203

def get_working_days(start_date, end_date)
  (start_date..end_date).filter do |day|
    !(day.saturday? || day.sunday?)
  end
end

#interval_days(interval) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/pacing/pacer.rb', line 118

def interval_days(interval)
  case interval
  when "monthly"
    return COMMON_YEAR_DAYS_IN_MONTH[(parse_date(@date)).month]
  when "weekly"
    return 6
  when "yearly"
    return parse_date(@date).leap? ? 366 : 365
  end
end

#occupational_discipline(services) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/pacing/pacer.rb', line 312

def occupational_discipline(services)
  discipline = {
    :discipline=>"Occupational Therapy",
    :remaining_visits=>0,
    :used_visits=>0,
    :pace=>0,
    :pace_indicator=>"🐢",
    :pace_suggestion=>"once a day",
    :suggested_rate => 0,
    :expected_visits_at_date=>0,
    :reset_date=> nil } # some arbitrarity date in the past

  discipline_services = services.filter do |service|
    ["occupation therapy", "occupational therapy"].include? (service[:type_of_service].downcase)
  end

  return {} if discipline_services.empty?

  discipline_data(discipline_services, discipline)
end

#pace(actual_visits, expected_visits) ⇒ Object



129
130
131
132
# File 'lib/pacing/pacer.rb', line 129

def pace(actual_visits, expected_visits)
  # Pace = actual visits at point in time - expected visits to meet frequency at that point in time
  actual_visits - expected_visits
end

#pace_indicator(pace) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/pacing/pacer.rb', line 134

def pace_indicator(pace)
  if pace == 0
    return "😁"
  elsif pace > 0
    return "🐇"
  elsif pace < 0
    return "🐢"
  end
end

#parse_date(date) ⇒ Object



144
145
146
147
148
149
# File 'lib/pacing/pacer.rb', line 144

def parse_date(date)
  begin
    Date.strptime(date, '%m-%d-%Y')
  rescue => exception
  end
end

#parse_summer_holiday_datesObject



437
438
439
440
441
442
443
444
445
# File 'lib/pacing/pacer.rb', line 437

def parse_summer_holiday_dates
  holidays_start = parse_date("05-13-#{parse_date(@date).year}")
  holidays_start += 1 until holidays_start.wday == 5

  holidays_end = parse_date("08-01-#{parse_date(@date).year}")
  holidays_start += 1 until holidays_start.wday == 1

  [holidays_start, holidays_end]
end

#parsed_non_business_daysObject



151
152
153
154
155
# File 'lib/pacing/pacer.rb', line 151

def parsed_non_business_days
  @non_business_days.map do |day|
    parse_date(day)
  end
end

#physical_discipline(services) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/pacing/pacer.rb', line 333

def physical_discipline(services)
  discipline = {
    :discipline=>"Physical Therapy",
    :remaining_visits=>0,
    :used_visits=>0,
    :pace=>0,
    :pace_indicator=>"🐢",
    :pace_suggestion=>"once a day",
    :suggested_rate => 0,
    :expected_visits_at_date=>0,
    :reset_date=> nil } # some arbitrarity date in the past

  discipline_services = services.filter do |service|
    ["Physical Therapy"].include? service[:type_of_service]
  end

  return {} if discipline_services.empty?

  discipline_data(discipline_services, discipline)
end

#readable_suggestion(rate:) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/pacing/pacer.rb', line 397

def readable_suggestion(rate:)
  # rate = suggested_rate(remaining_visits: remaining_visits, start_date: start_date, interval: interval)

  if rate < 0.2
    'less than once per week'
  elsif rate >= 0.2 && rate < 0.25
    'once a week'
  elsif rate >= 0.25 && rate < 0.33
    'once every 4 days'
  elsif rate >= 0.33 && rate < 0.5
    'once every 3 days'
  elsif rate == 0.5
    'once every other day'
  elsif rate > 0.5 && rate < 1
    'about once every other day'
  elsif rate >= 1.00
    'once a day'
  end
end

#remaining_days(start_date:, interval:) ⇒ Object



423
424
425
426
427
428
429
430
# File 'lib/pacing/pacer.rb', line 423

def remaining_days(start_date:, interval:)
  reset_start = start_of_treatment_date(start_date, interval)
  reset_end = end_of_treatment_date(reset_start, interval)

  days_left = business_days(parse_date(@date), reset_end).count

  days_left
end

#remaining_visits(completed_visits:, required_visits:) ⇒ Object

scoped to the interval



173
174
175
176
177
# File 'lib/pacing/pacer.rb', line 173

def remaining_visits(completed_visits:, required_visits:)
  visits = required_visits.to_i - completed_visits.to_i
  visits = 0 if visits < 0
  visits
end

#reset_date(start_date:, interval:) ⇒ Object

scoped to the interval



180
181
182
183
184
185
186
187
188
189
# File 'lib/pacing/pacer.rb', line 180

def reset_date(start_date:, interval:)
  case interval
  when "monthly"
    return reset_date_monthly(start_date, interval)
  when "weekly"
    return reset_date_weekly(start_date, interval)
  when "yearly"
    return reset_date_yearly(start_date)
  end
end

#reset_date_monthly(start_date, interval) ⇒ Object

reset date for the monthly interval



228
229
230
# File 'lib/pacing/pacer.rb', line 228

def reset_date_monthly(start_date, interval)
  (start_of_treatment_date(parse_date(start_date), interval) + COMMON_YEAR_DAYS_IN_MONTH[(parse_date(@date)).month]).strftime("%m-%d-%Y")
end

#reset_date_weekly(start_date, interval) ⇒ Object

reset date for the weekly interval



233
234
235
# File 'lib/pacing/pacer.rb', line 233

def reset_date_weekly(start_date, interval)
  (start_of_treatment_date(parse_date(start_date), interval) + 7).strftime("%m-%d-%Y")
end

#reset_date_yearly(start_date) ⇒ Object

reset date for the yearly interval



223
224
225
# File 'lib/pacing/pacer.rb', line 223

def reset_date_yearly(start_date)
  (parse_date(@date).leap? ? parse_date(start_date) + 366 : parse_date(start_date) + 365).strftime("%m-%d-%Y")
end

#speech_discipline(services) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/pacing/pacer.rb', line 291

def speech_discipline(services)
  discipline = {
    :discipline => "Speech Therapy",
    :remaining_visits => 0,
    :used_visits => 0,
    :pace => 0,
    :pace_indicator => "🐢",
    :pace_suggestion => "once a day",
    :suggested_rate => 0,
    :expected_visits_at_date => 0,
    :reset_date => nil } # some arbitrarity date in the past

  discipline_services = services.filter do |service|
    ["Language Therapy", "Speech Therapy", "Speech and Language Therapy", "Speech Language Therapy"].include? service[:type_of_service]
  end

  return {} if discipline_services.empty?

  discipline_data(discipline_services, discipline)
end

#start_of_treatment_date(start_date, interval = "monthly") ⇒ Object

scoped to the interval



192
193
194
195
196
197
198
199
200
201
# File 'lib/pacing/pacer.rb', line 192

def start_of_treatment_date(start_date, interval="monthly")
  case interval
  when "monthly"
    return start_of_treatment_date_monthly(start_date)
  when "weekly"
    return start_of_treatment_date_weekly(start_date)
  when "yearly"
    return start_of_treatment_date_yearly(start_date)
  end
end

#start_of_treatment_date_monthly(start_date) ⇒ Object

start of treatment for the montly interval



249
250
251
252
253
254
255
# File 'lib/pacing/pacer.rb', line 249

def start_of_treatment_date_monthly(start_date)
  if @mode == :strict
    return parse_date("#{parse_date(@date).month}-#{start_date.day}-#{parse_date(@date).year}")
  else
    return parse_date("#{parse_date(@date).month}-01-#{parse_date(@date).year}")
  end
end

#start_of_treatment_date_weekly(start_date) ⇒ Object

start of treatment for the weekly interval



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/pacing/pacer.rb', line 262

def start_of_treatment_date_weekly(start_date)
  parsed_date = parse_date(@date)
  week_start_date = week_start(parsed_date)
  weekly_date = week_start_date

  if week_start_date != parsed_date && @mode == :strict
    weekly_date = week_start_date + start_date.wday #unless start_date.wday == 1
    weekly_date = parsed_date < weekly_date ? weekly_date - 7 : weekly_date
  end

  weekly_date
end

#start_of_treatment_date_yearly(start_date) ⇒ Object

start of treatment for the yearly interval



238
239
240
241
242
243
244
245
246
# File 'lib/pacing/pacer.rb', line 238

def start_of_treatment_date_yearly(start_date)
  start = parse_date("#{start_date.month}-#{start_date.day}-#{parse_date(@date).year}")
  if start > parse_date(date)
    start = start_date
  end

  start
  # start_date
end

#suggested_rate(remaining_visits:, start_date:, interval:) ⇒ Object



417
418
419
420
421
# File 'lib/pacing/pacer.rb', line 417

def suggested_rate(remaining_visits:, start_date:, interval:)
  days_left = remaining_days(start_date: start_date, interval: interval).to_f
  days_left = 1 if days_left == 0
  (remaining_visits / days_left.to_f).round(2)
end

#week_start(date, offset_from_sunday = 0) ⇒ Object

get actual date of the first day of the week where date falls



217
218
219
220
# File 'lib/pacing/pacer.rb', line 217

def week_start(date, offset_from_sunday=0)
  return date if date.monday?
  date - ((date.wday - offset_from_sunday) % 7)
end