Skip to content

Commit

Permalink
Release plugnpy-v2.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas committed May 21, 2020
1 parent d044fd6 commit d289c5c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,15 @@ will simply return the data. However, if the data does not exist in the cache ma
cachemanager, so future calls can use the data from the cache manager. The data is valid for the time specified by the
TTL.

#### set_data

Sometimes data must be inserted or updated in the cache manager, without retrieving the existing data. In these
scenarios the **set_data** method can be used, this will create the cache manager client and call the **set_data**
method of the **CacheManagerClient** internally.

```python
CachemanagerUtils.set_data(key, data, ttl)
```

#### generate_key

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.6
2.0.7
2 changes: 1 addition & 1 deletion plugnpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""plugnpy - A Simple Python Library for creating Opsview Opspack plugins"""

__version__ = '2.0.6'
__version__ = '2.0.7'
__program_name__ = 'plugnpy'

from .check import Check
Expand Down
38 changes: 28 additions & 10 deletions plugnpy/cachemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class CacheManagerUtils(object): # pylint: disable=too-few-public-methods
"""Utility functions for cache manager"""

client = None
host = os.environ.get('OPSVIEW_CACHE_MANAGER_HOST')
port = os.environ.get('OPSVIEW_CACHE_MANAGER_PORT')
namespace = os.environ.get('OPSVIEW_CACHE_MANAGER_NAMESPACE')

@staticmethod
def generate_key(*args):
Expand All @@ -35,6 +38,24 @@ def generate_key(*args):
values.append(arg)
return hash_string(DELIMITER.join(values))

@staticmethod
def set_data(key, data, ttl=900):
"""Set data in the cache manager
:param key: The key to store the data under.
:param data: The data to store.
:param ttl: The number of seconds data is valid for.
"""
if not CacheManagerUtils.client:
CacheManagerUtils.client = CacheManagerClient(
CacheManagerUtils.host,
CacheManagerUtils.port,
CacheManagerUtils.namespace
)
data = json.dumps(data)
key = hash_string(key)
return CacheManagerUtils.client.set_data(key, data, ttl)

@staticmethod
def get_via_cachemanager(no_cachemanager, key, ttl, func, *args, **kwargs):
"""Gets data via the cache manager
Expand All @@ -43,26 +64,23 @@ def get_via_cachemanager(no_cachemanager, key, ttl, func, *args, **kwargs):
If the cache manager is required, tries to get the data from the cachemanager.
If the data does not exist, calls the function and stores the returned data in the cache manager.
Tries to get data via the cachemanager, if the data doesn't exists, calls the func with the specified args and
sets the
:param no_cachemanager: True if cache manager is not required, False otherwise.
:param key: The key to store the data under.
:param ttl: The number of seconds data is valid for
:param ttl: The number of seconds data is valid for.
:param func: The function to retrieve the data, if the data is not in the cache manager.
:param args: The arguments to pass to the user's data retrieval function.
:param kwargs: The keyword arguments to pass to the user's data retrieval function.
"""
host = os.environ.get('OPSVIEW_CACHE_MANAGER_HOST')
port = os.environ.get('OPSVIEW_CACHE_MANAGER_PORT')
namespace = os.environ.get('OPSVIEW_CACHE_MANAGER_NAMESPACE')

if not CacheManagerUtils._is_required(no_cachemanager, host):
if not CacheManagerUtils._is_required(no_cachemanager, CacheManagerUtils.host):
data = func(*args, **kwargs)
return data

if not CacheManagerUtils.client:
CacheManagerUtils.client = CacheManagerClient(host, port, namespace)
CacheManagerUtils.client = CacheManagerClient(
CacheManagerUtils.host,
CacheManagerUtils.port,
CacheManagerUtils.namespace
)

key = hash_string(key)
try:
Expand Down
11 changes: 10 additions & 1 deletion plugnpy/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class Metric(object): # pylint: disable=too-many-instance-attributes
CONVERTIBLE_UNITS_N = (UNIT_SECONDS, UNIT_HERTZ, UNIT_WATTS)
CONVERTIBLE_UNITS = CONVERTIBLE_UNITS_P + CONVERTIBLE_UNITS_N

UNIT_MAPPING = {
'per_second': '/s',
'per_minute': '/min',
'seconds_per_minute': 's/min',
'bytes_per_second': 'B/s',
'bytes_per_minute': 'B/min'
}

# Prefixes for units
DISPLAY_UNIT_FACTORS = {
UNIT_BYTES: lambda factor: 1.0,
Expand Down Expand Up @@ -130,7 +138,8 @@ def __str__(self):

if self.convert_metric:
value, unit = Metric.convert_value(self.value, self.unit, si_bytes_conversion=self.si_bytes_conversion)
unit = '/s' if self.unit == 'per_second' else unit

unit = self.UNIT_MAPPING.get(self.unit, unit)
# try to convert value to precision specified if it's a number
try:
value = float(value)
Expand Down
31 changes: 24 additions & 7 deletions test/test_cachemanager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import functools
import pytest
from socket import error as SocketError
Expand Down Expand Up @@ -34,9 +33,9 @@ def test_is_required(no_cachemanager, cachemanager_host, raises, expected):
)
def test_get_via_cachemanager(mocker, no_cachemanager, cachemanager_host, get_data_retval, get_data_side_effect, raises,
expected):
host = os.environ['OPSVIEW_CACHE_MANAGER_HOST'] = cachemanager_host
port = os.environ['OPSVIEW_CACHE_MANAGER_PORT'] = 'some_port'
namespace = os.environ['OPSVIEW_CACHE_MANAGER_NAMESPACE'] = 'some_namespace'
CacheManagerUtils.host = cachemanager_host
CacheManagerUtils.port = 'some_port'
CacheManagerUtils.namespace = 'some_namespace'

func = lambda x: str(x**2)

Expand All @@ -50,9 +49,9 @@ def test_get_via_cachemanager(mocker, no_cachemanager, cachemanager_host, get_da
)

def test_get_via_cachemanager_exception(mocker):
host = os.environ['OPSVIEW_CACHE_MANAGER_HOST'] = 'host'
port = os.environ['OPSVIEW_CACHE_MANAGER_PORT'] = 'some_port'
namespace = os.environ['OPSVIEW_CACHE_MANAGER_NAMESPACE'] = 'some_namespace'
CacheManagerUtils.host = 'host'
CacheManagerUtils.port = 'some_port'
CacheManagerUtils.namespace = 'some_namespace'

def func(error):
raise Exception(error)
Expand All @@ -66,6 +65,24 @@ def func(error):
{'error': 'Something went wrong'}
)

def test_set_data(mocker):
CacheManagerUtils.host = 'host'
CacheManagerUtils.port = 'some_port'
CacheManagerUtils.namespace = 'some_namespace'
CacheManagerUtils.client = None
expected = 'data'
CacheManagerClient.set_data = mocker.Mock(return_value=expected)

assert CacheManagerUtils.client == None
actual = CacheManagerUtils.set_data('key', {'some': 'data'}, 900)
assert actual == expected

# after call client will be initialised
assert CacheManagerUtils.client != None
actual = CacheManagerUtils.set_data('key', {'some': 'data'}, 900)
assert actual == expected


# generated with https://emn178.github.io/online-tools/sha256.html
@pytest.mark.parametrize(
'arg1, arg2, arg3, expected',
Expand Down

0 comments on commit d289c5c

Please sign in to comment.