Creating Appointments from Time and Hours of Availability in Dataweave
Author: Sanket Kangle
This article will learn how to create slots if you have starting time, duration of one slot, and total availability in hours using Dataweave script.
Use case: Let us say we are running a hospital, we have a visiting doctor coming in tomorrow, who will be available from morning 10:30 am for 3 hours and evening 5 pm for 2 hours. We want to create all possible appointments on that particular day.
Sample input:
{ “Doc_name”: “Dr. Max Mule”, “duration_in_minutes”: 30, “timeslots”: { “start_time”: [ “10:30”, “17:00” ], “hours”: [ 3, 2 ] }}
Required sample output:
[ { “Dr. Max Mule”: { “timeslot_start”: “10:30:00”, “timeslot_end”: “11:00:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “11:00:00”, “timeslot_end”: “11:30:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “11:30:00”, “timeslot_end”: “12:00:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “12:00:00”, “timeslot_end”: “12:30:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “12:30:00”, “timeslot_end”: “13:00:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “13:00:00”, “timeslot_end”: “13:30:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “17:00:00”, “timeslot_end”: “17:30:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “17:30:00”, “timeslot_end”: “18:00:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “18:00:00”, “timeslot_end”: “18:30:00” } }, { “Dr. Max Mule”: { “timeslot_start”: “18:30:00”, “timeslot_end”: “19:00:00” } }]
We can achieve this in following steps:
- Create an array of the number of time slots per window of availability
- Create an array of slots by adding duration in starting time appropriate number of times.
- Get the required output format.
Step 1: Create an array of number of time slots slots per window of availability
We need to find how many slots can be created. In our example, the duration for a single appointment is 30 minutes. The doctor is available for 3 hours from 10:30, i.e. we can have 6 slots in the morning. Similarly, in the evening we can have 4 appointments. We can get this using following script
%dw 2.0output application/jsonfun getNoOfSlots(hour, duration) = ((((hour as Number)*60)/duration)) as String {format: “#”} as Number var dur = payload.duration_in_minutesvar noOfSlots = payload.timeslots.hours map (hour, index) -> { a: getNoOfSlots(hour, dur)}.a — noOfSlots
The output will be as following
[ 6, 4]
Step 2: Create an array of slots by adding duration in starting time appropriate number of times.
This can be achieved by adding a few additional functions and variables in the above dataweave script which will look something like the following.
%dw 2.0output application/jsonfun getNoOfSlots(hour, duration) = ((((hour as Number)*60)/duration)) as String {format: “#”} as Number fun prepareList(list:Array, maxSize: Number) = if(sizeOf(list) >= maxSize )listelseprepareList(list ++ [(sizeOf(list)) as Number],maxSize) var dur = payload.duration_in_minutesvar duration_in_period = “PT” ++ (dur as String) ++ “M” var noOfSlots = payload.timeslots.hours map (hour, index) -> { a: getNoOfSlots(hour, dur)}.a var slot_num_list = noOfSlots map(slot, index) ->{ a: prepareList([], slot)}.a var start_time = payload.timeslots.start_timevar size = prepareList([], sizeOf(start_time))var slotsPerDay = flatten(size map(object, index) -> { a: slot_num_list[index] map(slot) ->{ b: start_time[index] as LocalTime+ (“PT” ++ (slot*dur) as String ++ “M”) }.b}.a) — slotsPerDay
The output is as following:
[ “10:30:00”, “11:00:00”, “11:30:00”, “12:00:00”, “12:30:00”, “13:00:00”, “17:00:00”, “17:30:00”, “18:00:00”, “18:30:00”]
Step 3: Get the required output format
With a few tweeks in the body section, we can get required output. Now body of our script will be as below
— flatten (slotsPerDay map(slot_start) ->{ (payload.Doc_name):{ timeslot_start: slot_start, timeslot_end: slot_start + duration_in_period }})
We will get the required output format as stated above.
Hope this was useful