Commit 4b3f05a9 authored by Sebastien Robin's avatar Sebastien Robin

- Alarm use mysql in order to store the next alarm date, this prevent the zodb to grow

  again and again
- the algorithm was very poor while calculatint the next alarm date,
  make it up to 1440 times faster
- modify a bit the test in order to take into account changes,
  thanks to changes, the test is going 4 to 5 times faster



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@11530 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 16345839
......@@ -94,10 +94,7 @@ class Alarm(Periodicity, XMLObject):
# Set the new date
LOG('activeSense, self.getPath()',0,self.getPath())
#self.setStartDate(DateTime())
#self.setStopDate(DateTime())
self.setNextAlarmDate()
self.reindexObject()
method_id = self.getActiveSenseMethodId()
if method_id is not None:
method = getattr(self.activate(),method_id)
......
......@@ -90,8 +90,14 @@ class Periodicity(Base):
# This is usefull to set the current date as parameter for
# unit testing, by default it should be now
current_date = DateTime()
if next_start_date is None:
next_start_date = current_date
if next_start_date > current_date:
return
else:
# Make sure the old date is not too far away
nb_days = int(current_date-next_start_date)
next_start_date = next_start_date + nb_days
def validateMinute(self,date, previous_date):
periodicity_minute_frequency = self.getPeriodicityMinuteFrequency()
......@@ -156,14 +162,26 @@ class Periodicity(Base):
previous_date = next_start_date
next_start_date = addToDate(next_start_date,minute=1)
while not(next_start_date >= current_date \
and validateMinute(self,next_start_date, previous_date) \
and validateHour(self,next_start_date) \
and validateDay(self,next_start_date) \
and validateWeek(self,next_start_date) \
and validateMonth(self,next_start_date)):
next_start_date = addToDate(next_start_date,minute=1)
self.setAlarmDate(next_start_date)
while 1:
validate_minute = validateMinute(self,next_start_date, previous_date)
validate_hour = validateHour(self,next_start_date)
validate_day = validateDay(self,next_start_date)
validate_week = validateWeek(self,next_start_date)
validate_month = validateMonth(self,next_start_date)
if (next_start_date >= current_date \
and validate_minute and validate_hour and validate_day \
and validate_week and validate_month):
break
else:
if not(validate_minute):
next_start_date = addToDate(next_start_date,minute=1)
else:
if not(validate_hour):
next_start_date = addToDate(next_start_date,hour=1)
else:
if not(validate_day and validate_week and validate_month):
next_start_date = addToDate(next_start_date,day=1)
self.Alarm_zUpdateAlarmDate(uid=self.getUid(),alarm_date=next_start_date)
security.declareProtected(Permissions.View, 'getAlarmDate')
......@@ -171,9 +189,16 @@ class Periodicity(Base):
"""
returns something like ['Sunday','Monday',...]
"""
alarm_date = self._baseGetAlarmDate()
if alarm_date is None:
alarm_date = self.getPeriodicityStartDate()
#alarm_date = self._baseGetAlarmDate()
#if alarm_date is None:
# alarm_date = self.getPeriodicityStartDate()
alarm_date=None
result_list = self.Alarm_zGetAlarmDate(uid=self.getUid())
if len(result_list)==1:
alarm_date = result_list[0].alarm_date
periodicity_start_date = self.getPeriodicityStartDate()
if alarm_date < periodicity_start_date:
alarm_date = periodicity_start_date
return alarm_date
......
......@@ -108,20 +108,11 @@ class AlarmTool(BaseTool):
"""
user = self.portal_catalog.getOwner()
newSecurityManager(self.REQUEST, user)
if to_active:
now = str(DateTime())
date_expression = '<= %s' % now
catalog_search = self.portal_catalog(portal_type = \
self.getPortalAlarmTypeList(), alarm_date = date_expression)
else:
catalog_search = self.portal_catalog(portal_type = \
self.getPortalAlarmTypeList())
alarm_list = map(lambda x:x.getObject(),catalog_search)
if to_active:
now = DateTime()
date_expression = '<= %s' % str(now)
catalog_search = self.portal_catalog(
portal_type = self.getPortalAlarmTypeList(), alarm_date=date_expression
portal_type = self.getPortalAlarmTypeList(),
alarm_date={'query':now,'range':'ngt'}
)
# check again the alarm date in case the alarm was not yet reindexed
alarm_list = [x.getObject() for x in catalog_search \
......@@ -130,7 +121,7 @@ class AlarmTool(BaseTool):
catalog_search = self.portal_catalog(
portal_type = self.getPortalAlarmTypeList()
)
alarm_list = map(lambda x:x.getObject(),catalog_search)
alarm_list = [x.getObject() for x in catalog_search]
return alarm_list
security.declareProtected(Permissions.ModifyPortalContent, 'tic')
......
......@@ -129,6 +129,8 @@ class TestAlarm(ERP5TypeTestCase):
ZopeTestCase._print('\n%s ' % message)
LOG('Testing... ',0,message)
alarm = self.newAlarm()
get_transaction().commit()
self.tic()
now = DateTime()
date = addToDate(now,day=1)
alarm.setPeriodicityStartDate(date)
......@@ -147,6 +149,8 @@ class TestAlarm(ERP5TypeTestCase):
date = addToDate(now,day=2)
alarm.setPeriodicityStartDate(date)
alarm.setPeriodicityHourFrequency(1)
get_transaction().commit()
self.tic()
alarm.setNextAlarmDate(current_date=now)
self.assertEquals(alarm.getAlarmDate(),date)
LOG(message + ' now :',0,now)
......@@ -173,6 +177,8 @@ class TestAlarm(ERP5TypeTestCase):
date = addToDate(now,day=2)
alarm.setPeriodicityStartDate(date)
alarm.setPeriodicityHourFrequency(3)
get_transaction().commit()
self.tic()
alarm.setNextAlarmDate(current_date=now)
self.assertEquals(alarm.getAlarmDate(),date)
LOG(message + ' now :',0,now)
......@@ -202,6 +208,8 @@ class TestAlarm(ERP5TypeTestCase):
hour_list = (6,10,15,21)
alarm.setPeriodicityStartDate(now)
alarm.setPeriodicityHourList(hour_list)
get_transaction().commit()
self.tic()
self.assertEquals(alarm.getAlarmDate(),right_first_date)
alarm.setNextAlarmDate(current_date=right_first_date)
self.assertEquals(alarm.getAlarmDate(),right_second_date)
......@@ -225,6 +233,8 @@ class TestAlarm(ERP5TypeTestCase):
alarm.setPeriodicityStartDate(now)
alarm.setPeriodicityDayFrequency(1)
alarm.setPeriodicityHourList((10,))
get_transaction().commit()
self.tic()
self.assertEquals(alarm.getAlarmDate(),right_first_date)
alarm.setNextAlarmDate(current_date=right_first_date)
self.assertEquals(alarm.getAlarmDate(),right_second_date)
......@@ -247,6 +257,8 @@ class TestAlarm(ERP5TypeTestCase):
alarm.setPeriodicityStartDate(right_first_date)
alarm.setPeriodicityDayFrequency(3)
alarm.setPeriodicityHourList((14,15,17))
get_transaction().commit()
self.tic()
self.assertEquals(alarm.getAlarmDate(),right_first_date)
alarm.setNextAlarmDate(current_date=right_first_date)
self.assertEquals(alarm.getAlarmDate(),right_second_date)
......@@ -268,6 +280,8 @@ class TestAlarm(ERP5TypeTestCase):
right_third_date = DateTime('%i/%i/%i %i:%i:%d' % (2004,10,1,6,00,00))
right_fourth_date = DateTime('%i/%i/%i %i:%i:%d' % (2004,10,1,15,00,00))
alarm = self.newAlarm()
get_transaction().commit()
self.tic()
alarm.setPeriodicityStartDate(right_first_date)
alarm.setPeriodicityWeekDayList(('Monday','Friday'))
alarm.setPeriodicityHourList((6,15))
......@@ -307,6 +321,8 @@ class TestAlarm(ERP5TypeTestCase):
alarm.setPeriodicityStartDate(right_first_date)
alarm.setPeriodicityMonthDayList((1,15))
alarm.setPeriodicityHourList((12,14))
get_transaction().commit()
self.tic()
self.checkDate(alarm, right_first_date, right_second_date, right_third_date, right_fourth_date)
def test_10_OnceEvery2Month(self, quiet=0, run=run_all_test):
......@@ -325,6 +341,8 @@ class TestAlarm(ERP5TypeTestCase):
alarm.setPeriodicityMonthDayList((1,))
alarm.setPeriodicityMonthFrequency(2)
alarm.setPeriodicityHourList((6,))
get_transaction().commit()
self.tic()
self.checkDate(alarm, right_first_date, right_second_date, right_third_date)
def test_11_EveryDayOnceWeek41And42(self, quiet=0, run=run_all_test):
......@@ -342,6 +360,8 @@ class TestAlarm(ERP5TypeTestCase):
alarm.setPeriodicityStartDate(right_first_date)
alarm.setPeriodicityHourList((6,))
alarm.setPeriodicityWeekList((41,43))
get_transaction().commit()
self.tic()
self.checkDate(alarm, right_first_date, right_second_date, right_third_date,right_fourth_date)
def test_12_Every5Minutes(self, quiet=0, run=run_all_test):
......@@ -357,6 +377,8 @@ class TestAlarm(ERP5TypeTestCase):
date = addToDate(now,day=2)
alarm.setPeriodicityStartDate(date)
alarm.setPeriodicityMinuteFrequency(5)
get_transaction().commit()
self.tic()
alarm.setNextAlarmDate(current_date=now)
self.assertEquals(alarm.getAlarmDate(),date)
LOG(message + ' now :',0,now)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment