mirror of
https://github.com/ferdzo/iotDashboard.git
synced 2026-04-05 09:06:26 +00:00
Functioning mqtt ingestion and db write, formating changes, device manager initiated
This commit is contained in:
@@ -9,7 +9,8 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
||||
|
||||
# Load environment variables
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv(os.path.join(os.path.dirname(__file__), '../.env'))
|
||||
|
||||
load_dotenv(os.path.join(os.path.dirname(__file__), "../.env"))
|
||||
|
||||
# Import your models
|
||||
from models import Base
|
||||
@@ -19,9 +20,9 @@ from models import Base
|
||||
config = context.config
|
||||
|
||||
# Set database URL from environment
|
||||
database_url = os.getenv('CONNECTION_STRING') or os.getenv('DATABASE_URL')
|
||||
database_url = os.getenv("CONNECTION_STRING") or os.getenv("DATABASE_URL")
|
||||
if database_url:
|
||||
config.set_main_option('sqlalchemy.url', database_url)
|
||||
config.set_main_option("sqlalchemy.url", database_url)
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
@@ -76,9 +77,7 @@ def run_migrations_online() -> None:
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
)
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
"""Initial
|
||||
|
||||
Revision ID: dae12d7f4ddf
|
||||
Revises:
|
||||
Revises:
|
||||
Create Date: 2025-10-28 22:06:45.637836+00:00
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
@@ -12,7 +13,7 @@ import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'dae12d7f4ddf'
|
||||
revision: str = "dae12d7f4ddf"
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
@@ -21,30 +22,43 @@ depends_on: Union[str, Sequence[str], None] = None
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('devices',
|
||||
sa.Column('id', sa.Text(), nullable=False),
|
||||
sa.Column('name', sa.Text(), nullable=False),
|
||||
sa.Column('location', sa.Text(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
op.create_table(
|
||||
"devices",
|
||||
sa.Column("id", sa.Text(), nullable=False),
|
||||
sa.Column("name", sa.Text(), nullable=False),
|
||||
sa.Column("location", sa.Text(), nullable=True),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_table('telemetry',
|
||||
sa.Column('time', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('device_id', sa.Text(), nullable=False),
|
||||
sa.Column('metric', sa.Text(), nullable=False),
|
||||
sa.Column('value', sa.Float(), nullable=False),
|
||||
sa.Column('unit', sa.Text(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['device_id'], ['devices.id'], ),
|
||||
sa.PrimaryKeyConstraint('time', 'device_id', 'metric')
|
||||
op.create_table(
|
||||
"telemetry",
|
||||
sa.Column("time", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("device_id", sa.Text(), nullable=False),
|
||||
sa.Column("metric", sa.Text(), nullable=False),
|
||||
sa.Column("value", sa.Float(), nullable=False),
|
||||
sa.Column("unit", sa.Text(), nullable=True),
|
||||
sa.ForeignKeyConstraint(
|
||||
["device_id"],
|
||||
["devices.id"],
|
||||
),
|
||||
sa.PrimaryKeyConstraint("time", "device_id", "metric"),
|
||||
)
|
||||
op.create_index('idx_telemetry_device_time', 'telemetry', ['device_id', 'time'], unique=False)
|
||||
op.create_index(
|
||||
"idx_telemetry_device_time", "telemetry", ["device_id", "time"], unique=False
|
||||
)
|
||||
|
||||
# Convert telemetry table to TimescaleDB hypertable
|
||||
op.execute("SELECT create_hypertable('telemetry', 'time', if_not_exists => TRUE);")
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index('idx_telemetry_device_time', table_name='telemetry')
|
||||
op.drop_table('telemetry')
|
||||
op.drop_table('devices')
|
||||
|
||||
# Note: TimescaleDB hypertables cannot be easily converted back to regular tables
|
||||
# The table will be dropped entirely, which removes the hypertable as well
|
||||
op.drop_index("idx_telemetry_device_time", table_name="telemetry")
|
||||
op.drop_table("telemetry")
|
||||
op.drop_table("devices")
|
||||
# ### end Alembic commands ###
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
"""add device_certificates table
|
||||
|
||||
Revision ID: f94393f57c35
|
||||
Revises: dae12d7f4ddf
|
||||
Create Date: 2025-10-29 21:57:58.983071+00:00
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "f94393f57c35"
|
||||
down_revision: Union[str, Sequence[str], None] = "dae12d7f4ddf"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"device_certificates",
|
||||
sa.Column("device_id", sa.Text(), nullable=False),
|
||||
sa.Column("certificate_pem", sa.Text(), nullable=False),
|
||||
sa.Column("private_key_pem", sa.Text(), nullable=True),
|
||||
sa.Column("issued_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("expires_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("revoked_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(["device_id"], ["devices.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("device_id"),
|
||||
)
|
||||
op.add_column(
|
||||
"devices",
|
||||
sa.Column(
|
||||
"created_at",
|
||||
sa.DateTime(timezone=True),
|
||||
server_default=sa.text("now()"),
|
||||
nullable=True,
|
||||
),
|
||||
)
|
||||
op.drop_index(op.f("telemetry_time_idx"), table_name="telemetry")
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_index(
|
||||
op.f("telemetry_time_idx"),
|
||||
"telemetry",
|
||||
[sa.literal_column("time DESC")],
|
||||
unique=False,
|
||||
)
|
||||
op.drop_column("devices", "created_at")
|
||||
op.drop_table("device_certificates")
|
||||
# ### end Alembic commands ###
|
||||
@@ -7,42 +7,61 @@ To modify schema:
|
||||
3. Review the generated migration in alembic/versions/
|
||||
4. Run: alembic upgrade head
|
||||
"""
|
||||
from sqlalchemy import Boolean, Column, Float, ForeignKey, Index,Text, DateTime
|
||||
|
||||
from sqlalchemy import Boolean, Column, Float, ForeignKey, Index, Text, DateTime
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class Device(Base):
|
||||
"""IoT devices registered in the system."""
|
||||
__tablename__ = 'devices'
|
||||
|
||||
|
||||
__tablename__ = "devices"
|
||||
|
||||
id = Column(Text, primary_key=True)
|
||||
name = Column(Text, nullable=False)
|
||||
location = Column(Text)
|
||||
is_active = Column(Boolean, default=True)
|
||||
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Device(id={self.id}, name={self.name})>"
|
||||
|
||||
|
||||
class DeviceCertificate(Base):
|
||||
"""X.509 certificates issued to devices for mTLS authentication."""
|
||||
|
||||
__tablename__ = "device_certificates"
|
||||
|
||||
device_id = Column(
|
||||
Text, ForeignKey("devices.id", ondelete="CASCADE"), primary_key=True
|
||||
)
|
||||
certificate_pem = Column(Text, nullable=False)
|
||||
private_key_pem = Column(Text) # Optional: for backup/escrow
|
||||
issued_at = Column(DateTime(timezone=True), nullable=False)
|
||||
expires_at = Column(DateTime(timezone=True), nullable=False)
|
||||
revoked_at = Column(DateTime(timezone=True))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<DeviceCertificate(device_id={self.device_id}, expires={self.expires_at})>"
|
||||
|
||||
|
||||
class Telemetry(Base):
|
||||
"""
|
||||
Time-series telemetry data from devices.
|
||||
This will be converted to a TimescaleDB hypertable.
|
||||
"""
|
||||
__tablename__ = 'telemetry'
|
||||
|
||||
|
||||
__tablename__ = "telemetry"
|
||||
|
||||
time = Column(DateTime(timezone=True), primary_key=True, nullable=False)
|
||||
device_id = Column(Text, ForeignKey('devices.id'), primary_key=True, nullable=False)
|
||||
metric = Column(Text, primary_key=True, nullable=False) # e.g., 'light', 'temperature'
|
||||
device_id = Column(Text, ForeignKey("devices.id"), primary_key=True, nullable=False)
|
||||
metric = Column(Text, primary_key=True, nullable=False)
|
||||
value = Column(Float, nullable=False)
|
||||
unit = Column(Text)
|
||||
|
||||
__table_args__ = (
|
||||
Index('idx_telemetry_device_time', 'device_id', 'time'),
|
||||
)
|
||||
|
||||
|
||||
__table_args__ = (Index("idx_telemetry_device_time", "device_id", "time"),)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Telemetry(device={self.device_id}, metric={self.metric}, value={self.value})>"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user