Skip to content

Commit

Permalink
[#62815] server: Add telemetry endpoints
Browse files Browse the repository at this point in the history
This commit implements telemetry endpoints for receiving
batched logs from registered devices. Since it adds a new
logs table to the database, a 3rd alembic revision is added
to allow for migrations.
  • Loading branch information
Kacper Zienkiewicz committed Aug 19, 2024
1 parent 3c0b44e commit 8025017
Show file tree
Hide file tree
Showing 13 changed files with 998 additions and 47 deletions.
11 changes: 11 additions & 0 deletions .ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ test-server-stress:
- server/tests/logs/*
when: always

test-server-logs:
extends: .build
stage: local-tests
script:
- cd server
- pytest tests/test-server-logs.py
only:
refs:
- merge_requests
- main

# ===== rdfm-client ===== #

build-rdfm-client:
Expand Down
81 changes: 81 additions & 0 deletions common/communication/src/rdfm/schema/v1/logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import ClassVar, Type, List
import datetime
from dataclasses import field
from typing import Optional

import marshmallow
from marshmallow import ValidationError
import marshmallow_dataclass
from rdfm.schema.validators import Contains


@marshmallow_dataclass.dataclass
class LogRouteParameters():
"""Represents GET parameters passed to a log route"""
since: Optional[datetime.datetime] = field(metadata={
"required": False,
"format": "rfc"
})
to: Optional[datetime.datetime] = field(metadata={
"required": False,
"format": "rfc"
})
name: Optional[str] = field(metadata={
"required": False
})
Schema: ClassVar[Type[marshmallow.Schema]] = marshmallow.Schema


@marshmallow_dataclass.dataclass
class LogEntry():
"""Represents a single log entry within a batch of logs
"""
device_timestamp: datetime.datetime = field(metadata={
"required": True,
"format": "rfc"
})
name: str = field(metadata={
"required": True
})
entry: str = field(metadata={
"required": True
})
Schema: ClassVar[Type[marshmallow.Schema]] = marshmallow.Schema


@marshmallow_dataclass.dataclass
class LogBatch():
"""Represents a log batch
"""
batch: List[LogEntry] = field(metadata={
"required": True
})
Schema: ClassVar[Type[marshmallow.Schema]] = marshmallow.Schema


@marshmallow_dataclass.dataclass
class Log():
"""Represents a log entry
"""
id: int = field(metadata={
"required": True
})
created: Optional[datetime.datetime] = field(metadata={
"required": True,
"allowed_none": True,
"format": "rfc"
})
device_id: int = field(metadata={
"required": True
})
device_timestamp: datetime.datetime = field(metadata={
"required": True,
"format": "rfc"
})
name: str = field(metadata={
"required": True
})
entry: str = field(metadata={
"required": True
})
Schema: ClassVar[Type[marshmallow.Schema]] = marshmallow.Schema
35 changes: 35 additions & 0 deletions server/alembic/versions/3_add_logs_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Add logs table
Revision ID: 3
Revises: 2
Create Date: 2024-07-25 10:13:47.069428
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '3'
down_revision: Union[str, None] = '2'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_table('logs',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('device_id', sa.Integer(), nullable=False),
sa.Column('device_timestamp', sa.DateTime(), nullable=False),
sa.Column('name', sa.Text(), nullable=False),
sa.Column('entry', sa.Text(), nullable=False),
sa.ForeignKeyConstraint(['device_id'], ['devices.id'], ondelete='RESTRICT'),
sa.PrimaryKeyConstraint('id')
)


def downgrade() -> None:
op.drop_table('logs')
2 changes: 2 additions & 0 deletions server/src/api/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import api.v1.groups
import api.v1.update
import api.v1.auth
import api.v1.logs
import api.v1.ws.device


Expand All @@ -15,5 +16,6 @@ def create_routes() -> Blueprint:
api_routes.register_blueprint(api.v1.groups.groups_blueprint)
api_routes.register_blueprint(api.v1.update.update_blueprint)
api_routes.register_blueprint(api.v1.auth.auth_blueprint)
api_routes.register_blueprint(api.v1.logs.logs_blueprint)
api_routes.register_blueprint(api.v1.ws.device.device_ws_blueprint)
return api_routes
Loading

0 comments on commit 8025017

Please sign in to comment.