[+] improve deploy:wheel
1. format with ruff; 2. use tomlq from venv;
This commit is contained in:
parent
99a0013f42
commit
b3c0dd2703
@ -1,6 +1,6 @@
|
|||||||
project(
|
project(
|
||||||
run_command(
|
run_command(
|
||||||
'tomlq', '-r', '.project.name', 'pyproject.toml',
|
'.venv/bin/tomlq', '-r', '.project.name', 'pyproject.toml',
|
||||||
check: true
|
check: true
|
||||||
).stdout().strip('\n'),
|
).stdout().strip('\n'),
|
||||||
# 'online.fxreader.uv',
|
# 'online.fxreader.uv',
|
||||||
|
@ -6,99 +6,88 @@ import datetime
|
|||||||
import django.http
|
import django.http
|
||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Literal, Any, Optional, Annotated, cast,
|
Literal,
|
||||||
TypeVar, Protocol, Generic, Callable,
|
Any,
|
||||||
|
Optional,
|
||||||
|
Annotated,
|
||||||
|
cast,
|
||||||
|
TypeVar,
|
||||||
|
Protocol,
|
||||||
|
Generic,
|
||||||
|
Callable,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Metric(pydantic.BaseModel):
|
class Metric(pydantic.BaseModel):
|
||||||
name: str
|
name: str
|
||||||
type: Literal['gauge', 'counter']
|
type: Literal['gauge', 'counter']
|
||||||
help: Optional[str] = None
|
help: Optional[str] = None
|
||||||
|
|
||||||
class Sample(pydantic.BaseModel):
|
class Sample(pydantic.BaseModel):
|
||||||
value: str
|
value: str
|
||||||
parameters: dict[str, str]
|
parameters: dict[str, str]
|
||||||
timestamp: Optional[datetime.datetime] = None
|
timestamp: Optional[datetime.datetime] = None
|
||||||
|
|
||||||
samples: list[Sample] = pydantic.Field(
|
samples: list[Sample] = pydantic.Field(
|
||||||
default_factory=lambda: [],
|
default_factory=lambda: [],
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def sample_serialize(
|
def sample_serialize(
|
||||||
cls,
|
cls,
|
||||||
o: 'Metric',
|
o: 'Metric',
|
||||||
s: 'Metric.Sample',
|
s: 'Metric.Sample',
|
||||||
) -> str:
|
) -> str:
|
||||||
samples: list[Metric.Sample] = [s,]
|
samples: list[Metric.Sample] = [
|
||||||
|
s,
|
||||||
|
]
|
||||||
|
|
||||||
if o.type == 'gauge':
|
if o.type == 'gauge':
|
||||||
samples.append(
|
samples.append(
|
||||||
Metric.Sample(
|
Metric.Sample(parameters=s.parameters, value='NaN', timestamp=(s.timestamp + datetime.timedelta(seconds=15) if s.timestamp else None))
|
||||||
parameters=s.parameters,
|
)
|
||||||
value='NaN',
|
|
||||||
timestamp=(
|
return ''.join(
|
||||||
s.timestamp + datetime.timedelta(seconds=15)
|
[
|
||||||
if s.timestamp
|
'{metric}{{{parameters}}} {value} {timestamp}\n'.format(
|
||||||
else None
|
metric=o.name,
|
||||||
)
|
parameters=','.join(
|
||||||
)
|
[
|
||||||
)
|
'%s=%s'
|
||||||
|
% (
|
||||||
|
k,
|
||||||
|
json.dumps(v),
|
||||||
|
)
|
||||||
|
for k, v in s2.parameters.items()
|
||||||
|
]
|
||||||
|
),
|
||||||
|
value=s2.value,
|
||||||
|
timestamp=('%.f' % (s2.timestamp.timestamp() * 1000,) if s2.timestamp else ''),
|
||||||
|
)
|
||||||
|
for s2 in samples
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
return ''.join([
|
|
||||||
'{metric}{{{parameters}}} {value} {timestamp}\n'.format(
|
|
||||||
metric=o.name,
|
|
||||||
parameters=','.join([
|
|
||||||
'%s=%s' % (
|
|
||||||
k,
|
|
||||||
json.dumps(v),
|
|
||||||
)
|
|
||||||
for k, v in s2.parameters.items()
|
|
||||||
]),
|
|
||||||
value=s2.value,
|
|
||||||
timestamp=(
|
|
||||||
'%.f' % (s2.timestamp.timestamp() * 1000,)
|
|
||||||
if s2.timestamp
|
|
||||||
else ''
|
|
||||||
),
|
|
||||||
)
|
|
||||||
for s2 in samples
|
|
||||||
])
|
|
||||||
|
|
||||||
def serialize(
|
def serialize(
|
||||||
metrics: list[Metric],
|
metrics: list[Metric],
|
||||||
):
|
):
|
||||||
return django.http.HttpResponse(
|
return django.http.HttpResponse(
|
||||||
''.join([
|
''.join(
|
||||||
'{help}{type}{samples}'.format(
|
[
|
||||||
#help='# HELP %s some metric' % o.name,
|
'{help}{type}{samples}'.format(
|
||||||
#type='# TYPE %s counter' % o.name,
|
# help='# HELP %s some metric' % o.name,
|
||||||
help=(
|
# type='# TYPE %s counter' % o.name,
|
||||||
'# HELP {0} {1}\n'.format(
|
help=('# HELP {0} {1}\n'.format(o.name, o.help) if o.help else ''),
|
||||||
o.name,
|
type=('# TYPE {0} {1}\n'.format(o.name, o.type) if o.type else ''),
|
||||||
o.help
|
samples=''.join([Metric.sample_serialize(o, s) for s in o.samples]),
|
||||||
)
|
)
|
||||||
if o.help
|
for o in metrics
|
||||||
else ''
|
if len(o.samples) > 0
|
||||||
),
|
]
|
||||||
type=(
|
),
|
||||||
'# TYPE {0} {1}\n'.format(
|
content_type='text/plain; version=0.0.4; charset=utf-8',
|
||||||
o.name,
|
)
|
||||||
o.type
|
|
||||||
)
|
|
||||||
if o.type
|
|
||||||
else ''
|
|
||||||
),
|
|
||||||
samples=''.join([
|
|
||||||
Metric.sample_serialize(o, s)
|
|
||||||
for s in o.samples
|
|
||||||
]),
|
|
||||||
)
|
|
||||||
for o in metrics
|
|
||||||
if len(o.samples) > 0
|
|
||||||
]),
|
|
||||||
content_type='text/plain; version=0.0.4; charset=utf-8',
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user