An effective way to calculate tasks per day with an interval of time

I have a problem that I am trying to solve, but I feel that I am not solving it in the most efficient way. Any new light on this is appreciated.

This is the period of time that I want to look at

start = '2013-01-01'; end = '2013-01-08'; 

List of appointments with start and end dates

 assignments = [{start: '2013-01-01', end: '2013-01-01'}, {start: '2013-01-01', end: '2013-01-02'}, {start: '2013-01-01', end: '2013-01-03'}, {start: '2013-01-01', end: '2013-01-04'}]; 

I want to get a result every day in a time span with a value representing how many appointments were active that day. This is what I want the result to look like this:

 result = [{date: '2013-01-01', value: 4}, {date: '2013-01-02', value: 3}, {date: '2013-01-03', value: 2}, {date: '2013-01-04', value: 1}, {date: '2013-01-05', value: 0}, {date: '2013-01-06', value: 0}, {date: '2013-01-07', value: 0}, {date: '2013-01-08', value: 0}]; 

My attempts include iterating through each date in a daterange and checking how many jobs fall out on that day. I also tried to go the other way, iterating through the destination, and then its start and end dates by pushing a value into an array for each date.

Is there one of these ways on the right track, or is there a smarter, more efficient way?

Note. I do this using javascript with underscore and js moment for dates.

+4
source share
1 answer

iterate through assignments and then start and end dates, pushing values ​​into an array for each date

Not sure if you mean the right thing here. Instead of clicking on an array, an object should be sufficient to match dates (like string keys) with their corresponding counter (integer). The fundamental aspect here is that you can directly access date counts (without loops).

Is there one of these ways on the right track or is there a more efficient way?

Both are on the right track. Which one is more efficient depends on your data, mainly, how sparse distributions are distributed over a range of dates (or maybe even outside?).

 Having a := number of assignments d := average duration of assignments (number of days per assignment) n := number of days in your range then the runtimes would be O(a*d) for iterating assignments and their duration O(a*d+n) for that and building a result with all days O(a*n) for iterating days and checking all assignments 

Since your result structure is an array of dates in a range, your first approach is probably the best choice.

If you have really large data arrays and, possibly, some tasks outside the date range, then sorting tasks can give you additional advantages, since you can easily filter out irrelevant ones.

0
source

Source: https://habr.com/ru/post/1482925/


All Articles