ERP system - how to handle timeline in js

184 views Asked by At

Im trying to build a simple ERP system in javascript for my company and learn something in progress. I have most things set up but I cant figure out how to handle a time.
For an example:
I have 3 machines, 2 operators working on some hours. Than i got list of things to be done for next 3 days (some of them takes 5 min others 3h). What i want to do is go through all jobs by its importance and place it with free operator and create a plan for all activities - what time they should start and end.
Sooo...
Operator 1 start first job at 8:00 and hes busy till 8:45,
Operator 2 get second job because operator 1 is busy he start at 8:00 and finish at 8:35.
Third job goes to operator 2 because he will finish first...ect
I know how to do it in theory i just dont know how to write it in code...
In other words, what is the best way to represent a timeline in code?

Regards

1

There are 1 answers

1
guysigner On BEST ANSWER

Record time info (start and duration) for each job. Record the occupying jobs for each operator.

Something like this:

  var jobs = [
  {
    id: 'Job1',
    topic: 'Do something',
    start: 1483200720000, // milliseconds from Jan 1 1970 UTC
    takes: 1500000, // 25 mins (in milliseconds)  
    operator: ''
  },
  {
    id: 'Job2',
    topic: 'Do another thing',
    start: 1483208700000, // milliseconds from Jan 1 1970 UTC
    takes: 5400000, // 1 hour 30 mins (in milliseconds)    
    operator: ''
  },
  // Job 3 starts one minute after Job2 starts so it must take Operator 2
  {
    id: 'Job3',
    topic: 'Do yet another thing',
    start: 1483208760000, // milliseconds from Jan 1 1970 UTC
    takes: 5400000, // 1 hour 30 mins (in milliseconds)    
    operator: ''
  },
];
var operators = 
[
  {
    name: 'Op1',
    jobs: [],
  },
  {
    name: 'Op2',
    jobs: []
  },
];

Then, order the jobs by they're start time, and find for every job, an available operator - that is an operator who has no other jobs in the wanted time span. Here's the rest of the code:

function allocateJobs() {
  // define sort by start time
  var jobsSorter = function (a,b) {return a.start - b.start};

  // sort the jobs by the time they start at
  jobs = jobs.sort(jobsSorter);
  for (var i = 0 ; i < jobs.length ; i++){
    var job = jobs[i];
    for (var j = 0 ; j < operators.length ; j++){
      var op = operators[j];
      if (isAvailable(op, job.start, job.start + job.takes)) {
        // fix job in operator data
        op.jobs.push(job);    

        // fix operator in job data
        job.operator = op.name;        
        break;
      }
    }

  }
}

/** Determines whether an operator is free between fromTime till toTime */
function isAvailable(operator, fromTime, toTime){
  for (var i = 0 ; i < operator.jobs.length ; i++){
    var job = operator.jobs[i];

    // case there is a job starting between fromTime and toTime
    if (job.start > fromTime && job.start < toTime) return false;

    var jobEnds = job.start + job.takes;
    // case there is a job ending between fromTime and toTime
    if (jobEnds > fromTime && jobEnds < toTime) return false;
  }
  return true;
}

Here's a JSFiddle Demo