Shame on meeeeee

... forgot convert.py and some test files
This commit is contained in:
benoit 2021-08-13 14:51:14 +02:00
parent 7258d5fd60
commit 1219cb8060
4 changed files with 157 additions and 0 deletions

58
check_patroni/convert.py Normal file
View file

@ -0,0 +1,58 @@
import click
import re
from typing import Union, Tuple
def size_to_byte(value: str) -> int:
"""Convert any size to Byte
>>> size_to_byte('1TB')
1099511627776
>>> size_to_byte('5kB')
5120
>>> size_to_byte('.5kB')
512
>>> size_to_byte('.5 yoyo')
Traceback (most recent call last):
...
click.exceptions.BadParameter: Invalid unit for size f{value}
"""
convert = {
"B": 1,
"kB": 1024,
"MB": 1024 * 1024,
"GB": 1024 * 1024 * 1024,
"TB": 1024 * 1024 * 1024 * 1024,
}
val, unit = strtod(value)
if val is None:
val = 1
if unit is None:
# No unit, all good
# we can round half bytes dont really make sense
return round(val)
else:
try:
multiplicateur = convert[unit]
except KeyError:
raise click.BadParameter("Invalid unit for size f{value}")
# we can round half bytes dont really make sense
return round(val * multiplicateur)
DBL_RE = re.compile(r"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?")
def strtod(value: str) -> Tuple[Union[float, None], Union[str, None]]:
"""As most as possible close equivalent of strtod(3) function used by postgres to parse parameter values.
>>> strtod(' A ') == (None, 'A')
True
"""
value = str(value).strip()
match = DBL_RE.match(value)
if match:
end = match.end()
return float(value[:end]), value[end:]
return None, value

View file

@ -0,0 +1,33 @@
{
"members": [
{
"name": "srv1",
"role": "leader",
"state": "running",
"api_url": "https://10.20.199.3:8008/patroni",
"host": "10.20.199.3",
"port": 5432,
"timeline": 51
},
{
"name": "srv2",
"role": "replica",
"state": "stopped",
"api_url": "https://10.20.199.4:8008/patroni",
"host": "10.20.199.4",
"port": 5432,
"timeline": 51,
"lag": "unknown"
},
{
"name": "srv3",
"role": "replica",
"state": "running",
"api_url": "https://10.20.199.5:8008/patroni",
"host": "10.20.199.5",
"port": 5432,
"timeline": 51,
"lag": 0
}
]
}

View file

@ -0,0 +1,33 @@
{
"members": [
{
"name": "srv1",
"role": "leader",
"state": "running",
"api_url": "https://10.20.199.3:8008/patroni",
"host": "10.20.199.3",
"port": 5432,
"timeline": 51
},
{
"name": "srv2",
"role": "replica",
"state": "running",
"api_url": "https://10.20.199.4:8008/patroni",
"host": "10.20.199.4",
"port": 5432,
"timeline": 51,
"lag": 10241024
},
{
"name": "srv3",
"role": "replica",
"state": "running",
"api_url": "https://10.20.199.5:8008/patroni",
"host": "10.20.199.5",
"port": 5432,
"timeline": 51,
"lag": 20000000
}
]
}

View file

@ -0,0 +1,33 @@
{
"members": [
{
"name": "srv1",
"role": "leader",
"state": "running",
"api_url": "https://10.20.199.3:8008/patroni",
"host": "10.20.199.3",
"port": 5432,
"timeline": 51
},
{
"name": "srv2",
"role": "replica",
"state": "running",
"api_url": "https://10.20.199.4:8008/patroni",
"host": "10.20.199.4",
"port": 5432,
"timeline": 51,
"lag": 1024
},
{
"name": "srv3",
"role": "replica",
"state": "running",
"api_url": "https://10.20.199.5:8008/patroni",
"host": "10.20.199.5",
"port": 5432,
"timeline": 51,
"lag": 0
}
]
}