I want to make Action Scheduler to run in every 30 seconds, but it only has 1 minute as the lowest possible selection for its interval.
How can I customize/change it so that it can run in seconds?
This is what I end up doing, which I need to test it for a few days before confirming if it gets the job done.
Inherited ir.cron model to add seconds selection.
Added seconds to _intervalTypes to make it compatible with seconds selection.
Copied the original _process_job to replace in here so that the modified _intervalTypes with seconds property exists for the method.
Installed this customized module.
Here is the code.
from odoo import models, fields, api
from datetime import datetime
from dateutil.relativedelta import relativedelta
import pytz
_intervalTypes = {
'days': lambda interval: relativedelta(days=interval),
'hours': lambda interval: relativedelta(hours=interval),
'weeks': lambda interval: relativedelta(days=7*interval),
'months': lambda interval: relativedelta(months=interval),
'minutes': lambda interval: relativedelta(minutes=interval),
'seconds': lambda interval: relativedelta(seconds=interval),
}
class IrCronInherit(models.Model):
_inherit = 'ir.cron'
interval_type = fields.Selection([
('seconds', 'Seconds'),
('minutes', 'Minutes'),
('hours', 'Hours'),
('days', 'Days'),
('weeks', 'Weeks'),
('months', 'Months')], string='Interval Unit', default='months')
#classmethod
def _process_job(cls, job_cr, job, cron_cr):
""" Run a given job taking care of the repetition.
:param job_cr: cursor to use to execute the job, safe to commit/rollback
:param job: job to be run (as a dictionary).
:param cron_cr: cursor holding lock on the cron job row, to use to update the next exec date,
must not be committed/rolled back!
"""
with api.Environment.manage():
try:
cron = api.Environment(job_cr, job['user_id'], {
'lastcall': fields.Datetime.from_string(job['lastcall'])
})[cls._name]
# Use the user's timezone to compare and compute datetimes,
# otherwise unexpected results may appear. For instance, adding
# 1 month in UTC to July 1st at midnight in GMT+2 gives July 30
# instead of August 1st!
now = fields.Datetime.context_timestamp(cron, datetime.now())
nextcall = fields.Datetime.context_timestamp(cron, fields.Datetime.from_string(job['nextcall']))
numbercall = job['numbercall']
ok = False
while nextcall < now and numbercall:
if numbercall > 0:
numbercall -= 1
if not ok or job['doall']:
cron._callback(job['cron_name'], job['ir_actions_server_id'], job['id'])
if numbercall:
nextcall += _intervalTypes[job['interval_type']](job['interval_number'])
ok = True
addsql = ''
if not numbercall:
addsql = ', active=False'
cron_cr.execute("UPDATE ir_cron SET nextcall=%s, numbercall=%s, lastcall=%s"+addsql+" WHERE id=%s",(
fields.Datetime.to_string(nextcall.astimezone(pytz.UTC)),
numbercall,
fields.Datetime.to_string(now.astimezone(pytz.UTC)),
job['id']
))
cron.flush()
cron.invalidate_cache()
finally:
job_cr.commit()
cron_cr.commit()
Right now, it runs under a minute but not constantly at 30 seconds when I actually set it to 30 seconds, but it will do for now.
Related
I am looking at file delivery times and can't work out how to compare two timedelta fields using a for loop if statement.
time_diff is the difference between cob_date and last_update_time
average_diff is based on the average for a particular file
I want to find the delay for each row.
I have been able to produce a column delay using average_diff - time_diff
However, when the average_diff - time_diff < 0 I just want to return delay = 0 as this is not a delay.
I have made a for loop but this isn't working and I don't know why. I'm sure the answer is very simple but I can't get there.
test_pv_import_v2['delay2'] = pd.to_timedelta('0')
for index, row in test_pv_import_v2.iterrows():
if test_pv_import_v2['time_diff'] > test_pv_import_v2['average_diff'] :
test_pv_import_v2['delay2'] = test_pv_import_v2['time_diff'] - test_pv_import_v2['average_diff']
Use Series.where for set 0 Timedelta by condition:
mask = test_pv_import_v2['time_diff'] > test_pv_import_v2['average_diff']
s = (test_pv_import_v2['time_diff'] - test_pv_import_v2['average_diff'])
test_pv_import_v2['delay2'] = s.where(mask, pd.to_timedelta('0'))
I need to write a function below that can compute the moving average of time series using a sliding window over an array. This function should take an array of date strings (say arr_date), an array of numbers (say arr_record), and a sliding window (default value 50). It should:
Return a list of dictionaries for all windows.
Each dictionary should include the date, average value, min, max, standard deviation at each window.
Able to handle missing data in time series by replacing missing data with the most recent available data.
(b) Download SPY daily data (Dec. 31, 2017 to Dec. 31, 2018) from Yahoo! as your test data in a .csv file. Read reading .csv file example and write a test programming for calling your function.
Does anyone have any thoughts? Extremely new to python and struggling.
So something following this logic should probably be a good starting point. Hope this is a helpful start, and welcome to the cs community.
def sliding_window( dates, numbers, sliding_window_value):
# list of dictionaries
return_dicts =[{}]
# if window size is greater than length of dates, there's only one window
if sliding_window_value >= len(dates):
return_dicts += [create_window(dates, numbers)]
return return_dicts
# gather all our windows into one list
for i in range (0, len(dates) - sliding_window_value ):
# get our window subsets
dates_subset = dates[i:(sliding_window_value+1)]
numbers_subset = numbers[i:(sliding_window_value+1)]
# get our window stats dictionary
window_stats = create_window(dates_subset,numbers_subset)
# add these stats to our return list
return_dicts += [window_stats]
return return_dicts
def create_window(dates_subset, numbers_subset):
window_min = 1000000 # some high minimum to start
window_max = -1000000 # some low maximuim to start
window_total = 0
for i in range ( 0, len(dates_subset)):
# calculate total
window_total += numbers_subset[i]
# calculate max
if numbers_subset[i] > window_max:
window_max = numbers_subset[i]
# calculate min
if numbers_subset[i] < window_min:
window_min = numbers_subset[i]
# other calculations....
return_dict = {
"min" : window_min,
"max" : window_max,
"average" : window_total / len(dates_subset),
# other calculations....
}
return return_dict
Good luck bud, the work is worth it.
How calculate hours in my example:
((FINISH - START) - PAUSE)
format in form view
start = 02/10/2017 07:55:54
finish = 02/10/2017 16:50:54
pause = 1 hour (default)
in console datetime look like this: 2017-02-10 07:55:54
Need this ==> result = 07:55
How in #api.depends('start', 'finish')
def calculate_hours(self):
#
You could use timedelta or relative delta to calculate the difference. Before that, you have to convert to datetime object. Here's how I would do
from openerp import api, fields
# if use relativedelta
from dateutil.relativedelta import relativedelta
#api.depends('start', 'finish')
def calculate_hours(self):
for record in self:
start_dt = fields.Datetime.from_string(record.start)
finish_dt = fields.Datetime.from_string(record.finish)
# using relativedelta
# pause time = 1, you could change to the number you desire.
pause = relativedelta(hours=1)
difference = relativedelta(finish_dt, start_dt) - pause
# from relative delta you could get hours or minutes as you wanted
# hours = difference.hours
# minutes = difference.minutes
# using timedelta
# pause = timedelta(seconds=3600)
# difference = finish_dt - start_dt - pause
# first element of timedelta is days, if you want to calculate the difference of days in hours, add difference.days * 24
# hours = int(difference.seconds/3600)
# minutes = (difference.seconds % 3600) / 60
# your code ....
Hope my answer meets your requirements.
I'm trying to update the orientation of a gratingStim every 100 ms or so in the psychopy coder. Currently, I'm updating the attribute (or trying to) with these lines :
orientationArray = orientation.split(',') #reading csv line as a list
selectOri = 0 #my tool to select the searched value in the list
gabor.ori = int(orientationArray[selectOri]) #select value as function of the "selectOri", in this case always the first one
continueroutine = True
while continueroutine:
if timer == 0.1: # This doesn't work but it shows you what is planned
selectOri = selectOri + 1 #update value
gabor.ori = int(orientationArray[selectOri]) #update value
win.flip()
I can't find a proper way to update in a desired time frame.
A neat way to do something every x frames is to use the modulo operation in combination with a loop containin win.flip(). So if you want to do something every 6 frames (100 ms on a 60 Hz monitor), just do this in every frame:
frame = 0 # the current frame number
while continueroutine:
if frame % 6 == 0: # % is modulo. Here every sixth frame
gabor.ori = int(orientationArray[selectOri + 1])
# Run this every iteration to synchronize the while-loop with the monitor's frames.
gabor.draw()
win.flip()
frame += 1
Currently I am getting time with the keyword Get time epoch , which is returning time in seconds. But I need time in milliseconds , So that I can get time span for a particular event.
or is there any other way to get the time span for a particular event or a testsceanrio?
Check the new test library DateTime, which contains keyword Get Current Date, which also returns milliseconds. It also has keyword Subtract Dates to calculate difference between two timestamps.
One of the more powerful features of robot is that you can directly call python code from a test script using the Evaluate keyword. For example, you can call the time.time() function, and do a little math:
*** Test cases
| Example getting the time in milliseconds
| | ${ms}= | Evaluate | int(round(time.time() * 1000)) | time
| | log | time in ms: ${ms}
Note that even though time.time returns a floating point value, not all systems will return a value more precise than one second.
Using the DateTime library, as suggested by janne:
*** Settings ***
Library DateTime
*** Test Cases ***
Performance Test
${timeAvgMs} = Test wall clock time 100 MyKeywordToPerformanceTest and optional arguments
Should be true ${timeAvgMs} < 50
*** Keywords ***
MyKeywordToPerformanceTest
# Do something here
Test wall clock time
[Arguments] ${iterations} #{commandAndArgs}
${timeBefore} = Get Current Date
:FOR ${it} IN RANGE ${iterations}
\ #{commandAndArgs}
${timeAfter} = Get Current Date
${timeTotalMs} = Subtract Date From Date ${timeAfter} ${timeBefore} result_format=number
${timeAvgMs} = Evaluate int(${timeTotalMs} / ${iterations} * 1000)
Return from keyword ${timeAvgMs}
In the report, for each suite, test and keyword, you have the information about start, end and length with millisecond details. Something like:
Start / End / Elapsed: 20140602 10:57:15.948 / 20140602 10:57:16.985 / 00:00:01.037
I don't see a way to do it using Builtin, look:
def get_time(format='timestamp', time_=None):
"""Return the given or current time in requested format.
If time is not given, current time is used. How time is returned is
is deternined based on the given 'format' string as follows. Note that all
checks are case insensitive.
- If 'format' contains word 'epoch' the time is returned in seconds after
the unix epoch.
- If 'format' contains any of the words 'year', 'month', 'day', 'hour',
'min' or 'sec' only selected parts are returned. The order of the returned
parts is always the one in previous sentence and order of words in
'format' is not significant. Parts are returned as zero padded strings
(e.g. May -> '05').
- Otherwise (and by default) the time is returned as a timestamp string in
format '2006-02-24 15:08:31'
"""
time_ = int(time_ or time.time())
format = format.lower()
# 1) Return time in seconds since epoc
if 'epoch' in format:
return time_
timetuple = time.localtime(time_)
parts = []
for i, match in enumerate('year month day hour min sec'.split()):
if match in format:
parts.append('%.2d' % timetuple[i])
# 2) Return time as timestamp
if not parts:
return format_time(timetuple, daysep='-')
# Return requested parts of the time
elif len(parts) == 1:
return parts[0]
else:
return parts
You have to write your own module, you need something like:
import time
def get_time_in_millies():
time_millies = lambda: int(round(time.time() * 1000))
return time_millies
Then import this library in Ride for the suite and you can use the method name like keyword, in my case it would be Get Time In Millies. More info here.