Skip to content

Commit

Permalink
* Added ScheduleMixin.to_unit that converts the given seconds
Browse files Browse the repository at this point in the history
… to the given ``unit`` in consideration of the given ``schedule_model``.
  • Loading branch information
eoyilmaz committed Sep 28, 2021
1 parent fc493fc commit a35c041
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -11,6 +11,9 @@ Stalker Changes
causes the attribute to be filled with parent data. This is a slight change,
but may break some workflows.

* Added ``ScheduleMixin.to_unit`` that converts the given ``seconds`` to the
given ``unit`` in consideration of the given ``schedule_model``.

0.2.26
======

Expand Down
2 changes: 1 addition & 1 deletion stalker/__init__.py
Expand Up @@ -11,7 +11,7 @@

__title__ = "stalker"
__description__ = 'A Production Asset Management (ProdAM) System'
__uri__ = 'http://github.com/eoyilmaz/stalker'
__uri__ = 'https://github.com/eoyilmaz/stalker'
__doc__ = "%s <%s>" % (__description__, __uri__)

__author__ = "Erkan Ozgur Yilmaz"
Expand Down
44 changes: 44 additions & 0 deletions stalker/models/mixins.py
Expand Up @@ -1329,6 +1329,50 @@ def to_seconds(cls, timing, unit, model):

return timing * lut[unit]

@classmethod
def to_unit(cls, seconds, unit, model):
"""converts the ``seconds`` value to the given ``unit``, depending on
to the ``schedule_model`` the value will differ. So if the
``schedule_model`` is 'effort' or 'length' then the ``seconds`` and
``schedule_unit`` values are interpreted as work time, if the
``schedule_model`` is 'duration' then the ``seconds`` and
``schedule_unit`` values are considered as calendar time.
:param int seconds: The seconds to convert
:param str unit: The unit value, one of 'min', 'h', 'd', 'w', 'm', 'y'
:param str model: The schedule model, one of 'effort', 'length' or
'duration'
"""
if not unit:
return None

lut = {
'min': 60,
'h': 3600,
'd': 86400,
'w': 604800,
'm': 2419200,
'y': 31536000
}

if model in ['effort', 'length']:
from stalker import defaults
day_wt = defaults.daily_working_hours * 3600
week_wt = defaults.weekly_working_days * day_wt
month_wt = 4 * week_wt
year_wt = int(defaults.yearly_working_days) * day_wt

lut = {
'min': 60,
'h': 3600,
'd': day_wt,
'w': week_wt,
'm': month_wt,
'y': year_wt
}

return seconds / lut[unit]

@property
def schedule_seconds(self):
"""Returns the schedule values as seconds, depending on to the
Expand Down
50 changes: 50 additions & 0 deletions tests/mixins/test_scheduleMixin.py
Expand Up @@ -458,6 +458,56 @@ def test_to_seconds_is_working_properly(self):
self.test_obj.schedule_model
)

def test_to_unit_is_working_properly(self):
"""testing if the to_unit method is working properly
"""
from stalker import defaults
defaults.daily_working_hours = 9
defaults.weekly_working_days = 5
defaults.weekly_working_hours = 45
defaults.yearly_working_days = 52.1428 * 5

test_values = [
# effort values
['effort', 1, 'min', 60],
['effort', 10, 'min', 600],
['effort', 20, 'min', 1200],
['effort', 1, 'h', 3600],
['effort', 1.01, 'h', 3636],
['effort', 2, 'h', 7200],
['effort', 1, 'd', 32400],
['effort', 1, 'w', 162000],
['effort', 1, 'm', 648000],
['effort', 1, 'y', 8424000],

# length values
['length', 1, 'min', 60],
['length', 540, 'min', 32400],
['length', 1, 'h', 3600],
['length', 1, 'd', 32400],
['length', 1, 'w', 162000],
['length', 1, 'm', 648000],
['length', 1, 'y', 8424000],

# duration values
['duration', 1, 'min', 60],
['duration', 60, 'min', 3600],
['duration', 1440, 'min', 86400],
['duration', 1, 'h', 3600],
['duration', 1.5, 'h', 5400],
['duration', 2, 'h', 7200],
['duration', 1, 'd', 86400],
['duration', 1, 'w', 604800],
['duration', 1, 'm', 2419200],
['duration', 1, 'y', 31536000]
]

for test_value in test_values:
schedule_model = test_value[0]
seconds = test_value[3]
schedule_unit = test_value[2]
assert test_value[1] == self.test_obj.to_unit(seconds, schedule_unit, schedule_model)

def test_schedule_seconds_is_working_properly(self):
"""testing if the schedule_seconds property is working properly
"""
Expand Down

0 comments on commit a35c041

Please sign in to comment.