Bugzilla – Attachment 105967 Details for
Bug 25723
Improve efficiency of holiday calculation
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 25723: Use the same code for single and exception holidays
Bug-25723-Use-the-same-code-for-single-and-excepti.patch (text/plain), 5.82 KB, created by
Martin Renvoize (ashimema)
on 2020-06-17 13:07:40 UTC
(
hide
)
Description:
Bug 25723: Use the same code for single and exception holidays
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2020-06-17 13:07:40 UTC
Size:
5.82 KB
patch
obsolete
>From 9f9243702221b50492ed1e00460f122acd7d1163 Mon Sep 17 00:00:00 2001 >From: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Date: Wed, 17 Jun 2020 11:14:30 +0100 >Subject: [PATCH] Bug 25723: Use the same code for single and exception > holidays > >This patch refactors is_exception_holiday and is_single_holiday to >utilise a single _holidays method which combines the logic of the >previous single_holidays and exception_holidays methods. > >As Koha::Calendar is instantiated at a branch level, we also move split >the cache into branches too. >--- > Koha/Calendar.pm | 119 +++++++++-------------------------------------- > 1 file changed, 21 insertions(+), 98 deletions(-) > >diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm >index f15f804e8c..8933efb2f1 100644 >--- a/Koha/Calendar.pm >+++ b/Koha/Calendar.pm >@@ -51,95 +51,45 @@ sub _init { > return; > } > >-sub exception_holidays { >+sub _holidays { > my ($self) = @_; > >- my $cache = Koha::Caches->get_instance(); >- my $exception_holidays = $cache->get_from_cache('exception_holidays'); >+ my $key = $self->{branchcode} . "_holidays"; >+ my $cache = Koha::Caches->get_instance(); >+ my $holidays = $cache->get_from_cache($key); > >- # Populate the cache is necessary >- unless ($exception_holidays) { >- my $dbh = C4::Context->dbh; >- $exception_holidays = {}; >- >- # Push holidays for each branch >- my $exception_holidays_sth = $dbh->prepare( >-'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 1' >- ); >- $exception_holidays_sth->execute(); >- my $dates = []; >- while ( my ( $day, $month, $year, $branch ) = >- $exception_holidays_sth->fetchrow ) >- { >- my $datestring = >- sprintf( "%04d", $year ) >- . sprintf( "%02d", $month ) >- . sprintf( "%02d", $day ); >- >- $exception_holidays->{$branch}->{$datestring} = 1; >- } >- $cache->set_in_cache( 'exception_holidays', $exception_holidays, >- { expiry => 76800 } ); >- } >- >- return $exception_holidays->{ $self->{branchcode} } // {}; >-} >- >-sub is_exception_holiday { >- my ( $self, $date ) = @_; >- >- return 1 if ( $self->exception_holidays->{$date} ); >- return 0; >-} >- >-sub single_holidays { >- my ($self) = @_; >- >- my $cache = Koha::Caches->get_instance(); >- my $single_holidays = $cache->get_from_cache('single_holidays'); >- >- # $single_holidays looks like: >+ # $holidays looks like: > # { >- # CPL => [ >- # [0] 20131122, >- # ... >- # ], >- # ... >+ # 20131122 => 1, # single_holiday >+ # 20131123 => 0, # exception_holiday >+ # ... > # } > > # Populate the cache if necessary >- unless ($single_holidays) { >+ unless ($holidays) { > my $dbh = C4::Context->dbh; >- $single_holidays = {}; >+ $holidays = {}; > >- # Push holidays for each branch >- my $single_holidays_sth = $dbh->prepare( >-'SELECT day, month, year, branchcode FROM special_holidays WHERE isexception = 0' >+ # Add holidays for each branch >+ my $holidays_sth = $dbh->prepare( >+'SELECT day, month, year, isexception FROM special_holidays WHERE branchcode = ?' > ); >- $single_holidays_sth->execute(); >+ $holidays_sth->execute($self->{branchcode}); > >- while ( my ( $day, $month, $year, $branch ) = >- $single_holidays_sth->fetchrow ) >+ while ( my ( $day, $month, $year, $exception ) = >+ $holidays_sth->fetchrow ) > { > my $datestring = > sprintf( "%04d", $year ) > . sprintf( "%02d", $month ) > . sprintf( "%02d", $day ); > >- $single_holidays->{$branch}->{$datestring} = 1; >+ $holidays->{$datestring} = !$exception; > } >- $cache->set_in_cache( 'single_holidays', $single_holidays, >- { expiry => 76800 } ); >+ $cache->set_in_cache( $key, $holidays, { expiry => 76800 } ); > } > >- return $single_holidays->{ $self->{branchcode} } // {}; >-} >- >-sub is_single_holiday { >- my ( $self, $date ) = @_; >- >- return 1 if ( $self->single_holidays->{$date} ); >- return 0; >+ return $holidays // {}; > } > > sub addDate { >@@ -258,17 +208,13 @@ sub is_holiday { > my $localdt = $dt->clone(); > my $day = $localdt->day; > my $month = $localdt->month; >- my $ymd = $localdt->ymd('') ; >+ my $ymd = $localdt->ymd(''); > > #Change timezone to "floating" before doing any calculations or comparisons > $localdt->set_time_zone("floating"); > $localdt->truncate( to => 'day' ); > >- >- if ( $self->is_exception_holiday( $ymd ) == 1 ) { >- # exceptions are not holidays >- return 0; >- } >+ return $self->_holidays->{$ymd} if defined($self->_holidays->{$ymd}); > > my $dow = $localdt->day_of_week; > # Representation fix >@@ -286,10 +232,6 @@ sub is_holiday { > return 1; > } > >- if ($self->is_single_holiday( $ymd ) == 1 ) { >- return 1; >- } >- > # damn have to go to work after all > return 0; > } >@@ -485,25 +427,6 @@ C<$unit> is a string value 'days' or 'hours' toflag granularity of duration > Currently unit is only used to invoke Staffs return Monday at 10 am rule this > parameter will be removed when issuingrules properly cope with that > >- >-=head2 single_holidays >- >-my $rc = $self->single_holidays( $ymd ); >- >-Passed a $date in Ymd (yyyymmdd) format - returns 1 if date is a single_holiday, or 0 if not. >- >-=head2 exception_holidays >- >-my $exceptions = $self->exception_holidays; >- >-Returns a hashref of exception holidays for the branch >- >-=head2 is_exception_holiday >- >-my $rc = $self->is_exception_holiday( $ymd ); >- >-Passed a $date in Ymd (yyyymmdd) format - returns 1 if the date is an exception_holiday, or 0 if not. >- > =head2 is_holiday > > $yesno = $calendar->is_holiday($dt); >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 25723
:
105789
|
105792
|
105793
|
105830
|
105831
|
105832
|
105833
|
105834
|
105835
|
105836
|
105936
|
105937
|
105939
|
105941
|
105947
|
105954
|
105955
|
105956
|
105957
|
105958
|
105959
|
105960
|
105961
|
105962
|
105963
|
105964
|
105965
|
105966
|
105967
|
105968
|
105969
|
105970
|
106991
|
106992
|
106993
|
106994
|
106995
|
106996
|
106997
|
106998
|
106999
|
107000
|
107001
|
107002
|
107003
|
107004
|
107005
|
107006
|
107007
|
107008
|
107009
|
107010
|
107013
|
107014
|
107015
|
107016
|
107017
|
107018
|
107019
|
107020
|
107021
|
107022
|
107023
|
107025