metric.py
68 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
from datetime import datetime | ||||
import sqlalchemy as sa | ||||
from sqlalchemy.dialects.postgresql import JSON | ||||
from ziggurat_foundations.models.base import BaseModel | ||||
from appenlight.lib.utils import convert_es_type | ||||
from appenlight.models import Base | ||||
class Metric(Base, BaseModel): | ||||
r153 | __tablename__ = "metrics" | |||
__table_args__ = {"implicit_returning": False} | ||||
r0 | ||||
pkey = sa.Column(sa.BigInteger(), primary_key=True) | ||||
r153 | resource_id = sa.Column( | |||
sa.Integer(), | ||||
sa.ForeignKey("applications.resource_id"), | ||||
nullable=False, | ||||
primary_key=True, | ||||
) | ||||
timestamp = sa.Column( | ||||
sa.DateTime(), default=datetime.utcnow, server_default=sa.func.now() | ||||
) | ||||
r0 | tags = sa.Column(JSON(), default={}) | |||
namespace = sa.Column(sa.Unicode(255)) | ||||
@property | ||||
def partition_id(self): | ||||
r153 | return "rcae_m_%s" % self.timestamp.strftime("%Y_%m_%d") | |||
r0 | ||||
def es_doc(self): | ||||
tags = {} | ||||
tag_list = [] | ||||
for name, value in self.tags.items(): | ||||
# replace dot in indexed tag name | ||||
r153 | name = name.replace(".", "_") | |||
r0 | tag_list.append(name) | |||
tags[name] = { | ||||
"values": convert_es_type(value), | ||||
r153 | "numeric_values": value | |||
if (isinstance(value, (int, float)) and not isinstance(value, bool)) | ||||
else None, | ||||
r0 | } | |||
return { | ||||
r153 | "resource_id": self.resource_id, | |||
"timestamp": self.timestamp, | ||||
"namespace": self.namespace, | ||||
"tags": tags, | ||||
"tag_list": tag_list, | ||||
r0 | } | |||