Skip to content

Commit 8444c1a

Browse files
committed
Move script files to lib
1 parent 05f7745 commit 8444c1a

File tree

7 files changed

+151
-5
lines changed

7 files changed

+151
-5
lines changed
File renamed without changes.

lib/export_date_range.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'date'
2+
3+
class ExportDateRange
4+
class << self
5+
def for(year)
6+
[start_date(year), end_date(year)]
7+
end
8+
9+
private
10+
11+
def start_date(year)
12+
Date.new(year, 1, 1)
13+
end
14+
15+
def end_date(year)
16+
if this_year?(year)
17+
Date.new(year, Date.today.month, Date.today.day - 1)
18+
else
19+
Date.new(year, 12, 31)
20+
end
21+
end
22+
23+
def this_year?(year)
24+
year == Date.today.year
25+
end
26+
end
27+
end

lib/holiday_client.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'holidays'
2+
3+
class HolidayClient
4+
attr_reader :startdate, :enddate
5+
6+
def initialize(startdate, enddate, country_code)
7+
@startdate = startdate
8+
@enddate = enddate
9+
@country_code = country_code
10+
end
11+
12+
def holidays
13+
holidays = {}
14+
holidays_between.each do |holiday|
15+
holidays[holiday[:name]] = holiday[:date]
16+
end
17+
holidays
18+
end
19+
20+
private
21+
22+
def holiday_regions
23+
Holidays.available_regions.select do |region|
24+
region.to_s.start_with?(@country_code)
25+
end
26+
end
27+
28+
def holidays_between
29+
Holidays.cache_between(
30+
startdate, startdate.next_year(1) - 1,
31+
*holiday_regions, :informal, :observed
32+
).values.flatten
33+
end
34+
end

lib/output_generator.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require_relative 'holiday_client'
2+
require_relative 'distribution'
3+
require_relative 'interval_value_randomizer'
4+
5+
class OpeningHours
6+
def initialize(hours_array)
7+
@hours_array = hours_array
8+
end
9+
10+
def open
11+
@hours_array[0]
12+
end
13+
14+
def close
15+
@hours_array[1]
16+
end
17+
end
18+
19+
class OutputGenerator
20+
attr_reader :start_date, :end_date, :region, :queue_name
21+
22+
def initialize(start_date:, end_date:, region:, queue_name:, opening_hours:)
23+
@region = region
24+
@start_date = start_date
25+
@end_date = end_date
26+
@queue_name = queue_name
27+
@opening_hours = OpeningHours.new(opening_hours)
28+
end
29+
30+
def run
31+
puts header
32+
33+
(start_date..end_date).each do |date|
34+
output_csv_lines_for(date)
35+
end
36+
end
37+
38+
def output_csv_lines_for(date)
39+
if holiday_dates.include?(date)
40+
generate_output(
41+
date, rand(12_000..13_500),
42+
distribution.special_day, distribution.special_week
43+
)
44+
else
45+
generate_output(date, rand(13_500..14_500),
46+
distribution.normal_day, distribution.normal_week)
47+
end
48+
end
49+
50+
def holiday_dates
51+
HolidayClient.new(
52+
start_date, end_date, region
53+
).holidays.map { |h| h[1] }
54+
end
55+
56+
private
57+
58+
def build_time_string(interval)
59+
time_in_minutes = interval * 15
60+
hours = time_in_minutes / 60
61+
minutes = time_in_minutes * 60 / 60 % 60
62+
time = Time.new(2001, 1, 1, hours, minutes, 0)
63+
time.strftime('%H:%M')
64+
end
65+
66+
def header
67+
'Queue;Date;Time;CallsOffered;CallsHandled;AHT'
68+
end
69+
70+
def generate_output(date, weekly_volume,
71+
daily_distribution, weekly_destribution)
72+
daily_volume = weekly_destribution[date.wday] * weekly_volume
73+
daily_distribution.each_with_index do |value, interval|
74+
random = IntervalValueRandomizer.new(daily_volume, value)
75+
time_string = build_time_string(interval)
76+
puts "#{queue_name};#{date.strftime('%d.%m.%Y')};#{time_string};" \
77+
"#{random.calls};#{random.handled_calls};#{random.aht}"
78+
end
79+
end
80+
81+
def distribution
82+
Distribution.new(open_time: @opening_hours.open,
83+
close_time: @opening_hours.close)
84+
end
85+
end

output_generator.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require_relative 'holiday_client'
2-
require_relative 'distribution'
3-
require_relative 'interval_value_randomizer'
1+
require_relative 'lib/holiday_client'
2+
require_relative 'lib/distribution'
3+
require_relative 'lib/interval_value_randomizer'
44

55
class OpeningHours
66
def initialize(hours_array)

random_csv.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require_relative 'export_date_range'
2-
require_relative 'output_generator'
1+
require_relative 'lib/export_date_range'
2+
require_relative 'lib/output_generator'
33

44
# generate file for importer using > operator
55
# ruby random_csv.rb > filename.csv

0 commit comments

Comments
 (0)