X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;fp=osm_mon%2Fcore%2Fdatabase.py;h=0000000000000000000000000000000000000000;hb=b6233b3d90ca1ba617e476b915fba056cf7891f7;hp=61bd18060122f759a22c625064e7a1b98b1db84e;hpb=cee3b8656b4eef1c9dda980191a3463a5fdb2a67;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py deleted file mode 100644 index 61bd180..0000000 --- a/osm_mon/core/database.py +++ /dev/null @@ -1,96 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright 2018 Whitestack, LLC -# ************************************************************* - -# This file is part of OSM Monitoring module -# All Rights Reserved to Whitestack, LLC - -# 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 - -# http://www.apache.org/licenses/LICENSE-2.0 - -# 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. - -# For those usages not covered by the Apache License, Version 2.0 please -# contact: bdiaz@whitestack.com or glavado@whitestack.com -## - -import logging -import os -from typing import Iterable - -from peewee import CharField, FloatField, Model, AutoField, Proxy, ForeignKeyField -from peewee_migrate import Router -from playhouse.db_url import connect - -from osm_mon import migrations -from osm_mon.core.config import Config - -log = logging.getLogger(__name__) - -db = Proxy() - - -class BaseModel(Model): - id = AutoField(primary_key=True) - - class Meta: - database = db - - -class Alarm(BaseModel): - uuid = CharField(unique=True) - name = CharField() - severity = CharField() - threshold = FloatField() - operation = CharField() - statistic = CharField() - metric = CharField() - - -class AlarmTag(BaseModel): - name = CharField() - value = CharField() - alarm = ForeignKeyField(Alarm, related_name='tags', on_delete='CASCADE') - - -class DatabaseManager: - def __init__(self, config: Config): - db.initialize(connect(config.get('sql', 'database_uri'))) - - def create_tables(self) -> None: - db.connect() - with db.atomic(): - router = Router(db, os.path.dirname(migrations.__file__)) - router.run() - db.close() - - -class AlarmTagRepository: - @staticmethod - def create(**query) -> Alarm: - return AlarmTag.create(**query) - - -class AlarmRepository: - @staticmethod - def create(**query) -> Alarm: - return Alarm.create(**query) - - @staticmethod - def get(*expressions) -> Alarm: - return Alarm.select().where(*expressions).get() - - @staticmethod - def list(*expressions) -> Iterable[Alarm]: - if expressions == (): - return Alarm.select() - else: - return Alarm.select().where(*expressions)