Ruby/ice_cube: Exclude whole day for hourly recurring event - ruby

Just started playing around with ice_cube I've got a weekly schedule (with a granularity of half an hour) created
schedule = IceCube::Schedule.new(Time.now.change(:min => 30))
with several rules (let's say 20) such as e.g.
IceCube::Rule.weekly.day(:tuesday).hour_of_day(14).minute_of_hour(30)
or
IceCube::Rule.weekly.day(:wednesday).hour_of_day(10).minute_of_hour(0)
Now I'd like to exclude a full day, which would subsequently exclude all occurrences during this full day.
I've tried
schedule.add_exception_date [DATE]
but it seems that my exception has to match the event exactly.
Is there a way to get this done without looping through all rules and creating exception for the exact times for the date specified?
Update:
To make a better example:
Weekly schedule:
* Every monday at 14:40
* Every monday at 15:00
* Every thursday at 16:00
* Every saturday at 10:00
Exception date:
Tuesday, 13th of September 2011
=> For the week from Monday 12th to Sunday 18th I'd like to get only the occurrences on Thursday and Saturday.
One solution could look something like this, but it's a little icky:
schedule = IceCube::Schedule.from_yaml([PERSISTED SCHEDULE])
occurrences = schedule.occurrences_between([START TIME], [END TIME])
exceptions = schedule.exdates.map(&:to_date)
occurrences.reject {|occurrence|
exceptions.include?(occurrence.to_date)
}
—Any better ideas?

Since there seems to be no other solution and in order to close this question, here’s what I'm using (as described in an update to the original question above already):
schedule = IceCube::Schedule.from_yaml([PERSISTED SCHEDULE])
occurrences = schedule.occurrences_between([START TIME], [END TIME])
exceptions = schedule.exdates.map(&:to_date)
occurrences.reject {|occurrence|
exceptions.include?(occurrence.to_date)
}

Related

Laravel How to Extra Rows with Duplicates

I am having problem constructing my query to find rows containing duplicated values specifically in ('day','time_from',''time_to').
The problem is that the value of 'day' column could be 1 or 2 combination(M or M-W)
The query should extract the following ('M-W','7:00','8:30') and ('M','7:00','8:30') as duplicate.
Is there any workarounds with these? Quite new to Laravel.
This my current query which can only extract if ('day') are exact value.
$dupeTeacher = DB::table('load_schedule')
->select('time_from', 'time_to', 'day')
->groupBy('time_from', 'time_to', 'day')
->havingRaw('COUNT(*) > 1');
$dupliTeacher=DB::table('load_schedule')
->select('load_schedule.*')
->joinSub($dupeTeacher, 'dupe_teacher', function ($join)
{
$join->on('load_schedule.day','=', 'dupe_Schedules.day');
$join->on('load_schedule.c_time','=', 'dupe_Schedules.time_from');
$join->on('load_schedule.c_time','=', 'dupe_Schedules.time_to');
})
->paginate(10);
I don't think you can do it as you're currently envisaging, as even if you could get it to see "M-W" as being a duplicate of either "M" or "W", it would not see it as a duplicate for "T". And two days of the week start with T...
You would be better, I think, storing the schedule entries in a separate table, using a relationship to tie it to a particular teacher. Each schedule entry would have a start time and an end time - when you populate it (however you populate it) if the schedule is for Monday 8:00 - 9:30 it just creates one entry in the schedule entries table. If the schedule is for Monday - Wednesday 8:00 - 9:30 then it creates three entries in the schedule entries table.
Ideally you would store these as datetime fields (ie. the actual date of the Monday / Tuesday / Wednesday in question, so a schedule entry for today would have a start time of 2021-12-08 08:00:00 and an end time of 2021-12 09:30:00 but if these are teachers, then it may be that it is "every Monday at 08:00:00" in which case your schedule entries table would have one column for the day of the week (as an integer, so 1 for Monday, 2 for Tuesday, etc.), one column for start time and one column for end time.
As it stands, you're going to be doing a lot of juggling to get it to work as you envisage - the above approach would simplify it considerably.

Is there a way to make __timeShift only select certain days of the week?

The current function in use in the JMeter script is
${__timeShift(dd/MM/yyyy,${__time(dd/MM/yyyy)},-P31D,,)}
to specify a review date 31 days ago.
I now learn that the project requires the review date to always fall on a Monday. Is there any way to make sure that performing a timeShift only selects a Monday?
I'm afraid __timeShift() function is not flexible enough, you can consider using __groovy() function instead and implement the following algorithm:
Get current date
Subtract 31 day
If current day of week is Monday - return the date in the past
Otherwise add 1 day until the date in the past becomes Monday
Example function code:
${__groovy(def now = new Date(); def monthAgo = now.minus(31); while (monthAgo[Calendar.DAY_OF_WEEK] != Calendar.MONDAY) { monthAgo = monthAgo.plus(1)}; return monthAgo.format('dd/MM/yyyy'),)}
Demo:
More information: Apache Groovy - Why and How You Should Use It

Can't get monthy CarbonPeriod to behave as desired

In the project I'm working on I have a daily command that basically checks the date of the last record in the database and tries to fetch data from an API from the day after and then each month after that (the data is published monthly).
Basically, the last record's date is 2019-08-30. I'm mocking as if I were running the task on 2019-09-01 with
$test = Carbon::create(2019,9,1,4);
Carbon::setTestNow($test);
I then create a monthly period between the next day of the last record's date and the last day of the current month like so:
$period = CarbonPeriod::create($last_record_date->addDay(), '1 month', $last_day_of_current_month);
Successfully generating a period with start_date = 2019-08-31 and end_date = 2019-09-30. Which I use in a simple foreach.
What I expected to happen is that it runs twice, once for August and once for September, but it's running only once for the start date. It's probably adding a month and going past the end date, but I don't know how to force the behaviour I'm looking for.
TL;DR:
$period = CarbonPeriod::create('2019-08-31', '1 month', '2019-09-30');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
This will print just 2019-08, while I expect 2019-08 and 2019-09. What's the best way to achieve that?
Solution :-
You can store actual date in $actual_day and current date for occurring monthly in $current_day. Put a check on comparing both dates, if not matched then make it on the same day it will skip 30,31 case in case of February month.
$current_date = $current_date->addMonths(1);
if($current_date->day != $actual_day){
$date = Carbon::parse($date->year."-".$date->month."-".$actual_day);
}
Your start date is 2019-08-31. Adding a month takes you to 2019-09-31. 2019-09-31 doesn't exist so instead you get 2019-10-01, which is after your end date. To avoid this I'd suggest you use a more regular interval such as 30 days.
Otherwise you're going to have to rigorously define what you mean by "a month later". If the start date is 31st Jan is the next date 28th February? Is the month after 28th or 31st March? How do leap years affect things?

Get a Specific WeekDay date after a date

I am trying to implement a delivery process that occurs at specific days of week for each region. So, in a Region that delivers on Tuesdays and Thursdays I need to be able to get next eligible date based on the date the product will be available. So, if I will have it read on the 5th, O need to get the date for the next Tuesday or Thursday.
I started implementing it on Carbon, but I and creating a lot of loops through dates, testing dates and checking if its valid. something of getting product availability date and checking each day after it if its a monday Tuesday or Thursday ... etc ///I am sure, using Carbon, will have a better way to do it.
Any hint on how to do that ?
Thanks for any help!
Define a date:
$date = Carbon::now();
Now you have to get the day:
$day = $date->dayOfWeekIso
If $date is monday, then you will get a integer and that would be 1. That is because: 1 (monday), 2 (Tuesday), ..., 7 (sunday).
Now that you have this number you just need to apply some simple logic.
If the number you get is a 2 (Tuesday) then you will need to add two days to your $date in order to get the delivery date:
$delivery_date = $date->addDays(2);
If your day is equal 4 (Thursday), then you need to add 6 days to your $date so that would give you the next Tuesday:
$delivery_date = $date->addDays(6);
I think that's what you want! I hope it helps!

WebI Adding Weekend Values to Last Working Day in cross tab

Based on "Day" I should calculate the sales in Cross tab.
Saturday & Sunday values should be added only for Friday (Last working day). Below examples give an idea...(cond: if "DAY is Fri then add values for Fri+Sat+Sun)
Please assist. I'm struggling hard with the formulas in WebI
To give more detailed view..Actually i'm using Cross tab --> Day wise across organization.
Day----USA-----UK-----INDIA
Day----USA-----UK-----INDIA
THU-----23------ 12-----36
FRI------65------12------10
SAT------9--------16-----24
SUN------2--------24----56
FRI------3------ 10-----37
SAT-----29------ 06-----87
SUN-----03------04-----13
Result should be: DAY only Fri = (Fri + Sat + Sun) rest same values
Day----USA-----UK-----INDIA
THU-----23------ 12-----36
FRI------76------52------90
SAT------9--------16-----24
SUN------2--------24----56
FRI------3------ -20-----137
SAT-----29------ 06-----87
SUN-----03------04-----13
If you create a variable with the following formula, and replace the Day dimension in your current cross tab, you should get the output that you need:
=If [Day] InList ("SAT";"SUN") Then "FRI" Else [Day]
Basically, if the value for Day is SAT or SUN, it will return FRI, else it will return the original value. Web Intelligence should then automatically aggregate the values accordingly.

Resources