Reservation example

A complete example of scheduling the reservations for a particular set of resources.

Summary

The example here is for reserving an elevator in an apartment complex. The example demonstrates:

  • Defining a set of reservations
  • Defining a set of elevators that will be reserved
  • Using schedule.tasks to get the tasks into the proper format
  • Using schedule.resources to get the resources into the proper format
  • Creating a new schedule

Note This example assumes the code is running in the browswer. To see an example that runs in Node, see the work item example.

Code

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
h1 {
  font-size: 16pt;
}

h2 {
  font-size: 14pt;
}

p {
  font-size: 12pt;
  line-height: 12pt;
  margin: 2pt;
  padding-left: 20px;
}

</style>
</head>
<body>
</body>

<script src="later.min.js" type="text/javascript"></script>
<script src="schedule.min.js" type="text/javascript"></script>
<script type="text/javascript">

// We'll use the Later text parser to create schedules that are readable
var p = later.parse.text;

// Step 1: Define our reservations (tasks)
var reservations = [
  {name: 'Joe', length: 4, availability: 'after 12:00'},
  {name: 'Mike', length: 2},
  {name: 'Frank', length: 8},
  {name: 'John', length: 3, availability: 'on Thurs and Fri'},
  {name: 'Peter', length: 1, availability: 'before 10:00am'},
  {name: 'Sam', length: 2},
  {name: 'Alan', length: 2},
  {name: 'James', length: 8},
  {name: 'Steve', length: 1, availability: 'after 12:00 and before 1:00pm'},
  {name: 'Mark', length: 2},
  {name: 'Alex', length: 8}
];

// Step 2: Define our elevators (resources)
var elevators = [
  {name: 'E1', availability: 'every weekday after 8:00am and before 4:00pm'},
  {name: 'E2', availability: 'every weekday after 8:00am and before 4:00pm'}
];

// Step 3: Tasks aren't in the right format, need to create a generator
var t = schedule.tasks()
          .id(function(d) { return d.name; })
          // our length is in hours, convert to minutes
          .duration(function(d) { return d.length * 60; })
          // use later.parse.text to parse text into a usable schedule
          .schedule(function(d) { return d.availability ? p(d.availability) : undefined; })
          // prioritize our reservations on first come first serve
          .priority(function(d, i) { return 100 - i; })
          // elevator reservations have to be contiguous
          .minSchedule(function(d) { return d.length * 60; })
          // assume that only one elevator is available for reservations to start
          .resources(['E1']);

var tasks = t(reservations);

// Step 4: Resources aren't in the right format, need to create a generator
var r = schedule.resources()
          .id(function(d) { return d.name; })
          .schedule(function(d) { return d.availability ? p(d.availability) : undefined; });

var resources = r(elevators);

// Step 5: Pick a start date for the schedule and set correct timezone
var start = new Date(2013, 2, 21);
schedule.date.localTime();

// Step 6: Create the schedule
var s = schedule.create(tasks, resources, null, start);

for(var id in s.scheduledTasks) {
  var st = s.scheduledTasks[id];
  console.log(st);
  document.write('<h2>' + id + '</h2>');
  document.write('<p><b>Duration:</b> ' + st.duration + ' mins</p>');
  document.write('<p><b>Start:</b> ' + new Date(st.earlyStart).toLocaleString() + '</p>');
  document.write('<p><b>Finish:</b> ' + new Date(st.earlyFinish).toLocaleString() + '</p>');
}

</script>
</html>