Merge "Remove oslo.utils dependency"
This commit is contained in:
commit
176c1332f6
@ -16,7 +16,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
import numpy
|
import numpy
|
||||||
from oslo_utils import timeutils
|
|
||||||
import pandas
|
import pandas
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@ -80,8 +79,8 @@ class MovingAverage(aggregates.CustomAggregator):
|
|||||||
msec = datetime.timedelta(milliseconds=1)
|
msec = datetime.timedelta(milliseconds=1)
|
||||||
zero = datetime.timedelta(seconds=0)
|
zero = datetime.timedelta(seconds=0)
|
||||||
half_span = datetime.timedelta(seconds=window / 2)
|
half_span = datetime.timedelta(seconds=window / 2)
|
||||||
start = timeutils.normalize_time(data.index[0])
|
start = utils.normalize_time(data.index[0])
|
||||||
stop = timeutils.normalize_time(
|
stop = utils.normalize_time(
|
||||||
data.index[-1] + datetime.timedelta(seconds=min_grain))
|
data.index[-1] + datetime.timedelta(seconds=min_grain))
|
||||||
# min_grain addition necessary since each bin of rolled-up data
|
# min_grain addition necessary since each bin of rolled-up data
|
||||||
# is indexed by leftmost timestamp of bin.
|
# is indexed by leftmost timestamp of bin.
|
||||||
@ -90,7 +89,7 @@ class MovingAverage(aggregates.CustomAggregator):
|
|||||||
right = 2 * half_span - left - msec
|
right = 2 * half_span - left - msec
|
||||||
# msec subtraction is so we don't include right endpoint in slice.
|
# msec subtraction is so we don't include right endpoint in slice.
|
||||||
|
|
||||||
x = timeutils.normalize_time(x)
|
x = utils.normalize_time(x)
|
||||||
|
|
||||||
if x - left >= start and x + right <= stop:
|
if x - left >= start and x + right <= stop:
|
||||||
dslice = data[x - left: x + right]
|
dslice = data[x - left: x + right]
|
||||||
|
@ -22,7 +22,6 @@ from cotyledon import oslo_config_glue
|
|||||||
from futurist import periodics
|
from futurist import periodics
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
from oslo_utils import timeutils
|
|
||||||
import six
|
import six
|
||||||
import tenacity
|
import tenacity
|
||||||
import tooz
|
import tooz
|
||||||
@ -120,10 +119,9 @@ class MetricProcessBase(cotyledon.Service):
|
|||||||
time.sleep(self.startup_delay)
|
time.sleep(self.startup_delay)
|
||||||
|
|
||||||
while not self._shutdown.is_set():
|
while not self._shutdown.is_set():
|
||||||
with timeutils.StopWatch() as timer:
|
with utils.StopWatch() as timer:
|
||||||
self._run_job()
|
self._run_job()
|
||||||
self._shutdown.wait(max(0, self.interval_delay -
|
self._shutdown.wait(max(0, self.interval_delay - timer.elapsed()))
|
||||||
timer.elapsed()))
|
|
||||||
self._shutdown_done.set()
|
self._shutdown_done.set()
|
||||||
|
|
||||||
def terminate(self):
|
def terminate(self):
|
||||||
|
@ -21,7 +21,6 @@ import decimal
|
|||||||
|
|
||||||
import iso8601
|
import iso8601
|
||||||
from oslo_db.sqlalchemy import models
|
from oslo_db.sqlalchemy import models
|
||||||
from oslo_utils import timeutils
|
|
||||||
import six
|
import six
|
||||||
import sqlalchemy
|
import sqlalchemy
|
||||||
from sqlalchemy.dialects import mysql
|
from sqlalchemy.dialects import mysql
|
||||||
@ -91,7 +90,7 @@ class PreciseTimestamp(types.TypeDecorator):
|
|||||||
|
|
||||||
def process_bind_param(self, value, dialect):
|
def process_bind_param(self, value, dialect):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
value = timeutils.normalize_time(value)
|
value = utils.normalize_time(value)
|
||||||
if dialect.name == 'mysql':
|
if dialect.name == 'mysql':
|
||||||
return self._dt_to_decimal(value)
|
return self._dt_to_decimal(value)
|
||||||
return value
|
return value
|
||||||
@ -100,7 +99,7 @@ class PreciseTimestamp(types.TypeDecorator):
|
|||||||
if dialect.name == 'mysql':
|
if dialect.name == 'mysql':
|
||||||
value = self._decimal_to_dt(value)
|
value = self._decimal_to_dt(value)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
return timeutils.normalize_time(value).replace(
|
return utils.normalize_time(value).replace(
|
||||||
tzinfo=iso8601.iso8601.UTC)
|
tzinfo=iso8601.iso8601.UTC)
|
||||||
|
|
||||||
|
|
||||||
@ -116,7 +115,7 @@ class TimestampUTC(types.TypeDecorator):
|
|||||||
|
|
||||||
def process_bind_param(self, value, dialect):
|
def process_bind_param(self, value, dialect):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
return timeutils.normalize_time(value)
|
return utils.normalize_time(value)
|
||||||
|
|
||||||
def process_result_value(self, value, dialect):
|
def process_result_value(self, value, dialect):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
|
@ -23,7 +23,6 @@ from concurrent import futures
|
|||||||
import iso8601
|
import iso8601
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
from oslo_utils import timeutils
|
|
||||||
import six
|
import six
|
||||||
import six.moves
|
import six.moves
|
||||||
|
|
||||||
@ -91,7 +90,7 @@ class CarbonaraBasedStorage(storage.StorageDriver):
|
|||||||
be retrieved, returns None.
|
be retrieved, returns None.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
with timeutils.StopWatch() as sw:
|
with utils.StopWatch() as sw:
|
||||||
raw_measures = (
|
raw_measures = (
|
||||||
self._get_unaggregated_timeserie(
|
self._get_unaggregated_timeserie(
|
||||||
metric)
|
metric)
|
||||||
@ -447,14 +446,14 @@ class CarbonaraBasedStorage(storage.StorageDriver):
|
|||||||
new_first_block_timestamp)
|
new_first_block_timestamp)
|
||||||
for aggregation in agg_methods))
|
for aggregation in agg_methods))
|
||||||
|
|
||||||
with timeutils.StopWatch() as sw:
|
with utils.StopWatch() as sw:
|
||||||
ts.set_values(measures,
|
ts.set_values(measures,
|
||||||
before_truncate_callback=_map_add_measures,
|
before_truncate_callback=_map_add_measures,
|
||||||
ignore_too_old_timestamps=True)
|
ignore_too_old_timestamps=True)
|
||||||
|
|
||||||
elapsed = sw.elapsed()
|
|
||||||
number_of_operations = (len(agg_methods) * len(definition))
|
number_of_operations = (len(agg_methods) * len(definition))
|
||||||
perf = ""
|
perf = ""
|
||||||
|
elapsed = sw.elapsed()
|
||||||
if elapsed > 0:
|
if elapsed > 0:
|
||||||
perf = " (%d points/s, %d measures/s)" % (
|
perf = " (%d points/s, %d measures/s)" % (
|
||||||
((number_of_operations * computed_points['number']) /
|
((number_of_operations * computed_points['number']) /
|
||||||
|
@ -18,7 +18,7 @@ import functools
|
|||||||
import math
|
import math
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
from oslo_utils import timeutils
|
import iso8601
|
||||||
from oslotest import base
|
from oslotest import base
|
||||||
import pandas
|
import pandas
|
||||||
import six
|
import six
|
||||||
@ -138,13 +138,13 @@ class TestAggregatedTimeSerie(base.BaseTestCase):
|
|||||||
[(datetime.datetime(2014, 1, 1, 12, 0, 4), 1, 5),
|
[(datetime.datetime(2014, 1, 1, 12, 0, 4), 1, 5),
|
||||||
(datetime.datetime(2014, 1, 1, 12, 0, 9), 1, 6)],
|
(datetime.datetime(2014, 1, 1, 12, 0, 9), 1, 6)],
|
||||||
ts.fetch(
|
ts.fetch(
|
||||||
from_timestamp=timeutils.parse_isotime(
|
from_timestamp=iso8601.parse_date(
|
||||||
"2014-01-01 12:00:04")))
|
"2014-01-01 12:00:04")))
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
[(datetime.datetime(2014, 1, 1, 12, 0, 4), 1, 5),
|
[(datetime.datetime(2014, 1, 1, 12, 0, 4), 1, 5),
|
||||||
(datetime.datetime(2014, 1, 1, 12, 0, 9), 1, 6)],
|
(datetime.datetime(2014, 1, 1, 12, 0, 9), 1, 6)],
|
||||||
ts.fetch(
|
ts.fetch(
|
||||||
from_timestamp=timeutils.parse_isotime(
|
from_timestamp=iso8601.parse_date(
|
||||||
"2014-01-01 13:00:04+01:00")))
|
"2014-01-01 13:00:04+01:00")))
|
||||||
|
|
||||||
def test_before_epoch(self):
|
def test_before_epoch(self):
|
||||||
|
@ -23,9 +23,9 @@ import hashlib
|
|||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import iso8601
|
||||||
from keystonemiddleware import fixture as ksm_fixture
|
from keystonemiddleware import fixture as ksm_fixture
|
||||||
import mock
|
import mock
|
||||||
from oslo_utils import timeutils
|
|
||||||
import six
|
import six
|
||||||
from stevedore import extension
|
from stevedore import extension
|
||||||
import testscenarios
|
import testscenarios
|
||||||
@ -417,7 +417,7 @@ class MetricTest(RestTest):
|
|||||||
self.app.get("/v1/metric/%s/measures" % metric['id'],
|
self.app.get("/v1/metric/%s/measures" % metric['id'],
|
||||||
status=403)
|
status=403)
|
||||||
|
|
||||||
@mock.patch.object(timeutils, 'utcnow')
|
@mock.patch.object(utils, 'utcnow')
|
||||||
def test_get_measure_start_relative(self, utcnow):
|
def test_get_measure_start_relative(self, utcnow):
|
||||||
"""Make sure the timestamps can be relative to now."""
|
"""Make sure the timestamps can be relative to now."""
|
||||||
utcnow.return_value = datetime.datetime(2014, 1, 1, 10, 23)
|
utcnow.return_value = datetime.datetime(2014, 1, 1, 10, 23)
|
||||||
@ -732,7 +732,7 @@ class ResourceTest(RestTest):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _strtime_to_httpdate(dt):
|
def _strtime_to_httpdate(dt):
|
||||||
return email_utils.formatdate(calendar.timegm(
|
return email_utils.formatdate(calendar.timegm(
|
||||||
timeutils.parse_isotime(dt).timetuple()), usegmt=True)
|
iso8601.parse_date(dt).timetuple()), usegmt=True)
|
||||||
|
|
||||||
def _check_etag(self, response, resource):
|
def _check_etag(self, response, resource):
|
||||||
lastmodified = self._strtime_to_httpdate(resource['revision_start'])
|
lastmodified = self._strtime_to_httpdate(resource['revision_start'])
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import iso8601
|
||||||
import mock
|
import mock
|
||||||
from oslo_utils import timeutils
|
|
||||||
from oslotest import base
|
from oslotest import base
|
||||||
import six.moves
|
import six.moves
|
||||||
|
|
||||||
@ -649,7 +649,7 @@ class TestStorageDriver(tests_base.TestCase):
|
|||||||
(utils.datetime_utc(2014, 1, 1, 12), 300.0, 69.0),
|
(utils.datetime_utc(2014, 1, 1, 12), 300.0, 69.0),
|
||||||
], self.storage.get_measures(
|
], self.storage.get_measures(
|
||||||
self.metric,
|
self.metric,
|
||||||
from_timestamp=timeutils.parse_isotime("2014-1-1 13:00:00+01:00"),
|
from_timestamp=iso8601.parse_date("2014-1-1 13:00:00+01:00"),
|
||||||
to_timestamp=datetime.datetime(2014, 1, 1, 12, 0, 2)))
|
to_timestamp=datetime.datetime(2014, 1, 1, 12, 0, 2)))
|
||||||
|
|
||||||
self.assertEqual([
|
self.assertEqual([
|
||||||
|
@ -76,3 +76,30 @@ class TestResourceUUID(tests_base.TestCase):
|
|||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
uuid.UUID('853e5c64-f45e-58b2-999c-96df856fbe3d'),
|
uuid.UUID('853e5c64-f45e-58b2-999c-96df856fbe3d'),
|
||||||
utils.ResourceUUID("foo", ""))
|
utils.ResourceUUID("foo", ""))
|
||||||
|
|
||||||
|
|
||||||
|
class StopWatchTest(tests_base.TestCase):
|
||||||
|
def test_no_states(self):
|
||||||
|
watch = utils.StopWatch()
|
||||||
|
self.assertRaises(RuntimeError, watch.stop)
|
||||||
|
|
||||||
|
def test_start_stop(self):
|
||||||
|
watch = utils.StopWatch()
|
||||||
|
watch.start()
|
||||||
|
watch.stop()
|
||||||
|
|
||||||
|
def test_no_elapsed(self):
|
||||||
|
watch = utils.StopWatch()
|
||||||
|
self.assertRaises(RuntimeError, watch.elapsed)
|
||||||
|
|
||||||
|
def test_elapsed(self):
|
||||||
|
watch = utils.StopWatch()
|
||||||
|
watch.start()
|
||||||
|
watch.stop()
|
||||||
|
elapsed = watch.elapsed()
|
||||||
|
self.assertAlmostEqual(elapsed, watch.elapsed())
|
||||||
|
|
||||||
|
def test_context_manager(self):
|
||||||
|
with utils.StopWatch() as watch:
|
||||||
|
pass
|
||||||
|
self.assertGreater(watch.elapsed(), 0)
|
||||||
|
@ -24,15 +24,14 @@ import os
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import iso8601
|
import iso8601
|
||||||
|
import monotonic
|
||||||
import numpy
|
import numpy
|
||||||
from oslo_log import log
|
from oslo_log import log
|
||||||
from oslo_utils import timeutils
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import six
|
import six
|
||||||
import tenacity
|
import tenacity
|
||||||
from tooz import coordination
|
from tooz import coordination
|
||||||
|
|
||||||
|
|
||||||
LOG = log.getLogger(__name__)
|
LOG = log.getLogger(__name__)
|
||||||
|
|
||||||
# uuid5 namespace for id transformation.
|
# uuid5 namespace for id transformation.
|
||||||
@ -160,8 +159,16 @@ def to_timespan(value):
|
|||||||
|
|
||||||
|
|
||||||
def utcnow():
|
def utcnow():
|
||||||
"""Better version of utcnow() that returns utcnow with a correct TZ."""
|
"""Version of utcnow() that returns utcnow with a correct TZ."""
|
||||||
return timeutils.utcnow(True)
|
return datetime.datetime.now(tz=iso8601.iso8601.UTC)
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_time(timestamp):
|
||||||
|
"""Normalize time in arbitrary timezone to UTC naive object."""
|
||||||
|
offset = timestamp.utcoffset()
|
||||||
|
if offset is None:
|
||||||
|
return timestamp
|
||||||
|
return timestamp.replace(tzinfo=None) - offset
|
||||||
|
|
||||||
|
|
||||||
def datetime_utc(*args):
|
def datetime_utc(*args):
|
||||||
@ -214,3 +221,79 @@ def strtobool(v):
|
|||||||
if isinstance(v, bool):
|
if isinstance(v, bool):
|
||||||
return v
|
return v
|
||||||
return bool(distutils.util.strtobool(v))
|
return bool(distutils.util.strtobool(v))
|
||||||
|
|
||||||
|
|
||||||
|
class StopWatch(object):
|
||||||
|
"""A simple timer/stopwatch helper class.
|
||||||
|
|
||||||
|
Inspired by: apache-commons-lang java stopwatch.
|
||||||
|
|
||||||
|
Not thread-safe (when a single watch is mutated by multiple threads at
|
||||||
|
the same time). Thread-safe when used by a single thread (not shared) or
|
||||||
|
when operations are performed in a thread-safe manner on these objects by
|
||||||
|
wrapping those operations with locks.
|
||||||
|
|
||||||
|
It will use the `monotonic`_ pypi library to find an appropriate
|
||||||
|
monotonically increasing time providing function (which typically varies
|
||||||
|
depending on operating system and python version).
|
||||||
|
|
||||||
|
.. _monotonic: https://pypi.python.org/pypi/monotonic/
|
||||||
|
"""
|
||||||
|
_STARTED = object()
|
||||||
|
_STOPPED = object()
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._started_at = None
|
||||||
|
self._stopped_at = None
|
||||||
|
self._state = None
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""Starts the watch (if not already started).
|
||||||
|
|
||||||
|
NOTE(harlowja): resets any splits previously captured (if any).
|
||||||
|
"""
|
||||||
|
if self._state == self._STARTED:
|
||||||
|
return self
|
||||||
|
self._started_at = monotonic.monotonic()
|
||||||
|
self._state = self._STARTED
|
||||||
|
return self
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _delta_seconds(earlier, later):
|
||||||
|
# Uses max to avoid the delta/time going backwards (and thus negative).
|
||||||
|
return max(0.0, later - earlier)
|
||||||
|
|
||||||
|
def elapsed(self):
|
||||||
|
"""Returns how many seconds have elapsed."""
|
||||||
|
if self._state not in (self._STARTED, self._STOPPED):
|
||||||
|
raise RuntimeError("Can not get the elapsed time of a stopwatch"
|
||||||
|
" if it has not been started/stopped")
|
||||||
|
if self._state == self._STOPPED:
|
||||||
|
elapsed = self._delta_seconds(self._started_at, self._stopped_at)
|
||||||
|
else:
|
||||||
|
elapsed = self._delta_seconds(
|
||||||
|
self._started_at, monotonic.monotonic())
|
||||||
|
return elapsed
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
"""Starts the watch."""
|
||||||
|
self.start()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type, value, traceback):
|
||||||
|
"""Stops the watch (ignoring errors if stop fails)."""
|
||||||
|
try:
|
||||||
|
self.stop()
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""Stops the watch."""
|
||||||
|
if self._state == self._STOPPED:
|
||||||
|
return self
|
||||||
|
if self._state != self._STARTED:
|
||||||
|
raise RuntimeError("Can not stop a stopwatch that has not been"
|
||||||
|
" started")
|
||||||
|
self._stopped_at = monotonic.monotonic()
|
||||||
|
self._state = self._STOPPED
|
||||||
|
return self
|
||||||
|
@ -4,7 +4,6 @@ iso8601
|
|||||||
oslo.config>=3.22.0
|
oslo.config>=3.22.0
|
||||||
oslo.log>=2.3.0
|
oslo.log>=2.3.0
|
||||||
oslo.policy>=0.3.0
|
oslo.policy>=0.3.0
|
||||||
oslo.utils>=3.18.0
|
|
||||||
oslo.middleware>=3.22.0
|
oslo.middleware>=3.22.0
|
||||||
pandas>=0.18.0
|
pandas>=0.18.0
|
||||||
scipy>=0.18.1 # BSD
|
scipy>=0.18.1 # BSD
|
||||||
@ -22,3 +21,4 @@ tenacity>=3.1.0 # Apache-2.0
|
|||||||
WebOb>=1.4.1
|
WebOb>=1.4.1
|
||||||
Paste
|
Paste
|
||||||
PasteDeploy
|
PasteDeploy
|
||||||
|
monotonic
|
||||||
|
Loading…
Reference in New Issue
Block a user