Implement lab6 OpenTelemetry observability stack

This commit is contained in:
2026-11-21 19:29:20 +03:00
parent 470d9d6f1e
commit ad8db1f32b
54 changed files with 5116 additions and 19 deletions
+74
View File
@@ -0,0 +1,74 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username VARCHAR(32) NOT NULL UNIQUE,
display_name VARCHAR(100) NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT username_format
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT display_name_length
CHECK (char_length(display_name) BETWEEN 1 AND 100),
CONSTRAINT display_name_not_blank
CHECK (length(btrim(display_name)) > 0)
);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT title_not_blank
CHECK (length(btrim(title)) > 0)
);
CREATE TABLE IF NOT EXISTS chat_members (
chat_id UUID NOT NULL
REFERENCES chats(id) ON DELETE CASCADE,
user_id UUID NOT NULL
REFERENCES users(id),
position INTEGER NOT NULL,
PRIMARY KEY (chat_id, user_id),
UNIQUE (chat_id, position)
);
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL
REFERENCES chats(id) ON DELETE CASCADE,
sender_id UUID NOT NULL
REFERENCES users(id),
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT message_length
CHECK (char_length(text) BETWEEN 1 AND 4000),
CONSTRAINT message_not_blank
CHECK (length(btrim(text)) > 0)
);
CREATE TABLE IF NOT EXISTS sessions (
token_hash CHAR(64) PRIMARY KEY,
user_id UUID NOT NULL
REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMPTZ NOT NULL,
revoked BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX IF NOT EXISTS idx_chat_members_user
ON chat_members(user_id, chat_id);
CREATE INDEX IF NOT EXISTS idx_chats_order
ON chats(created_at, id);
CREATE INDEX IF NOT EXISTS idx_messages_history
ON messages(chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS idx_sessions_user
ON sessions(user_id);
CREATE INDEX IF NOT EXISTS idx_sessions_expiry
ON sessions(expires_at);
+14
View File
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS message_idempotency (
user_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
idempotency_key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (user_id, chat_id, idempotency_key)
);
CREATE INDEX IF NOT EXISTS idx_idempotency_created
ON message_idempotency(created_at);
+34
View File
@@ -0,0 +1,34 @@
CREATE TABLE IF NOT EXISTS attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
uploader_id UUID NOT NULL REFERENCES users(id),
filename TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
sha256 CHAR(64) NOT NULL,
object_key TEXT NOT NULL UNIQUE,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
status TEXT NOT NULL DEFAULT 'ready',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT attachment_size_valid
CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachment_dimensions_valid
CHECK (width > 0 AND height > 0),
CONSTRAINT attachment_status_valid
CHECK (status IN ('ready', 'failed'))
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat
ON attachments(chat_id);
CREATE TABLE IF NOT EXISTS message_attachments (
message_id UUID NOT NULL REFERENCES messages(id),
attachment_id UUID NOT NULL REFERENCES attachments(id),
PRIMARY KEY (message_id, attachment_id)
);
@@ -0,0 +1,13 @@
-- Lab4: messages with attachments may have empty text.
-- The API checks that a message contains text or attachments.
ALTER TABLE messages
DROP CONSTRAINT IF EXISTS message_length;
ALTER TABLE messages
DROP CONSTRAINT IF EXISTS message_not_blank;
ALTER TABLE messages
ADD CONSTRAINT message_length
CHECK (char_length(text) <= 4000);
+95
View File
@@ -0,0 +1,95 @@
-- Allow asynchronous attachment states.
ALTER TABLE attachments
DROP CONSTRAINT IF EXISTS attachment_status_valid;
ALTER TABLE attachments
ADD CONSTRAINT attachment_status_valid
CHECK (
status IN (
'queued',
'processing',
'ready',
'failed'
)
);
-- Staging object stored in the private S3 bucket.
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS staging_key TEXT;
-- Machine-readable final error.
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS error_code TEXT;
-- Last metadata update timestamp.
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS updated_at
TIMESTAMPTZ NOT NULL DEFAULT NOW();
-- Worker jobs and durable publication intent.
CREATE TABLE IF NOT EXISTS attachment_jobs (
id UUID PRIMARY KEY,
attachment_id UUID NOT NULL UNIQUE
REFERENCES attachments(id),
status TEXT NOT NULL DEFAULT 'queued',
attempts INTEGER NOT NULL DEFAULT 0,
max_attempts INTEGER NOT NULL DEFAULT 5,
published_at TIMESTAMPTZ,
started_at TIMESTAMPTZ,
completed_at TIMESTAMPTZ,
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT attachment_job_status_valid
CHECK (
status IN (
'queued',
'processing',
'ready',
'failed'
)
),
CONSTRAINT attachment_job_attempts_valid
CHECK (
attempts >= 0
AND max_attempts BETWEEN 1 AND 20
)
);
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_pending
ON attachment_jobs(status, next_attempt_at);
-- Image variants created by the worker.
CREATE TABLE IF NOT EXISTS attachment_variants (
attachment_id UUID NOT NULL
REFERENCES attachments(id),
variant_name TEXT NOT NULL,
object_key TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
sha256 CHAR(64) NOT NULL,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (attachment_id, variant_name),
CONSTRAINT variant_name_valid
CHECK (variant_name IN ('thumbnail')),
CONSTRAINT variant_size_valid
CHECK (size_bytes > 0),
CONSTRAINT variant_dimensions_valid
CHECK (width > 0 AND height > 0)
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_attachment_variants_key
ON attachment_variants(object_key);