X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;h=07d92cf3355a1dcb863bd83ef840cf3343550bec;hb=416a753c60f1ba9545f4aa36fb45e1730046e4b2;hp=2191106fc16edb33a7dc6fb0fe68381c67d4a68a;hpb=b3f86c9f43a0eb4d56487118a403c39f979ec042;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index 2191106..07d92cf 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -23,45 +23,65 @@ ## import logging +import os +from typing import Iterable -from peewee import * -from playhouse.sqlite_ext import SqliteExtDatabase +from peewee import CharField, FloatField, Model, AutoField, Proxy +from peewee_migrate import Router +from playhouse.db_url import connect -from osm_mon.plugins.OpenStack.settings import Config +from osm_mon import migrations +from osm_mon.core.config import Config log = logging.getLogger(__name__) -cfg = Config.instance() -db = SqliteExtDatabase('mon.db') +db = Proxy() class BaseModel(Model): + id = AutoField(primary_key=True) + class Meta: database = db -class VimCredentials(BaseModel): - uuid = CharField() +class Alarm(BaseModel): + uuid = CharField(unique=True) name = CharField() - type = CharField() - url = CharField() - user = CharField() - password = CharField() - tenant_name = CharField() - config = TextField() + severity = CharField() + threshold = FloatField() + operation = CharField() + statistic = CharField() + monitoring_param = CharField() + vdur_name = CharField() + vnf_member_index = CharField() + nsr_id = CharField() class DatabaseManager: - def create_tables(self): - try: - db.connect() - db.create_tables([VimCredentials]) - db.close() - except Exception as e: - log.exception("Error creating tables: ") - - def get_credentials(self, vim_uuid): - return VimCredentials.get(VimCredentials.uuid == vim_uuid) - - def save_credentials(self, vim_credentials): - vim_credentials.save() + 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 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)