Files
service-aware-frontend/pkgs/clan-cli/clan_cli/webui/sql_models.py
2024-01-11 15:34:01 +01:00

75 lines
2.3 KiB
Python

from sqlalchemy import JSON, Boolean, Column, Enum, ForeignKey, Integer, String, Text
from sqlalchemy.orm import relationship
from .schemas import Roles
from .sql_db import Base
# Relationsship example
# https://dev.to/freddiemazzilli/flask-sqlalchemy-relationships-exploring-relationship-associations-igo
class Entity(Base):
__tablename__ = "entities"
## Queryable body ##
did = Column(String, primary_key=True, index=True)
name = Column(String, index=True, unique=True)
ip = Column(String, index=True)
network = Column(String, index=True)
role = Column(Enum(Roles), index=True, nullable=False) # type: ignore
# role = Column(String, index=True, nullable=False)
attached = Column(Boolean, index=True)
visible = Column(Boolean, index=True)
stop_health_task = Column(Boolean)
## Non queryable body ##
# In here we deposit: Not yet defined stuff
other = Column(JSON)
## Relations ##
services = relationship("Service", back_populates="entity")
class ServiceAbstract(Base):
__abstract__ = True
# Queryable body
uuid = Column(Text(length=36), primary_key=True, index=True)
service_name = Column(String, index=True)
service_type = Column(String, index=True)
endpoint_url = Column(String, index=True)
status = Column(String, index=True)
## Non queryable body ##
# In here we deposit: Action
other = Column(JSON)
class Service(ServiceAbstract):
__tablename__ = "services"
## Relations ##
# One entity can have many services
entity = relationship("Entity", back_populates="services")
entity_did = Column(String, ForeignKey("entities.did"))
class Eventmessage(Base):
__tablename__ = "eventmessages"
## Queryable body ##
id = Column(Integer, primary_key=True, index=True)
timestamp = Column(Integer, unique=True, index=True)
group = Column(Integer, index=True)
group_id = Column(Integer, index=True)
msg_type = Column(Integer, index=True) # message type for the label
src_did = Column(String, index=True)
des_did = Column(String, index=True)
## Non queryable body ##
# In here we deposit: Network, Roles, Visible, etc.
msg = Column(JSON)
## Relations ##
# One entity can send many messages