Commit 0d869acd authored by LETORT Sebastien's avatar LETORT Sebastien

'from' default value change to a default period regarding step value.

parent 9f27c235
......@@ -8,7 +8,7 @@ Metrics API allows to get some metrics about some apps.
- a nemo user will have access to nothing.
"""
from datetime import date, datetime
from datetime import date, datetime, timedelta
import logging
from django.utils.timezone import make_aware
......@@ -29,23 +29,30 @@ DATE_IN_FMT = "%Y-%m-%d"
def str2date(date):
return datetime.strptime( date, DATE_IN_FMT )
DATE_MIN = make_aware(str2date('2013-01-01'))
# SLetort : I used this function to set the default
# because metrics function have all their params, possibly set to None
# and this because i don't want to use the 'request' param from the API
# because those functions will be called also by the interface.
def get_params(from_, to, step, d_defaults):
d_return = d_defaults.copy()
__DEFAULT_DELTA = {
'year' : timedelta(days=5*365),
'month': timedelta(days=2*365),
'day' : timedelta(days=30+31),
}
def get_params(from_, to, step):
"""get params, if needed, set default or format."""
if step is None:
step = 'month'
if None is not from_:
d_return['from'] = make_aware(str2date(from_))
if None is not to:
d_return['to'] = make_aware(str2date(to))
if None is not step:
d_return['step'] = step
if to is None:
to = timezone.now()
else:
to = make_aware(str2date(to))
return ( d_return[x] for x in ('from', 'to', 'step' ))
if from_ is None:
from_ = to - __DEFAULT_DELTA[step]
else:
from_ = make_aware(str2date(from_))
return from_, to, step
def __api_dict(from_, to, step, l_records ):
"""from_ and to are datetime.datetime object."""
......@@ -73,8 +80,7 @@ def nb_jobs_per_user(app_id, from_=None, to=None, step=None):
and 'data' which contains a list of dictionnaries {user:, uname:, time_period:, n:}
:rtype: dictionnary
"""
d_defaults = { 'from': DATE_MIN, 'to': timezone.now(), 'step': 'month' }
from_,to,step = get_params(from_, to, step, d_defaults)
from_,to,step = get_params(from_, to, step)
msg = "provided from '{}' to '{}', step of '{}'"\
.format(from_, to, step)
log.info(msg)
......@@ -84,7 +90,6 @@ def nb_jobs_per_user(app_id, from_=None, to=None, step=None):
.values('time_period', 'user', 'user__username') \
.annotate(n=Count('user'))
# SLETORT: There should be a "django" way of doing this !
for d_res in res:
d_res['uname'] = d_res.pop('user__username')
d_res['time_period'] = __fmt_date( d_res.pop('time_period') )
......@@ -108,8 +113,7 @@ def nb_jobs_per_state(app_id, from_=None, to=None, step=None):
and 'data' which contains a list of dictionnaries {result:, time_period:, n:}
:rtype: dictionnary
"""
d_defaults = { 'from': DATE_MIN, 'to': timezone.now(), 'step': 'month' }
from_,to,step = get_params(from_, to, step, d_defaults)
from_,to,step = get_params(from_, to, step)
msg = "provided from '{}' to '{}', step of '{}'"\
.format(from_, to, step)
log.info(msg)
......@@ -143,10 +147,8 @@ def nb_jobs_created(app_id, from_=None, to=None, step=None):
:rtype: dictionnary
"""
d_defaults = { 'from': DATE_MIN, 'to': timezone.now(), 'step': 'month' }
from_,to,step = get_params(from_, to, step, d_defaults)
msg = "provided from '{}' to '{}', step of '{}'"\
.format(from_, to, step)
from_,to,step = get_params(from_, to, step)
msg = "provided from '{}' to '{}', step of '{}'".format(from_, to, step)
log.info(msg)
res = Job.objects.filter(webapp=app_id, created_at__range=[from_,to]) \
......
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