diff --git a/.dockerignore b/.dockerignore index bc16e556..355220e8 100644 --- a/.dockerignore +++ b/.dockerignore @@ -16,5 +16,9 @@ backups .venv __pycache__ node_modules +**/bin +**/obj evidence .DS_Store + +.local diff --git a/.gitignore b/.gitignore index b5a71896..60be5d67 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ certs/private/ *.pem *.tfstate* volumes/ -data/ +/data/ backups/ # Runtime/build artifacts .venv/ @@ -30,3 +30,5 @@ coverage/ .vscode/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. + +/.local/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..162159c5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +FROM mcr.microsoft.com/dotnet/sdk:9.0.306 AS build + +WORKDIR /src + +COPY src/Messenger.Api/Messenger.Api.csproj src/Messenger.Api/ +RUN dotnet restore src/Messenger.Api/Messenger.Api.csproj + +COPY src/Messenger.Api/ src/Messenger.Api/ + +RUN dotnet publish src/Messenger.Api/Messenger.Api.csproj \ + -c Release \ + --no-restore \ + -o /app/publish + + +FROM mcr.microsoft.com/dotnet/aspnet:9.0.10 AS runtime + +RUN apt-get update \ + && apt-get install -y --no-install-recommends curl \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +ARG BUILD_REVISION=local +LABEL org.opencontainers.image.revision=$BUILD_REVISION + +COPY --from=build /app/publish . + +ENV APP_HOST=0.0.0.0 +ENV APP_PORT=8080 +ENV APP_ENV=lab + +EXPOSE 8080 + +USER app + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD curl --fail --silent http://127.0.0.1:8080/health/ready > /dev/null || exit 1 + +ENTRYPOINT ["dotnet", "Messenger.Api.dll"] diff --git a/Makefile b/Makefile index 5d86d171..7944eb1a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -PYTHON ?= python3 -BASE_URL ?= http://localhost:8080 +PYTHON ?= .venv/bin/python +BASE_URL ?= http://localhost:18085 CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) @@ -28,3 +28,49 @@ test-persistence: test-failover: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +.PHONY: setup up down build restore-api stop-worker start-worker replay reconcile +setup: + python3 -m venv .venv + .venv/bin/python -m pip install -r requirements-dev.txt -r requirements-student.txt -r requirements-research.txt + ./scripts/init-secrets.sh +up: + ./scripts/init-secrets.sh + ./scripts/compose.sh build api + ./scripts/compose.sh up -d --wait --wait-timeout 150 +build: + dotnet build src/Messenger.Api +restore-api: + ./scripts/compose.sh up -d --wait api lb +stop-worker: + ./scripts/compose.sh stop worker +start-worker: + ./scripts/compose.sh up -d worker +replay: + @test -n "$(JOB_ID)" || (echo "Set JOB_ID"; exit 2) + ./scripts/compose.sh exec -T dispatcher dotnet Messenger.Api.dll --replay "$(JOB_ID)" +reconcile: + ./scripts/compose.sh exec -T dispatcher dotnet Messenger.Api.dll --reconcile +down: + ./scripts/compose.sh down + +.PHONY: student-test +student-test: + $(PYTHON) student-tests/pipeline_tests.py --base-url "$(BASE_URL)" + +.PHONY: research-grade5 +research-grade5: + $(PYTHON) student-tests/overload_research.py --base-url "$(BASE_URL)" + $(PYTHON) scripts/plot_overload.py + +.PHONY: share stop-share operations-test +share: + python3 scripts/share.py +stop-share: + python3 scripts/share.py --stop +operations-test: + $(PYTHON) student-tests/operations_panel.py + +.PHONY: operator +operator: + python3 scripts/configure-operator.py "$(USERNAME)" diff --git a/REPORT.md b/REPORT.md index e98f75e9..869bee8a 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,305 @@ -# Отчёт — лабораторная 5 +# Лабораторная 5 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +Автор: Каменский Никита, БВТ-2302. +Целевая оценка: 5. Трек 1: очередь под перегрузкой. +PR: [ЛР 5 - БВТ-2302 - Каменский - на 5](https://gitea.privcloud.tech/mtusi2026/mtusi2026lab5/pulls/38). -## Запуск из чистого clone +## Запуск -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны Docker Desktop, Python 3.9+ и доступ в интернет для сборки. +Для сборки вне Docker нужен .NET SDK 9. -## Архитектура и решения +```sh +make setup +make up +make check +make validate +make test +make student-test +make research-grade5 +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +make restore-api +``` -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Сайт: http://localhost:18085/. Lab5 использует отдельную базу и volumes. +Для входа сначала зарегистрировать аккаунт с паролем 12–128 символов. +Остановка с сохранением данных: make down. +Намеренное удаление учебных данных: ./scripts/compose.sh down -v. +Секреты создаёт scripts/init-secrets.py в исключённой папке secrets/. +Повторный setup сохраняет существующие ключи. Общий volume RabbitMQ закреплён +за hostname broker; миграции запускаются отдельно с блокировкой PostgreSQL. -## Доказательства критериев +Версии: .NET SDK 9.0.306/runtime 9.0.10, PostgreSQL 17.6, Redis 7.4.2, +HAProxy 3.2.7-alpine, SeaweedFS 3.97, RabbitMQ 4.2.0-alpine, +RabbitMQ.Client 7.1.2, AWSSDK.S3 4.0.0, SkiaSharp 3.119.1. +Pillow 12.0.0 используется только для тестовых изображений и проверки результатов. -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Изолированная сборка из копии исходников без .env, secrets, bin/obj и старых +volumes проверена отдельно: evidence/clean-build.txt. Созданы новые secrets, +проверен Compose config и собран Docker-образ. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +## Архитектура -## Эксперименты и отказы +Перенесены собственные исходники и интерфейс Lab4, Dockerfile, конфигурация +HAProxy и операционные скрипты. Skeleton, contracts и публичные tests взяты +из Lab5. Сопоставление архитектуры: docs/ARCHITECTURE.md. -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Две API за HAProxy обслуживают общий PostgreSQL, Redis и приватный S3. +Добавлены отдельные RabbitMQ, worker и outbox dispatcher. Один C# образ +используется для нескольких ролей, но worker и API являются разными процессами. +Интерфейс опрашивает метаданные; queued/processing показываются пользователю, +после ready работает прежняя галерея с оригиналами. В браузере проверен переход +при остановленном и затем запущенном worker: evidence/ui-queued.jpg и ui-ready.jpg. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +## Границы сохранности -## Ограничения и следующие шаги +API полностью принимает PNG/JPEG до 10 MiB, проверяет MIME, имя и максимум +40 млн пикселей. Проверка декодируемости остаётся на API для совместимости +с ошибкой 415; resize выполняет только worker. Копирование ограничено буфером +64 KiB и временным файлом. Один upload/decoder на API, deadline 90 секунд. -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Под блокировкой приёма проверяется количество queued/processing jobs. Если +лимит PIPELINE_CAPACITY достигнут, возвращается 429 и Retry-After: 3, без +записи нового объекта. По умолчанию лимит 128. Входящие HTTP-передачи и +валидация занимают ограниченные ресурсы до этой проверки; лимит защищает +принятую работу, а не означает отсутствие стоимости отклонённых запросов. + +Полный оригинал сохраняется в приватный staging/. В одной +транзакции PostgreSQL записываются Attachment queued, job и outbox. Только +после S3 PUT и COMMIT API возвращает снимок 202 queued. Даже если worker уже +успел завершиться, ответ upload остаётся queued по контракту. Broker и worker +не входят в readiness API: при их отказе работа принимается до лимита outbox. +PostgreSQL и S3 остаются критичными для синхронного приёма. + +Outbox dispatcher выбирает доступную запись FOR UPDATE SKIP LOCKED, отправляет +persistent сообщение в durable quorum queue attachments.v1 с mandatory и +publisher confirms. Отметка published_at записывается только после confirm. +Сбой между publish и COMMIT приводит к дублю, но не теряет намерение. +Брокер получает только поля jobs.schema.json, без байтов и credentials. +Job ID сохраняется при управляемых повторах и redelivery. Реальный carrier +проверен по jobs.schema.json с FormatChecker: evidence/job-envelope.json. + +Worker получает job с ручным ack и проверяет состояние в PostgreSQL. +Для job используется session advisory lock; параллельный дубль не начинает +второй эффект. queued атомарно становится processing с attempt и lease_token. +Работа скачивается из доверенного staging_key в БД, проверяются размер и SHA-256. +Клиентский filename не используется как путь или ключ. + +Имена результатов определены attachment_id/pipeline_version/variant. +Оригинал: originals//v1; варианты: variants//v1/. Оригинальные +байты не перекодируются. После записи всех объектов одна транзакция публикует +метаданные вариантов и ready. Фиксация проверяет lease_token и срок аренды. +Ack выполняется после устойчивого результата или устойчивого решения о retry/failed. +Дубль ready не меняет completed_at, attempts или варианты. + +## Изображения и ограничения worker + +Thumbnail: JPEG 85, вписан в 256×256 без увеличения. Crop: центральный квадрат +128×128, для маленьких изображений возможно увеличение. Optimized: WebP 80, +вписан в 1280×1280 без увеличения. Размеры округляются вниз с минимумом 1 px. +EXIF orientation применяется перед расчётом размеров и crop. Alpha компонуется +на белый. Перекодирование из bitmap удаляет EXIF. Для fixture 320×200 thumbnail +имеет 256×160. Не гарантируется, что WebP каждого маленького файла меньше PNG. + +Worker повторно проверяет bytes/checksum, заголовок, пиксели и декодирование. +Нативное преобразование идёт в дочернем процессе, который принудительно +останавливается по JOB_TIMEOUT_SECONDS, по умолчанию 30. Аренда дольше на 5 s. +Один decoder хранит RGBA bitmap до 160 млн байт и небольшие выходные bitmap. +Варианты формируются по очереди. Общие CPU/RAM worker ограничены Docker: +1 CPU, 1024 MiB. Время/память ограничены и при ошибках нативной библиотеки. +Переменные WORKER_CONCURRENCY и WORKER_PREFETCH по умолчанию 2 и 1; +каждый канал обрабатывает одно сообщение одновременно, prefetch ограничивает +число полученных, но ещё не подтверждённых сообщений на канале. + +## Ошибки, повторы и replay + +Невалидный или потерянный staging становится failed с безопасным invalid_image. +Временные ошибки S3/БД/таймаута приводят к queued и следующей попытке. +Backoff: min(30, 2^attempt) секунд плюс случайный jitter [0,1). +По умолчанию JOB_MAX_ATTEMPTS=5, затем failed с retry_exhausted. +Реестр failed находится в attachment_jobs, журнал в attachment_job_events. +Секреты, внутренние адреса и stack trace не выдаются клиенту. + +```sh +make replay JOB_ID= +``` + +Replay разрешён только для failed с ещё сохранённым staging. Он открывает +новый цикл попыток, увеличивает replay_count и сохраняет audit-событие; +идентификатор логического задания и ключ результата остаются прежними. +Replay уже ready является no-op. Контроль доступа к replay осуществляется +операторским доступом к Docker, публичного HTTP endpoint replay нет. + +После гибели worker просроченное processing восстанавливается dispatcher. +Прежде чем менять его, проверяется advisory lock: работа живого владельца +не захватывается вторым worker. Lease ограничивает восстановление зависшего +состояния; нативный subprocess имеет отдельный принудительный timeout. +Потерянная доставка queued восстанавливается из сохранённого намерения по +истечении 30 s. Это at-least-once доставка с идемпотентным эффектом, не exactly-once. + +## Очистка staging + +После ready dispatcher удаляет staging только под блокировкой job и после +проверки устойчивого завершения. Отказ между DELETE и отметкой повторяется +безопасно. Failed staging сохраняется 86400 секунд для replay; минимум +retention 3600. Текущие queued/processing не очищаются. + +```sh +make reconcile +./scripts/compose.sh exec -T dispatcher dotnet Messenger.Api.dll --reconcile --apply +``` + +Сверка untracked staging имеет grace 1 час и общую блокировку с приёмом upload. +Она не удаляет staging, известный job, в том числе текущую работу. Это закрывает +сбой S3 PUT до COMMIT приёма. Автоматически копировать отсутствующие байты +без резервной копии невозможно; такое задание получает failed. + +## Проверки + +| Критерий | Сценарий | Доказательство | +| --- | --- | --- | +| Весь обязательный HTTP, 202, оригинал, thumbnail | make test | evidence/checks.txt | +| Worker остановлен, API и broker перезапущены | make student-test | evidence/pipeline.json | +| Сохранность опубликованного задания в broker volume | make student-test | evidence/pipeline.json | +| Crop/WebP, EXIF orientation, alpha, отсутствие EXIF | make student-test | evidence/pipeline.json | +| Bytes/pixels/MIME/filename и worker pixel guard | make student-test | evidence/pipeline.json | +| COMMIT без publish, confirm без отметки outbox | make student-test | evidence/pipeline.json | +| Повтор готовой job, crash после S3 и после COMMIT до ack | make student-test | evidence/pipeline.json | +| Ограниченные retries, failed, явный replay и no-op ready | make student-test | evidence/pipeline.json | +| Очистка готового staging и retention failed | make student-test | evidence/pipeline.json | +| Три настройки, два размера, lag/p95/RSS | make research-grade5 | evidence/overload.json, docs/OVERLOAD.md | +| Backpressure и восстановление принятой работы | make research-grade5 | evidence/overload.json | +| Сессия и оригинал после restart всех хранилищ | make test-persistence | evidence/persistence.txt | +| Переключение API и общая идемпотентность | make test-failover | evidence/failover.txt | + +Fault injection включается только оператором через PIPELINE_FAULT_FILE для +worker/dispatcher. Локальный файл содержит выбранный job ID и точку остановки; +он одноразовый. Публичный клиент не может включить паузу. Тест ждёт marker, +убивает настоящий контейнер и проверяет восстановление. По умолчанию путь +пустой и инструментация не исполняется. Тесты создают только синтетические данные. + +## Исследование + +Гипотеза: при одинаковых CPU/RAM рост concurrency может повысить расход памяти +без пропорционального роста скорости. Методика: 1/1, 2/2, 4/4 для concurrency / +prefetch; два размера PNG; три повтора каждой точки. Порядок меняется между +повторами. Каждая точка прогревается одним изображением. Затем batch 8 файлов, +четыре HTTP-клиента, ожидание ready всех восьми. Метрики и ограничения измерения +описаны в docs/OVERLOAD.md, индивидуальные job/HTTP и выборки в overload.json. + +Отдельная серия останавливает worker, устанавливает лимит 4 и отправляет +8 файлов одновременно. Проверяются ровно 4 принятых и 4 отклонённых с 429, +непревышение лимита в БД и последующая готовность всех принятых после запуска. +Для крупных PNG медиана throughput составила 0.808, 0.951 и 0.813 готовых/с +для профилей 1/1, 2/2 и 4/4. Их медианы p95 готовности: 7.693, 6.520 и +8.308 s; RSS процессов worker: 189.7, 268.6 и 378.8 MiB. У маленьких PNG +лучше оказался 1/1. Рост concurrency не оказался универсальным ускорением. +Профили меняют concurrency и prefetch вместе; отдельный эффект каждого параметра +из этой серии не выводится. По умолчанию оставлено 2/1 как ограниченный +рабочий вариант; этот профиль отдельно не измерен и не объявляется оптимальным. + +Пилотная серия выявила нехватку свободных томов S3 при настройке volume.max=8, +перенесённой из маленького стенда Lab4. До окончательных измерений лимит +поднят до 64; volumes сохранены, неудавшиеся пилотные задания восстановлены +явным replay. Пилотные результаты отделены от основной серии и не включаются +в её статистику. Это изменение ёмкости хранилища, не результат ускорения worker. + +Отдельный запуск публичного smoke во время намеренного рестарта API получил +503; этот вывод сохранён в evidence/checks-during-restart.txt. Финальные проверки +на готовом стенде записаны в evidence/checks.txt. + +## Ограничения и настройки + +Один Docker-host и единственные PostgreSQL, S3, RabbitMQ и HAProxy являются +точками отказа. Несколько API и durable queue не защищают от потери host/диска. +Для исходников и broker volume требуются резервные копии. Внешний доступ +и настройка туннеля не входят в Lab5; наружу опубликован только localhost:18085. + +Из .env.example используются APP_HOST/PORT, SESSION_TTL_SECONDS, API_REPLICAS, +WORKER_CONCURRENCY, JOB_MAX_ATTEMPTS, JOB_TIMEOUT_SECONDS и STAGING_RETENTION_SECONDS. +BROKER_URL_FILE сопоставлен BROKER_HOST и BROKER_PASSWORD_FILE. +S3_ACCESS_KEY_ID_FILE/S3_SECRET_ACCESS_KEY_FILE сопоставлены S3_ACCESS_KEY_FILE/ +S3_SECRET_KEY_FILE. Endpoint S3 http://s3:8333, регион us-east-1, bucket messenger. +Redis остаётся кэшем, PostgreSQL источником сессий. Byte/pixel/Idempotency TTL +фиксированы контрактом в коде. Отдельно реализованы WORKER_PREFETCH и PIPELINE_CAPACITY. + +Источники: контракт Lab5; официальная документация RabbitMQ publisher confirms, +consumer acknowledgements и .NET Client API Guide; AWS SDK for .NET; SkiaSharp. +ChatGPT использован при реализации и проверках. + +## Панель обработки изображений + +Кнопка «Обработка» под изображением открывает состояния, число попыток, время всего цикла и события задания из PostgreSQL. После готовности доступны оригинал, миниатюра, crop и WebP с размерами и скачиванием. Endpoint GET /api/v1/attachments/{id}/processing доступен только участникам чата; ключи S3 и служебные lease не возвращаются. Панель обновляется только пока открыта и задание выполняется. Проверка: .venv/bin/python student-tests/processing_panel.py. Результаты нагрузочного эксперимента выше относятся к версии до добавления этого интерфейса. + +## Панель системы и внешний доступ + +Кнопка «Система» доступна только аккаунту, UUID которого задан в OPS_USER_ID. +После регистрации владельца: `make operator USERNAME=ваш_логин`. Команда записывает +UUID в локальный .env и обновляет API. Если OPS_USER_ID не настроен, панель закрыта +для всех. Клиентское скрытие кнопки дополнено проверкой каждой серверной операции. + +Панель показывает две API-реплики по heartbeat Redis, PostgreSQL, Redis, S3, +RabbitMQ, worker и dispatcher. Heartbeat процессов хранится в pipeline_presence; +срок свежести 10 секунд. Это признак работающего процесса, а не гарантия успешной +обработки любого задания. Проверка HAProxy основана на ответе текущего адреса. +Графики содержат последние 60 замеров открытой панели: активные задания в БД +и длительность запроса проверки зависимостей. CPU и память здесь не измеряются. +При ошибке запроса старые состояния отмечаются как неизвестные. + +Последние 30 заданий и событий доступны владельцу. Кнопка «Повторить» использует +существующий механизм replay: только failed с сохранённым staging. Проверка состояния +и изменение задания выполняются в одной транзакции под advisory lock; второй +запрос не создаёт ещё один replay. Обычный пользователь получает 404, анонимный 401. +Проверка: `OPS_TEST_PASSWORD=пароль_владельца make operations-test` (логин по умолчанию +n1k0der, другой задаётся OPS_TEST_USERNAME). Тест использует контролируемый failed +fixture и проверяет завершение настоящим worker, восстанавливая worker в finally. + +Создать чат можно по @логину или UUID. Поиск выполняется только по точному логину, +после входа; возвращает публичный User DTO без password_hash. + +Временный внешний доступ: установить cloudflared, затем `make share`. +Отключение: `make stop-share`. Ссылка и состояние процесса остаются в .local вне Git. +Ссылка работает, пока включён компьютер, запущены Docker и туннель; при следующем +запуске адрес может измениться. Снаружи доступен только HTTP-вход мессенджера, +порты БД и RabbitMQ не публикуются. Собеседник регистрирует свой аккаунт и создаёт +чат с @логином владельца. Учётная запись собеседника не получает права оператора. + +Через HTTPS-туннель пустой POST входа может приходить с Transfer-Encoding: chunked. +API проверяет фактическое отсутствие тела (с таймаутом 3 секунды), а не отклоняет +такой способ передачи целиком. Непустое тело по-прежнему возвращает 400. +Проверка: `.venv/bin/python student-tests/chunked_login.py`. +Внешняя регистрация, вход, точный поиск, создание общего чата и сообщение проверены +через публичный HTTPS-адрес; результат без токенов записан в evidence/operations.txt. + +## Оформление интерфейса + +Общие стили мессенджера находятся в wwwroot/design.css, панель системы в +operations.css и operations.js. Добавлены 16 SVG на общей сетке и спрайт, +две новые иллюстрации персонажа в WebP с прозрачностью. Использование графики +описано в docs/ASSETS.md. Изображения загружаются в размерах 320/640, без +исходных PNG в webroot. На ширине 1366 и 390 проверены отсутствие горизонтальной +прокрутки, загрузка иллюстраций, навигация панели и доступность поля сообщения. +В браузере проверены реальные состояния восьми карточек API/зависимостей. +SVG разобраны XML-парсером, все новые asset URL отвечают 200. make check, +make validate и публичный smoke-сценарий прошли после изменений. + +## Удобство и устойчивость интерфейса + +В чате добавлены копирование текста, вставка PNG/JPEG из буфера и перенос файла +мышью. Поиск чатов открывается сочетанием ⌘K/Ctrl+K, результаты можно очистить +кнопкой или Escape. В панели задания фильтруются среди последних 30 записей; +состояние кнопки повтора не перерисовывается без изменения данных. При смене +пользователя содержимое панели удаляется из страницы. Анимации уважают настройку +уменьшения движения. + +Формы входа и регистрации ожидают загрузки скриптов; их резервный метод POST не +помещает поля в адрес страницы. HAProxy проверяет API с тайм-аутом 2 секунды и +интервалом 1 секунда. Docker healthcheck RabbitMQ проверяет TCP-порт 5672 без +запуска Erlang-процесса на каждый опрос; проверка панели дополнительно выполняет +AMQP-запрос. Лимит RabbitMQ по умолчанию 512 МиБ, настраивается BROKER_MEMORY. +При прежней проверке регулярные Erlang-процессы исчерпывали лимит при запуске. + +Результаты повторных проверок и переключения API: evidence/ui-reliability.txt. +Предыдущие измерения нагрузки относятся к настройкам и версии, указанным в их +исходных результатах; повторный эксперимент для новых параметров не проводился. diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..0e417f01 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,263 @@ +name: mtusi2026lab5 + +x-api: &api + image: messenger-lab5:local + build: . + environment: + OPS_USER_ID: ${OPS_USER_ID:-} + BROKER_HOST: broker + BROKER_PASSWORD_FILE: /run/secrets/broker_password + APP_HOST: 0.0.0.0 + APP_PORT: 8080 + DATABASE_URL_FILE: /run/secrets/database_url + REDIS_URL: redis:6379 + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + DB_POOL_SIZE: ${DB_POOL_SIZE:-20} + S3_ENDPOINT: http://s3:8333 + S3_BUCKET: messenger + S3_ACCESS_KEY_FILE: /run/secrets/s3_access + S3_SECRET_KEY_FILE: /run/secrets/s3_secret + PIPELINE_CAPACITY: ${PIPELINE_CAPACITY:-128} + MESSAGE_BATCHING: ${MESSAGE_BATCHING:-true} + Logging__LogLevel__Default: Warning + Logging__LogLevel__Microsoft.Hosting.Lifetime: Information + secrets: + - broker_password + - database_url + - s3_access + - s3_secret + cpus: ${API_CPUS:-1} + mem_limit: ${API_MEMORY:-512m} + stop_grace_period: 10s + healthcheck: + test: ["CMD", "curl", "--fail", "--silent", "--max-time", "1", "http://127.0.0.1:8080/health/ready"] + interval: 2s + timeout: 2s + retries: 5 + start_period: 5s + restart: unless-stopped + depends_on: + s3-bootstrap: + condition: service_completed_successfully + migrate: + condition: service_completed_successfully + redis: + condition: service_healthy + +services: + postgres: + image: postgres:17.6 + environment: + POSTGRES_DB: messenger + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + cpus: ${POSTGRES_CPUS:-0.5} + mem_limit: 512m + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 2s + timeout: 2s + retries: 30 + restart: unless-stopped + + redis: + image: redis:7.4.2-alpine + command: ["redis-server", "--save", "", "--appendonly", "no", "--maxmemory", "64mb", "--maxmemory-policy", "allkeys-lru"] + cpus: 0.25 + mem_limit: 96m + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 2s + timeout: 2s + retries: 15 + restart: unless-stopped + + migrate: + image: messenger-lab5:local + command: ["--migrate"] + environment: + DATABASE_URL_FILE: /run/secrets/database_url + secrets: + - database_url + cpus: 0.5 + mem_limit: 256m + restart: "no" + healthcheck: + disable: true + depends_on: + postgres: + condition: service_healthy + + s3: + image: chrislusf/seaweedfs:3.97 + command: ["server", "-dir=/data", "-ip=s3", "-master.volumeSizeLimitMB=100", "-volume.max=64", "-filer", "-s3", "-s3.config=/run/secrets/s3_config"] + secrets: + - s3_config + volumes: + - s3_data:/data + cpus: 0.5 + mem_limit: 512m + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "wget -S -O /dev/null http://127.0.0.1:8333/ 2>&1 | grep -q '403'"] + interval: 2s + timeout: 2s + retries: 40 + start_period: 35s + + s3-bootstrap: + image: messenger-lab5:local + command: ["--bootstrap"] + environment: + S3_ENDPOINT: http://s3:8333 + S3_ACCESS_KEY_FILE: /run/secrets/s3_admin_access + S3_SECRET_KEY_FILE: /run/secrets/s3_admin_secret + secrets: + - s3_admin_access + - s3_admin_secret + depends_on: + s3: + condition: service_healthy + restart: on-failure + healthcheck: + disable: true + ulimits: + core: 0 + cpus: 0.25 + mem_limit: 128m + + broker: + image: rabbitmq:4.2.0-alpine + hostname: broker + environment: + RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: "+S 2:2 +sbwt none" + secrets: + - source: broker_config + target: /etc/rabbitmq/conf.d/20-messenger.conf + volumes: + - broker_data:/var/lib/rabbitmq + cpus: 0.5 + mem_limit: ${BROKER_MEMORY:-512m} + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "bash -c 'exec 3<>/dev/tcp/127.0.0.1/5672'"] + interval: 3s + timeout: 5s + retries: 30 + start_period: 15s + + dispatcher: + image: messenger-lab5:local + command: ["--dispatcher"] + environment: &pipeline-env + DATABASE_URL_FILE: /run/secrets/database_url + S3_ENDPOINT: http://s3:8333 + S3_BUCKET: messenger + S3_ACCESS_KEY_FILE: /run/secrets/s3_access + S3_SECRET_KEY_FILE: /run/secrets/s3_secret + BROKER_HOST: broker + BROKER_PASSWORD_FILE: /run/secrets/broker_password + PIPELINE_FAULT_FILE: ${PIPELINE_FAULT_FILE:-} + JOB_MAX_ATTEMPTS: ${JOB_MAX_ATTEMPTS:-5} + JOB_TIMEOUT_SECONDS: ${JOB_TIMEOUT_SECONDS:-30} + STAGING_RETENTION_SECONDS: ${STAGING_RETENTION_SECONDS:-86400} + volumes: + - ./.local/faults:/run/faults + secrets: &pipeline-secrets + - database_url + - s3_access + - s3_secret + - broker_password + depends_on: + migrate: + condition: service_completed_successfully + s3-bootstrap: + condition: service_completed_successfully + broker: + condition: service_healthy + healthcheck: + disable: true + cpus: 0.25 + mem_limit: 192m + restart: unless-stopped + stop_grace_period: 10s + + worker: + image: messenger-lab5:local + command: ["--worker"] + environment: + <<: *pipeline-env + WORKER_CONCURRENCY: ${WORKER_CONCURRENCY:-2} + WORKER_PREFETCH: ${WORKER_PREFETCH:-1} + volumes: + - ./.local/faults:/run/faults + secrets: *pipeline-secrets + depends_on: + migrate: + condition: service_completed_successfully + s3-bootstrap: + condition: service_completed_successfully + broker: + condition: service_healthy + healthcheck: + disable: true + cpus: 1 + mem_limit: 1024m + restart: unless-stopped + stop_grace_period: 10s + ulimits: + core: 0 + + api: + <<: *api + deploy: + replicas: ${API_REPLICAS:-2} + + lb: + image: haproxy:3.2.7-alpine + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + ports: + - "127.0.0.1:${API_PORT:-18085}:8080" + cpus: 0.5 + mem_limit: 96m + stop_grace_period: 10s + healthcheck: + test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:8080/health/ready"] + interval: 2s + timeout: 2s + retries: 30 + start_period: 5s + restart: unless-stopped + depends_on: + api: + condition: service_healthy + +secrets: + broker_config: + file: ./secrets/broker.conf + broker_password: + file: ./secrets/broker_password + s3_config: + file: ./secrets/s3_config + s3_admin_access: + file: ./secrets/s3_admin_access + s3_admin_secret: + file: ./secrets/s3_admin_secret + s3_access: + file: ./secrets/s3_access + s3_secret: + file: ./secrets/s3_secret + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + +volumes: + broker_data: + s3_data: + postgres_data: diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md new file mode 100644 index 00000000..01e6b083 --- /dev/null +++ b/docs/ARCHITECTURE.md @@ -0,0 +1,38 @@ +# Переход Lab4 → Lab5 + +| Часть | Lab4 | Lab5 | +| --- | --- | --- | +| Приём файла | S3 PUT и 201 ready | Приватный staging, метаданные, job и outbox, затем 202 queued | +| Преобразования | Нет | Worker: JPEG thumbnail, JPEG crop, WebP optimized | +| Брокер | Нет | RabbitMQ, durable quorum queue, persistent messages, publisher confirms | +| Надёжность публикации | Не требуется | OutboxDispatcher читает PostgreSQL и публикует с подтверждением | +| Повторная доставка | Только отправка сообщений | Дополнительно job_id, состояние, advisory lock, lease, детерминированные S3 keys | +| Ошибки | Синхронный HTTP | Ограниченные попытки, backoff/jitter, failed registry и явный replay | +| Интерфейс | Превью готового оригинала | Опрос queued/processing, затем прежняя галерея и скачивание | + +## Процессы + +Один образ C# запускается в нескольких ролях. API обрабатывает HTTP. Worker +запускается с --worker, dispatcher с --dispatcher. Это отдельные контейнеры +и процессы. Миграции и создание bucket выполняются одноразовыми сервисами. +Worker запускает короткоживущий дочерний процесс --render-image для SkiaSharp: +его можно принудительно остановить при превышении времени декодирования. + +PostgreSQL хранит метаданные, состояния, аренду работы, outbox и журнал событий. +SeaweedFS хранит staging, оригинал и варианты. Redis сохраняет прежнюю роль +кэша сессий. RabbitMQ переносит только идентификаторы и служебные поля. + +## Файлы + +- Services/AttachmentService.cs: приём, проверка, лимит очереди, staging и транзакция приёма. +- Data/Migrations/005_pipeline.sql: jobs, outbox, журнал, новые статусы и варианты. +- Pipeline/OutboxDispatcher.cs: надёжная публикация в RabbitMQ. +- Pipeline/PipelineWorker.cs: получение, ограниченная обработка, запись S3 и результата. +- Pipeline/ImageRenderer.cs: EXIF orientation, resize, crop, JPEG/WebP. +- Pipeline/JobStore.cs: захват работы, lease, повтор, восстановление и replay. +- Pipeline/PipelineMaintenance.cs: очистка staging с проверкой состояния. +- Pipeline/FaultInjection.cs: выключенные по умолчанию локальные точки остановки для тестов. +- wwwroot/attachments.js: ожидание обработки и просмотр изображения. + +Program.cs выбирает режим процесса и подключает зависимости. HTTP-контракт, +авторизация, доступ к чатам и идемпотентность сообщений продолжают работать. diff --git a/docs/ASSETS.md b/docs/ASSETS.md new file mode 100644 index 00000000..4ca88be6 --- /dev/null +++ b/docs/ASSETS.md @@ -0,0 +1,41 @@ +# Графика интерфейса + +Набор: src/Messenger.Api/wwwroot/assets/. + +## SVG + +16 отдельных файлов в icons/: api, database, queue, storage, worker, dispatcher, +cache, proxy, browser, chat, image, activity, check, clock, error, overview. +Размерная сетка 48×48, обводка 2, скруглённые окончания, прозрачный фон. +Для обычной картинки: `База данных`. +Для управления цветом через CSS используйте спрайт: + +```html + +``` + +Файл system-icons.svg должен находиться на том же origin. В другом проекте +измените путь к спрайту. Для кнопки задайте текст или aria-label; декоративному +SVG оставьте aria-hidden. Исходные SVG не зависят от JavaScript или библиотеки. +conversation-grid.svg используется как малоконтрастный фон переписки. + +## Иллюстрации + +kolovertysh-message-320.webp / kolovertysh-message-640.webp: пустой экран чатов +и боковая панель системы. kolovertysh-reconnect-320.webp: ошибка подключения. +Все изображения содержат alpha; прозрачность сохранена при экспорте. +Созданы с помощью ChatGPT по существующему образу персонажа. Для UI используются +уменьшенные WebP, исходные PNG оставлены локально вне webroot. + +## Цвета и движение + +Основной фон #262626, акцент #FFE135, основной текст #EEEEE2. +Рабочие поверхности #282A25 и #30312C, вторичный текст #A8AC9B. +Состояния: зелёный #A6CF9A, ошибка #EEB09F. +Анимация точек между узлами запускается после реального ответа панели. +Выбранный API определяется по X-Instance-Id. При prefers-reduced-motion +переходы, появление окон и движение точек отключены. Графики строят прямые +отрезки между измерениями, без сглаживания или искусственных данных. diff --git a/docs/OVERLOAD.md b/docs/OVERLOAD.md new file mode 100644 index 00000000..b95cc30e --- /dev/null +++ b/docs/OVERLOAD.md @@ -0,0 +1,35 @@ +# Очередь под нагрузкой + +Три повтора; одинаковые CPU/RAM worker; batch 8, четыре HTTP-клиента. + +| Файл | Concurrency / prefetch | Готовых/с, медиана | p95 до ready, s | p95 ожидания, s | RSS процессов, MiB | +| --- | --- | ---: | ---: | ---: | ---: | +| small | 1/1 | 4.411 | 1.524 | 1.291 | 157.6 | +| small | 2/2 | 3.625 | 1.675 | 1.319 | 165.2 | +| small | 4/4 | 3.904 | 1.788 | 1.052 | 281.9 | +| large | 1/1 | 0.808 | 7.693 | 6.868 | 189.7 | +| large | 2/2 | 0.951 | 6.520 | 4.749 | 268.6 | +| large | 4/4 | 0.813 | 8.308 | 4.477 | 378.8 | + +p95: nearest rank; при batch 8 это максимальная задержка. RSS: сумма VmRSS +процессов dotnet внутри worker, включая дочерние декодеры; общие страницы могут +считаться несколько раз. memory.current измерен отдельно. Период выборок 200 ms +плюс docker exec; короткие пики могут быть пропущены. Время включает загрузку +batch и ожидание последнего ready, без регистрации и прогрева. Queue lag: от +создания записи задания до первой processing. Время до ready: по часам PostgreSQL. + +Backpressure: при остановленном worker лимит 4 принял ровно 4 из 8 файлов; +остальные получили 429. После запуска обработались все четыре принятых задания. + +Исходные результаты: evidence/overload.json. Один Docker-host, небольшие batch, +параллельно могут работать другие локальные приложения. Эти значения не являются +пределом производительности сервера. Вывод сравнения относится к этому профилю. + +![Результаты](../evidence/overload.png) + +В этом профиле для small один обработчик оказался быстрее двух и четырёх. +Для large вариант 2/2 дал лучшую медиану throughput и меньшую p95 готовности, +чем 1/1, при большем RSS. Четыре обработчика уменьшили время ожидания начала, +но увеличили полное время обработки и память. По умолчанию оставлены 2/1: +два параллельных обработчика с небольшим prefetch. Пара 2/1 отдельно не входит +в сравнение, поэтому не заявляется как измеренный оптимум. diff --git a/docs/PR.md b/docs/PR.md new file mode 100644 index 00000000..b6dd7efe --- /dev/null +++ b/docs/PR.md @@ -0,0 +1,23 @@ +ЛР 5 - БВТ-2302 - Каменский - на 5 + +Автор / группа: Каменский Никита, БВТ-2302. +Целевая оценка: 5. Трек 1: очередь под перегрузкой. + +Из Lab4 перенесены собственные API, PostgreSQL, Redis, S3, HAProxy и интерфейс. +Загрузка теперь возвращает 202 queued после сохранения staging и транзакции +Attachment/job/outbox. Отдельные RabbitMQ, dispatcher и worker выполняют +доставку и обработку: thumbnail JPEG, crop JPEG, optimized WebP. + +Добавлены ограниченные retries с backoff/jitter, failed registry, replay, +lease recovery и очистка staging после устойчивого результата. Дубли не +создают второй набор вариантов. UI показывает ожидание и обработку изображения. + +Чистый запуск: make setup, make up. Проверки: make check, make validate, +make test, make student-test, make research-grade5; persistence/failover +по REPORT.md. Результаты: evidence/, docs/OVERLOAD.md и REPORT.md. + +Публичный контракт, tests и README задания сохранены. Один Docker-host, +единственные БД, S3, RabbitMQ и HAProxy остаются точками отказа. Измерения +получены на локальном стенде с ограниченными ресурсами и небольшими batch. + +ChatGPT использован при реализации и проверках. diff --git a/evidence/checks-during-restart.txt b/evidence/checks-during-restart.txt new file mode 100644 index 00000000..4ca61bd2 --- /dev/null +++ b/evidence/checks-during-restart.txt @@ -0,0 +1,9 @@ +.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. +.venv/bin/python tests/smoke.py --base-url "http://localhost:18085" +FAIL: GET /api/v1/attachments/bb21267f-b7da-491b-96b3-7f55f8c3d884: expected [200], received 503 +make: *** [test] Error 1 +.venv/bin/python tests/smoke.py --base-url "http://localhost:18085" +PASS: lab5 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/checks.txt b/evidence/checks.txt new file mode 100644 index 00000000..e8ed6760 --- /dev/null +++ b/evidence/checks.txt @@ -0,0 +1,6 @@ +.venv/bin/python scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +.venv/bin/python scripts/validate_openapi.py +PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested. +.venv/bin/python tests/smoke.py --base-url "http://localhost:18085" +PASS: lab5 public API scenario; infrastructure and grading evidence remain separate. diff --git a/evidence/clean-build.txt b/evidence/clean-build.txt new file mode 100644 index 00000000..e38e5589 --- /dev/null +++ b/evidence/clean-build.txt @@ -0,0 +1,67 @@ +Local secrets ready +#0 building with "desktop-linux" instance using docker driver + +#1 [internal] load build definition from Dockerfile +#1 transferring dockerfile: 969B done +#1 DONE 0.0s + +#2 [internal] load metadata for mcr.microsoft.com/dotnet/sdk:9.0.306 +#2 DONE 0.6s + +#3 [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:9.0.10 +#3 DONE 0.6s + +#4 [internal] load .dockerignore +#4 transferring context: 218B done +#4 DONE 0.0s + +#5 [build 1/6] FROM mcr.microsoft.com/dotnet/sdk:9.0.306@sha256:81f6d622fe21ed9d31375167f62a3538ff4d6835f9d5e6da9c2defa8a84b7687 +#5 resolve mcr.microsoft.com/dotnet/sdk:9.0.306@sha256:81f6d622fe21ed9d31375167f62a3538ff4d6835f9d5e6da9c2defa8a84b7687 0.0s done +#5 DONE 0.0s + +#6 [runtime 1/4] FROM mcr.microsoft.com/dotnet/aspnet:9.0.10@sha256:3dcb33395722d14c80d19107158293ed677b2c07841100d51df07275ae2b2682 +#6 resolve mcr.microsoft.com/dotnet/aspnet:9.0.10@sha256:3dcb33395722d14c80d19107158293ed677b2c07841100d51df07275ae2b2682 0.0s done +#6 DONE 0.0s + +#7 [internal] load build context +#7 transferring context: 420.67kB 0.0s done +#7 DONE 0.0s + +#8 [build 3/6] COPY src/Messenger.Api/Messenger.Api.csproj src/Messenger.Api/ +#8 CACHED + +#9 [runtime 2/4] RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/* +#9 CACHED + +#10 [runtime 3/4] WORKDIR /app +#10 CACHED + +#11 [build 4/6] RUN dotnet restore src/Messenger.Api/Messenger.Api.csproj +#11 CACHED + +#12 [build 6/6] RUN dotnet publish src/Messenger.Api/Messenger.Api.csproj -c Release --no-restore -o /app/publish +#12 CACHED + +#13 [build 2/6] WORKDIR /src +#13 CACHED + +#14 [build 5/6] COPY src/Messenger.Api/ src/Messenger.Api/ +#14 CACHED + +#15 [runtime 4/4] COPY --from=build /app/publish . +#15 CACHED + +#16 exporting to image +#16 exporting layers done +#16 exporting manifest sha256:8032c23ca82abca0a98189e25912a472fa9f9766aaadfb1dc72e3a8ffd87797d done +#16 exporting config sha256:6d5f902dbe672c56753a47a14e780cedd15d0a6134b91f7e1bfcb0da75d784e6 0.0s done +#16 exporting attestation manifest sha256:1c91c52f053bf7e2022c725dfdc1906fc181127ae049e472f4f61cbc5255c452 0.0s done +#16 exporting manifest list sha256:bc7b3964de3dcbf7f81a4313518695817ae781561b99f22cf02e8114b41271ba 0.0s done +#16 naming to docker.io/library/messenger-lab5:clean-check done +#16 unpacking to docker.io/library/messenger-lab5:clean-check +#16 unpacking to docker.io/library/messenger-lab5:clean-check done +#16 DONE 0.1s + +View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/mwlwenbjkzp9ukzg8a92tezj0 + +PASS: isolated source context, fresh generated secrets, compose config and Docker build. No existing .env, volumes or build outputs copied. diff --git a/evidence/failover.txt b/evidence/failover.txt new file mode 100644 index 00000000..1eecf45f --- /dev/null +++ b/evidence/failover.txt @@ -0,0 +1,55 @@ +.venv/bin/python tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:18085" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 118, "failed_attempts": 1, "first_recovery_seconds": 1.87} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab5-api-2 messenger-lab5:local "dotnet Messenger.Ap…" api 3 minutes ago Up 30 seconds (healthy) 8080/tcp +mtusi2026lab5-broker-1 rabbitmq:4.2.0-alpine "docker-entrypoint.s…" broker 15 minutes ago Up 54 seconds (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp +mtusi2026lab5-dispatcher-1 messenger-lab5:local "dotnet Messenger.Ap…" dispatcher About a minute ago Up 30 seconds 8080/tcp +mtusi2026lab5-lb-1 haproxy:3.2.7-alpine "docker-entrypoint.s…" lb 15 minutes ago Up 30 seconds (healthy) 127.0.0.1:18085->8080/tcp +mtusi2026lab5-postgres-1 postgres:17.6 "docker-entrypoint.s…" postgres 15 minutes ago Up 55 seconds (healthy) 5432/tcp +mtusi2026lab5-redis-1 redis:7.4.2-alpine "docker-entrypoint.s…" redis 15 minutes ago Up 55 seconds (healthy) 6379/tcp +mtusi2026lab5-s3-1 chrislusf/seaweedfs:3.97 "/entrypoint.sh serv…" s3 7 minutes ago Up 52 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp +mtusi2026lab5-worker-1 messenger-lab5:local "dotnet Messenger.Ap…" worker About a minute ago Up 30 seconds 8080/tcp +./scripts/compose.sh up -d --wait api lb + Container mtusi2026lab5-redis-1 Running + Container mtusi2026lab5-api-2 Running + Container mtusi2026lab5-s3-1 Running + Container mtusi2026lab5-lb-1 Running + Container mtusi2026lab5-postgres-1 Running + Container mtusi2026lab5-s3-1 Waiting + Container mtusi2026lab5-postgres-1 Waiting + Container mtusi2026lab5-s3-1 Healthy + Container mtusi2026lab5-postgres-1 Healthy + Container mtusi2026lab5-s3-bootstrap-1 Starting + Container mtusi2026lab5-migrate-1 Starting + Container mtusi2026lab5-s3-bootstrap-1 Started + Container mtusi2026lab5-migrate-1 Started + Container mtusi2026lab5-migrate-1 Waiting + Container mtusi2026lab5-redis-1 Waiting + Container mtusi2026lab5-s3-bootstrap-1 Waiting + Container mtusi2026lab5-redis-1 Healthy + Container mtusi2026lab5-migrate-1 Exited + Container mtusi2026lab5-s3-bootstrap-1 Exited + Container mtusi2026lab5-api-1 Starting + Container mtusi2026lab5-api-1 Started + Container mtusi2026lab5-api-2 Waiting + Container mtusi2026lab5-api-1 Waiting + Container mtusi2026lab5-api-2 Healthy + Container mtusi2026lab5-api-1 Healthy + Container mtusi2026lab5-s3-bootstrap-1 Waiting + Container mtusi2026lab5-postgres-1 Waiting + Container mtusi2026lab5-redis-1 Waiting + Container mtusi2026lab5-api-2 Waiting + Container mtusi2026lab5-api-1 Waiting + Container mtusi2026lab5-migrate-1 Waiting + Container mtusi2026lab5-s3-1 Waiting + Container mtusi2026lab5-lb-1 Waiting + Container mtusi2026lab5-migrate-1 Exited + Container mtusi2026lab5-lb-1 Healthy + Container mtusi2026lab5-s3-bootstrap-1 Exited + Container mtusi2026lab5-redis-1 Healthy + Container mtusi2026lab5-s3-1 Healthy + Container mtusi2026lab5-postgres-1 Healthy + Container mtusi2026lab5-api-2 Healthy + Container mtusi2026lab5-api-1 Healthy diff --git a/evidence/job-envelope.json b/evidence/job-envelope.json new file mode 100644 index 00000000..4d9bf685 --- /dev/null +++ b/evidence/job-envelope.json @@ -0,0 +1,9 @@ +{ + "schema_version": 1, + "job_id": "3590b5cc-4392-4c8e-819f-a44e67ca7c64", + "type": "attachment.process.v1", + "attachment_id": "80fc41c9-e25b-4bcb-8475-e08d62822e78", + "pipeline_version": 1, + "attempt": 1, + "created_at": "2026-11-09T22:22:38.7816100Z" +} diff --git a/evidence/operations.txt b/evidence/operations.txt new file mode 100644 index 00000000..414de0e7 --- /dev/null +++ b/evidence/operations.txt @@ -0,0 +1,9 @@ +Проверки дополнения панели системы: +- Сборка .NET: 0 ошибок, 0 предупреждений. +- make check, make validate, make test: PASS. +- operations_panel.py: PASS: operator-only metrics and replay; anonymous 401; regular user 404; exact login lookup; one replay completes with real worker. +- Повторный POST replay для queued: 409; replay_count остался 1. +- Интерфейс проверен в браузере: экземпляры API, службы, графики, события и повторный запуск доступны владельцу. +Первый прогон теста после рестарта API встретил временный 503 при ожидании ready; само задание завершилось. Ожидание учитывает 503 до общего таймаута, финальное состояние ready остаётся обязательным. +PASS: public HTTPS registration, login, lookup @n1k0der, shared chat and message; operations denied to guest +PASS: empty chunked login accepted; nonempty chunked login rejected (student-tests/chunked_login.py). diff --git a/evidence/overload-checks.txt b/evidence/overload-checks.txt new file mode 100644 index 00000000..a3902ae1 --- /dev/null +++ b/evidence/overload-checks.txt @@ -0,0 +1,20 @@ +.venv/bin/python student-tests/overload_research.py --base-url "http://localhost:18085" +run 1/18: small, 1/1, 4.41 ready/s +run 2/18: small, 2/2, 3.62 ready/s +run 3/18: small, 4/4, 3.91 ready/s +run 4/18: large, 1/1, 0.78 ready/s +run 5/18: large, 2/2, 0.95 ready/s +run 6/18: large, 4/4, 0.81 ready/s +run 7/18: large, 2/2, 0.99 ready/s +run 8/18: large, 4/4, 0.97 ready/s +run 9/18: large, 1/1, 0.88 ready/s +run 10/18: small, 2/2, 3.57 ready/s +run 11/18: small, 4/4, 3.62 ready/s +run 12/18: small, 1/1, 4.65 ready/s +run 13/18: small, 4/4, 3.90 ready/s +run 14/18: small, 1/1, 3.86 ready/s +run 15/18: small, 2/2, 4.66 ready/s +run 16/18: large, 4/4, 0.69 ready/s +run 17/18: large, 1/1, 0.81 ready/s +run 18/18: large, 2/2, 0.73 ready/s +PASS: overload research and bounded admission diff --git a/evidence/overload-initial-checks.txt b/evidence/overload-initial-checks.txt new file mode 100644 index 00000000..03dfcbc9 --- /dev/null +++ b/evidence/overload-initial-checks.txt @@ -0,0 +1,14 @@ +.venv/bin/python student-tests/overload_research.py --base-url "http://localhost:18085" +run 1/18: small, 1/1, 4.60 ready/s +run 2/18: small, 2/2, 4.06 ready/s +run 3/18: small, 4/4, 0.84 ready/s +run 4/18: large, 1/1, 0.86 ready/s +FAIL: Infrastructure command failed: 2cad4d5d_mtusi2026lab5-api-2 Stopping + Container 74e89bf6ba55_mtusi2026lab5-worker-1 Recreate + Container 5c6d2cad4d5d_mtusi2026lab5-api-2 Stopped + Container 5c6d2cad4d5d_mtusi2026lab5-api-2 Removing + Container mtusi2026lab5-api-2 Error response from daemon: No such container: 5c6d2cad4d5db9f150fce5fc6f432103bddadddad8b1bf0d5f723466384f4843 + Container mtusi2026lab5-api-2 Removing +Error response from daemon: No such container: 5c6d2cad4d5db9f150fce5fc6f432103bddadddad8b1bf0d5f723466384f4843 + +make: *** [research-grade5] Error 1 diff --git a/evidence/overload-initial-progress.json b/evidence/overload-initial-progress.json new file mode 100644 index 00000000..1374137a --- /dev/null +++ b/evidence/overload-initial-progress.json @@ -0,0 +1,693 @@ +{ + "hypothesis": "With fixed worker CPU, more simultaneous image processes need not improve throughput and can increase RSS; bounded admission protects the queue when the worker stops.", + "profile": { + "settings": [ + [ + 1, + 1 + ], + [ + 2, + 2 + ], + [ + 4, + 4 + ] + ], + "repeats": 3, + "batch": 8, + "upload_clients": 4, + "worker_cpu": 1, + "worker_memory_limit_mib": 1024, + "warmup_images": 1, + "image_sizes_bytes": { + "small": 111817, + "large": 3319965 + } + }, + "runs": [ + { + "repeat": 1, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.7385662909946404, + "throughput": 4.601492644507199, + "p95_ready_seconds": 1.355825, + "p95_queue_lag_seconds": 1.221817, + "peak_rss_bytes": 152162304, + "peak_cgroup_memory_bytes": 53309440, + "worker_cpu_seconds": 1.329851, + "requests": [ + { + "upload_seconds": 0.04113916699861875, + "status": 202 + }, + { + "upload_seconds": 0.10876779200043529, + "status": 202 + }, + { + "upload_seconds": 0.12739412499649916, + "status": 202 + }, + { + "upload_seconds": 0.1493414579963428, + "status": 202 + }, + { + "upload_seconds": 0.11392083399550756, + "status": 202 + }, + { + "upload_seconds": 0.05546583300019847, + "status": 202 + }, + { + "upload_seconds": 0.04235250000056112, + "status": 202 + }, + { + "upload_seconds": 0.02778862499690149, + "status": 202 + } + ], + "jobs": [ + { + "id": "d2b88786-66d5-44f9-8398-afd799333b0d", + "ready_seconds": 0.27748, + "queue_lag_seconds": 0.115927, + "attempts": 1 + }, + { + "id": "746abbed-2f2a-44a7-8054-8e2fb4e40d60", + "ready_seconds": 0.405159, + "queue_lag_seconds": 0.269961, + "attempts": 1 + }, + { + "id": "a4fbcad3-77a5-402f-8ce9-a286c3c50998", + "ready_seconds": 0.52426, + "queue_lag_seconds": 0.395915, + "attempts": 1 + }, + { + "id": "bbc83e93-f179-4f84-a5a1-4bfb334ecf5d", + "ready_seconds": 0.629692, + "queue_lag_seconds": 0.459954, + "attempts": 1 + }, + { + "id": "8d5f7573-9a93-4f75-9871-fe57beaafa15", + "ready_seconds": 0.909593, + "queue_lag_seconds": 0.620331, + "attempts": 1 + }, + { + "id": "8280ed8d-da55-40a0-bde3-36ee21893e72", + "ready_seconds": 1.068921, + "queue_lag_seconds": 0.902328, + "attempts": 1 + }, + { + "id": "d8dbae97-6243-4f95-b9cc-4a3e8fdfefbb", + "ready_seconds": 1.225459, + "queue_lag_seconds": 1.068507, + "attempts": 1 + }, + { + "id": "dcd72436-5215-4aca-900d-74767f0385b2", + "ready_seconds": 1.355825, + "queue_lag_seconds": 1.221817, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262118.743776, + "memory_bytes": 42041344, + "cpu_usec": 795694, + "rss_bytes": 103636992 + }, + { + "time": 1794262119.0674748, + "memory_bytes": 48332800, + "cpu_usec": 1134009, + "rss_bytes": 127852544 + }, + { + "time": 1794262119.545117, + "memory_bytes": 50176000, + "cpu_usec": 1544976, + "rss_bytes": 152162304 + }, + { + "time": 1794262119.89533, + "memory_bytes": 53309440, + "cpu_usec": 1889921, + "rss_bytes": 147939328 + }, + { + "time": 1794262120.2800698, + "memory_bytes": 46026752, + "cpu_usec": 2125545, + "rss_bytes": 107089920 + } + ] + }, + { + "repeat": 1, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.969336124995607, + "throughput": 4.062282663919469, + "p95_ready_seconds": 1.525365, + "p95_queue_lag_seconds": 1.016688, + "peak_rss_bytes": 219119616, + "peak_cgroup_memory_bytes": 62783488, + "worker_cpu_seconds": 1.534243, + "requests": [ + { + "upload_seconds": 0.03250100000150269, + "status": 202 + }, + { + "upload_seconds": 0.044958750004298054, + "status": 202 + }, + { + "upload_seconds": 0.05604845799825853, + "status": 202 + }, + { + "upload_seconds": 0.06452524999622256, + "status": 202 + }, + { + "upload_seconds": 0.0502204169970355, + "status": 202 + }, + { + "upload_seconds": 0.04115625000122236, + "status": 202 + }, + { + "upload_seconds": 0.03952566699445015, + "status": 202 + }, + { + "upload_seconds": 0.0422521250002319, + "status": 202 + } + ], + "jobs": [ + { + "id": "9353ef12-3388-4f17-a52e-7788d9631d5e", + "ready_seconds": 0.394761, + "queue_lag_seconds": 0.116528, + "attempts": 1 + }, + { + "id": "5ff9fdf3-e9e6-446e-95c7-978c9b3fd22b", + "ready_seconds": 0.404182, + "queue_lag_seconds": 0.129876, + "attempts": 1 + }, + { + "id": "314ef166-fd5e-4c85-abf7-15185aea7576", + "ready_seconds": 0.692893, + "queue_lag_seconds": 0.388414, + "attempts": 1 + }, + { + "id": "52a91f37-b649-4896-8051-5f39802fb3ad", + "ready_seconds": 0.751849, + "queue_lag_seconds": 0.382157, + "attempts": 1 + }, + { + "id": "052cd532-0568-47b6-a95c-e35bc68c6d81", + "ready_seconds": 0.976998, + "queue_lag_seconds": 0.669662, + "attempts": 1 + }, + { + "id": "5df94c7d-97a6-4ca1-a3bb-c663ea5a7e41", + "ready_seconds": 1.032436, + "queue_lag_seconds": 0.738607, + "attempts": 1 + }, + { + "id": "926d6d55-a9b6-408f-a7e2-2bf2b98871b0", + "ready_seconds": 1.523118, + "queue_lag_seconds": 1.005413, + "attempts": 1 + }, + { + "id": "dad6cf86-7200-4468-b927-aca54ae14db9", + "ready_seconds": 1.525365, + "queue_lag_seconds": 1.016688, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262122.505246, + "memory_bytes": 42008576, + "cpu_usec": 782744, + "rss_bytes": 103116800 + }, + { + "time": 1794262122.863539, + "memory_bytes": 54919168, + "cpu_usec": 1204770, + "rss_bytes": 161333248 + }, + { + "time": 1794262123.2467399, + "memory_bytes": 57008128, + "cpu_usec": 1566113, + "rss_bytes": 175915008 + }, + { + "time": 1794262123.8038552, + "memory_bytes": 62783488, + "cpu_usec": 2036485, + "rss_bytes": 219119616 + }, + { + "time": 1794262124.115168, + "memory_bytes": 45998080, + "cpu_usec": 2316987, + "rss_bytes": 106729472 + } + ] + }, + { + "repeat": 1, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.549317416000122, + "throughput": 0.8377562135065066, + "p95_ready_seconds": 9.209337, + "p95_queue_lag_seconds": 7.286448, + "peak_rss_bytes": 289599488, + "peak_cgroup_memory_bytes": 87416832, + "worker_cpu_seconds": 5.107425, + "requests": [ + { + "upload_seconds": 0.03872108399809804, + "status": 202 + }, + { + "upload_seconds": 0.06579587499436457, + "status": 202 + }, + { + "upload_seconds": 0.08007283299957635, + "status": 202 + }, + { + "upload_seconds": 0.09368379199440824, + "status": 202 + }, + { + "upload_seconds": 0.040345542001887225, + "status": 202 + }, + { + "upload_seconds": 0.07635662500251783, + "status": 202 + }, + { + "upload_seconds": 0.032701916999940295, + "status": 202 + }, + { + "upload_seconds": 0.05977749999874504, + "status": 202 + } + ], + "jobs": [ + { + "id": "c70a41eb-b3da-40f7-9669-fbc0adf914d1", + "ready_seconds": 9.074966, + "queue_lag_seconds": 7.278132, + "attempts": 1 + }, + { + "id": "e799ce82-e00a-4e15-aacc-daa6fa757335", + "ready_seconds": 9.150763, + "queue_lag_seconds": 7.224741, + "attempts": 1 + }, + { + "id": "d248b714-1eeb-4668-b497-4a30b3b0aad8", + "ready_seconds": 9.16808, + "queue_lag_seconds": 7.267137, + "attempts": 1 + }, + { + "id": "a540b150-be0e-4d38-9077-92802293c12d", + "ready_seconds": 9.209337, + "queue_lag_seconds": 7.286448, + "attempts": 1 + }, + { + "id": "d62a309a-a54e-4a7d-aebc-864fd251bed4", + "ready_seconds": 7.164243, + "queue_lag_seconds": 0.116455, + "attempts": 1 + }, + { + "id": "063093ae-38a4-4fc4-a841-d3e5db5624b3", + "ready_seconds": 7.164926, + "queue_lag_seconds": 0.135483, + "attempts": 1 + }, + { + "id": "06613954-2472-4efe-b2a7-42644a1d8a30", + "ready_seconds": 7.149471, + "queue_lag_seconds": 0.127605, + "attempts": 1 + }, + { + "id": "f593fbcd-f166-4e3e-9d89-09faeaa592bb", + "ready_seconds": 7.127115, + "queue_lag_seconds": 0.132045, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262126.910836, + "memory_bytes": 44044288, + "cpu_usec": 1063223, + "rss_bytes": 143413248 + }, + { + "time": 1794262127.9235861, + "memory_bytes": 87416832, + "cpu_usec": 1873587, + "rss_bytes": 284831744 + }, + { + "time": 1794262130.018199, + "memory_bytes": 42848256, + "cpu_usec": 3399041, + "rss_bytes": 106557440 + }, + { + "time": 1794262130.52949, + "memory_bytes": 43167744, + "cpu_usec": 3541243, + "rss_bytes": 106573824 + }, + { + "time": 1794262131.362358, + "memory_bytes": 42827776, + "cpu_usec": 3715963, + "rss_bytes": 106573824 + }, + { + "time": 1794262133.449707, + "memory_bytes": 44789760, + "cpu_usec": 4203236, + "rss_bytes": 108687360 + }, + { + "time": 1794262133.8703759, + "memory_bytes": 47022080, + "cpu_usec": 4272996, + "rss_bytes": 108298240 + }, + { + "time": 1794262134.622085, + "memory_bytes": 77737984, + "cpu_usec": 5000530, + "rss_bytes": 289599488 + }, + { + "time": 1794262135.440066, + "memory_bytes": 55914496, + "cpu_usec": 5808140, + "rss_bytes": 108367872 + }, + { + "time": 1794262135.956406, + "memory_bytes": 44863488, + "cpu_usec": 6170648, + "rss_bytes": 109416448 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.295377750000625, + "throughput": 0.8606428071198572, + "p95_ready_seconds": 7.433391, + "p95_queue_lag_seconds": 6.388493, + "peak_rss_bytes": 193171456, + "peak_cgroup_memory_bytes": 102318080, + "worker_cpu_seconds": 7.074789, + "requests": [ + { + "upload_seconds": 0.7639207920001354, + "status": 202 + }, + { + "upload_seconds": 0.9345893749996321, + "status": 202 + }, + { + "upload_seconds": 1.1469821249993402, + "status": 202 + }, + { + "upload_seconds": 1.436994916999538, + "status": 202 + }, + { + "upload_seconds": 0.7003262910002377, + "status": 202 + }, + { + "upload_seconds": 0.9740857910001068, + "status": 202 + }, + { + "upload_seconds": 0.39281854200089583, + "status": 202 + }, + { + "upload_seconds": 0.8782962080003927, + "status": 202 + } + ], + "jobs": [ + { + "id": "2be2644d-861c-43c4-8085-5fbba90a4d64", + "ready_seconds": 1.712623, + "queue_lag_seconds": 0.363585, + "attempts": 1 + }, + { + "id": "ad0083b2-3bf8-4f42-8043-4a57ad7b84a9", + "ready_seconds": 2.629908, + "queue_lag_seconds": 1.699148, + "attempts": 1 + }, + { + "id": "407c2a2e-e5aa-4437-ab5b-49a2ec05829b", + "ready_seconds": 3.021709, + "queue_lag_seconds": 2.407417, + "attempts": 1 + }, + { + "id": "854309f9-5d4f-4305-ad65-bcb005b3844f", + "ready_seconds": 3.852419, + "queue_lag_seconds": 2.844126, + "attempts": 1 + }, + { + "id": "fae78450-5058-4101-8cd9-4dcbcecd9e2c", + "ready_seconds": 4.70857, + "queue_lag_seconds": 3.753701, + "attempts": 1 + }, + { + "id": "fe283bf1-cdff-4ee2-867d-369338758dd4", + "ready_seconds": 5.265006, + "queue_lag_seconds": 4.502767, + "attempts": 1 + }, + { + "id": "86228db3-70a0-4118-bd9c-1f0aeb13ccf4", + "ready_seconds": 6.477127, + "queue_lag_seconds": 5.104128, + "attempts": 1 + }, + { + "id": "3fea2d31-ad7b-4282-b815-23adaa068c7a", + "ready_seconds": 7.433391, + "queue_lag_seconds": 6.388493, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262140.0249028, + "memory_bytes": 41619456, + "cpu_usec": 1623573, + "rss_bytes": 106139648 + }, + { + "time": 1794262140.4498188, + "memory_bytes": 41545728, + "cpu_usec": 1659252, + "rss_bytes": 106139648 + }, + { + "time": 1794262140.9149399, + "memory_bytes": 60805120, + "cpu_usec": 2022910, + "rss_bytes": 150097920 + }, + { + "time": 1794262141.32444, + "memory_bytes": 88137728, + "cpu_usec": 2454146, + "rss_bytes": 191004672 + }, + { + "time": 1794262141.7649221, + "memory_bytes": 54976512, + "cpu_usec": 2788341, + "rss_bytes": 106950656 + }, + { + "time": 1794262142.153526, + "memory_bytes": 82583552, + "cpu_usec": 3145199, + "rss_bytes": 180039680 + }, + { + "time": 1794262142.4781618, + "memory_bytes": 98701312, + "cpu_usec": 3485405, + "rss_bytes": 192589824 + }, + { + "time": 1794262142.818176, + "memory_bytes": 54738944, + "cpu_usec": 3670691, + "rss_bytes": 106897408 + }, + { + "time": 1794262143.150275, + "memory_bytes": 92516352, + "cpu_usec": 4041841, + "rss_bytes": 187322368 + }, + { + "time": 1794262143.4721, + "memory_bytes": 58019840, + "cpu_usec": 4330075, + "rss_bytes": 106909696 + }, + { + "time": 1794262143.816778, + "memory_bytes": 91193344, + "cpu_usec": 4691490, + "rss_bytes": 185057280 + }, + { + "time": 1794262144.319894, + "memory_bytes": 54292480, + "cpu_usec": 4990675, + "rss_bytes": 106909696 + }, + { + "time": 1794262144.7009401, + "memory_bytes": 66936832, + "cpu_usec": 5267047, + "rss_bytes": 147943424 + }, + { + "time": 1794262145.049203, + "memory_bytes": 98369536, + "cpu_usec": 5612259, + "rss_bytes": 192471040 + }, + { + "time": 1794262145.4414349, + "memory_bytes": 63508480, + "cpu_usec": 5901715, + "rss_bytes": 109088768 + }, + { + "time": 1794262145.81682, + "memory_bytes": 92336128, + "cpu_usec": 6171315, + "rss_bytes": 182304768 + }, + { + "time": 1794262146.1910982, + "memory_bytes": 75481088, + "cpu_usec": 6539191, + "rss_bytes": 109092864 + }, + { + "time": 1794262146.612237, + "memory_bytes": 73048064, + "cpu_usec": 6912398, + "rss_bytes": 154517504 + }, + { + "time": 1794262147.0016918, + "memory_bytes": 96288768, + "cpu_usec": 7288823, + "rss_bytes": 190681088 + }, + { + "time": 1794262147.491589, + "memory_bytes": 92078080, + "cpu_usec": 7759923, + "rss_bytes": 179814400 + }, + { + "time": 1794262147.9121048, + "memory_bytes": 64536576, + "cpu_usec": 8079635, + "rss_bytes": 128561152 + }, + { + "time": 1794262148.251782, + "memory_bytes": 102318080, + "cpu_usec": 8385623, + "rss_bytes": 193171456 + }, + { + "time": 1794262148.6732469, + "memory_bytes": 67047424, + "cpu_usec": 8698362, + "rss_bytes": 115306496 + } + ] + } + ] +} diff --git a/evidence/overload-progress.json b/evidence/overload-progress.json new file mode 100644 index 00000000..8626635a --- /dev/null +++ b/evidence/overload-progress.json @@ -0,0 +1,3229 @@ +{ + "hypothesis": "With fixed worker CPU, more simultaneous image processes need not improve throughput and can increase RSS; bounded admission protects the queue when the worker stops.", + "profile": { + "settings": [ + [ + 1, + 1 + ], + [ + 2, + 2 + ], + [ + 4, + 4 + ] + ], + "repeats": 3, + "batch": 8, + "upload_clients": 4, + "worker_cpu": 1, + "worker_memory_limit_mib": 1024, + "warmup_images": 1, + "image_sizes_bytes": { + "small": 111817, + "large": 3319965 + } + }, + "runs": [ + { + "repeat": 1, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.8135867090022657, + "throughput": 4.411148339525026, + "p95_ready_seconds": 1.523784, + "p95_queue_lag_seconds": 1.262202, + "peak_rss_bytes": 165265408, + "peak_cgroup_memory_bytes": 56442880, + "worker_cpu_seconds": 1.445814, + "requests": [ + { + "upload_seconds": 0.04025879099935992, + "status": 202 + }, + { + "upload_seconds": 0.0557741669981624, + "status": 202 + }, + { + "upload_seconds": 0.02573179200408049, + "status": 202 + }, + { + "upload_seconds": 0.18365204199653817, + "status": 202 + }, + { + "upload_seconds": 0.15453533300023992, + "status": 202 + }, + { + "upload_seconds": 0.018875291003496386, + "status": 202 + }, + { + "upload_seconds": 0.21073149999574525, + "status": 202 + }, + { + "upload_seconds": 0.13923129100294318, + "status": 202 + } + ], + "jobs": [ + { + "id": "0ccede90-ead1-4a90-a6dd-80cf3eb19dcc", + "ready_seconds": 0.27133, + "queue_lag_seconds": 0.115042, + "attempts": 1 + }, + { + "id": "248fd684-b378-4c0c-9362-2533a2904a1e", + "ready_seconds": 1.231278, + "queue_lag_seconds": 1.014569, + "attempts": 1 + }, + { + "id": "f39b02fd-2265-4dc6-84bd-b91574ec1449", + "ready_seconds": 0.495172, + "queue_lag_seconds": 0.256786, + "attempts": 1 + }, + { + "id": "43fe1dc0-edbb-48f4-b25c-bc2dcb093c1a", + "ready_seconds": 1.008641, + "queue_lag_seconds": 0.855938, + "attempts": 1 + }, + { + "id": "9d1220c6-4852-45cd-b5e1-0888767914e2", + "ready_seconds": 1.523784, + "queue_lag_seconds": 1.262202, + "attempts": 1 + }, + { + "id": "ae449c02-9a46-4794-927c-da9935b99d1c", + "ready_seconds": 0.641612, + "queue_lag_seconds": 0.481173, + "attempts": 1 + }, + { + "id": "c4b1ce2a-851c-4b8c-9407-f1673eb0bfef", + "ready_seconds": 0.754448, + "queue_lag_seconds": 0.561934, + "attempts": 1 + }, + { + "id": "93964e00-34fc-41d3-90e1-6e60e7be1edd", + "ready_seconds": 0.858382, + "queue_lag_seconds": 0.733633, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262308.2044969, + "memory_bytes": 42037248, + "cpu_usec": 846749, + "rss_bytes": 103288832 + }, + { + "time": 1794262308.619572, + "memory_bytes": 40370176, + "cpu_usec": 1244009, + "rss_bytes": 104734720 + }, + { + "time": 1794262308.974692, + "memory_bytes": 46178304, + "cpu_usec": 1614392, + "rss_bytes": 106708992 + }, + { + "time": 1794262309.3393369, + "memory_bytes": 48230400, + "cpu_usec": 1958144, + "rss_bytes": 123957248 + }, + { + "time": 1794262309.7072818, + "memory_bytes": 56442880, + "cpu_usec": 2292563, + "rss_bytes": 165265408 + } + ] + }, + { + "repeat": 1, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.207073791003495, + "throughput": 3.6247088940160097, + "p95_ready_seconds": 1.675188, + "p95_queue_lag_seconds": 1.318631, + "peak_rss_bytes": 144166912, + "peak_cgroup_memory_bytes": 50225152, + "worker_cpu_seconds": 1.626515, + "requests": [ + { + "upload_seconds": 0.032242041001154575, + "status": 202 + }, + { + "upload_seconds": 0.045670040999539196, + "status": 202 + }, + { + "upload_seconds": 0.05951904099492822, + "status": 202 + }, + { + "upload_seconds": 0.0768088330005412, + "status": 202 + }, + { + "upload_seconds": 0.05488000000332249, + "status": 202 + }, + { + "upload_seconds": 0.04967383300390793, + "status": 202 + }, + { + "upload_seconds": 0.04204687500168802, + "status": 202 + }, + { + "upload_seconds": 0.03497491600137437, + "status": 202 + } + ], + "jobs": [ + { + "id": "fd94ff6a-838f-48c5-b8a3-00c7ead78063", + "ready_seconds": 0.469287, + "queue_lag_seconds": 0.10038, + "attempts": 1 + }, + { + "id": "7be0e978-4272-437d-8e74-682e5284dbfe", + "ready_seconds": 1.248626, + "queue_lag_seconds": 0.822855, + "attempts": 1 + }, + { + "id": "3c8bf977-e0f0-4a16-a8f4-e4524ad0e594", + "ready_seconds": 0.849686, + "queue_lag_seconds": 0.454018, + "attempts": 1 + }, + { + "id": "43d83901-5ee0-46ec-b177-5cf893d62d39", + "ready_seconds": 1.675188, + "queue_lag_seconds": 1.318631, + "attempts": 1 + }, + { + "id": "914a5c4b-7357-4abf-9e5f-ef9a123919a9", + "ready_seconds": 0.477878, + "queue_lag_seconds": 0.115539, + "attempts": 1 + }, + { + "id": "dd075e01-2d54-41c2-bebf-e03852480384", + "ready_seconds": 1.300378, + "queue_lag_seconds": 0.826505, + "attempts": 1 + }, + { + "id": "0c5a1967-e3b2-4fdd-bc8f-a42b6daa55e6", + "ready_seconds": 0.849473, + "queue_lag_seconds": 0.467112, + "attempts": 1 + }, + { + "id": "91081b2a-4eac-4679-a210-835405664cc4", + "ready_seconds": 1.627132, + "queue_lag_seconds": 1.279613, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262312.5737548, + "memory_bytes": 42983424, + "cpu_usec": 993118, + "rss_bytes": 140197888 + }, + { + "time": 1794262312.962958, + "memory_bytes": 46227456, + "cpu_usec": 1386895, + "rss_bytes": 144166912 + }, + { + "time": 1794262313.35336, + "memory_bytes": 50225152, + "cpu_usec": 1762234, + "rss_bytes": 143147008 + }, + { + "time": 1794262313.773581, + "memory_bytes": 45711360, + "cpu_usec": 2204620, + "rss_bytes": 106614784 + }, + { + "time": 1794262314.166707, + "memory_bytes": 45285376, + "cpu_usec": 2593158, + "rss_bytes": 106643456 + }, + { + "time": 1794262314.538958, + "memory_bytes": 45490176, + "cpu_usec": 2619633, + "rss_bytes": 106643456 + } + ] + }, + { + "repeat": 1, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.0476217079994967, + "throughput": 3.906971668031352, + "p95_ready_seconds": 1.788149, + "p95_queue_lag_seconds": 0.96431, + "peak_rss_bytes": 295608320, + "peak_cgroup_memory_bytes": 74162176, + "worker_cpu_seconds": 1.521378, + "requests": [ + { + "upload_seconds": 0.0409248750002007, + "status": 202 + }, + { + "upload_seconds": 0.06408179199934239, + "status": 202 + }, + { + "upload_seconds": 0.07904470799985575, + "status": 202 + }, + { + "upload_seconds": 0.09678162500495091, + "status": 202 + }, + { + "upload_seconds": 0.07141170799877727, + "status": 202 + }, + { + "upload_seconds": 0.06645174999721348, + "status": 202 + }, + { + "upload_seconds": 0.06909837500279536, + "status": 202 + }, + { + "upload_seconds": 0.06092287499632221, + "status": 202 + } + ], + "jobs": [ + { + "id": "5d269ef3-9476-4f6f-b27b-fbb14c0e4caa", + "ready_seconds": 0.964258, + "queue_lag_seconds": 0.136546, + "attempts": 1 + }, + { + "id": "c2e7e0ab-c0ea-472f-a273-b3a4955fbac1", + "ready_seconds": 1.788149, + "queue_lag_seconds": 0.96431, + "attempts": 1 + }, + { + "id": "1804debb-8da5-47ae-a8fe-b80ad0be4e2a", + "ready_seconds": 0.940762, + "queue_lag_seconds": 0.116292, + "attempts": 1 + }, + { + "id": "5992ff07-5eca-41e8-9e4a-7711f9248df2", + "ready_seconds": 1.783108, + "queue_lag_seconds": 0.949592, + "attempts": 1 + }, + { + "id": "8c0827bc-bdbd-4b84-b035-608aa303f8d8", + "ready_seconds": 0.879942, + "queue_lag_seconds": 0.042626, + "attempts": 1 + }, + { + "id": "ec251619-feac-476d-af95-9d65fd1ec5db", + "ready_seconds": 1.711594, + "queue_lag_seconds": 0.900856, + "attempts": 1 + }, + { + "id": "e0277e42-4342-4019-a9d1-35900708918b", + "ready_seconds": 0.865465, + "queue_lag_seconds": 0.055853, + "attempts": 1 + }, + { + "id": "8e126133-6c15-47e0-aa0d-e91283068a9a", + "ready_seconds": 1.68961, + "queue_lag_seconds": 0.882134, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262317.7497191, + "memory_bytes": 55816192, + "cpu_usec": 1189242, + "rss_bytes": 212205568 + }, + { + "time": 1794262318.25758, + "memory_bytes": 66314240, + "cpu_usec": 1726302, + "rss_bytes": 222949376 + }, + { + "time": 1794262318.7573462, + "memory_bytes": 74162176, + "cpu_usec": 2283956, + "rss_bytes": 295608320 + }, + { + "time": 1794262319.248426, + "memory_bytes": 66113536, + "cpu_usec": 2710620, + "rss_bytes": 227430400 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 10.214052916999208, + "throughput": 0.7832346341857728, + "p95_ready_seconds": 8.463946, + "p95_queue_lag_seconds": 7.654972, + "peak_rss_bytes": 199614464, + "peak_cgroup_memory_bytes": 101957632, + "worker_cpu_seconds": 7.518454, + "requests": [ + { + "upload_seconds": 0.4734504169973661, + "status": 202 + }, + { + "upload_seconds": 0.6722895000057179, + "status": 202 + }, + { + "upload_seconds": 0.7707648750001681, + "status": 202 + }, + { + "upload_seconds": 0.8576565000039409, + "status": 202 + }, + { + "upload_seconds": 0.35896287499781465, + "status": 202 + }, + { + "upload_seconds": 0.6733611249946989, + "status": 202 + }, + { + "upload_seconds": 0.38681283299956704, + "status": 202 + }, + { + "upload_seconds": 0.6135808749968419, + "status": 202 + } + ], + "jobs": [ + { + "id": "11d75d8d-cd9b-4d10-a405-109dd2e60e03", + "ready_seconds": 3.874429, + "queue_lag_seconds": 1.346038, + "attempts": 1 + }, + { + "id": "a2284f7f-5012-40b5-826a-4b8a2795e7c5", + "ready_seconds": 7.047366, + "queue_lag_seconds": 6.172377, + "attempts": 1 + }, + { + "id": "fa0141b9-4b19-4f0e-af03-d45ec4e890d3", + "ready_seconds": 8.463946, + "queue_lag_seconds": 7.654972, + "attempts": 1 + }, + { + "id": "0bf04082-3c2a-4d14-ab16-2226069cb82f", + "ready_seconds": 4.603033, + "queue_lag_seconds": 3.726946, + "attempts": 1 + }, + { + "id": "3cd335e4-b400-4c48-894b-108f11769b57", + "ready_seconds": 7.686789, + "queue_lag_seconds": 6.893187, + "attempts": 1 + }, + { + "id": "82d60e9f-793c-4b49-b3bb-69d48fcc1c82", + "ready_seconds": 1.355779, + "queue_lag_seconds": 0.231652, + "attempts": 1 + }, + { + "id": "59eb40ef-a7a0-4584-b9e3-5958010ea250", + "ready_seconds": 5.512865, + "queue_lag_seconds": 4.425121, + "attempts": 1 + }, + { + "id": "737301a8-fc53-402a-85b0-36ab105ec1b5", + "ready_seconds": 6.237758, + "queue_lag_seconds": 5.427986, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262324.2375848, + "memory_bytes": 44666880, + "cpu_usec": 2065973, + "rss_bytes": 106340352 + }, + { + "time": 1794262324.677325, + "memory_bytes": 51453952, + "cpu_usec": 2311291, + "rss_bytes": 141185024 + }, + { + "time": 1794262325.088465, + "memory_bytes": 87121920, + "cpu_usec": 2690405, + "rss_bytes": 189181952 + }, + { + "time": 1794262325.504909, + "memory_bytes": 47222784, + "cpu_usec": 3063668, + "rss_bytes": 107204608 + }, + { + "time": 1794262325.9938948, + "memory_bytes": 84570112, + "cpu_usec": 3494369, + "rss_bytes": 180502528 + }, + { + "time": 1794262326.47446, + "memory_bytes": 77090816, + "cpu_usec": 3970594, + "rss_bytes": 176230400 + }, + { + "time": 1794262326.81706, + "memory_bytes": 59994112, + "cpu_usec": 4107626, + "rss_bytes": 107495424 + }, + { + "time": 1794262327.157752, + "memory_bytes": 60157952, + "cpu_usec": 4127645, + "rss_bytes": 107495424 + }, + { + "time": 1794262327.69939, + "memory_bytes": 60305408, + "cpu_usec": 4220682, + "rss_bytes": 107495424 + }, + { + "time": 1794262328.0880482, + "memory_bytes": 61517824, + "cpu_usec": 4291251, + "rss_bytes": 108978176 + }, + { + "time": 1794262328.4875982, + "memory_bytes": 86396928, + "cpu_usec": 4660762, + "rss_bytes": 180977664 + }, + { + "time": 1794262328.871718, + "memory_bytes": 97525760, + "cpu_usec": 5031249, + "rss_bytes": 194334720 + }, + { + "time": 1794262329.288575, + "memory_bytes": 69193728, + "cpu_usec": 5403575, + "rss_bytes": 157335552 + }, + { + "time": 1794262329.6867101, + "memory_bytes": 97075200, + "cpu_usec": 5799498, + "rss_bytes": 191086592 + }, + { + "time": 1794262330.04568, + "memory_bytes": 65396736, + "cpu_usec": 6128659, + "rss_bytes": 113606656 + }, + { + "time": 1794262330.391624, + "memory_bytes": 91922432, + "cpu_usec": 6404042, + "rss_bytes": 186523648 + }, + { + "time": 1794262330.76962, + "memory_bytes": 80707584, + "cpu_usec": 6768430, + "rss_bytes": 178741248 + }, + { + "time": 1794262331.1224809, + "memory_bytes": 71532544, + "cpu_usec": 7069751, + "rss_bytes": 153964544 + }, + { + "time": 1794262331.500947, + "memory_bytes": 101236736, + "cpu_usec": 7437300, + "rss_bytes": 198979584 + }, + { + "time": 1794262331.910403, + "memory_bytes": 57864192, + "cpu_usec": 7770565, + "rss_bytes": 113946624 + }, + { + "time": 1794262332.261354, + "memory_bytes": 98197504, + "cpu_usec": 8117409, + "rss_bytes": 192552960 + }, + { + "time": 1794262332.617752, + "memory_bytes": 65212416, + "cpu_usec": 8423479, + "rss_bytes": 113954816 + }, + { + "time": 1794262332.989001, + "memory_bytes": 75464704, + "cpu_usec": 8687402, + "rss_bytes": 161304576 + }, + { + "time": 1794262333.334304, + "memory_bytes": 101957632, + "cpu_usec": 9041253, + "rss_bytes": 199614464 + }, + { + "time": 1794262333.7091782, + "memory_bytes": 60796928, + "cpu_usec": 9214412, + "rss_bytes": 113958912 + }, + { + "time": 1794262334.120569, + "memory_bytes": 58785792, + "cpu_usec": 9584427, + "rss_bytes": 115216384 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 8.415711208006542, + "throughput": 0.9506029618018448, + "p95_ready_seconds": 6.307119, + "p95_queue_lag_seconds": 4.704642, + "peak_rss_bytes": 277078016, + "peak_cgroup_memory_bytes": 145489920, + "worker_cpu_seconds": 7.462616, + "requests": [ + { + "upload_seconds": 0.3655178750050254, + "status": 202 + }, + { + "upload_seconds": 0.46488608400250087, + "status": 202 + }, + { + "upload_seconds": 0.9044631249998929, + "status": 202 + }, + { + "upload_seconds": 1.318556540994905, + "status": 202 + }, + { + "upload_seconds": 1.2203440420053084, + "status": 202 + }, + { + "upload_seconds": 1.3046807499995339, + "status": 202 + }, + { + "upload_seconds": 1.0543449169999803, + "status": 202 + }, + { + "upload_seconds": 0.7488859579971177, + "status": 202 + } + ], + "jobs": [ + { + "id": "8faec5ee-2fa6-4d07-8630-fc0e8bcf6eb1", + "ready_seconds": 4.116328, + "queue_lag_seconds": 2.488239, + "attempts": 1 + }, + { + "id": "b6e499e9-a7be-49de-a6bd-cc232f016844", + "ready_seconds": 5.13449, + "queue_lag_seconds": 3.550491, + "attempts": 1 + }, + { + "id": "7f99c8aa-cfbb-476f-9ef3-1b65d68ce421", + "ready_seconds": 6.307119, + "queue_lag_seconds": 4.471636, + "attempts": 1 + }, + { + "id": "717a9e90-a5b0-42a4-a7e1-a8874f646abe", + "ready_seconds": 2.990502, + "queue_lag_seconds": 0.28872, + "attempts": 1 + }, + { + "id": "69b1fc5f-0761-4aa5-8be9-5168a8680dcd", + "ready_seconds": 6.285874, + "queue_lag_seconds": 4.704642, + "attempts": 1 + }, + { + "id": "e90fcacb-a24e-42ba-a39f-7928d3910d06", + "ready_seconds": 2.492907, + "queue_lag_seconds": 0.150521, + "attempts": 1 + }, + { + "id": "aacb59ef-7575-48ef-8820-36f6bb63dcdc", + "ready_seconds": 4.308028, + "queue_lag_seconds": 2.810447, + "attempts": 1 + }, + { + "id": "618eb9bb-9cc3-40b1-a00d-635f48b1f129", + "ready_seconds": 5.029212, + "queue_lag_seconds": 3.404008, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262337.490489, + "memory_bytes": 44957696, + "cpu_usec": 1388440, + "rss_bytes": 106409984 + }, + { + "time": 1794262337.939751, + "memory_bytes": 60059648, + "cpu_usec": 1666638, + "rss_bytes": 141225984 + }, + { + "time": 1794262338.709249, + "memory_bytes": 97263616, + "cpu_usec": 2314922, + "rss_bytes": 224612352 + }, + { + "time": 1794262339.232517, + "memory_bytes": 128987136, + "cpu_usec": 2844467, + "rss_bytes": 264658944 + }, + { + "time": 1794262339.6184871, + "memory_bytes": 129318912, + "cpu_usec": 3284496, + "rss_bytes": 260890624 + }, + { + "time": 1794262340.150041, + "memory_bytes": 63504384, + "cpu_usec": 3756474, + "rss_bytes": 109555712 + }, + { + "time": 1794262340.604726, + "memory_bytes": 71729152, + "cpu_usec": 4128343, + "rss_bytes": 158167040 + }, + { + "time": 1794262341.026336, + "memory_bytes": 131174400, + "cpu_usec": 4607664, + "rss_bytes": 260644864 + }, + { + "time": 1794262341.428124, + "memory_bytes": 145489920, + "cpu_usec": 5010041, + "rss_bytes": 277078016 + }, + { + "time": 1794262341.821051, + "memory_bytes": 69431296, + "cpu_usec": 5397722, + "rss_bytes": 109563904 + }, + { + "time": 1794262342.315008, + "memory_bytes": 97644544, + "cpu_usec": 5854211, + "rss_bytes": 217735168 + }, + { + "time": 1794262342.7356732, + "memory_bytes": 135110656, + "cpu_usec": 6298093, + "rss_bytes": 270577664 + }, + { + "time": 1794262343.203978, + "memory_bytes": 106741760, + "cpu_usec": 6743952, + "rss_bytes": 195207168 + }, + { + "time": 1794262343.709871, + "memory_bytes": 73314304, + "cpu_usec": 7248146, + "rss_bytes": 151134208 + }, + { + "time": 1794262344.2393038, + "memory_bytes": 118419456, + "cpu_usec": 7724934, + "rss_bytes": 258355200 + }, + { + "time": 1794262344.630781, + "memory_bytes": 140279808, + "cpu_usec": 8201607, + "rss_bytes": 274935808 + }, + { + "time": 1794262345.107616, + "memory_bytes": 113319936, + "cpu_usec": 8619190, + "rss_bytes": 197951488 + }, + { + "time": 1794262345.437784, + "memory_bytes": 63397888, + "cpu_usec": 8851056, + "rss_bytes": 112402432 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.835286958994402, + "throughput": 0.8133977212209323, + "p95_ready_seconds": 8.308307, + "p95_queue_lag_seconds": 4.476807, + "peak_rss_bytes": 424382464, + "peak_cgroup_memory_bytes": 191934464, + "worker_cpu_seconds": 8.774336, + "requests": [ + { + "upload_seconds": 0.5555352090013912, + "status": 202 + }, + { + "upload_seconds": 0.6558055420027813, + "status": 202 + }, + { + "upload_seconds": 0.9479415829991922, + "status": 202 + }, + { + "upload_seconds": 1.1452457920022425, + "status": 202 + }, + { + "upload_seconds": 0.7364269999961834, + "status": 202 + }, + { + "upload_seconds": 1.0115752920028172, + "status": 202 + }, + { + "upload_seconds": 0.7167560000016238, + "status": 202 + }, + { + "upload_seconds": 0.614655125005811, + "status": 202 + } + ], + "jobs": [ + { + "id": "e0e40541-2a18-41b8-a6d2-c0f70e8f1a45", + "ready_seconds": 4.461059, + "queue_lag_seconds": 0.159653, + "attempts": 1 + }, + { + "id": "2fd41873-9992-4e83-be7e-eab1000fc857", + "ready_seconds": 7.839581, + "queue_lag_seconds": 4.040879, + "attempts": 1 + }, + { + "id": "ee1d8b70-3815-4cd5-bb4c-8846d3ebfba1", + "ready_seconds": 5.068001, + "queue_lag_seconds": 0.366455, + "attempts": 1 + }, + { + "id": "97ac83a3-e22f-4731-b3fe-5292f1636ea1", + "ready_seconds": 8.136236, + "queue_lag_seconds": 4.244041, + "attempts": 1 + }, + { + "id": "2791b3f0-7874-4e9a-904f-9c75e7e84c8d", + "ready_seconds": 8.155908, + "queue_lag_seconds": 4.476807, + "attempts": 1 + }, + { + "id": "e50dfa64-98b4-4254-a222-e73704e6591d", + "ready_seconds": 5.172821, + "queue_lag_seconds": 0.524741, + "attempts": 1 + }, + { + "id": "b413f15e-097d-4ceb-952a-973208180b19", + "ready_seconds": 8.308307, + "queue_lag_seconds": 4.407467, + "attempts": 1 + }, + { + "id": "cf8f0b94-5d4b-454b-926b-c5c396ede828", + "ready_seconds": 5.121261, + "queue_lag_seconds": 0.610854, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262348.8572469, + "memory_bytes": 41979904, + "cpu_usec": 1504505, + "rss_bytes": 106389504 + }, + { + "time": 1794262349.364602, + "memory_bytes": 51986432, + "cpu_usec": 1782613, + "rss_bytes": 120537088 + }, + { + "time": 1794262350.057267, + "memory_bytes": 89964544, + "cpu_usec": 2404540, + "rss_bytes": 224448512 + }, + { + "time": 1794262350.557502, + "memory_bytes": 136720384, + "cpu_usec": 3005696, + "rss_bytes": 344702976 + }, + { + "time": 1794262351.574933, + "memory_bytes": 188698624, + "cpu_usec": 3905731, + "rss_bytes": 418521088 + }, + { + "time": 1794262352.352106, + "memory_bytes": 190210048, + "cpu_usec": 4654780, + "rss_bytes": 424382464 + }, + { + "time": 1794262352.955371, + "memory_bytes": 165081088, + "cpu_usec": 5265101, + "rss_bytes": 347693056 + }, + { + "time": 1794262353.4730828, + "memory_bytes": 84316160, + "cpu_usec": 5877246, + "rss_bytes": 110092288 + }, + { + "time": 1794262354.058292, + "memory_bytes": 71311360, + "cpu_usec": 6333640, + "rss_bytes": 151801856 + }, + { + "time": 1794262354.654955, + "memory_bytes": 96731136, + "cpu_usec": 6984175, + "rss_bytes": 197021696 + }, + { + "time": 1794262355.266908, + "memory_bytes": 127197184, + "cpu_usec": 7579041, + "rss_bytes": 329019392 + }, + { + "time": 1794262355.9633112, + "memory_bytes": 180551680, + "cpu_usec": 8213345, + "rss_bytes": 414875648 + }, + { + "time": 1794262356.658124, + "memory_bytes": 191934464, + "cpu_usec": 8867388, + "rss_bytes": 417808384 + }, + { + "time": 1794262357.257552, + "memory_bytes": 191336448, + "cpu_usec": 9496265, + "rss_bytes": 358735872 + }, + { + "time": 1794262357.7575018, + "memory_bytes": 89542656, + "cpu_usec": 10077162, + "rss_bytes": 179494912 + }, + { + "time": 1794262358.1040719, + "memory_bytes": 71675904, + "cpu_usec": 10278841, + "rss_bytes": 113885184 + } + ] + }, + { + "repeat": 2, + "image": "large", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 8.101745708001545, + "throughput": 0.9874415080812697, + "p95_ready_seconds": 6.520202, + "p95_queue_lag_seconds": 4.749383, + "peak_rss_bytes": 281972736, + "peak_cgroup_memory_bytes": 138719232, + "worker_cpu_seconds": 6.765876, + "requests": [ + { + "upload_seconds": 0.4162295000060112, + "status": 202 + }, + { + "upload_seconds": 0.6149917079965235, + "status": 202 + }, + { + "upload_seconds": 0.7204787919981754, + "status": 202 + }, + { + "upload_seconds": 0.8338184999956866, + "status": 202 + }, + { + "upload_seconds": 0.5898740409975289, + "status": 202 + }, + { + "upload_seconds": 0.44792183300160104, + "status": 202 + }, + { + "upload_seconds": 0.40524725000432227, + "status": 202 + }, + { + "upload_seconds": 0.3922059159958735, + "status": 202 + } + ], + "jobs": [ + { + "id": "1b40b858-9b09-4deb-ac61-bcbe83002a64", + "ready_seconds": 3.602113, + "queue_lag_seconds": 1.651728, + "attempts": 1 + }, + { + "id": "d718a11b-9618-4bb0-92bb-922bed1cda30", + "ready_seconds": 5.015496, + "queue_lag_seconds": 3.268629, + "attempts": 1 + }, + { + "id": "471bf22d-f431-49f0-be58-2bab4ae00b6b", + "ready_seconds": 2.086863, + "queue_lag_seconds": 0.392642, + "attempts": 1 + }, + { + "id": "6c37b530-f5dd-4829-b86c-cf5a996faad4", + "ready_seconds": 3.374384, + "queue_lag_seconds": 1.740594, + "attempts": 1 + }, + { + "id": "32eb0f39-66e8-49e3-a976-891f005e6cb2", + "ready_seconds": 4.857129, + "queue_lag_seconds": 3.190966, + "attempts": 1 + }, + { + "id": "7ae9cc9a-04e4-49d2-98ed-749c1ee5079f", + "ready_seconds": 6.520202, + "queue_lag_seconds": 4.749383, + "attempts": 1 + }, + { + "id": "9e3f88d0-424c-45b5-8d51-70ccaa6025c0", + "ready_seconds": 1.734729, + "queue_lag_seconds": 0.168296, + "attempts": 1 + }, + { + "id": "ce237d98-bdcf-427a-8215-2c814ea99c45", + "ready_seconds": 6.48153, + "queue_lag_seconds": 4.722514, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262361.755059, + "memory_bytes": 46284800, + "cpu_usec": 1512139, + "rss_bytes": 106438656 + }, + { + "time": 1794262362.099647, + "memory_bytes": 55246848, + "cpu_usec": 1610221, + "rss_bytes": 107171840 + }, + { + "time": 1794262362.5327559, + "memory_bytes": 92733440, + "cpu_usec": 2069645, + "rss_bytes": 214233088 + }, + { + "time": 1794262362.995512, + "memory_bytes": 124538880, + "cpu_usec": 2487794, + "rss_bytes": 268300288 + }, + { + "time": 1794262363.4179351, + "memory_bytes": 96542720, + "cpu_usec": 2923192, + "rss_bytes": 193921024 + }, + { + "time": 1794262363.926462, + "memory_bytes": 60690432, + "cpu_usec": 3335444, + "rss_bytes": 139583488 + }, + { + "time": 1794262364.321169, + "memory_bytes": 99147776, + "cpu_usec": 3760055, + "rss_bytes": 230227968 + }, + { + "time": 1794262364.72891, + "memory_bytes": 131219456, + "cpu_usec": 4163416, + "rss_bytes": 271032320 + }, + { + "time": 1794262365.107834, + "memory_bytes": 100380672, + "cpu_usec": 4543584, + "rss_bytes": 193835008 + }, + { + "time": 1794262365.495488, + "memory_bytes": 63741952, + "cpu_usec": 4857710, + "rss_bytes": 109920256 + }, + { + "time": 1794262366.006165, + "memory_bytes": 75325440, + "cpu_usec": 5336939, + "rss_bytes": 190963712 + }, + { + "time": 1794262366.420844, + "memory_bytes": 124809216, + "cpu_usec": 5797722, + "rss_bytes": 263680000 + }, + { + "time": 1794262366.8136802, + "memory_bytes": 138719232, + "cpu_usec": 6195877, + "rss_bytes": 281972736 + }, + { + "time": 1794262367.14334, + "memory_bytes": 63270912, + "cpu_usec": 6484149, + "rss_bytes": 111247360 + }, + { + "time": 1794262367.518545, + "memory_bytes": 66465792, + "cpu_usec": 6762218, + "rss_bytes": 150065152 + }, + { + "time": 1794262367.912593, + "memory_bytes": 108752896, + "cpu_usec": 7119600, + "rss_bytes": 254676992 + }, + { + "time": 1794262368.297046, + "memory_bytes": 133009408, + "cpu_usec": 7506426, + "rss_bytes": 276078592 + }, + { + "time": 1794262368.822019, + "memory_bytes": 85430272, + "cpu_usec": 7981602, + "rss_bytes": 177520640 + }, + { + "time": 1794262369.1505752, + "memory_bytes": 62181376, + "cpu_usec": 8238854, + "rss_bytes": 111968256 + }, + { + "time": 1794262369.591321, + "memory_bytes": 54579200, + "cpu_usec": 8278015, + "rss_bytes": 111972352 + } + ] + }, + { + "repeat": 2, + "image": "large", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 8.238695665997511, + "throughput": 0.9710274932253355, + "p95_ready_seconds": 7.160109, + "p95_queue_lag_seconds": 3.687723, + "peak_rss_bytes": 397238272, + "peak_cgroup_memory_bytes": 186429440, + "worker_cpu_seconds": 7.385869, + "requests": [ + { + "upload_seconds": 0.3346457500010729, + "status": 202 + }, + { + "upload_seconds": 0.5753866659943014, + "status": 202 + }, + { + "upload_seconds": 0.7584450409995043, + "status": 202 + }, + { + "upload_seconds": 0.8696108340009232, + "status": 202 + }, + { + "upload_seconds": 0.8214943749990198, + "status": 202 + }, + { + "upload_seconds": 0.7732604170014383, + "status": 202 + }, + { + "upload_seconds": 0.7054776670047431, + "status": 202 + }, + { + "upload_seconds": 0.6835131249972619, + "status": 202 + } + ], + "jobs": [ + { + "id": "33932f1c-0698-4f05-b977-3fc73c84d6c9", + "ready_seconds": 3.923971, + "queue_lag_seconds": 0.420322, + "attempts": 1 + }, + { + "id": "3eaceca7-ee12-495b-8e1d-4c6a423cf73a", + "ready_seconds": 6.82308, + "queue_lag_seconds": 3.365487, + "attempts": 1 + }, + { + "id": "15021bae-702e-468c-bd14-0d65a31afbe8", + "ready_seconds": 4.171333, + "queue_lag_seconds": 0.460526, + "attempts": 1 + }, + { + "id": "e4f47fb2-487f-4c3a-8c90-7cdf88b69cf6", + "ready_seconds": 5.026424, + "queue_lag_seconds": 3.024265, + "attempts": 1 + }, + { + "id": "1a701e3f-bc3b-4aa9-96a8-04836da1cf2f", + "ready_seconds": 6.654839, + "queue_lag_seconds": 3.207782, + "attempts": 1 + }, + { + "id": "c83ed650-1526-4d75-90cd-d23c1132c673", + "ready_seconds": 7.160109, + "queue_lag_seconds": 3.687723, + "attempts": 1 + }, + { + "id": "f0a7cb39-5f52-47ce-8d89-26ced277bfac", + "ready_seconds": 4.321114, + "queue_lag_seconds": 0.444005, + "attempts": 1 + }, + { + "id": "37e4e587-6b46-4114-ab2b-6927bb0cab3f", + "ready_seconds": 3.52955, + "queue_lag_seconds": 0.10123, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262373.329525, + "memory_bytes": 41304064, + "cpu_usec": 1433050, + "rss_bytes": 106479616 + }, + { + "time": 1794262373.8107378, + "memory_bytes": 68022272, + "cpu_usec": 1776641, + "rss_bytes": 174014464 + }, + { + "time": 1794262374.488162, + "memory_bytes": 96083968, + "cpu_usec": 2399862, + "rss_bytes": 226676736 + }, + { + "time": 1794262374.9780211, + "memory_bytes": 140443648, + "cpu_usec": 2973180, + "rss_bytes": 373719040 + }, + { + "time": 1794262375.570348, + "memory_bytes": 159723520, + "cpu_usec": 3486167, + "rss_bytes": 397238272 + }, + { + "time": 1794262376.084664, + "memory_bytes": 168640512, + "cpu_usec": 4093594, + "rss_bytes": 355319808 + }, + { + "time": 1794262376.58712, + "memory_bytes": 173109248, + "cpu_usec": 4555744, + "rss_bytes": 345436160 + }, + { + "time": 1794262377.096107, + "memory_bytes": 82251776, + "cpu_usec": 5020577, + "rss_bytes": 183726080 + }, + { + "time": 1794262377.4688299, + "memory_bytes": 99491840, + "cpu_usec": 5448708, + "rss_bytes": 187719680 + }, + { + "time": 1794262377.9759092, + "memory_bytes": 103354368, + "cpu_usec": 5950011, + "rss_bytes": 201535488 + }, + { + "time": 1794262378.575902, + "memory_bytes": 100859904, + "cpu_usec": 6486176, + "rss_bytes": 258154496 + }, + { + "time": 1794262379.072046, + "memory_bytes": 150761472, + "cpu_usec": 7050597, + "rss_bytes": 338993152 + }, + { + "time": 1794262379.481832, + "memory_bytes": 168144896, + "cpu_usec": 7503146, + "rss_bytes": 355807232 + }, + { + "time": 1794262379.982485, + "memory_bytes": 186429440, + "cpu_usec": 7952800, + "rss_bytes": 376799232 + }, + { + "time": 1794262380.500739, + "memory_bytes": 113004544, + "cpu_usec": 8412230, + "rss_bytes": 253005824 + }, + { + "time": 1794262380.865719, + "memory_bytes": 71774208, + "cpu_usec": 8700431, + "rss_bytes": 120758272 + }, + { + "time": 1794262381.233644, + "memory_bytes": 59719680, + "cpu_usec": 8818919, + "rss_bytes": 120791040 + } + ] + }, + { + "repeat": 2, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.10356504200172, + "throughput": 0.8787766070863307, + "p95_ready_seconds": 7.155672, + "p95_queue_lag_seconds": 6.245195, + "peak_rss_bytes": 198897664, + "peak_cgroup_memory_bytes": 102072320, + "worker_cpu_seconds": 7.205255, + "requests": [ + { + "upload_seconds": 0.4608520830006455, + "status": 202 + }, + { + "upload_seconds": 0.6706778750012745, + "status": 202 + }, + { + "upload_seconds": 0.8592802909988677, + "status": 202 + }, + { + "upload_seconds": 0.9627979590004543, + "status": 202 + }, + { + "upload_seconds": 0.6193941250021453, + "status": 202 + }, + { + "upload_seconds": 1.1034054579940857, + "status": 202 + }, + { + "upload_seconds": 0.7112387499946635, + "status": 202 + }, + { + "upload_seconds": 1.002451292006299, + "status": 202 + } + ], + "jobs": [ + { + "id": "58ce9899-c415-47b5-8868-cede8b3630ac", + "ready_seconds": 1.696655, + "queue_lag_seconds": 0.154809, + "attempts": 1 + }, + { + "id": "76f2658e-3368-454f-9722-7db3afa455de", + "ready_seconds": 5.877514, + "queue_lag_seconds": 4.924412, + "attempts": 1 + }, + { + "id": "2aa5d55e-5d00-413b-8298-3e94e8084c73", + "ready_seconds": 4.402172, + "queue_lag_seconds": 3.268996, + "attempts": 1 + }, + { + "id": "02adb4c0-d52b-47f5-b20f-91eaa96d93de", + "ready_seconds": 6.499813, + "queue_lag_seconds": 5.538896, + "attempts": 1 + }, + { + "id": "d91deed3-3fbd-4c47-9c92-2c60a2666318", + "ready_seconds": 7.155672, + "queue_lag_seconds": 6.245195, + "attempts": 1 + }, + { + "id": "8c90a845-ac18-4532-9cc5-3961b42b19e6", + "ready_seconds": 2.640209, + "queue_lag_seconds": 1.750154, + "attempts": 1 + }, + { + "id": "c33aa07b-4157-4142-8263-c4a74841726f", + "ready_seconds": 3.482302, + "queue_lag_seconds": 2.567262, + "attempts": 1 + }, + { + "id": "be0fbbc5-10aa-4584-af14-d3b233c27635", + "ready_seconds": 5.005695, + "queue_lag_seconds": 4.267294, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262384.9518871, + "memory_bytes": 50688000, + "cpu_usec": 1535289, + "rss_bytes": 106483712 + }, + { + "time": 1794262385.391333, + "memory_bytes": 52957184, + "cpu_usec": 1736056, + "rss_bytes": 107204608 + }, + { + "time": 1794262385.952516, + "memory_bytes": 79167488, + "cpu_usec": 2237909, + "rss_bytes": 184524800 + }, + { + "time": 1794262386.37323, + "memory_bytes": 56131584, + "cpu_usec": 2693456, + "rss_bytes": 107274240 + }, + { + "time": 1794262386.7641852, + "memory_bytes": 49291264, + "cpu_usec": 2780661, + "rss_bytes": 107274240 + }, + { + "time": 1794262387.166975, + "memory_bytes": 86786048, + "cpu_usec": 3181564, + "rss_bytes": 183619584 + }, + { + "time": 1794262387.525435, + "memory_bytes": 53587968, + "cpu_usec": 3532657, + "rss_bytes": 107507712 + }, + { + "time": 1794262387.878753, + "memory_bytes": 84377600, + "cpu_usec": 3799155, + "rss_bytes": 180383744 + }, + { + "time": 1794262388.267936, + "memory_bytes": 96100352, + "cpu_usec": 4179284, + "rss_bytes": 192999424 + }, + { + "time": 1794262388.689367, + "memory_bytes": 60686336, + "cpu_usec": 4510952, + "rss_bytes": 110395392 + }, + { + "time": 1794262389.069269, + "memory_bytes": 77176832, + "cpu_usec": 4889609, + "rss_bytes": 168751104 + }, + { + "time": 1794262389.402836, + "memory_bytes": 102072320, + "cpu_usec": 5221784, + "rss_bytes": 198897664 + }, + { + "time": 1794262389.7577758, + "memory_bytes": 62373888, + "cpu_usec": 5415786, + "rss_bytes": 113463296 + }, + { + "time": 1794262390.144099, + "memory_bytes": 89358336, + "cpu_usec": 5808430, + "rss_bytes": 190545920 + }, + { + "time": 1794262390.5190332, + "memory_bytes": 58834944, + "cpu_usec": 6176317, + "rss_bytes": 113463296 + }, + { + "time": 1794262390.871752, + "memory_bytes": 94416896, + "cpu_usec": 6534606, + "rss_bytes": 190406656 + }, + { + "time": 1794262391.256792, + "memory_bytes": 84656128, + "cpu_usec": 6942327, + "rss_bytes": 180928512 + }, + { + "time": 1794262391.602048, + "memory_bytes": 68218880, + "cpu_usec": 7251000, + "rss_bytes": 152457216 + }, + { + "time": 1794262391.9413068, + "memory_bytes": 100884480, + "cpu_usec": 7583109, + "rss_bytes": 198283264 + }, + { + "time": 1794262392.369327, + "memory_bytes": 59207680, + "cpu_usec": 7819707, + "rss_bytes": 114708480 + }, + { + "time": 1794262392.796236, + "memory_bytes": 91262976, + "cpu_usec": 8191147, + "rss_bytes": 188411904 + }, + { + "time": 1794262393.2521758, + "memory_bytes": 69705728, + "cpu_usec": 8640936, + "rss_bytes": 114708480 + }, + { + "time": 1794262393.590945, + "memory_bytes": 65884160, + "cpu_usec": 8740544, + "rss_bytes": 114712576 + } + ] + }, + { + "repeat": 2, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.2421264169970527, + "throughput": 3.5680414535745224, + "p95_ready_seconds": 1.812734, + "p95_queue_lag_seconds": 1.400673, + "peak_rss_bytes": 173174784, + "peak_cgroup_memory_bytes": 55123968, + "worker_cpu_seconds": 1.744227, + "requests": [ + { + "upload_seconds": 0.03406304199597798, + "status": 202 + }, + { + "upload_seconds": 0.05620845799421659, + "status": 202 + }, + { + "upload_seconds": 0.08089262500288896, + "status": 202 + }, + { + "upload_seconds": 0.09777987500274321, + "status": 202 + }, + { + "upload_seconds": 0.07787354099855293, + "status": 202 + }, + { + "upload_seconds": 0.11417283300397685, + "status": 202 + }, + { + "upload_seconds": 0.14169870800105855, + "status": 202 + }, + { + "upload_seconds": 0.1499457500030985, + "status": 202 + } + ], + "jobs": [ + { + "id": "22225742-842a-459f-a56f-a5ede53ff4ad", + "ready_seconds": 1.123713, + "queue_lag_seconds": 0.636735, + "attempts": 1 + }, + { + "id": "180f58f4-cbca-4e4d-8cce-870a43212930", + "ready_seconds": 1.812734, + "queue_lag_seconds": 1.400673, + "attempts": 1 + }, + { + "id": "964c6011-8364-4c55-9769-328d71df88bb", + "ready_seconds": 0.658602, + "queue_lag_seconds": 0.242416, + "attempts": 1 + }, + { + "id": "4c9a468b-0df8-4f8e-8897-0bb4fb154f2f", + "ready_seconds": 1.46804, + "queue_lag_seconds": 1.09217, + "attempts": 1 + }, + { + "id": "3b31b076-2844-4f43-af58-8c6ae233ace1", + "ready_seconds": 0.625811, + "queue_lag_seconds": 0.146498, + "attempts": 1 + }, + { + "id": "9a8ee4c0-e2c1-49e3-9d00-9fda7905db70", + "ready_seconds": 1.457865, + "queue_lag_seconds": 1.070151, + "attempts": 1 + }, + { + "id": "2fcdd4f3-5106-41c6-859f-5affde9ba40a", + "ready_seconds": 1.106118, + "queue_lag_seconds": 0.61946, + "attempts": 1 + }, + { + "id": "95c50bcc-ccdb-48ea-8b4a-443e17865997", + "ready_seconds": 1.798918, + "queue_lag_seconds": 1.378992, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262396.595484, + "memory_bytes": 42094592, + "cpu_usec": 1157632, + "rss_bytes": 120750080 + }, + { + "time": 1794262397.1200352, + "memory_bytes": 51560448, + "cpu_usec": 1707624, + "rss_bytes": 154648576 + }, + { + "time": 1794262397.512683, + "memory_bytes": 55123968, + "cpu_usec": 2184197, + "rss_bytes": 154685440 + }, + { + "time": 1794262397.920637, + "memory_bytes": 54521856, + "cpu_usec": 2569106, + "rss_bytes": 173174784 + }, + { + "time": 1794262398.2757602, + "memory_bytes": 50442240, + "cpu_usec": 2901859, + "rss_bytes": 106463232 + } + ] + }, + { + "repeat": 2, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.2075934589956887, + "throughput": 3.623855636734618, + "p95_ready_seconds": 1.758009, + "p95_queue_lag_seconds": 1.05238, + "peak_rss_bytes": 276193280, + "peak_cgroup_memory_bytes": 81403904, + "worker_cpu_seconds": 1.686702, + "requests": [ + { + "upload_seconds": 0.038460707997728605, + "status": 202 + }, + { + "upload_seconds": 0.05114162500103703, + "status": 202 + }, + { + "upload_seconds": 0.0650086250025197, + "status": 202 + }, + { + "upload_seconds": 0.07656995899742469, + "status": 202 + }, + { + "upload_seconds": 0.06139033399813343, + "status": 202 + }, + { + "upload_seconds": 0.08048520800366532, + "status": 202 + }, + { + "upload_seconds": 0.10385104199667694, + "status": 202 + }, + { + "upload_seconds": 0.10880170900054509, + "status": 202 + } + ], + "jobs": [ + { + "id": "159f8845-db5a-4058-9e02-724f4c84b7e4", + "ready_seconds": 0.901334, + "queue_lag_seconds": 0.082086, + "attempts": 1 + }, + { + "id": "f0575066-e653-4232-8936-f52b8fd26409", + "ready_seconds": 1.671008, + "queue_lag_seconds": 0.858153, + "attempts": 1 + }, + { + "id": "8effb44b-ed55-43ef-a551-be6f61c62b39", + "ready_seconds": 1.0686, + "queue_lag_seconds": 0.285051, + "attempts": 1 + }, + { + "id": "eb3f04bb-f355-4a91-9112-06348b0799c9", + "ready_seconds": 1.078856, + "queue_lag_seconds": 0.280137, + "attempts": 1 + }, + { + "id": "b1b17332-00f8-416c-998f-486921fd99ab", + "ready_seconds": 1.751065, + "queue_lag_seconds": 1.019367, + "attempts": 1 + }, + { + "id": "1d480fd1-58b1-4ac1-a570-ee90f1e2c28f", + "ready_seconds": 1.758009, + "queue_lag_seconds": 1.05238, + "attempts": 1 + }, + { + "id": "337ed589-285d-457e-9ebf-d908604d45cb", + "ready_seconds": 0.883419, + "queue_lag_seconds": 0.09468, + "attempts": 1 + }, + { + "id": "f4dfa406-a2aa-463d-8a33-19a0461bdec4", + "ready_seconds": 1.640444, + "queue_lag_seconds": 0.843, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262401.2805781, + "memory_bytes": 45793280, + "cpu_usec": 1174367, + "rss_bytes": 155987968 + }, + { + "time": 1794262401.8661351, + "memory_bytes": 81403904, + "cpu_usec": 1655568, + "rss_bytes": 276193280 + }, + { + "time": 1794262402.4577339, + "memory_bytes": 63311872, + "cpu_usec": 2249590, + "rss_bytes": 250191872 + }, + { + "time": 1794262402.867541, + "memory_bytes": 65937408, + "cpu_usec": 2779692, + "rss_bytes": 167735296 + }, + { + "time": 1794262403.2292979, + "memory_bytes": 44146688, + "cpu_usec": 2861069, + "rss_bytes": 107249664 + } + ] + }, + { + "repeat": 2, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.7222723750019213, + "throughput": 4.645026022664432, + "p95_ready_seconds": 1.514625, + "p95_queue_lag_seconds": 1.291204, + "peak_rss_bytes": 116092928, + "peak_cgroup_memory_bytes": 46608384, + "worker_cpu_seconds": 1.418109, + "requests": [ + { + "upload_seconds": 0.03548512500128709, + "status": 202 + }, + { + "upload_seconds": 0.05716745799873024, + "status": 202 + }, + { + "upload_seconds": 0.06340191700292053, + "status": 202 + }, + { + "upload_seconds": 0.07141516600677278, + "status": 202 + }, + { + "upload_seconds": 0.028250875002413522, + "status": 202 + }, + { + "upload_seconds": 0.052529375003359746, + "status": 202 + }, + { + "upload_seconds": 0.029087916998832952, + "status": 202 + }, + { + "upload_seconds": 0.04534787499869708, + "status": 202 + } + ], + "jobs": [ + { + "id": "270b668e-598b-4dbe-9a7b-28dde474ba66", + "ready_seconds": 0.295571, + "queue_lag_seconds": 0.100362, + "attempts": 1 + }, + { + "id": "c6479645-b1b0-4fef-bf17-4fa4d164eacb", + "ready_seconds": 0.465528, + "queue_lag_seconds": 0.297742, + "attempts": 1 + }, + { + "id": "fb3ad7fa-8553-4016-a561-eb2fac93dfb4", + "ready_seconds": 0.922614, + "queue_lag_seconds": 0.793791, + "attempts": 1 + }, + { + "id": "3260fcc9-0a00-4b97-a928-fec52ad5d0b1", + "ready_seconds": 1.154276, + "queue_lag_seconds": 0.916923, + "attempts": 1 + }, + { + "id": "559adef9-6f6b-416a-a826-df78660bd3eb", + "ready_seconds": 0.612838, + "queue_lag_seconds": 0.451514, + "attempts": 1 + }, + { + "id": "c5fc2c84-c12a-4384-873a-1c42faafb767", + "ready_seconds": 1.290854, + "queue_lag_seconds": 1.152609, + "attempts": 1 + }, + { + "id": "859524b5-8367-4a4b-a835-b24287388b12", + "ready_seconds": 0.798171, + "queue_lag_seconds": 0.615507, + "attempts": 1 + }, + { + "id": "57136a84-a43d-493c-bd16-2cfca4194267", + "ready_seconds": 1.514625, + "queue_lag_seconds": 1.291204, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262405.5725389, + "memory_bytes": 43700224, + "cpu_usec": 898962, + "rss_bytes": 110108672 + }, + { + "time": 1794262405.931159, + "memory_bytes": 41541632, + "cpu_usec": 1258898, + "rss_bytes": 116092928 + }, + { + "time": 1794262406.266677, + "memory_bytes": 46608384, + "cpu_usec": 1588260, + "rss_bytes": 106504192 + }, + { + "time": 1794262406.6328561, + "memory_bytes": 46579712, + "cpu_usec": 1934212, + "rss_bytes": 106582016 + }, + { + "time": 1794262407.030196, + "memory_bytes": 45948928, + "cpu_usec": 2317071, + "rss_bytes": 106602496 + } + ] + }, + { + "repeat": 3, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.0493234580062563, + "throughput": 3.903727334377479, + "p95_ready_seconds": 1.879584, + "p95_queue_lag_seconds": 1.052141, + "peak_rss_bytes": 346427392, + "peak_cgroup_memory_bytes": 87994368, + "worker_cpu_seconds": 1.58035, + "requests": [ + { + "upload_seconds": 0.03708612499758601, + "status": 202 + }, + { + "upload_seconds": 0.04979004099732265, + "status": 202 + }, + { + "upload_seconds": 0.062493375000485685, + "status": 202 + }, + { + "upload_seconds": 0.07259312500536907, + "status": 202 + }, + { + "upload_seconds": 0.05210570799681591, + "status": 202 + }, + { + "upload_seconds": 0.04339720799907809, + "status": 202 + }, + { + "upload_seconds": 0.038661874998069834, + "status": 202 + }, + { + "upload_seconds": 0.03924845800065668, + "status": 202 + } + ], + "jobs": [ + { + "id": "fbcb2196-1abf-4fb3-a818-fe3aee15ea75", + "ready_seconds": 1.072389, + "queue_lag_seconds": 0.109725, + "attempts": 1 + }, + { + "id": "97441d05-e70f-4b67-a7a7-b0f2ecd3809b", + "ready_seconds": 1.851275, + "queue_lag_seconds": 1.028243, + "attempts": 1 + }, + { + "id": "1f1d4d72-269a-4984-8f39-099ff5e62cce", + "ready_seconds": 1.091508, + "queue_lag_seconds": 0.089204, + "attempts": 1 + }, + { + "id": "f498e151-5de9-4898-82d0-00d613c71b73", + "ready_seconds": 1.065876, + "queue_lag_seconds": 0.162786, + "attempts": 1 + }, + { + "id": "3369e169-4c12-4357-af6d-8ead89d4100c", + "ready_seconds": 1.82523, + "queue_lag_seconds": 1.032192, + "attempts": 1 + }, + { + "id": "af3058d6-8ef2-4d7b-a16d-5cdc8ee095db", + "ready_seconds": 1.879584, + "queue_lag_seconds": 1.052141, + "attempts": 1 + }, + { + "id": "8f9ed5dc-eb28-4669-a444-9c2bf7c178bd", + "ready_seconds": 1.089309, + "queue_lag_seconds": 0.112428, + "attempts": 1 + }, + { + "id": "c143932d-a6f1-49b4-9e26-280cb1bf78d3", + "ready_seconds": 1.731002, + "queue_lag_seconds": 1.039114, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262410.393358, + "memory_bytes": 55742464, + "cpu_usec": 1271330, + "rss_bytes": 226807808 + }, + { + "time": 1794262410.898071, + "memory_bytes": 87994368, + "cpu_usec": 1828256, + "rss_bytes": 346427392 + }, + { + "time": 1794262411.4896598, + "memory_bytes": 78336000, + "cpu_usec": 2311204, + "rss_bytes": 275132416 + }, + { + "time": 1794262411.910083, + "memory_bytes": 49283072, + "cpu_usec": 2851680, + "rss_bytes": 106897408 + } + ] + }, + { + "repeat": 3, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.0718359999955283, + "throughput": 3.8613094858942825, + "p95_ready_seconds": 1.579124, + "p95_queue_lag_seconds": 1.313482, + "peak_rss_bytes": 167481344, + "peak_cgroup_memory_bytes": 60809216, + "worker_cpu_seconds": 1.489548, + "requests": [ + { + "upload_seconds": 0.046921875000407454, + "status": 202 + }, + { + "upload_seconds": 0.06458500000007916, + "status": 202 + }, + { + "upload_seconds": 0.09368895899388008, + "status": 202 + }, + { + "upload_seconds": 0.10713133300305344, + "status": 202 + }, + { + "upload_seconds": 0.05416333399625728, + "status": 202 + }, + { + "upload_seconds": 0.08453687500150409, + "status": 202 + }, + { + "upload_seconds": 0.04616966699541081, + "status": 202 + }, + { + "upload_seconds": 0.06727762499940582, + "status": 202 + } + ], + "jobs": [ + { + "id": "cb1d7378-a81c-4cba-abc3-765e49e4feb8", + "ready_seconds": 0.380824, + "queue_lag_seconds": 0.103185, + "attempts": 1 + }, + { + "id": "0bffdb9c-b637-4da7-9206-0798e9b0e2f0", + "ready_seconds": 1.023788, + "queue_lag_seconds": 0.871741, + "attempts": 1 + }, + { + "id": "056073e3-d257-4861-8d33-9872d20b520d", + "ready_seconds": 1.579124, + "queue_lag_seconds": 1.313482, + "attempts": 1 + }, + { + "id": "3dff25ed-0223-42b6-94d6-e627ab3c9000", + "ready_seconds": 0.745562, + "queue_lag_seconds": 0.544542, + "attempts": 1 + }, + { + "id": "66eafee7-5bd0-419e-b069-6b33176339b3", + "ready_seconds": 0.894289, + "queue_lag_seconds": 0.733248, + "attempts": 1 + }, + { + "id": "176ef072-0811-4a76-a378-5f7c20103066", + "ready_seconds": 1.320301, + "queue_lag_seconds": 1.201136, + "attempts": 1 + }, + { + "id": "49fc3f2c-4dea-4f56-a80d-2ca4a207eb71", + "ready_seconds": 0.557781, + "queue_lag_seconds": 0.379291, + "attempts": 1 + }, + { + "id": "ae186aba-bbe9-4406-b2fe-6a1cf2e2cc04", + "ready_seconds": 1.208355, + "queue_lag_seconds": 1.014114, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262415.613364, + "memory_bytes": 43040768, + "cpu_usec": 1254626, + "rss_bytes": 134688768 + }, + { + "time": 1794262415.977576, + "memory_bytes": 44519424, + "cpu_usec": 1625601, + "rss_bytes": 104448000 + }, + { + "time": 1794262416.309793, + "memory_bytes": 59355136, + "cpu_usec": 1972565, + "rss_bytes": 167481344 + }, + { + "time": 1794262416.659306, + "memory_bytes": 57217024, + "cpu_usec": 2302566, + "rss_bytes": 166821888 + }, + { + "time": 1794262417.0047119, + "memory_bytes": 60809216, + "cpu_usec": 2648258, + "rss_bytes": 166809600 + }, + { + "time": 1794262417.332344, + "memory_bytes": 48402432, + "cpu_usec": 2744174, + "rss_bytes": 106450944 + } + ] + }, + { + "repeat": 3, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.7169063330002245, + "throughput": 4.659543649082081, + "p95_ready_seconds": 1.565986, + "p95_queue_lag_seconds": 1.147304, + "peak_rss_bytes": 225964032, + "peak_cgroup_memory_bytes": 68136960, + "worker_cpu_seconds": 1.301956, + "requests": [ + { + "upload_seconds": 0.041546416003257036, + "status": 202 + }, + { + "upload_seconds": 0.060863958999107126, + "status": 202 + }, + { + "upload_seconds": 0.07863833400188014, + "status": 202 + }, + { + "upload_seconds": 0.1037755420038593, + "status": 202 + }, + { + "upload_seconds": 0.08276591699541314, + "status": 202 + }, + { + "upload_seconds": 0.10874649999459507, + "status": 202 + }, + { + "upload_seconds": 0.10369779200118501, + "status": 202 + }, + { + "upload_seconds": 0.0915510409977287, + "status": 202 + } + ], + "jobs": [ + { + "id": "a5f773f5-7603-4661-ae9a-83b0b3f817a0", + "ready_seconds": 0.82323, + "queue_lag_seconds": 0.428073, + "attempts": 1 + }, + { + "id": "6f123c49-0e98-468f-9a0f-c522c728df63", + "ready_seconds": 1.515223, + "queue_lag_seconds": 1.099548, + "attempts": 1 + }, + { + "id": "13c2931e-f809-4b66-96c7-7b58429a3446", + "ready_seconds": 0.471413, + "queue_lag_seconds": 0.062399, + "attempts": 1 + }, + { + "id": "acd77d65-886a-497b-a023-f398646974fe", + "ready_seconds": 0.452546, + "queue_lag_seconds": 0.063642, + "attempts": 1 + }, + { + "id": "ef507876-972e-4087-a198-f0ce5536cc29", + "ready_seconds": 1.18232, + "queue_lag_seconds": 0.812552, + "attempts": 1 + }, + { + "id": "86fa8bc4-fc9d-4bee-b587-2a7897dbaa0a", + "ready_seconds": 1.159715, + "queue_lag_seconds": 0.832331, + "attempts": 1 + }, + { + "id": "ca41c982-9e7b-48d8-8c57-994817c94498", + "ready_seconds": 0.849327, + "queue_lag_seconds": 0.447346, + "attempts": 1 + }, + { + "id": "d169a9e2-5ce7-4261-a22a-1ece2a5a3563", + "ready_seconds": 1.565986, + "queue_lag_seconds": 1.147304, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262420.793129, + "memory_bytes": 56025088, + "cpu_usec": 1490008, + "rss_bytes": 192344064 + }, + { + "time": 1794262421.21904, + "memory_bytes": 63148032, + "cpu_usec": 1917366, + "rss_bytes": 217477120 + }, + { + "time": 1794262421.61412, + "memory_bytes": 68136960, + "cpu_usec": 2351511, + "rss_bytes": 225693696 + }, + { + "time": 1794262422.095878, + "memory_bytes": 65114112, + "cpu_usec": 2791964, + "rss_bytes": 225964032 + } + ] + }, + { + "repeat": 3, + "image": "large", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 11.567342832997383, + "throughput": 0.691602221486765, + "p95_ready_seconds": 9.949, + "p95_queue_lag_seconds": 5.551759, + "peak_rss_bytes": 378175488, + "peak_cgroup_memory_bytes": 177049600, + "worker_cpu_seconds": 10.350173, + "requests": [ + { + "upload_seconds": 0.5822087910055416, + "status": 202 + }, + { + "upload_seconds": 1.0521450830055983, + "status": 202 + }, + { + "upload_seconds": 1.197281708002265, + "status": 202 + }, + { + "upload_seconds": 1.3832097920021624, + "status": 202 + }, + { + "upload_seconds": 1.0122472919974825, + "status": 202 + }, + { + "upload_seconds": 0.7380604999998468, + "status": 202 + }, + { + "upload_seconds": 0.9001225829997566, + "status": 202 + }, + { + "upload_seconds": 0.986606499995105, + "status": 202 + } + ], + "jobs": [ + { + "id": "3ab05215-56fd-4ef1-9f07-63b42aec55dc", + "ready_seconds": 9.949, + "queue_lag_seconds": 5.29829, + "attempts": 1 + }, + { + "id": "34691cf0-2b94-49f4-a353-52d6d6bb1175", + "ready_seconds": 6.215799, + "queue_lag_seconds": 0.807096, + "attempts": 1 + }, + { + "id": "930f6c90-f771-4243-a97f-d5977d418647", + "ready_seconds": 9.630719, + "queue_lag_seconds": 5.301089, + "attempts": 1 + }, + { + "id": "f32a3a06-7f5e-4ffe-9977-3ca5377e0c6e", + "ready_seconds": 6.042704, + "queue_lag_seconds": 0.454156, + "attempts": 1 + }, + { + "id": "2b0e0991-2462-4c1a-af48-cb6ad1671840", + "ready_seconds": 4.482368, + "queue_lag_seconds": 0.247504, + "attempts": 1 + }, + { + "id": "093171c9-4d36-47cd-88cd-be205b5de3de", + "ready_seconds": 9.739683, + "queue_lag_seconds": 5.551759, + "attempts": 1 + }, + { + "id": "92142d14-8e43-450e-91ac-b42cebadb100", + "ready_seconds": 7.883405, + "queue_lag_seconds": 3.759794, + "attempts": 1 + }, + { + "id": "f53f0efe-65c0-43b6-b6a2-a348b294f895", + "ready_seconds": 6.53409, + "queue_lag_seconds": 0.636628, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262425.999084, + "memory_bytes": 41082880, + "cpu_usec": 1663015, + "rss_bytes": 106700800 + }, + { + "time": 1794262426.380343, + "memory_bytes": 48549888, + "cpu_usec": 1711939, + "rss_bytes": 106708992 + }, + { + "time": 1794262426.8502111, + "memory_bytes": 72732672, + "cpu_usec": 2101364, + "rss_bytes": 180154368 + }, + { + "time": 1794262427.480101, + "memory_bytes": 90595328, + "cpu_usec": 2668924, + "rss_bytes": 212520960 + }, + { + "time": 1794262428.167731, + "memory_bytes": 116719616, + "cpu_usec": 3399935, + "rss_bytes": 299827200 + }, + { + "time": 1794262429.151486, + "memory_bytes": 136507392, + "cpu_usec": 4315373, + "rss_bytes": 367857664 + }, + { + "time": 1794262429.644265, + "memory_bytes": 153702400, + "cpu_usec": 4866121, + "rss_bytes": 336216064 + }, + { + "time": 1794262430.258294, + "memory_bytes": 171859968, + "cpu_usec": 5443319, + "rss_bytes": 358014976 + }, + { + "time": 1794262430.9448361, + "memory_bytes": 158593024, + "cpu_usec": 6109826, + "rss_bytes": 346882048 + }, + { + "time": 1794262431.7647948, + "memory_bytes": 106033152, + "cpu_usec": 6844068, + "rss_bytes": 271921152 + }, + { + "time": 1794262432.253286, + "memory_bytes": 75567104, + "cpu_usec": 7542413, + "rss_bytes": 162009088 + }, + { + "time": 1794262432.756811, + "memory_bytes": 100659200, + "cpu_usec": 8022771, + "rss_bytes": 193961984 + }, + { + "time": 1794262433.343982, + "memory_bytes": 117932032, + "cpu_usec": 8571197, + "rss_bytes": 287518720 + }, + { + "time": 1794262433.947423, + "memory_bytes": 142024704, + "cpu_usec": 9120937, + "rss_bytes": 378175488 + }, + { + "time": 1794262434.645704, + "memory_bytes": 153182208, + "cpu_usec": 9790721, + "rss_bytes": 336863232 + }, + { + "time": 1794262435.561161, + "memory_bytes": 177049600, + "cpu_usec": 10665993, + "rss_bytes": 367210496 + }, + { + "time": 1794262436.239607, + "memory_bytes": 145113088, + "cpu_usec": 11437552, + "rss_bytes": 337195008 + }, + { + "time": 1794262436.645513, + "memory_bytes": 70598656, + "cpu_usec": 11891048, + "rss_bytes": 115445760 + }, + { + "time": 1794262437.032929, + "memory_bytes": 73572352, + "cpu_usec": 12013188, + "rss_bytes": 115449856 + } + ] + }, + { + "repeat": 3, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.895933082996635, + "throughput": 0.8084129038570137, + "p95_ready_seconds": 7.692664, + "p95_queue_lag_seconds": 6.867809, + "peak_rss_bytes": 197115904, + "peak_cgroup_memory_bytes": 101888000, + "worker_cpu_seconds": 7.538266, + "requests": [ + { + "upload_seconds": 0.4662438750019646, + "status": 202 + }, + { + "upload_seconds": 0.6516994580015307, + "status": 202 + }, + { + "upload_seconds": 0.9337635410047369, + "status": 202 + }, + { + "upload_seconds": 1.0977309580048313, + "status": 202 + }, + { + "upload_seconds": 1.2482558750052704, + "status": 202 + }, + { + "upload_seconds": 1.2442702500047744, + "status": 202 + }, + { + "upload_seconds": 1.3717015000001993, + "status": 202 + }, + { + "upload_seconds": 1.3582418339938158, + "status": 202 + } + ], + "jobs": [ + { + "id": "ad0538c4-d78f-4441-abc2-94017e43989a", + "ready_seconds": 4.663425, + "queue_lag_seconds": 3.660659, + "attempts": 1 + }, + { + "id": "22cb074d-623d-403a-88a5-98c747486509", + "ready_seconds": 7.034566, + "queue_lag_seconds": 5.984522, + "attempts": 1 + }, + { + "id": "4afc9b98-cbb1-461a-b05a-2f2d2817f877", + "ready_seconds": 2.090172, + "queue_lag_seconds": 0.199733, + "attempts": 1 + }, + { + "id": "33b0f653-e3f4-49c0-8cb5-e794807f3859", + "ready_seconds": 5.792541, + "queue_lag_seconds": 4.419849, + "attempts": 1 + }, + { + "id": "589106c2-8835-4297-bdf9-ce7b37bd0d51", + "ready_seconds": 7.692664, + "queue_lag_seconds": 6.867809, + "attempts": 1 + }, + { + "id": "8d6c6505-44a5-4256-9570-f064b95b020e", + "ready_seconds": 2.979272, + "queue_lag_seconds": 2.096541, + "attempts": 1 + }, + { + "id": "6e4e160e-70b4-478e-9feb-1fe797c19cc1", + "ready_seconds": 6.533553, + "queue_lag_seconds": 5.647585, + "attempts": 1 + }, + { + "id": "1e65c301-e37a-4806-9dcf-2764d82ff3ed", + "ready_seconds": 3.860303, + "queue_lag_seconds": 2.863414, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262441.173097, + "memory_bytes": 45621248, + "cpu_usec": 1729800, + "rss_bytes": 106565632 + }, + { + "time": 1794262441.683848, + "memory_bytes": 52445184, + "cpu_usec": 2007744, + "rss_bytes": 121364480 + }, + { + "time": 1794262442.398595, + "memory_bytes": 82268160, + "cpu_usec": 2508688, + "rss_bytes": 184696832 + }, + { + "time": 1794262442.823581, + "memory_bytes": 52346880, + "cpu_usec": 2978119, + "rss_bytes": 107433984 + }, + { + "time": 1794262443.2013881, + "memory_bytes": 50536448, + "cpu_usec": 3010074, + "rss_bytes": 107433984 + }, + { + "time": 1794262443.56844, + "memory_bytes": 65798144, + "cpu_usec": 3272589, + "rss_bytes": 154914816 + }, + { + "time": 1794262443.9425979, + "memory_bytes": 98009088, + "cpu_usec": 3642023, + "rss_bytes": 193007616 + }, + { + "time": 1794262444.351456, + "memory_bytes": 56811520, + "cpu_usec": 3989156, + "rss_bytes": 133300224 + }, + { + "time": 1794262444.7525601, + "memory_bytes": 96026624, + "cpu_usec": 4365059, + "rss_bytes": 191180800 + }, + { + "time": 1794262445.159318, + "memory_bytes": 57802752, + "cpu_usec": 4638462, + "rss_bytes": 107241472 + }, + { + "time": 1794262445.55554, + "memory_bytes": 82378752, + "cpu_usec": 5002983, + "rss_bytes": 180408320 + }, + { + "time": 1794262445.967915, + "memory_bytes": 77910016, + "cpu_usec": 5416420, + "rss_bytes": 174346240 + }, + { + "time": 1794262446.46769, + "memory_bytes": 61718528, + "cpu_usec": 5769579, + "rss_bytes": 117579776 + }, + { + "time": 1794262446.813834, + "memory_bytes": 92237824, + "cpu_usec": 6089664, + "rss_bytes": 183562240 + }, + { + "time": 1794262447.255758, + "memory_bytes": 86560768, + "cpu_usec": 6547945, + "rss_bytes": 181460992 + }, + { + "time": 1794262447.682785, + "memory_bytes": 59322368, + "cpu_usec": 6751679, + "rss_bytes": 112062464 + }, + { + "time": 1794262448.0586839, + "memory_bytes": 96845824, + "cpu_usec": 7162998, + "rss_bytes": 187457536 + }, + { + "time": 1794262448.392413, + "memory_bytes": 68132864, + "cpu_usec": 7494310, + "rss_bytes": 112062464 + }, + { + "time": 1794262448.806738, + "memory_bytes": 72351744, + "cpu_usec": 7748876, + "rss_bytes": 128098304 + }, + { + "time": 1794262449.289658, + "memory_bytes": 101888000, + "cpu_usec": 8201010, + "rss_bytes": 197115904 + }, + { + "time": 1794262449.658975, + "memory_bytes": 66256896, + "cpu_usec": 8496781, + "rss_bytes": 112062464 + }, + { + "time": 1794262450.018466, + "memory_bytes": 97886208, + "cpu_usec": 8868606, + "rss_bytes": 188559360 + }, + { + "time": 1794262450.372195, + "memory_bytes": 68079616, + "cpu_usec": 9226409, + "rss_bytes": 112062464 + }, + { + "time": 1794262450.707536, + "memory_bytes": 65867776, + "cpu_usec": 9268066, + "rss_bytes": 112066560 + } + ] + }, + { + "repeat": 3, + "image": "large", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 10.974901708999823, + "throughput": 0.7289359132428226, + "p95_ready_seconds": 9.261154, + "p95_queue_lag_seconds": 7.115312, + "peak_rss_bytes": 281636864, + "peak_cgroup_memory_bytes": 137314304, + "worker_cpu_seconds": 8.619296, + "requests": [ + { + "upload_seconds": 0.5528413339998224, + "status": 202 + }, + { + "upload_seconds": 0.618678999999247, + "status": 202 + }, + { + "upload_seconds": 0.9114771669992479, + "status": 202 + }, + { + "upload_seconds": 1.132410833000904, + "status": 202 + }, + { + "upload_seconds": 0.7579690839993418, + "status": 202 + }, + { + "upload_seconds": 0.8154654999962077, + "status": 202 + }, + { + "upload_seconds": 0.4669017080013873, + "status": 202 + }, + { + "upload_seconds": 0.8220295840001199, + "status": 202 + } + ], + "jobs": [ + { + "id": "11319b09-8480-4bf7-8343-467739eebbbe", + "ready_seconds": 5.278422, + "queue_lag_seconds": 2.866249, + "attempts": 1 + }, + { + "id": "04bcebd4-b08d-4cd0-a0d4-0cf7941f7ff6", + "ready_seconds": 3.067793, + "queue_lag_seconds": 0.269334, + "attempts": 1 + }, + { + "id": "144031aa-beb1-48e3-86a5-872ce73c29d5", + "ready_seconds": 5.366127, + "queue_lag_seconds": 2.950958, + "attempts": 1 + }, + { + "id": "b6ca8afd-927a-4b78-bf48-e9de74dde5e3", + "ready_seconds": 7.427039, + "queue_lag_seconds": 4.944347, + "attempts": 1 + }, + { + "id": "6c86c361-f45c-462c-b7b2-58bfc353c846", + "ready_seconds": 9.261154, + "queue_lag_seconds": 7.115312, + "attempts": 1 + }, + { + "id": "d3024905-e173-431c-b093-f52da938e00e", + "ready_seconds": 3.060139, + "queue_lag_seconds": 0.3589, + "attempts": 1 + }, + { + "id": "27383307-0cf0-48b2-86b5-679938e461c7", + "ready_seconds": 7.223779, + "queue_lag_seconds": 4.788639, + "attempts": 1 + }, + { + "id": "39d7dba3-aea0-46fc-9eb1-2919a447f6f2", + "ready_seconds": 9.095291, + "queue_lag_seconds": 6.945049, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262455.167735, + "memory_bytes": 41877504, + "cpu_usec": 1728128, + "rss_bytes": 106283008 + }, + { + "time": 1794262455.6366248, + "memory_bytes": 46886912, + "cpu_usec": 1838601, + "rss_bytes": 106319872 + }, + { + "time": 1794262456.231374, + "memory_bytes": 67100672, + "cpu_usec": 2411824, + "rss_bytes": 197558272 + }, + { + "time": 1794262456.781374, + "memory_bytes": 112640000, + "cpu_usec": 2948767, + "rss_bytes": 257323008 + }, + { + "time": 1794262457.389336, + "memory_bytes": 132554752, + "cpu_usec": 3488311, + "rss_bytes": 280322048 + }, + { + "time": 1794262457.9726589, + "memory_bytes": 61906944, + "cpu_usec": 4087858, + "rss_bytes": 110342144 + }, + { + "time": 1794262458.36638, + "memory_bytes": 44871680, + "cpu_usec": 4209324, + "rss_bytes": 109940736 + }, + { + "time": 1794262458.890047, + "memory_bytes": 65904640, + "cpu_usec": 4690364, + "rss_bytes": 186933248 + }, + { + "time": 1794262459.3172328, + "memory_bytes": 109899776, + "cpu_usec": 5169719, + "rss_bytes": 258424832 + }, + { + "time": 1794262459.798551, + "memory_bytes": 137314304, + "cpu_usec": 5659124, + "rss_bytes": 281636864 + }, + { + "time": 1794262460.282627, + "memory_bytes": 54554624, + "cpu_usec": 6133829, + "rss_bytes": 110514176 + }, + { + "time": 1794262460.656821, + "memory_bytes": 57778176, + "cpu_usec": 6278094, + "rss_bytes": 110522368 + }, + { + "time": 1794262461.1044202, + "memory_bytes": 61755392, + "cpu_usec": 6620843, + "rss_bytes": 140283904 + }, + { + "time": 1794262461.5962932, + "memory_bytes": 109383680, + "cpu_usec": 7093342, + "rss_bytes": 256442368 + }, + { + "time": 1794262462.117553, + "memory_bytes": 127512576, + "cpu_usec": 7574233, + "rss_bytes": 276664320 + }, + { + "time": 1794262462.503906, + "memory_bytes": 100667392, + "cpu_usec": 8003279, + "rss_bytes": 248053760 + }, + { + "time": 1794262462.8923898, + "memory_bytes": 58302464, + "cpu_usec": 8275401, + "rss_bytes": 110604288 + }, + { + "time": 1794262463.295119, + "memory_bytes": 46878720, + "cpu_usec": 8426428, + "rss_bytes": 110628864 + }, + { + "time": 1794262463.8872309, + "memory_bytes": 74416128, + "cpu_usec": 8986830, + "rss_bytes": 201560064 + }, + { + "time": 1794262464.382762, + "memory_bytes": 115367936, + "cpu_usec": 9503601, + "rss_bytes": 264175616 + }, + { + "time": 1794262464.810621, + "memory_bytes": 133775360, + "cpu_usec": 9973809, + "rss_bytes": 258146304 + }, + { + "time": 1794262465.197512, + "memory_bytes": 54276096, + "cpu_usec": 10269619, + "rss_bytes": 110628864 + }, + { + "time": 1794262465.553686, + "memory_bytes": 49750016, + "cpu_usec": 10347424, + "rss_bytes": 110632960 + } + ] + } + ] +} diff --git a/evidence/overload.json b/evidence/overload.json new file mode 100644 index 00000000..83c44ec9 --- /dev/null +++ b/evidence/overload.json @@ -0,0 +1,3239 @@ +{ + "hypothesis": "With fixed worker CPU, more simultaneous image processes need not improve throughput and can increase RSS; bounded admission protects the queue when the worker stops.", + "profile": { + "settings": [ + [ + 1, + 1 + ], + [ + 2, + 2 + ], + [ + 4, + 4 + ] + ], + "repeats": 3, + "batch": 8, + "upload_clients": 4, + "worker_cpu": 1, + "worker_memory_limit_mib": 1024, + "warmup_images": 1, + "image_sizes_bytes": { + "small": 111817, + "large": 3319965 + } + }, + "runs": [ + { + "repeat": 1, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.8135867090022657, + "throughput": 4.411148339525026, + "p95_ready_seconds": 1.523784, + "p95_queue_lag_seconds": 1.262202, + "peak_rss_bytes": 165265408, + "peak_cgroup_memory_bytes": 56442880, + "worker_cpu_seconds": 1.445814, + "requests": [ + { + "upload_seconds": 0.04025879099935992, + "status": 202 + }, + { + "upload_seconds": 0.0557741669981624, + "status": 202 + }, + { + "upload_seconds": 0.02573179200408049, + "status": 202 + }, + { + "upload_seconds": 0.18365204199653817, + "status": 202 + }, + { + "upload_seconds": 0.15453533300023992, + "status": 202 + }, + { + "upload_seconds": 0.018875291003496386, + "status": 202 + }, + { + "upload_seconds": 0.21073149999574525, + "status": 202 + }, + { + "upload_seconds": 0.13923129100294318, + "status": 202 + } + ], + "jobs": [ + { + "id": "0ccede90-ead1-4a90-a6dd-80cf3eb19dcc", + "ready_seconds": 0.27133, + "queue_lag_seconds": 0.115042, + "attempts": 1 + }, + { + "id": "248fd684-b378-4c0c-9362-2533a2904a1e", + "ready_seconds": 1.231278, + "queue_lag_seconds": 1.014569, + "attempts": 1 + }, + { + "id": "f39b02fd-2265-4dc6-84bd-b91574ec1449", + "ready_seconds": 0.495172, + "queue_lag_seconds": 0.256786, + "attempts": 1 + }, + { + "id": "43fe1dc0-edbb-48f4-b25c-bc2dcb093c1a", + "ready_seconds": 1.008641, + "queue_lag_seconds": 0.855938, + "attempts": 1 + }, + { + "id": "9d1220c6-4852-45cd-b5e1-0888767914e2", + "ready_seconds": 1.523784, + "queue_lag_seconds": 1.262202, + "attempts": 1 + }, + { + "id": "ae449c02-9a46-4794-927c-da9935b99d1c", + "ready_seconds": 0.641612, + "queue_lag_seconds": 0.481173, + "attempts": 1 + }, + { + "id": "c4b1ce2a-851c-4b8c-9407-f1673eb0bfef", + "ready_seconds": 0.754448, + "queue_lag_seconds": 0.561934, + "attempts": 1 + }, + { + "id": "93964e00-34fc-41d3-90e1-6e60e7be1edd", + "ready_seconds": 0.858382, + "queue_lag_seconds": 0.733633, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262308.2044969, + "memory_bytes": 42037248, + "cpu_usec": 846749, + "rss_bytes": 103288832 + }, + { + "time": 1794262308.619572, + "memory_bytes": 40370176, + "cpu_usec": 1244009, + "rss_bytes": 104734720 + }, + { + "time": 1794262308.974692, + "memory_bytes": 46178304, + "cpu_usec": 1614392, + "rss_bytes": 106708992 + }, + { + "time": 1794262309.3393369, + "memory_bytes": 48230400, + "cpu_usec": 1958144, + "rss_bytes": 123957248 + }, + { + "time": 1794262309.7072818, + "memory_bytes": 56442880, + "cpu_usec": 2292563, + "rss_bytes": 165265408 + } + ] + }, + { + "repeat": 1, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.207073791003495, + "throughput": 3.6247088940160097, + "p95_ready_seconds": 1.675188, + "p95_queue_lag_seconds": 1.318631, + "peak_rss_bytes": 144166912, + "peak_cgroup_memory_bytes": 50225152, + "worker_cpu_seconds": 1.626515, + "requests": [ + { + "upload_seconds": 0.032242041001154575, + "status": 202 + }, + { + "upload_seconds": 0.045670040999539196, + "status": 202 + }, + { + "upload_seconds": 0.05951904099492822, + "status": 202 + }, + { + "upload_seconds": 0.0768088330005412, + "status": 202 + }, + { + "upload_seconds": 0.05488000000332249, + "status": 202 + }, + { + "upload_seconds": 0.04967383300390793, + "status": 202 + }, + { + "upload_seconds": 0.04204687500168802, + "status": 202 + }, + { + "upload_seconds": 0.03497491600137437, + "status": 202 + } + ], + "jobs": [ + { + "id": "fd94ff6a-838f-48c5-b8a3-00c7ead78063", + "ready_seconds": 0.469287, + "queue_lag_seconds": 0.10038, + "attempts": 1 + }, + { + "id": "7be0e978-4272-437d-8e74-682e5284dbfe", + "ready_seconds": 1.248626, + "queue_lag_seconds": 0.822855, + "attempts": 1 + }, + { + "id": "3c8bf977-e0f0-4a16-a8f4-e4524ad0e594", + "ready_seconds": 0.849686, + "queue_lag_seconds": 0.454018, + "attempts": 1 + }, + { + "id": "43d83901-5ee0-46ec-b177-5cf893d62d39", + "ready_seconds": 1.675188, + "queue_lag_seconds": 1.318631, + "attempts": 1 + }, + { + "id": "914a5c4b-7357-4abf-9e5f-ef9a123919a9", + "ready_seconds": 0.477878, + "queue_lag_seconds": 0.115539, + "attempts": 1 + }, + { + "id": "dd075e01-2d54-41c2-bebf-e03852480384", + "ready_seconds": 1.300378, + "queue_lag_seconds": 0.826505, + "attempts": 1 + }, + { + "id": "0c5a1967-e3b2-4fdd-bc8f-a42b6daa55e6", + "ready_seconds": 0.849473, + "queue_lag_seconds": 0.467112, + "attempts": 1 + }, + { + "id": "91081b2a-4eac-4679-a210-835405664cc4", + "ready_seconds": 1.627132, + "queue_lag_seconds": 1.279613, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262312.5737548, + "memory_bytes": 42983424, + "cpu_usec": 993118, + "rss_bytes": 140197888 + }, + { + "time": 1794262312.962958, + "memory_bytes": 46227456, + "cpu_usec": 1386895, + "rss_bytes": 144166912 + }, + { + "time": 1794262313.35336, + "memory_bytes": 50225152, + "cpu_usec": 1762234, + "rss_bytes": 143147008 + }, + { + "time": 1794262313.773581, + "memory_bytes": 45711360, + "cpu_usec": 2204620, + "rss_bytes": 106614784 + }, + { + "time": 1794262314.166707, + "memory_bytes": 45285376, + "cpu_usec": 2593158, + "rss_bytes": 106643456 + }, + { + "time": 1794262314.538958, + "memory_bytes": 45490176, + "cpu_usec": 2619633, + "rss_bytes": 106643456 + } + ] + }, + { + "repeat": 1, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.0476217079994967, + "throughput": 3.906971668031352, + "p95_ready_seconds": 1.788149, + "p95_queue_lag_seconds": 0.96431, + "peak_rss_bytes": 295608320, + "peak_cgroup_memory_bytes": 74162176, + "worker_cpu_seconds": 1.521378, + "requests": [ + { + "upload_seconds": 0.0409248750002007, + "status": 202 + }, + { + "upload_seconds": 0.06408179199934239, + "status": 202 + }, + { + "upload_seconds": 0.07904470799985575, + "status": 202 + }, + { + "upload_seconds": 0.09678162500495091, + "status": 202 + }, + { + "upload_seconds": 0.07141170799877727, + "status": 202 + }, + { + "upload_seconds": 0.06645174999721348, + "status": 202 + }, + { + "upload_seconds": 0.06909837500279536, + "status": 202 + }, + { + "upload_seconds": 0.06092287499632221, + "status": 202 + } + ], + "jobs": [ + { + "id": "5d269ef3-9476-4f6f-b27b-fbb14c0e4caa", + "ready_seconds": 0.964258, + "queue_lag_seconds": 0.136546, + "attempts": 1 + }, + { + "id": "c2e7e0ab-c0ea-472f-a273-b3a4955fbac1", + "ready_seconds": 1.788149, + "queue_lag_seconds": 0.96431, + "attempts": 1 + }, + { + "id": "1804debb-8da5-47ae-a8fe-b80ad0be4e2a", + "ready_seconds": 0.940762, + "queue_lag_seconds": 0.116292, + "attempts": 1 + }, + { + "id": "5992ff07-5eca-41e8-9e4a-7711f9248df2", + "ready_seconds": 1.783108, + "queue_lag_seconds": 0.949592, + "attempts": 1 + }, + { + "id": "8c0827bc-bdbd-4b84-b035-608aa303f8d8", + "ready_seconds": 0.879942, + "queue_lag_seconds": 0.042626, + "attempts": 1 + }, + { + "id": "ec251619-feac-476d-af95-9d65fd1ec5db", + "ready_seconds": 1.711594, + "queue_lag_seconds": 0.900856, + "attempts": 1 + }, + { + "id": "e0277e42-4342-4019-a9d1-35900708918b", + "ready_seconds": 0.865465, + "queue_lag_seconds": 0.055853, + "attempts": 1 + }, + { + "id": "8e126133-6c15-47e0-aa0d-e91283068a9a", + "ready_seconds": 1.68961, + "queue_lag_seconds": 0.882134, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262317.7497191, + "memory_bytes": 55816192, + "cpu_usec": 1189242, + "rss_bytes": 212205568 + }, + { + "time": 1794262318.25758, + "memory_bytes": 66314240, + "cpu_usec": 1726302, + "rss_bytes": 222949376 + }, + { + "time": 1794262318.7573462, + "memory_bytes": 74162176, + "cpu_usec": 2283956, + "rss_bytes": 295608320 + }, + { + "time": 1794262319.248426, + "memory_bytes": 66113536, + "cpu_usec": 2710620, + "rss_bytes": 227430400 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 10.214052916999208, + "throughput": 0.7832346341857728, + "p95_ready_seconds": 8.463946, + "p95_queue_lag_seconds": 7.654972, + "peak_rss_bytes": 199614464, + "peak_cgroup_memory_bytes": 101957632, + "worker_cpu_seconds": 7.518454, + "requests": [ + { + "upload_seconds": 0.4734504169973661, + "status": 202 + }, + { + "upload_seconds": 0.6722895000057179, + "status": 202 + }, + { + "upload_seconds": 0.7707648750001681, + "status": 202 + }, + { + "upload_seconds": 0.8576565000039409, + "status": 202 + }, + { + "upload_seconds": 0.35896287499781465, + "status": 202 + }, + { + "upload_seconds": 0.6733611249946989, + "status": 202 + }, + { + "upload_seconds": 0.38681283299956704, + "status": 202 + }, + { + "upload_seconds": 0.6135808749968419, + "status": 202 + } + ], + "jobs": [ + { + "id": "11d75d8d-cd9b-4d10-a405-109dd2e60e03", + "ready_seconds": 3.874429, + "queue_lag_seconds": 1.346038, + "attempts": 1 + }, + { + "id": "a2284f7f-5012-40b5-826a-4b8a2795e7c5", + "ready_seconds": 7.047366, + "queue_lag_seconds": 6.172377, + "attempts": 1 + }, + { + "id": "fa0141b9-4b19-4f0e-af03-d45ec4e890d3", + "ready_seconds": 8.463946, + "queue_lag_seconds": 7.654972, + "attempts": 1 + }, + { + "id": "0bf04082-3c2a-4d14-ab16-2226069cb82f", + "ready_seconds": 4.603033, + "queue_lag_seconds": 3.726946, + "attempts": 1 + }, + { + "id": "3cd335e4-b400-4c48-894b-108f11769b57", + "ready_seconds": 7.686789, + "queue_lag_seconds": 6.893187, + "attempts": 1 + }, + { + "id": "82d60e9f-793c-4b49-b3bb-69d48fcc1c82", + "ready_seconds": 1.355779, + "queue_lag_seconds": 0.231652, + "attempts": 1 + }, + { + "id": "59eb40ef-a7a0-4584-b9e3-5958010ea250", + "ready_seconds": 5.512865, + "queue_lag_seconds": 4.425121, + "attempts": 1 + }, + { + "id": "737301a8-fc53-402a-85b0-36ab105ec1b5", + "ready_seconds": 6.237758, + "queue_lag_seconds": 5.427986, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262324.2375848, + "memory_bytes": 44666880, + "cpu_usec": 2065973, + "rss_bytes": 106340352 + }, + { + "time": 1794262324.677325, + "memory_bytes": 51453952, + "cpu_usec": 2311291, + "rss_bytes": 141185024 + }, + { + "time": 1794262325.088465, + "memory_bytes": 87121920, + "cpu_usec": 2690405, + "rss_bytes": 189181952 + }, + { + "time": 1794262325.504909, + "memory_bytes": 47222784, + "cpu_usec": 3063668, + "rss_bytes": 107204608 + }, + { + "time": 1794262325.9938948, + "memory_bytes": 84570112, + "cpu_usec": 3494369, + "rss_bytes": 180502528 + }, + { + "time": 1794262326.47446, + "memory_bytes": 77090816, + "cpu_usec": 3970594, + "rss_bytes": 176230400 + }, + { + "time": 1794262326.81706, + "memory_bytes": 59994112, + "cpu_usec": 4107626, + "rss_bytes": 107495424 + }, + { + "time": 1794262327.157752, + "memory_bytes": 60157952, + "cpu_usec": 4127645, + "rss_bytes": 107495424 + }, + { + "time": 1794262327.69939, + "memory_bytes": 60305408, + "cpu_usec": 4220682, + "rss_bytes": 107495424 + }, + { + "time": 1794262328.0880482, + "memory_bytes": 61517824, + "cpu_usec": 4291251, + "rss_bytes": 108978176 + }, + { + "time": 1794262328.4875982, + "memory_bytes": 86396928, + "cpu_usec": 4660762, + "rss_bytes": 180977664 + }, + { + "time": 1794262328.871718, + "memory_bytes": 97525760, + "cpu_usec": 5031249, + "rss_bytes": 194334720 + }, + { + "time": 1794262329.288575, + "memory_bytes": 69193728, + "cpu_usec": 5403575, + "rss_bytes": 157335552 + }, + { + "time": 1794262329.6867101, + "memory_bytes": 97075200, + "cpu_usec": 5799498, + "rss_bytes": 191086592 + }, + { + "time": 1794262330.04568, + "memory_bytes": 65396736, + "cpu_usec": 6128659, + "rss_bytes": 113606656 + }, + { + "time": 1794262330.391624, + "memory_bytes": 91922432, + "cpu_usec": 6404042, + "rss_bytes": 186523648 + }, + { + "time": 1794262330.76962, + "memory_bytes": 80707584, + "cpu_usec": 6768430, + "rss_bytes": 178741248 + }, + { + "time": 1794262331.1224809, + "memory_bytes": 71532544, + "cpu_usec": 7069751, + "rss_bytes": 153964544 + }, + { + "time": 1794262331.500947, + "memory_bytes": 101236736, + "cpu_usec": 7437300, + "rss_bytes": 198979584 + }, + { + "time": 1794262331.910403, + "memory_bytes": 57864192, + "cpu_usec": 7770565, + "rss_bytes": 113946624 + }, + { + "time": 1794262332.261354, + "memory_bytes": 98197504, + "cpu_usec": 8117409, + "rss_bytes": 192552960 + }, + { + "time": 1794262332.617752, + "memory_bytes": 65212416, + "cpu_usec": 8423479, + "rss_bytes": 113954816 + }, + { + "time": 1794262332.989001, + "memory_bytes": 75464704, + "cpu_usec": 8687402, + "rss_bytes": 161304576 + }, + { + "time": 1794262333.334304, + "memory_bytes": 101957632, + "cpu_usec": 9041253, + "rss_bytes": 199614464 + }, + { + "time": 1794262333.7091782, + "memory_bytes": 60796928, + "cpu_usec": 9214412, + "rss_bytes": 113958912 + }, + { + "time": 1794262334.120569, + "memory_bytes": 58785792, + "cpu_usec": 9584427, + "rss_bytes": 115216384 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 8.415711208006542, + "throughput": 0.9506029618018448, + "p95_ready_seconds": 6.307119, + "p95_queue_lag_seconds": 4.704642, + "peak_rss_bytes": 277078016, + "peak_cgroup_memory_bytes": 145489920, + "worker_cpu_seconds": 7.462616, + "requests": [ + { + "upload_seconds": 0.3655178750050254, + "status": 202 + }, + { + "upload_seconds": 0.46488608400250087, + "status": 202 + }, + { + "upload_seconds": 0.9044631249998929, + "status": 202 + }, + { + "upload_seconds": 1.318556540994905, + "status": 202 + }, + { + "upload_seconds": 1.2203440420053084, + "status": 202 + }, + { + "upload_seconds": 1.3046807499995339, + "status": 202 + }, + { + "upload_seconds": 1.0543449169999803, + "status": 202 + }, + { + "upload_seconds": 0.7488859579971177, + "status": 202 + } + ], + "jobs": [ + { + "id": "8faec5ee-2fa6-4d07-8630-fc0e8bcf6eb1", + "ready_seconds": 4.116328, + "queue_lag_seconds": 2.488239, + "attempts": 1 + }, + { + "id": "b6e499e9-a7be-49de-a6bd-cc232f016844", + "ready_seconds": 5.13449, + "queue_lag_seconds": 3.550491, + "attempts": 1 + }, + { + "id": "7f99c8aa-cfbb-476f-9ef3-1b65d68ce421", + "ready_seconds": 6.307119, + "queue_lag_seconds": 4.471636, + "attempts": 1 + }, + { + "id": "717a9e90-a5b0-42a4-a7e1-a8874f646abe", + "ready_seconds": 2.990502, + "queue_lag_seconds": 0.28872, + "attempts": 1 + }, + { + "id": "69b1fc5f-0761-4aa5-8be9-5168a8680dcd", + "ready_seconds": 6.285874, + "queue_lag_seconds": 4.704642, + "attempts": 1 + }, + { + "id": "e90fcacb-a24e-42ba-a39f-7928d3910d06", + "ready_seconds": 2.492907, + "queue_lag_seconds": 0.150521, + "attempts": 1 + }, + { + "id": "aacb59ef-7575-48ef-8820-36f6bb63dcdc", + "ready_seconds": 4.308028, + "queue_lag_seconds": 2.810447, + "attempts": 1 + }, + { + "id": "618eb9bb-9cc3-40b1-a00d-635f48b1f129", + "ready_seconds": 5.029212, + "queue_lag_seconds": 3.404008, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262337.490489, + "memory_bytes": 44957696, + "cpu_usec": 1388440, + "rss_bytes": 106409984 + }, + { + "time": 1794262337.939751, + "memory_bytes": 60059648, + "cpu_usec": 1666638, + "rss_bytes": 141225984 + }, + { + "time": 1794262338.709249, + "memory_bytes": 97263616, + "cpu_usec": 2314922, + "rss_bytes": 224612352 + }, + { + "time": 1794262339.232517, + "memory_bytes": 128987136, + "cpu_usec": 2844467, + "rss_bytes": 264658944 + }, + { + "time": 1794262339.6184871, + "memory_bytes": 129318912, + "cpu_usec": 3284496, + "rss_bytes": 260890624 + }, + { + "time": 1794262340.150041, + "memory_bytes": 63504384, + "cpu_usec": 3756474, + "rss_bytes": 109555712 + }, + { + "time": 1794262340.604726, + "memory_bytes": 71729152, + "cpu_usec": 4128343, + "rss_bytes": 158167040 + }, + { + "time": 1794262341.026336, + "memory_bytes": 131174400, + "cpu_usec": 4607664, + "rss_bytes": 260644864 + }, + { + "time": 1794262341.428124, + "memory_bytes": 145489920, + "cpu_usec": 5010041, + "rss_bytes": 277078016 + }, + { + "time": 1794262341.821051, + "memory_bytes": 69431296, + "cpu_usec": 5397722, + "rss_bytes": 109563904 + }, + { + "time": 1794262342.315008, + "memory_bytes": 97644544, + "cpu_usec": 5854211, + "rss_bytes": 217735168 + }, + { + "time": 1794262342.7356732, + "memory_bytes": 135110656, + "cpu_usec": 6298093, + "rss_bytes": 270577664 + }, + { + "time": 1794262343.203978, + "memory_bytes": 106741760, + "cpu_usec": 6743952, + "rss_bytes": 195207168 + }, + { + "time": 1794262343.709871, + "memory_bytes": 73314304, + "cpu_usec": 7248146, + "rss_bytes": 151134208 + }, + { + "time": 1794262344.2393038, + "memory_bytes": 118419456, + "cpu_usec": 7724934, + "rss_bytes": 258355200 + }, + { + "time": 1794262344.630781, + "memory_bytes": 140279808, + "cpu_usec": 8201607, + "rss_bytes": 274935808 + }, + { + "time": 1794262345.107616, + "memory_bytes": 113319936, + "cpu_usec": 8619190, + "rss_bytes": 197951488 + }, + { + "time": 1794262345.437784, + "memory_bytes": 63397888, + "cpu_usec": 8851056, + "rss_bytes": 112402432 + } + ] + }, + { + "repeat": 1, + "image": "large", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.835286958994402, + "throughput": 0.8133977212209323, + "p95_ready_seconds": 8.308307, + "p95_queue_lag_seconds": 4.476807, + "peak_rss_bytes": 424382464, + "peak_cgroup_memory_bytes": 191934464, + "worker_cpu_seconds": 8.774336, + "requests": [ + { + "upload_seconds": 0.5555352090013912, + "status": 202 + }, + { + "upload_seconds": 0.6558055420027813, + "status": 202 + }, + { + "upload_seconds": 0.9479415829991922, + "status": 202 + }, + { + "upload_seconds": 1.1452457920022425, + "status": 202 + }, + { + "upload_seconds": 0.7364269999961834, + "status": 202 + }, + { + "upload_seconds": 1.0115752920028172, + "status": 202 + }, + { + "upload_seconds": 0.7167560000016238, + "status": 202 + }, + { + "upload_seconds": 0.614655125005811, + "status": 202 + } + ], + "jobs": [ + { + "id": "e0e40541-2a18-41b8-a6d2-c0f70e8f1a45", + "ready_seconds": 4.461059, + "queue_lag_seconds": 0.159653, + "attempts": 1 + }, + { + "id": "2fd41873-9992-4e83-be7e-eab1000fc857", + "ready_seconds": 7.839581, + "queue_lag_seconds": 4.040879, + "attempts": 1 + }, + { + "id": "ee1d8b70-3815-4cd5-bb4c-8846d3ebfba1", + "ready_seconds": 5.068001, + "queue_lag_seconds": 0.366455, + "attempts": 1 + }, + { + "id": "97ac83a3-e22f-4731-b3fe-5292f1636ea1", + "ready_seconds": 8.136236, + "queue_lag_seconds": 4.244041, + "attempts": 1 + }, + { + "id": "2791b3f0-7874-4e9a-904f-9c75e7e84c8d", + "ready_seconds": 8.155908, + "queue_lag_seconds": 4.476807, + "attempts": 1 + }, + { + "id": "e50dfa64-98b4-4254-a222-e73704e6591d", + "ready_seconds": 5.172821, + "queue_lag_seconds": 0.524741, + "attempts": 1 + }, + { + "id": "b413f15e-097d-4ceb-952a-973208180b19", + "ready_seconds": 8.308307, + "queue_lag_seconds": 4.407467, + "attempts": 1 + }, + { + "id": "cf8f0b94-5d4b-454b-926b-c5c396ede828", + "ready_seconds": 5.121261, + "queue_lag_seconds": 0.610854, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262348.8572469, + "memory_bytes": 41979904, + "cpu_usec": 1504505, + "rss_bytes": 106389504 + }, + { + "time": 1794262349.364602, + "memory_bytes": 51986432, + "cpu_usec": 1782613, + "rss_bytes": 120537088 + }, + { + "time": 1794262350.057267, + "memory_bytes": 89964544, + "cpu_usec": 2404540, + "rss_bytes": 224448512 + }, + { + "time": 1794262350.557502, + "memory_bytes": 136720384, + "cpu_usec": 3005696, + "rss_bytes": 344702976 + }, + { + "time": 1794262351.574933, + "memory_bytes": 188698624, + "cpu_usec": 3905731, + "rss_bytes": 418521088 + }, + { + "time": 1794262352.352106, + "memory_bytes": 190210048, + "cpu_usec": 4654780, + "rss_bytes": 424382464 + }, + { + "time": 1794262352.955371, + "memory_bytes": 165081088, + "cpu_usec": 5265101, + "rss_bytes": 347693056 + }, + { + "time": 1794262353.4730828, + "memory_bytes": 84316160, + "cpu_usec": 5877246, + "rss_bytes": 110092288 + }, + { + "time": 1794262354.058292, + "memory_bytes": 71311360, + "cpu_usec": 6333640, + "rss_bytes": 151801856 + }, + { + "time": 1794262354.654955, + "memory_bytes": 96731136, + "cpu_usec": 6984175, + "rss_bytes": 197021696 + }, + { + "time": 1794262355.266908, + "memory_bytes": 127197184, + "cpu_usec": 7579041, + "rss_bytes": 329019392 + }, + { + "time": 1794262355.9633112, + "memory_bytes": 180551680, + "cpu_usec": 8213345, + "rss_bytes": 414875648 + }, + { + "time": 1794262356.658124, + "memory_bytes": 191934464, + "cpu_usec": 8867388, + "rss_bytes": 417808384 + }, + { + "time": 1794262357.257552, + "memory_bytes": 191336448, + "cpu_usec": 9496265, + "rss_bytes": 358735872 + }, + { + "time": 1794262357.7575018, + "memory_bytes": 89542656, + "cpu_usec": 10077162, + "rss_bytes": 179494912 + }, + { + "time": 1794262358.1040719, + "memory_bytes": 71675904, + "cpu_usec": 10278841, + "rss_bytes": 113885184 + } + ] + }, + { + "repeat": 2, + "image": "large", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 8.101745708001545, + "throughput": 0.9874415080812697, + "p95_ready_seconds": 6.520202, + "p95_queue_lag_seconds": 4.749383, + "peak_rss_bytes": 281972736, + "peak_cgroup_memory_bytes": 138719232, + "worker_cpu_seconds": 6.765876, + "requests": [ + { + "upload_seconds": 0.4162295000060112, + "status": 202 + }, + { + "upload_seconds": 0.6149917079965235, + "status": 202 + }, + { + "upload_seconds": 0.7204787919981754, + "status": 202 + }, + { + "upload_seconds": 0.8338184999956866, + "status": 202 + }, + { + "upload_seconds": 0.5898740409975289, + "status": 202 + }, + { + "upload_seconds": 0.44792183300160104, + "status": 202 + }, + { + "upload_seconds": 0.40524725000432227, + "status": 202 + }, + { + "upload_seconds": 0.3922059159958735, + "status": 202 + } + ], + "jobs": [ + { + "id": "1b40b858-9b09-4deb-ac61-bcbe83002a64", + "ready_seconds": 3.602113, + "queue_lag_seconds": 1.651728, + "attempts": 1 + }, + { + "id": "d718a11b-9618-4bb0-92bb-922bed1cda30", + "ready_seconds": 5.015496, + "queue_lag_seconds": 3.268629, + "attempts": 1 + }, + { + "id": "471bf22d-f431-49f0-be58-2bab4ae00b6b", + "ready_seconds": 2.086863, + "queue_lag_seconds": 0.392642, + "attempts": 1 + }, + { + "id": "6c37b530-f5dd-4829-b86c-cf5a996faad4", + "ready_seconds": 3.374384, + "queue_lag_seconds": 1.740594, + "attempts": 1 + }, + { + "id": "32eb0f39-66e8-49e3-a976-891f005e6cb2", + "ready_seconds": 4.857129, + "queue_lag_seconds": 3.190966, + "attempts": 1 + }, + { + "id": "7ae9cc9a-04e4-49d2-98ed-749c1ee5079f", + "ready_seconds": 6.520202, + "queue_lag_seconds": 4.749383, + "attempts": 1 + }, + { + "id": "9e3f88d0-424c-45b5-8d51-70ccaa6025c0", + "ready_seconds": 1.734729, + "queue_lag_seconds": 0.168296, + "attempts": 1 + }, + { + "id": "ce237d98-bdcf-427a-8215-2c814ea99c45", + "ready_seconds": 6.48153, + "queue_lag_seconds": 4.722514, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262361.755059, + "memory_bytes": 46284800, + "cpu_usec": 1512139, + "rss_bytes": 106438656 + }, + { + "time": 1794262362.099647, + "memory_bytes": 55246848, + "cpu_usec": 1610221, + "rss_bytes": 107171840 + }, + { + "time": 1794262362.5327559, + "memory_bytes": 92733440, + "cpu_usec": 2069645, + "rss_bytes": 214233088 + }, + { + "time": 1794262362.995512, + "memory_bytes": 124538880, + "cpu_usec": 2487794, + "rss_bytes": 268300288 + }, + { + "time": 1794262363.4179351, + "memory_bytes": 96542720, + "cpu_usec": 2923192, + "rss_bytes": 193921024 + }, + { + "time": 1794262363.926462, + "memory_bytes": 60690432, + "cpu_usec": 3335444, + "rss_bytes": 139583488 + }, + { + "time": 1794262364.321169, + "memory_bytes": 99147776, + "cpu_usec": 3760055, + "rss_bytes": 230227968 + }, + { + "time": 1794262364.72891, + "memory_bytes": 131219456, + "cpu_usec": 4163416, + "rss_bytes": 271032320 + }, + { + "time": 1794262365.107834, + "memory_bytes": 100380672, + "cpu_usec": 4543584, + "rss_bytes": 193835008 + }, + { + "time": 1794262365.495488, + "memory_bytes": 63741952, + "cpu_usec": 4857710, + "rss_bytes": 109920256 + }, + { + "time": 1794262366.006165, + "memory_bytes": 75325440, + "cpu_usec": 5336939, + "rss_bytes": 190963712 + }, + { + "time": 1794262366.420844, + "memory_bytes": 124809216, + "cpu_usec": 5797722, + "rss_bytes": 263680000 + }, + { + "time": 1794262366.8136802, + "memory_bytes": 138719232, + "cpu_usec": 6195877, + "rss_bytes": 281972736 + }, + { + "time": 1794262367.14334, + "memory_bytes": 63270912, + "cpu_usec": 6484149, + "rss_bytes": 111247360 + }, + { + "time": 1794262367.518545, + "memory_bytes": 66465792, + "cpu_usec": 6762218, + "rss_bytes": 150065152 + }, + { + "time": 1794262367.912593, + "memory_bytes": 108752896, + "cpu_usec": 7119600, + "rss_bytes": 254676992 + }, + { + "time": 1794262368.297046, + "memory_bytes": 133009408, + "cpu_usec": 7506426, + "rss_bytes": 276078592 + }, + { + "time": 1794262368.822019, + "memory_bytes": 85430272, + "cpu_usec": 7981602, + "rss_bytes": 177520640 + }, + { + "time": 1794262369.1505752, + "memory_bytes": 62181376, + "cpu_usec": 8238854, + "rss_bytes": 111968256 + }, + { + "time": 1794262369.591321, + "memory_bytes": 54579200, + "cpu_usec": 8278015, + "rss_bytes": 111972352 + } + ] + }, + { + "repeat": 2, + "image": "large", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 8.238695665997511, + "throughput": 0.9710274932253355, + "p95_ready_seconds": 7.160109, + "p95_queue_lag_seconds": 3.687723, + "peak_rss_bytes": 397238272, + "peak_cgroup_memory_bytes": 186429440, + "worker_cpu_seconds": 7.385869, + "requests": [ + { + "upload_seconds": 0.3346457500010729, + "status": 202 + }, + { + "upload_seconds": 0.5753866659943014, + "status": 202 + }, + { + "upload_seconds": 0.7584450409995043, + "status": 202 + }, + { + "upload_seconds": 0.8696108340009232, + "status": 202 + }, + { + "upload_seconds": 0.8214943749990198, + "status": 202 + }, + { + "upload_seconds": 0.7732604170014383, + "status": 202 + }, + { + "upload_seconds": 0.7054776670047431, + "status": 202 + }, + { + "upload_seconds": 0.6835131249972619, + "status": 202 + } + ], + "jobs": [ + { + "id": "33932f1c-0698-4f05-b977-3fc73c84d6c9", + "ready_seconds": 3.923971, + "queue_lag_seconds": 0.420322, + "attempts": 1 + }, + { + "id": "3eaceca7-ee12-495b-8e1d-4c6a423cf73a", + "ready_seconds": 6.82308, + "queue_lag_seconds": 3.365487, + "attempts": 1 + }, + { + "id": "15021bae-702e-468c-bd14-0d65a31afbe8", + "ready_seconds": 4.171333, + "queue_lag_seconds": 0.460526, + "attempts": 1 + }, + { + "id": "e4f47fb2-487f-4c3a-8c90-7cdf88b69cf6", + "ready_seconds": 5.026424, + "queue_lag_seconds": 3.024265, + "attempts": 1 + }, + { + "id": "1a701e3f-bc3b-4aa9-96a8-04836da1cf2f", + "ready_seconds": 6.654839, + "queue_lag_seconds": 3.207782, + "attempts": 1 + }, + { + "id": "c83ed650-1526-4d75-90cd-d23c1132c673", + "ready_seconds": 7.160109, + "queue_lag_seconds": 3.687723, + "attempts": 1 + }, + { + "id": "f0a7cb39-5f52-47ce-8d89-26ced277bfac", + "ready_seconds": 4.321114, + "queue_lag_seconds": 0.444005, + "attempts": 1 + }, + { + "id": "37e4e587-6b46-4114-ab2b-6927bb0cab3f", + "ready_seconds": 3.52955, + "queue_lag_seconds": 0.10123, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262373.329525, + "memory_bytes": 41304064, + "cpu_usec": 1433050, + "rss_bytes": 106479616 + }, + { + "time": 1794262373.8107378, + "memory_bytes": 68022272, + "cpu_usec": 1776641, + "rss_bytes": 174014464 + }, + { + "time": 1794262374.488162, + "memory_bytes": 96083968, + "cpu_usec": 2399862, + "rss_bytes": 226676736 + }, + { + "time": 1794262374.9780211, + "memory_bytes": 140443648, + "cpu_usec": 2973180, + "rss_bytes": 373719040 + }, + { + "time": 1794262375.570348, + "memory_bytes": 159723520, + "cpu_usec": 3486167, + "rss_bytes": 397238272 + }, + { + "time": 1794262376.084664, + "memory_bytes": 168640512, + "cpu_usec": 4093594, + "rss_bytes": 355319808 + }, + { + "time": 1794262376.58712, + "memory_bytes": 173109248, + "cpu_usec": 4555744, + "rss_bytes": 345436160 + }, + { + "time": 1794262377.096107, + "memory_bytes": 82251776, + "cpu_usec": 5020577, + "rss_bytes": 183726080 + }, + { + "time": 1794262377.4688299, + "memory_bytes": 99491840, + "cpu_usec": 5448708, + "rss_bytes": 187719680 + }, + { + "time": 1794262377.9759092, + "memory_bytes": 103354368, + "cpu_usec": 5950011, + "rss_bytes": 201535488 + }, + { + "time": 1794262378.575902, + "memory_bytes": 100859904, + "cpu_usec": 6486176, + "rss_bytes": 258154496 + }, + { + "time": 1794262379.072046, + "memory_bytes": 150761472, + "cpu_usec": 7050597, + "rss_bytes": 338993152 + }, + { + "time": 1794262379.481832, + "memory_bytes": 168144896, + "cpu_usec": 7503146, + "rss_bytes": 355807232 + }, + { + "time": 1794262379.982485, + "memory_bytes": 186429440, + "cpu_usec": 7952800, + "rss_bytes": 376799232 + }, + { + "time": 1794262380.500739, + "memory_bytes": 113004544, + "cpu_usec": 8412230, + "rss_bytes": 253005824 + }, + { + "time": 1794262380.865719, + "memory_bytes": 71774208, + "cpu_usec": 8700431, + "rss_bytes": 120758272 + }, + { + "time": 1794262381.233644, + "memory_bytes": 59719680, + "cpu_usec": 8818919, + "rss_bytes": 120791040 + } + ] + }, + { + "repeat": 2, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.10356504200172, + "throughput": 0.8787766070863307, + "p95_ready_seconds": 7.155672, + "p95_queue_lag_seconds": 6.245195, + "peak_rss_bytes": 198897664, + "peak_cgroup_memory_bytes": 102072320, + "worker_cpu_seconds": 7.205255, + "requests": [ + { + "upload_seconds": 0.4608520830006455, + "status": 202 + }, + { + "upload_seconds": 0.6706778750012745, + "status": 202 + }, + { + "upload_seconds": 0.8592802909988677, + "status": 202 + }, + { + "upload_seconds": 0.9627979590004543, + "status": 202 + }, + { + "upload_seconds": 0.6193941250021453, + "status": 202 + }, + { + "upload_seconds": 1.1034054579940857, + "status": 202 + }, + { + "upload_seconds": 0.7112387499946635, + "status": 202 + }, + { + "upload_seconds": 1.002451292006299, + "status": 202 + } + ], + "jobs": [ + { + "id": "58ce9899-c415-47b5-8868-cede8b3630ac", + "ready_seconds": 1.696655, + "queue_lag_seconds": 0.154809, + "attempts": 1 + }, + { + "id": "76f2658e-3368-454f-9722-7db3afa455de", + "ready_seconds": 5.877514, + "queue_lag_seconds": 4.924412, + "attempts": 1 + }, + { + "id": "2aa5d55e-5d00-413b-8298-3e94e8084c73", + "ready_seconds": 4.402172, + "queue_lag_seconds": 3.268996, + "attempts": 1 + }, + { + "id": "02adb4c0-d52b-47f5-b20f-91eaa96d93de", + "ready_seconds": 6.499813, + "queue_lag_seconds": 5.538896, + "attempts": 1 + }, + { + "id": "d91deed3-3fbd-4c47-9c92-2c60a2666318", + "ready_seconds": 7.155672, + "queue_lag_seconds": 6.245195, + "attempts": 1 + }, + { + "id": "8c90a845-ac18-4532-9cc5-3961b42b19e6", + "ready_seconds": 2.640209, + "queue_lag_seconds": 1.750154, + "attempts": 1 + }, + { + "id": "c33aa07b-4157-4142-8263-c4a74841726f", + "ready_seconds": 3.482302, + "queue_lag_seconds": 2.567262, + "attempts": 1 + }, + { + "id": "be0fbbc5-10aa-4584-af14-d3b233c27635", + "ready_seconds": 5.005695, + "queue_lag_seconds": 4.267294, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262384.9518871, + "memory_bytes": 50688000, + "cpu_usec": 1535289, + "rss_bytes": 106483712 + }, + { + "time": 1794262385.391333, + "memory_bytes": 52957184, + "cpu_usec": 1736056, + "rss_bytes": 107204608 + }, + { + "time": 1794262385.952516, + "memory_bytes": 79167488, + "cpu_usec": 2237909, + "rss_bytes": 184524800 + }, + { + "time": 1794262386.37323, + "memory_bytes": 56131584, + "cpu_usec": 2693456, + "rss_bytes": 107274240 + }, + { + "time": 1794262386.7641852, + "memory_bytes": 49291264, + "cpu_usec": 2780661, + "rss_bytes": 107274240 + }, + { + "time": 1794262387.166975, + "memory_bytes": 86786048, + "cpu_usec": 3181564, + "rss_bytes": 183619584 + }, + { + "time": 1794262387.525435, + "memory_bytes": 53587968, + "cpu_usec": 3532657, + "rss_bytes": 107507712 + }, + { + "time": 1794262387.878753, + "memory_bytes": 84377600, + "cpu_usec": 3799155, + "rss_bytes": 180383744 + }, + { + "time": 1794262388.267936, + "memory_bytes": 96100352, + "cpu_usec": 4179284, + "rss_bytes": 192999424 + }, + { + "time": 1794262388.689367, + "memory_bytes": 60686336, + "cpu_usec": 4510952, + "rss_bytes": 110395392 + }, + { + "time": 1794262389.069269, + "memory_bytes": 77176832, + "cpu_usec": 4889609, + "rss_bytes": 168751104 + }, + { + "time": 1794262389.402836, + "memory_bytes": 102072320, + "cpu_usec": 5221784, + "rss_bytes": 198897664 + }, + { + "time": 1794262389.7577758, + "memory_bytes": 62373888, + "cpu_usec": 5415786, + "rss_bytes": 113463296 + }, + { + "time": 1794262390.144099, + "memory_bytes": 89358336, + "cpu_usec": 5808430, + "rss_bytes": 190545920 + }, + { + "time": 1794262390.5190332, + "memory_bytes": 58834944, + "cpu_usec": 6176317, + "rss_bytes": 113463296 + }, + { + "time": 1794262390.871752, + "memory_bytes": 94416896, + "cpu_usec": 6534606, + "rss_bytes": 190406656 + }, + { + "time": 1794262391.256792, + "memory_bytes": 84656128, + "cpu_usec": 6942327, + "rss_bytes": 180928512 + }, + { + "time": 1794262391.602048, + "memory_bytes": 68218880, + "cpu_usec": 7251000, + "rss_bytes": 152457216 + }, + { + "time": 1794262391.9413068, + "memory_bytes": 100884480, + "cpu_usec": 7583109, + "rss_bytes": 198283264 + }, + { + "time": 1794262392.369327, + "memory_bytes": 59207680, + "cpu_usec": 7819707, + "rss_bytes": 114708480 + }, + { + "time": 1794262392.796236, + "memory_bytes": 91262976, + "cpu_usec": 8191147, + "rss_bytes": 188411904 + }, + { + "time": 1794262393.2521758, + "memory_bytes": 69705728, + "cpu_usec": 8640936, + "rss_bytes": 114708480 + }, + { + "time": 1794262393.590945, + "memory_bytes": 65884160, + "cpu_usec": 8740544, + "rss_bytes": 114712576 + } + ] + }, + { + "repeat": 2, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.2421264169970527, + "throughput": 3.5680414535745224, + "p95_ready_seconds": 1.812734, + "p95_queue_lag_seconds": 1.400673, + "peak_rss_bytes": 173174784, + "peak_cgroup_memory_bytes": 55123968, + "worker_cpu_seconds": 1.744227, + "requests": [ + { + "upload_seconds": 0.03406304199597798, + "status": 202 + }, + { + "upload_seconds": 0.05620845799421659, + "status": 202 + }, + { + "upload_seconds": 0.08089262500288896, + "status": 202 + }, + { + "upload_seconds": 0.09777987500274321, + "status": 202 + }, + { + "upload_seconds": 0.07787354099855293, + "status": 202 + }, + { + "upload_seconds": 0.11417283300397685, + "status": 202 + }, + { + "upload_seconds": 0.14169870800105855, + "status": 202 + }, + { + "upload_seconds": 0.1499457500030985, + "status": 202 + } + ], + "jobs": [ + { + "id": "22225742-842a-459f-a56f-a5ede53ff4ad", + "ready_seconds": 1.123713, + "queue_lag_seconds": 0.636735, + "attempts": 1 + }, + { + "id": "180f58f4-cbca-4e4d-8cce-870a43212930", + "ready_seconds": 1.812734, + "queue_lag_seconds": 1.400673, + "attempts": 1 + }, + { + "id": "964c6011-8364-4c55-9769-328d71df88bb", + "ready_seconds": 0.658602, + "queue_lag_seconds": 0.242416, + "attempts": 1 + }, + { + "id": "4c9a468b-0df8-4f8e-8897-0bb4fb154f2f", + "ready_seconds": 1.46804, + "queue_lag_seconds": 1.09217, + "attempts": 1 + }, + { + "id": "3b31b076-2844-4f43-af58-8c6ae233ace1", + "ready_seconds": 0.625811, + "queue_lag_seconds": 0.146498, + "attempts": 1 + }, + { + "id": "9a8ee4c0-e2c1-49e3-9d00-9fda7905db70", + "ready_seconds": 1.457865, + "queue_lag_seconds": 1.070151, + "attempts": 1 + }, + { + "id": "2fcdd4f3-5106-41c6-859f-5affde9ba40a", + "ready_seconds": 1.106118, + "queue_lag_seconds": 0.61946, + "attempts": 1 + }, + { + "id": "95c50bcc-ccdb-48ea-8b4a-443e17865997", + "ready_seconds": 1.798918, + "queue_lag_seconds": 1.378992, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262396.595484, + "memory_bytes": 42094592, + "cpu_usec": 1157632, + "rss_bytes": 120750080 + }, + { + "time": 1794262397.1200352, + "memory_bytes": 51560448, + "cpu_usec": 1707624, + "rss_bytes": 154648576 + }, + { + "time": 1794262397.512683, + "memory_bytes": 55123968, + "cpu_usec": 2184197, + "rss_bytes": 154685440 + }, + { + "time": 1794262397.920637, + "memory_bytes": 54521856, + "cpu_usec": 2569106, + "rss_bytes": 173174784 + }, + { + "time": 1794262398.2757602, + "memory_bytes": 50442240, + "cpu_usec": 2901859, + "rss_bytes": 106463232 + } + ] + }, + { + "repeat": 2, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.2075934589956887, + "throughput": 3.623855636734618, + "p95_ready_seconds": 1.758009, + "p95_queue_lag_seconds": 1.05238, + "peak_rss_bytes": 276193280, + "peak_cgroup_memory_bytes": 81403904, + "worker_cpu_seconds": 1.686702, + "requests": [ + { + "upload_seconds": 0.038460707997728605, + "status": 202 + }, + { + "upload_seconds": 0.05114162500103703, + "status": 202 + }, + { + "upload_seconds": 0.0650086250025197, + "status": 202 + }, + { + "upload_seconds": 0.07656995899742469, + "status": 202 + }, + { + "upload_seconds": 0.06139033399813343, + "status": 202 + }, + { + "upload_seconds": 0.08048520800366532, + "status": 202 + }, + { + "upload_seconds": 0.10385104199667694, + "status": 202 + }, + { + "upload_seconds": 0.10880170900054509, + "status": 202 + } + ], + "jobs": [ + { + "id": "159f8845-db5a-4058-9e02-724f4c84b7e4", + "ready_seconds": 0.901334, + "queue_lag_seconds": 0.082086, + "attempts": 1 + }, + { + "id": "f0575066-e653-4232-8936-f52b8fd26409", + "ready_seconds": 1.671008, + "queue_lag_seconds": 0.858153, + "attempts": 1 + }, + { + "id": "8effb44b-ed55-43ef-a551-be6f61c62b39", + "ready_seconds": 1.0686, + "queue_lag_seconds": 0.285051, + "attempts": 1 + }, + { + "id": "eb3f04bb-f355-4a91-9112-06348b0799c9", + "ready_seconds": 1.078856, + "queue_lag_seconds": 0.280137, + "attempts": 1 + }, + { + "id": "b1b17332-00f8-416c-998f-486921fd99ab", + "ready_seconds": 1.751065, + "queue_lag_seconds": 1.019367, + "attempts": 1 + }, + { + "id": "1d480fd1-58b1-4ac1-a570-ee90f1e2c28f", + "ready_seconds": 1.758009, + "queue_lag_seconds": 1.05238, + "attempts": 1 + }, + { + "id": "337ed589-285d-457e-9ebf-d908604d45cb", + "ready_seconds": 0.883419, + "queue_lag_seconds": 0.09468, + "attempts": 1 + }, + { + "id": "f4dfa406-a2aa-463d-8a33-19a0461bdec4", + "ready_seconds": 1.640444, + "queue_lag_seconds": 0.843, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262401.2805781, + "memory_bytes": 45793280, + "cpu_usec": 1174367, + "rss_bytes": 155987968 + }, + { + "time": 1794262401.8661351, + "memory_bytes": 81403904, + "cpu_usec": 1655568, + "rss_bytes": 276193280 + }, + { + "time": 1794262402.4577339, + "memory_bytes": 63311872, + "cpu_usec": 2249590, + "rss_bytes": 250191872 + }, + { + "time": 1794262402.867541, + "memory_bytes": 65937408, + "cpu_usec": 2779692, + "rss_bytes": 167735296 + }, + { + "time": 1794262403.2292979, + "memory_bytes": 44146688, + "cpu_usec": 2861069, + "rss_bytes": 107249664 + } + ] + }, + { + "repeat": 2, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.7222723750019213, + "throughput": 4.645026022664432, + "p95_ready_seconds": 1.514625, + "p95_queue_lag_seconds": 1.291204, + "peak_rss_bytes": 116092928, + "peak_cgroup_memory_bytes": 46608384, + "worker_cpu_seconds": 1.418109, + "requests": [ + { + "upload_seconds": 0.03548512500128709, + "status": 202 + }, + { + "upload_seconds": 0.05716745799873024, + "status": 202 + }, + { + "upload_seconds": 0.06340191700292053, + "status": 202 + }, + { + "upload_seconds": 0.07141516600677278, + "status": 202 + }, + { + "upload_seconds": 0.028250875002413522, + "status": 202 + }, + { + "upload_seconds": 0.052529375003359746, + "status": 202 + }, + { + "upload_seconds": 0.029087916998832952, + "status": 202 + }, + { + "upload_seconds": 0.04534787499869708, + "status": 202 + } + ], + "jobs": [ + { + "id": "270b668e-598b-4dbe-9a7b-28dde474ba66", + "ready_seconds": 0.295571, + "queue_lag_seconds": 0.100362, + "attempts": 1 + }, + { + "id": "c6479645-b1b0-4fef-bf17-4fa4d164eacb", + "ready_seconds": 0.465528, + "queue_lag_seconds": 0.297742, + "attempts": 1 + }, + { + "id": "fb3ad7fa-8553-4016-a561-eb2fac93dfb4", + "ready_seconds": 0.922614, + "queue_lag_seconds": 0.793791, + "attempts": 1 + }, + { + "id": "3260fcc9-0a00-4b97-a928-fec52ad5d0b1", + "ready_seconds": 1.154276, + "queue_lag_seconds": 0.916923, + "attempts": 1 + }, + { + "id": "559adef9-6f6b-416a-a826-df78660bd3eb", + "ready_seconds": 0.612838, + "queue_lag_seconds": 0.451514, + "attempts": 1 + }, + { + "id": "c5fc2c84-c12a-4384-873a-1c42faafb767", + "ready_seconds": 1.290854, + "queue_lag_seconds": 1.152609, + "attempts": 1 + }, + { + "id": "859524b5-8367-4a4b-a835-b24287388b12", + "ready_seconds": 0.798171, + "queue_lag_seconds": 0.615507, + "attempts": 1 + }, + { + "id": "57136a84-a43d-493c-bd16-2cfca4194267", + "ready_seconds": 1.514625, + "queue_lag_seconds": 1.291204, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262405.5725389, + "memory_bytes": 43700224, + "cpu_usec": 898962, + "rss_bytes": 110108672 + }, + { + "time": 1794262405.931159, + "memory_bytes": 41541632, + "cpu_usec": 1258898, + "rss_bytes": 116092928 + }, + { + "time": 1794262406.266677, + "memory_bytes": 46608384, + "cpu_usec": 1588260, + "rss_bytes": 106504192 + }, + { + "time": 1794262406.6328561, + "memory_bytes": 46579712, + "cpu_usec": 1934212, + "rss_bytes": 106582016 + }, + { + "time": 1794262407.030196, + "memory_bytes": 45948928, + "cpu_usec": 2317071, + "rss_bytes": 106602496 + } + ] + }, + { + "repeat": 3, + "image": "small", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.0493234580062563, + "throughput": 3.903727334377479, + "p95_ready_seconds": 1.879584, + "p95_queue_lag_seconds": 1.052141, + "peak_rss_bytes": 346427392, + "peak_cgroup_memory_bytes": 87994368, + "worker_cpu_seconds": 1.58035, + "requests": [ + { + "upload_seconds": 0.03708612499758601, + "status": 202 + }, + { + "upload_seconds": 0.04979004099732265, + "status": 202 + }, + { + "upload_seconds": 0.062493375000485685, + "status": 202 + }, + { + "upload_seconds": 0.07259312500536907, + "status": 202 + }, + { + "upload_seconds": 0.05210570799681591, + "status": 202 + }, + { + "upload_seconds": 0.04339720799907809, + "status": 202 + }, + { + "upload_seconds": 0.038661874998069834, + "status": 202 + }, + { + "upload_seconds": 0.03924845800065668, + "status": 202 + } + ], + "jobs": [ + { + "id": "fbcb2196-1abf-4fb3-a818-fe3aee15ea75", + "ready_seconds": 1.072389, + "queue_lag_seconds": 0.109725, + "attempts": 1 + }, + { + "id": "97441d05-e70f-4b67-a7a7-b0f2ecd3809b", + "ready_seconds": 1.851275, + "queue_lag_seconds": 1.028243, + "attempts": 1 + }, + { + "id": "1f1d4d72-269a-4984-8f39-099ff5e62cce", + "ready_seconds": 1.091508, + "queue_lag_seconds": 0.089204, + "attempts": 1 + }, + { + "id": "f498e151-5de9-4898-82d0-00d613c71b73", + "ready_seconds": 1.065876, + "queue_lag_seconds": 0.162786, + "attempts": 1 + }, + { + "id": "3369e169-4c12-4357-af6d-8ead89d4100c", + "ready_seconds": 1.82523, + "queue_lag_seconds": 1.032192, + "attempts": 1 + }, + { + "id": "af3058d6-8ef2-4d7b-a16d-5cdc8ee095db", + "ready_seconds": 1.879584, + "queue_lag_seconds": 1.052141, + "attempts": 1 + }, + { + "id": "8f9ed5dc-eb28-4669-a444-9c2bf7c178bd", + "ready_seconds": 1.089309, + "queue_lag_seconds": 0.112428, + "attempts": 1 + }, + { + "id": "c143932d-a6f1-49b4-9e26-280cb1bf78d3", + "ready_seconds": 1.731002, + "queue_lag_seconds": 1.039114, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262410.393358, + "memory_bytes": 55742464, + "cpu_usec": 1271330, + "rss_bytes": 226807808 + }, + { + "time": 1794262410.898071, + "memory_bytes": 87994368, + "cpu_usec": 1828256, + "rss_bytes": 346427392 + }, + { + "time": 1794262411.4896598, + "memory_bytes": 78336000, + "cpu_usec": 2311204, + "rss_bytes": 275132416 + }, + { + "time": 1794262411.910083, + "memory_bytes": 49283072, + "cpu_usec": 2851680, + "rss_bytes": 106897408 + } + ] + }, + { + "repeat": 3, + "image": "small", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 2.0718359999955283, + "throughput": 3.8613094858942825, + "p95_ready_seconds": 1.579124, + "p95_queue_lag_seconds": 1.313482, + "peak_rss_bytes": 167481344, + "peak_cgroup_memory_bytes": 60809216, + "worker_cpu_seconds": 1.489548, + "requests": [ + { + "upload_seconds": 0.046921875000407454, + "status": 202 + }, + { + "upload_seconds": 0.06458500000007916, + "status": 202 + }, + { + "upload_seconds": 0.09368895899388008, + "status": 202 + }, + { + "upload_seconds": 0.10713133300305344, + "status": 202 + }, + { + "upload_seconds": 0.05416333399625728, + "status": 202 + }, + { + "upload_seconds": 0.08453687500150409, + "status": 202 + }, + { + "upload_seconds": 0.04616966699541081, + "status": 202 + }, + { + "upload_seconds": 0.06727762499940582, + "status": 202 + } + ], + "jobs": [ + { + "id": "cb1d7378-a81c-4cba-abc3-765e49e4feb8", + "ready_seconds": 0.380824, + "queue_lag_seconds": 0.103185, + "attempts": 1 + }, + { + "id": "0bffdb9c-b637-4da7-9206-0798e9b0e2f0", + "ready_seconds": 1.023788, + "queue_lag_seconds": 0.871741, + "attempts": 1 + }, + { + "id": "056073e3-d257-4861-8d33-9872d20b520d", + "ready_seconds": 1.579124, + "queue_lag_seconds": 1.313482, + "attempts": 1 + }, + { + "id": "3dff25ed-0223-42b6-94d6-e627ab3c9000", + "ready_seconds": 0.745562, + "queue_lag_seconds": 0.544542, + "attempts": 1 + }, + { + "id": "66eafee7-5bd0-419e-b069-6b33176339b3", + "ready_seconds": 0.894289, + "queue_lag_seconds": 0.733248, + "attempts": 1 + }, + { + "id": "176ef072-0811-4a76-a378-5f7c20103066", + "ready_seconds": 1.320301, + "queue_lag_seconds": 1.201136, + "attempts": 1 + }, + { + "id": "49fc3f2c-4dea-4f56-a80d-2ca4a207eb71", + "ready_seconds": 0.557781, + "queue_lag_seconds": 0.379291, + "attempts": 1 + }, + { + "id": "ae186aba-bbe9-4406-b2fe-6a1cf2e2cc04", + "ready_seconds": 1.208355, + "queue_lag_seconds": 1.014114, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262415.613364, + "memory_bytes": 43040768, + "cpu_usec": 1254626, + "rss_bytes": 134688768 + }, + { + "time": 1794262415.977576, + "memory_bytes": 44519424, + "cpu_usec": 1625601, + "rss_bytes": 104448000 + }, + { + "time": 1794262416.309793, + "memory_bytes": 59355136, + "cpu_usec": 1972565, + "rss_bytes": 167481344 + }, + { + "time": 1794262416.659306, + "memory_bytes": 57217024, + "cpu_usec": 2302566, + "rss_bytes": 166821888 + }, + { + "time": 1794262417.0047119, + "memory_bytes": 60809216, + "cpu_usec": 2648258, + "rss_bytes": 166809600 + }, + { + "time": 1794262417.332344, + "memory_bytes": 48402432, + "cpu_usec": 2744174, + "rss_bytes": 106450944 + } + ] + }, + { + "repeat": 3, + "image": "small", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 1.7169063330002245, + "throughput": 4.659543649082081, + "p95_ready_seconds": 1.565986, + "p95_queue_lag_seconds": 1.147304, + "peak_rss_bytes": 225964032, + "peak_cgroup_memory_bytes": 68136960, + "worker_cpu_seconds": 1.301956, + "requests": [ + { + "upload_seconds": 0.041546416003257036, + "status": 202 + }, + { + "upload_seconds": 0.060863958999107126, + "status": 202 + }, + { + "upload_seconds": 0.07863833400188014, + "status": 202 + }, + { + "upload_seconds": 0.1037755420038593, + "status": 202 + }, + { + "upload_seconds": 0.08276591699541314, + "status": 202 + }, + { + "upload_seconds": 0.10874649999459507, + "status": 202 + }, + { + "upload_seconds": 0.10369779200118501, + "status": 202 + }, + { + "upload_seconds": 0.0915510409977287, + "status": 202 + } + ], + "jobs": [ + { + "id": "a5f773f5-7603-4661-ae9a-83b0b3f817a0", + "ready_seconds": 0.82323, + "queue_lag_seconds": 0.428073, + "attempts": 1 + }, + { + "id": "6f123c49-0e98-468f-9a0f-c522c728df63", + "ready_seconds": 1.515223, + "queue_lag_seconds": 1.099548, + "attempts": 1 + }, + { + "id": "13c2931e-f809-4b66-96c7-7b58429a3446", + "ready_seconds": 0.471413, + "queue_lag_seconds": 0.062399, + "attempts": 1 + }, + { + "id": "acd77d65-886a-497b-a023-f398646974fe", + "ready_seconds": 0.452546, + "queue_lag_seconds": 0.063642, + "attempts": 1 + }, + { + "id": "ef507876-972e-4087-a198-f0ce5536cc29", + "ready_seconds": 1.18232, + "queue_lag_seconds": 0.812552, + "attempts": 1 + }, + { + "id": "86fa8bc4-fc9d-4bee-b587-2a7897dbaa0a", + "ready_seconds": 1.159715, + "queue_lag_seconds": 0.832331, + "attempts": 1 + }, + { + "id": "ca41c982-9e7b-48d8-8c57-994817c94498", + "ready_seconds": 0.849327, + "queue_lag_seconds": 0.447346, + "attempts": 1 + }, + { + "id": "d169a9e2-5ce7-4261-a22a-1ece2a5a3563", + "ready_seconds": 1.565986, + "queue_lag_seconds": 1.147304, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262420.793129, + "memory_bytes": 56025088, + "cpu_usec": 1490008, + "rss_bytes": 192344064 + }, + { + "time": 1794262421.21904, + "memory_bytes": 63148032, + "cpu_usec": 1917366, + "rss_bytes": 217477120 + }, + { + "time": 1794262421.61412, + "memory_bytes": 68136960, + "cpu_usec": 2351511, + "rss_bytes": 225693696 + }, + { + "time": 1794262422.095878, + "memory_bytes": 65114112, + "cpu_usec": 2791964, + "rss_bytes": 225964032 + } + ] + }, + { + "repeat": 3, + "image": "large", + "concurrency": 4, + "prefetch": 4, + "successful_jobs": 8, + "errors": 0, + "seconds": 11.567342832997383, + "throughput": 0.691602221486765, + "p95_ready_seconds": 9.949, + "p95_queue_lag_seconds": 5.551759, + "peak_rss_bytes": 378175488, + "peak_cgroup_memory_bytes": 177049600, + "worker_cpu_seconds": 10.350173, + "requests": [ + { + "upload_seconds": 0.5822087910055416, + "status": 202 + }, + { + "upload_seconds": 1.0521450830055983, + "status": 202 + }, + { + "upload_seconds": 1.197281708002265, + "status": 202 + }, + { + "upload_seconds": 1.3832097920021624, + "status": 202 + }, + { + "upload_seconds": 1.0122472919974825, + "status": 202 + }, + { + "upload_seconds": 0.7380604999998468, + "status": 202 + }, + { + "upload_seconds": 0.9001225829997566, + "status": 202 + }, + { + "upload_seconds": 0.986606499995105, + "status": 202 + } + ], + "jobs": [ + { + "id": "3ab05215-56fd-4ef1-9f07-63b42aec55dc", + "ready_seconds": 9.949, + "queue_lag_seconds": 5.29829, + "attempts": 1 + }, + { + "id": "34691cf0-2b94-49f4-a353-52d6d6bb1175", + "ready_seconds": 6.215799, + "queue_lag_seconds": 0.807096, + "attempts": 1 + }, + { + "id": "930f6c90-f771-4243-a97f-d5977d418647", + "ready_seconds": 9.630719, + "queue_lag_seconds": 5.301089, + "attempts": 1 + }, + { + "id": "f32a3a06-7f5e-4ffe-9977-3ca5377e0c6e", + "ready_seconds": 6.042704, + "queue_lag_seconds": 0.454156, + "attempts": 1 + }, + { + "id": "2b0e0991-2462-4c1a-af48-cb6ad1671840", + "ready_seconds": 4.482368, + "queue_lag_seconds": 0.247504, + "attempts": 1 + }, + { + "id": "093171c9-4d36-47cd-88cd-be205b5de3de", + "ready_seconds": 9.739683, + "queue_lag_seconds": 5.551759, + "attempts": 1 + }, + { + "id": "92142d14-8e43-450e-91ac-b42cebadb100", + "ready_seconds": 7.883405, + "queue_lag_seconds": 3.759794, + "attempts": 1 + }, + { + "id": "f53f0efe-65c0-43b6-b6a2-a348b294f895", + "ready_seconds": 6.53409, + "queue_lag_seconds": 0.636628, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262425.999084, + "memory_bytes": 41082880, + "cpu_usec": 1663015, + "rss_bytes": 106700800 + }, + { + "time": 1794262426.380343, + "memory_bytes": 48549888, + "cpu_usec": 1711939, + "rss_bytes": 106708992 + }, + { + "time": 1794262426.8502111, + "memory_bytes": 72732672, + "cpu_usec": 2101364, + "rss_bytes": 180154368 + }, + { + "time": 1794262427.480101, + "memory_bytes": 90595328, + "cpu_usec": 2668924, + "rss_bytes": 212520960 + }, + { + "time": 1794262428.167731, + "memory_bytes": 116719616, + "cpu_usec": 3399935, + "rss_bytes": 299827200 + }, + { + "time": 1794262429.151486, + "memory_bytes": 136507392, + "cpu_usec": 4315373, + "rss_bytes": 367857664 + }, + { + "time": 1794262429.644265, + "memory_bytes": 153702400, + "cpu_usec": 4866121, + "rss_bytes": 336216064 + }, + { + "time": 1794262430.258294, + "memory_bytes": 171859968, + "cpu_usec": 5443319, + "rss_bytes": 358014976 + }, + { + "time": 1794262430.9448361, + "memory_bytes": 158593024, + "cpu_usec": 6109826, + "rss_bytes": 346882048 + }, + { + "time": 1794262431.7647948, + "memory_bytes": 106033152, + "cpu_usec": 6844068, + "rss_bytes": 271921152 + }, + { + "time": 1794262432.253286, + "memory_bytes": 75567104, + "cpu_usec": 7542413, + "rss_bytes": 162009088 + }, + { + "time": 1794262432.756811, + "memory_bytes": 100659200, + "cpu_usec": 8022771, + "rss_bytes": 193961984 + }, + { + "time": 1794262433.343982, + "memory_bytes": 117932032, + "cpu_usec": 8571197, + "rss_bytes": 287518720 + }, + { + "time": 1794262433.947423, + "memory_bytes": 142024704, + "cpu_usec": 9120937, + "rss_bytes": 378175488 + }, + { + "time": 1794262434.645704, + "memory_bytes": 153182208, + "cpu_usec": 9790721, + "rss_bytes": 336863232 + }, + { + "time": 1794262435.561161, + "memory_bytes": 177049600, + "cpu_usec": 10665993, + "rss_bytes": 367210496 + }, + { + "time": 1794262436.239607, + "memory_bytes": 145113088, + "cpu_usec": 11437552, + "rss_bytes": 337195008 + }, + { + "time": 1794262436.645513, + "memory_bytes": 70598656, + "cpu_usec": 11891048, + "rss_bytes": 115445760 + }, + { + "time": 1794262437.032929, + "memory_bytes": 73572352, + "cpu_usec": 12013188, + "rss_bytes": 115449856 + } + ] + }, + { + "repeat": 3, + "image": "large", + "concurrency": 1, + "prefetch": 1, + "successful_jobs": 8, + "errors": 0, + "seconds": 9.895933082996635, + "throughput": 0.8084129038570137, + "p95_ready_seconds": 7.692664, + "p95_queue_lag_seconds": 6.867809, + "peak_rss_bytes": 197115904, + "peak_cgroup_memory_bytes": 101888000, + "worker_cpu_seconds": 7.538266, + "requests": [ + { + "upload_seconds": 0.4662438750019646, + "status": 202 + }, + { + "upload_seconds": 0.6516994580015307, + "status": 202 + }, + { + "upload_seconds": 0.9337635410047369, + "status": 202 + }, + { + "upload_seconds": 1.0977309580048313, + "status": 202 + }, + { + "upload_seconds": 1.2482558750052704, + "status": 202 + }, + { + "upload_seconds": 1.2442702500047744, + "status": 202 + }, + { + "upload_seconds": 1.3717015000001993, + "status": 202 + }, + { + "upload_seconds": 1.3582418339938158, + "status": 202 + } + ], + "jobs": [ + { + "id": "ad0538c4-d78f-4441-abc2-94017e43989a", + "ready_seconds": 4.663425, + "queue_lag_seconds": 3.660659, + "attempts": 1 + }, + { + "id": "22cb074d-623d-403a-88a5-98c747486509", + "ready_seconds": 7.034566, + "queue_lag_seconds": 5.984522, + "attempts": 1 + }, + { + "id": "4afc9b98-cbb1-461a-b05a-2f2d2817f877", + "ready_seconds": 2.090172, + "queue_lag_seconds": 0.199733, + "attempts": 1 + }, + { + "id": "33b0f653-e3f4-49c0-8cb5-e794807f3859", + "ready_seconds": 5.792541, + "queue_lag_seconds": 4.419849, + "attempts": 1 + }, + { + "id": "589106c2-8835-4297-bdf9-ce7b37bd0d51", + "ready_seconds": 7.692664, + "queue_lag_seconds": 6.867809, + "attempts": 1 + }, + { + "id": "8d6c6505-44a5-4256-9570-f064b95b020e", + "ready_seconds": 2.979272, + "queue_lag_seconds": 2.096541, + "attempts": 1 + }, + { + "id": "6e4e160e-70b4-478e-9feb-1fe797c19cc1", + "ready_seconds": 6.533553, + "queue_lag_seconds": 5.647585, + "attempts": 1 + }, + { + "id": "1e65c301-e37a-4806-9dcf-2764d82ff3ed", + "ready_seconds": 3.860303, + "queue_lag_seconds": 2.863414, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262441.173097, + "memory_bytes": 45621248, + "cpu_usec": 1729800, + "rss_bytes": 106565632 + }, + { + "time": 1794262441.683848, + "memory_bytes": 52445184, + "cpu_usec": 2007744, + "rss_bytes": 121364480 + }, + { + "time": 1794262442.398595, + "memory_bytes": 82268160, + "cpu_usec": 2508688, + "rss_bytes": 184696832 + }, + { + "time": 1794262442.823581, + "memory_bytes": 52346880, + "cpu_usec": 2978119, + "rss_bytes": 107433984 + }, + { + "time": 1794262443.2013881, + "memory_bytes": 50536448, + "cpu_usec": 3010074, + "rss_bytes": 107433984 + }, + { + "time": 1794262443.56844, + "memory_bytes": 65798144, + "cpu_usec": 3272589, + "rss_bytes": 154914816 + }, + { + "time": 1794262443.9425979, + "memory_bytes": 98009088, + "cpu_usec": 3642023, + "rss_bytes": 193007616 + }, + { + "time": 1794262444.351456, + "memory_bytes": 56811520, + "cpu_usec": 3989156, + "rss_bytes": 133300224 + }, + { + "time": 1794262444.7525601, + "memory_bytes": 96026624, + "cpu_usec": 4365059, + "rss_bytes": 191180800 + }, + { + "time": 1794262445.159318, + "memory_bytes": 57802752, + "cpu_usec": 4638462, + "rss_bytes": 107241472 + }, + { + "time": 1794262445.55554, + "memory_bytes": 82378752, + "cpu_usec": 5002983, + "rss_bytes": 180408320 + }, + { + "time": 1794262445.967915, + "memory_bytes": 77910016, + "cpu_usec": 5416420, + "rss_bytes": 174346240 + }, + { + "time": 1794262446.46769, + "memory_bytes": 61718528, + "cpu_usec": 5769579, + "rss_bytes": 117579776 + }, + { + "time": 1794262446.813834, + "memory_bytes": 92237824, + "cpu_usec": 6089664, + "rss_bytes": 183562240 + }, + { + "time": 1794262447.255758, + "memory_bytes": 86560768, + "cpu_usec": 6547945, + "rss_bytes": 181460992 + }, + { + "time": 1794262447.682785, + "memory_bytes": 59322368, + "cpu_usec": 6751679, + "rss_bytes": 112062464 + }, + { + "time": 1794262448.0586839, + "memory_bytes": 96845824, + "cpu_usec": 7162998, + "rss_bytes": 187457536 + }, + { + "time": 1794262448.392413, + "memory_bytes": 68132864, + "cpu_usec": 7494310, + "rss_bytes": 112062464 + }, + { + "time": 1794262448.806738, + "memory_bytes": 72351744, + "cpu_usec": 7748876, + "rss_bytes": 128098304 + }, + { + "time": 1794262449.289658, + "memory_bytes": 101888000, + "cpu_usec": 8201010, + "rss_bytes": 197115904 + }, + { + "time": 1794262449.658975, + "memory_bytes": 66256896, + "cpu_usec": 8496781, + "rss_bytes": 112062464 + }, + { + "time": 1794262450.018466, + "memory_bytes": 97886208, + "cpu_usec": 8868606, + "rss_bytes": 188559360 + }, + { + "time": 1794262450.372195, + "memory_bytes": 68079616, + "cpu_usec": 9226409, + "rss_bytes": 112062464 + }, + { + "time": 1794262450.707536, + "memory_bytes": 65867776, + "cpu_usec": 9268066, + "rss_bytes": 112066560 + } + ] + }, + { + "repeat": 3, + "image": "large", + "concurrency": 2, + "prefetch": 2, + "successful_jobs": 8, + "errors": 0, + "seconds": 10.974901708999823, + "throughput": 0.7289359132428226, + "p95_ready_seconds": 9.261154, + "p95_queue_lag_seconds": 7.115312, + "peak_rss_bytes": 281636864, + "peak_cgroup_memory_bytes": 137314304, + "worker_cpu_seconds": 8.619296, + "requests": [ + { + "upload_seconds": 0.5528413339998224, + "status": 202 + }, + { + "upload_seconds": 0.618678999999247, + "status": 202 + }, + { + "upload_seconds": 0.9114771669992479, + "status": 202 + }, + { + "upload_seconds": 1.132410833000904, + "status": 202 + }, + { + "upload_seconds": 0.7579690839993418, + "status": 202 + }, + { + "upload_seconds": 0.8154654999962077, + "status": 202 + }, + { + "upload_seconds": 0.4669017080013873, + "status": 202 + }, + { + "upload_seconds": 0.8220295840001199, + "status": 202 + } + ], + "jobs": [ + { + "id": "11319b09-8480-4bf7-8343-467739eebbbe", + "ready_seconds": 5.278422, + "queue_lag_seconds": 2.866249, + "attempts": 1 + }, + { + "id": "04bcebd4-b08d-4cd0-a0d4-0cf7941f7ff6", + "ready_seconds": 3.067793, + "queue_lag_seconds": 0.269334, + "attempts": 1 + }, + { + "id": "144031aa-beb1-48e3-86a5-872ce73c29d5", + "ready_seconds": 5.366127, + "queue_lag_seconds": 2.950958, + "attempts": 1 + }, + { + "id": "b6ca8afd-927a-4b78-bf48-e9de74dde5e3", + "ready_seconds": 7.427039, + "queue_lag_seconds": 4.944347, + "attempts": 1 + }, + { + "id": "6c86c361-f45c-462c-b7b2-58bfc353c846", + "ready_seconds": 9.261154, + "queue_lag_seconds": 7.115312, + "attempts": 1 + }, + { + "id": "d3024905-e173-431c-b093-f52da938e00e", + "ready_seconds": 3.060139, + "queue_lag_seconds": 0.3589, + "attempts": 1 + }, + { + "id": "27383307-0cf0-48b2-86b5-679938e461c7", + "ready_seconds": 7.223779, + "queue_lag_seconds": 4.788639, + "attempts": 1 + }, + { + "id": "39d7dba3-aea0-46fc-9eb1-2919a447f6f2", + "ready_seconds": 9.095291, + "queue_lag_seconds": 6.945049, + "attempts": 1 + } + ], + "samples": [ + { + "time": 1794262455.167735, + "memory_bytes": 41877504, + "cpu_usec": 1728128, + "rss_bytes": 106283008 + }, + { + "time": 1794262455.6366248, + "memory_bytes": 46886912, + "cpu_usec": 1838601, + "rss_bytes": 106319872 + }, + { + "time": 1794262456.231374, + "memory_bytes": 67100672, + "cpu_usec": 2411824, + "rss_bytes": 197558272 + }, + { + "time": 1794262456.781374, + "memory_bytes": 112640000, + "cpu_usec": 2948767, + "rss_bytes": 257323008 + }, + { + "time": 1794262457.389336, + "memory_bytes": 132554752, + "cpu_usec": 3488311, + "rss_bytes": 280322048 + }, + { + "time": 1794262457.9726589, + "memory_bytes": 61906944, + "cpu_usec": 4087858, + "rss_bytes": 110342144 + }, + { + "time": 1794262458.36638, + "memory_bytes": 44871680, + "cpu_usec": 4209324, + "rss_bytes": 109940736 + }, + { + "time": 1794262458.890047, + "memory_bytes": 65904640, + "cpu_usec": 4690364, + "rss_bytes": 186933248 + }, + { + "time": 1794262459.3172328, + "memory_bytes": 109899776, + "cpu_usec": 5169719, + "rss_bytes": 258424832 + }, + { + "time": 1794262459.798551, + "memory_bytes": 137314304, + "cpu_usec": 5659124, + "rss_bytes": 281636864 + }, + { + "time": 1794262460.282627, + "memory_bytes": 54554624, + "cpu_usec": 6133829, + "rss_bytes": 110514176 + }, + { + "time": 1794262460.656821, + "memory_bytes": 57778176, + "cpu_usec": 6278094, + "rss_bytes": 110522368 + }, + { + "time": 1794262461.1044202, + "memory_bytes": 61755392, + "cpu_usec": 6620843, + "rss_bytes": 140283904 + }, + { + "time": 1794262461.5962932, + "memory_bytes": 109383680, + "cpu_usec": 7093342, + "rss_bytes": 256442368 + }, + { + "time": 1794262462.117553, + "memory_bytes": 127512576, + "cpu_usec": 7574233, + "rss_bytes": 276664320 + }, + { + "time": 1794262462.503906, + "memory_bytes": 100667392, + "cpu_usec": 8003279, + "rss_bytes": 248053760 + }, + { + "time": 1794262462.8923898, + "memory_bytes": 58302464, + "cpu_usec": 8275401, + "rss_bytes": 110604288 + }, + { + "time": 1794262463.295119, + "memory_bytes": 46878720, + "cpu_usec": 8426428, + "rss_bytes": 110628864 + }, + { + "time": 1794262463.8872309, + "memory_bytes": 74416128, + "cpu_usec": 8986830, + "rss_bytes": 201560064 + }, + { + "time": 1794262464.382762, + "memory_bytes": 115367936, + "cpu_usec": 9503601, + "rss_bytes": 264175616 + }, + { + "time": 1794262464.810621, + "memory_bytes": 133775360, + "cpu_usec": 9973809, + "rss_bytes": 258146304 + }, + { + "time": 1794262465.197512, + "memory_bytes": 54276096, + "cpu_usec": 10269619, + "rss_bytes": 110628864 + }, + { + "time": 1794262465.553686, + "memory_bytes": 49750016, + "cpu_usec": 10347424, + "rss_bytes": 110632960 + } + ] + } + ], + "backpressure": { + "attempted": 8, + "accepted": 4, + "rejected_429": 4, + "pending_at_limit": 4, + "all_accepted_recovered": true, + "recovery_seconds": 2.6798054590035463 + }, + "environment": "linux aarch64 8 CPUs 4106604544 bytes RAM", + "source_sha256": "3c21d297fea5f8b3c0d2c717c62a7c44dec60e1f8dac0854de3084acecf22eb4" +} diff --git a/evidence/overload.png b/evidence/overload.png new file mode 100644 index 00000000..1123635c Binary files /dev/null and b/evidence/overload.png differ diff --git a/evidence/overload.svg b/evidence/overload.svg new file mode 100644 index 00000000..e25510bf --- /dev/null +++ b/evidence/overload.svg @@ -0,0 +1,2847 @@ + + + + + + + + 2026-11-10T01:16:37.279149 + image/svg+xml + + + Matplotlib v3.11.2, https://matplotlib.org/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..e51e7f03 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,23 @@ +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab5-api-1 messenger-lab5:local "dotnet Messenger.Ap…" api 2 minutes ago Up About a minute (healthy) 8080/tcp +mtusi2026lab5-api-2 messenger-lab5:local "dotnet Messenger.Ap…" api 2 minutes ago Up About a minute (healthy) 8080/tcp +mtusi2026lab5-broker-1 rabbitmq:4.2.0-alpine "docker-entrypoint.s…" broker 14 minutes ago Up About a minute (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp +mtusi2026lab5-dispatcher-1 messenger-lab5:local "dotnet Messenger.Ap…" dispatcher 2 seconds ago Up Less than a second 8080/tcp +mtusi2026lab5-lb-1 haproxy:3.2.7-alpine "docker-entrypoint.s…" lb 14 minutes ago Up 13 minutes (healthy) 127.0.0.1:18085->8080/tcp +mtusi2026lab5-postgres-1 postgres:17.6 "docker-entrypoint.s…" postgres 14 minutes ago Up 14 minutes (healthy) 5432/tcp +mtusi2026lab5-redis-1 redis:7.4.2-alpine "docker-entrypoint.s…" redis 14 minutes ago Up 14 minutes (healthy) 6379/tcp +mtusi2026lab5-s3-1 chrislusf/seaweedfs:3.97 "/entrypoint.sh serv…" s3 6 minutes ago Up 6 minutes (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp +mtusi2026lab5-worker-1 messenger-lab5:local "dotnet Messenger.Ap…" worker 2 seconds ago Up Less than a second 8080/tcp +.venv/bin/python tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:18085" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab5-api-1 messenger-lab5:local "dotnet Messenger.Ap…" api 2 minutes ago Up 7 seconds (healthy) 8080/tcp +mtusi2026lab5-api-2 messenger-lab5:local "dotnet Messenger.Ap…" api 2 minutes ago Up 7 seconds (healthy) 8080/tcp +mtusi2026lab5-broker-1 rabbitmq:4.2.0-alpine "docker-entrypoint.s…" broker 15 minutes ago Up 30 seconds (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp +mtusi2026lab5-dispatcher-1 messenger-lab5:local "dotnet Messenger.Ap…" dispatcher 44 seconds ago Up 7 seconds 8080/tcp +mtusi2026lab5-lb-1 haproxy:3.2.7-alpine "docker-entrypoint.s…" lb 15 minutes ago Up 7 seconds (healthy) 127.0.0.1:18085->8080/tcp +mtusi2026lab5-postgres-1 postgres:17.6 "docker-entrypoint.s…" postgres 15 minutes ago Up 32 seconds (healthy) 5432/tcp +mtusi2026lab5-redis-1 redis:7.4.2-alpine "docker-entrypoint.s…" redis 15 minutes ago Up 32 seconds (healthy) 6379/tcp +mtusi2026lab5-s3-1 chrislusf/seaweedfs:3.97 "/entrypoint.sh serv…" s3 7 minutes ago Up 29 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp +mtusi2026lab5-worker-1 messenger-lab5:local "dotnet Messenger.Ap…" worker 44 seconds ago Up 7 seconds 8080/tcp diff --git a/evidence/pipeline-checks.txt b/evidence/pipeline-checks.txt new file mode 100644 index 00000000..eea0bcc7 --- /dev/null +++ b/evidence/pipeline-checks.txt @@ -0,0 +1,2 @@ +.venv/bin/python student-tests/pipeline_tests.py --base-url "http://localhost:18085" +PASS: 11 pipeline scenarios diff --git a/evidence/pipeline.json b/evidence/pipeline.json new file mode 100644 index 00000000..b7437216 --- /dev/null +++ b/evidence/pipeline.json @@ -0,0 +1,18 @@ +{ + "checks": [ + "HTTP size/MIME/corruption/filename validation remains compatible with Lab4", + "202 while worker stopped; durable outbox survives API restart; published queue survives broker restart; same ID becomes ready", + "thumbnail/crop/WebP bytes and dimensions; no EXIF; orientation applied; transparent small image remains small with white background; private variants", + "three duplicate ready deliveries are no-op", + "after_objects: worker killed, same logical job recovered, one published result", + "after_commit: worker killed, same logical job recovered, one published result", + "publisher confirm before outbox COMMIT crash recovers without lost task or duplicate effect", + "invalid staging checksum becomes failed after one attempt; failed attachment cannot be linked", + "worker independently rejects >40 million pixels before bitmap allocation", + "transient failure retries with bounded attempts then failed; explicit replay succeeds; replay of ready is no-op", + "ready staging cleaned after durable publication; failed staging retained for replay" + ], + "broker_queue_before_restart": "[\n{\"name\":\"attachments.v1\",\"messages_ready\":1}\n]\n", + "retry_test_max_attempts": 3, + "default_max_attempts": 5 +} diff --git a/evidence/ui-queued.jpg b/evidence/ui-queued.jpg new file mode 100644 index 00000000..b8762d18 Binary files /dev/null and b/evidence/ui-queued.jpg differ diff --git a/evidence/ui-ready.jpg b/evidence/ui-ready.jpg new file mode 100644 index 00000000..55bceee2 Binary files /dev/null and b/evidence/ui-ready.jpg differ diff --git a/evidence/ui-reliability.txt b/evidence/ui-reliability.txt new file mode 100644 index 00000000..6c118083 --- /dev/null +++ b/evidence/ui-reliability.txt @@ -0,0 +1,19 @@ +Проверка после изменений интерфейса и настроек сервисов: + +make check: PASS +make validate: PASS +make test: PASS +student-tests/chunked_login.py: PASS +student-tests/processing_panel.py: PASS +make operations-test: PASS (повторное задание обработано worker) +make test-failover ACTION_SCRIPT=scripts/stop-one.sh: + observed_instances_before: 2 + successful_writes: 109 + failed_attempts: 1 + first_recovery_seconds: 2.756 + PASS: подтверждённые данные и сессия сохранены. + +Первый запуск failover остановился до сценария из-за тайм-аута регистрации, +пока RabbitMQ запускался. После готовности сервисов повторный запуск прошёл. +В браузере проверены поиск, копирование сообщения, вставка PNG, превью и удаление +черновика, фильтр ошибок и отображение состояний сервисов. diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..24823f9a --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,40 @@ +global + maxconn 2000 + stats socket ipv4@127.0.0.1:9999 level admin + hard-stop-after 8s + +defaults + mode http + timeout connect 2s + timeout check 2s + timeout client 15s + timeout server 12s + timeout http-request 5s + timeout http-keep-alive 5s + timeout queue 2s + retries 1 + +resolvers docker + nameserver dns 127.0.0.11:53 + accepted_payload_size 8192 + resolve_retries 2 + timeout resolve 500ms + timeout retry 500ms + hold valid 1s + hold nx 1s + hold other 1s + hold refused 1s + hold timeout 1s + hold obsolete 0s + +frontend public + bind :8080 + default_backend messenger + +backend messenger + balance roundrobin + option httpchk + http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host localhost + http-check expect status 200 + default-server check inter 1s fastinter 500ms downinter 1s fall 2 rise 2 + server-template api 1-8 api:8080 resolvers docker resolve-prefer ipv4 init-addr none diff --git a/requirements-research.txt b/requirements-research.txt new file mode 100644 index 00000000..71363ac8 --- /dev/null +++ b/requirements-research.txt @@ -0,0 +1 @@ +matplotlib==3.11.2 diff --git a/requirements-student.txt b/requirements-student.txt new file mode 100644 index 00000000..7126a796 --- /dev/null +++ b/requirements-student.txt @@ -0,0 +1 @@ +Pillow==12.0.0 diff --git a/scripts/compose.sh b/scripts/compose.sh new file mode 100755 index 00000000..31e8ca03 --- /dev/null +++ b/scripts/compose.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +root="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" +exec docker compose --project-name mtusi2026lab5 --project-directory "$root" "$@" diff --git a/scripts/configure-operator.py b/scripts/configure-operator.py new file mode 100644 index 00000000..5761db7b --- /dev/null +++ b/scripts/configure-operator.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +"""Grant the local operations UI to an existing messenger account.""" +import re,subprocess,sys +from pathlib import Path +root=Path(__file__).resolve().parents[1] +if len(sys.argv)!=2 or not re.fullmatch('[a-z0-9_]{3,32}',sys.argv[1]):raise SystemExit('Укажите логин: make operator USERNAME=n1k0der') +result=subprocess.check_output([str(root/'scripts/compose.sh'),'exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-c',"SELECT id FROM users WHERE username='"+sys.argv[1]+"'"],cwd=root,text=True).strip() +if not result:raise SystemExit('Сначала зарегистрируйте этот аккаунт в мессенджере') +p=root/'.env';lines=p.read_text().splitlines() if p.exists() else [] +lines=[line for line in lines if not line.startswith('OPS_USER_ID=')];lines.append('OPS_USER_ID='+result);p.write_text('\n'.join(lines)+'\n');p.chmod(0o600) +subprocess.run([str(root/'scripts/compose.sh'),'up','-d','--no-deps','--wait','api'],cwd=root,check=True) +print('Панель доступна аккаунту '+sys.argv[1]+'. Перезайдите в мессенджер.') diff --git a/scripts/init-secrets.py b/scripts/init-secrets.py new file mode 100644 index 00000000..5aec7161 --- /dev/null +++ b/scripts/init-secrets.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +from pathlib import Path +import secrets,json,os +os.umask(0o077) +p=Path(__file__).resolve().parents[1]/'secrets';p.mkdir(exist_ok=True) +def secret(name,size=24): + f=p/name + if not f.exists(): f.write_text(secrets.token_hex(size)) + return f.read_text().strip() +pw=secret('postgres_password') +f=p/'database_url' +if not f.exists(): f.write_text('Host=postgres;Port=5432;Database=messenger;Username=messenger;Password='+pw) +admin=secret('s3_admin_access',12); admin_secret=secret('s3_admin_secret') +access=secret('s3_access',12); key=secret('s3_secret') +config={'identities':[ + {'name':'bootstrap','credentials':[{'accessKey':admin,'secretKey':admin_secret}],'actions':['Admin','Read','Write','List','Tagging']}, + {'name':'messenger','credentials':[{'accessKey':access,'secretKey':key}],'actions':['Read:messenger','Write:messenger','List:messenger']}]} +(p/'s3_config').write_text(json.dumps(config)) +print('Local secrets ready') + +broker_password=secret('broker_password') +(p/'broker.conf').write_text('default_user = messenger\ndefault_pass = '+broker_password+'\ndefault_vhost = messenger\n') + +(p.parent/'.local/faults').mkdir(parents=True,exist_ok=True) + +(p.parent/'.local/faults').chmod(0o777) diff --git a/scripts/init-secrets.sh b/scripts/init-secrets.sh new file mode 100755 index 00000000..063eb4ac --- /dev/null +++ b/scripts/init-secrets.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/init-secrets.py" diff --git a/scripts/plot_overload.py b/scripts/plot_overload.py new file mode 100644 index 00000000..558e8e37 --- /dev/null +++ b/scripts/plot_overload.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +from pathlib import Path +import json,statistics +import matplotlib +matplotlib.use('Agg') +import matplotlib.pyplot as plt +ROOT=Path(__file__).resolve().parents[1] +runs=json.loads((ROOT/'evidence/overload.json').read_text())['runs'] +fig,axes=plt.subplots(1,3,figsize=(13,4.3),layout='constrained') +settings=[1,2,4] +for label,color in [('small','#4b6fba'),('large','#719036')]: + for ax,key,title,unit in zip(axes,['throughput','p95_ready_seconds','peak_rss_bytes'],['Скорость обработки','Время до ready: p95 batch','Память процессов worker'],['готовых файлов/с','секунды','MiB']): + groups=[[r[key]/(1048576 if key=='peak_rss_bytes' else 1) for r in runs if r['image']==label and r['concurrency']==n] for n in settings] + mid=[statistics.median(g) for g in groups] + ax.errorbar(range(3),mid,yerr=[[m-min(g) for m,g in zip(mid,groups)],[max(g)-m for m,g in zip(mid,groups)]],color=color,marker='o',capsize=4,label=label) + ax.set_xticks(range(3),['1 / 1','2 / 2','4 / 4']);ax.set_xlabel('Concurrency / prefetch');ax.set_ylabel(unit);ax.set_title(title,fontsize=11);ax.set_ylim(bottom=0);ax.grid(alpha=.2) +for ax,key in zip(axes,['throughput','p95_ready_seconds','peak_rss_bytes']): + ax.set_ylim(0,max(r[key] for r in runs)/(1048576 if key=='peak_rss_bytes' else 1)*1.12) +axes[0].legend(title='Размер PNG') +fig.suptitle('Lab5: одинаковые 1 CPU / 1024 MiB worker; медианы и диапазон 3 повторов',fontsize=12) +fig.savefig(ROOT/'evidence/overload.png',dpi=170) +fig.savefig(ROOT/'evidence/overload.svg') +print('Saved evidence/overload.png and overload.svg') diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..165943e8 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +./scripts/compose.sh restart postgres redis s3 broker +./scripts/compose.sh up -d --wait --wait-timeout 60 postgres redis s3 broker +./scripts/compose.sh restart api lb dispatcher worker +./scripts/compose.sh up -d --wait --wait-timeout 60 api lb dispatcher worker diff --git a/scripts/share.py b/scripts/share.py new file mode 100644 index 00000000..e9a865e8 --- /dev/null +++ b/scripts/share.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +"""Manage a temporary HTTPS tunnel for this project's HTTP entry point.""" +import fcntl,os,re,shutil,signal,socket,subprocess,sys,time,urllib.request +from pathlib import Path +ROOT=Path(__file__).resolve().parents[1] +STATE=ROOT/'.local';STATE.mkdir(exist_ok=True) +PID=STATE/'share.pid';URL=STATE/'share.url';origin='http://127.0.0.1:18085' +def running(): + if not PID.exists():return None + try:pid=int(PID.read_text()) + except ValueError:return None + command=subprocess.run(['ps','-p',str(pid),'-o','command='],capture_output=True,text=True).stdout + return pid if 'cloudflared tunnel' in command and '--url '+origin in command else None +with (STATE/'share.lock').open('w') as lock: + fcntl.flock(lock,fcntl.LOCK_EX) + pid=running() + if '--stop' in sys.argv: + if pid: + os.kill(pid,signal.SIGTERM) + for _ in range(50): + if not running():break + time.sleep(.1) + else: + try:os.kill(pid,signal.SIGKILL) + except ProcessLookupError:pass + PID.unlink(missing_ok=True);URL.unlink(missing_ok=True) + print('Внешний доступ отключён. Localhost продолжает работать.');raise SystemExit + if pid: + print(URL.read_text().strip() if URL.exists() else 'Туннель запущен, ссылка ещё создаётся.');raise SystemExit + binary=shutil.which('cloudflared') + if not binary:raise SystemExit('Установите cloudflared: brew install cloudflared') + try:urllib.request.urlopen(origin+'/health/ready',timeout=5).close() + except Exception:raise SystemExit('Сначала запустите мессенджер: make up') + URL.unlink(missing_ok=True) + (STATE/'share.log').unlink(missing_ok=True) + with (STATE/'share.log').open('w') as log: + edge=socket.gethostbyname('region1.v2.argotunnel.com') + process=subprocess.Popen([binary,'tunnel','--no-autoupdate','--protocol','http2','--edge',edge+':7844','--url',origin],stdout=log,stderr=subprocess.STDOUT,start_new_session=True,env={**os.environ,'GODEBUG':'netdns=cgo'}) + PID.write_text(str(process.pid));PID.chmod(0o600) + deadline=time.monotonic()+120 + while time.monotonic()&2; exit 1; } +for id in $(./scripts/compose.sh ps -q api); do + instance="$(docker exec "$id" curl -fsS -D - -o /dev/null http://127.0.0.1:8080/health/live | tr -d '\r' | awk 'tolower($1) == "x-instance-id:" { print $2 }')" + if [ "$instance" = "$TARGET_INSTANCE_ID" ]; then + docker stop --time 8 "$id" >/dev/null + exit 0 + fi +done +echo "Requested API instance was not found" >&2 +exit 1 diff --git a/src/Messenger.Api/Contracts/AttachmentDto.cs b/src/Messenger.Api/Contracts/AttachmentDto.cs new file mode 100644 index 00000000..ca40a661 --- /dev/null +++ b/src/Messenger.Api/Contracts/AttachmentDto.cs @@ -0,0 +1,4 @@ +namespace Messenger.Api.Contracts; +public sealed record AttachmentDto(Guid Id, Guid ChatId, Guid OwnerId, string Filename, + string ContentType, long SizeBytes, string Sha256, string Status, object[] Variants, + string? ErrorCode, string CreatedAt, string UpdatedAt); diff --git a/src/Messenger.Api/Contracts/ChatCursor.cs b/src/Messenger.Api/Contracts/ChatCursor.cs new file mode 100644 index 00000000..f8a7fb06 --- /dev/null +++ b/src/Messenger.Api/Contracts/ChatCursor.cs @@ -0,0 +1,7 @@ +namespace Messenger.Api.Contracts; + +public sealed record ChatCursor( + string Scope, + string CreatedAt, + Guid Id +); diff --git a/src/Messenger.Api/Contracts/ChatDto.cs b/src/Messenger.Api/Contracts/ChatDto.cs new file mode 100644 index 00000000..67a757a1 --- /dev/null +++ b/src/Messenger.Api/Contracts/ChatDto.cs @@ -0,0 +1,3 @@ +namespace Messenger.Api.Contracts; + +public sealed record ChatDto(Guid Id, string Title, List MemberIds, string CreatedAt); diff --git a/src/Messenger.Api/Contracts/ChatPreviewDto.cs b/src/Messenger.Api/Contracts/ChatPreviewDto.cs new file mode 100644 index 00000000..4dded210 --- /dev/null +++ b/src/Messenger.Api/Contracts/ChatPreviewDto.cs @@ -0,0 +1,4 @@ +namespace Messenger.Api.Contracts; + +public sealed record ChatPreviewDto( + Guid Id, string Title, List MemberIds, string CreatedAt, MessageDto? LastMessage); diff --git a/src/Messenger.Api/Contracts/CreateChatRequest.cs b/src/Messenger.Api/Contracts/CreateChatRequest.cs new file mode 100644 index 00000000..cd55763b --- /dev/null +++ b/src/Messenger.Api/Contracts/CreateChatRequest.cs @@ -0,0 +1,6 @@ +namespace Messenger.Api.Contracts; + +public sealed record CreateChatRequest( + string? Title, + List? MemberIds +); diff --git a/src/Messenger.Api/Contracts/CreateMessageRequest.cs b/src/Messenger.Api/Contracts/CreateMessageRequest.cs new file mode 100644 index 00000000..2d7136a8 --- /dev/null +++ b/src/Messenger.Api/Contracts/CreateMessageRequest.cs @@ -0,0 +1,8 @@ +using System.Text.Json; + +namespace Messenger.Api.Contracts; + +public sealed record CreateMessageRequest( + string? Text, + JsonElement AttachmentIds = default +); diff --git a/src/Messenger.Api/Contracts/CreateUserRequest.cs b/src/Messenger.Api/Contracts/CreateUserRequest.cs new file mode 100644 index 00000000..cd60371d --- /dev/null +++ b/src/Messenger.Api/Contracts/CreateUserRequest.cs @@ -0,0 +1,7 @@ +namespace Messenger.Api.Contracts; + +public sealed record CreateUserRequest( + string? Username, + string? DisplayName, + string? Password +); diff --git a/src/Messenger.Api/Contracts/MessageDto.cs b/src/Messenger.Api/Contracts/MessageDto.cs new file mode 100644 index 00000000..b45c9398 --- /dev/null +++ b/src/Messenger.Api/Contracts/MessageDto.cs @@ -0,0 +1,3 @@ +namespace Messenger.Api.Contracts; + +public sealed record MessageDto(Guid Id, Guid ChatId, Guid SenderId, string Text, string CreatedAt, Guid[] AttachmentIds); diff --git a/src/Messenger.Api/Contracts/SessionDto.cs b/src/Messenger.Api/Contracts/SessionDto.cs new file mode 100644 index 00000000..8e0177f0 --- /dev/null +++ b/src/Messenger.Api/Contracts/SessionDto.cs @@ -0,0 +1,3 @@ +namespace Messenger.Api.Contracts; + +public sealed record SessionDto(string Token, string TokenType, string ExpiresAt, UserDto User); diff --git a/src/Messenger.Api/Contracts/UserDto.cs b/src/Messenger.Api/Contracts/UserDto.cs new file mode 100644 index 00000000..d3ebc3c0 --- /dev/null +++ b/src/Messenger.Api/Contracts/UserDto.cs @@ -0,0 +1,3 @@ +namespace Messenger.Api.Contracts; + +public sealed record UserDto(Guid Id, string Username, string DisplayName, string CreatedAt); diff --git a/src/Messenger.Api/Contracts/VariantDto.cs b/src/Messenger.Api/Contracts/VariantDto.cs new file mode 100644 index 00000000..89e59a6e --- /dev/null +++ b/src/Messenger.Api/Contracts/VariantDto.cs @@ -0,0 +1,2 @@ +namespace Messenger.Api.Contracts; +public sealed record VariantDto(string Name, string ContentType, long SizeBytes, int Width, int Height, string Sha256); diff --git a/src/Messenger.Api/Data/AppDatabase.cs b/src/Messenger.Api/Data/AppDatabase.cs new file mode 100644 index 00000000..ce29d472 --- /dev/null +++ b/src/Messenger.Api/Data/AppDatabase.cs @@ -0,0 +1,206 @@ +using Messenger.Api.Data.Entities; +using System.Net.Sockets; +using Npgsql; + +namespace Messenger.Api.Data; + +public sealed partial class AppDatabase(NpgsqlDataSource dataSource, IConfiguration configuration) +{ + public async Task IsReadyAsync() + { + try + { + await using var command = dataSource.CreateCommand("SELECT 1"); + return (int)(await command.ExecuteScalarAsync())! == 1; + } + catch (Exception exception) when (exception is NpgsqlException or SocketException or TimeoutException) + { + return false; + } + } + + public async Task FindUserByUsernameAsync(string username) + { + await using var command = dataSource.CreateCommand( + "SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = @username"); + command.Parameters.AddWithValue("username", username); + await using var reader = await command.ExecuteReaderAsync(); + return await reader.ReadAsync() ? ReadUser(reader) : null; + } + + public async Task FindUserByIdAsync(Guid id) + { + await using var command = dataSource.CreateCommand( + "SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = @id"); + command.Parameters.AddWithValue("id", id); + await using var reader = await command.ExecuteReaderAsync(); + return await reader.ReadAsync() ? ReadUser(reader) : null; + } + + public async Task> ListChatUsersAsync(Guid chatId) + { + await using var command = dataSource.CreateCommand(""" + SELECT u.id, u.username, u.display_name, u.password_hash, u.created_at + FROM users u JOIN chat_members m ON m.user_id = u.id + WHERE m.chat_id = @chat_id ORDER BY u.display_name, u.id + """); + command.Parameters.AddWithValue("chat_id", chatId); + await using var reader = await command.ExecuteReaderAsync(); + var result = new List(); + while (await reader.ReadAsync()) result.Add(ReadUser(reader)); + return result; + } + + public async Task CreateUserAsync(string username, string displayName, string passwordHash) + { + var user = new UserEntity(Guid.NewGuid(), username, displayName, passwordHash, UtcClock.Now()); + await using var command = dataSource.CreateCommand( + "INSERT INTO users(id, username, display_name, password_hash, created_at) VALUES (@id, @username, @display_name, @password_hash, @created_at)"); + command.Parameters.AddWithValue("id", user.Id); + command.Parameters.AddWithValue("username", user.Username); + command.Parameters.AddWithValue("display_name", user.DisplayName); + command.Parameters.AddWithValue("password_hash", user.PasswordHash); + command.Parameters.AddWithValue("created_at", user.CreatedAt); + await command.ExecuteNonQueryAsync(); + return user; + } + + public async Task CreateSessionAsync(byte[] tokenHash, Guid userId, DateTime expiresAt) + { + await using var command = dataSource.CreateCommand( + "INSERT INTO sessions(token_hash, user_id, expires_at, created_at) VALUES (@token_hash, @user_id, @expires_at, @created_at)"); + command.Parameters.AddWithValue("token_hash", tokenHash); + command.Parameters.AddWithValue("user_id", userId); + command.Parameters.AddWithValue("expires_at", expiresAt); + command.Parameters.AddWithValue("created_at", UtcClock.Now()); + await command.ExecuteNonQueryAsync(); + } + + public async Task FindActiveSessionAsync(byte[] tokenHash) + { + await using var command = dataSource.CreateCommand(""" + SELECT user_id, expires_at FROM sessions + WHERE token_hash = @token_hash AND revoked_at IS NULL AND expires_at > now() + """); + command.Parameters.AddWithValue("token_hash", tokenHash); + await using var reader = await command.ExecuteReaderAsync(); + return await reader.ReadAsync() ? new SessionEntity(reader.GetGuid(0), reader.GetDateTime(1)) : null; + } + + public async Task RevokeSessionAsync(byte[] tokenHash) + { + await using var command = dataSource.CreateCommand(""" + UPDATE sessions SET revoked_at = now() + WHERE token_hash = @token_hash AND revoked_at IS NULL AND expires_at > now() + """); + command.Parameters.AddWithValue("token_hash", tokenHash); + return await command.ExecuteNonQueryAsync() == 1; + } + + public async Task CreateChatAsync(string title, List memberIds) + { + await using var connection = await dataSource.OpenConnectionAsync(); + await using var transaction = await connection.BeginTransactionAsync(); + + await using (var membersCommand = new NpgsqlCommand( + "SELECT count(*) FROM users WHERE id = ANY(@member_ids)", connection, transaction)) + { + membersCommand.Parameters.AddWithValue("member_ids", memberIds.ToArray()); + if ((long)(await membersCommand.ExecuteScalarAsync())! != memberIds.Count) + { + await transaction.RollbackAsync(); + return null; + } + } + + var chat = new ChatEntity(Guid.NewGuid(), title, memberIds, UtcClock.Now()); + await using (var chatCommand = new NpgsqlCommand( + "INSERT INTO chats(id, title, created_at) VALUES (@id, @title, @created_at)", connection, transaction)) + { + chatCommand.Parameters.AddWithValue("id", chat.Id); + chatCommand.Parameters.AddWithValue("title", chat.Title); + chatCommand.Parameters.AddWithValue("created_at", chat.CreatedAt); + await chatCommand.ExecuteNonQueryAsync(); + } + + await using (var membershipCommand = new NpgsqlCommand( + "INSERT INTO chat_members(chat_id, user_id) SELECT @chat_id, unnest(CAST(@member_ids AS uuid[]))", connection, transaction)) + { + membershipCommand.Parameters.AddWithValue("chat_id", chat.Id); + membershipCommand.Parameters.AddWithValue("member_ids", memberIds.ToArray()); + await membershipCommand.ExecuteNonQueryAsync(); + } + + await transaction.CommitAsync(); + return chat; + } + + public async Task FindChatForMemberAsync(Guid chatId, Guid userId) + { + await using var command = dataSource.CreateCommand(""" + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id) + FROM chats c + WHERE c.id = @chat_id AND EXISTS ( + SELECT 1 FROM chat_members m WHERE m.chat_id = c.id AND m.user_id = @user_id) + """); + command.Parameters.AddWithValue("chat_id", chatId); + command.Parameters.AddWithValue("user_id", userId); + await using var reader = await command.ExecuteReaderAsync(); + return await reader.ReadAsync() ? ReadChat(reader) : null; + } + + public async Task> ListChatsAsync(Guid userId, DateTime? cursorTime, Guid? cursorId, int count) + { + var sql = """ + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT m2.user_id FROM chat_members m2 WHERE m2.chat_id = c.id ORDER BY m2.user_id) + FROM chats c JOIN chat_members m ON m.chat_id = c.id + WHERE m.user_id = @user_id + """; + if (cursorTime is not null) + sql += " AND (c.created_at, c.id) > (@cursor_time, @cursor_id)"; + sql += " ORDER BY c.created_at, c.id LIMIT @count"; + + await using var command = dataSource.CreateCommand(sql); + command.Parameters.AddWithValue("user_id", userId); + if (cursorTime is not null) + { + command.Parameters.AddWithValue("cursor_time", cursorTime.Value); + command.Parameters.AddWithValue("cursor_id", cursorId!.Value); + } + command.Parameters.AddWithValue("count", count); + await using var reader = await command.ExecuteReaderAsync(); + var result = new List(); + while (await reader.ReadAsync()) result.Add(ReadChat(reader)); + return result; + } + + public async Task> ListMessagesAsync(Guid chatId, DateTime? cursorTime, Guid? cursorId, int count, bool newestFirst = false) + { + var sql = "SELECT id, chat_id, sender_id, text, created_at, attachment_ids FROM messages WHERE chat_id = @chat_id"; + if (cursorTime is not null) + sql += newestFirst ? " AND (created_at, id) < (@cursor_time, @cursor_id)" : " AND (created_at, id) > (@cursor_time, @cursor_id)"; + sql += newestFirst ? " ORDER BY created_at DESC, id DESC LIMIT @count" : " ORDER BY created_at, id LIMIT @count"; + + await using var command = dataSource.CreateCommand(sql); + command.Parameters.AddWithValue("chat_id", chatId); + if (cursorTime is not null) + { + command.Parameters.AddWithValue("cursor_time", cursorTime.Value); + command.Parameters.AddWithValue("cursor_id", cursorId!.Value); + } + command.Parameters.AddWithValue("count", count); + await using var reader = await command.ExecuteReaderAsync(); + var result = new List(); + while (await reader.ReadAsync()) result.Add(new MessageEntity( + reader.GetGuid(0), reader.GetGuid(1), reader.GetGuid(2), reader.GetString(3), reader.GetDateTime(4), reader.GetFieldValue(5))); + return result; + } + + private static UserEntity ReadUser(NpgsqlDataReader reader) => new( + reader.GetGuid(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetDateTime(4)); + + private static ChatEntity ReadChat(NpgsqlDataReader reader) => new( + reader.GetGuid(0), reader.GetString(1), reader.GetFieldValue(3).ToList(), reader.GetDateTime(2)); +} diff --git a/src/Messenger.Api/Data/Attachments.cs b/src/Messenger.Api/Data/Attachments.cs new file mode 100644 index 00000000..16aab73b --- /dev/null +++ b/src/Messenger.Api/Data/Attachments.cs @@ -0,0 +1,23 @@ +using Messenger.Api.Contracts; +using Npgsql; +namespace Messenger.Api.Data; +public sealed partial class AppDatabase +{ + public const long AttachmentGate = 48260104; + public async Task<(AttachmentDto Dto, string Key)?> FindAttachmentAsync(Guid id, Guid actor) + { + await using var cmd = dataSource.CreateCommand(""" + SELECT a.id,a.chat_id,a.owner_id,a.filename,a.content_type,a.size_bytes,a.sha256, + a.status,a.created_at,a.updated_at,a.object_key,a.variants::text,a.error_code + FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id + WHERE a.id=@id AND m.user_id=@actor + """); + cmd.Parameters.AddWithValue("id",id); cmd.Parameters.AddWithValue("actor",actor); + await using var r=await cmd.ExecuteReaderAsync(); + return await r.ReadAsync() ? (ReadAttachment(r),r.GetString(10)) : null; + } + public static AttachmentDto ReadAttachment(NpgsqlDataReader r) => new(r.GetGuid(0),r.GetGuid(1), + r.GetGuid(2),r.GetString(3),r.GetString(4),r.GetInt64(5),r.GetString(6),r.GetString(7), + System.Text.Json.JsonSerializer.Deserialize(r.GetString(11), Pipeline.JobJson.Options) ?? [], + r.IsDBNull(12)?null:r.GetString(12),r.GetDateTime(8).ToString("O"),r.GetDateTime(9).ToString("O")); +} diff --git a/src/Messenger.Api/Data/ChatPreviews.cs b/src/Messenger.Api/Data/ChatPreviews.cs new file mode 100644 index 00000000..40d63209 --- /dev/null +++ b/src/Messenger.Api/Data/ChatPreviews.cs @@ -0,0 +1,51 @@ +using Messenger.Api.Contracts; +using Npgsql; + +namespace Messenger.Api.Data; + +public sealed partial class AppDatabase +{ + public async Task> ListChatPreviewsAsync(Guid userId, DateTime? cursorTime, Guid? cursorId, int count) + { + var query = """ + WITH page AS ( + SELECT c.id, c.title, c.created_at, + ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id) AS member_ids + FROM chats c JOIN chat_members membership ON membership.chat_id = c.id + WHERE membership.user_id = @user_id + """; + if (cursorTime is not null) query += " AND (c.created_at, c.id) > (@cursor_time, @cursor_id)"; + query += """ + ORDER BY c.created_at, c.id LIMIT @count + ) + SELECT p.id, p.title, p.created_at, p.member_ids, + latest.id, latest.sender_id, latest.text, latest.created_at, latest.attachment_ids + FROM page p + LEFT JOIN LATERAL ( + SELECT id, sender_id, text, created_at, attachment_ids + FROM messages WHERE chat_id = p.id + ORDER BY created_at DESC, id DESC LIMIT 1 + ) latest ON true + ORDER BY p.created_at, p.id + """; + await using var command = dataSource.CreateCommand(query); + command.Parameters.AddWithValue("user_id", userId); + command.Parameters.AddWithValue("count", count); + if (cursorTime is not null) + { + command.Parameters.AddWithValue("cursor_time", cursorTime.Value); + command.Parameters.AddWithValue("cursor_id", cursorId!.Value); + } + await using var reader = await command.ExecuteReaderAsync(); + var result = new List(); + while (await reader.ReadAsync()) + { + var id = reader.GetGuid(0); + var message = reader.IsDBNull(4) ? null : new MessageDto(reader.GetGuid(4), id, + reader.GetGuid(5), reader.GetString(6), reader.GetDateTime(7).ToString("O"), reader.GetFieldValue(8)); + result.Add(new ChatPreviewDto(id, reader.GetString(1), reader.GetFieldValue(3).ToList(), + reader.GetDateTime(2).ToString("O"), message)); + } + return result; + } +} diff --git a/src/Messenger.Api/Data/DatabaseMigrator.cs b/src/Messenger.Api/Data/DatabaseMigrator.cs new file mode 100644 index 00000000..91cb8068 --- /dev/null +++ b/src/Messenger.Api/Data/DatabaseMigrator.cs @@ -0,0 +1,40 @@ +using Npgsql; + +namespace Messenger.Api.Data; + +public static class DatabaseMigrator +{ + public static async Task ApplyAsync(NpgsqlDataSource dataSource) + { + await using var connection = await dataSource.OpenConnectionAsync(); + await using var transaction = await connection.BeginTransactionAsync(); + await using (var command = new NpgsqlCommand("SELECT pg_advisory_xact_lock(202602)", connection, transaction)) + await command.ExecuteNonQueryAsync(); + await using (var command = new NpgsqlCommand( + "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY, applied_at timestamptz NOT NULL)", + connection, transaction)) + await command.ExecuteNonQueryAsync(); + + var assembly = typeof(DatabaseMigrator).Assembly; + var migrations = assembly.GetManifestResourceNames() + .Where(name => name.Contains(".Data.Migrations.", StringComparison.Ordinal) && name.EndsWith(".sql", StringComparison.Ordinal)) + .Order(StringComparer.Ordinal); + foreach (var name in migrations) + { + var version = int.Parse(name.Split('.')[^2].Split('_')[0]); + await using var check = new NpgsqlCommand("SELECT EXISTS (SELECT 1 FROM schema_migrations WHERE version = @version)", connection, transaction); + check.Parameters.AddWithValue("version", version); + if ((bool)(await check.ExecuteScalarAsync())!) continue; + + await using var stream = assembly.GetManifestResourceStream(name)!; + using var reader = new StreamReader(stream); + await using (var migration = new NpgsqlCommand(await reader.ReadToEndAsync(), connection, transaction)) + await migration.ExecuteNonQueryAsync(); + await using var record = new NpgsqlCommand( + "INSERT INTO schema_migrations(version, applied_at) VALUES (@version, now())", connection, transaction); + record.Parameters.AddWithValue("version", version); + await record.ExecuteNonQueryAsync(); + } + await transaction.CommitAsync(); + } +} diff --git a/src/Messenger.Api/Data/Entities/ChatEntity.cs b/src/Messenger.Api/Data/Entities/ChatEntity.cs new file mode 100644 index 00000000..9daea081 --- /dev/null +++ b/src/Messenger.Api/Data/Entities/ChatEntity.cs @@ -0,0 +1,6 @@ +namespace Messenger.Api.Data.Entities; + +public sealed record ChatEntity(Guid Id, string Title, List MemberIds, DateTime CreatedAt) +{ + public Contracts.ChatDto ToDto() => new(Id, Title, MemberIds, CreatedAt.ToString("O")); +} diff --git a/src/Messenger.Api/Data/Entities/MessageEntity.cs b/src/Messenger.Api/Data/Entities/MessageEntity.cs new file mode 100644 index 00000000..cd4614a8 --- /dev/null +++ b/src/Messenger.Api/Data/Entities/MessageEntity.cs @@ -0,0 +1,6 @@ +namespace Messenger.Api.Data.Entities; + +public sealed record MessageEntity(Guid Id, Guid ChatId, Guid SenderId, string Text, DateTime CreatedAt, Guid[]? AttachmentIds = null) +{ + public Contracts.MessageDto ToDto() => new(Id, ChatId, SenderId, Text, CreatedAt.ToString("O"), AttachmentIds ?? []); +} diff --git a/src/Messenger.Api/Data/Entities/SessionEntity.cs b/src/Messenger.Api/Data/Entities/SessionEntity.cs new file mode 100644 index 00000000..ddb29de1 --- /dev/null +++ b/src/Messenger.Api/Data/Entities/SessionEntity.cs @@ -0,0 +1,3 @@ +namespace Messenger.Api.Data.Entities; + +public sealed record SessionEntity(Guid UserId, DateTime ExpiresAt); diff --git a/src/Messenger.Api/Data/Entities/UserEntity.cs b/src/Messenger.Api/Data/Entities/UserEntity.cs new file mode 100644 index 00000000..9389ce4c --- /dev/null +++ b/src/Messenger.Api/Data/Entities/UserEntity.cs @@ -0,0 +1,6 @@ +namespace Messenger.Api.Data.Entities; + +public sealed record UserEntity(Guid Id, string Username, string DisplayName, string PasswordHash, DateTime CreatedAt) +{ + public Contracts.UserDto ToDto() => new(Id, Username, DisplayName, CreatedAt.ToString("O")); +} diff --git a/src/Messenger.Api/Data/MessageRequests.cs b/src/Messenger.Api/Data/MessageRequests.cs new file mode 100644 index 00000000..a9cf236e --- /dev/null +++ b/src/Messenger.Api/Data/MessageRequests.cs @@ -0,0 +1,91 @@ +using System.Security.Cryptography; +using System.Text; +using Messenger.Api.Data.Entities; +using Npgsql; + +namespace Messenger.Api.Data; + +public sealed partial class AppDatabase +{ + private bool BatchCommands => !string.Equals(configuration["MESSAGE_BATCHING"], "false", StringComparison.OrdinalIgnoreCase); + + public async Task CreateMessageOnceAsync(Guid chatId, Guid senderId, string text, string key, Guid[] attachmentIds) + { + var hash = SHA256.HashData(System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(new { text, attachment_ids = attachmentIds })); + await using var connection = await dataSource.OpenConnectionAsync(); + await using var transaction = await connection.BeginTransactionAsync(); + + await using (var gate = new NpgsqlCommand($"SELECT pg_advisory_xact_lock_shared({AttachmentGate})",connection,transaction)) + await gate.ExecuteNonQueryAsync(); + if (attachmentIds.Length>0) + { + await using var attachments = new NpgsqlCommand("SELECT id,status FROM attachments WHERE chat_id=@chat AND id=ANY(@ids)",connection,transaction); + attachments.Parameters.AddWithValue("chat",chatId); attachments.Parameters.AddWithValue("ids",attachmentIds); + await using var found = await attachments.ExecuteReaderAsync(); + var count=0; var ready=true; + while(await found.ReadAsync()) { count++; ready &= found.GetString(1)!="failed"; } + if(count!=attachmentIds.Length) throw new Services.AttachmentException(404,"not_found","Вложение не найдено"); + if(!ready) throw new Services.AttachmentException(409,"conflict","Вложение недоступно"); + } + // A transaction-scoped lock coordinates the same request across all API processes. + if (!BatchCommands) + await using (var gate = new NpgsqlCommand("SELECT pg_advisory_xact_lock(hashtextextended(@scope, 0))", connection, transaction)) + { + gate.Parameters.AddWithValue("scope", $"{senderId}:POST:/api/v1/chats/{chatId}/messages:{key}"); + await gate.ExecuteNonQueryAsync(); + } + await using (var previous = new NpgsqlCommand((BatchCommands ? "SELECT pg_advisory_xact_lock(hashtextextended(@scope, 0));" : "") + """ + SELECT r.request_hash, m.id, m.chat_id, m.sender_id, m.text, m.created_at, m.attachment_ids + FROM message_requests r JOIN messages m ON m.id = r.message_id + WHERE r.user_id = @user_id AND r.chat_id = @chat_id AND r.request_key = @key + AND r.expires_at > clock_timestamp() + """, connection, transaction)) + { + previous.Parameters.AddWithValue("scope", $"{senderId}:POST:/api/v1/chats/{chatId}/messages:{key}"); + previous.Parameters.AddWithValue("user_id", senderId); + previous.Parameters.AddWithValue("chat_id", chatId); + previous.Parameters.AddWithValue("key", key); + await using var reader = await previous.ExecuteReaderAsync(); + if (BatchCommands) await reader.NextResultAsync(); + if (await reader.ReadAsync()) + { + var same = reader.GetFieldValue(0).AsSpan().SequenceEqual(hash); + var result = same ? new MessageEntity(reader.GetGuid(1), reader.GetGuid(2), reader.GetGuid(3), reader.GetString(4), reader.GetDateTime(5), reader.GetFieldValue(6)) : null; + await reader.DisposeAsync(); + await transaction.CommitAsync(); + return result; + } + } + if (!BatchCommands) + await using (var cleanup = new NpgsqlCommand(""" + DELETE FROM message_requests + WHERE user_id = @user_id AND chat_id = @chat_id AND request_key = @key + """, connection, transaction)) + { + cleanup.Parameters.AddWithValue("user_id", senderId); + cleanup.Parameters.AddWithValue("chat_id", chatId); + cleanup.Parameters.AddWithValue("key", key); + await cleanup.ExecuteNonQueryAsync(); + } + var message = new MessageEntity(Guid.NewGuid(), chatId, senderId, text, UtcClock.Now(), attachmentIds); + await using (var insert = new NpgsqlCommand((BatchCommands ? "DELETE FROM message_requests WHERE user_id = @sender_id AND chat_id = @chat_id AND request_key = @key;" : "") + """ + INSERT INTO messages(id, chat_id, sender_id, text, created_at, attachment_ids) + VALUES (@id, @chat_id, @sender_id, @text, @created_at, @attachments); + INSERT INTO message_requests(user_id, chat_id, request_key, request_hash, message_id, expires_at) + VALUES (@sender_id, @chat_id, @key, @hash, @id, clock_timestamp() + interval '24 hours'); + """, connection, transaction)) + { + insert.Parameters.AddWithValue("attachments", attachmentIds); + insert.Parameters.AddWithValue("id", message.Id); + insert.Parameters.AddWithValue("chat_id", chatId); + insert.Parameters.AddWithValue("sender_id", senderId); + insert.Parameters.AddWithValue("text", text); + insert.Parameters.AddWithValue("created_at", message.CreatedAt); + insert.Parameters.AddWithValue("key", key); + insert.Parameters.AddWithValue("hash", hash); + await insert.ExecuteNonQueryAsync(); + } + await transaction.CommitAsync(); + return message; + } +} diff --git a/src/Messenger.Api/Data/Migrations/001_initial.sql b/src/Messenger.Api/Data/Migrations/001_initial.sql new file mode 100644 index 00000000..2cf544fc --- /dev/null +++ b/src/Messenger.Api/Data/Migrations/001_initial.sql @@ -0,0 +1,43 @@ +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL, + password_hash text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats(id), + user_id uuid NOT NULL REFERENCES users(id), + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id); +CREATE INDEX chats_created_id_idx ON chats (created_at, id); + +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + sender_id uuid NOT NULL REFERENCES users(id), + text text NOT NULL, + created_at timestamptz NOT NULL +); + +CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id); + +CREATE TABLE sessions ( + token_hash bytea PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users(id), + expires_at timestamptz NOT NULL, + revoked_at timestamptz, + created_at timestamptz NOT NULL +); + +CREATE INDEX sessions_user_id_idx ON sessions (user_id); +CREATE INDEX sessions_expires_at_idx ON sessions (expires_at); diff --git a/src/Messenger.Api/Data/Migrations/002_idempotency.sql b/src/Messenger.Api/Data/Migrations/002_idempotency.sql new file mode 100644 index 00000000..9ec17406 --- /dev/null +++ b/src/Messenger.Api/Data/Migrations/002_idempotency.sql @@ -0,0 +1,11 @@ +CREATE TABLE message_requests ( + user_id uuid NOT NULL REFERENCES users(id), + chat_id uuid NOT NULL REFERENCES chats(id), + request_key text NOT NULL, + request_hash bytea NOT NULL, + message_id uuid NOT NULL REFERENCES messages(id), + expires_at timestamptz NOT NULL, + PRIMARY KEY (user_id, chat_id, request_key) +); + +CREATE INDEX message_requests_expires_idx ON message_requests (expires_at); diff --git a/src/Messenger.Api/Data/Migrations/003_reactions.sql b/src/Messenger.Api/Data/Migrations/003_reactions.sql new file mode 100644 index 00000000..c4b1e44b --- /dev/null +++ b/src/Messenger.Api/Data/Migrations/003_reactions.sql @@ -0,0 +1,6 @@ +CREATE TABLE message_reactions ( + message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, + reaction text NOT NULL CHECK (reaction IN ('like', 'love', 'laugh', 'mushroom')), + PRIMARY KEY (message_id, user_id, reaction) +); diff --git a/src/Messenger.Api/Data/Migrations/004_attachments.sql b/src/Messenger.Api/Data/Migrations/004_attachments.sql new file mode 100644 index 00000000..ec837482 --- /dev/null +++ b/src/Messenger.Api/Data/Migrations/004_attachments.sql @@ -0,0 +1,11 @@ +CREATE TABLE attachments ( + id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), + owner_id uuid NOT NULL REFERENCES users(id), filename text NOT NULL, + content_type text NOT NULL, size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 text NOT NULL, object_key text NOT NULL UNIQUE, + status text NOT NULL CHECK (status IN ('ready','missing')), + created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL +); +CREATE INDEX attachments_created_idx ON attachments(created_at, id); +ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}'; +CREATE INDEX messages_attachments_idx ON messages USING gin(attachment_ids); diff --git a/src/Messenger.Api/Data/Migrations/005_pipeline.sql b/src/Messenger.Api/Data/Migrations/005_pipeline.sql new file mode 100644 index 00000000..e4bb8e2d --- /dev/null +++ b/src/Messenger.Api/Data/Migrations/005_pipeline.sql @@ -0,0 +1,25 @@ +ALTER TABLE attachments DROP CONSTRAINT attachments_status_check; +UPDATE attachments SET status='failed' WHERE status='missing'; +ALTER TABLE attachments ADD CONSTRAINT attachments_status_check CHECK(status IN ('queued','processing','ready','failed')); +ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]'; +ALTER TABLE attachments ADD COLUMN error_code text; +UPDATE attachments SET error_code='original_missing' WHERE status='failed'; +CREATE TABLE attachment_jobs ( + id uuid PRIMARY KEY, attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id), + pipeline_version int NOT NULL DEFAULT 1, staging_key text NOT NULL UNIQUE, + state text NOT NULL CHECK(state IN ('queued','processing','ready','failed')), + attempts int NOT NULL DEFAULT 0, next_at timestamptz NOT NULL DEFAULT now(), + lease_token uuid, lease_until timestamptz, error_code text, + created_at timestamptz NOT NULL DEFAULT now(), completed_at timestamptz, + staging_deleted_at timestamptz, replay_count int NOT NULL DEFAULT 0 +); +CREATE INDEX attachment_jobs_active_idx ON attachment_jobs(state,next_at); +CREATE TABLE attachment_outbox ( + job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id), + available_at timestamptz NOT NULL DEFAULT now(), published_at timestamptz +); +CREATE INDEX attachment_outbox_pending_idx ON attachment_outbox(available_at) WHERE published_at IS NULL; +CREATE TABLE attachment_job_events ( + id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES attachment_jobs(id), + event text NOT NULL, attempt int NOT NULL, created_at timestamptz NOT NULL DEFAULT now() +); diff --git a/src/Messenger.Api/Data/Migrations/006_process_presence.sql b/src/Messenger.Api/Data/Migrations/006_process_presence.sql new file mode 100644 index 00000000..7eca2743 --- /dev/null +++ b/src/Messenger.Api/Data/Migrations/006_process_presence.sql @@ -0,0 +1 @@ +CREATE TABLE pipeline_presence (role text PRIMARY KEY, seen_at timestamptz NOT NULL); diff --git a/src/Messenger.Api/Data/Reactions.cs b/src/Messenger.Api/Data/Reactions.cs new file mode 100644 index 00000000..7d40ffce --- /dev/null +++ b/src/Messenger.Api/Data/Reactions.cs @@ -0,0 +1,48 @@ +using Npgsql; + +namespace Messenger.Api.Data; + +public sealed record ReactionDto(Guid MessageId, string Reaction, int Count, bool Mine); + +public sealed partial class AppDatabase +{ + public async Task SetReactionAsync(Guid chatId, Guid messageId, Guid userId, string reaction, bool present) + { + await using var command = dataSource.CreateCommand(present ? """ + WITH target AS (SELECT id FROM messages WHERE id = @message_id AND chat_id = @chat_id), + written AS ( + INSERT INTO message_reactions(message_id, user_id, reaction) + SELECT id, @user_id, @reaction FROM target ON CONFLICT DO NOTHING + ) SELECT EXISTS(SELECT 1 FROM target) + """ : """ + WITH target AS (SELECT id FROM messages WHERE id = @message_id AND chat_id = @chat_id), + removed AS ( + DELETE FROM message_reactions WHERE message_id IN (SELECT id FROM target) + AND user_id = @user_id AND reaction = @reaction + ) SELECT EXISTS(SELECT 1 FROM target) + """); + command.Parameters.AddWithValue("chat_id", chatId); + command.Parameters.AddWithValue("message_id", messageId); + command.Parameters.AddWithValue("user_id", userId); + command.Parameters.AddWithValue("reaction", reaction); + return (bool)(await command.ExecuteScalarAsync())!; + } + + public async Task> ListReactionsAsync(Guid chatId, Guid[] messageIds, Guid userId) + { + await using var command = dataSource.CreateCommand(""" + SELECT r.message_id, r.reaction, count(*)::integer, bool_or(r.user_id = @user_id) + FROM message_reactions r JOIN messages m ON m.id = r.message_id + WHERE m.chat_id = @chat_id AND m.id = ANY(@message_ids) + GROUP BY r.message_id, r.reaction ORDER BY r.message_id, r.reaction + """); + command.Parameters.AddWithValue("chat_id", chatId); + command.Parameters.AddWithValue("message_ids", messageIds); + command.Parameters.AddWithValue("user_id", userId); + await using var reader = await command.ExecuteReaderAsync(); + var items = new List(); + while (await reader.ReadAsync()) + items.Add(new(reader.GetGuid(0), reader.GetString(1), reader.GetInt32(2), reader.GetBoolean(3))); + return items; + } +} diff --git a/src/Messenger.Api/Data/UtcClock.cs b/src/Messenger.Api/Data/UtcClock.cs new file mode 100644 index 00000000..30f9fbf7 --- /dev/null +++ b/src/Messenger.Api/Data/UtcClock.cs @@ -0,0 +1,11 @@ +namespace Messenger.Api.Data; + +public static class UtcClock +{ + // PostgreSQL timestamptz stores microseconds; keep the response identical to the saved value. + public static DateTime Now() + { + var ticks = DateTime.UtcNow.Ticks; + return new DateTime(ticks - ticks % 10, DateTimeKind.Utc); + } +} diff --git a/src/Messenger.Api/Endpoints/AttachmentEndpoints.cs b/src/Messenger.Api/Endpoints/AttachmentEndpoints.cs new file mode 100644 index 00000000..2d3b1a4c --- /dev/null +++ b/src/Messenger.Api/Endpoints/AttachmentEndpoints.cs @@ -0,0 +1,79 @@ +using Messenger.Api.Data; +using Messenger.Api.Infrastructure; +using Messenger.Api.Services; +using Amazon.S3; +using Amazon.S3.Model; +using static Messenger.Api.Services.ApiSupport; +namespace Messenger.Api.Endpoints; +public static class AttachmentEndpoints +{ + public static IEndpointRouteBuilder MapAttachmentEndpoints(this IEndpointRouteBuilder app) + { + app.MapPost("/api/v1/chats/{chat_id}/attachments",async(string chat_id,HttpContext ctx, + SessionService sessions,ChatService chats,AttachmentService files)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx); if(actor is null) return Unauthorized(ctx); + if(!Guid.TryParse(chat_id,out var chat)) return ApiError(ctx,400,"invalid_request","Некорректный chat_id"); + if(await chats.FindForMemberAsync(chat,actor.Id) is null) return ApiError(ctx,404,"not_found","Чат не найден"); + var names=ctx.Request.Query["filename"]; var name=names.ToString(); + if(names.Count!=1 || string.IsNullOrWhiteSpace(name) || name.EnumerateRunes().Count()>255 || name.Any(c=>char.IsControl(c)||c is '/' or '\\')) + return ApiError(ctx,400,"invalid_request","Некорректное имя файла"); + if(ctx.Request.ContentType?.Split(';')[0].Trim() is not "image/png" and not "image/jpeg") + return ApiError(ctx,415,"unsupported_media_type","Требуется PNG или JPEG"); + return Results.Json(await files.UploadAsync(ctx.Request,chat,actor.Id,name),statusCode:202); + }); + app.MapGet("/api/v1/attachments/{attachment_id}",async(string attachment_id,HttpContext ctx, + SessionService sessions,AppDatabase db)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx); if(actor is null) return Unauthorized(ctx); + if(!Guid.TryParse(attachment_id,out var id)) return ApiError(ctx,400,"invalid_request","Некорректный ID"); + var file=await db.FindAttachmentAsync(id,actor.Id); + if(file is null) return ApiError(ctx,404,"not_found","Вложение не найдено"); + return Results.Ok(file.Value.Dto); + }); + app.MapGet("/api/v1/attachments/{attachment_id}/processing",async(string attachment_id,HttpContext ctx, + SessionService sessions,AppDatabase db,Npgsql.NpgsqlDataSource source)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx); if(actor is null) return Unauthorized(ctx); + if(!Guid.TryParse(attachment_id,out var id)) return ApiError(ctx,400,"invalid_request","Некорректный ID"); + if(await db.FindAttachmentAsync(id,actor.Id) is null) return ApiError(ctx,404,"not_found","Вложение не найдено"); + await using var cmd=source.CreateCommand(""" + SELECT j.state,j.attempts,j.error_code,j.created_at,j.completed_at, + e.event,e.attempt,e.created_at + FROM attachment_jobs j LEFT JOIN attachment_job_events e ON e.job_id=j.id + WHERE j.attachment_id=@id ORDER BY e.id + """); + cmd.Parameters.AddWithValue("id",id); + await using var reader=await cmd.ExecuteReaderAsync(ctx.RequestAborted); + string? state=null,error=null;int attempts=0;DateTime? created=null,completed=null; + var events=new List(); + while(await reader.ReadAsync(ctx.RequestAborted)) { + state=reader.GetString(0);attempts=reader.GetInt32(1); + error=reader.IsDBNull(2)?null:reader.GetString(2); + created=reader.GetDateTime(3);completed=reader.IsDBNull(4)?null:reader.GetDateTime(4); + if(!reader.IsDBNull(5)) events.Add(new {name=reader.GetString(5),attempt=reader.GetInt32(6),at=reader.GetDateTime(7)}); + } + ctx.Response.Headers.CacheControl="no-store"; + return Results.Ok(new {state,attempts,error_code=error,created_at=created,completed_at=completed,events}); + }); + app.MapGet("/api/v1/attachments/{attachment_id}/content",async(string attachment_id,HttpContext ctx, + SessionService sessions,AppDatabase db,S3Storage storage)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx); if(actor is null) return Unauthorized(ctx); + if(!Guid.TryParse(attachment_id,out var id)) return ApiError(ctx,400,"invalid_request","Некорректный ID"); + var file=await db.FindAttachmentAsync(id,actor.Id); + if(file is null) return ApiError(ctx,404,"not_found","Вложение не найдено"); + if(file.Value.Dto.Status!="ready") return ApiError(ctx,409,"conflict","Оригинал недоступен"); + var variant=ctx.Request.Query.TryGetValue("variant",out var value)?value.ToString():"original"; + var selected=file.Value.Dto.Variants.OfType().FirstOrDefault(v=>v.Name==variant); + if(variant!="original" && selected is null) return ApiError(ctx,404,"not_found","Вариант не найден"); + var key=variant=="original"?file.Value.Key:$"variants/{id:N}/v1/{variant}"; + var mime=selected?.ContentType ?? file.Value.Dto.ContentType; + var filename=variant=="original"?file.Value.Dto.Filename:variant+(mime=="image/webp"?".webp":".jpg"); + try { + var original=await storage.Client.GetObjectAsync(storage.Bucket,key,ctx.RequestAborted); + return Results.Stream(async stream=> {using(original) {await original.ResponseStream.CopyToAsync(stream,ctx.RequestAborted);}}, + mime,filename); + } catch(AmazonS3Exception ex) when(ex.StatusCode==System.Net.HttpStatusCode.NotFound) { + return ApiError(ctx,503,"unavailable","Оригинал временно недоступен"); + } + }); + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/AuthEndpoints.cs b/src/Messenger.Api/Endpoints/AuthEndpoints.cs new file mode 100644 index 00000000..04de49af --- /dev/null +++ b/src/Messenger.Api/Endpoints/AuthEndpoints.cs @@ -0,0 +1,72 @@ +using System.Text; +using System.Text.RegularExpressions; +using Messenger.Api.Services; +using static Messenger.Api.Services.ApiSupport; + +namespace Messenger.Api.Endpoints; + +public static class AuthEndpoints +{ + private static readonly UTF8Encoding StrictUtf8 = new(false, true); + + public static IEndpointRouteBuilder MapAuthEndpoints(this IEndpointRouteBuilder app) + { + app.MapPost("/api/v1/auth/sessions", async (HttpContext context, SessionService sessions) => + { + if (!context.Request.Headers.TryGetValue("Authorization", out var header)) + return Unauthorized(context, basic: true); + + var authorization = header.ToString(); + if (!authorization.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase)) + return Unauthorized(context, basic: true); + + string credentials; + try + { + credentials = StrictUtf8.GetString(Convert.FromBase64String(authorization[6..])); + } + catch (Exception exception) when (exception is FormatException or DecoderFallbackException) + { + return Unauthorized(context, basic: true); + } + + var delimiter = credentials.IndexOf(':'); + if (delimiter < 0) return Unauthorized(context, basic: true); + var username = credentials[..delimiter]; + var password = credentials[(delimiter + 1)..]; + if (!Regex.IsMatch(username, "\\A[a-z0-9_]{3,32}\\z")) + return Unauthorized(context, basic: true); + + if (context.Request.ContentLength > 0) + return ApiError(context, 400, "invalid_request", "Вход принимает Basic без тела запроса"); + + if (context.Request.ContentLength is null && context.Request.Headers.ContainsKey("Transfer-Encoding")) + { + using var deadline=CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); + deadline.CancelAfter(TimeSpan.FromSeconds(3)); + try { + if(await context.Request.Body.ReadAsync(new byte[1],deadline.Token)>0) + return ApiError(context,400,"invalid_request","Вход принимает Basic без тела запроса"); + } catch(OperationCanceledException) when(!context.RequestAborted.IsCancellationRequested) { + return ApiError(context,400,"invalid_request","Тело запроса не завершено"); + } + } + + var session = await sessions.LoginAsync(username, password); + return session is null + ? Unauthorized(context, basic: true) + : Results.Json(session, statusCode: StatusCodes.Status201Created); + }); + + app.MapDelete("/api/v1/auth/sessions/current", async (HttpContext context, SessionService sessions) => + { + var (user, tokenHash) = await sessions.AuthenticateAsync(context); + if (user is null || tokenHash is null) return Unauthorized(context); + return await sessions.RevokeAsync(tokenHash) + ? Results.NoContent() + : Unauthorized(context); + }); + + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/ChatEndpoints.cs b/src/Messenger.Api/Endpoints/ChatEndpoints.cs new file mode 100644 index 00000000..127feb04 --- /dev/null +++ b/src/Messenger.Api/Endpoints/ChatEndpoints.cs @@ -0,0 +1,67 @@ +using Messenger.Api.Contracts; +using Messenger.Api.Services; +using static Messenger.Api.Services.ApiSupport; + +namespace Messenger.Api.Endpoints; + +public static class ChatEndpoints +{ + public static IEndpointRouteBuilder MapChatEndpoints(this IEndpointRouteBuilder app) + { + app.MapPost("/api/v1/chats", async (HttpContext context, SessionService sessions, ChatService chats) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + + var (request, error) = await ReadJsonAsync(context); + if (error is not null) return error; + if (string.IsNullOrWhiteSpace(request!.Title) || request.Title.EnumerateRunes().Count() > 100) + return ApiError(context, 400, "invalid_request", "Некорректное название чата"); + if (request.MemberIds is null || request.MemberIds.Count is < 1 or > 100 || + request.MemberIds.Distinct().Count() != request.MemberIds.Count || + !request.MemberIds.Contains(actor.Id)) + return ApiError(context, 400, "invalid_request", "Некорректный список участников"); + + var chat = await chats.CreateAsync(request.Title, request.MemberIds); + return chat is null + ? ApiError(context, 400, "invalid_request", "Один из участников не существует") + : Results.Json(chat, statusCode: StatusCodes.Status201Created); + }); + + app.MapGet("/api/v1/chats", async (HttpContext context, SessionService sessions, ChatService chats) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + var (limit, limitError) = ParseLimit(context); + if (limitError is not null) return limitError; + var (cursor, cursorTime, cursorError) = ParseCursor(context, $"user:{actor.Id}"); + if (cursorError is not null) return cursorError; + + var page = await chats.ListAsync(actor.Id, cursorTime, cursor?.Id, limit + 1); + string? nextCursor = null; + if (page.Count > limit) + { + var last = page[limit - 1]; + nextCursor = CursorService.EncodeChatCursor(new ChatCursor($"user:{actor.Id}", last.CreatedAt, last.Id)); + page = page.Take(limit).ToList(); + } + return Results.Ok(new { items = page, next_cursor = nextCursor }); + }); + + app.MapGet("/api/v1/chats/{chat_id}", async ( + string chat_id, HttpContext context, SessionService sessions, ChatService chats) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId)) + return ApiError(context, 400, "invalid_request", "Некорректный chat_id"); + + var chat = await chats.FindForMemberAsync(chatId, actor.Id); + return chat is null + ? ApiError(context, 404, "not_found", "Чат не найден") + : Results.Ok(chat); + }); + + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/HealthEndpoints.cs b/src/Messenger.Api/Endpoints/HealthEndpoints.cs new file mode 100644 index 00000000..da8f4eb0 --- /dev/null +++ b/src/Messenger.Api/Endpoints/HealthEndpoints.cs @@ -0,0 +1,18 @@ +using Messenger.Api.Data; +using Messenger.Api.Services; +using Messenger.Api.Infrastructure; + +namespace Messenger.Api.Endpoints; + +public static class HealthEndpoints +{ + public static IEndpointRouteBuilder MapHealthEndpoints(this IEndpointRouteBuilder app) + { + app.MapGet("/health/live", () => Results.Ok(new { status = "ok" })); + app.MapGet("/health/ready", async (HttpContext context, AppDatabase database, ReadinessState readiness, S3Storage storage) => + !readiness.IsStopping && await database.IsReadyAsync() && await storage.ReadyAsync() + ? Results.Ok(new { status = "ok" }) + : ApiSupport.ApiError(context, 503, "unavailable", "Хранилище недоступно")); + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/MessageEndpoints.cs b/src/Messenger.Api/Endpoints/MessageEndpoints.cs new file mode 100644 index 00000000..db0b9985 --- /dev/null +++ b/src/Messenger.Api/Endpoints/MessageEndpoints.cs @@ -0,0 +1,80 @@ +using Messenger.Api.Contracts; +using Messenger.Api.Services; +using System.Text.Json; +using static Messenger.Api.Services.ApiSupport; + +namespace Messenger.Api.Endpoints; + +public static class MessageEndpoints +{ + public static IEndpointRouteBuilder MapMessageEndpoints(this IEndpointRouteBuilder app) + { + app.MapPost("/api/v1/chats/{chat_id}/messages", async ( + string chat_id, HttpContext context, SessionService sessions, ChatService chats, MessageService messages) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId)) + return ApiError(context, 400, "invalid_request", "Некорректный chat_id"); + if (await chats.FindForMemberAsync(chatId, actor.Id) is null) + return ApiError(context, 404, "not_found", "Чат не найден"); + + var keys = context.Request.Headers["Idempotency-Key"]; + var key = keys.ToString(); + if (keys.Count != 1 || key.Length is < 1 or > 128 || + key.Any(c => !char.IsAsciiLetterOrDigit(c) && c is not '.' and not '_' and not ':' and not '-')) + return ApiError(context, 400, "invalid_request", "Требуется корректный Idempotency-Key"); + + var (request, error) = await ReadJsonAsync(context); + if (error is not null) return error; + if (request!.Text is null || request.Text.EnumerateRunes().Count() > 4000) + return ApiError(context, 400, "invalid_request", "Некорректный текст сообщения"); + var ids = new List(); + if (request.AttachmentIds.ValueKind != JsonValueKind.Undefined) + { + if (request.AttachmentIds.ValueKind != JsonValueKind.Array || request.AttachmentIds.GetArrayLength()>10) + return ApiError(context,400,"invalid_request","Некорректный список вложений"); + foreach(var item in request.AttachmentIds.EnumerateArray()) + { + if(item.ValueKind!=JsonValueKind.String || !item.TryGetGuid(out var id) || ids.Contains(id)) + return ApiError(context,400,"invalid_request","Некорректный ID вложения"); + ids.Add(id); + } + } + if(string.IsNullOrWhiteSpace(request.Text) && ids.Count==0) + return ApiError(context,400,"invalid_request","Нужен текст или вложение"); + + var message = await messages.CreateAsync(chatId, actor.Id, request.Text, key, ids.ToArray()); + if (message is null) + return ApiError(context, 409, "conflict", "Этот ключ уже использован для другого сообщения"); + return Results.Json(message, statusCode: StatusCodes.Status201Created); + }); + + app.MapGet("/api/v1/chats/{chat_id}/messages", async ( + string chat_id, HttpContext context, SessionService sessions, ChatService chats, MessageService messages) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId)) + return ApiError(context, 400, "invalid_request", "Некорректный chat_id"); + if (await chats.FindForMemberAsync(chatId, actor.Id) is null) + return ApiError(context, 404, "not_found", "Чат не найден"); + var (limit, limitError) = ParseLimit(context); + if (limitError is not null) return limitError; + var (cursor, cursorTime, cursorError) = ParseCursor(context, $"chat:{chatId}"); + if (cursorError is not null) return cursorError; + + var page = await messages.ListAsync(chatId, cursorTime, cursor?.Id, limit + 1); + string? nextCursor = null; + if (page.Count > limit) + { + var last = page[limit - 1]; + nextCursor = CursorService.EncodeChatCursor(new ChatCursor($"chat:{chatId}", last.CreatedAt, last.Id)); + page = page.Take(limit).ToList(); + } + return Results.Ok(new { items = page, next_cursor = nextCursor }); + }); + + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/OperationsEndpoints.cs b/src/Messenger.Api/Endpoints/OperationsEndpoints.cs new file mode 100644 index 00000000..e3873a7c --- /dev/null +++ b/src/Messenger.Api/Endpoints/OperationsEndpoints.cs @@ -0,0 +1,79 @@ +using Messenger.Api.Data; +using Messenger.Api.Infrastructure; +using Messenger.Api.Pipeline; +using Messenger.Api.Services; +using Npgsql; +using static Messenger.Api.Services.ApiSupport; +namespace Messenger.Api.Endpoints; +public static class OperationsEndpoints +{ + public static bool IsOperator(Guid id,IConfiguration config)=>Guid.TryParse(config["OPS_USER_ID"],out var owner)&&owner==id; + public static IEndpointRouteBuilder MapOperationsEndpoints(this IEndpointRouteBuilder app) + { + app.MapGet("/api/ui/operator",async(HttpContext ctx,SessionService sessions,IConfiguration config)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx);if(actor is null)return Unauthorized(ctx); + ctx.Response.Headers.CacheControl="no-store"; + return Results.Ok(new {enabled=IsOperator(actor.Id,config)}); + }); + app.MapGet("/api/ui/operations",async(HttpContext ctx,SessionService sessions,IConfiguration config, + NpgsqlDataSource source,ClusterPresence cluster,S3Storage storage)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx);if(actor is null)return Unauthorized(ctx); + if(!IsOperator(actor.Id,config))return ApiError(ctx,404,"not_found","Доступ только владельцу системы"); + ctx.Response.Headers.CacheControl="no-store"; + using var deadline=CancellationTokenSource.CreateLinkedTokenSource(ctx.RequestAborted);deadline.CancelAfter(TimeSpan.FromSeconds(7)); + var ct=deadline.Token; + var counts=new Dictionary{{"queued",0},{"processing",0},{"ready",0},{"failed",0}}; + var recent=new List();var history=new List();var heartbeats=new Dictionary(); + long pending=0;double? latency=null; + await using(var cmd=source.CreateCommand("SELECT state,count(*) FROM attachment_jobs GROUP BY state")) + await using(var rd=await cmd.ExecuteReaderAsync(ct))while(await rd.ReadAsync(ct))counts[rd.GetString(0)]=rd.GetInt64(1); + await using(var cmd=source.CreateCommand("SELECT count(*) FROM attachment_outbox WHERE published_at IS NULL"))pending=(long)(await cmd.ExecuteScalarAsync(ct))!; + await using(var cmd=source.CreateCommand("SELECT avg(extract(epoch FROM (completed_at-created_at)))::double precision FROM attachment_jobs WHERE state='ready' AND completed_at>now()-interval '15 minutes'")) { + var value=await cmd.ExecuteScalarAsync(ct);if(value is double seconds)latency=seconds; + } + await using(var cmd=source.CreateCommand("SELECT role,seen_at FROM pipeline_presence")) + await using(var rd=await cmd.ExecuteReaderAsync(ct))while(await rd.ReadAsync(ct))heartbeats[rd.GetString(0)]=rd.GetDateTime(1); + await using(var cmd=source.CreateCommand(""" + SELECT a.id,a.filename,j.state,j.attempts,j.error_code,j.created_at,j.completed_at, + j.state='failed' AND j.staging_deleted_at IS NULL + FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id ORDER BY j.created_at DESC LIMIT 30 + """)) + await using(var rd=await cmd.ExecuteReaderAsync(ct))while(await rd.ReadAsync(ct))recent.Add(new { + attachment_id=rd.GetGuid(0),filename=rd.GetString(1),state=rd.GetString(2),attempts=rd.GetInt32(3), + error_code=rd.IsDBNull(4)?null:rd.GetString(4),created_at=rd.GetDateTime(5), + completed_at=rd.IsDBNull(6)?(DateTime?)null:rd.GetDateTime(6),can_replay=rd.GetBoolean(7)}); + await using(var cmd=source.CreateCommand(""" + SELECT e.event,e.attempt,e.created_at,a.filename FROM attachment_job_events e + JOIN attachment_jobs j ON j.id=e.job_id JOIN attachments a ON a.id=j.attachment_id ORDER BY e.id DESC LIMIT 30 + """)) + await using(var rd=await cmd.ExecuteReaderAsync(ct))while(await rd.ReadAsync(ct))history.Add(new { + name=rd.GetString(0),attempt=rd.GetInt32(1),at=rd.GetDateTime(2),filename=rd.GetString(3)}); + bool? s3=null,broker=null;uint? messages=null,consumers=null; + async Task ProbeStorage() {try {await storage.Client.GetBucketLocationAsync(storage.Bucket,ct);s3=true;}catch {s3=false;}} + async Task ProbeBroker() {try { + await using var connection=await BrokerConnection.OpenAsync(config,ct); + await using var channel=await connection.CreateChannelAsync(cancellationToken:ct); + var result=await channel.QueueDeclarePassiveAsync(BrokerConnection.Queue,ct); + broker=true;messages=result.MessageCount;consumers=result.ConsumerCount; + }catch {broker=false;}} + var snapshotTask=cluster.ReadAsync();await Task.WhenAll(ProbeStorage(),ProbeBroker());var snapshot=await snapshotTask; + var now=DateTime.UtcNow; + return Results.Ok(new {checked_at=now,counts,pending_outbox=pending,average_cycle_seconds=latency, + services=new {postgres=true,redis=snapshot.RedisAvailable,s3,broker, + worker=heartbeats.TryGetValue("worker",out var worker)&&(now-worker).TotalSeconds<10, + dispatcher=heartbeats.TryGetValue("dispatcher",out var dispatcher)&&(now-dispatcher).TotalSeconds<10}, + instances=snapshot.Instances,broker_messages=messages,broker_consumers=consumers,jobs=recent,events=history}); + }); + app.MapPost("/api/ui/operations/attachments/{id:guid}/replay",async(Guid id,HttpContext ctx, + SessionService sessions,IConfiguration config,NpgsqlDataSource source)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx);if(actor is null)return Unauthorized(ctx); + if(!IsOperator(actor.Id,config))return ApiError(ctx,404,"not_found","Доступ только владельцу системы"); + await using var cmd=source.CreateCommand("SELECT id FROM attachment_jobs WHERE attachment_id=@id");cmd.Parameters.AddWithValue("id",id); + var job=await cmd.ExecuteScalarAsync(ctx.RequestAborted); + if(job is not Guid jobId)return ApiError(ctx,404,"not_found","Задание не найдено"); + var changed=await new JobStore(source,config).ReplayAsync(jobId,ctx.RequestAborted); + return changed?Results.Accepted(value:new {status="queued"}):ApiError(ctx,409,"conflict","Повтор недоступен: задание не завершилось ошибкой или исходник уже удалён"); + }); + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/ReactionEndpoints.cs b/src/Messenger.Api/Endpoints/ReactionEndpoints.cs new file mode 100644 index 00000000..5459a1a4 --- /dev/null +++ b/src/Messenger.Api/Endpoints/ReactionEndpoints.cs @@ -0,0 +1,39 @@ +using Messenger.Api.Data; +using Messenger.Api.Services; +using static Messenger.Api.Services.ApiSupport; + +namespace Messenger.Api.Endpoints; + +public static class ReactionEndpoints +{ + public static IEndpointRouteBuilder MapReactionEndpoints(this IEndpointRouteBuilder app) + { + app.MapGet("/api/ui/chats/{chat_id}/reactions", async ( + string chat_id, HttpContext context, SessionService sessions, ChatService chats, AppDatabase database) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId)) return ApiError(context, 400, "invalid_request", "Некорректный chat_id"); + if (await chats.FindForMemberAsync(chatId, actor.Id) is null) return ApiError(context, 404, "not_found", "Чат не найден"); + var values = context.Request.Query["message_ids"].ToString().Split(','); + if (values.Length is < 1 or > 100 || values.Any(value => !Guid.TryParse(value, out _))) + return ApiError(context, 400, "invalid_request", "Укажите от 1 до 100 ID сообщений"); + var ids = values.Select(Guid.Parse).Distinct().ToArray(); + return Results.Ok(new { items = await database.ListReactionsAsync(chatId, ids, actor.Id) }); + }); + app.MapMethods("/api/ui/chats/{chat_id}/messages/{message_id}/reactions/{reaction}", ["PUT", "DELETE"], async ( + string chat_id, string message_id, string reaction, HttpContext context, + SessionService sessions, ChatService chats, AppDatabase database) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId) || !Guid.TryParse(message_id, out var messageId) + || reaction is not ("like" or "love" or "laugh" or "mushroom")) + return ApiError(context, 400, "invalid_request", "Некорректная реакция или ID"); + if (await chats.FindForMemberAsync(chatId, actor.Id) is null) return ApiError(context, 404, "not_found", "Чат не найден"); + return await database.SetReactionAsync(chatId, messageId, actor.Id, reaction, context.Request.Method == "PUT") + ? Results.NoContent() : ApiError(context, 404, "not_found", "Сообщение не найдено"); + }); + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/UiEndpoints.cs b/src/Messenger.Api/Endpoints/UiEndpoints.cs new file mode 100644 index 00000000..75a6131f --- /dev/null +++ b/src/Messenger.Api/Endpoints/UiEndpoints.cs @@ -0,0 +1,87 @@ +using Messenger.Api.Contracts; +using Messenger.Api.Services; +using Messenger.Api.Data; +using Messenger.Api.Infrastructure; +using static Messenger.Api.Services.ApiSupport; + +namespace Messenger.Api.Endpoints; + +public static class UiEndpoints +{ + // The required /api/v1 history stays ascending. The web client has a separate, + // optional route to open a large chat at its newest page and page backwards. + public static IEndpointRouteBuilder MapUiEndpoints(this IEndpointRouteBuilder app) + { + app.MapGet("/api/ui/cluster", async (HttpContext context, SessionService sessions, ClusterPresence presence) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + context.Response.Headers.CacheControl = "no-store"; + return Results.Ok(await presence.ReadAsync()); + }); + + app.MapGet("/api/ui/users/{username}",async(string username,HttpContext ctx,SessionService sessions,AppDatabase db)=> { + var(actor,_)=await sessions.AuthenticateAsync(ctx);if(actor is null)return Unauthorized(ctx); + if(!System.Text.RegularExpressions.Regex.IsMatch(username,"\\A[a-z0-9_]{3,32}\\z"))return ApiError(ctx,400,"invalid_request","Некорректный логин"); + var user=await db.FindUserByUsernameAsync(username); + return user is null?ApiError(ctx,404,"not_found","Пользователь не найден"):Results.Ok(user.ToDto()); + }); + + app.MapGet("/api/ui/chats", async (HttpContext context, SessionService sessions, AppDatabase database) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + var (limit, limitError) = ParseLimit(context); + if (limitError is not null) return limitError; + var (cursor, cursorTime, cursorError) = ParseCursor(context, $"ui-chats:{actor.Id}"); + if (cursorError is not null) return cursorError; + var page = await database.ListChatPreviewsAsync(actor.Id, cursorTime, cursor?.Id, limit + 1); + string? nextCursor = null; + if (page.Count > limit) + { + var last = page[limit - 1]; + nextCursor = CursorService.EncodeChatCursor(new ChatCursor($"ui-chats:{actor.Id}", last.CreatedAt, last.Id)); + page = page.Take(limit).ToList(); + } + return Results.Ok(new { items = page, next_cursor = nextCursor }); + }); + + app.MapGet("/api/ui/chats/{chat_id}/members", async ( + string chat_id, HttpContext context, SessionService sessions, ChatService chats, UserService users) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId)) + return ApiError(context, 400, "invalid_request", "Некорректный chat_id"); + if (await chats.FindForMemberAsync(chatId, actor.Id) is null) + return ApiError(context, 404, "not_found", "Чат не найден"); + return Results.Ok(new { items = await users.ListForChatAsync(chatId) }); + }); + + app.MapGet("/api/ui/chats/{chat_id}/messages", async ( + string chat_id, HttpContext context, SessionService sessions, ChatService chats, MessageService messages) => + { + var (actor, _) = await sessions.AuthenticateAsync(context); + if (actor is null) return Unauthorized(context); + if (!Guid.TryParse(chat_id, out var chatId)) + return ApiError(context, 400, "invalid_request", "Некорректный chat_id"); + if (await chats.FindForMemberAsync(chatId, actor.Id) is null) + return ApiError(context, 404, "not_found", "Чат не найден"); + var (limit, limitError) = ParseLimit(context); + if (limitError is not null) return limitError; + var (cursor, cursorTime, cursorError) = ParseCursor(context, $"recent-chat:{chatId}"); + if (cursorError is not null) return cursorError; + + var page = await messages.ListAsync(chatId, cursorTime, cursor?.Id, limit + 1, newestFirst: true); + string? nextCursor = null; + if (page.Count > limit) + { + var last = page[limit - 1]; + nextCursor = CursorService.EncodeChatCursor(new ChatCursor($"recent-chat:{chatId}", last.CreatedAt, last.Id)); + page = page.Take(limit).ToList(); + } + return Results.Ok(new { items = page, next_cursor = nextCursor }); + }); + return app; + } +} diff --git a/src/Messenger.Api/Endpoints/UserEndpoints.cs b/src/Messenger.Api/Endpoints/UserEndpoints.cs new file mode 100644 index 00000000..a6725b19 --- /dev/null +++ b/src/Messenger.Api/Endpoints/UserEndpoints.cs @@ -0,0 +1,40 @@ +using System.Text.RegularExpressions; +using Messenger.Api.Contracts; +using Messenger.Api.Services; +using static Messenger.Api.Services.ApiSupport; + +namespace Messenger.Api.Endpoints; + +public static class UserEndpoints +{ + public static IEndpointRouteBuilder MapUserEndpoints(this IEndpointRouteBuilder app) + { + app.MapPost("/api/v1/users", async (HttpContext context, UserService users) => + { + var (request, error) = await ReadJsonAsync(context); + if (error is not null) return error; + + if (request!.Username is null || !Regex.IsMatch(request.Username, "\\A[a-z0-9_]{3,32}\\z")) + return ApiError(context, 400, "invalid_request", "Некорректный username"); + if (string.IsNullOrWhiteSpace(request.DisplayName) || + request.DisplayName.EnumerateRunes().Count() is < 1 or > 100) + return ApiError(context, 400, "invalid_request", "Некорректный display_name"); + if (string.IsNullOrWhiteSpace(request.Password) || + request.Password.EnumerateRunes().Count() is < 12 or > 128) + return ApiError(context, 400, "invalid_request", "Пароль должен содержать 12-128 символов"); + + var user = await users.RegisterAsync(request.Username, request.DisplayName, request.Password); + return user is null + ? ApiError(context, 409, "conflict", "Username уже занят") + : Results.Json(user, statusCode: StatusCodes.Status201Created); + }); + + app.MapGet("/api/v1/users/me", async (HttpContext context, SessionService sessions) => + { + var (user, _) = await sessions.AuthenticateAsync(context); + return user is null ? Unauthorized(context) : Results.Ok(user); + }); + + return app; + } +} diff --git a/src/Messenger.Api/Infrastructure/ClusterPresence.cs b/src/Messenger.Api/Infrastructure/ClusterPresence.cs new file mode 100644 index 00000000..adccb65b --- /dev/null +++ b/src/Messenger.Api/Infrastructure/ClusterPresence.cs @@ -0,0 +1,55 @@ +using Messenger.Api.Data; +using StackExchange.Redis; + +namespace Messenger.Api.Infrastructure; + +// Registry entries live ten minutes; an eight-second heartbeat gap means unavailable. +// A Redis outage is reported as unknown, never as proof that an API has stopped. +public sealed class ClusterPresence(IConnectionMultiplexer redis, InstanceIdentity identity, + ReadinessState readiness, AppDatabase database, ILogger logger) : BackgroundService +{ + private const string Registry = "messenger:ui:instances"; + public sealed record Node(string Id, bool Ready, long LastSeen); + public sealed record Snapshot(bool RedisAvailable, bool PostgresAvailable, long CheckedAt, List Instances); + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + using var timer = new PeriodicTimer(TimeSpan.FromSeconds(2)); + try + { + do + { + try + { + if (redis.IsConnected && !readiness.IsStopping && await database.IsReadyAsync()) + { + var db = redis.GetDatabase(); + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + await db.SortedSetAddAsync(Registry, identity.Id, now).WaitAsync(TimeSpan.FromSeconds(1), stoppingToken); + await db.SortedSetRemoveRangeByScoreAsync(Registry, double.NegativeInfinity, now - 600_000); + await db.KeyExpireAsync(Registry, TimeSpan.FromMinutes(15)); + } + } + catch (Exception error) when (error is RedisException or TimeoutException) + { logger.LogDebug("Cluster heartbeat unavailable"); } + } while (await timer.WaitForNextTickAsync(stoppingToken)); + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { } + } + + public async Task ReadAsync() + { + var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var postgres = await database.IsReadyAsync(); + try + { + if (!redis.IsConnected) return new(false, postgres, now, []); + var entries = await redis.GetDatabase().SortedSetRangeByScoreWithScoresAsync(Registry, + now - 600_000, double.PositiveInfinity).WaitAsync(TimeSpan.FromSeconds(1)); + return new(true, postgres, now, entries.Select(entry => new Node(entry.Element.ToString(), + now - entry.Score < 8000, (long)entry.Score)).ToList()); + } + catch (Exception error) when (error is RedisException or TimeoutException) + { return new(false, postgres, now, []); } + } +} diff --git a/src/Messenger.Api/Infrastructure/InstanceIdentity.cs b/src/Messenger.Api/Infrastructure/InstanceIdentity.cs new file mode 100644 index 00000000..855f4460 --- /dev/null +++ b/src/Messenger.Api/Infrastructure/InstanceIdentity.cs @@ -0,0 +1,6 @@ +namespace Messenger.Api.Infrastructure; + +public sealed class InstanceIdentity +{ + public string Id { get; } = Guid.NewGuid().ToString("N"); +} diff --git a/src/Messenger.Api/Infrastructure/ReadinessState.cs b/src/Messenger.Api/Infrastructure/ReadinessState.cs new file mode 100644 index 00000000..35981ef2 --- /dev/null +++ b/src/Messenger.Api/Infrastructure/ReadinessState.cs @@ -0,0 +1,8 @@ +namespace Messenger.Api.Infrastructure; + +public sealed class ReadinessState +{ + private int _stopping; + public bool IsStopping => Volatile.Read(ref _stopping) != 0; + public void Stop() => Interlocked.Exchange(ref _stopping, 1); +} diff --git a/src/Messenger.Api/Infrastructure/RedisSessionCache.cs b/src/Messenger.Api/Infrastructure/RedisSessionCache.cs new file mode 100644 index 00000000..09e977fa --- /dev/null +++ b/src/Messenger.Api/Infrastructure/RedisSessionCache.cs @@ -0,0 +1,58 @@ +using System.Text.Json; +using Messenger.Api.Contracts; +using StackExchange.Redis; + +namespace Messenger.Api.Infrastructure; + +// PostgreSQL validates the session on every request. This cache saves the user lookup +// and never grants access independently of the durable session row. +public sealed class RedisSessionCache(IConnectionMultiplexer connection, ILogger logger) +{ + private static readonly TimeSpan OperationBudget = TimeSpan.FromMilliseconds(750); + private static string Key(byte[] hash) => "messenger:session:" + Convert.ToHexString(hash).ToLowerInvariant(); + + public sealed record Entry(UserDto User, DateTime ExpiresAt); + + public async Task GetAsync(byte[] hash) + { + if (!connection.IsConnected) return null; + try + { + var value = await connection.GetDatabase().StringGetAsync(Key(hash)).WaitAsync(OperationBudget); + return value.IsNullOrEmpty ? null : JsonSerializer.Deserialize(value.ToString()); + } + catch (Exception exception) when (exception is RedisException or TimeoutException or OperationCanceledException or JsonException) + { + logger.LogWarning("Session cache read unavailable; using PostgreSQL"); + return null; + } + } + + public async Task SetAsync(byte[] hash, UserDto user, DateTime expiresAt) + { + var remaining = expiresAt - DateTime.UtcNow; + if (!connection.IsConnected || remaining <= TimeSpan.Zero) return; + try + { + await connection.GetDatabase().StringSetAsync(Key(hash), + JsonSerializer.Serialize(new Entry(user, expiresAt)), remaining).WaitAsync(OperationBudget); + } + catch (Exception exception) when (exception is RedisException or TimeoutException or OperationCanceledException) + { + logger.LogWarning("Session cache write unavailable; using PostgreSQL"); + } + } + + public async Task RemoveAsync(byte[] hash) + { + if (!connection.IsConnected) return; + try + { + await connection.GetDatabase().KeyDeleteAsync(Key(hash)).WaitAsync(OperationBudget); + } + catch (Exception exception) when (exception is RedisException or TimeoutException or OperationCanceledException) + { + logger.LogWarning("Session cache invalidation unavailable; PostgreSQL still enforces revocation"); + } + } +} diff --git a/src/Messenger.Api/Infrastructure/S3Storage.cs b/src/Messenger.Api/Infrastructure/S3Storage.cs new file mode 100644 index 00000000..719fb30b --- /dev/null +++ b/src/Messenger.Api/Infrastructure/S3Storage.cs @@ -0,0 +1,26 @@ +using Amazon.S3; +using Amazon.S3.Model; +using Amazon.Runtime; +namespace Messenger.Api.Infrastructure; +public sealed class S3Storage : IDisposable +{ + public AmazonS3Client Client { get; } + public string Bucket { get; } + public S3Storage(IConfiguration config) + { + Bucket=config["S3_BUCKET"] ?? "messenger"; + Client=new AmazonS3Client(new BasicAWSCredentials( + File.ReadAllText(config["S3_ACCESS_KEY_FILE"]!).Trim(), + File.ReadAllText(config["S3_SECRET_KEY_FILE"]!).Trim()),new AmazonS3Config { + ServiceURL=config["S3_ENDPOINT"] ?? "http://s3:9000", ForcePathStyle=true, + AuthenticationRegion="us-east-1", MaxErrorRetry=0, Timeout=TimeSpan.FromSeconds(20) + }); + } + public async Task ReadyAsync() + { + using var timeout=new CancellationTokenSource(TimeSpan.FromSeconds(1)); + try { await Client.ListObjectsV2Async(new ListObjectsV2Request {BucketName=Bucket,MaxKeys=1},timeout.Token); return true; } + catch { return false; } + } + public void Dispose() => Client.Dispose(); +} diff --git a/src/Messenger.Api/Messenger.Api.csproj b/src/Messenger.Api/Messenger.Api.csproj new file mode 100644 index 00000000..a616f1c9 --- /dev/null +++ b/src/Messenger.Api/Messenger.Api.csproj @@ -0,0 +1,23 @@ + + + + net9.0 + enable + enable + + + + + + + + + + + + + + + + + diff --git a/src/Messenger.Api/Middleware/ExceptionHandlingMiddleware.cs b/src/Messenger.Api/Middleware/ExceptionHandlingMiddleware.cs new file mode 100644 index 00000000..1047f952 --- /dev/null +++ b/src/Messenger.Api/Middleware/ExceptionHandlingMiddleware.cs @@ -0,0 +1,42 @@ +using Messenger.Api.Services; +using System.Net.Sockets; +using Npgsql; + +namespace Messenger.Api.Middleware; + +public sealed class ExceptionHandlingMiddleware(RequestDelegate next, ILogger logger) +{ + public async Task InvokeAsync(HttpContext context) + { + try + { + await next(context); + } + catch (AttachmentException exception) + { + if(!context.Response.HasStarted) + await ApiSupport.ApiError(context,exception.Status,exception.Code,exception.Message).ExecuteAsync(context); + } + catch (OperationCanceledException) when (!context.RequestAborted.IsCancellationRequested) + { + if(!context.Response.HasStarted) + await ApiSupport.ApiError(context,503,"unavailable","Время ожидания истекло").ExecuteAsync(context); + } + catch (BadHttpRequestException exception) when(exception.StatusCode==413) + { + await ApiSupport.ApiError(context,413,"payload_too_large","Слишком большой запрос").ExecuteAsync(context); + } + catch (Exception exception) when (exception is NpgsqlException or SocketException or TimeoutException or Amazon.S3.AmazonS3Exception or HttpRequestException) + { + logger.LogError(exception, "Database request failed"); + if (!context.Response.HasStarted) + await ApiSupport.ApiError(context, 503, "unavailable", "Сервис временно недоступен").ExecuteAsync(context); + } + catch (Exception exception) + { + logger.LogError(exception, "Request failed"); + if (!context.Response.HasStarted) + await ApiSupport.ApiError(context, 500, "internal_error", "Внутренняя ошибка").ExecuteAsync(context); + } + } +} diff --git a/src/Messenger.Api/Middleware/InstanceMiddleware.cs b/src/Messenger.Api/Middleware/InstanceMiddleware.cs new file mode 100644 index 00000000..c4b75741 --- /dev/null +++ b/src/Messenger.Api/Middleware/InstanceMiddleware.cs @@ -0,0 +1,18 @@ +using Messenger.Api.Infrastructure; +using Messenger.Api.Services; + +namespace Messenger.Api.Middleware; + +public sealed class InstanceMiddleware(RequestDelegate next) +{ + public async Task InvokeAsync(HttpContext context, InstanceIdentity identity, ReadinessState readiness) + { + context.Response.Headers["X-Instance-Id"] = identity.Id; + if (readiness.IsStopping && context.Request.Path != "/health/live") + { + await ApiSupport.ApiError(context, 503, "unavailable", "Экземпляр API останавливается").ExecuteAsync(context); + return; + } + await next(context); + } +} diff --git a/src/Messenger.Api/Middleware/RequestIdMiddleware.cs b/src/Messenger.Api/Middleware/RequestIdMiddleware.cs new file mode 100644 index 00000000..973b6373 --- /dev/null +++ b/src/Messenger.Api/Middleware/RequestIdMiddleware.cs @@ -0,0 +1,16 @@ +namespace Messenger.Api.Middleware; + +public sealed class RequestIdMiddleware +{ + private readonly RequestDelegate _next; + + public RequestIdMiddleware(RequestDelegate next) => _next = next; + + public async Task InvokeAsync(HttpContext context) + { + var requestId = Guid.NewGuid().ToString(); + context.Items["RequestId"] = requestId; + context.Response.Headers["X-Request-Id"] = requestId; + await _next(context); + } +} diff --git a/src/Messenger.Api/Middleware/RequestLoggingMiddleware.cs b/src/Messenger.Api/Middleware/RequestLoggingMiddleware.cs new file mode 100644 index 00000000..865f56d6 --- /dev/null +++ b/src/Messenger.Api/Middleware/RequestLoggingMiddleware.cs @@ -0,0 +1,25 @@ +namespace Messenger.Api.Middleware; + +public sealed class RequestLoggingMiddleware +{ + private readonly RequestDelegate _next; + + public RequestLoggingMiddleware(RequestDelegate next) => _next = next; + + public async Task InvokeAsync(HttpContext context) + { + var logger = context.RequestServices + .GetRequiredService() + .CreateLogger("Http"); + + await _next(context); + + logger.LogInformation( + "{Method} {Path} -> {StatusCode} request_id={RequestId}", + context.Request.Method, + context.Request.Path, + context.Response.StatusCode, + context.Items["RequestId"] + ); + } +} diff --git a/src/Messenger.Api/Pipeline/BrokerConnection.cs b/src/Messenger.Api/Pipeline/BrokerConnection.cs new file mode 100644 index 00000000..e6323f44 --- /dev/null +++ b/src/Messenger.Api/Pipeline/BrokerConnection.cs @@ -0,0 +1,21 @@ +using RabbitMQ.Client; +namespace Messenger.Api.Pipeline; +public static class BrokerConnection +{ + public const string Queue="attachments.v1"; + public static Task OpenAsync(IConfiguration config,CancellationToken ct) + { + var factory=new ConnectionFactory { + HostName=config["BROKER_HOST"]??"broker", UserName="messenger", + Password=File.ReadAllText(config["BROKER_PASSWORD_FILE"]!).Trim(), + VirtualHost="messenger",AutomaticRecoveryEnabled=true, + NetworkRecoveryInterval=TimeSpan.FromSeconds(2),RequestedHeartbeat=TimeSpan.FromSeconds(10), + RequestedConnectionTimeout=TimeSpan.FromSeconds(5) + }; + return factory.CreateConnectionAsync(ct); + } + public static Task DeclareAsync(IChannel channel,CancellationToken ct)=>channel.QueueDeclareAsync( + Queue,durable:true,exclusive:false,autoDelete:false,arguments:new Dictionary { + ["x-queue-type"]="quorum",["x-max-length"]=256,["x-overflow"]="reject-publish" + },cancellationToken:ct); +} diff --git a/src/Messenger.Api/Pipeline/FaultInjection.cs b/src/Messenger.Api/Pipeline/FaultInjection.cs new file mode 100644 index 00000000..e616dae7 --- /dev/null +++ b/src/Messenger.Api/Pipeline/FaultInjection.cs @@ -0,0 +1,19 @@ +using System.Text.Json; +namespace Messenger.Api.Pipeline; +public static class FaultInjection +{ + // Explicit local operator instrumentation; disabled in the normal Compose configuration. + public static async Task HitAsync(IConfiguration config,string point,Guid job,CancellationToken ct) + { + var path=config["PIPELINE_FAULT_FILE"]; + if(string.IsNullOrEmpty(path)||!File.Exists(path))return; + try { + using var json=JsonDocument.Parse(await File.ReadAllTextAsync(path,ct)); + if(json.RootElement.GetProperty("point").GetString()!=point || json.RootElement.GetProperty("job_id").GetGuid()!=job)return; + File.Move(path,path+".used",true); + await File.WriteAllTextAsync(path+".hit",point,ct); + }catch(IOException){return;} + Console.WriteLine($"Operator fault pause: {point}, job {job}"); + await Task.Delay(Timeout.InfiniteTimeSpan,ct); + } +} diff --git a/src/Messenger.Api/Pipeline/ImageRenderer.cs b/src/Messenger.Api/Pipeline/ImageRenderer.cs new file mode 100644 index 00000000..a05b82e1 --- /dev/null +++ b/src/Messenger.Api/Pipeline/ImageRenderer.cs @@ -0,0 +1,57 @@ +using System.Text.Json; +using System.Security.Cryptography; +using Messenger.Api.Contracts; +using SkiaSharp; +namespace Messenger.Api.Pipeline; +public static class ImageRenderer +{ + public static void Render(string input,string output) + { + using var stream=File.OpenRead(input);using var managed=new SKManagedStream(stream,false); + using var codec=SKCodec.Create(managed); + if(codec is null || codec.EncodedFormat is not SKEncodedImageFormat.Png and not SKEncodedImageFormat.Jpeg) + throw new InvalidDataException("invalid_image"); + var info=codec.Info; + if((long)info.Width*info.Height>40_000_000 || info.Width<1 || info.Height<1)throw new InvalidDataException("invalid_image"); + using var bitmap=new SKBitmap(new SKImageInfo(info.Width,info.Height,SKColorType.Rgba8888,SKAlphaType.Premul)); + if(codec.GetPixels(bitmap.Info,bitmap.GetPixels())!=SKCodecResult.Success)throw new InvalidDataException("invalid_image"); + var rotated=(int)codec.EncodedOrigin>=5; + int width=rotated?info.Height:info.Width,height=rotated?info.Width:info.Height; + var matrix=codec.EncodedOrigin switch { + SKEncodedOrigin.TopRight=>new SKMatrix(-1,0,info.Width,0,1,0,0,0,1), + SKEncodedOrigin.BottomRight=>new SKMatrix(-1,0,info.Width,0,-1,info.Height,0,0,1), + SKEncodedOrigin.BottomLeft=>new SKMatrix(1,0,0,0,-1,info.Height,0,0,1), + SKEncodedOrigin.LeftTop=>new SKMatrix(0,1,0,1,0,0,0,0,1), + SKEncodedOrigin.RightTop=>new SKMatrix(0,-1,info.Height,1,0,0,0,0,1), + SKEncodedOrigin.RightBottom=>new SKMatrix(0,-1,info.Height,-1,0,info.Width,0,0,1), + SKEncodedOrigin.LeftBottom=>new SKMatrix(0,1,0,-1,0,info.Width,0,0,1), + _=>SKMatrix.Identity + }; + Directory.CreateDirectory(output);var variants=new List(); + foreach(var name in new[]{"thumbnail","crop","optimized"}) + { + var crop=name=="crop";var limit=name=="thumbnail"?256:1280; + var scale=Math.Min(1,(double)limit/Math.Max(width,height)); + int targetWidth=crop?128:Math.Max(1,(int)Math.Floor(width*scale)); + int targetHeight=crop?128:Math.Max(1,(int)Math.Floor(height*scale)); + float side=Math.Min(width,height); + var rect=crop?new SKRect((width-side)/2,(height-side)/2,(width+side)/2,(height+side)/2):new SKRect(0,0,width,height); + using var target=new SKBitmap(new SKImageInfo(targetWidth,targetHeight,SKColorType.Rgba8888,SKAlphaType.Premul)); + using(var canvas=new SKCanvas(target)) + { + canvas.Clear(SKColors.White); + canvas.Scale(targetWidth/rect.Width,targetHeight/rect.Height);canvas.Translate(-rect.Left,-rect.Top); + canvas.Concat(matrix); + using var paint=new SKPaint { IsAntialias=true }; + canvas.DrawBitmap(bitmap,0,0,paint); + } + using var image=SKImage.FromBitmap(target); + var webp=name=="optimized"; + using var data=image.Encode(webp?SKEncodedImageFormat.Webp:SKEncodedImageFormat.Jpeg,webp?80:85); + var bytes=data.ToArray();File.WriteAllBytes(Path.Combine(output,name),bytes); + variants.Add(new(name,webp?"image/webp":"image/jpeg",bytes.Length,targetWidth,targetHeight, + Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant())); + } + File.WriteAllText(Path.Combine(output,"variants.json"),JsonSerializer.Serialize(variants,JobJson.Options)); + } +} diff --git a/src/Messenger.Api/Pipeline/JobEnvelope.cs b/src/Messenger.Api/Pipeline/JobEnvelope.cs new file mode 100644 index 00000000..411b041e --- /dev/null +++ b/src/Messenger.Api/Pipeline/JobEnvelope.cs @@ -0,0 +1,8 @@ +using System.Text.Json; +namespace Messenger.Api.Pipeline; +public sealed record JobEnvelope(int SchemaVersion, Guid JobId, string Type, Guid AttachmentId, + int PipelineVersion, int Attempt, string CreatedAt); +public static class JobJson +{ + public static readonly JsonSerializerOptions Options=new() {PropertyNamingPolicy=JsonNamingPolicy.SnakeCaseLower}; +} diff --git a/src/Messenger.Api/Pipeline/JobOperations.cs b/src/Messenger.Api/Pipeline/JobOperations.cs new file mode 100644 index 00000000..f5492a40 --- /dev/null +++ b/src/Messenger.Api/Pipeline/JobOperations.cs @@ -0,0 +1,22 @@ +using System.Text.Json; +using Npgsql; +using RabbitMQ.Client; +namespace Messenger.Api.Pipeline; +public static class JobOperations +{ + public static async Task RedeliverAsync(NpgsqlDataSource db,IConfiguration config,Guid id,CancellationToken ct) + { + await using var connection=await db.OpenConnectionAsync(ct); + await using var cmd=JobStore.Command(connection,"SELECT attachment_id,pipeline_version,attempts,state,created_at FROM attachment_jobs WHERE id=@id",("id",id)); + JobEnvelope job; + await using(var r=await cmd.ExecuteReaderAsync(ct)) { + if(!await r.ReadAsync(ct))throw new InvalidOperationException("Job not found"); + job=new(1,id,"attachment.process.v1",r.GetGuid(0),r.GetInt32(1),Math.Max(1,r.GetInt32(2)+(r.GetString(3)=="queued"?1:0)),r.GetDateTime(4).ToString("O")); + } + await using var broker=await BrokerConnection.OpenAsync(config,ct); + await using var channel=await broker.CreateChannelAsync(new CreateChannelOptions(true,true),ct); + await BrokerConnection.DeclareAsync(channel,ct); + await channel.BasicPublishAsync("",BrokerConnection.Queue,true,new BasicProperties {Persistent=true,ContentType="application/json",MessageId=id.ToString()},JsonSerializer.SerializeToUtf8Bytes(job,JobJson.Options),ct); + Console.WriteLine(JsonSerializer.Serialize(job,JobJson.Options)); + } +} diff --git a/src/Messenger.Api/Pipeline/JobStore.cs b/src/Messenger.Api/Pipeline/JobStore.cs new file mode 100644 index 00000000..4a915acd --- /dev/null +++ b/src/Messenger.Api/Pipeline/JobStore.cs @@ -0,0 +1,130 @@ +using Npgsql; +namespace Messenger.Api.Pipeline; +public sealed record ClaimedJob(Guid Id,Guid AttachmentId,string StagingKey,string Sha256,long Size,string ContentType,int Attempt,Guid Lease); +public sealed class JobStore(NpgsqlDataSource db,IConfiguration config) +{ + public int MaxAttempts=>Math.Clamp(config.GetValue("JOB_MAX_ATTEMPTS",5),1,20); + public int TimeoutSeconds=>Math.Clamp(config.GetValue("JOB_TIMEOUT_SECONDS",30),5,180); + public static NpgsqlCommand Command(NpgsqlConnection connection,string sql,params (string,object)[] values) + { + var cmd=new NpgsqlCommand(sql,connection); + foreach(var(name,value) in values) cmd.Parameters.AddWithValue(name,value); + return cmd; + } + public async Task ClaimAsync(NpgsqlConnection connection,JobEnvelope envelope,CancellationToken ct) + { + await using var tx=await connection.BeginTransactionAsync(ct); + var lease=Guid.NewGuid(); + await using var cmd=Command(connection,""" + UPDATE attachment_jobs SET state='processing',attempts=attempts+1,lease_token=@lease, + lease_until=clock_timestamp()+make_interval(secs=>@seconds),error_code=NULL + WHERE id=@id AND attachment_id=@attachment AND pipeline_version=@version AND state='queued' + AND attempts+1=@attempt AND next_at<=clock_timestamp() + RETURNING staging_key,attempts + """,("id",envelope.JobId),("attachment",envelope.AttachmentId),("version",envelope.PipelineVersion), + ("attempt",envelope.Attempt),("lease",lease),("seconds",TimeoutSeconds+5)); + string staging;int attempt; + await using(var r=await cmd.ExecuteReaderAsync(ct)) + { if(!await r.ReadAsync(ct))return null;staging=r.GetString(0);attempt=r.GetInt32(1); } + await using var attachment=Command(connection,""" + UPDATE attachments SET status='processing',error_code=NULL,updated_at=clock_timestamp() WHERE id=@id + RETURNING sha256,size_bytes,content_type + """,("id",envelope.AttachmentId)); + string hash,mime;long size; + await using(var r=await attachment.ExecuteReaderAsync(ct)) + {await r.ReadAsync(ct);hash=r.GetString(0);size=r.GetInt64(1);mime=r.GetString(2);} + await EventAsync(connection,envelope.JobId,"processing",attempt,ct); + await tx.CommitAsync(ct); + return new(envelope.JobId,envelope.AttachmentId,staging,hash,size,mime,attempt,lease); + } + public static async Task EventAsync(NpgsqlConnection connection,Guid id,string name,int attempt,CancellationToken ct) + { + await using var cmd=Command(connection,"INSERT INTO attachment_job_events(job_id,event,attempt) VALUES (@id,@event,@attempt)", + ("id",id),("event",name),("attempt",attempt));await cmd.ExecuteNonQueryAsync(ct); + } + public async Task FailAsync(NpgsqlConnection connection,ClaimedJob job,bool permanent,string code,CancellationToken ct) + { + var final=permanent||job.Attempt>=MaxAttempts; + var status=final?"failed":"queued"; + var error=final && !permanent?"retry_exhausted":code; + var delay=Math.Min(30,Math.Pow(2,job.Attempt))+Random.Shared.NextDouble(); + await using var tx=await connection.BeginTransactionAsync(ct); + await using var cmd=Command(connection,""" + UPDATE attachment_jobs SET state=@status,error_code=@code,lease_token=NULL,lease_until=NULL, + next_at=clock_timestamp()+make_interval(secs=>@delay) + WHERE id=@id AND state='processing' AND lease_token=@lease + """,("id",job.Id),("status",status),("code",error),("delay",delay),("lease",job.Lease)); + if(await cmd.ExecuteNonQueryAsync(ct)==1) + { + await using var attachment=Command(connection,"UPDATE attachments SET status=@status,error_code=@code,updated_at=clock_timestamp() WHERE id=@id", + ("id",job.AttachmentId),("status",status),("code",final?(object)error:DBNull.Value)); + await attachment.ExecuteNonQueryAsync(ct); + if(!final) + { + await using var outbox=Command(connection,""" + INSERT INTO attachment_outbox(job_id,available_at) SELECT id,next_at FROM attachment_jobs WHERE id=@id + ON CONFLICT(job_id) DO UPDATE SET published_at=NULL,available_at=EXCLUDED.available_at + """,("id",job.Id));await outbox.ExecuteNonQueryAsync(ct); + } + await EventAsync(connection,job.Id,final?"failed":"retry_scheduled",job.Attempt,ct); + } + await tx.CommitAsync(ct); + } + public async Task RecoverAsync(CancellationToken ct) + { + await using var connection=await db.OpenConnectionAsync(ct); + var ids=new List(); + await using(var query=Command(connection,"SELECT id FROM attachment_jobs WHERE state='processing' AND lease_until=@max THEN 'failed' ELSE 'queued' END, + error_code=CASE WHEN attempts>=@max THEN 'retry_exhausted' ELSE 'worker_lost' END, + lease_token=NULL,lease_until=NULL,next_at=clock_timestamp() + WHERE id=@id AND state='processing' AND lease_until ReplayAsync(Guid id,CancellationToken ct) + { + await using var connection=await db.OpenConnectionAsync(ct); + await using var tx=await connection.BeginTransactionAsync(ct); + await using var gate=Command(connection,"SELECT pg_advisory_xact_lock(hashtextextended(@scope,0))",("scope","job:"+id));await gate.ExecuteNonQueryAsync(ct); + await using var cmd=Command(connection,""" + UPDATE attachment_jobs SET state='queued',attempts=0,error_code=NULL,next_at=clock_timestamp(),replay_count=replay_count+1, + lease_token=NULL,lease_until=NULL WHERE id=@id AND state='failed' AND staging_deleted_at IS NULL + RETURNING attachment_id + """,("id",id)); + var attachment=await cmd.ExecuteScalarAsync(ct); + if(attachment is Guid attachmentId) + { + await using(var update=Command(connection,"UPDATE attachments SET status='queued',error_code=NULL,updated_at=clock_timestamp() WHERE id=@id",("id",attachmentId)))await update.ExecuteNonQueryAsync(ct); + await using(var outbox=Command(connection,"UPDATE attachment_outbox SET published_at=NULL,available_at=clock_timestamp() WHERE job_id=@id",("id",id)))await outbox.ExecuteNonQueryAsync(ct); + await EventAsync(connection,id,"operator_replay",0,ct); + } + await tx.CommitAsync(ct);Console.WriteLine(attachment is Guid?"Replay scheduled":"No-op: job is not failed or staging has expired"); + return attachment is Guid; + } +} diff --git a/src/Messenger.Api/Pipeline/OutboxDispatcher.cs b/src/Messenger.Api/Pipeline/OutboxDispatcher.cs new file mode 100644 index 00000000..fadecc2b --- /dev/null +++ b/src/Messenger.Api/Pipeline/OutboxDispatcher.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using Messenger.Api.Infrastructure; +using Npgsql; +using RabbitMQ.Client; +namespace Messenger.Api.Pipeline; +public sealed class OutboxDispatcher(NpgsqlDataSource db,S3Storage storage,IConfiguration config) +{ + public async Task RunAsync(CancellationToken ct) + { + var jobs=new JobStore(db,config);var maintenance=new PipelineMaintenance(db,storage,config); + var nextMaintenance=DateTime.MinValue; + while(!ct.IsCancellationRequested) + { + try { + await using var broker=await BrokerConnection.OpenAsync(config,ct); + await using var channel=await broker.CreateChannelAsync(new CreateChannelOptions(publisherConfirmationsEnabled:true,publisherConfirmationTrackingEnabled:true),ct); + await BrokerConnection.DeclareAsync(channel,ct); + while(!ct.IsCancellationRequested && channel.IsOpen) + { + if(DateTime.UtcNow>=nextMaintenance) + { + await jobs.RecoverAsync(ct);await maintenance.CleanupAsync(ct); + nextMaintenance=DateTime.UtcNow.AddSeconds(5); + } + await using var connection=await db.OpenConnectionAsync(ct); + await using var tx=await connection.BeginTransactionAsync(ct); + JobEnvelope? job=null; + await using(var select=JobStore.Command(connection,""" + SELECT j.id,j.attachment_id,j.pipeline_version,j.attempts+1,j.created_at + FROM attachment_outbox o JOIN attachment_jobs j ON j.id=o.job_id + WHERE o.published_at IS NULL AND o.available_at<=clock_timestamp() AND j.state='queued' + ORDER BY o.available_at,o.job_id LIMIT 1 FOR UPDATE OF o SKIP LOCKED + """)) + await using(var r=await select.ExecuteReaderAsync(ct)) + if(await r.ReadAsync(ct))job=new(1,r.GetGuid(0),"attachment.process.v1",r.GetGuid(1),r.GetInt32(2),r.GetInt32(3),r.GetDateTime(4).ToString("O")); + if(job is null) {await tx.CommitAsync(ct);await Task.Delay(100,ct);continue;} + using var confirmation=CancellationTokenSource.CreateLinkedTokenSource(ct);confirmation.CancelAfter(TimeSpan.FromSeconds(5)); + await channel.BasicPublishAsync("",BrokerConnection.Queue,true, + new BasicProperties {Persistent=true,ContentType="application/json",MessageId=job.JobId.ToString()}, + JsonSerializer.SerializeToUtf8Bytes(job,JobJson.Options),confirmation.Token); + await FaultInjection.HitAsync(config,"after_publish",job.JobId,ct); + await using var mark=JobStore.Command(connection,"UPDATE attachment_outbox SET published_at=clock_timestamp() WHERE job_id=@id",("id",job.JobId)); + await mark.ExecuteNonQueryAsync(ct);await JobStore.EventAsync(connection,job.JobId,"published",job.Attempt,ct);await tx.CommitAsync(ct); + } + } catch(OperationCanceledException) when(ct.IsCancellationRequested) {break;} + catch(Exception ex) {Console.WriteLine("Outbox deferred: "+ex.GetType().Name);await Task.Delay(2000,ct);} + } + } +} diff --git a/src/Messenger.Api/Pipeline/PipelineMaintenance.cs b/src/Messenger.Api/Pipeline/PipelineMaintenance.cs new file mode 100644 index 00000000..543b5e23 --- /dev/null +++ b/src/Messenger.Api/Pipeline/PipelineMaintenance.cs @@ -0,0 +1,54 @@ +using Amazon.S3.Model; +using Messenger.Api.Data; +using Messenger.Api.Infrastructure; +using Npgsql; +namespace Messenger.Api.Pipeline; +public sealed class PipelineMaintenance(NpgsqlDataSource db,S3Storage storage,IConfiguration config) +{ + public async Task CleanupAsync(CancellationToken ct) + { + await using var connection=await db.OpenConnectionAsync(ct); + var rows=new List<(Guid Id,string Key)>(); + var retention=Math.Max(3600,config.GetValue("STAGING_RETENTION_SECONDS",86400)); + await using(var cmd=JobStore.Command(connection,""" + SELECT id,staging_key FROM attachment_jobs WHERE staging_deleted_at IS NULL + AND (state='ready' OR state='failed' AND created_at@retention)) LIMIT 20 + """,("retention",retention))) + await using(var r=await cmd.ExecuteReaderAsync(ct))while(await r.ReadAsync(ct))rows.Add((r.GetGuid(0),r.GetString(1))); + foreach(var row in rows) + { + await using var tx=await connection.BeginTransactionAsync(ct); + await using var gate=JobStore.Command(connection,"SELECT pg_try_advisory_xact_lock(hashtextextended(@scope,0))",("scope","job:"+row.Id)); + if(!(bool)(await gate.ExecuteScalarAsync(ct))!)continue; + await using var check=JobStore.Command(connection,""" + SELECT state FROM attachment_jobs WHERE id=@id AND staging_deleted_at IS NULL + AND (state='ready' OR state='failed' AND created_at@retention)) FOR UPDATE + """,("id",row.Id),("retention",retention)); + if(await check.ExecuteScalarAsync(ct) is not string)continue; + await storage.Client.DeleteObjectAsync(storage.Bucket,row.Key,ct); + await using var mark=JobStore.Command(connection,"UPDATE attachment_jobs SET staging_deleted_at=clock_timestamp() WHERE id=@id",("id",row.Id)); + await mark.ExecuteNonQueryAsync(ct);await tx.CommitAsync(ct); + } + } + public async Task ReconcileStagingAsync(bool apply,CancellationToken ct) + { + var actions=new List(); + await using var connection=await db.OpenConnectionAsync(ct); + await using var tx=await connection.BeginTransactionAsync(ct); + await using(var gate=JobStore.Command(connection,$"SELECT pg_advisory_xact_lock({AppDatabase.AttachmentGate})"))await gate.ExecuteNonQueryAsync(ct); + string? continuation=null; + do { + var page=await storage.Client.ListObjectsV2Async(new ListObjectsV2Request {BucketName=storage.Bucket,Prefix="staging/",MaxKeys=1000,ContinuationToken=continuation},ct); + foreach(var obj in page.S3Objects ?? []) + { + if(obj.LastModified is null || obj.LastModified.Value.ToUniversalTime()>DateTime.UtcNow.AddHours(-1))continue; + await using var check=JobStore.Command(connection,"SELECT EXISTS(SELECT 1 FROM attachment_jobs WHERE staging_key=@key)",("key",obj.Key)); + if(!(bool)(await check.ExecuteScalarAsync(ct))!) { + actions.Add(obj.Key);if(apply)await storage.Client.DeleteObjectAsync(storage.Bucket,obj.Key,ct); + } + } + continuation=page.IsTruncated==true?page.NextContinuationToken:null; + }while(continuation is not null); + await tx.CommitAsync(ct);Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(new {mode=apply?"apply":"dry-run",old_untracked_staging=actions})); + } +} diff --git a/src/Messenger.Api/Pipeline/PipelineWorker.cs b/src/Messenger.Api/Pipeline/PipelineWorker.cs new file mode 100644 index 00000000..1040a745 --- /dev/null +++ b/src/Messenger.Api/Pipeline/PipelineWorker.cs @@ -0,0 +1,125 @@ +using System.Diagnostics; +using System.Text.Json; +using System.Security.Cryptography; +using Amazon.S3.Model; +using Messenger.Api.Contracts; +using Messenger.Api.Infrastructure; +using Npgsql; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; +namespace Messenger.Api.Pipeline; +public sealed class PipelineWorker(NpgsqlDataSource db,S3Storage storage,IConfiguration config) +{ + private readonly JobStore jobs=new(db,config); + public async Task RunAsync(CancellationToken ct) + { + var concurrency=Math.Clamp(config.GetValue("WORKER_CONCURRENCY",2),1,8); + var prefetch=(ushort)Math.Clamp(config.GetValue("WORKER_PREFETCH",1),1,16); + while(!ct.IsCancellationRequested) + { + try { + await using var connection=await BrokerConnection.OpenAsync(config,ct); + var channels=new List(); + try { + for(var i=0;i { + try { + var envelope=JsonSerializer.Deserialize(delivery.Body.Span,JobJson.Options); + if(envelope is not null && envelope.SchemaVersion==1 && envelope.Type=="attachment.process.v1" && envelope.PipelineVersion==1) + await ProcessAsync(envelope,ct); + await channel.BasicAckAsync(delivery.DeliveryTag,false,ct); + } catch(JsonException) { await channel.BasicRejectAsync(delivery.DeliveryTag,false,ct); } + catch(OperationCanceledException) when(ct.IsCancellationRequested) { } + catch(Exception ex) { + Console.WriteLine("Delivery deferred: "+ex.GetType().Name); + try {await channel.BasicNackAsync(delivery.DeliveryTag,false,true,ct);}catch { } + await Task.Delay(500,ct); + } + }; + await channel.BasicConsumeAsync(BrokerConnection.Queue,false,consumer,ct); + } + Console.WriteLine($"Worker ready: concurrency={concurrency}, prefetch={prefetch}"); + while(!ct.IsCancellationRequested && connection.IsOpen && channels.All(c=>c.IsOpen))await Task.Delay(500,ct); + } finally { foreach(var channel in channels)await channel.DisposeAsync(); } + } catch(OperationCanceledException) when(ct.IsCancellationRequested) {break;} + catch(Exception ex) {Console.WriteLine("Worker connection unavailable: "+ex.GetType().Name);await Task.Delay(2000,ct);} + } + } + private async Task ProcessAsync(JobEnvelope envelope,CancellationToken stop) + { + await using var connection=await db.OpenConnectionAsync(stop); + await using var gate=JobStore.Command(connection,"SELECT pg_try_advisory_lock(hashtextextended(@scope,0))",("scope","job:"+envelope.JobId)); + if(!(bool)(await gate.ExecuteScalarAsync(stop))!)return; + try { + var job=await jobs.ClaimAsync(connection,envelope,stop);if(job is null)return; + Console.WriteLine($"Processing job {job.Id}, attempt {job.Attempt}"); + var directory=Path.Combine(Path.GetTempPath(),"job-"+Guid.NewGuid().ToString("N"));Directory.CreateDirectory(directory); + using var timeout=CancellationTokenSource.CreateLinkedTokenSource(stop);timeout.CancelAfter(TimeSpan.FromSeconds(jobs.TimeoutSeconds)); + var ct=timeout.Token; + try { + var input=Path.Combine(directory,"original"); + using(var source=await storage.Client.GetObjectAsync(storage.Bucket,job.StagingKey,ct)) + { + if(source.ContentLength>10485760)throw new InvalidDataException("invalid_image"); + await using var file=File.Create(input);var buffer=new byte[65536];long count=0; + while(true) { + var n=await source.ResponseStream.ReadAsync(buffer,ct);if(n==0)break; + count+=n;if(count>10485760)throw new InvalidDataException("invalid_image"); + await file.WriteAsync(buffer.AsMemory(0,n),ct); + } + } + await using(var source=File.OpenRead(input)) + if(source.Length!=job.Size || Convert.ToHexString(await SHA256.HashDataAsync(source,ct)).ToLowerInvariant()!=job.Sha256) + throw new InvalidDataException("invalid_image"); + var render=new ProcessStartInfo("dotnet") {UseShellExecute=false,RedirectStandardOutput=true,RedirectStandardError=true}; + render.ArgumentList.Add(typeof(PipelineWorker).Assembly.Location);render.ArgumentList.Add("--render-image"); + render.ArgumentList.Add(input);render.ArgumentList.Add(directory); + using(var process=Process.Start(render)!) + { + var stdout=process.StandardOutput.ReadToEndAsync(ct);var stderr=process.StandardError.ReadToEndAsync(ct); + try { await process.WaitForExitAsync(ct);await Task.WhenAll(stdout,stderr); } + catch { if(!process.HasExited)process.Kill(true);await process.WaitForExitAsync(CancellationToken.None);throw; } + if(process.ExitCode==2)throw new InvalidDataException("invalid_image"); + if(process.ExitCode!=0)throw new IOException("renderer_unavailable"); + } + var variants=JsonSerializer.Deserialize(await File.ReadAllTextAsync(Path.Combine(directory,"variants.json"),ct),JobJson.Options)!; + await PutAsync($"originals/{job.AttachmentId:N}/v1",input,job.ContentType,ct); + foreach(var variant in variants)await PutAsync($"variants/{job.AttachmentId:N}/v1/{variant.Name}",Path.Combine(directory,variant.Name),variant.ContentType,ct); + await FaultInjection.HitAsync(config,"after_objects",job.Id,ct); + await using(var tx=await connection.BeginTransactionAsync(ct)) + { + await using var done=JobStore.Command(connection,""" + UPDATE attachment_jobs SET state='ready',completed_at=clock_timestamp(),error_code=NULL,lease_token=NULL,lease_until=NULL + WHERE id=@id AND state='processing' AND lease_token=@lease AND lease_until>clock_timestamp() + """,("id",job.Id),("lease",job.Lease)); + if(await done.ExecuteNonQueryAsync(ct)!=1)throw new OperationCanceledException("Lease expired"); + await using var result=JobStore.Command(connection,"UPDATE attachments SET status='ready',variants=CAST(@variants AS jsonb),error_code=NULL,updated_at=clock_timestamp() WHERE id=@id", + ("id",job.AttachmentId),("variants",JsonSerializer.Serialize(variants,JobJson.Options)));await result.ExecuteNonQueryAsync(ct); + await JobStore.EventAsync(connection,job.Id,"ready",job.Attempt,ct);await tx.CommitAsync(ct); + } + await FaultInjection.HitAsync(config,"after_commit",job.Id,stop); + Console.WriteLine($"Ready job {job.Id}"); + } catch(Exception ex) when(!stop.IsCancellationRequested) { + var permanent=ex is InvalidDataException || ex is Amazon.S3.AmazonS3Exception s3 && s3.StatusCode==System.Net.HttpStatusCode.NotFound; + var code=permanent?"invalid_image":ex is OperationCanceledException?"processing_timeout":"dependency_unavailable"; + await jobs.FailAsync(connection,job,permanent,code,stop); + Console.WriteLine($"Job {job.Id}: {code} ({ex.GetType().Name})"); + } + finally {Directory.Delete(directory,true);} + } + finally { + await using var unlock=JobStore.Command(connection,"SELECT pg_advisory_unlock(hashtextextended(@scope,0))",("scope","job:"+envelope.JobId)); + await unlock.ExecuteNonQueryAsync(CancellationToken.None); + } + } + private async Task PutAsync(string key,string path,string mime,CancellationToken ct) + { + await using var file=File.OpenRead(path); + await storage.Client.PutObjectAsync(new PutObjectRequest {BucketName=storage.Bucket,Key=key,InputStream=file,ContentType=mime,UseChunkEncoding=false,AutoCloseStream=false},ct); + } +} diff --git a/src/Messenger.Api/Pipeline/ProcessPresence.cs b/src/Messenger.Api/Pipeline/ProcessPresence.cs new file mode 100644 index 00000000..d752cbab --- /dev/null +++ b/src/Messenger.Api/Pipeline/ProcessPresence.cs @@ -0,0 +1,17 @@ +using Npgsql; +namespace Messenger.Api.Pipeline; +public static class ProcessPresence +{ + public static async Task RunAsync(NpgsqlDataSource source,string role,CancellationToken ct) + { + try { + while(!ct.IsCancellationRequested) { + try { + await using var cmd=source.CreateCommand("INSERT INTO pipeline_presence(role,seen_at) VALUES(@role,clock_timestamp()) ON CONFLICT(role) DO UPDATE SET seen_at=EXCLUDED.seen_at"); + cmd.Parameters.AddWithValue("role",role);await cmd.ExecuteNonQueryAsync(ct); + } catch(Exception) when(!ct.IsCancellationRequested) { } + await Task.Delay(2000,ct); + } + } catch(OperationCanceledException) when(ct.IsCancellationRequested) { } + } +} diff --git a/src/Messenger.Api/Program.cs b/src/Messenger.Api/Program.cs new file mode 100644 index 00000000..0b9bab6d --- /dev/null +++ b/src/Messenger.Api/Program.cs @@ -0,0 +1,131 @@ +using Messenger.Api.Pipeline; +using System.Text.Json; +using System.Text.Json.Serialization; +using Messenger.Api.Data; +using Messenger.Api.Endpoints; +using Messenger.Api.Infrastructure; +using Messenger.Api.Middleware; +using Messenger.Api.Services; +using Npgsql; +using StackExchange.Redis; + +ThreadPool.GetMinThreads(out var workerThreads, out var completionThreads); +ThreadPool.SetMinThreads(Math.Max(8, workerThreads), completionThreads); + +if(args.Length==3 && args[0]=="--render-image") +{ + try {ImageRenderer.Render(args[1],args[2]);} + catch(InvalidDataException) {Environment.ExitCode=2;} + catch {Environment.ExitCode=3;} + return; +} +var builder = WebApplication.CreateBuilder(args); + +if(args.Contains("--bootstrap")) +{ + using var storage=new S3Storage(builder.Configuration); + try { await storage.Client.GetBucketLocationAsync(storage.Bucket); } + catch(Amazon.S3.AmazonS3Exception ex) when(ex.StatusCode==System.Net.HttpStatusCode.NotFound) + { await storage.Client.PutBucketAsync(storage.Bucket); } + Console.WriteLine("Private bucket ready"); + return; +} + +var host = Environment.GetEnvironmentVariable("APP_HOST") ?? "0.0.0.0"; +var port = Environment.GetEnvironmentVariable("APP_PORT") ?? "8080"; +builder.WebHost.UseUrls($"http://{host}:{port}"); + +builder.Services.ConfigureHttpJsonOptions(options => +{ + options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower; + options.SerializerOptions.UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow; +}); + +var databaseUrlFile = Environment.GetEnvironmentVariable("DATABASE_URL_FILE"); +var databaseUrl = databaseUrlFile is not null + ? File.ReadAllText(databaseUrlFile).Trim() + : Environment.GetEnvironmentVariable("DATABASE_URL"); +if (string.IsNullOrWhiteSpace(databaseUrl)) + throw new InvalidOperationException("Set DATABASE_URL_FILE or DATABASE_URL before starting the API"); + +var connectionString = new NpgsqlConnectionStringBuilder(databaseUrl) +{ + MaxPoolSize = int.TryParse(builder.Configuration["DB_POOL_SIZE"], out var poolSize) && poolSize > 0 ? poolSize : 20, + Timeout = 5, + ApplicationName = "messenger-api", + CommandTimeout = 10 +}; +var dataSource = NpgsqlDataSource.Create(connectionString.ConnectionString); +if (args.Contains("--migrate", StringComparer.Ordinal)) +{ + await DatabaseMigrator.ApplyAsync(dataSource); + await dataSource.DisposeAsync(); + return; +} +if(args.Contains("--worker") || args.Contains("--dispatcher") || args.Contains("--replay") || args.Contains("--redeliver") || args.Contains("--reconcile")) +{ + using var storage=new S3Storage(builder.Configuration); + using var cancellation=new CancellationTokenSource(); + Console.CancelKeyPress+=(_,e)=>{e.Cancel=true;cancellation.Cancel();}; + using var signal=System.Runtime.InteropServices.PosixSignalRegistration.Create(System.Runtime.InteropServices.PosixSignal.SIGTERM,ctx=>{ctx.Cancel=true;cancellation.Cancel();}); + var heartbeat=(args.Contains("--worker") || args.Contains("--dispatcher")) + ? ProcessPresence.RunAsync(dataSource,args.Contains("--worker")?"worker":"dispatcher",cancellation.Token) : Task.CompletedTask; + try { + if(args.Contains("--worker"))await new PipelineWorker(dataSource,storage,builder.Configuration).RunAsync(cancellation.Token); + else if(args.Contains("--dispatcher"))await new OutboxDispatcher(dataSource,storage,builder.Configuration).RunAsync(cancellation.Token); + else if(args.Contains("--redeliver"))await JobOperations.RedeliverAsync(dataSource,builder.Configuration,Guid.Parse(args[Array.IndexOf(args,"--redeliver")+1]),cancellation.Token); + else if(args.Contains("--replay"))await new JobStore(dataSource,builder.Configuration).ReplayAsync(Guid.Parse(args[Array.IndexOf(args,"--replay")+1]),cancellation.Token); + else await new PipelineMaintenance(dataSource,storage,builder.Configuration).ReconcileStagingAsync(args.Contains("--apply"),cancellation.Token); + }catch(OperationCanceledException) when(cancellation.IsCancellationRequested) { } + cancellation.Cancel();await heartbeat; + await dataSource.DisposeAsync();return; +} +builder.Services.AddSingleton(dataSource); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.Configure(options => options.ShutdownTimeout = TimeSpan.FromSeconds(8)); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +var redisOptions = ConfigurationOptions.Parse(builder.Configuration["REDIS_URL"] ?? "localhost:6379"); +redisOptions.AbortOnConnectFail = false; +redisOptions.ConnectTimeout = 500; +redisOptions.ConnectRetry = 0; +redisOptions.AsyncTimeout = 500; +redisOptions.SyncTimeout = 500; +redisOptions.BacklogPolicy = BacklogPolicy.FailFast; +builder.Services.AddSingleton(await ConnectionMultiplexer.ConnectAsync(redisOptions)); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddHostedService(services => services.GetRequiredService()); + +var app = builder.Build(); +app.Lifetime.ApplicationStopping.Register(app.Services.GetRequiredService().Stop); + +app.UseMiddleware(); +app.UseMiddleware(); +app.UseMiddleware(); +app.UseMiddleware(); +app.UseDefaultFiles(); +app.UseStaticFiles(new StaticFileOptions +{ + OnPrepareResponse = context => context.Context.Response.Headers.CacheControl = "no-cache" +}); + +app.MapHealthEndpoints(); +app.MapUserEndpoints(); +app.MapAuthEndpoints(); +app.MapChatEndpoints(); +app.MapMessageEndpoints(); +app.MapAttachmentEndpoints(); +app.MapUiEndpoints(); +app.MapOperationsEndpoints(); +app.MapReactionEndpoints(); + +app.Run(); diff --git a/src/Messenger.Api/Properties/launchSettings.json b/src/Messenger.Api/Properties/launchSettings.json new file mode 100644 index 00000000..ac9df787 --- /dev/null +++ b/src/Messenger.Api/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5052", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7253;http://localhost:5052", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/Messenger.Api/Services/ApiSupport.cs b/src/Messenger.Api/Services/ApiSupport.cs new file mode 100644 index 00000000..7936d712 --- /dev/null +++ b/src/Messenger.Api/Services/ApiSupport.cs @@ -0,0 +1,93 @@ +using System.Globalization; +using System.Text.Json; +using Messenger.Api.Contracts; + +namespace Messenger.Api.Services; + +public static class ApiSupport +{ + public static async Task<(T? Value, IResult? Error)> ReadJsonAsync(HttpContext context) + { + if (!context.Request.HasJsonContentType()) + return (default, ApiError(context, 400, "invalid_request", "Требуется application/json")); + + try + { + var value = await context.Request.ReadFromJsonAsync(); + return value is null + ? (default, ApiError(context, 400, "invalid_request", "Тело запроса обязательно")) + : (value, null); + } + catch (Exception exception) when (exception is JsonException or NotSupportedException or BadHttpRequestException) + { + return (default, ApiError(context, 400, "invalid_request", "Некорректный JSON")); + } + } + + public static IResult Unauthorized(HttpContext context, bool basic = false) + { + context.Response.Headers["WWW-Authenticate"] = basic + ? "Basic realm=\"messenger\", charset=\"UTF-8\"" + : "Bearer realm=\"messenger\""; + return ApiError(context, 401, "unauthorized", "Требуется действительная сессия"); + } + + public static (ChatCursor? Cursor, DateTime? Time, IResult? Error) ParseCursor( + HttpContext context, string scope) + { + if (!context.Request.Query.TryGetValue("cursor", out var value)) + return (null, null, null); + + var cursor = CursorService.DecodeChatCursor(value.ToString()); + if (cursor is null || cursor.Scope != scope || cursor.Id == Guid.Empty || + !DateTime.TryParse(cursor.CreatedAt, CultureInfo.InvariantCulture, + DateTimeStyles.RoundtripKind, out var time) || time.Kind != DateTimeKind.Utc) + return (null, null, ApiError(context, 400, "invalid_request", "Некорректный cursor")); + + return (cursor, time, null); + } + + public static (int Limit, IResult? Error) ParseLimit(HttpContext context) + { + const int defaultLimit = 50; + + if (!context.Request.Query.TryGetValue("limit", out var value)) + return (defaultLimit, null); + + if (!int.TryParse(value.ToString(), out var limit) || + limit < 1 || + limit > 100) + { + return ( + 0, + ApiError(context, 400, "invalid_request", "limit должен быть от 1 до 100") + ); + } + + return (limit, null); + } + + public static IResult ApiError( + HttpContext context, + int status, + string code, + string message) + { + var requestId = + context.Items["RequestId"]?.ToString() + ?? Guid.NewGuid().ToString(); + + return Results.Json( + new + { + error = new + { + code, + message, + request_id = requestId + } + }, + statusCode: status + ); + } +} diff --git a/src/Messenger.Api/Services/AttachmentException.cs b/src/Messenger.Api/Services/AttachmentException.cs new file mode 100644 index 00000000..cb8dc39b --- /dev/null +++ b/src/Messenger.Api/Services/AttachmentException.cs @@ -0,0 +1,6 @@ +namespace Messenger.Api.Services; +public sealed class AttachmentException(int status, string code, string message) : Exception(message) +{ + public int Status { get; } = status; + public string Code { get; } = code; +} diff --git a/src/Messenger.Api/Services/AttachmentService.cs b/src/Messenger.Api/Services/AttachmentService.cs new file mode 100644 index 00000000..77f0cad3 --- /dev/null +++ b/src/Messenger.Api/Services/AttachmentService.cs @@ -0,0 +1,89 @@ +using System.Security.Cryptography; +using Amazon.S3.Model; +using Messenger.Api.Contracts; +using Messenger.Api.Data; +using Messenger.Api.Infrastructure; +using Npgsql; +using SkiaSharp; +namespace Messenger.Api.Services; +public sealed class AttachmentService(S3Storage storage, NpgsqlDataSource db, IConfiguration config) +{ + public const long MaxBytes=10*1024*1024; + private readonly SemaphoreSlim slots=new(1,1); + public async Task UploadAsync(HttpRequest request, Guid chat, Guid actor, string name) + { + using var timeout=CancellationTokenSource.CreateLinkedTokenSource(request.HttpContext.RequestAborted); + timeout.CancelAfter(TimeSpan.FromSeconds(90)); + var ct=timeout.Token; + if (request.ContentLength>MaxBytes) throw new AttachmentException(413,"payload_too_large","Файл больше 10 MiB"); + await slots.WaitAsync(ct); + var path=Path.Combine(Path.GetTempPath(),Guid.NewGuid().ToString("N")); + try + { + await using var file=new FileStream(path,FileMode.CreateNew,FileAccess.ReadWrite,FileShare.None, + 65536,FileOptions.Asynchronous|FileOptions.DeleteOnClose); + var buffer=new byte[65536]; long size=0; + using var digest=IncrementalHash.CreateHash(HashAlgorithmName.SHA256); + while(true) + { + var count=await request.Body.ReadAsync(buffer,ct); if(count==0) break; + size+=count; + if(size>MaxBytes) throw new AttachmentException(413,"payload_too_large","Файл больше 10 MiB"); + digest.AppendData(buffer,0,count); await file.WriteAsync(buffer.AsMemory(0,count),ct); + } + if(size==0) throw new AttachmentException(415,"unsupported_media_type","Пустое изображение"); + string mime; + file.Position=0; + using (var stream=new SKManagedStream(file,false)) + using (var codec=SKCodec.Create(stream)) + { + if(codec is null) throw new AttachmentException(415,"unsupported_media_type","Повреждённое изображение"); + var info=codec.Info; + if((long)info.Width*info.Height>40_000_000) + throw new AttachmentException(413,"payload_too_large","Слишком много пикселей"); + mime=codec.EncodedFormat switch {SKEncodedImageFormat.Png=>"image/png",SKEncodedImageFormat.Jpeg=>"image/jpeg",_=>""}; + if(mime=="" || request.ContentType?.Split(';')[0].Trim()!=mime) + throw new AttachmentException(415,"unsupported_media_type","Требуется PNG или JPEG с верным MIME"); + using var bitmap=new SKBitmap(new SKImageInfo(info.Width,info.Height,SKColorType.Rgba8888,SKAlphaType.Premul)); + if(codec.GetPixels(bitmap.Info,bitmap.GetPixels())!=SKCodecResult.Success) + throw new AttachmentException(415,"unsupported_media_type","Повреждённое изображение"); + } + var id=Guid.NewGuid(); var job=Guid.NewGuid(); var staging=$"staging/{id:N}"; var key=$"originals/{id:N}/v1"; var now=UtcClock.Now(); + var sha=Convert.ToHexString(digest.GetHashAndReset()).ToLowerInvariant(); + await using var connection=await db.OpenConnectionAsync(ct); + await using var transaction=await connection.BeginTransactionAsync(ct); + await using(var gate=new NpgsqlCommand($"SELECT pg_advisory_xact_lock_shared({AppDatabase.AttachmentGate})",connection,transaction)) + await gate.ExecuteNonQueryAsync(ct); + await using(var gate=new NpgsqlCommand("SELECT pg_advisory_xact_lock(48260105)",connection,transaction)) + await gate.ExecuteNonQueryAsync(ct); + var capacity=Math.Clamp(config.GetValue("PIPELINE_CAPACITY",128),1,10000); + await using(var count=new NpgsqlCommand("SELECT count(*) FROM attachment_jobs WHERE state IN ('queued','processing')",connection,transaction)) + if((long)(await count.ExecuteScalarAsync(ct))!>=capacity) + { + request.HttpContext.Response.Headers.RetryAfter="3"; + throw new AttachmentException(429,"rate_limited","Очередь заполнена. Попробуйте через несколько секунд"); + } + file.Position=0; + await storage.Client.PutObjectAsync(new PutObjectRequest {BucketName=storage.Bucket,Key=staging, + InputStream=file,ContentType=mime,AutoCloseStream=false,UseChunkEncoding=false},ct); + await using(var insert=new NpgsqlCommand(""" + INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,status,created_at,updated_at) + VALUES (@id,@chat,@actor,@name,@mime,@size,@sha,@key,'queued',@now,@now); + INSERT INTO attachment_jobs(id,attachment_id,staging_key,state) VALUES (@job,@id,@staging,'queued'); + INSERT INTO attachment_outbox(job_id) VALUES (@job); + INSERT INTO attachment_job_events(job_id,event,attempt) VALUES (@job,'accepted',0) + """,connection,transaction)) + { + insert.Parameters.AddWithValue("job",job);insert.Parameters.AddWithValue("staging",staging); + insert.Parameters.AddWithValue("id",id);insert.Parameters.AddWithValue("chat",chat); + insert.Parameters.AddWithValue("actor",actor);insert.Parameters.AddWithValue("name",name); + insert.Parameters.AddWithValue("mime",mime);insert.Parameters.AddWithValue("size",size); + insert.Parameters.AddWithValue("sha",sha);insert.Parameters.AddWithValue("key",key);insert.Parameters.AddWithValue("now",now); + await insert.ExecuteNonQueryAsync(ct); + } + await transaction.CommitAsync(ct); + return new(id,chat,actor,name,mime,size,sha,"queued",[],null,now.ToString("O"),now.ToString("O")); + } + finally { slots.Release(); File.Delete(path); } + } +} diff --git a/src/Messenger.Api/Services/ChatService.cs b/src/Messenger.Api/Services/ChatService.cs new file mode 100644 index 00000000..38979f01 --- /dev/null +++ b/src/Messenger.Api/Services/ChatService.cs @@ -0,0 +1,16 @@ +using Messenger.Api.Contracts; +using Messenger.Api.Data; + +namespace Messenger.Api.Services; + +public sealed class ChatService(AppDatabase database) +{ + public async Task CreateAsync(string title, List memberIds) => + (await database.CreateChatAsync(title, memberIds))?.ToDto(); + + public async Task FindForMemberAsync(Guid chatId, Guid userId) => + (await database.FindChatForMemberAsync(chatId, userId))?.ToDto(); + + public async Task> ListAsync(Guid userId, DateTime? cursorTime, Guid? cursorId, int count) => + (await database.ListChatsAsync(userId, cursorTime, cursorId, count)).Select(chat => chat.ToDto()).ToList(); +} diff --git a/src/Messenger.Api/Services/CursorService.cs b/src/Messenger.Api/Services/CursorService.cs new file mode 100644 index 00000000..f260e9e7 --- /dev/null +++ b/src/Messenger.Api/Services/CursorService.cs @@ -0,0 +1,49 @@ +using System.Text; +using System.Text.Json; +using Messenger.Api.Contracts; + +namespace Messenger.Api.Services; + +public static class CursorService +{ + public static string EncodeChatCursor(ChatCursor cursor) + { + var json = JsonSerializer.Serialize(cursor); + + return Convert.ToBase64String(Encoding.UTF8.GetBytes(json)) + .TrimEnd('=') + .Replace('+', '-') + .Replace('/', '_'); + } + + public static ChatCursor? DecodeChatCursor(string cursor) + { + try + { + var base64 = cursor + .Replace('-', '+') + .Replace('_', '/'); + + switch (base64.Length % 4) + { + case 2: + base64 += "=="; + break; + case 3: + base64 += "="; + break; + case 1: + return null; + } + + var bytes = Convert.FromBase64String(base64); + var json = Encoding.UTF8.GetString(bytes); + + return JsonSerializer.Deserialize(json); + } + catch + { + return null; + } + } +} diff --git a/src/Messenger.Api/Services/MessageService.cs b/src/Messenger.Api/Services/MessageService.cs new file mode 100644 index 00000000..b36b399c --- /dev/null +++ b/src/Messenger.Api/Services/MessageService.cs @@ -0,0 +1,13 @@ +using Messenger.Api.Contracts; +using Messenger.Api.Data; + +namespace Messenger.Api.Services; + +public sealed class MessageService(AppDatabase database) +{ + public async Task CreateAsync(Guid chatId, Guid senderId, string text, string key, Guid[] attachmentIds) => + (await database.CreateMessageOnceAsync(chatId, senderId, text, key, attachmentIds))?.ToDto(); + + public async Task> ListAsync(Guid chatId, DateTime? cursorTime, Guid? cursorId, int count, bool newestFirst = false) => + (await database.ListMessagesAsync(chatId, cursorTime, cursorId, count, newestFirst)).Select(message => message.ToDto()).ToList(); +} diff --git a/src/Messenger.Api/Services/PasswordService.cs b/src/Messenger.Api/Services/PasswordService.cs new file mode 100644 index 00000000..2d1d4157 --- /dev/null +++ b/src/Messenger.Api/Services/PasswordService.cs @@ -0,0 +1,28 @@ +using System.Security.Cryptography; +using System.Text; +using Isopoh.Cryptography.Argon2; + +namespace Messenger.Api.Services; + +public sealed class PasswordService +{ + public string Hash(string password) + { + var salt = RandomNumberGenerator.GetBytes(16); + var config = new Argon2Config + { + Type = Argon2Type.HybridAddressing, + Version = Argon2Version.Nineteen, + TimeCost = 3, + MemoryCost = 32768, + Lanes = 1, + Threads = 1, + Password = Encoding.UTF8.GetBytes(password), + Salt = salt, + HashLength = 32 + }; + return Argon2.Hash(config); + } + + public bool Verify(string hash, string password) => Argon2.Verify(hash, password); +} diff --git a/src/Messenger.Api/Services/SessionService.cs b/src/Messenger.Api/Services/SessionService.cs new file mode 100644 index 00000000..d9a134fb --- /dev/null +++ b/src/Messenger.Api/Services/SessionService.cs @@ -0,0 +1,65 @@ +using System.Security.Cryptography; +using System.Text; +using Messenger.Api.Contracts; +using Messenger.Api.Data; +using Messenger.Api.Infrastructure; + +namespace Messenger.Api.Services; + +public sealed class SessionService(AppDatabase database, PasswordService passwords, RedisSessionCache cache, IConfiguration configuration) +{ + private readonly int _ttlSeconds = int.TryParse(configuration["SESSION_TTL_SECONDS"], out var ttl) && ttl > 0 + ? ttl : 3600; + + public async Task LoginAsync(string username, string password) + { + var user = await database.FindUserByUsernameAsync(username); + if (user is null || !passwords.Verify(user.PasswordHash, password)) + return null; + + var token = Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)) + .TrimEnd('=').Replace('+', '-').Replace('/', '_'); + var expiresAt = UtcClock.Now().AddSeconds(_ttlSeconds); + var hash = HashToken(token); + await database.CreateSessionAsync(hash, user.Id, expiresAt); + await cache.SetAsync(hash, user.ToDto(), expiresAt); + return new SessionDto(token, "Bearer", expiresAt.ToString("O"), user.ToDto()); + } + + public async Task<(UserDto? User, byte[]? TokenHash)> AuthenticateAsync(HttpContext context) + { + if (!context.Request.Headers.TryGetValue("Authorization", out var value)) + return (null, null); + + var authorization = value.ToString(); + if (!authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) + return (null, null); + + var token = authorization[7..]; + if (token.Length != 43 || token.Any(c => !char.IsAsciiLetterOrDigit(c) && c != '-' && c != '_')) + return (null, null); + + var hash = HashToken(token); + var session = await database.FindActiveSessionAsync(hash); + if (session is null) return (null, null); + + var cached = await cache.GetAsync(hash); + if (cached is not null && cached.User.Id == session.UserId && cached.ExpiresAt == session.ExpiresAt) + return (cached.User, hash); + + var user = await database.FindUserByIdAsync(session.UserId); + if (user is null) return (null, null); + var dto = user.ToDto(); + await cache.SetAsync(hash, dto, session.ExpiresAt); + return (dto, hash); + } + + public async Task RevokeAsync(byte[] tokenHash) + { + var revoked = await database.RevokeSessionAsync(tokenHash); + if (revoked) await cache.RemoveAsync(tokenHash); + return revoked; + } + + private static byte[] HashToken(string token) => SHA256.HashData(Encoding.ASCII.GetBytes(token)); +} diff --git a/src/Messenger.Api/Services/UserService.cs b/src/Messenger.Api/Services/UserService.cs new file mode 100644 index 00000000..6dafd366 --- /dev/null +++ b/src/Messenger.Api/Services/UserService.cs @@ -0,0 +1,24 @@ +using Messenger.Api.Contracts; +using Messenger.Api.Data; +using Npgsql; + +namespace Messenger.Api.Services; + +public sealed class UserService(AppDatabase database, PasswordService passwords) +{ + public async Task> ListForChatAsync(Guid chatId) => + (await database.ListChatUsersAsync(chatId)).Select(user => user.ToDto()).ToList(); + + public async Task RegisterAsync(string username, string displayName, string password) + { + var hash = passwords.Hash(password); + try + { + return (await database.CreateUserAsync(username, displayName, hash)).ToDto(); + } + catch (PostgresException exception) when (exception.SqlState == PostgresErrorCodes.UniqueViolation) + { + return null; + } + } +} diff --git a/src/Messenger.Api/appsettings.Development.json b/src/Messenger.Api/appsettings.Development.json new file mode 100644 index 00000000..ff66ba6b --- /dev/null +++ b/src/Messenger.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/Messenger.Api/appsettings.json b/src/Messenger.Api/appsettings.json new file mode 100644 index 00000000..4d566948 --- /dev/null +++ b/src/Messenger.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/src/Messenger.Api/wwwroot/app.css b/src/Messenger.Api/wwwroot/app.css new file mode 100644 index 00000000..7bc42308 --- /dev/null +++ b/src/Messenger.Api/wwwroot/app.css @@ -0,0 +1,438 @@ +:root { + color-scheme: dark; + --ink: #FFE135; + --muted: #b6b5aa; + --accent: #FFE135; + --accent-hover: #ffeb79; + --paper: #262626; + --surface: #303030; + --soft: #38382c; + --border: #414139; + --danger: #ffaaa0; + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; + color: var(--ink); + background: #262626; + font-size: 15px; +} +* { box-sizing: border-box; } +body { margin: 0; } +button, input, textarea { font: inherit; } +button, a, input, textarea { -webkit-tap-highlight-color: transparent; } +button { cursor: pointer; } +button:disabled { cursor: default; opacity: .5; } +button, textarea, input { outline: none; } +button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; } +input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px #ffe13515; } +a { color: inherit; text-decoration: none; } +[hidden] { display: none !important; } +svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; } +.svg-sprite { position: absolute; width: 0; height: 0; overflow: hidden; } +.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; } +::selection { color: #262626; background: var(--accent); } + +/* Brand and shared controls */ +.brand { display: inline-flex; align-items: center; font-size: 30px; font-weight: 750; letter-spacing: -1.6px; gap: 10px; line-height: 1; } +.brand-dot { color: var(--accent); margin-left: -8px; } +.brand-mark { height: 36px; width: 36px; background: var(--accent); border-radius: 12px; display: flex; flex-direction: column; justify-content: center; gap: 4px; padding: 9px; } +.brand-mark i { display: block; height: 2px; border-radius: 2px; background: #262626; } +.brand-mark i:nth-child(2) { width: 75%; } +.brand-mark i:nth-child(3) { width: 45%; } +.eyebrow { display: block; color: var(--muted); font-size: 10px; font-weight: 650; letter-spacing: 2px; text-transform: uppercase; line-height: 1.6; } +.muted { color: var(--muted); font-size: 14px; line-height: 1.7; } +label { display: grid; gap: 9px; font-size: 12px; font-weight: 550; color: var(--ink); } +input, textarea { width: 100%; color: var(--ink); background: #292929; border: 1px solid #4b4b42; border-radius: 12px; padding: 14px 15px; font-size: 14px; transition: border-color .18s, box-shadow .18s; } +input::placeholder, textarea::placeholder { color: #a6a69a; } +textarea { resize: vertical; line-height: 1.5; } +.field-note { color: var(--muted); font-size: 11px; font-weight: 400; line-height: 1.7; } +.button { display: inline-flex; align-items: center; justify-content: center; gap: 10px; border-radius: 12px; border: 1px solid transparent; padding: 14px 20px; font-weight: 650; font-size: 13px; transition: background .18s, transform .18s; } +.button.primary { background: var(--accent); color: #262626; } +.button.primary:hover { background: var(--accent-hover); transform: translateY(-1px); } +.button.secondary { background: #3a3a32; border-color: var(--border); color: var(--ink); } +.button.secondary:hover { background: #454538; } +.button.wide { width: 100%; } +.icon-button { display: inline-flex; align-items: center; justify-content: center; border: 1px solid transparent; background: transparent; color: var(--muted); width: 36px; height: 36px; border-radius: 11px; flex-shrink: 0; transition: background .18s, color .18s; } +.icon-button:hover { background: #3c3c32; color: var(--accent); } +.icon-button.filled { background: var(--accent); color: #262626; } +.icon-button.filled:hover { background: var(--accent-hover); } +.text-button { background: none; border: 0; color: var(--accent); font-size: 11px; padding: 10px 14px; border-radius: 9px; } +.text-button:hover { background: #ffe1350e; } +.form-notice { color: var(--danger); font-size: 12px; line-height: 1.6; background: #452f2d; border: 1px solid #70453e; border-radius: 10px; padding: 11px 13px; } +.form-notice.success { color: var(--ink); background: var(--soft); border-color: #686032; } + +/* Entry screen */ +.auth-layout { min-height: 100svh; display: grid; grid-template-columns: 1.1fr 1fr; max-width: 1440px; margin: auto; } +.intro { position: relative; min-height: 760px; padding: 50px 64px 40px; overflow: hidden; background: radial-gradient(ellipse at 5% 95%, #ffe13508, transparent 65%), var(--paper); } +.intro::before { content: ""; position: absolute; inset: auto auto -270px -250px; width: 760px; height: 760px; border: 1px solid #ffe13520; border-radius: 50%; pointer-events: none; } +.intro::after { content: ""; position: absolute; right: -190px; top: 90px; width: 440px; height: 440px; border: 1px solid #ffe13512; border-radius: 50%; pointer-events: none; } +.intro-copy { margin-top: 82px; position: relative; z-index: 1; } +.intro-copy .eyebrow { color: var(--accent); padding-left: 22px; position: relative; } +.intro-copy .eyebrow::before { content: ""; width: 7px; height: 7px; background: var(--accent); border-radius: 50%; position: absolute; left: 0; top: 5px; } +.intro h1 { font-size: clamp(48px, 5vw, 72px); line-height: 1.04; font-weight: 700; letter-spacing: -3.5px; margin: 22px 0 26px; } +.intro em, .welcome em { font-family: Georgia, "Times New Roman", serif; font-weight: 400; color: var(--accent); } +.intro-copy p { color: var(--muted); line-height: 1.8; font-size: 15px; } +.conversation-art { position: relative; width: 330px; height: 165px; margin: 38px 0 25px 34px; z-index: 1; } +.art-bubble { position: absolute; padding: 17px 22px 13px; border-radius: 18px; font-size: 14px; box-shadow: 0 12px 30px #00000020; } +.art-bubble span { display: block; font-size: 10px; text-align: right; margin-top: 8px; opacity: .65; } +.art-bubble b { font-weight: 500; } +.art-one { background: #353530; border: 1px solid #4a4a3c; left: 0; top: 0; transform: rotate(-4deg); border-bottom-left-radius: 5px; min-width: 195px; } +.art-two { background: var(--accent); color: #262626; right: 0; top: 67px; transform: rotate(3deg); border-bottom-right-radius: 5px; min-width: 218px; } +.art-typing { position: absolute; bottom: 0; left: 15px; display: flex; gap: 5px; border-radius: 12px; background: #353530; border: 1px solid var(--border); padding: 13px 19px; } +.art-typing i { background: var(--accent); width: 5px; height: 5px; border-radius: 50%; } +.art-orbit { position: absolute; right: -45px; top: 5px; width: 11px; height: 11px; background: var(--accent); border-radius: 50%; box-shadow: 0 0 0 9px #ffe13508; } +.intro-footer { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 9px; margin-top: 38px; position: relative; } +.status-dot { height: 6px; width: 6px; background: var(--accent); border-radius: 50%; } +.auth-side { background: #2c2c2a; border-left: 1px solid var(--border); display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 50px 64px 25px; min-height: 760px; } +.auth-card { width: 100%; max-width: 360px; } +.auth-switch { display: flex; padding: 5px; background: #222222; border: 1px solid #404037; border-radius: 14px; margin-bottom: 42px; } +.auth-switch button { width: 50%; background: transparent; border: 0; border-radius: 9px; color: var(--muted); padding: 11px; font-size: 12px; font-weight: 600; } +.auth-switch .active { background: var(--accent); color: #262626; box-shadow: 0 3px 12px #00000018; } +.auth-card h2 { font-weight: 650; font-size: 40px; letter-spacing: -1.7px; line-height: 1.15; margin: 12px 0; } +.auth-card .muted { margin: 13px 0 30px; } +.auth-card form { display: grid; gap: 20px; } +.auth-card .wide { margin-top: 5px; justify-content: space-between; } +.auth-card .wide > span { font-size: 21px; line-height: 1; } +.auth-footnote { font-size: 11px; color: var(--muted); margin: 24px 0; text-align: center; } +.auth-bottom { color: var(--muted); font-size: 10px; letter-spacing: .6px; margin-top: 40px; } + +/* Conversations */ +.app-shell { display: grid; grid-template-columns: 310px minmax(0,1fr); height: calc(100svh - 44px); max-width: 1440px; margin: 22px auto; border: 1px solid #46463c; border-radius: 24px; overflow: hidden; box-shadow: 0 20px 70px #00000025; background: var(--paper); } +.sidebar { min-height: 0; background: #222222; border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 29px 19px 0; } +.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 0 9px; } +.sidebar .brand { font-size: 28px; } +.sidebar .brand-mark { width: 32px; height: 32px; border-radius: 10px; } +.sidebar-heading { display: flex; justify-content: space-between; align-items: center; padding: 35px 9px 22px; } +.sidebar-heading .eyebrow { font-size: 9px; letter-spacing: 1.7px; } +.sidebar-heading h2 { margin: 6px 0 0; font-size: 22px; font-weight: 650; letter-spacing: -.7px; } +.count { font-size: 11px; font-weight: 600; background: #ffe13516; border: 1px solid #ffe13525; border-radius: 7px; margin-left: 9px; padding: 3px 7px; vertical-align: middle; color: var(--accent); } +.count:empty { display: none; } +.search-field { display: flex; align-items: center; background: #2d2d2b; border: 1px solid #414139; border-radius: 12px; margin: 0 9px 22px; padding: 0 11px; gap: 8px; } +.search-field > span:not(.visually-hidden) { font-size: 25px; line-height: 1; color: var(--muted); } +.search-field input { border: 0; background: transparent; padding: 12px 0; font-size: 12px; box-shadow: none; } +.search-field:focus-within { border-color: var(--accent); } +.chat-list { flex: 1; overflow: auto; min-height: 0; scrollbar-width: thin; scrollbar-color: #646451 transparent; } +.chat-row { display: flex; align-items: center; gap: 13px; position: relative; border: 1px solid transparent; background: transparent; text-align: left; padding: 15px 11px; width: 100%; border-radius: 14px; color: var(--ink); margin-bottom: 6px; transition: background .18s, border-color .18s; } +.chat-row:hover { background: #2e2e29; } +.chat-row.active { background: #ffe1350f; border-color: #ffe13540; } +.chat-row.active::after { content: ""; position: absolute; right: 11px; height: 6px; width: 6px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px #ffe1350b; } +.avatar { display: flex; align-items: center; justify-content: center; width: 43px; height: 43px; border-radius: 14px; background: #45432a; color: var(--accent); font-size: 14px; font-weight: 650; flex-shrink: 0; letter-spacing: -.5px; border: 1px solid #ffe1351a; } +.avatar.tone-1 { background: #4b3e2a; color: #ffd68a; border-color: #ffd68a20; } +.avatar.tone-2 { background: #3b4032; color: #d9df9d; border-color: #d9df9d20; } +.avatar.tone-3 { background: #493b36; color: #edc5a5; border-color: #edc5a520; } +.chat-row.active .avatar { background: var(--accent); color: #262626; border-color: transparent; } +.chat-row-text { display: grid; gap: 6px; min-width: 0; flex: 1; } +.chat-row strong { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 10px; } +.chat-row small { font-size: 11px; color: var(--muted); } +.list-empty { font-size: 12px; text-align: center; line-height: 1.7; color: var(--muted); padding: 20px 15px; } +.profile { border-top: 1px solid var(--border); display: flex; align-items: center; gap: 6px; margin-top: 12px; padding: 20px 7px; } +.profile-main { display: flex; align-items: center; gap: 11px; text-align: left; background: none; border: 0; padding: 0; min-width: 0; flex: 1; color: var(--ink); } +.profile .avatar { border-radius: 50%; background: #39392c; color: var(--accent); width: 37px; height: 37px; font-size: 12px; } +.profile-text { display: grid; gap: 5px; min-width: 0; } +.profile-text strong { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; } +.profile-text > span { font-size: 10px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +.workspace { min-width: 0; min-height: 0; position: relative; background: var(--paper); } +.welcome { height: 100%; display: flex; align-items: center; justify-content: center; flex-direction: column; text-align: center; position: relative; padding: 35px; background: radial-gradient(ellipse at center, #ffe13505, transparent 70%); } +.welcome-symbol { width: 110px; height: 110px; background: #ffe1350b; border: 1px solid #ffe13529; border-radius: 34px; position: relative; display: flex; align-items: center; justify-content: center; margin-bottom: 34px; transform: rotate(-6deg); } +.welcome-symbol svg { width: 44px; height: 44px; color: var(--accent); stroke-width: 1.4; transform: rotate(6deg); } +.tiny-dot { position: absolute; width: 17px; height: 17px; right: -4px; top: 4px; border-radius: 50%; background: var(--accent); border: 4px solid var(--paper); } +.welcome .eyebrow { font-size: 9px; letter-spacing: 1.5px; } +.welcome h1 { font-weight: 650; font-size: 43px; letter-spacing: -1.7px; line-height: 1.2; margin: 18px 0; } +.welcome p { font-size: 13px; line-height: 1.8; color: var(--muted); margin: 0 0 27px; } +.welcome .button svg { width: 17px; height: 17px; } +.welcome-bottom { position: absolute; bottom: 30px; font-size: 10px; color: var(--muted); letter-spacing: .4px; } +.conversation { height: 100%; display: flex; flex-direction: column; } +.chat-header { display: flex; align-items: center; gap: 14px; padding: 21px 30px; border-bottom: 1px solid var(--border); min-height: 90px; background: #292929; } +.chat-header .chat-avatar { height: 43px; width: 43px; } +.chat-heading { flex: 1; min-width: 0; } +.chat-heading h2 { font-size: 17px; font-weight: 650; margin: 0 0 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; letter-spacing: -.3px; } +.chat-heading span { font-size: 11px; color: var(--muted); } +.message-region { position: relative; flex: 1; min-height: 0; display: flex; } +.message-scroll { flex: 1; min-height: 0; overflow: auto; padding: 12px 32px 24px; scrollbar-width: thin; scrollbar-color: #646451 transparent; scroll-behavior: auto; background: radial-gradient(ellipse at 100% 100%, #ffe13504, transparent 70%); } +.history-control { text-align: center; min-height: 18px; } +.message-list { display: flex; flex-direction: column; gap: 14px; } +.date-separator { align-self: center; background: #30302c; border: 1px solid #414139; border-radius: 20px; padding: 6px 12px; text-align: center; color: var(--muted); font-size: 10px; letter-spacing: .3px; margin: 20px 0 12px; } +.message { max-width: 76%; align-self: flex-start; display: grid; gap: 6px; } +.message.mine { align-self: flex-end; } +.message-author { font-size: 10px; color: var(--muted); padding-left: 8px; } +.message-bubble { background: #343430; border: 1px solid #46463b; border-radius: 17px 17px 17px 5px; padding: 13px 16px 10px; min-width: 90px; box-shadow: 0 4px 14px #00000008; } +.mine .message-bubble { background: var(--accent); color: #262626; border: 1px solid var(--accent); border-radius: 17px 17px 5px 17px; } +.message-text { font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.65; margin: 0; } +.message-time { font-size: 10px; display: block; text-align: right; color: var(--muted); margin-top: 7px; } +.mine .message-time { color: #5c5324; } +.empty-conversation { text-align: center; position: relative; top: 28%; font-size: 13px; color: var(--muted); line-height: 1.9; } +.composer { display: grid; grid-template-columns: minmax(0,1fr) 46px; gap: 10px; align-items: end; border-top: 1px solid var(--border); padding: 20px 29px 16px; background: #292929; } +.composer textarea { border: 1px solid #4b4b42; background: #30302d; border-radius: 13px; padding: 13px 15px; font-size: 14px; resize: none; max-height: 140px; min-height: 47px; scrollbar-width: thin; } +.send-button { display: flex; align-items: center; justify-content: center; background: var(--accent); border: 0; color: #262626; width: 46px; height: 47px; border-radius: 13px; margin-bottom: 1px; transition: background .18s; } +.send-button:hover { background: var(--accent-hover); } +.send-button:disabled { background: #444439; opacity: 1; color: #abab95; } +.send-button svg { height: 21px; width: 21px; } +.composer-note { grid-column: 1/-1; display: flex; justify-content: space-between; font-size: 9px; color: var(--muted); padding: 2px 3px; gap: 15px; } +.composer-note .over-limit { color: var(--danger); } +.mobile-only { display: none; } +.toast { position: fixed; bottom: 27px; left: 50%; transform: translateX(-50%); background: var(--accent); color: #262626; border: 1px solid #ffec7d; box-shadow: 0 8px 35px #00000030; border-radius: 13px; padding: 13px 20px; font-size: 12px; line-height: 1.5; max-width: min(90%,520px); z-index: 20; } +.toast.error { background: #452f2d; color: var(--danger); border-color: #70453e; } + +/* Dialogs */ +dialog { border: 1px solid #575744; border-radius: 22px; padding: 28px; max-width: 440px; width: calc(100% - 36px); color: var(--ink); background: #2c2c2a; box-shadow: 0 24px 100px #00000060; } +dialog::backdrop { background: #151515aa; backdrop-filter: blur(6px); } +.dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 15px; } +.dialog-heading h2 { font-size: 25px; font-weight: 650; letter-spacing: -.8px; margin: 0; } +.dialog-heading .icon-button { width: 30px; height: 30px; } +dialog .muted { font-size: 12px; margin: 14px 0 25px; } +dialog form { display: grid; gap: 20px; } +.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 3px; } +#my-id { font-size: 12px; font-family: ui-monospace, monospace; background: #242424; } +#profile-dialog .wide { margin-top: 17px; } +.members-list { display: grid; gap: 16px; padding-top: 25px; } +.member-item { display: grid; gap: 8px; } +.member-item strong { font-size: 13px; font-weight: 550; } +.member-item code { font-size: 11px; overflow-wrap: anywhere; color: var(--muted); background: #242424; border: 1px solid var(--border); padding: 12px; border-radius: 10px; } + +/* Optional API explanation panel */ +.diagnostics-toggle { display: flex; align-items: center; justify-content: center; gap: 8px; background: transparent; border: 1px solid #515143; border-radius: 11px; padding: 11px 12px; color: var(--muted); font-size: 11px; font-weight: 550; } +.diagnostics-toggle svg { width: 15px; height: 15px; } +.diagnostics-toggle:hover, .diagnostics-toggle[aria-expanded=true] { background: #ffe1350d; border-color: #ffe13565; color: var(--accent); } +.auth-bottom .diagnostics-toggle { margin: 15px auto 0; } +.sidebar-diagnostics { margin: 10px 9px 0; flex-shrink: 0; } +.diagnostics-panel { position: fixed; z-index: 15; right: 30px; top: 108px; width: 330px; max-height: min(56svh,490px); overflow: auto; background: #2c2c2a; border: 1px solid #66603a; border-radius: 20px; box-shadow: 0 18px 55px #00000030; padding: 20px; scrollbar-width: thin; scrollbar-color: #646451 transparent; } +.diagnostics-heading { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 18px; } +.diagnostics-heading .eyebrow { font-size: 9px; letter-spacing: 1.4px; } +.diagnostics-heading h2 { font-size: 19px; font-weight: 650; letter-spacing: -.5px; margin: 5px 0 0; } +.diagnostics-heading .icon-button { width: 30px; height: 30px; } +.diagnostics-label { display: block; font-size: 10px; color: var(--muted); margin-bottom: 6px; } +.diagnostics-summary { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 14px; background: #ffe1350c; border: 1px solid #ffe13520; border-radius: 12px; padding: 12px; } +.diagnostics-summary strong { display: block; font-size: 11px; overflow-wrap: anywhere; } +.diagnostics-history { padding: 16px 0; border-bottom: 1px solid var(--border); } +.diagnostics-history > strong { display: block; font-size: 13px; overflow-wrap: anywhere; } +.diagnostics-history > p { font-size: 11px; color: var(--muted); margin: 8px 0 0; } +.diagnostics-request { padding-top: 16px; } +.diagnostics-section-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; } +.diagnostics-section-heading h3 { font-size: 12px; margin: 0; font-weight: 600; } +.diagnostics-section-heading time { font-size: 10px; color: var(--muted); } +.diagnostics-request-line { display: flex; align-items: center; gap: 8px; } +.diagnostics-method, .diagnostics-status { font: 600 11px ui-monospace, monospace; padding: 6px 8px; border-radius: 6px; background: #404036; color: var(--accent); } +.diagnostics-status { background: var(--accent); color: #262626; } +.diagnostics-status.failed { background: #56332e; color: var(--danger); } +.diagnostics-path { display: block; font: 11px/1.6 ui-monospace, monospace; overflow-wrap: anywhere; color: var(--muted); margin-top: 10px; } +.diagnostics-result { font-size: 12px; line-height: 1.6; color: var(--accent); margin: 10px 0 14px; } +.diagnostics-result.failed { color: var(--danger); } +.diagnostics-metrics { display: grid; grid-template-columns: 1fr 1fr; margin: 0 0 14px; gap: 12px; } +.diagnostics-metrics dt { font-size: 10px; color: var(--muted); margin-bottom: 6px; } +.diagnostics-metrics dd { font: 550 12px ui-monospace, monospace; margin: 0; color: var(--ink); } +.diagnostics-request-id { display: block; font: 10px/1.7 ui-monospace, monospace; overflow-wrap: anywhere; color: var(--muted); } +.diagnostics-previous { list-style: none; margin: 15px 0 0; padding: 12px 0 0; border-top: 1px solid var(--border); display: grid; gap: 10px; } +.diagnostics-previous li { display: flex; gap: 8px; align-items: center; min-width: 0; } +.diagnostics-mini-status { font: 550 10px ui-monospace, monospace; color: var(--accent); min-width: 23px; } +.diagnostics-mini-status.failed { color: var(--danger); } +.diagnostics-previous code { font: 10px ui-monospace, monospace; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } +.diagnostics-background { font-size: 10px; color: var(--muted); margin: 12px 0 0; line-height: 1.7; } +.diagnostics-note { font-size: 11px; color: var(--muted); line-height: 1.7; margin: 0; } +.diagnostics-footnote { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 14px; font-size: 10px; } + +@media (min-width: 1490px) { + .app-shell { height: calc(100svh - 64px); margin: 32px auto; } +} +@media (max-width: 1100px) { + .intro { padding: 40px 38px; } + .auth-side { padding: 40px 35px; } + .app-shell { margin: 14px; height: calc(100svh - 28px); grid-template-columns: 280px minmax(0,1fr); } + .conversation-art { margin-left: 0; width: 290px; } + .intro h1 { font-size: 56px; } + .sidebar { padding-left: 14px; padding-right: 14px; } + .message-scroll { padding-left: 24px; padding-right: 24px; } +} +@media (max-width: 720px) { + .auth-layout { display: block; } + .intro { min-height: 0; padding: 28px; } + .intro-copy { margin-top: 30px; } + .intro h1 { font-size: 42px; letter-spacing: -2px; line-height: 1.08; margin: 14px 0 0; } + .intro h1 br { display: none; } + .intro-copy .eyebrow { font-size: 9px; } + .intro-copy p, .conversation-art, .intro-footer { display: none; } + .auth-side { min-height: 0; padding: 30px 28px; align-items: center; border-left: 0; border-top: 1px solid var(--border); } + .auth-card { max-width: 440px; } + .auth-switch { margin-bottom: 28px; } + .auth-card h2 { font-size: 32px; } + .auth-card h2 br { display: none; } + .auth-card .muted { margin-bottom: 22px; } + .auth-card form { gap: 17px; } + .auth-bottom { margin-top: 20px; } + .app-shell { margin: 0; border: 0; border-radius: 0; height: 100svh; display: block; } + .sidebar { height: 100%; width: 100%; padding: 25px 18px 0; border: 0; } + .sidebar-heading { padding-top: 30px; } + .workspace { display: none; height: 100%; } + .app-shell.chat-open .sidebar { display: none; } + .app-shell.chat-open .workspace { display: block; } + .welcome { display: none; } + .mobile-only { display: flex; } + .chat-header { padding: 15px; gap: 7px; min-height: 75px; } + .chat-header .avatar { width: 35px; height: 35px; border-radius: 11px; } + .chat-heading h2 { font-size: 14px; } + .chat-header .icon-button { width: 31px; height: 31px; } + .chat-header .icon-button svg { width: 19px; height: 19px; } + .message-scroll { padding: 10px 16px 20px; } + .message { max-width: 88%; } + .composer { padding: 14px 16px max(13px, env(safe-area-inset-bottom)); } + .composer-note { font-size: 9px; } + .composer-note > span:first-child { max-width: 70%; } + .profile { padding-bottom: max(18px, env(safe-area-inset-bottom)); } + .toast { bottom: 20px; } + .diagnostics-panel { top: 83px; right: 12px; width: calc(100% - 24px); max-height: 52svh; padding: 16px; } + .sidebar-diagnostics { margin-top: 9px; } +} +@media (min-width: 721px) { + body:not(.has-session) .diagnostics-panel { right: 24px; left: auto; } +} +@media (min-width: 1100px) { + .diagnostics-open.has-session .app-shell { margin-left: 22px; margin-right: 380px; } + .diagnostics-open.has-session .diagnostics-panel { top: 22px; max-height: calc(100svh - 44px); } +} +@media (prefers-reduced-motion: reduce) { + * { transition: none !important; scroll-behavior: auto !important; } +} + +/* Password controls and the animated SVG character. */ +.password-field { position: relative; display: block; } +.password-field input { padding-right: 47px; } +.password-toggle { position: absolute; right: 7px; top: 7px; width: 33px; height: 33px; border: 0; background: transparent; border-radius: 9px; color: var(--muted); display: flex; align-items: center; justify-content: center; } +.password-toggle:hover, .password-toggle[aria-pressed=true] { color: var(--accent); background: #ffe1350c; } +.password-toggle svg { width: 18px; height: 18px; } +/* One SVG mascot; CSS handles poses and the browser handles animation frames. */ +.mascot-stage { position: relative; width: 350px; max-width: 100%; height: 280px; margin: 20px 0 0 5px; pointer-events: none; isolation: isolate; --scene-surface: #262626; --gaze-x: 0px; --gaze-y: 0px; } +.spirit-svg { stroke: none; position: absolute; left: 0; bottom: 0; width: 285px; height: 285px; overflow: visible; z-index: 2; } +.spirit-motion { animation: spirit-breathe 4.5s ease-in-out infinite; } +.spirit-lean { transform-origin: 150px 268px; transition: transform .65s cubic-bezier(.22,1,.36,1); } +.spirit-eye { transform-box: fill-box; transform-origin: center; animation: spirit-blink 7s infinite; } +.spirit-eye-right { animation-delay: .1s; } +.spirit-pupil { transform: translate(var(--gaze-x), var(--gaze-y)); transition: transform .16s ease-out; } +.spirit-hand { transform-box: view-box; transition: transform .55s cubic-bezier(.22,1,.36,1); } +.spirit-gasp { opacity: 0; } +.mascot-stage[data-mood=looking] .spirit-lean { transform: translate(8px,-3px) rotate(3deg); } +.mascot-stage.is-curious .spirit-motion { animation: spirit-step .42s ease-in-out 3; } +.mascot-stage:is([data-mood=private],[data-mood=short],[data-mood=revealed]) .spirit-lean { transform: rotate(-4deg); } +.mascot-stage:is([data-mood=private],[data-mood=short],[data-mood=revealed]) .spirit-hand-left { transform: translate(28px,-37px) rotate(3deg); } +.mascot-stage:is([data-mood=private],[data-mood=short]) .spirit-hand-right { transform: translate(-28px,-25px) rotate(-3deg); } +.mascot-stage:is([data-mood=private],[data-mood=short]) .spirit-eye { animation: none; scale: 1 .08; } +.mascot-stage[data-mood=revealed] .spirit-hand-right { transform: translate(-28px,-39px) rotate(-3deg); } +.mascot-stage[data-mood=revealed] .spirit-eye { animation: none; scale: 1 .08; } +.mascot-stage[data-mood=pending] .spirit-motion { animation: spirit-breathe 1.1s ease-in-out infinite; } +.mascot-stage[data-mood=error] .spirit-motion { animation: spirit-shake .4s ease-in-out 2; } +.mascot-stage[data-mood=success] .spirit-motion { animation: spirit-hop .65s ease-in-out; } +.mascot-stage.is-sleepy .spirit-eye { animation: none; scale: 1 .2; } +.mascot-stage.is-sleepy .spirit-lean { transform: rotate(-5deg); } +.mascot-stage.is-scared .spirit-motion { animation: spirit-shake .25s ease-in-out infinite; } +.mascot-stage.is-scared :is(.spirit-smile,.kolo-tooth) { opacity: 0; } +.mascot-stage.is-scared .spirit-gasp { opacity: 1; } +.mascot-stage:not(.is-visible) * { animation-play-state: paused; } +.mascot-conversation { position: absolute; inset: 0; z-index: 0; } +.mascot-bubble { position: absolute; border-radius: 17px; animation: spirit-float 5s ease-in-out infinite; } +.mascot-bubble-filled { left: 247px; top: 20px; width: 88px; height: 50px; background: var(--accent); display: flex; align-items: center; justify-content: center; gap: 6px; rotate: -6deg; } +.mascot-bubble-filled::after { content: ""; position: absolute; bottom: -7px; left: 18px; width: 17px; height: 17px; background: var(--accent); clip-path: polygon(0 0,100% 0,0 100%); } +.mascot-bubble-filled span { width: 5px; height: 5px; border-radius: 50%; background: #262626; animation: spirit-typing 1.8s ease-in-out infinite; } +.mascot-bubble-filled span:nth-child(2) { animation-delay: .16s; } +.mascot-bubble-filled span:nth-child(3) { animation-delay: .32s; } +.mascot-bubble-outline { left: 265px; top: 107px; width: 77px; height: 55px; border: 1.5px solid #ffe13555; background: var(--scene-surface); rotate: 7deg; animation-delay: -2.5s; padding: 18px 15px; } +.mascot-bubble-outline::after { content: ""; position: absolute; bottom: -6px; right: 16px; width: 11px; height: 11px; border-right: 1.5px solid #ffe13555; border-bottom: 1.5px solid #ffe13555; background: var(--scene-surface); rotate: 45deg; } +.mascot-bubble-outline i { display: block; height: 2px; width: 100%; margin-bottom: 8px; border-radius: 2px; background: #ffe13555; } +.mascot-bubble-outline i:last-child { width: 65%; } +@keyframes spirit-breathe { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-3px); } } +@keyframes spirit-step { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-6px); } } +@keyframes spirit-blink { 0%,42%,46%,100% { scale: 1 1; } 44% { scale: 1 .08; } } +@keyframes spirit-peek { 0%,20%,76%,100% { scale: 1 .12; } 30%,66% { scale: 1 1; } } +@keyframes spirit-shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } } +@keyframes spirit-hop { 0%,100% { transform: translateY(0); } 25% { transform: translateY(3px); } 60% { transform: translateY(-15px); } } +@keyframes spirit-float { 0%,100% { translate: 0 0; } 50% { translate: 0 -5px; } } +@keyframes spirit-typing { 0%,55%,100% { translate: 0 0; opacity: .4; } 28% { translate: 0 -3px; opacity: 1; } } + +.scenario-section { padding: 17px 0 15px; border-bottom: 1px solid var(--border); } +.scenario-section h3 { font-size: 13px; margin: 0 0 9px; } +.scenario-description { font-size: 10px; line-height: 1.7; color: var(--muted); margin: 0 0 12px; } +.scenario-actions { display: grid; gap: 8px; } +.scenario-actions .button { padding: 11px 10px; font-size: 11px; } +.scenario-status { font-size: 11px; line-height: 1.6; margin: 12px 0 0; color: var(--ink); } +.scenario-status.failed { color: var(--danger); } +.scenario-scope { margin: 12px 0 0; } +.scenario-steps { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 8px; max-height: 245px; overflow: auto; scrollbar-width: thin; scrollbar-color: #646451 transparent; } +.scenario-step { border: 1px solid var(--border); border-radius: 10px; background: #262626; padding: 10px; } +.scenario-step[data-state=failed] { border-color: #9a5b51; } +.scenario-step summary { display: flex; align-items: flex-start; gap: 8px; cursor: pointer; font-size: 11px; line-height: 1.5; list-style: none; } +.scenario-step summary::before { content: "▸"; color: var(--muted); } +.scenario-step[open] summary::before { content: "▾"; } +.scenario-step summary::-webkit-details-marker { display: none; } +.scenario-step-badge { flex-shrink: 0; color: var(--accent); font: 650 10px/1.65 ui-monospace, monospace; } +.scenario-step[data-state=failed] .scenario-step-badge { color: var(--danger); } +.scenario-step[data-state=pending] .scenario-step-badge { color: var(--muted); } +.scenario-step p { color: var(--muted); font-size: 10px; line-height: 1.7; margin: 9px 0 0; } +.scenario-step code { display: block; color: var(--muted); font: 10px/1.7 ui-monospace, monospace; overflow-wrap: anywhere; margin-top: 7px; } +@media (max-width: 720px) { + .auth-card .mascot-stage { width: 100%; height: 166px; margin: -14px auto 14px; --scene-surface: #2c2c2a; } + .spirit-svg { width: 178px; height: 178px; left: calc(50% - 105px); } + .mascot-bubble { border-radius: 13px; } + .mascot-bubble-filled { left: calc(50% + 45px); top: 9px; width: 58px; height: 34px; gap: 4px; } + .mascot-bubble-filled span { width: 4px; height: 4px; } + .mascot-bubble-filled::after { width: 12px; height: 12px; bottom: -5px; left: 13px; } + .mascot-bubble-outline { left: calc(50% + 52px); top: 66px; width: 54px; height: 40px; padding: 13px 10px; } + .mascot-bubble-outline i { margin-bottom: 6px; } + .mascot-bubble-outline::after { width: 8px; height: 8px; bottom: -5px; right: 12px; } + .intro h1 { font-size: 36px; } + .intro { padding-bottom: 18px; } + .scenario-steps { max-height: 175px; } +} +.diagnostics-origin { margin-top: 10px; } + +.auth-card form { margin-top: 24px; } +.auth-card .muted + form { margin-top: 0; } +.diagnostics-help { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 12px; } +.diagnostics-help summary { color: var(--muted); font-size: 10px; cursor: pointer; } +.diagnostics-help .diagnostics-note { margin-top: 10px; font-size: 10px; } +@media (prefers-reduced-motion: reduce) { + .mascot-stage *, .mascot-stage *::before { animation: none !important; transition: none !important; } +} + +/* Keep the reader's position while messages arrive below. */ +.jump-latest { position: absolute; right: 24px; bottom: 16px; border: 1px solid #ffe13555; background: #36362d; color: var(--accent); padding: 10px 15px; border-radius: 20px; font-size: 12px; box-shadow: 0 5px 20px #0004; z-index: 2; } +.jump-latest:hover { background: #424234; } +.message.grouped { margin-top: -9px; } +.message.grouped .message-bubble { border-top-left-radius: 8px; } +.message.mine.grouped .message-bubble { border-top-left-radius: 17px; border-top-right-radius: 8px; } +@media (max-width: 720px) { .jump-latest { right: 14px; bottom: 12px; } } + +.welcome-illustration { width: 260px; max-width: 75%; height: auto; margin-bottom: 12px; } +.welcome h1 { font-size: 38px; margin: 14px 0; } +.empty-conversation { top: auto; padding: 35px 16px; color: #d6d6c9; } +.empty-conversation img { width: 165px; max-width: 70%; height: auto; } +.empty-conversation p { color: var(--ink); font-size: 16px; margin: 15px 0 8px; } +.empty-conversation span { color: var(--muted); font-size: 12px; } +.message-bubble { color: #e5e5dc; } +.mine .message-bubble { color: #262626; } +.composer-note { font-size: 10px; line-height: 1.5; } +@media (max-width: 720px) { .welcome-illustration { width: 200px; } .welcome h1 { font-size: 32px; } } +.message-scroll { background-image: url('/assets/chat-pattern.svg'); background-size: 180px 160px; } + +/* Head, ears and mushrooms remain independent of the body. */ +.kolo-head { transform-origin: 151px 202px; transition: transform .65s cubic-bezier(.22,1,.36,1); } +.kolo-ear { transform-origin: 150px 135px; transition: transform .7s ease; } +.kolo-mushrooms { transform-origin: 153px 83px; animation: kolo-sway 6s ease-in-out infinite; } +.mascot-stage[data-mood=looking] .kolo-head { transform: rotate(4deg) translate(3px,-2px); } +.mascot-stage[data-mood=looking] .kolo-ear-right { transform: rotate(-3deg); } +.mascot-stage:is([data-mood=private],[data-mood=short],[data-mood=revealed]) .kolo-head { transform: translate(-5px,2px) rotate(-9deg); } +.mascot-stage:is([data-mood=private],[data-mood=short],[data-mood=revealed]) .spirit-face { opacity: .65; } +.mascot-stage[data-mood=success] .kolo-ear { transform: rotate(-3deg); } +.mascot-stage.is-sleepy .kolo-head { transform: rotate(-6deg) translateY(3px); } +.auth-error-art { float: right; width: 66px; height: 66px; object-fit: contain; margin: 10px 0 0 10px; } +.state-error { text-align: center; padding: 28px 18px; color: var(--muted); } +.state-error img { width: 160px; max-width: 65%; height: auto; } +.state-error h2, .state-error h3 { color: var(--ink); font-size: 19px; font-weight: 600; margin: 14px 0 10px; } +.state-error p { font-size: 12px; line-height: 1.7; margin: 0 0 18px; } +.state-error .button { font-size: 12px; padding: 11px 18px; } +.welcome.state-error { height: 100%; } +@keyframes kolo-sway { 0%,100% { transform: rotate(-1deg); } 50% { transform: rotate(2deg); } } +@media (max-width: 720px) { + .state-error img { width: 130px; } + .welcome-illustration { width: 200px; max-height: 24svh; object-fit: contain; } +} +@media (prefers-reduced-motion: reduce) { + .kolo-mushrooms { animation: none; } +} + +.message-scroll:has(> :is(.empty-conversation,.state-error):not([hidden])) { display: flex; flex-direction: column; } +.message-scroll > :is(.empty-conversation,.state-error) { margin: auto 0; } diff --git a/src/Messenger.Api/wwwroot/app.js b/src/Messenger.Api/wwwroot/app.js new file mode 100644 index 00000000..e0dc1618 --- /dev/null +++ b/src/Messenger.Api/wwwroot/app.js @@ -0,0 +1,785 @@ +import { operationsSession } from '/operations.js'; +import { attachmentSession } from '/attachments.js'; +import { recordRequest, updateSessionInfo, updateHistoryInfo } from "/diagnostics.js"; + +import { setMascotMood, celebrateMascots } from "/mascots.js"; +import { MessageView } from "/message-view.js"; +import { paintAvatar } from "/avatars.js"; +import { startClusterView } from '/cluster-view.js'; + +const $ = id => document.getElementById(id); +const storageKey = "n1k0der.session"; +const messageView = new MessageView($("message-list")); +const state = { session: null, chats: [], chatsStatus: "loading", chatsCursor: null, current: null, messages: new Map(), + serverIds: new Set(), members: new Map(), olderCursor: null, historyReady: false, epoch: 0, syncing: false, sending: false, drafts: new Map() }; +let toastTimer, unseenMessages = 0; +const pendingMessages = new Map(); +const attachedDrafts = new Map(); +let uploadBusy = false; +const chatRows = new Map(); +let chatsLoading = false, lastChatRefresh = 0; +const reactions = new Map(), reactionBusy = new Set(); +let reactionVersion = 0; + +class ApiError extends Error { + constructor(message, status = 0) { super(message); this.status = status; } +} + +function notice(id, message, success = false) { + const element = $(id); + element.textContent = message; + element.hidden = !message; + element.classList.toggle("success", success); +} + +function toast(message, error = false) { + clearTimeout(toastTimer); + $("toast").textContent = message; + $("toast").classList.toggle("error", error); + $("toast").hidden = false; + toastTimer = setTimeout(() => { $("toast").hidden = true; }, error ? 6500 : 3000); +} + +async function request(path, { method = "GET", body, headers = {}, auth = true, background = false, raw } = {}) { + const started = performance.now(); + let httpStatus = null, requestId = null, instanceId = null, outcome = "network"; + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), raw ? 90000 : 12000); + // Authorization is explicit; disable the browser's implicit Basic-auth handling. + const options = { method, headers: { Accept: "application/json", ...headers }, signal: controller.signal, cache: "no-store", credentials: "omit" }; + if (auth && state.session) options.headers.Authorization = "Bearer " + state.session.token; + if (body !== undefined) { + options.body = JSON.stringify(body); + options.headers["Content-Type"] = "application/json"; + } + if (raw !== undefined) options.body = raw; + try { + const response = await fetch(path, options); + httpStatus = response.status; + requestId = response.headers.get("X-Request-Id"); + instanceId = response.headers.get("X-Instance-Id"); + outcome = "unreadable"; + const data = response.status === 204 ? null : await response.json(); + outcome = response.ok ? "success" : "http_error"; + if (!response.ok) { + if (response.status === 401 && auth) endSession("Сессия закончилась. Войдите снова."); + throw new ApiError(data?.error?.message || "Не удалось выполнить запрос.", response.status); + } + return data; + } catch (error) { + if (error instanceof ApiError) throw error; + throw new ApiError("Нет ответа от сервера. Проверьте соединение и попробуйте позже."); + } finally { + clearTimeout(timer); + recordRequest({ method, path, status: httpStatus, requestId, instanceId, outcome, background, + durationMs: Math.round(performance.now() - started), + authorization: options.headers.Authorization?.split(" ")[0] || "Без авторизации" }); + } +} + +function basic(username, password) { + const bytes = new TextEncoder().encode(username + ":" + password); + return "Basic " + btoa(Array.from(bytes, byte => String.fromCharCode(byte)).join("")); +} + +function selectAuthTab(register) { + $("register-panel").hidden = !register; + $("login-panel").hidden = register; + $("tab-register").classList.toggle("active", register); + $("tab-login").classList.toggle("active", !register); + $("tab-register").setAttribute("aria-pressed", String(register)); + $("tab-login").setAttribute("aria-pressed", String(!register)); + notice("auth-notice", ""); +} + +function setFormBusy(form, busy) { + form.querySelectorAll("input, textarea, button").forEach(element => { element.disabled = busy; }); + form.setAttribute("aria-busy", String(busy)); + const label = form.querySelector(".button-label"); + if (label) label.textContent = busy ? label.dataset.busy : label.dataset.label; +} + +function persistSession(session) { + try { sessionStorage.setItem(storageKey, JSON.stringify(session)); } catch { /* session still works in memory */ } +} + +async function enterSession(session) { + reactions.clear(); reactionBusy.clear(); reactionVersion++; + state.epoch++; + state.session = session; + attachmentSession(session.token); + operationsSession(session.token); + clearAttachedDrafts(); + updateSessionInfo({ name: session.user.display_name, expiresAt: session.expires_at }); + updateHistoryInfo(null); + state.current = null; + state.messages.clear(); + state.serverIds.clear(); + state.chats = []; + chatRows.clear(); + state.chatsStatus = "loading"; + state.chatsCursor = null; + state.drafts.clear(); + state.syncing = false; + state.sending = false; + persistSession(session); + $("auth-screen").hidden = true; + $("app-screen").hidden = false; + $("app-screen").classList.remove("chat-open"); + $("welcome").hidden = false; + $("workspace-error").hidden = true; + $("conversation-error").hidden = true; + $("conversation").hidden = true; + $("profile-name").textContent = session.user.display_name; + $("profile-username").textContent = "@" + session.user.username; + paintAvatar($("profile-avatar"), session.user.id, session.user.display_name); + $("chat-search").value = ""; + $("clear-chat-search").hidden = true; + renderChats(); + try { await loadChats(false, true); } catch (error) { + if (state.session?.token === session.token) { $("welcome").hidden = true; $("workspace-error").hidden = false; } + } +} + +function endSession(message = "") { + reactions.clear(); reactionBusy.clear(); reactionVersion++; + state.epoch++; + state.session = null; + attachmentSession(null); + operationsSession(null); + clearAttachedDrafts(); + updateSessionInfo(null); + updateHistoryInfo(null); + state.current = null; + state.chats = []; + chatRows.clear(); + state.messages.clear(); + state.serverIds.clear(); + state.drafts.clear(); + pendingMessages.clear(); + try { sessionStorage.removeItem(storageKey); } catch { /* storage unavailable */ } + document.querySelectorAll("dialog[open]").forEach(dialog => dialog.close()); + messageView.clear(); + unseenMessages = 0; + updateJumpButton(); + $("chat-list").replaceChildren(); + $("message-input").value = ""; + $("app-screen").hidden = true; + $("auth-screen").hidden = false; + $("login-password").value = ""; + selectAuthTab(false); + notice("auth-notice", message); +} + +$("tab-login").addEventListener("click", () => selectAuthTab(false)); +$("tab-register").addEventListener("click", () => selectAuthTab(true)); + +$("login-form").addEventListener("submit", async event => { + event.preventDefault(); + const form = event.currentTarget; + const username = $("login-username").value.trim(); + const password = $("login-password").value; + setFormBusy(form, true); + setMascotMood("pending"); + notice("auth-notice", ""); + try { + const session = await request("/api/v1/auth/sessions", { method: "POST", auth: false, headers: { Authorization: basic(username, password) } }); + form.reset(); + await celebrateMascots(); + await enterSession(session); + } catch (error) { setMascotMood("error"); notice("auth-notice", error.status === 401 ? "Неверный логин или пароль." : error.message); } + finally { setFormBusy(form, false); } +}); + +$("register-form").addEventListener("submit", async event => { + event.preventDefault(); + const form = event.currentTarget; + const username = $("register-username").value.trim(); + const displayName = $("register-name").value.trim(); + const password = $("register-password").value; + if (Array.from(displayName).length > 100) return notice("auth-notice", "Имя должно содержать от 1 до 100 символов."); + if (Array.from(password).length < 12 || Array.from(password).length > 128 || !password.trim()) { + setMascotMood("short"); + return notice("auth-notice", "Пароль должен содержать от 12 до 128 символов."); + } + setFormBusy(form, true); + setMascotMood("pending"); + notice("auth-notice", ""); + let registered = false; + try { + await request("/api/v1/users", { method: "POST", auth: false, body: { username, display_name: displayName, password } }); + registered = true; + const session = await request("/api/v1/auth/sessions", { method: "POST", auth: false, headers: { Authorization: basic(username, password) } }); + form.reset(); + await celebrateMascots(); + await enterSession(session); + toast("Добро пожаловать, " + session.user.display_name + "!"); + } catch (error) { + setMascotMood("error"); + if (registered) { + form.reset(); + selectAuthTab(false); + $("login-username").value = username; + notice("auth-notice", "Аккаунт создан. Войдите, когда сервер будет доступен."); + } else notice("auth-notice", error.status === 409 ? "Это имя пользователя уже занято. Выберите другое." : error.message); + } finally { setFormBusy(form, false); } +}); + +$("login-submit").disabled = false; +$("register-submit").disabled = false; + +function memberCount(count) { const n = count % 100, d = count % 10; return count + " " + (n >= 11 && n <= 14 ? "участников" : d === 1 ? "участник" : d >= 2 && d <= 4 ? "участника" : "участников"); } +function chatActivity(chat) { return chat.last_message?.created_at || chat.created_at; } +function chatTime(value) { + const date = new Date(value), now = new Date(); + return date.toDateString() === now.toDateString() + ? date.toLocaleTimeString("ru-RU", { hour: "2-digit", minute: "2-digit" }) + : date.toLocaleDateString("ru-RU", { day: "numeric", month: "short" }); +} + +async function loadChats(more = false, background = false) { + if (chatsLoading) return; + chatsLoading = true; + const token = state.session?.token; + const cursor = more ? state.chatsCursor : null; + $("chat-list").setAttribute("aria-busy", "true"); + $("refresh-chats").classList.add("is-loading"); + try { + const data = await request("/api/ui/chats?limit=100" + (cursor ? "&cursor=" + encodeURIComponent(cursor) : ""), { background }); + if (token !== state.session?.token) return; + const previous = new Map(state.chats.map(chat => [chat.id, chat])); + const incoming = data.items.map(chat => { + const existing = previous.get(chat.id)?.last_message; + return existing && (!chat.last_message || compareMessages(existing, chat.last_message) > 0) + ? { ...chat, last_message: existing } : chat; + }); + state.chats = more ? [...state.chats, ...incoming.filter(chat => !previous.has(chat.id))] : incoming; + state.chatsCursor = data.next_cursor; + state.chatsStatus = "ready"; + lastChatRefresh = Date.now(); + $("workspace-error").hidden = true; + if (!state.current) $("welcome").hidden = false; + renderChats(); + } catch (error) { + if (token === state.session?.token && !state.chats.length) { + state.chatsStatus = "error"; renderChats(); + } + throw error; + } finally { + chatsLoading = false; + $("chat-list").setAttribute("aria-busy", "false"); + $("refresh-chats").classList.remove("is-loading"); + } +} + +function renderChats() { + const query = $("chat-search").value.trim().toLocaleLowerCase("ru-RU"); + const chats = [...state.chats].sort((a, b) => chatActivity(b).localeCompare(chatActivity(a)) || b.id.localeCompare(a.id)) + .filter(chat => chat.title.toLocaleLowerCase("ru-RU").includes(query)); + $("chat-count").textContent = state.chats.length || ""; + $("more-chats").hidden = !state.chatsCursor; + const nodes = chats.map(chat => { + let row = chatRows.get(chat.id); + if (!row) { + const button = document.createElement("button"); button.className = "chat-row"; + button.dataset.chatId = chat.id; + const avatar = document.createElement("span"); avatar.className = "avatar"; + const description = document.createElement("span"); description.className = "chat-row-text"; + const heading = document.createElement("span"); heading.className = "chat-row-heading"; + const title = document.createElement("strong"), time = document.createElement("time"); + const preview = document.createElement("small"); preview.className = "chat-preview"; + heading.append(title, time); description.append(heading, preview); button.append(avatar, description); + button.addEventListener("click", () => { + const current = state.chats.find(item => item.id === chat.id); + if (current) void selectChat(current); + }); + row = { button, avatar, title, time, preview }; chatRows.set(chat.id, row); + } + row.button.classList.toggle("active", chat.id === state.current?.id); + row.button.setAttribute("aria-pressed", String(chat.id === state.current?.id)); + paintAvatar(row.avatar, chat.id, chat.title); + row.title.textContent = chat.title; + row.time.dateTime = chatActivity(chat); row.time.textContent = chatTime(chatActivity(chat)); + row.time.title = new Date(chatActivity(chat)).toLocaleString("ru-RU"); + const message = chat.last_message; + row.preview.textContent = message + ? (message.sender_id === state.session?.user.id ? "Вы: " : "") + (message.text.replace(/\s+/gu, " ").trim() || "Изображение") + : "Пока без сообщений · " + memberCount(chat.member_ids.length); + return row.button; + }); + if (!nodes.length) { + const empty = document.createElement("p"); empty.className = "list-empty"; + empty.textContent = state.chatsStatus === "loading" ? "Загружаем чаты…" : state.chatsStatus === "error" ? "Не удалось загрузить чаты." : query ? "Чаты не найдены." : "Пока нет чатов."; + nodes.push(empty); + } + const list = $("chat-list"), wanted = new Set(nodes); + for (const node of [...list.children]) if (!wanted.has(node)) node.remove(); + let next = list.firstChild; + for (const node of nodes) { + if (node === next) next = next.nextSibling; + else list.insertBefore(node, next); + } +} + +function updateChatPreview(chatId, items) { + const chat = state.chats.find(item => item.id === chatId); + const last = items.reduce((latest, message) => !latest || compareMessages(message, latest) > 0 ? message : latest, null); + if (chat && last && (!chat.last_message || compareMessages(last, chat.last_message) > 0)) { + chat.last_message = last; renderChats(); + } +} + +$("retry-chats").addEventListener("click", async () => { + const button = $("retry-chats"); button.disabled = true; + try { await loadChats(); } catch (error) { toast(error.message, true); } + finally { button.disabled = false; } +}); +$("retry-messages").addEventListener("click", async () => { + const button = $("retry-messages"); button.disabled = true; + try { await syncRecent(true); } catch (error) { toast(error.message, true); } + finally { button.disabled = false; } +}); + +$("chat-search").addEventListener("input", () => { + $("clear-chat-search").hidden = !$("chat-search").value; + renderChats(); +}); +$("clear-chat-search").addEventListener("click", () => { + $("chat-search").value = ""; $("clear-chat-search").hidden = true; renderChats(); $("chat-search").focus(); +}); +$("chat-search").addEventListener("keydown", event => { + if (event.key === "Escape" && event.currentTarget.value) { event.stopPropagation(); $("clear-chat-search").click(); } +}); +document.addEventListener("keydown", event => { + if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k" && state.session && !document.querySelector("dialog[open]")) { + event.preventDefault(); + if (matchMedia("(max-width: 720px)").matches && $("app-screen").classList.contains("chat-open")) $("back-to-chats").click(); + $("chat-search").focus(); + } +}); +$("message-list").addEventListener("messagecopy", event => toast(event.detail.success ? "Текст скопирован" : "Не удалось скопировать текст", !event.detail.success)); +$("refresh-chats").addEventListener("click", async () => { try { await loadChats(); } catch (error) { toast(error.message, true); } }); +$("more-chats").addEventListener("click", async () => { try { await loadChats(true); } catch (error) { toast(error.message, true); } }); + +async function selectChat(chat) { + if (state.current?.id === chat.id) { $("app-screen").classList.add("chat-open"); if(matchMedia("(min-width: 721px)").matches) $("message-input").focus({preventScroll:true}); return; } + if (state.current) state.drafts.set(state.current.id, $("message-input").value); + state.epoch++; + reactions.clear(); reactionBusy.clear(); reactionVersion++; + state.current = chat; + state.members = new Map([[state.session.user.id, state.session.user]]); + renderChats(); + state.messages = new Map(); + state.serverIds = new Set(); + state.olderCursor = null; + state.historyReady = false; + updateHistoryInfo({ title: chat.title, loaded: 0, hasOlder: false, ready: false }); + state.syncing = false; + $("current-title").textContent = chat.title; + $("current-members").textContent = memberCount(chat.member_ids.length); + paintAvatar($("current-avatar"), chat.id, chat.title); + $("welcome").hidden = true; + $("workspace-error").hidden = true; + $("conversation-error").hidden = true; + $("conversation").hidden = false; + $("app-screen").classList.add("chat-open"); + $("message-input").value = state.drafts.get(chat.id) || ""; + renderAttachedDrafts(); + updateComposer(); + messageView.clear(); + unseenMessages = 0; + updateJumpButton(); + $("empty-conversation").hidden = true; + $("older-messages").hidden = true; + $("messages-loading").hidden = false; + $("message-list").setAttribute("aria-busy", "true"); + if(matchMedia("(min-width: 721px)").matches) $("message-input").focus({preventScroll:true}); + if (!matchMedia("(prefers-reduced-motion: reduce)").matches) $("conversation").animate([{ opacity: 0, transform: "translateY(5px)" }, { opacity: 1, transform: "none" }], { duration: 180, easing: "ease-out" }); + try { await Promise.all([syncRecent(true), loadMembers(state.epoch, chat.id)]); } + catch (error) { if (state.historyReady) toast(error.message, true); } +} + +async function loadMembers(epoch, chatId) { + const data = await request(`/api/ui/chats/${chatId}/members`, { background: true }); + if (epoch !== state.epoch) return; + state.members = new Map(data.items.map(user => [user.id, user])); + if (state.historyReady) renderMessages(); +} + +function mergeMessages(items) { + let changed = false; + for (const item of items) { + const previous = state.messages.get(item.id); + if (!previous || previous.text !== item.text || previous.sender_id !== item.sender_id || previous.created_at !== item.created_at) { + state.messages.set(item.id, item); + changed = true; + } + state.serverIds.add(item.id); + } + return changed; +} + +function compareMessages(a, b) { + return a.created_at.localeCompare(b.created_at) || a.id.localeCompare(b.id); +} + +async function syncRecent(initial = false, background = false) { + if (!state.current || !state.session || state.syncing) return; + const epoch = state.epoch, chatId = state.current.id; + state.syncing = true; + try { + const page = await request(`/api/ui/chats/${chatId}/messages?limit=50`, { background }); + if (epoch !== state.epoch) return; + $("conversation-error").hidden = true; + const wasReady = state.historyReady, oldCursor = state.olderCursor; + const latest = [...state.messages.values()].reduce((last, item) => !last || compareMessages(item, last) > 0 ? item : last, null); + const incoming = initial || !latest ? 0 : page.items.filter(item => !state.messages.has(item.id) && compareMessages(item, latest) > 0).length; + const overlap = page.items.some(item => state.serverIds.has(item.id)); + state.historyReady = true; + // On a large burst, the older button pages through the newly discovered gap. + if (initial || (!overlap && page.items.length === 50)) state.olderCursor = page.next_cursor; + updateChatPreview(chatId, page.items); + const changed = mergeMessages(page.items); + if (initial || changed || !wasReady || oldCursor !== state.olderCursor) { + renderMessages(initial ? "bottom" : "preserve", incoming); + } + const visible = [...document.querySelectorAll('article.message')].filter(node => { + const rect = node.getBoundingClientRect(), region = $('message-scroll').getBoundingClientRect(); + return rect.bottom >= region.top && rect.top <= region.bottom; + }).map(node => node.dataset.messageId); + await refreshReactions([...new Set([...visible, ...page.items.map(item => item.id)])].slice(0,100)); + } catch (error) { + if (epoch === state.epoch && state.session && !state.historyReady) { + $("empty-conversation").hidden = true; + $("conversation-error").hidden = false; + } + throw error; + } finally { if (epoch === state.epoch) { + state.syncing = false; + $("messages-loading").hidden = true; + $("message-list").setAttribute("aria-busy", "false"); + } } +} + +$("older-messages").addEventListener("click", async () => { + if (!state.olderCursor || !state.current) return; + const epoch = state.epoch; + $("older-messages").disabled = true; + try { + const page = await request(`/api/ui/chats/${state.current.id}/messages?limit=50&cursor=${encodeURIComponent(state.olderCursor)}`); + if (epoch !== state.epoch) return; + mergeMessages(page.items); + state.olderCursor = page.next_cursor; + renderMessages("prepend"); + await refreshReactions(page.items.map(item => item.id)); + } catch (error) { toast(error.message, true); } + finally { $("older-messages").disabled = false; } +}); + +function renderMessages(mode = "preserve", incoming = 0) { + const scroll = $("message-scroll"), oldHeight = scroll.scrollHeight, oldTop = scroll.scrollTop; + const atBottom = oldHeight - oldTop - scroll.clientHeight < 85; + const messages = [...state.messages.values()].sort(compareMessages); + messageView.render(messages, state.session?.user.id, state.members, mode === "bottom" || incoming > 0); + messageView.renderReactions(reactions, reactionBusy); + $("empty-conversation").hidden = !state.historyReady || messages.length !== 0; + $("older-messages").hidden = !state.olderCursor; + updateHistoryInfo({ title: state.current?.title, loaded: messages.length, hasOlder: Boolean(state.olderCursor), ready: state.historyReady }); + if (mode === "bottom" || (mode === "preserve" && atBottom)) { + scroll.scrollTop = scroll.scrollHeight; + unseenMessages = 0; + } else if (mode === "prepend") scroll.scrollTop = oldTop + scroll.scrollHeight - oldHeight; + else { + scroll.scrollTop = oldTop; + unseenMessages += incoming; + } + updateJumpButton(); +} + +async function refreshReactions(ids) { + if (!ids.length || !state.current) return; + const epoch = state.epoch, version = reactionVersion, chatId = state.current.id; + try { + const data = await request(`/api/ui/chats/${chatId}/reactions?message_ids=${ids.join(',')}`, {background:true}); + if (epoch !== state.epoch || version !== reactionVersion) return; + const scroll = $('message-scroll'), oldHeight = scroll.scrollHeight, oldTop = scroll.scrollTop; + const atBottom = oldHeight - oldTop - scroll.clientHeight < 85; + for (const id of ids) reactions.set(id, data.items.filter(item => item.message_id === id)); + messageView.renderReactions(reactions, reactionBusy); + scroll.scrollTop = atBottom ? scroll.scrollHeight : oldTop; + } catch { /* Message history remains usable; reaction changes show their own error. */ } +} + +$('message-list').addEventListener('reactionchange', async event => { + const {messageId, reaction, present} = event.detail; + if (!state.current || reactionBusy.has(messageId)) return; + const epoch = state.epoch, chatId = state.current.id; + reactionVersion++; reactionBusy.add(messageId); messageView.renderReactions(reactions, reactionBusy); + try { + await request(`/api/ui/chats/${chatId}/messages/${messageId}/reactions/${reaction}`, {method:present ? 'PUT' : 'DELETE'}); + if (epoch !== state.epoch) return; + reactionVersion++; + await refreshReactions([messageId]); + } catch (error) { if (epoch === state.epoch) toast('Не удалось изменить реакцию. ' + error.message, true); } + finally { if (epoch === state.epoch) { reactionBusy.delete(messageId); messageView.renderReactions(reactions, reactionBusy); } } +}); + +function updateJumpButton() { + const scroll = $("message-scroll"), button = $("jump-latest"); + const atBottom = scroll.scrollHeight - scroll.scrollTop - scroll.clientHeight < 85; + if (atBottom) unseenMessages = 0; + button.hidden = atBottom || state.messages.size === 0; + const label = unseenMessages ? `Новые сообщения: ${unseenMessages} ↓` : "К последним ↓"; + if (button.textContent !== label) button.textContent = label; +} + +$("message-scroll").addEventListener("scroll", updateJumpButton, { passive: true }); +$("jump-latest").addEventListener("click", () => { + const scroll = $("message-scroll"); + scroll.scrollTo({ top: scroll.scrollHeight, behavior: matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth" }); +}); + +function updateComposer() { + const text = $("message-input").value, count = Array.from(text).length; + $("message-count").textContent = `${count} / 4000`; + $("message-count").classList.toggle("over-limit", count > 4000); + $("pick-file").disabled = state.sending || uploadBusy; + $("send-message").disabled = state.sending || uploadBusy || (!text.trim() && !(attachedDrafts.get(state.current?.id)?.length)) || count > 4000; + $("message-input").style.height = "auto"; + const height = $("message-input").scrollHeight + 2; + $("message-input").style.height = Math.min(140, height) + "px"; + $("message-input").style.overflowY = height > 140 ? "auto" : "hidden"; + if (state.current) state.drafts.set(state.current.id, text); +} + +$("message-input").addEventListener("input", updateComposer); +$("message-input").addEventListener("keydown", event => { + if (event.key === "Enter" && !event.shiftKey && !event.isComposing) { + event.preventDefault(); + if (!$("send-message").disabled) $("message-form").requestSubmit(); + } +}); +$("message-form").addEventListener("submit", async event => { + event.preventDefault(); + if (state.sending || !state.current || $("send-message").disabled) return; + const epoch = state.epoch, chatId = state.current.id, text = $("message-input").value; + const attachment_ids = (attachedDrafts.get(chatId) || []).map(file => file.id); + const previous = pendingMessages.get(chatId); + const attempt = previous?.text === text && JSON.stringify(previous.attachment_ids) === JSON.stringify(attachment_ids) ? previous : { text, attachment_ids, key: crypto.randomUUID() }; + pendingMessages.set(chatId, attempt); + state.sending = true; updateComposer(); renderAttachedDrafts(); + try { + const message = await request(`/api/v1/chats/${chatId}/messages`, { + method: "POST", body: { text, attachment_ids }, headers: { "Idempotency-Key": attempt.key } + }); + pendingMessages.delete(chatId); + clearAttachedDrafts(chatId); + renderAttachedDrafts(); + if (state.drafts.get(chatId) === text) state.drafts.set(chatId, ""); + if (epoch !== state.epoch) return; + state.messages.set(message.id, message); + updateChatPreview(chatId, [message]); + if ($("message-input").value === text) $("message-input").value = ""; + renderMessages("bottom"); + await syncRecent(false, true); + } catch (error) { toast(error.status === 0 ? "Ответ не получен. Повторите отправку: сообщение не продублируется." : error.message, true); } + finally { state.sending = false; updateComposer(); renderAttachedDrafts(); } +}); + +function openNewChat() { + $("new-chat-form").reset(); notice("chat-dialog-notice", ""); + $("new-chat-dialog").showModal(); $("new-chat-title").focus(); +} +$("new-chat").addEventListener("click", openNewChat); +$("welcome-new-chat").addEventListener("click", openNewChat); +document.querySelectorAll("[data-close-dialog]").forEach(button => button.addEventListener("click", () => button.closest("dialog").close())); +$("new-chat-form").addEventListener("submit", async event => { + event.preventDefault(); + const form = event.currentTarget, token = state.session?.token; + const title = $("new-chat-title").value.trim(); + if (!title || Array.from(title).length > 100) return notice("chat-dialog-notice", "Название должно содержать от 1 до 100 символов."); + const entries = $("new-chat-members").value.split(/[\s,;]+/u).filter(Boolean); + if(entries.length>99)return notice("chat-dialog-notice","Не более 99 приглашений за раз."); + setFormBusy(form, true); notice("chat-dialog-notice", ""); + try { + const ids=[]; + for(const entry of entries) { + if(/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(entry))ids.push(entry.toLowerCase()); + else { + const username=entry.replace(/^@/,'').toLowerCase(); + if(!/^[a-z0-9_]{3,32}$/.test(username))throw new Error('Введите логин или полный ID участника.'); + let user; + try { user=await request('/api/ui/users/'+encodeURIComponent(username)); } + catch(error) { if(error.status===404)throw new Error('Пользователь @'+username+' не найден.');throw error; } + ids.push(user.id); + } + } + if(token!==state.session?.token)return; + const members=[...new Set([state.session.user.id,...ids])]; + const chat = await request("/api/v1/chats", { method: "POST", body: { title, member_ids: members } }); + if (token !== state.session?.token) return; + $("new-chat-dialog").close(); state.chats.push(chat); renderChats(); + await selectChat(chat); toast("Чат создан."); + } catch (error) { notice("chat-dialog-notice", error.message); } + finally { setFormBusy(form, false); } +}); + +$("open-profile").addEventListener("click", () => { + $("profile-details").textContent = state.session.user.display_name + " · @" + state.session.user.username; + $("my-id").value = state.session.user.id; $("profile-dialog").showModal(); +}); +$("copy-id").addEventListener("click", async () => { + try { await navigator.clipboard.writeText(state.session.user.id); toast("ID скопирован."); } + catch { $("my-id").select(); toast("Выделили ID. Скопируйте его сочетанием ⌘C или Ctrl+C."); } +}); +$("show-members").addEventListener("click", () => { + $("members-list").replaceChildren(...state.current.member_ids.map(id => { + const item = document.createElement("div"); item.className = "member-item"; + const label = document.createElement("strong"); label.textContent = (state.members.get(id)?.display_name || "Участник") + (id === state.session.user.id ? " (вы)" : ""); + const avatar = document.createElement("span"); avatar.className = "avatar"; + paintAvatar(avatar, id, state.members.get(id)?.display_name || "Участник"); + const details = document.createElement("span"); details.className = "member-details"; + const code = document.createElement("code"); code.textContent = id; details.append(label, code); item.append(avatar, details); return item; + })); + $("members-dialog").showModal(); +}); +$("logout").addEventListener("click", async () => { + $("logout").disabled = true; + try { await request("/api/v1/auth/sessions/current", { method: "DELETE" }); endSession(); } + catch (error) { if (error.status !== 401) toast(error.message, true); } + finally { $("logout").disabled = false; } +}); +$("back-to-chats").addEventListener("click", () => { + $("app-screen").classList.remove("chat-open"); + const row=chatRows.get(state.current?.id)?.button; + if(row?.isConnected)row.focus({preventScroll:true}); +}); +$("refresh-messages").addEventListener("click", async () => { + try { await syncRecent(); } catch (error) { toast(error.message, true); } +}); + +setInterval(async () => { + if (!state.session || document.hidden) return; + if (new Date(state.session.expires_at).getTime() <= Date.now()) return endSession("Сессия закончилась. Войдите снова."); + if (state.current) { try { await syncRecent(false, true); } catch { /* manual actions still show errors */ } } + if (!state.chatsCursor && state.chats.length <= 100 && Date.now() - lastChatRefresh > 12000) { + try { await loadChats(false, true); } catch { /* manual refresh shows errors */ } + } +}, 4000); + +async function resume() { + try { + const stored = sessionStorage.getItem(storageKey); + if (!stored) return; + const session = JSON.parse(stored); + if (!session?.token || !session.user || !(new Date(session.expires_at).getTime() > Date.now())) return endSession(); + state.session = session; + attachmentSession(session.token); + operationsSession(session.token); + const user = await request("/api/v1/users/me"); + await enterSession({ ...session, user }); + } catch (error) { + state.session = null; + attachmentSession(null); + operationsSession(null); + clearAttachedDrafts(); + if (error instanceof ApiError) notice("auth-notice", error.message); + else { try { sessionStorage.removeItem(storageKey); } catch { /* storage unavailable */ } } + } +} + +void resume(); +startClusterView(() => request('/api/ui/cluster', {background:true}), () => state.session?.token); + +function clearAttachedDrafts(chatId) { + for (const [id, files] of attachedDrafts) { + if (chatId && id !== chatId) continue; + for (const file of files) { + file.removed = true; + if (file.previewUrl) URL.revokeObjectURL(file.previewUrl); + } + attachedDrafts.delete(id); + } +} +function renderAttachedDrafts() { + const root = $("attached-files"); root.replaceChildren(); + for (const file of attachedDrafts.get(state.current?.id) || []) { + const item = document.createElement("div"); item.className = "draft-image"; + const image = document.createElement("img"); image.src = file.previewUrl; image.alt = file.filename; + const label = document.createElement("span"); label.className="draft-image-name"; label.textContent=file.filename;label.title=file.filename; + const remove = document.createElement("button"); remove.type="button";remove.className="draft-image-remove";remove.textContent="×"; + remove.setAttribute("aria-label","Убрать " + file.filename);remove.title="Убрать изображение";remove.disabled=state.sending; + remove.addEventListener("click",()=>{ + const chat = state.current.id; + file.removed=true;URL.revokeObjectURL(file.previewUrl); + attachedDrafts.set(chat,(attachedDrafts.get(chat)||[]).filter(item=>item!==file)); + renderAttachedDrafts();updateComposer(); + }); + item.append(image,label,remove); + if(!file.id) { + const status=document.createElement("span");status.className="draft-image-loading";status.textContent="Загрузка…";status.setAttribute("role","status");item.append(status); + } + root.append(item); + } +} +$("pick-file").addEventListener("click",()=>$("image-file").click()); +$("image-file").addEventListener("change",()=>{ + const file=$("image-file").files[0];$("image-file").value="";void attachImage(file); +}); +async function attachImage(file) { + if(!file || !state.current || uploadBusy || state.sending) return; + if(file.size>10485760) {toast("Максимум 10 МБ",true);return;} + if(!["image/png","image/jpeg"].includes(file.type)) {toast("Выберите PNG или JPEG",true);return;} + if((attachedDrafts.get(state.current.id)||[]).length>=10) {toast("Максимум 10 вложений",true);return;} + const chat=state.current.id, session=state.session.token; + const filename=file.name && !/[\/\\\x00-\x1f]/.test(file.name) ? file.name : (file.type === "image/png" ? "image.png" : "image.jpg"); + const draft={filename,previewUrl:URL.createObjectURL(file),id:null,removed:false}; + attachedDrafts.set(chat,[...(attachedDrafts.get(chat)||[]),draft]);renderAttachedDrafts(); + uploadBusy=true;$("pick-file").disabled=true;updateComposer(); + try { + const attachment=await request(`/api/v1/chats/${chat}/attachments?filename=${encodeURIComponent(filename)}`,{method:"POST",raw:file,headers:{"Content-Type":file.type}}); + if(state.session?.token!==session || draft.removed) return; + Object.assign(draft,attachment);renderAttachedDrafts(); + }catch(error){ + if(!draft.removed) { + draft.removed=true;URL.revokeObjectURL(draft.previewUrl); + attachedDrafts.set(chat,(attachedDrafts.get(chat)||[]).filter(item=>item!==draft));renderAttachedDrafts(); + } + if(state.session?.token===session) toast(error.message,true); + } + finally {uploadBusy=false;$("pick-file").disabled=state.sending;updateComposer();} +} + +const dropArea = $("conversation"); +let dragDepth = 0; +function resetDrag() { dragDepth = 0; dropArea.classList.remove("is-dragging-file"); } +for (const type of ["dragenter", "dragover"]) dropArea.addEventListener(type, event => { + if (!event.dataTransfer?.types.includes("Files") || !state.current || state.sending) return; + event.preventDefault(); + if (type === "dragenter") dragDepth++; + event.dataTransfer.dropEffect = "copy"; + dropArea.classList.add("is-dragging-file"); +}); +dropArea.addEventListener("dragleave", event => { + if (!event.dataTransfer?.types.includes("Files")) return; + dragDepth = Math.max(0, dragDepth - 1); + if (!dragDepth) resetDrag(); +}); +dropArea.addEventListener("drop", event => { + if (!event.dataTransfer?.types.includes("Files")) return; + event.preventDefault(); resetDrag(); + if (uploadBusy || state.sending) { toast("Дождитесь завершения загрузки", true); return; } + const files = [...event.dataTransfer.files]; + if (files.length > 1) { toast("Добавляйте изображения по одному", true); return; } + void attachImage(files[0]); +}); +$("message-input").addEventListener("paste", event => { + const file = [...(event.clipboardData?.files || [])].find(item => item.type.startsWith("image/")); + if (!file) return; + event.preventDefault(); + if (uploadBusy || state.sending) { toast("Дождитесь завершения загрузки", true); return; } + void attachImage(file); +}); + +window.addEventListener("dragend", resetDrag); +window.addEventListener("drop", resetDrag); diff --git a/src/Messenger.Api/wwwroot/assets/chat-pattern.svg b/src/Messenger.Api/wwwroot/assets/chat-pattern.svg new file mode 100644 index 00000000..27e307c1 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/chat-pattern.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/Messenger.Api/wwwroot/assets/conversation-grid.svg b/src/Messenger.Api/wwwroot/assets/conversation-grid.svg new file mode 100644 index 00000000..d7eadc66 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/conversation-grid.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/activity.svg b/src/Messenger.Api/wwwroot/assets/icons/activity.svg new file mode 100644 index 00000000..49753abc --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/activity.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/api.svg b/src/Messenger.Api/wwwroot/assets/icons/api.svg new file mode 100644 index 00000000..f1a8e0f5 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/api.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/browser.svg b/src/Messenger.Api/wwwroot/assets/icons/browser.svg new file mode 100644 index 00000000..a708046a --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/browser.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/cache.svg b/src/Messenger.Api/wwwroot/assets/icons/cache.svg new file mode 100644 index 00000000..17be8d7d --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/cache.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/chat.svg b/src/Messenger.Api/wwwroot/assets/icons/chat.svg new file mode 100644 index 00000000..6a535f1e --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/chat.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/check.svg b/src/Messenger.Api/wwwroot/assets/icons/check.svg new file mode 100644 index 00000000..cd469e6e --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/check.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/clock.svg b/src/Messenger.Api/wwwroot/assets/icons/clock.svg new file mode 100644 index 00000000..fd3ef0a5 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/clock.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/database.svg b/src/Messenger.Api/wwwroot/assets/icons/database.svg new file mode 100644 index 00000000..e70b753e --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/database.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/dispatcher.svg b/src/Messenger.Api/wwwroot/assets/icons/dispatcher.svg new file mode 100644 index 00000000..1cd18e4f --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/dispatcher.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/error.svg b/src/Messenger.Api/wwwroot/assets/icons/error.svg new file mode 100644 index 00000000..d11d908b --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/error.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/image.svg b/src/Messenger.Api/wwwroot/assets/icons/image.svg new file mode 100644 index 00000000..477545c7 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/image.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/overview.svg b/src/Messenger.Api/wwwroot/assets/icons/overview.svg new file mode 100644 index 00000000..235f80d1 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/overview.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/proxy.svg b/src/Messenger.Api/wwwroot/assets/icons/proxy.svg new file mode 100644 index 00000000..e57ac697 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/proxy.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/queue.svg b/src/Messenger.Api/wwwroot/assets/icons/queue.svg new file mode 100644 index 00000000..f2aeb73a --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/queue.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/storage.svg b/src/Messenger.Api/wwwroot/assets/icons/storage.svg new file mode 100644 index 00000000..ca686578 --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/storage.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/icons/worker.svg b/src/Messenger.Api/wwwroot/assets/icons/worker.svg new file mode 100644 index 00000000..c87fa75e --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/icons/worker.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-empty-320.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-empty-320.webp new file mode 100644 index 00000000..567ace88 Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-empty-320.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-error-320.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-error-320.webp new file mode 100644 index 00000000..b085ea9b Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-error-320.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-message-320.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-message-320.webp new file mode 100644 index 00000000..5cf87cce Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-message-320.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-message-640.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-message-640.webp new file mode 100644 index 00000000..df936fa6 Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-message-640.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-reconnect-320.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-reconnect-320.webp new file mode 100644 index 00000000..b58bf0b8 Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-reconnect-320.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-welcome-320.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-welcome-320.webp new file mode 100644 index 00000000..7f8c3609 Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-welcome-320.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh-welcome-640.webp b/src/Messenger.Api/wwwroot/assets/kolovertysh-welcome-640.webp new file mode 100644 index 00000000..3a4e5967 Binary files /dev/null and b/src/Messenger.Api/wwwroot/assets/kolovertysh-welcome-640.webp differ diff --git a/src/Messenger.Api/wwwroot/assets/kolovertysh.svg b/src/Messenger.Api/wwwroot/assets/kolovertysh.svg new file mode 100644 index 00000000..29d1871a --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/kolovertysh.svg @@ -0,0 +1,63 @@ + diff --git a/src/Messenger.Api/wwwroot/assets/system-icons.svg b/src/Messenger.Api/wwwroot/assets/system-icons.svg new file mode 100644 index 00000000..8ff9280c --- /dev/null +++ b/src/Messenger.Api/wwwroot/assets/system-icons.svg @@ -0,0 +1 @@ + diff --git a/src/Messenger.Api/wwwroot/attachments.css b/src/Messenger.Api/wwwroot/attachments.css new file mode 100644 index 00000000..7b0a5ea3 --- /dev/null +++ b/src/Messenger.Api/wwwroot/attachments.css @@ -0,0 +1,83 @@ +.attachment-card { display:grid; gap:0; width:min(320px, 58vw); max-width:100%; margin:5px 0 0; } +.attachment-preview { display:grid; position:relative; width:100%; aspect-ratio:4/3; padding:0; border:0; border-radius:12px; overflow:hidden; background:#00000016; cursor:zoom-in; color:inherit; isolation:isolate; } +.attachment-preview img { width:100%; height:100%; object-fit:cover; display:block; opacity:0; transition:opacity .18s; } +.attachment-card.is-ready .attachment-preview img { opacity:1; } +.attachment-status { position:absolute; inset:0; display:grid; place-items:center; padding:24px; font-size:12px; text-align:center; line-height:1.6; } +.is-ready .attachment-status { display:none; } +.attachment-expand { position:absolute; bottom:10px; right:10px; display:grid; place-items:center; width:30px; height:30px; background:#1616169c; color:#fff; border-radius:9px; opacity:0; transition:opacity .16s; } +.attachment-preview:hover .attachment-expand, .attachment-preview:focus-visible .attachment-expand { opacity:1; } +.attachment-info { display:flex; gap:10px; align-items:center; padding:9px 0 2px; } +.attachment-label { display:grid; flex:1; gap:3px; min-width:0; } +.attachment-name { font-size:12px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; font-weight:550; } +.attachment-size { font-size:10px; opacity:.65; } +.attachment-download { display:grid; place-items:center; width:35px; height:35px; border:1px solid #ffffff22; border-radius:10px; color:inherit; background:#ffffff08; cursor:pointer; } +.mine .attachment-download { border-color:#26262625; background:#26262608; } +.attachment-download:hover { background:#ffffff20; } +.attachment-download:disabled { opacity:.3; cursor:default; } +.attachment-card svg, .image-viewer svg { width:20px; height:20px; fill:none; stroke:currentColor; stroke-width:1.7; stroke-linecap:round; stroke-linejoin:round; } +.attachment-preview:focus-visible, .attachment-download:focus-visible, .image-viewer button:focus-visible { outline:2px solid var(--accent); outline-offset:3px; } +.mine .attachment-preview:focus-visible, .mine .attachment-download:focus-visible { outline-color:#262626; } +.composer { grid-template-columns:40px minmax(0,1fr) 46px; } +#pick-file { height:47px; width:40px; margin-bottom:1px; } +.attached-files { grid-column:1/-1; order:-1; display:flex; gap:10px; overflow-x:auto; padding:4px 2px 7px; min-width:0; } +.attached-files:empty { display:none; } +.draft-image { width:84px; flex-shrink:0; position:relative; border-radius:12px; } +.draft-image > img { width:84px; height:76px; display:block; object-fit:cover; border-radius:12px; border:1px solid #ffffff20; } +.draft-image-name { display:block; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; font-size:10px; color:var(--muted); padding-top:5px; } +.draft-image-remove { position:absolute; top:3px; right:3px; display:grid; place-items:center; width:25px; height:25px; font-size:21px; line-height:1; color:white; background:#171717c9; border:1px solid #ffffff30; border-radius:50%; cursor:pointer; } +.draft-image-remove:disabled { opacity:.4; cursor:default; } +.draft-image-loading { position:absolute; top:0; height:76px; inset-inline:0; display:grid; align-items:end; padding:7px 4px; background:linear-gradient(transparent,#000b); color:white; border-radius:12px; font-size:10px; text-align:center; pointer-events:none; } +.viewing-image { overflow:hidden; } +dialog.image-viewer { position:fixed; inset:0; width:100%; height:100%; width:100dvw; height:100dvh; max-width:none; max-height:none; margin:0; padding:0; border:0; border-radius:0; background:#141414f7; color:#f4f3ed; overflow:hidden; box-shadow:none; } +.image-viewer[open] { display:grid; grid-template-rows:auto minmax(0,1fr); } +.image-viewer::backdrop { background:#141414; backdrop-filter:none; } +.image-viewer-header { display:flex; gap:20px; align-items:center; justify-content:space-between; padding:18px max(20px,env(safe-area-inset-right)) 18px max(24px,env(safe-area-inset-left)); border-bottom:1px solid #ffffff10; background:#1d1d1d; z-index:2; } +.image-viewer-label { min-width:0; display:grid; gap:5px; } +.image-viewer-label strong { font-weight:550; font-size:14px; white-space:nowrap; text-overflow:ellipsis; overflow:hidden; } +.image-viewer-label > span { font-size:11px; color:#a7a79d; } +.image-viewer-actions { display:flex; gap:8px; flex-shrink:0; } +.image-viewer button { display:grid; place-items:center; color:inherit; background:#ffffff0b; border:1px solid #ffffff14; border-radius:12px; cursor:pointer; padding:0; width:42px; height:42px; transition:background .15s; } +.image-viewer button:hover { background:#ffffff20; } +.image-viewer button:disabled { opacity:.35; cursor:default; } +.image-viewer-stage { display:grid; place-items:center; position:relative; overflow:auto; padding:24px 70px max(24px,env(safe-area-inset-bottom)); min-height:0; min-width:0; } +.image-viewer-stage > img { display:block; max-width:100%; max-height:100%; min-height:0; object-fit:contain; border-radius:3px; user-select:none; -webkit-user-drag:none; } +.image-viewer-stage > img:not([src]) { display:none; } +.image-viewer-status { position:absolute; text-align:center; font-size:13px; color:#c8c8b9; max-width:300px; padding:20px; } +.image-viewer-status:empty { display:none; } +.image-viewer.is-zoomed .image-viewer-stage { display:block; padding:24px; } +.image-viewer.is-zoomed .image-viewer-stage > img { max-width:none; max-height:none; width:auto; height:auto; margin:auto; cursor:zoom-out; } +.image-viewer .image-viewer-prev, .image-viewer .image-viewer-next { position:absolute; top:calc(50% + 35px); background:#262626cc; width:44px; height:52px; z-index:1; } +.image-viewer-prev { left:16px; } .image-viewer-next { right:16px; } +@media (max-width:600px) { + .image-viewer-header { padding:12px; gap:8px; } + .image-viewer-label strong { font-size:12px; } + .image-viewer-actions { gap:5px; } + .image-viewer-actions button { width:36px; height:38px; border-radius:10px; } + .image-viewer-stage { padding:14px 12px 68px; } + .image-viewer .image-viewer-prev, .image-viewer .image-viewer-next { top:auto; bottom:max(12px,env(safe-area-inset-bottom)); height:38px; width:44px; } + .image-viewer-prev { left:calc(50% - 52px); } .image-viewer-next { right:calc(50% - 52px); } + .attachment-expand { opacity:1; } +} +@media (prefers-reduced-motion:reduce) { .attachment-preview img, .attachment-expand, .image-viewer button { transition:none; } } +.processing-link { border:0; background:transparent; color:inherit; text-decoration:underline; font:inherit; font-size:11px; cursor:pointer; padding:8px 3px; } +.processing-panel { width:min(900px,94vw); max-height:90dvh; padding:0; border:1px solid #ffffff25; border-radius:20px; background:#262626; color:#f4f3ed; } +.processing-panel::backdrop { background:#000a; } +.processing-panel section { padding:24px; } +.processing-panel header,.processing-summary { display:flex; align-items:center; justify-content:space-between; gap:16px; } +.processing-panel header { margin-bottom:24px; } +.processing-panel h2 { margin:0; font-size:21px; } +.processing-panel h3 { font-size:14px; margin-top:26px; } +.processing-panel button,.processing-results a { background:#ffffff0b; color:inherit; border:1px solid #ffffff25; border-radius:8px; padding:8px 12px; cursor:pointer; } +.processing-summary strong { overflow-wrap:anywhere; } +.processing-badge { color:#ffe135; white-space:nowrap; } +.processing-steps { display:flex; padding:0; list-style:none; gap:8px; margin:24px 0; } +.processing-steps li { flex:1; border-top:3px solid #ffffff20; padding-top:10px; font-size:12px; color:#aaa; } +.processing-steps .reached { border-color:#ffe135; color:#ffe135; } +.processing-history { padding-left:20px; color:#c3c3b8; font-size:12px; line-height:2; } +.processing-results { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:16px; } +.processing-results article { background:#ffffff05; border:1px solid #ffffff15; border-radius:12px; padding:14px; } +.processing-results img { display:block; width:100%; height:150px; object-fit:contain; margin-bottom:12px; background:#151515; border-radius:6px; } +.processing-results p { font-size:12px; color:#c3c3b8; } +.processing-results a { display:inline-block; text-decoration:none; font-size:12px; margin:4px 8px 0 0; } +.processing-error { color:#ffaaa0; } +@media(max-width:550px) { .processing-panel section {padding:16px;} .processing-results {grid-template-columns:1fr;} .processing-panel h2 {font-size:17px;} } diff --git a/src/Messenger.Api/wwwroot/attachments.js b/src/Messenger.Api/wwwroot/attachments.js new file mode 100644 index 00000000..9d634ef1 --- /dev/null +++ b/src/Messenger.Api/wwwroot/attachments.js @@ -0,0 +1,208 @@ +import { openProcessing, closeProcessing } from './processing.js'; +let token = null, generation = 0, activeLoads = 0; +const cards = new Set(), waiters = []; +let active = null, viewer = null, returnFocus = null; +const observer = new IntersectionObserver(entries => { + for (const entry of entries) { + const card = entry.target.attachment; + card.visible = entry.isIntersecting; + if (card.visible) void load(card).catch(() => {}); + else if (active !== card) release(card); + } +}, { rootMargin: '400px' }); + +const paths = { + download: 'M12 3v12m-5-5 5 5 5-5M5 17v4h14v-4', + close: 'm6 6 12 12M18 6 6 18', + zoom: 'M10 4a6 6 0 1 0 0 12 6 6 0 0 0 0-12m4.5 10.5L21 21M7 10h6m-3-3v6', + left: 'm15 5-7 7 7 7', right: 'm9 5 7 7-7 7' +}; +function icon(name) { + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); + svg.setAttribute('viewBox', '0 0 24 24'); svg.setAttribute('aria-hidden', 'true'); + const path = document.createElementNS(svg.namespaceURI, 'path'); path.setAttribute('d', paths[name]); + svg.append(path); return svg; +} +function control(label, name, action) { + const button = document.createElement('button'); button.type = 'button'; + button.title = label; button.setAttribute('aria-label', label); button.append(icon(name)); + button.addEventListener('click', action); return button; +} +function bytes(size) { return size < 1048576 ? Math.max(1, Math.round(size / 1024)) + ' КБ' : (size / 1048576).toFixed(1) + ' МБ'; } + +export function attachmentSession(value) { + generation++; token = value; closeProcessing(); + if (viewer?.open) viewer.close(); + clearAttachmentCards(); +} +export function clearAttachmentCards() { + closeProcessing(); + if (viewer?.open) viewer.close(); + for (const card of cards) { card.disposed = true; observer.unobserve(card.root); release(card); } + cards.clear(); +} +function release(card) { + card.controller?.abort(); + if (card.url) URL.revokeObjectURL(card.url); + card.download.disabled = true; + card.url = null; card.image.removeAttribute('src'); card.root.classList.remove('is-ready'); +} +async function load(card) { + if (card.url) return; + if (card.promise) return card.promise; + const session = token, version = generation; + if (!session || card.disposed) throw new Error('Сессия недоступна'); + card.promise = (async () => { + if (activeLoads >= 2) await new Promise(resolve => waiters.push(resolve)); + activeLoads++; + try { + if (card.disposed || version !== generation || (!card.visible && active !== card)) return; + card.root.classList.remove('has-error'); card.status.textContent = 'Загрузка изображения…'; + const controller = new AbortController(); card.controller = controller; + const signal = controller.signal; + const options = {headers:{Authorization:'Bearer ' + session},credentials:'omit',cache:'no-store',signal}; + let firstCheck=true; + while (firstCheck || !card.meta || card.meta.status === 'queued' || card.meta.status === 'processing') { + firstCheck=false; + const response = await fetch(`/api/v1/attachments/${card.id}`, {...options, signal:AbortSignal.any([signal,AbortSignal.timeout(15000)])}); + if (!response.ok) throw new Error('Изображение недоступно'); + card.meta = await response.json(); + card.name.textContent = card.meta.filename; + card.size.textContent = bytes(card.meta.size_bytes); + card.open.setAttribute('aria-label', 'Открыть ' + card.meta.filename + ' на весь экран'); + if (card.meta.status === 'queued' || card.meta.status === 'processing') { + card.status.textContent = card.meta.status === 'queued' ? 'В очереди…' : 'Обрабатывается…'; + if(active === card && viewer?.open) viewer.status.textContent=card.status.textContent; + await new Promise((resolve,reject)=>{ + const done=()=>{signal.removeEventListener('abort',cancel);resolve();}; + const timer=setTimeout(done,1200); + const cancel=()=>{clearTimeout(timer);reject(new DOMException('Cancelled','AbortError'));}; + signal.addEventListener('abort',cancel,{once:true}); + if(signal.aborted)cancel(); + }); + } + } + if(card.meta.status === 'failed') throw new Error('Не удалось обработать изображение'); + const response = await fetch(`/api/v1/attachments/${card.id}/content`, options); + if (!response.ok) throw new Error('Изображение недоступно'); + const blob = await response.blob(); + if (card.disposed || version !== generation || controller.signal.aborted) return; + card.url = URL.createObjectURL(blob); card.image.src = card.url; + card.image.alt = card.meta.filename; card.root.classList.add('is-ready'); + card.download.disabled = false; + card.status.textContent = ''; + } catch (error) { + if (error.name !== 'AbortError' && !card.disposed && version === generation) { + card.root.classList.add('has-error'); card.status.textContent = card.meta?.status === 'failed' ? 'Не удалось обработать изображение' : 'Не удалось загрузить. Нажмите, чтобы повторить.'; + } + throw error; + } finally { + card.promise = null; activeLoads--; waiters.shift()?.(); + if (card.controller?.signal.aborted && !card.disposed && version === generation && (card.visible || active === card)) + queueMicrotask(() => { void load(card).catch(() => {}); }); + } + })(); + return card.promise; +} + +export function attachmentCard(id) { + const root = document.createElement('div'); root.className = 'attachment-card'; + const image = document.createElement('img'); image.alt = ''; image.decoding = 'async'; + const open = document.createElement('button'); open.type = 'button'; open.className = 'attachment-preview'; + open.setAttribute('aria-label', 'Открыть изображение на весь экран'); + const status = document.createElement('span'); status.className = 'attachment-status'; status.textContent = 'Загрузка изображения…'; + const hint = document.createElement('span'); hint.className = 'attachment-expand'; hint.append(icon('zoom')); + open.append(image, status, hint); + const info = document.createElement('div'); info.className = 'attachment-info'; + const label = document.createElement('span'); label.className = 'attachment-label'; + const name = document.createElement('span'); name.className = 'attachment-name'; name.textContent = 'Изображение'; + const size = document.createElement('span'); size.className = 'attachment-size'; label.append(name, size); + const card = {id, root, open, image, status, name, size, url:null, meta:null, visible:false, disposed:false, promise:null}; + const download = control('Скачать изображение', 'download', () => save(card)); + download.className = 'attachment-download'; download.disabled = true; card.download = download; + const processing = document.createElement('button'); processing.type='button'; processing.className='processing-link'; processing.textContent='Обработка'; processing.addEventListener('click',()=>openProcessing(card.id,token)); + info.append(label, processing, download); root.append(open, info); root.attachment = card; + open.addEventListener('click', () => { if (root.classList.contains('has-error')) { void load(card).catch(() => {}); } else openViewer(card); }); + cards.add(card); observer.observe(root); return root; +} +function save(card) { + if (!card?.url) return; + const link = document.createElement('a'); link.href = card.url; link.download = card.meta?.filename || 'image'; + document.body.append(link); link.click(); link.remove(); +} +function gallery() { + return [...cards].filter(card => !card.disposed && card.root.isConnected).sort((a,b) => + a.root.compareDocumentPosition(b.root) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1); +} +function move(direction) { + const items = gallery(), index = items.indexOf(active), next = items[index + direction]; + if (next) void show(next); +} +function zoom() { + if (!active?.url) return; + viewer.classList.toggle('is-zoomed'); + viewer.zoom.setAttribute('aria-pressed', String(viewer.classList.contains('is-zoomed'))); + viewer.stage.scrollTo(0, 0); +} +function createViewer() { + const dialog = document.createElement('dialog'); dialog.className = 'image-viewer'; + dialog.setAttribute('aria-label', 'Просмотр изображения'); + const header = document.createElement('header'); header.className = 'image-viewer-header'; + const label = document.createElement('div'); label.className = 'image-viewer-label'; + const name = document.createElement('strong'), detail = document.createElement('span');label.append(name, detail); + const actions = document.createElement('div');actions.className = 'image-viewer-actions'; + const zoomButton = control('Увеличить изображение', 'zoom', zoom); zoomButton.setAttribute('aria-pressed','false'); + const download = control('Скачать оригинал', 'download', () => save(active)); + const close = control('Закрыть просмотр', 'close', () => dialog.close()); + actions.append(zoomButton, download, close); header.append(label, actions); + const stage = document.createElement('div'); stage.className = 'image-viewer-stage'; + const image = document.createElement('img'); image.alt = ''; + const status = document.createElement('p');status.className = 'image-viewer-status';status.setAttribute('role','status'); + stage.append(image, status); + const previous = control('Предыдущее изображение', 'left', () => move(-1)); previous.className = 'image-viewer-prev'; + const next = control('Следующее изображение', 'right', () => move(1)); next.className = 'image-viewer-next'; + dialog.append(header, stage, previous, next); document.body.append(dialog); + Object.assign(dialog, {picture:image, nameLabel:name, detail, status, stage, download, zoom:zoomButton, previous, next}); + dialog.addEventListener('click', event => { if (event.target === stage || event.target === dialog) dialog.close(); }); + image.addEventListener('dblclick', zoom); + dialog.addEventListener('keydown', event => { + if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') { event.preventDefault(); move(event.key === 'ArrowLeft' ? -1 : 1); } + }); + let touch = null; + stage.addEventListener('touchstart', e => {touch = e.touches.length === 1 ? [e.touches[0].clientX, e.touches[0].clientY] : null;},{passive:true}); + stage.addEventListener('touchend', e => { + if (!touch || dialog.classList.contains('is-zoomed')) return; + const dx=e.changedTouches[0].clientX-touch[0], dy=e.changedTouches[0].clientY-touch[1];touch=null; + if (Math.abs(dx)>65 && Math.abs(dx)>Math.abs(dy)*1.5) move(dx<0?1:-1); + },{passive:true}); + dialog.addEventListener('close', () => { + const previousCard=active;active=null;dialog.picture.removeAttribute('src'); + document.documentElement.classList.remove('viewing-image'); + if(previousCard && !previousCard.visible) release(previousCard); + if(returnFocus?.isConnected) returnFocus.focus({preventScroll:true}); + }); + return dialog; +} +function openViewer(card) { + if (!viewer) viewer=createViewer(); + if (!viewer.open) {returnFocus=document.activeElement;viewer.showModal();document.documentElement.classList.add('viewing-image');} + void show(card); +} +async function show(card) { + const previous=active;active=card; + if(previous && previous!==card && !previous.visible) release(previous); + viewer.classList.remove('is-zoomed');viewer.zoom.setAttribute('aria-pressed','false'); + viewer.stage.scrollTo(0,0);viewer.picture.removeAttribute('src');viewer.picture.alt=''; + viewer.nameLabel.textContent=card.meta?.filename || 'Изображение';viewer.detail.textContent=''; + viewer.status.textContent='Загрузка…';viewer.download.disabled=true;viewer.zoom.disabled=true; + const items=gallery(), index=items.indexOf(card); + viewer.previous.hidden=index<=0;viewer.next.hidden=index===items.length-1; + try { + await load(card); + if(active!==card || !viewer.open || !card.url) return; + viewer.picture.src=card.url;viewer.picture.alt=card.meta.filename; + viewer.nameLabel.textContent=card.meta.filename; + viewer.detail.textContent=`${index+1} из ${items.length} · ${bytes(card.meta.size_bytes)}`; + viewer.status.textContent='';viewer.download.disabled=false;viewer.zoom.disabled=false; + } catch {if(active===card) viewer.status.textContent='Изображение недоступно. Закройте просмотр и повторите загрузку.';} +} diff --git a/src/Messenger.Api/wwwroot/avatars.js b/src/Messenger.Api/wwwroot/avatars.js new file mode 100644 index 00000000..2b68db1d --- /dev/null +++ b/src/Messenger.Api/wwwroot/avatars.js @@ -0,0 +1,42 @@ +const svgNS = "http://www.w3.org/2000/svg"; +const palettes = [ + ["#394639", "#c2d39b"], ["#474033", "#e5c38b"], + ["#3c414d", "#b9c9e4"], ["#493b42", "#e4b6c5"], + ["#444333", "#e0d29a"], ["#354647", "#afd5ce"] +]; + +function element(tag, attributes = {}) { + const node = document.createElementNS(svgNS, tag); + for (const [key, value] of Object.entries(attributes)) node.setAttribute(key, value); + return node; +} + +export function initials(name) { + return (name.match(/[\p{L}\p{N}]+/gu) || []).slice(0, 2) + .map(part => Array.from(part)[0]).join("").toUpperCase(); +} + +export function paintAvatar(target, id, name) { + if (target.dataset.identity === id && target.dataset.name === name) return; + let hash = 2166136261; + for (const character of id) hash = Math.imul(hash ^ character.charCodeAt(0), 16777619) >>> 0; + const [background, foreground] = palettes[hash % palettes.length]; + const svg = element("svg", { viewBox: "0 0 64 64", class: "avatar-art", "aria-hidden": "true" }); + svg.append(element("rect", { width: 64, height: 64, rx: 20, fill: background })); + const patterns = [ + "M-7 18Q21-7 39 9T74 18M-8 49Q21 27 40 44T74 49", + "M-3 3 28 3 64 39M-3 21 21 21 64 64M-3 39 14 39 39 64", + "M7 59Q-6 24 25 8Q60-7 70 26M-2 42Q23 69 54 49Q75 31 52 8" + ]; + svg.append(element("path", { d: patterns[(hash >>> 4) % patterns.length], fill: "none", + stroke: foreground, "stroke-width": 6, opacity: .12 })); + svg.append(element("circle", { cx: 50, cy: 13, r: 3, fill: foreground, opacity: .5 })); + const label = element("text", { x: 32, y: 34, fill: foreground, + "text-anchor": "middle", "dominant-baseline": "middle", "font-size": 22, + "font-family": "system-ui, sans-serif", "font-weight": 650 }); + label.textContent = initials(name); + svg.append(label); + target.replaceChildren(svg); + target.dataset.identity = id; + target.dataset.name = name; +} diff --git a/src/Messenger.Api/wwwroot/cluster-view.js b/src/Messenger.Api/wwwroot/cluster-view.js new file mode 100644 index 00000000..ab9f1588 --- /dev/null +++ b/src/Messenger.Api/wwwroot/cluster-view.js @@ -0,0 +1,90 @@ +const $ = id => document.getElementById(id); +const observed = new Map(); +const cards = new Map(); +let signedIn = false, lastInstance = null, total = 0, pulseTimer; + +export function setClusterIdentity(active) { + signedIn = active; + observed.clear(); cards.clear(); total = 0; lastInstance = null; + $('cluster-instances').replaceChildren(); + $('cluster-status').textContent = active ? 'Получаем heartbeat…' : 'Схема доступна после входа'; + $('cluster-detail').textContent = 'Подсветка — реальный X-Instance-Id ответа.'; + $('cluster-copy').disabled = true; + for (const id of ['cluster-postgres', 'cluster-redis', 'cluster-balancer']) $(id).dataset.state = 'unknown'; +} + +export function observeClusterRequest(entry) { + if (!signedIn || !entry.instanceId) return; + total++; + lastInstance = entry.instanceId; + observed.set(lastInstance, (observed.get(lastInstance) || 0) + 1); + for (const [id, card] of cards) { + card.root.classList.toggle('just-served', id === lastInstance); + card.count.textContent = `${observed.get(id) || 0} / ${total} запросов`; + } + $('cluster-balancer').dataset.state = 'up'; + $('cluster-detail').textContent = `${entry.method} · ${entry.status ?? 'нет ответа'} · ${entry.durationMs} мс → ${lastInstance.slice(0, 8)}`; + clearTimeout(pulseTimer); + pulseTimer = setTimeout(() => { for (const card of cards.values()) card.root.classList.remove('just-served'); }, 1100); +} + +function render(snapshot) { + $('cluster-redis').dataset.state = snapshot.redis_available ? 'up' : 'down'; + $('cluster-postgres').dataset.state = snapshot.postgres_available ? 'up' : 'down'; + if (!snapshot.redis_available) { + $('cluster-status').textContent = 'Redis недоступен · состояние API неизвестно'; + for (const card of cards.values()) { card.root.dataset.state = 'unknown'; card.state.textContent = 'Нет heartbeat'; } + $('cluster-copy').disabled = true; + return; + } + // Keep outages witnessed by this tab, without presenting earlier deployments as new failures. + const visible = snapshot.instances.filter(node => node.ready || cards.has(node.id) || observed.has(node.id)); + const alive = visible.filter(node => node.ready), missing = visible.length - alive.length; + $('cluster-status').textContent = `${alive.length} API в сети${missing ? ` · ${missing} без heartbeat` : ''}`; + const wanted = new Set(visible.map(node => node.id)); + for (const [id, card] of cards) if (!wanted.has(id)) { card.root.remove(); cards.delete(id); } + for (const node of [...visible].sort((a,b) => a.id.localeCompare(b.id))) { + let card = cards.get(node.id); + if (!card) { + const root = document.createElement('div'); root.className = 'cluster-api cluster-node'; root.dataset.instanceId = node.id; + const heading = document.createElement('strong'); heading.textContent = `API · ${node.id.slice(0,8)}`; + const state = document.createElement('span'), count = document.createElement('small'); + root.append(heading, state, count); root.title = `X-Instance-Id: ${node.id}`; + card = {root, state, count}; cards.set(node.id, card); $('cluster-instances').append(root); + } + card.root.dataset.state = node.ready ? 'up' : 'down'; + card.state.textContent = node.ready ? 'В сети' : 'Heartbeat пропал'; + card.count.textContent = `${observed.get(node.id) || 0} / ${total} запросов`; + } + const target = alive.find(node => node.id === lastInstance) || alive[0]; + $('cluster-command').textContent = target ? `python3 scripts/demo-failover.py --instance ${target.id}` : 'Ожидаем работающие API…'; + $('cluster-copy').disabled = alive.length < 2; +} + +export function startClusterView(readSnapshot, getToken) { + let busy = false; + async function refresh() { + const token = getToken(); + if (!token || busy || document.hidden || $('diagnostics-panel').hidden) return; + busy = true; + try { const data = await readSnapshot(); if (token === getToken()) render(data); } + catch { + if (token === getToken()) { + $('cluster-status').textContent = 'Нет свежих данных о кластере'; + $('cluster-balancer').dataset.state = 'unknown'; + $('cluster-postgres').dataset.state = 'unknown'; + $('cluster-redis').dataset.state = 'unknown'; + for (const card of cards.values()) { card.root.dataset.state = 'unknown'; card.state.textContent = 'Нет свежих данных'; } + $('cluster-copy').disabled = true; + } + } finally { busy = false; } + } + new MutationObserver(refresh).observe($('diagnostics-panel'), { attributes: true, attributeFilter: ['hidden'] }); + setInterval(refresh, 2000); + $('cluster-probe').addEventListener('click', refresh); + $('cluster-copy').addEventListener('click', async () => { + try { await navigator.clipboard.writeText($('cluster-command').textContent); $('cluster-copy').textContent = 'Скопировано'; } + catch { $('cluster-copy').textContent = 'Выделите команду ниже'; } + setTimeout(() => { $('cluster-copy').textContent = 'Скопировать команду'; }, 2000); + }); +} diff --git a/src/Messenger.Api/wwwroot/design.css b/src/Messenger.Api/wwwroot/design.css new file mode 100644 index 00000000..c59bdf1b --- /dev/null +++ b/src/Messenger.Api/wwwroot/design.css @@ -0,0 +1,134 @@ +:root {--paper:#262626;--surface:#30312c;--border:#ffffff14;--ink:#eeeee2;--muted:#a8ac9b;--accent:#FFE135;--soft:#ffe1350c;--danger:#eeb09f;} +body {background:#1e201c;} +input,textarea {background:#252722;border-color:#ffffff1c;color:#eeeee2;} +input::placeholder,textarea::placeholder {color:#7d8571;} +.button {border-radius:11px;min-height:44px;} +.button.primary {box-shadow:0 4px 18px #ffe13508;} +.button.primary:hover {box-shadow:0 5px 22px #ffe13518;} +.button.secondary {background:#ffffff07;border-color:#ffffff1a;} +.icon-button {border-radius:10px;} +.icon-button:hover {background:#ffe1350b;} +.brand {color:#ffe135;letter-spacing:-1px;} +.app-shell {max-width:1560px;margin:16px auto;height:calc(100svh - 32px);border-radius:20px;border-color:#ffffff12;box-shadow:0 16px 80px #0003;background:#252722;} +.sidebar {background:#20221e;border-color:#ffffff0d;padding:27px 16px 0;} +.sidebar .brand {font-size:24px;} +.sidebar .brand-mark {width:30px;height:30px;} +.sidebar-heading {padding:29px 10px 18px;} +.sidebar-heading .eyebrow {display:none;} +.sidebar-heading h2 {font-size:21px;color:#eeeee2;font-weight:550;} +.search-field {background:#2a2d25;border-color:#ffffff0e;border-radius:11px;margin-bottom:16px;} +.search-field input {font-size:12px;} +.chat-row {padding:14px 10px;border-radius:12px;gap:12px;margin-bottom:5px;transition:background .18s,transform .18s,border-color .18s;} +.chat-row:hover {background:#ffffff04;transform:translateX(2px);} +.chat-row.active {background:#ffe1350a;border-color:#ffe13524;} +.chat-row-heading strong {font-weight:550;font-size:13px;} +.chat-preview {font-size:11px;color:#969e86;} +.chat-row-heading time {font-size:9px;color:#929b84;} +.avatar {border-radius:13px;} +.profile {border-color:#ffffff0b;} +.profile-text strong {font-weight:500;color:#e4e6d6;} +.profile-text span {font-size:10px;color:#8b947d;} +#open-operations {display:flex;align-items:center;gap:10px;padding:12px 14px;border:1px solid #ffe13525;border-radius:11px;background:#ffe13509;color:#ffe135;font-size:12px;margin:12px 5px 0;} +#open-operations svg {width:17px;height:17px;fill:none;stroke:currentColor;} +#open-operations::after {content:'↗';margin-left:auto;opacity:.6;} +#open-operations:hover {background:#ffe13511;} +.sidebar-diagnostics:not(#open-operations) {font-size:10px;color:#7e896f;margin-bottom:4px;} +.workspace {background:#262823;} +.chat-header {background:#292c25;border-bottom-color:#ffffff0d;padding:18px 26px;min-height:82px;} +.chat-heading h2 {font-size:16px;color:#eeeee2;font-weight:550;} +.chat-heading span {font-size:10px;color:#8e9a7d;} +.message-scroll {background-color:#252722;background-image:url('/assets/conversation-grid.svg');background-size:300px 240px;padding:12px 28px 26px;} +.message-list {gap:13px;} +.message-bubble {background:#32362b;border:1px solid #ffffff0d;border-radius:15px 15px 15px 4px;padding:12px 15px 9px;box-shadow:0 3px 8px #0000000a;} +.message-text {font-size:14px;line-height:1.65;color:#e8ebdf;} +.mine .message-bubble {background:#e8d44e;border-color:#e8d44e;border-radius:15px 15px 4px 15px;} +.mine .message-text {color:#282c1e;} +.message-time {font-size:9px;color:#929d7f;} +.mine .message-time {color:#655e2d;} +.message-author {font-size:10px;color:#b2c397;} +.date-separator {background:#ffffff05;border-color:#ffffff0b;color:#8c9879;font-size:9px;padding:5px 11px;backdrop-filter:blur(8px);} +.composer {background:#292c25;border-color:#ffffff0d;padding:16px 24px 14px;} +.composer textarea {background:#22251e;border-color:#ffffff17;border-radius:12px;line-height:1.5;} +.composer textarea:focus {border-color:#b3a739;box-shadow:0 0 0 3px #ffe13507;} +.composer-note {font-size:9px;color:#929e81;} +.send-button {border-radius:12px;} +.send-button:disabled {background:#3b4130;color:#7d896b;} +#pick-file {border:1px solid #ffffff13;background:#ffffff03;border-radius:11px;} +.attachment-card {border-radius:12px;padding:5px;background:#00000006;} +.attachment-preview {border-radius:10px;} +.attachment-name {font-size:11px;} +.attachment-size {font-size:9px;} +.processing-link {font-size:10px;text-decoration:none;border:1px solid currentColor;border-radius:7px;padding:6px 8px;opacity:.7;} +.processing-link:hover {opacity:1;} +.reaction-button,.reaction-picker {background:#2a2e24;border-color:#454e37;border-radius:9px;font-size:11px;} +.reaction-button[aria-pressed=true] {border-color:#b6a43c;background:#ffe1350d;} +.welcome {background:radial-gradient(ellipse at 50% 45%,#ffe13506,transparent 65%);} +.welcome-illustration {width:300px;max-width:75%;filter:drop-shadow(0 18px 25px #0002);} +.welcome h1 {font-size:27px;font-weight:550;letter-spacing:-.8px;color:#eceee0;margin:12px 0;} +.welcome p {font-size:12px;color:#909a80;} +.empty-conversation img {width:180px!important;height:auto;filter:drop-shadow(0 12px 16px #0002);} +.empty-conversation p {font-size:14px;font-weight:500;color:#dce2cf;} +.empty-conversation span {font-size:11px;color:#8a9679;} +.auth-layout {max-width:1440px;} +.intro {background:radial-gradient(ellipse at 40% 60%,#ffe13507,transparent 65%),#242721;} +.intro::before {border-color:#ffe1350e;} +.intro-copy h1 {font-family:inherit;font-size:43px;font-weight:550;letter-spacing:-1.5px;line-height:1.2;color:#eaeede;} +.auth-side {background:#2a2d26;border-color:#ffffff0d;} +.auth-switch {background:#20231d;border-color:#ffffff12;border-radius:12px;margin-bottom:34px;} +.auth-switch button {transition:background .25s,color .25s;} +.auth-card h2 {font-size:31px;letter-spacing:-1px;font-weight:550;color:#ecefe1;} +.auth-card form {gap:19px;} +.auth-card label {font-size:11px;color:#b8c1a8;} +.auth-card input {padding:14px;background:#22251e;border-color:#ffffff19;border-radius:10px;} +.auth-card input:focus {border-color:#b7a940;} +.auth-bottom {color:#7f8b6f;} +dialog:not(.ops-panel):not(.image-viewer) {background:#292d24;border-color:#ffffff17;border-radius:18px;color:#e7ecda;box-shadow:0 25px 90px #0007;} +dialog::backdrop {background:#10130db3;} +.dialog-heading h2 {font-size:21px;font-weight:550;letter-spacing:-.5px;} +.dialog-heading {border-color:#ffffff10;} +.processing-panel {width:min(950px,94vw);} +.processing-panel section {padding:26px;} +.processing-panel header {padding-bottom:17px;border-bottom:1px solid #ffffff0d;} +.processing-panel h2 {font-size:19px;font-weight:550;} +.processing-panel h3 {color:#b8c5a4;font-weight:500;} +.processing-summary {padding:8px 0;} +.processing-summary strong {font-size:13px;} +.processing-badge {font-size:11px;padding:5px 9px;border:1px solid #ffe13525;border-radius:7px;background:#ffe13507;} +.processing-results article {background:#22261d;border-color:#ffffff0e;} +.processing-results img {height:170px;border-radius:9px;background:#1d2118;} +.processing-results strong {font-size:12px;} +.processing-results a {border-color:#ffffff15;transition:background .18s;} +.processing-results a:hover {background:#ffe1350a;} +.toast {border-radius:11px;box-shadow:0 10px 40px #0004;} +@media(min-width:721px){.app-shell{grid-template-columns:300px minmax(0,1fr);}} +@media(min-width:721px) and (max-width:1100px){.app-shell{margin:12px;height:calc(100svh - 24px);grid-template-columns:270px minmax(0,1fr);}} +@media(max-width:720px){.app-shell{margin:0;height:100svh;border:0;border-radius:0;}.sidebar{padding:24px 14px 0;}.sidebar-heading{padding-top:24px;}.chat-header{padding:14px 16px;min-height:74px;}.message-scroll{padding:8px 15px 20px;}.composer{padding:12px 14px max(12px,env(safe-area-inset-bottom));}.message{max-width:88%;}.message-text{font-size:14px;}.auth-card h2{font-size:28px;}.processing-panel section{padding:17px;}.processing-panel h2{font-size:17px;}.processing-results img{height:150px;}} +@media(prefers-reduced-motion:no-preference){dialog[open]:not(.ops-panel):not(.image-viewer){animation:dialog-appear .22s cubic-bezier(.2,.7,.3,1);} .welcome-illustration{animation:welcome-appear .55s ease-out;} .chat-row:active{transform:scale(.985);} .ops-panel[open] .ops-metrics{animation:welcome-appear .35s ease-out;} } +@keyframes dialog-appear{from{opacity:0;transform:translateY(8px) scale(.985);}to{opacity:1;transform:none;}} +@keyframes welcome-appear{from{opacity:0;transform:translateY(9px);}to{opacity:1;transform:none;}} +@media(prefers-reduced-motion:reduce){.chat-row,.button,.icon-button,.processing-results a,.auth-switch button{transition:none;}.chat-row:hover{transform:none;}} +.search-field {position:relative;} +.search-field input[type=search]::-webkit-search-cancel-button {-webkit-appearance:none;} +.search-clear {display:grid;place-items:center;width:24px;height:24px;padding:0;border:0;border-radius:7px;background:#ffffff0b;color:#aeb8a3;font-size:18px;line-height:1;flex-shrink:0;} +.search-clear:hover {background:#ffe13518;color:#ffe135;} +.message {position:relative;} +.message-copy {position:absolute;right:-34px;bottom:2px;display:grid;place-items:center;width:28px;height:28px;border:1px solid #ffffff10;border-radius:8px;background:#2b2e27;color:#b2bca5;opacity:0;pointer-events:none;transition:opacity .18s,background .18s;} +.message-copy svg {width:15px;height:15px;fill:none;stroke:currentColor;stroke-width:1.7;stroke-linecap:round;stroke-linejoin:round;} +.message.mine .message-copy {right:auto;left:-34px;} +.message:hover .message-copy,.message:focus-within .message-copy {opacity:1;pointer-events:auto;} +.message-copy:hover {color:#ffe135;background:#353a2d;} +.conversation {position:relative;} +.conversation.is-dragging-file::after {content:'Отпустите изображение';position:absolute;z-index:6;inset:12px;display:grid;place-items:center;border:2px dashed #ffe13599;border-radius:16px;background:#20231def;color:#ffe135;font-size:16px;font-weight:550;pointer-events:none;} +@media(hover:none) {.message-copy {opacity:.8;pointer-events:auto;} .reaction-picker {opacity:.75;}} +@media(max-width:720px) {.message {max-width:calc(100% - 36px);}.message-copy {opacity:.8;pointer-events:auto;}} +@media(prefers-reduced-motion:reduce) {.message-copy {transition:none;}} +.auth-card .wide > .button-label {font-size:13px;line-height:1.4;} +.message-copy.is-copied {color:#ffe135;border-color:#ffe13550;background:#ffe1350d;} +@media(prefers-reduced-motion:no-preference) { + .messages-loading i {animation:message-loading 1.5s ease-in-out infinite alternate;} + .messages-loading i:nth-child(2) {animation-delay:.18s;} + .messages-loading i:nth-child(3) {animation-delay:.36s;} + .attachment-preview img {transition:opacity .28s ease;} + .draft-image {animation:dialog-appear .22s ease-out;} +} +@keyframes message-loading {from{opacity:.45}to{opacity:1}} diff --git a/src/Messenger.Api/wwwroot/diagnostics.js b/src/Messenger.Api/wwwroot/diagnostics.js new file mode 100644 index 00000000..3299a629 --- /dev/null +++ b/src/Messenger.Api/wwwroot/diagnostics.js @@ -0,0 +1,113 @@ +// Only explicitly selected HTTP metadata reaches this view. Never pass request +// headers, bodies, session objects or raw response JSON into the panel. +import { observeClusterRequest, setClusterIdentity } from '/cluster-view.js'; +const $ = id => document.getElementById(id); +const preferenceKey = "n1k0der.diagnostics"; +const requests = []; +let session = null, history = null, backgroundRequest = null, returnFocus = null; + +function setOpen(open) { + $("diagnostics-panel").hidden = !open; + document.body.classList.toggle("diagnostics-open", open); + document.querySelectorAll("[data-diagnostics-toggle]").forEach(button => button.setAttribute("aria-expanded", String(open))); + try { sessionStorage.setItem(preferenceKey, open ? "on" : "off"); } catch { /* optional preference */ } + if (open) { render(); $("diagnostics-close").focus(); } + else if (returnFocus?.isConnected) returnFocus.focus(); +} + +document.querySelectorAll("[data-diagnostics-toggle]").forEach(button => button.addEventListener("click", () => { + returnFocus = button; + setOpen($("diagnostics-panel").hidden); +})); +$("diagnostics-close").addEventListener("click", () => setOpen(false)); +document.addEventListener("keydown", event => { + if (event.key === "Escape" && !$("diagnostics-panel").hidden && !document.querySelector("dialog[open]")) { + event.preventDefault(); setOpen(false); + } +}); + +export function updateSessionInfo(value) { + setClusterIdentity(Boolean(value)); + session = value ? { name: value.name, expiresAt: value.expiresAt } : null; + document.body.classList.toggle("has-session", Boolean(session)); + if (!$("diagnostics-panel").hidden) renderSession(); +} + +export function updateHistoryInfo(value) { + history = value ? { title: value.title, loaded: value.loaded, hasOlder: value.hasOlder, ready: value.ready } : null; + if (!$("diagnostics-panel").hidden) renderHistory(); +} + +export function recordRequest(value) { + const entry = { + method: value.method, path: value.path.split("?")[0], status: value.status, + authorization: value.authorization, durationMs: value.durationMs, + requestId: value.requestId, instanceId: value.instanceId, outcome: value.outcome, source: value.source, at: new Date() + }; + observeClusterRequest(entry); + if (value.background) backgroundRequest = entry; + else { requests.unshift(entry); requests.length = Math.min(requests.length, 4); } + if (!$("diagnostics-panel").hidden) renderRequests(); +} + +function renderSession() { + $("diagnostics-user").textContent = session?.name || "Вход не выполнен"; + const seconds = session ? Math.max(0, Math.ceil((new Date(session.expiresAt).getTime() - Date.now()) / 1000)) : null; + $("diagnostics-session").textContent = seconds === null ? "-" : seconds === 0 ? "Срок истёк" : `${Math.floor(seconds / 60)} мин ${String(seconds % 60).padStart(2, "0")} с`; +} + +function renderHistory() { + $("diagnostics-chat").textContent = history?.title || "Выберите чат"; + $("diagnostics-messages").textContent = history + ? `Загружено: ${history.loaded} · ${!history.ready ? "страница ещё не получена" : history.hasOlder ? "есть старые сообщения ↑" : "старых страниц нет"}` + : "Сообщения ещё не загружены"; +} + +function result(entry) { + if (entry.status === null) return "Ответ не получен: соединение или таймаут. Результат записи может быть неизвестен."; + if (entry.outcome === "unreadable") return "HTTP-ответ получен, но его данные не удалось прочитать."; + if (entry.status === 204 && entry.method === "DELETE" && entry.path === "/api/v1/auth/sessions/current") return "Сессия отозвана. Ответ без тела."; + return ({200: "Запрос выполнен", 201: "Объект создан", 204: "Выполнено без тела ответа", + 400: "Некорректные данные", 401: "Нужен действующий вход", 404: "Ресурс отсутствует или недоступен", + 409: "Конфликт данных", 415: "Неподдерживаемый формат", 503: "Сервис временно недоступен", + 500: "Внутренняя ошибка сервера"})[entry.status] || `HTTP ${entry.status}`; +} + +function shortPath(path) { return path.replace(/\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}(?=\/|$)/gi, "/…"); } + +function renderRequests() { + const current = requests[0]; + $("diagnostics-empty").hidden = Boolean(current); + $("diagnostics-request-details").hidden = !current; + $("diagnostics-request-time").textContent = current ? current.at.toLocaleTimeString("ru-RU") : ""; + if (current) { + const failed = current.status === null || current.status >= 400 || current.outcome === "unreadable"; + $("diagnostics-method").textContent = current.method; + $("diagnostics-status").textContent = current.status ?? "Нет ответа"; + $("diagnostics-status").classList.toggle("failed", failed); + $("diagnostics-origin").textContent = current.source || "Основной интерфейс"; + $("diagnostics-path").textContent = shortPath(current.path); + $("diagnostics-path").title = current.path; + $("diagnostics-result").textContent = result(current); + $("diagnostics-result").classList.toggle("failed", failed); + $("diagnostics-auth").textContent = current.authorization; + $("diagnostics-duration").textContent = `${current.durationMs} мс`; + $("diagnostics-request-id").textContent = current.requestId || "Не получен"; + $("diagnostics-instance-id").textContent = current.instanceId || "Не получен"; + } + const previous = requests.slice(1).map(entry => { + const item = document.createElement("li"); + const status = document.createElement("span"); status.textContent = entry.status ?? "-"; + status.className = "diagnostics-mini-status" + (entry.status === null || entry.status >= 400 ? " failed" : ""); + const route = document.createElement("code"); route.textContent = `${entry.method} ${shortPath(entry.path)}`; + route.title = entry.path; item.append(status, route); return item; + }); + $("diagnostics-previous").replaceChildren(...previous); + $("diagnostics-previous").hidden = previous.length === 0; + $("diagnostics-background").hidden = !backgroundRequest; + if (backgroundRequest) $("diagnostics-background").textContent = `Фоновый запрос: ${backgroundRequest.status ?? "нет ответа"} · ${backgroundRequest.durationMs} мс · ${backgroundRequest.at.toLocaleTimeString("ru-RU")}`; +} + +function render() { renderSession(); renderHistory(); renderRequests(); } +setInterval(() => { if (!$("diagnostics-panel").hidden) renderSession(); }, 1000); +try { if (sessionStorage.getItem(preferenceKey) === "on") setOpen(true); } catch { /* optional preference */ } diff --git a/src/Messenger.Api/wwwroot/index.html b/src/Messenger.Api/wwwroot/index.html new file mode 100644 index 00000000..2aa5eedd --- /dev/null +++ b/src/Messenger.Api/wwwroot/index.html @@ -0,0 +1,172 @@ + + + + + + + + n1k0der · Мессенджер + + + + + + + + + + + + +
+
+ n1k0der. +

Мессенджер

+ +
+
+
+
+

С возвращением.

+
+
+ + + +
+
n1k0der
+
+
+ + + + + +

Собрать чат

+

Ваш профиль

Поделитесь ID для приглашения в чат.

+

Участники чата

+ + diff --git a/src/Messenger.Api/wwwroot/mascots.js b/src/Messenger.Api/wwwroot/mascots.js new file mode 100644 index 00000000..ef6f57fa --- /dev/null +++ b/src/Messenger.Api/wwwroot/mascots.js @@ -0,0 +1,143 @@ +// Animation reads field state and password length, never password text. +const stage = document.getElementById("mascot-stage"); +const auth = document.getElementById("auth-screen"); +const reduceMotion = matchMedia("(prefers-reduced-motion: reduce)"); +const mobile = matchMedia("(max-width: 720px)"); +let pending = false, feedback = false; +let feedbackTimer, sleepTimer, approachTimer, pointerFrame = null; +let pointer = { x: 0, y: 0 }, lastWake = 0; + +function wakeUp() { + stage.classList.remove("is-sleepy"); + clearTimeout(sleepTimer); + sleepTimer = setTimeout(() => { + if (stage.dataset.mood === "idle") stage.classList.add("is-sleepy"); + }, 12000); +} + +function activeField() { + const focused = document.activeElement; + return focused?.matches("[data-password-toggle]") + ? document.getElementById(focused.dataset.passwordToggle) : focused; +} + +function focusedMood() { + if (pending || feedback) return; + const field = activeField(); + let mood = "idle"; + if (field?.matches("#login-password, #register-password")) { + mood = field.type === "text" ? "revealed" : "private"; + if (field.id === "register-password" && field.value.length > 0 && Array.from(field.value).length < 12) mood = "short"; + } else if (field?.matches("#login-username, #register-name, #register-username")) mood = "looking"; + stage.dataset.mood = mood; + document.getElementById("auth-error-art").hidden = mood !== "error"; +} + +export function setMascotMood(mood) { + clearTimeout(feedbackTimer); + pending = mood === "pending"; + feedback = ["success", "error", "short"].includes(mood); + stage.dataset.mood = mood; + document.getElementById("auth-error-art").hidden = mood !== "error"; + if (pending || feedback) stage.classList.remove("is-curious"); + if (feedback) feedbackTimer = setTimeout(() => { feedback = false; focusedMood(); }, 3500); +} + +for (const form of auth.querySelectorAll("form")) { + form.addEventListener("focusin", () => { + if (!pending) { feedback = false; clearTimeout(feedbackTimer); } + wakeUp(); focusedMood(); + }); + form.addEventListener("focusout", () => requestAnimationFrame(focusedMood)); + form.addEventListener("input", () => { + feedback = false; focusedMood(); wakeUp(); + stage.classList.add("is-curious"); + clearTimeout(approachTimer); + approachTimer = setTimeout(() => stage.classList.remove("is-curious"), 1600); + }); + form.addEventListener("invalid", () => setMascotMood("error"), true); + form.addEventListener("reset", () => { + for (const button of form.querySelectorAll("[data-password-toggle]")) { + document.getElementById(button.dataset.passwordToggle).type = "password"; + button.setAttribute("aria-pressed", "false"); + button.setAttribute("aria-label", "Показать пароль"); + button.querySelector("use").setAttribute("href", "#i-eye"); + } + }); +} + +for (const button of auth.querySelectorAll("[data-password-toggle]")) { + button.addEventListener("click", () => { + const field = document.getElementById(button.dataset.passwordToggle); + const shown = field.type === "password"; + field.type = shown ? "text" : "password"; + button.setAttribute("aria-pressed", String(shown)); + button.setAttribute("aria-label", shown ? "Скрыть пароль" : "Показать пароль"); + button.querySelector("use").setAttribute("href", shown ? "#i-eye-off" : "#i-eye"); + feedback = false; wakeUp(); focusedMood(); + }); +} +for (const button of auth.querySelectorAll(".auth-switch button")) { + button.addEventListener("click", () => { + pending = feedback = false; + clearTimeout(feedbackTimer); wakeUp(); focusedMood(); + }); +} + +// Mouse events are combined into at most one update per browser frame. +auth.addEventListener("pointermove", event => { + if (event.pointerType === "touch" || reduceMotion.matches || auth.hidden || document.hidden) return; + pointer = { x: event.clientX, y: event.clientY }; + if (performance.now() - lastWake > 1000) { wakeUp(); lastWake = performance.now(); } + if (pointerFrame !== null) return; + pointerFrame = requestAnimationFrame(() => { + pointerFrame = null; + const rect = stage.getBoundingClientRect(); + const x = Math.max(-4, Math.min(4, (pointer.x - rect.left - rect.width / 2) / 55)); + const y = Math.max(-3, Math.min(3, (pointer.y - rect.top - rect.height / 2) / 65)); + stage.style.setProperty("--gaze-x", `${x}px`); + stage.style.setProperty("--gaze-y", `${y}px`); + }); +}); +auth.addEventListener("pointerleave", () => { + stage.style.setProperty("--gaze-x", "0px"); stage.style.setProperty("--gaze-y", "0px"); +}); + +const close = document.getElementById("diagnostics-close"); +let closeHovered = false; +function reactToClose() { + stage.classList.toggle("is-scared", !close.closest("[hidden]") && + (closeHovered || close.matches(":focus-visible"))); +} +close.addEventListener("pointerenter", () => { closeHovered = true; reactToClose(); }); +close.addEventListener("pointerleave", () => { closeHovered = false; reactToClose(); }); +close.addEventListener("focus", reactToClose); +close.addEventListener("blur", reactToClose); +close.addEventListener("keydown", () => requestAnimationFrame(reactToClose)); +new MutationObserver(reactToClose).observe(document.getElementById("diagnostics-panel"), { attributes: true, attributeFilter: ["hidden"] }); + +function updateVisibility() { + const visible = !auth.hidden && !document.hidden; + stage.classList.toggle("is-visible", visible); + if (!visible) { + if (pointerFrame !== null) cancelAnimationFrame(pointerFrame); + pointerFrame = null; + clearTimeout(sleepTimer); clearTimeout(approachTimer); clearTimeout(feedbackTimer); + stage.classList.remove("is-curious", "is-sleepy"); + pending = feedback = false; + } else { wakeUp(); focusedMood(); } +} +new MutationObserver(updateVisibility).observe(auth, { attributes: true, attributeFilter: ["hidden"] }); +document.addEventListener("visibilitychange", updateVisibility); +function placeStage() { + if (mobile.matches) { + const anchor = document.getElementById("login-panel"); anchor.before(stage); + } else document.querySelector(".intro").append(stage); +} +mobile.addEventListener("change", placeStage); +placeStage(); updateVisibility(); + +export async function celebrateMascots() { + setMascotMood("success"); + if (!reduceMotion.matches) await new Promise(resolve => setTimeout(resolve, 650)); +} diff --git a/src/Messenger.Api/wwwroot/message-view.js b/src/Messenger.Api/wwwroot/message-view.js new file mode 100644 index 00000000..d7d1aec9 --- /dev/null +++ b/src/Messenger.Api/wwwroot/message-view.js @@ -0,0 +1,105 @@ +// Each message keeps its own DOM element, identified by the message ID. +import { attachmentCard, clearAttachmentCards } from '/attachments.js'; +import { ReactionControls } from '/reactions.js'; +export class MessageView { + constructor(list) { + this.list = list; + this.messages = new Map(); + this.days = new Map(); + } + + clear() { + clearAttachmentCards(); + this.messages.clear(); + this.days.clear(); + this.list.replaceChildren(); + } + + render(messages, userId, members, animate = false) { + const nodes = [], entering = []; + const hadMessages = this.messages.size > 0; + let previous = null, previousDay = ""; + + for (const message of messages) { + const date = new Date(message.created_at); + const day = date.toLocaleDateString("ru-RU", { day: "numeric", month: "long", year: "numeric" }); + if (day !== previousDay) { + if (!this.days.has(day)) { + const divider = document.createElement("div"); + divider.className = "date-separator"; + divider.textContent = day; + this.days.set(day, divider); + } + nodes.push(this.days.get(day)); + } + + let entry = this.messages.get(message.id); + if (!entry) { + const article = document.createElement("article"); + article.className = "message"; + article.dataset.messageId = message.id; + const author = document.createElement("span"); + author.className = "message-author"; + const bubble = document.createElement("div"); + bubble.className = "message-bubble"; + const text = document.createElement("p"); + text.className = "message-text"; + const stamp = document.createElement("time"); + stamp.className = "message-time"; + bubble.append(text, ...((message.attachment_ids || []).map(attachmentCard)), stamp); + const reactions = new ReactionControls(message.id); + const copy = document.createElement("button"); copy.type = "button"; copy.className = "message-copy"; + copy.setAttribute("aria-label", "Скопировать текст сообщения"); copy.title = "Скопировать текст"; + copy.innerHTML = ''; + copy.addEventListener("click", async () => { + let success = false; + try { await navigator.clipboard.writeText(entry.text.textContent); success = true; copy.classList.add("is-copied"); setTimeout(() => copy.classList.remove("is-copied"), 1200); } catch { /* clipboard may be unavailable */ } + article.dispatchEvent(new CustomEvent("messagecopy", {bubbles:true, detail:{success}})); + }); + article.append(author, bubble, reactions.root, copy); + entry = { article, author, text, stamp, reactions }; + this.messages.set(message.id, entry); + if (animate && hadMessages) entering.push(article); + } + + const mine = message.sender_id === userId; + const grouped = previousDay === day && previous?.sender_id === message.sender_id && + date.getTime() - new Date(previous.created_at).getTime() <= 5 * 60 * 1000; + entry.article.classList.toggle("mine", mine); + entry.article.classList.toggle("grouped", grouped); + entry.author.hidden = mine || grouped; + entry.author.title = message.sender_id; + setText(entry.author, members.get(message.sender_id)?.display_name || "Участник " + message.sender_id.slice(0, 8)); + setText(entry.text, message.text); + entry.text.hidden = !message.text; + entry.article.querySelector(".message-copy").hidden = !message.text; + entry.stamp.dateTime = message.created_at; + setText(entry.stamp, date.toLocaleTimeString("ru-RU", { hour: "2-digit", minute: "2-digit" })); + nodes.push(entry.article); + previous = message; + previousDay = day; + } + + // Keep existing nodes in place. Insert only new or reordered nodes. + const wanted = new Set(nodes); + for (const node of [...this.list.children]) { + if (!wanted.has(node)) node.remove(); + } + let next = this.list.firstChild; + for (const node of nodes) { + if (node === next) next = next.nextSibling; + else this.list.insertBefore(node, next); + } + if (!matchMedia("(prefers-reduced-motion: reduce)").matches) { + for (const node of entering) node.animate([{ opacity: 0, transform: "translateY(5px)" }, { opacity: 1, transform: "none" }], { duration: 190, easing: "ease-out" }); + } + } + + renderReactions(items, busy) { + for (const [id, entry] of this.messages) entry.reactions.render(items.get(id) || [], busy.has(id)); + } +} + +function setText(element, text) { + if (element.textContent !== text) element.textContent = text; +} diff --git a/src/Messenger.Api/wwwroot/operations.css b/src/Messenger.Api/wwwroot/operations.css new file mode 100644 index 00000000..1b112ef0 --- /dev/null +++ b/src/Messenger.Api/wwwroot/operations.css @@ -0,0 +1,120 @@ +.ops-panel {position:fixed;inset:0;width:100dvw;height:100dvh;max-width:none;max-height:none;margin:0;border:0;border-radius:0;background:#222321;color:#eeeee7;padding:0;overflow:auto;color-scheme:dark;scroll-behavior:smooth;} +.ops-panel::backdrop {background:#222321;} +.ops-panel * {box-sizing:border-box;} +.ops-panel svg {width:26px;height:26px;fill:none;stroke:currentColor;flex-shrink:0;} +.ops-shell {max-width:1800px;margin:auto;display:grid;grid-template-columns:210px minmax(0,1fr);min-height:100%;} +.ops-rail {position:sticky;top:0;height:100dvh;display:flex;flex-direction:column;padding:34px 20px 24px;background:#1d1e1c;border-right:1px solid #ffffff0d;} +.ops-brand {color:#ffe135;text-decoration:none;display:grid;font-size:25px;font-weight:750;letter-spacing:-1px;padding:0 14px;gap:8px;} +.ops-brand span {color:#a0a297;font-size:11px;font-weight:400;letter-spacing:0;} +.ops-rail nav {display:grid;gap:6px;margin-top:42px;} +.ops-panel button {cursor:pointer;color:inherit;background:#ffffff05;border:1px solid #ffffff18;border-radius:10px;padding:10px 14px;font:inherit;font-size:12px;transition:background .2s,color .2s,border-color .2s;} +.ops-panel button:hover {background:#ffffff0d;border-color:#ffffff30;} +.ops-panel button:focus-visible,.ops-panel a:focus-visible,.ops-legend summary:focus-visible {outline:2px solid #ffe135;outline-offset:3px;} +.ops-rail nav button {display:flex;align-items:center;gap:12px;border-color:transparent;background:transparent;text-align:left;color:#a8aa9e;padding:13px;} +.ops-rail nav button svg {width:20px;height:20px;} +.ops-rail nav .is-active {color:#ffe135;background:#ffe1350d;border-color:#ffe1351a;} +.ops-rail-art {margin:auto -2px 16px;padding-top:30px;} +.ops-rail-art img {display:block;width:100%;height:auto;opacity:.85;filter:drop-shadow(0 12px 15px #0004);} +.ops-close {text-align:left;color:#b5b7a9!important;} +.ops-content {min-width:0;padding:0 32px;} +.ops-header {display:flex;align-items:center;gap:18px;padding:25px 0;margin-bottom:4px;} +.ops-header h1 {font-size:24px;letter-spacing:-.7px;margin:0 0 7px;font-weight:600;color:#f2f2e9;} +.ops-checked {font-size:10px;color:#85897b;} +.ops-live {display:flex;align-items:center;gap:8px;margin-left:auto;font-size:11px;color:#b8c8a4;} +.ops-live::before {content:'';width:6px;height:6px;border-radius:50%;background:#b8c8a4;box-shadow:0 0 0 4px #b8c8a409;} +.ops-panel .ops-refresh {font-size:22px;padding:5px 13px;color:#a8aa9a;} +.ops-panel .ops-mobile-close {display:none;} +.ops-notice {color:#efb39f;font-size:12px;background:#efb39f08;border:1px solid #efb39f24;border-radius:10px;padding:14px;} +.ops-notice:empty {display:none;} +.ops-metrics {display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:14px;margin:12px 0 22px;scroll-margin-top:20px;} +.ops-metrics article {position:relative;display:grid;gap:14px;background:linear-gradient(140deg,#32332e,#2b2c28);border:1px solid #ffffff0d;padding:22px;border-radius:15px;overflow:hidden;} +.ops-metrics article::after {content:'';position:absolute;right:-12px;bottom:-35px;width:90px;height:90px;border:1px solid #ffffff06;border-radius:50%;} +.ops-metrics svg {position:absolute;right:18px;top:20px;width:24px;height:24px;color:#aaa993;opacity:.65;} +.ops-metrics span {font-size:11px;color:#b5b7a7;padding-right:26px;} +.ops-metrics strong {font-size:34px;letter-spacing:-1px;line-height:1.1;font-weight:500;font-variant-numeric:tabular-nums;} +.ops-metrics [data-kind=queued] strong,.ops-metrics [data-kind=queued] svg {color:#ffe135;} +.ops-metrics [data-kind=ready] svg {color:#a9c49b;} +.ops-metrics [data-kind=failed] svg {color:#dda28b;} +.ops-box {background:#282a25;border:1px solid #ffffff0c;padding:24px;border-radius:16px;min-width:0;scroll-margin-top:20px;} +.ops-section-head {display:flex;align-items:center;justify-content:space-between;gap:14px;margin-bottom:22px;} +.ops-box h2 {font-size:15px;font-weight:550;letter-spacing:-.2px;margin:0;color:#e3e5d8;} +.ops-topology-count,.ops-job-count {font-size:10px;color:#9ca18c;border:1px solid #ffffff12;border-radius:20px;padding:5px 9px;white-space:nowrap;} +.ops-flow {display:flex;align-items:center;gap:16px;padding:4px 0 25px;} +.ops-node,.ops-api-nodes {flex:1;min-width:0;} +.ops-node {background:#22241f;padding:18px;border:1px solid #ffffff12;border-radius:12px;display:grid;gap:9px;} +.ops-node svg {color:#d1cfaa;width:28px;height:28px;margin-bottom:5px;} +.ops-node strong {font-size:14px;font-weight:550;} +.ops-node small {font-size:10px;color:#97a28b;} +.ops-arrow {position:relative;flex:0 0 36px;height:1px;background:#ffe13530;} +.ops-arrow::after {content:'';position:absolute;right:0;top:-3px;width:6px;height:6px;border-top:1px solid #b4ad63;border-right:1px solid #b4ad63;transform:rotate(45deg);} +.ops-api-nodes {display:grid;gap:8px;} +.ops-services {display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px;} +.ops-service {position:relative;border:1px solid #ffffff0f;border-radius:11px;padding:16px;display:grid;grid-template-columns:30px minmax(0,1fr);gap:6px 12px;min-width:0;background:#ffffff02;transition:border-color .35s,background .35s;} +.ops-service>svg {grid-row:1/4;color:#c4c4a4;width:28px;height:28px;margin-top:3px;} +.ops-service-status {grid-column:2;font-size:9px;color:#a6bf94;display:flex;align-items:center;gap:6px;} +.ops-service-status::before {content:'';width:4px;height:4px;background:currentColor;border-radius:50%;} +.ops-service strong {font-size:12px;font-weight:550;overflow-wrap:anywhere;grid-column:2;} +.ops-service small {font-size:9px;color:#939a82;line-height:1.5;grid-column:2;} +.ops-service[data-state=down] {border-color:#d798762a;background:#d7987605;} +.ops-service[data-state=down] .ops-service-status {color:#dda18b;} +.ops-service[data-state=unknown] {border-style:dashed;} +.ops-service[data-state=unknown] .ops-service-status {color:#a1a299;} +.ops-service.served {border-color:#ffe13566;background:#ffe13506;animation:ops-served 1.3s ease-out;} +.ops-service.served>svg {color:#ffe135;} +.ops-pipeline {display:flex;flex-wrap:wrap;align-items:center;justify-content:center;gap:12px;padding:18px 10px 0;margin-top:16px;border-top:1px solid #ffffff0b;font-size:10px;color:#b7bea4;} +.ops-pipeline span {padding:5px 9px;border:1px solid #ffffff0c;border-radius:6px;background:#22241f;} +.ops-pipeline i {color:#777c65;font-style:normal;} +.ops-legend {font-size:10px;color:#97a28b;margin-top:18px;line-height:1.7;} +.ops-legend summary {cursor:pointer;width:fit-content;} +.ops-legend p {max-width:700px;color:#a6ae95;} +.ops-charts {display:grid;grid-template-columns:1fr 1fr;gap:18px;margin-top:18px;scroll-margin-top:20px;} +.ops-charts .ops-section-head {margin-bottom:12px;} +.ops-charts svg {width:100%;height:auto;min-height:145px;display:block;stroke:none;overflow:visible;} +.ops-chart-legend {display:flex;align-items:center;gap:6px;font-size:9px;color:#8e977d;margin-bottom:15px;} +.ops-chart-legend i {width:5px;height:5px;border-radius:50%;background:#ffe135;} +.ops-chart-legend.secondary i {background:#a6cf9a;} +.ops-chart-caption {display:flex;justify-content:space-between;font-size:9px;color:#97a28b;font-variant-numeric:tabular-nums;} +.ops-chart-point:hover {stroke:#efefd4;stroke-width:2;} +.ops-queue-value,.ops-latency-value {font-size:21px;font-variant-numeric:tabular-nums;font-weight:500;letter-spacing:-.5px;color:#ffe135;} +.ops-latency-value {color:#b3c5a5;} +.ops-footnote {font-size:9px;color:#97a28b;line-height:1.7;margin:16px 0 0;} +.ops-bottom {display:grid;grid-template-columns:1.2fr 1fr;gap:18px;margin:18px 0;} +.ops-jobs,.ops-events {max-height:390px;overflow:auto;scrollbar-width:thin;scrollbar-color:#494e3f transparent;} +.ops-job-heading {display:flex;justify-content:space-between;font-size:9px;color:#97a28b;padding:0 0 12px;border-bottom:1px solid #ffffff0b;} +.ops-job {display:flex;align-items:center;gap:10px;padding:14px 0;border-bottom:1px solid #ffffff08;} +.ops-job>svg {width:30px;height:30px;color:#899476;background:#ffffff04;padding:6px;border-radius:7px;} +.ops-job>div {display:grid;gap:6px;min-width:0;flex:1;} +.ops-job strong {font-size:11px;font-weight:450;overflow-wrap:anywhere;} +.ops-job small {font-size:9px;color:#97a28b;line-height:1.5;} +.ops-job-state {font-size:9px;white-space:nowrap;border-radius:6px;padding:5px 7px;background:#ffffff06;color:#b8bca9;} +.ops-job-state.ready {color:#b0c79a;background:#b0c79a0a;}.ops-job-state.failed {color:#d7a18d;background:#d7a18d0a;}.ops-job-state.queued {color:#e4d172;background:#e4d1720a;} +.ops-job button {font-size:10px;padding:6px 8px;color:#ffe135;} +.ops-events {list-style:none;padding:0;margin:0;} +.ops-events li {position:relative;display:grid;grid-template-columns:50px 1fr;gap:5px 10px;padding:0 0 20px 15px;font-size:10px;border-left:1px solid #ffffff0c;margin-left:3px;} +.ops-events li::before {content:'';position:absolute;width:5px;height:5px;left:-3px;top:4px;border-radius:50%;background:#737b61;} +.ops-events li:first-child::before {background:#ffe135;box-shadow:0 0 0 4px #ffe1350b;} +.ops-events time {color:#97a28b;font-size:9px;font-variant-numeric:tabular-nums;} +.ops-events strong {font-weight:450;color:#cbd0ba;} +.ops-events li>span {grid-column:2;font-size:9px;color:#97a28b;overflow-wrap:anywhere;} +.ops-event-dot {height:6px;width:6px;border-radius:50%;background:#b1c69b;} +.ops-empty {font-size:11px;line-height:1.7;color:#a2aa91;} +.ops-content footer {padding:7px 0 25px;font-size:10px;color:#828e70;} +.ops-panel.is-stale .ops-live {color:#dba18e;}.ops-panel.is-stale .ops-live::before {background:#dba18e;} +@keyframes ops-served {from{box-shadow:0 0 0 3px #ffe13512;}to{box-shadow:0 0 0 0 #ffe13500;}} +@media(min-width:1500px){.ops-services{grid-template-columns:repeat(6,minmax(0,1fr));}.ops-service{grid-template-columns:1fr;}.ops-service>svg{grid-row:auto;}.ops-service-status,.ops-service strong,.ops-service small{grid-column:1;}.ops-api-nodes .ops-service{grid-template-columns:30px 1fr;}.ops-api-nodes .ops-service>svg{grid-row:1/4;}.ops-api-nodes .ops-service-status,.ops-api-nodes .ops-service strong,.ops-api-nodes .ops-service small{grid-column:2;}} +@media(max-width:1100px){.ops-shell{grid-template-columns:170px minmax(0,1fr);}.ops-rail{padding:28px 12px 18px;}.ops-content{padding:0 22px;}.ops-bottom{grid-template-columns:1fr;}.ops-flow{gap:10px;}.ops-arrow{flex-basis:20px;}.ops-node{padding:14px;}.ops-services{grid-template-columns:repeat(2,minmax(0,1fr));}.ops-metrics article{padding:18px;}.ops-metrics svg{right:12px;width:20px;}.ops-metrics span{font-size:10px;}} +@media(max-width:720px){.ops-shell{display:block;}.ops-rail{position:sticky;top:0;z-index:3;height:auto;padding:9px 12px;border:0;border-bottom:1px solid #ffffff0d;}.ops-brand,.ops-rail-art,.ops-close{display:none!important;}.ops-rail nav{display:flex;gap:4px;margin:0;overflow:auto;scrollbar-width:none;}.ops-rail nav button{padding:10px;font-size:10px;white-space:nowrap;gap:6px;}.ops-rail nav button svg{width:15px;height:15px;}.ops-content{padding:0 14px;}.ops-header{padding:20px 0 15px;gap:12px;}.ops-header h1{font-size:21px;}.ops-live{font-size:9px;}.ops-panel .ops-mobile-close{display:block;padding:6px 12px;font-size:20px;}.ops-panel .ops-refresh{display:none;}.ops-metrics{grid-template-columns:repeat(2,minmax(0,1fr));gap:9px;margin:4px 0 14px;}.ops-metrics article{padding:16px;gap:11px;}.ops-metrics strong{font-size:30px;}.ops-box{padding:17px;scroll-margin-top:70px;}.ops-section-head{margin-bottom:18px;}.ops-flow{flex-wrap:wrap;gap:10px;}.ops-node{flex-basis:calc(50% - 25px);padding:15px;}.ops-arrow{flex-basis:20px;}.ops-arrow:nth-of-type(4){display:none;}.ops-api-nodes{flex-basis:100%;grid-template-columns:repeat(2,minmax(0,1fr));}.ops-api-nodes .ops-service{display:flex;flex-direction:column;padding:12px;gap:6px;}.ops-api-nodes .ops-service>svg{display:none;}.ops-services{gap:8px;}.ops-service{padding:13px;grid-template-columns:22px 1fr;gap:5px 8px;}.ops-service>svg{width:22px;height:22px;}.ops-service strong{font-size:11px;}.ops-service small{font-size:9px;}.ops-service-status{font-size:8px;}.ops-pipeline{gap:7px;justify-content:flex-start;}.ops-pipeline span{font-size:9px;padding:4px 7px;}.ops-charts{grid-template-columns:1fr;gap:14px;margin-top:14px;scroll-margin-top:70px;}.ops-bottom{gap:14px;margin-top:14px;}.ops-job{flex-wrap:wrap;}.ops-job button{margin-left:auto;}.ops-metrics{scroll-margin-top:70px;}} +@media(prefers-reduced-motion:reduce){.ops-panel{scroll-behavior:auto;}.ops-service.served{animation:none;}.ops-panel button,.ops-service{transition:none;}} +.ops-arrow i {position:absolute;width:5px;height:5px;border-radius:50%;top:-2px;left:0;background:#ffe135;opacity:0;box-shadow:0 0 7px #ffe13544;} +.ops-filters {display:flex;gap:6px;flex-wrap:wrap;margin:-3px 0 16px;} +.ops-panel .ops-filters button {padding:7px 11px;border-color:transparent;background:#ffffff05;color:#a6ad9c;font-size:10px;} +.ops-panel .ops-filters button[aria-pressed=true] {background:#ffe13512;border-color:#ffe13525;color:#ffe135;} +.ops-panel .ops-filters button:hover {background:#ffffff0d;} +.ops-rail nav button[aria-current=location] {color:#ffe135;background:#ffe1350d;border-color:#ffe1351a;} +@media(prefers-reduced-motion:no-preference) { + .ops-service,.ops-node,.ops-metrics article {transition:border-color .25s,background .25s,box-shadow .25s;} + .ops-service:hover,.ops-node:hover {border-color:#ffe13530;background:#ffe13504;} + .ops-rail nav button svg {transition:transform .25s ease;} + .ops-rail nav button:hover svg {transform:translateX(2px);} + .ops-job-state {transition:background .2s,color .2s;} +} diff --git a/src/Messenger.Api/wwwroot/operations.js b/src/Messenger.Api/wwwroot/operations.js new file mode 100644 index 00000000..999bb190 --- /dev/null +++ b/src/Messenger.Api/wwwroot/operations.js @@ -0,0 +1,130 @@ +let token=null, epoch=0, panel, timer, abort, samples=[], knownInstances=new Set(), lastJobs=[], jobFilter="all"; +const $=selector=>panel.querySelector(selector); +const labels={queued:'В очереди',processing:'Обработка',ready:'Готово',failed:'Ошибка'}; +const eventLabels={accepted:'Файл принят',published:'В очереди RabbitMQ',processing:'Обработчик начал работу',ready:'Результаты сохранены',retry_scheduled:'Назначен повтор',failed:'Ошибка обработки',lease_recovered:'Восстановлено после остановки',operator_replay:'Повторный запуск'}; +function node(tag,text,cls){const e=document.createElement(tag);if(text!==undefined)e.textContent=text;if(cls)e.className=cls;return e;} +export async function operationsSession(value){ + token=value;epoch++;clearTimeout(timer);abort?.abort(); + if(panel){panel.close();panel.remove();panel=null;} + samples=[];knownInstances.clear();lastJobs=[];jobFilter="all"; + const button=document.getElementById('open-operations');button.hidden=true; + const version=epoch;if(!value)return; + try {const response=await fetch('/api/ui/operator',{headers:{Authorization:'Bearer '+value},cache:'no-store',signal:AbortSignal.timeout(8000)});const data=await response.json();if(version===epoch)button.hidden=!response.ok||!data.enabled;}catch{} +} +function icon(name){ + const svg=document.createElementNS('http://www.w3.org/2000/svg','svg');svg.setAttribute('viewBox','0 0 48 48');svg.setAttribute('aria-hidden','true'); + const use=document.createElementNS(svg.namespaceURI,'use');use.setAttribute('href','/assets/system-icons.svg#'+name);svg.append(use);return svg; +} +function init(){ + panel=node('dialog',undefined,'ops-panel');panel.setAttribute('aria-label','Система мессенджера'); + panel.innerHTML=`
+

Система

Подключение…
Обновление каждые 3 с
+

+

Архитектура

API
+
БраузерHTTP / Bearer
HAProxyБалансировка запросов
+
Как проверяются состояния

API: heartbeat Redis. Worker и dispatcher: heartbeat PostgreSQL за последние 10 с. S3 и RabbitMQ: прямой запрос. Ответ панели подтверждает доступность HTTP-входа.

+
Файл→Outbox→Dispatcher→RabbitMQ→Worker→S3
+
+

Активные задания

0
В очереди и в обработке

Последние 60 замеров открытой панели

Отклик системы

…
Запрос с проверкой зависимостей

Миллисекунды · наведите на точку для деталей

+

Последние задания

ФайлСостояние

События

    +
    `; + document.body.append(panel);$('.ops-close').onclick=()=>panel.close();$('.ops-mobile-close').onclick=()=>panel.close();$('.ops-brand').onclick=e=>e.preventDefault();$('.ops-refresh').onclick=()=>{clearTimeout(timer);void refresh();}; + const navIcons=['overview','proxy','activity','image','clock']; + panel.querySelectorAll('[data-section]').forEach((button,index)=>{button.prepend(icon(navIcons[index]));button.type='button';button.onclick=()=>{ + panel.querySelectorAll('[data-section]').forEach(b=>b.classList.toggle('is-active',b===button)); + $('#'+button.dataset.section).scrollIntoView({behavior:matchMedia('(prefers-reduced-motion: reduce)').matches?'instant':'smooth',block:'start'}); + };}); + panel.querySelectorAll('[data-icon]').forEach(card=>card.prepend(icon(card.dataset.icon))); + panel.querySelectorAll('[data-job-filter]').forEach(button=>button.addEventListener('click',()=>{ + jobFilter=button.dataset.jobFilter; + panel.querySelectorAll('[data-job-filter]').forEach(item=>item.setAttribute('aria-pressed',String(item===button))); + renderJobs(); + })); + const sections=[...panel.querySelectorAll('[data-section]')].map(button=>$('#'+button.dataset.section)); + let scrollFrame=0; + panel.addEventListener('scroll',()=>{ + if(scrollFrame)return; + scrollFrame=requestAnimationFrame(()=>{ + scrollFrame=0;const top=panel.getBoundingClientRect().top+Math.min(170,panel.clientHeight*.3); + let active=sections[0];for(const section of sections)if(section.getBoundingClientRect().top<=top)active=section; + panel.querySelectorAll('[data-section]').forEach(button=>{ + const selected=button.dataset.section===active.id;button.classList.toggle('is-active',selected); + if(selected)button.setAttribute('aria-current','location');else button.removeAttribute('aria-current'); + }); + }); + },{passive:true}); + const currentPanel=panel;panel.addEventListener('close',()=>{if(panel===currentPanel&&!currentPanel.open){clearTimeout(timer);abort?.abort();}}); +} +function chart(target,key,color){ + const host=$(target);host.replaceChildren();const ns='http://www.w3.org/2000/svg'; + const svg=document.createElementNS(ns,'svg');svg.setAttribute('viewBox','0 0 600 180');svg.setAttribute('role','img'); + svg.setAttribute('aria-label',samples.length<2?'Ожидаем второй замер':'График последних '+samples.length+' замеров'); + const values=samples.map(s=>s[key]),max=key==='queue'?Math.max(3,Math.ceil(Math.max(0,...values)/3)*3):Math.max(1,...values),points=values.map((v,i)=>[40+i*550/Math.max(1,values.length-1),145-v/max*125]); + function shape(tag,attributes){const item=document.createElementNS(ns,tag);for(const [name,value] of Object.entries(attributes))item.setAttribute(name,value);return item;} + const defs=shape('defs',{}),gradient=shape('linearGradient',{id:'ops-gradient-'+key,x1:0,y1:0,x2:0,y2:1}); + gradient.append(shape('stop',{offset:'0%','stop-color':color,'stop-opacity':'.2'}),shape('stop',{offset:'100%','stop-color':color,'stop-opacity':'0'}));defs.append(gradient);svg.append(defs); + for(let i=0;i<4;i++){ + const y=20+i*125/3;svg.append(shape('path',{d:`M40 ${y}H590`,stroke:'#ffffff0d','stroke-dasharray':'3 5'})); + const label=shape('text',{x:0,y:y+4,fill:'#85877e','font-size':10});label.textContent=String(Math.round(max*(1-i/3)));svg.append(label); + } + if(points.length){ + const line=points.map(p=>p.join(',')).join(' '); + svg.append(shape('polygon',{points:`40,145 ${line} ${points.at(-1)[0]},145`,fill:`url(#ops-gradient-${key})`})); + svg.append(shape('polyline',{points:line,stroke:color,'stroke-width':2.5,fill:'none','stroke-linejoin':'round','stroke-linecap':'round'})); + for(let i=0;ijobFilter==='all'||(jobFilter==='active'&&['queued','processing'].includes(job.state))||(jobFilter==='failed'&&job.state==='failed')); + if(!visible.length)jobs.append(node('p',lastJobs.length?'Нет заданий в этой категории':'Загрузите изображение в чат. Здесь появится его задание.','ops-empty')); + for(const job of visible){ + const row=node('div',undefined,'ops-job'),info=node('div'); + info.append(node('strong',job.filename),node('small',`Попыток: ${job.attempts}${job.error_code?' · '+job.error_code:''}`)); + row.append(icon('image'),info,node('span',labels[job.state]||job.state,'ops-job-state '+job.state)); + if(job.can_replay){const button=node('button','Повторить');button.type='button';button.onclick=()=>replay(job.attachment_id,button);row.append(button);} + jobs.append(row); + } +} +async function refresh(){ + if(!panel?.open||!token)return;if(document.hidden){timer=setTimeout(refresh,3000);return;} + if(abort&&!abort.signal.aborted)abort.abort();abort=new AbortController();const version=epoch,session=token,signal=abort.signal;const started=performance.now(); + try { + const response=await fetch('/api/ui/operations',{headers:{Authorization:'Bearer '+session},cache:'no-store',signal:AbortSignal.any([signal,AbortSignal.timeout(10000)])}); + if(!response.ok)throw new Error(response.status===404?'Нет доступа к управлению системой':response.status===401?'Войдите снова':'Сервер временно не отвечает');const data=await response.json(); + if(version!==epoch||!panel.open||signal.aborted)return; + const elapsed=Math.round(performance.now()-started);samples.push({queue:data.counts.queued+data.counts.processing,latency:elapsed,at:Date.now()});if(samples.length>60)samples.shift(); + panel.classList.remove('is-stale');$('.ops-live').textContent='Данные обновлены';$('.ops-notice').textContent=''; + if(!matchMedia('(prefers-reduced-motion: reduce)').matches)panel.querySelectorAll('.ops-arrow').forEach((arrow,index)=>arrow.querySelector('i')?.animate([{opacity:0,transform:'translateX(0)'},{opacity:1,offset:.25},{opacity:0,transform:`translateX(${arrow.clientWidth-5}px)`}],{duration:850,delay:index*170,easing:'ease-in-out'})); + $('.ops-checked').textContent='Проверено '+new Date(data.checked_at).toLocaleTimeString(); + const metrics=$('.ops-metrics'),metricsSignature=JSON.stringify(data.counts);if(metrics.dataset.signature!==metricsSignature){metrics.dataset.signature=metricsSignature;metrics.replaceChildren(...Object.entries(labels).map(([key,label])=>{const card=node('article');card.dataset.kind=key;card.append(icon({queued:'clock',processing:'worker',ready:'check',failed:'error'}[key]),node('span',label),node('strong',String(data.counts[key])));return card;}));} + const apis=$('.ops-api-nodes');data.instances.filter(n=>n.ready).forEach(n=>knownInstances.add(n.id)); + const instances=data.instances.filter(n=>n.ready||knownInstances.has(n.id)); + apis.replaceChildren(...instances.map(n=>service('API · '+n.id.slice(0,8),n.ready,'Heartbeat · '+new Date(n.last_seen).toLocaleTimeString()))); + if(!apis.childNodes.length)apis.append(service('API',false,'Нет свежего heartbeat')); + $('.ops-topology-count').textContent=instances.filter(n=>n.ready).length+' API на связи'; + const current=response.headers.get('X-Instance-Id');for(const card of apis.children)card.classList.toggle('served',card.textContent.includes(current?.slice(0,8)||'never')); + const s=data.services;$('.ops-services').replaceChildren(service('PostgreSQL',s.postgres,'Пользователи, чаты, задания'),service('Redis',s.redis,'Кэш и heartbeat API'),service('S3',s.s3,'Оригиналы и варианты'),service('RabbitMQ',s.broker,`Сообщений: ${data.broker_messages??'?'} · подписчиков: ${data.broker_consumers??'?'}`),service('Dispatcher',s.dispatcher,'Доставка outbox в очередь'),service('Worker',s.worker,'Обработка изображений')); + $('.ops-queue-value').textContent=String(samples.at(-1).queue);$('.ops-latency-value').textContent=elapsed+' мс';chart('.ops-queue-chart','queue','#ffe135');chart('.ops-latency-chart','latency','#a6cf9a'); + $('.ops-cycle').textContent=`Outbox: ${data.pending_outbox} · Средний цикл за 15 мин: ${data.average_cycle_seconds===null?'нет завершённых заданий':data.average_cycle_seconds.toFixed(2)+' с'}`; + $('.ops-job-count').textContent='Последние '+data.jobs.length; + lastJobs=data.jobs;renderJobs(); + const list=$('.ops-events'),eventSignature=JSON.stringify(data.events);if(list.dataset.signature!==eventSignature){list.dataset.signature=eventSignature;list.replaceChildren();for(const event of data.events){const item=node('li');item.append(node('time',new Date(event.at).toLocaleTimeString()),node('strong',eventLabels[event.name]||event.name),node('span',event.filename));list.append(item);}if(!data.events.length)list.append(node('li','Событий пока нет'));} + }catch(error){if(version===epoch&&panel.open&&!signal.aborted)stale(error.message);}finally{if(version===epoch&&panel?.open){clearTimeout(timer);timer=setTimeout(refresh,3000);}} +} +async function replay(id,button){button.disabled=true;button.textContent='Запускаем…';try{const response=await fetch(`/api/ui/operations/attachments/${id}/replay`,{method:'POST',headers:{Authorization:'Bearer '+token},signal:AbortSignal.timeout(10000)});const data=await response.json();if(!response.ok)throw new Error(data.error?.message||'Повтор недоступен');clearTimeout(timer);await refresh();}catch(error){$('.ops-notice').textContent=error.message;button.disabled=false;button.textContent='Повторить';}} +document.getElementById('open-operations').addEventListener('click',()=>{if(!panel)init();panel.showModal();void refresh();}); +document.addEventListener('visibilitychange',()=>{if(!document.hidden&&panel?.open){clearTimeout(timer);void refresh();}}); diff --git a/src/Messenger.Api/wwwroot/polish.css b/src/Messenger.Api/wwwroot/polish.css new file mode 100644 index 00000000..df0ae386 --- /dev/null +++ b/src/Messenger.Api/wwwroot/polish.css @@ -0,0 +1,41 @@ +/* Shared details for the chat list, generated avatars and gentle motion. */ +.avatar { overflow: hidden; background: transparent; border-color: #ffffff14; } +.avatar .avatar-art { display: block; width: 100%; height: 100%; stroke: none; } +.avatar-art text { letter-spacing: -.5px; } +.profile .avatar { background: transparent; } +.chat-row.active .avatar { background: transparent; border-color: #ffe13545; } +.chat-row { padding: 14px 11px; gap: 12px; } +.chat-row.active { background: #ffe1350b; border-color: #ffe13528; } +.chat-row.active::after { display: none; } +.chat-row-heading { display: flex; align-items: baseline; gap: 8px; min-width: 0; } +.chat-row-heading strong { flex: 1; min-width: 0; padding: 0; color: #e7e6dc; } +.chat-row-heading time { flex-shrink: 0; font-size: 10px; color: #a5a598; font-variant-numeric: tabular-nums; } +.chat-row.active .chat-row-heading strong { color: var(--accent); } +.chat-row.active .chat-row-heading time { color: #d4c878; } +.chat-preview { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1.5; } +.search-field > svg { width: 17px; height: 17px; color: var(--muted); } +.action-arrow { width: 20px; height: 20px; } +.member-item { grid-template-columns: 38px minmax(0, 1fr); gap: 12px; align-items: start; } +.member-item .avatar { width: 38px; height: 38px; border-radius: 12px; } +.member-details { display: grid; min-width: 0; gap: 8px; } +.member-details code { padding: 9px 10px; } +.message-scroll { background-size: 320px 280px; } +.messages-loading { display: grid; gap: 15px; padding: 22px 0; } +.messages-loading i { display: block; width: 48%; max-width: 240px; height: 53px; border-radius: 16px 16px 16px 5px; background: #ffffff06; } +.messages-loading i:nth-child(2) { justify-self: end; width: 37%; border-radius: 16px 16px 5px 16px; background: #ffe1350b; } +.messages-loading i:nth-child(3) { width: 58%; } +.send-button { transition: background .18s, transform .18s; } +.send-button:not(:disabled):active { transform: scale(.93); } +.send-button:not(:disabled):hover svg { transform: translate(1px, -1px); } +.send-button svg { transition: transform .18s; } +.icon-button.is-loading svg { animation: refresh-turn .9s linear infinite; } +.kolo-ear-left { transform-origin: 108px 135px; } +.kolo-ear-right { transform-origin: 195px 135px; } +.mascot-stage[data-mood=idle] .kolo-ear-left { animation: ear-settle 9s ease-in-out infinite; } +.spirit-svg { filter: drop-shadow(0 8px 12px #00000014); } +@keyframes refresh-turn { to { transform: rotate(360deg); } } +@keyframes ear-settle { 0%, 78%, 90%, 100% { transform: rotate(0); } 82% { transform: rotate(-3deg); } 86% { transform: rotate(2deg); } } +@media (prefers-reduced-motion: reduce) { + .mascot-stage[data-mood=idle] .kolo-ear-left, .icon-button.is-loading svg { animation: none; } + .send-button, .send-button svg { transition: none; } +} diff --git a/src/Messenger.Api/wwwroot/processing.js b/src/Messenger.Api/wwwroot/processing.js new file mode 100644 index 00000000..e54ab669 --- /dev/null +++ b/src/Messenger.Api/wwwroot/processing.js @@ -0,0 +1,56 @@ +let dialog, controller, timer, urls = [], revision = 0; +const labels = {queued:'В очереди',processing:'Обрабатывается',ready:'Готово',failed:'Ошибка'}; +const events = {accepted:'Файл принят и задание сохранено',processing:'Обработчик принял задание',ready:'Результаты сохранены',completed:'Результаты сохранены',published:'Отправлено в очередь',retry_scheduled:'Назначена повторная попытка',failed:'Обработка завершилась ошибкой',lease_recovered:'Задание восстановлено после остановки',operator_replay:'Запущено повторно'}; +const names = {thumbnail:'Миниатюра',crop:'Обрезанная версия',optimized:'WebP'}; +const size = n => n < 1048576 ? `${(n/1024).toFixed(1)} КБ` : `${(n/1048576).toFixed(2)} МБ`; +function el(tag, text, cls) { const node=document.createElement(tag);if(text!==undefined)node.textContent=text;if(cls)node.className=cls;return node; } +export function closeProcessing() { dialog?.close(); cleanup(); } +function cleanup() { revision++;clearTimeout(timer);controller?.abort();urls.forEach(URL.revokeObjectURL);urls=[]; } +export function openProcessing(id, token) { + closeProcessing(); + if(!dialog) { + dialog=el('dialog',undefined,'processing-panel');dialog.setAttribute('aria-label','Обработка изображения'); + document.body.append(dialog);dialog.addEventListener('close',()=>{if(!dialog.open)cleanup();}); + dialog.addEventListener('click',e=>{if(e.target===dialog)dialog.close();}); + } + const content=el('section'), header=el('header'), title=el('h2','Обработка изображения'); + const close=el('button','Закрыть');close.type='button';close.addEventListener('click',()=>dialog.close());header.append(title,close); + const body=el('div','Загрузка…');body.setAttribute('aria-live','polite');content.append(header,body);dialog.replaceChildren(content);dialog.showModal(); + const version=revision;controller=new AbortController();const signal=controller.signal; + async function get(path) { + const response=await fetch(`/api/v1/attachments/${id}${path}`,{headers:{Authorization:`Bearer ${token}`},cache:'no-store',signal:AbortSignal.any([signal,AbortSignal.timeout(15000)])}); + if(!response.ok)throw new Error(response.status===404?'Изображение недоступно':'Не удалось получить состояние');return response; + } + let rendered=false; + async function refresh() { + try { + const [meta,job]=await Promise.all([get('').then(r=>r.json()),get('/processing').then(r=>r.json())]); + if(version!==revision)return; + const summary=el('div',undefined,'processing-summary');summary.append(el('strong',meta.filename),el('span',labels[meta.status]||meta.status,'processing-badge')); + const steps=el('ol',undefined,'processing-steps'); + ['queued','processing','ready'].forEach((state,index)=>{const step=el('li',labels[state]);if(index<=['queued','processing','ready'].indexOf(meta.status))step.className='reached';steps.append(step);}); + const details=el('p',`Оригинал: ${size(meta.size_bytes)} · Попыток: ${job.attempts}`); + if(job.completed_at&&job.created_at) details.append(document.createTextNode(` · Весь цикл: ${Math.max(0,(Date.parse(job.completed_at)-Date.parse(job.created_at))/1000).toFixed(2)} с`)); + const history=el('ol',undefined,'processing-history'); + if(job.created_at)history.append(el('li',`${new Date(job.created_at).toLocaleTimeString()} · Задание создано`)); + for(const event of job.events)history.append(el('li',`${new Date(event.at).toLocaleTimeString()} · ${events[event.name]||event.name}${event.attempt?' · попытка '+event.attempt:''}`)); + if(!rendered)body.replaceChildren(summary,steps,details,el('h3','События задания'),history); + if(meta.error_code)body.append(el('p',`Причина: ${meta.error_code}`,'processing-error')); + if(meta.status==='ready'&&!rendered) { + rendered=true;const grid=el('div',undefined,'processing-results');body.append(el('h3','Результаты'),grid); + const variants=[{name:'original',size_bytes:meta.size_bytes,content_type:meta.content_type},...meta.variants]; + for(const variant of variants) { + const card=el('article'),label=variant.name==='original'?'Оригинал':names[variant.name]||variant.name; + card.append(el('strong',label),el('p',`${size(variant.size_bytes)}${variant.width?' · '+variant.width+' × '+variant.height:''}`)); + if(variant.name==='optimized')card.append(el('p',variant.size_bytesopenProcessing(id,token);body.append(retry);}} + } + void refresh(); +} diff --git a/src/Messenger.Api/wwwroot/reactions.js b/src/Messenger.Api/wwwroot/reactions.js new file mode 100644 index 00000000..4eaa0a75 --- /dev/null +++ b/src/Messenger.Api/wwwroot/reactions.js @@ -0,0 +1,45 @@ +const choices = [['like','👍','Нравится'],['love','❤️','Сердце'],['laugh','😂','Смешно'],['mushroom','🍄','Гриб']]; + +export class ReactionControls { + constructor(messageId) { + this.root = document.createElement('div'); this.root.className = 'reaction-controls'; + this.badges = new Map(); this.items = []; + this.picker = document.createElement('button'); this.picker.type = 'button'; this.picker.className = 'reaction-picker'; + this.picker.textContent = '☺+'; this.picker.setAttribute('aria-label', 'Добавить реакцию'); this.picker.setAttribute('aria-expanded','false'); + this.menu = document.createElement('div'); this.menu.className = 'reaction-menu'; this.menu.hidden = true; + this.menu.setAttribute('role','group'); this.menu.setAttribute('aria-label','Выберите реакцию'); + this.root.append(this.picker, this.menu); + const choose = reaction => { + this.close(); + this.root.dispatchEvent(new CustomEvent('reactionchange', { bubbles: true, + detail: { messageId, reaction, present: !this.items.find(item => item.reaction === reaction)?.mine } })); + }; + for (const [reaction, emoji, label] of choices) { + const button = document.createElement('button'); button.type = 'button'; button.textContent = emoji; + button.setAttribute('aria-label',label); button.addEventListener('click',() => choose(reaction)); this.menu.append(button); + const badge = document.createElement('button'); badge.type = 'button'; badge.className = 'reaction-button'; badge.hidden = true; + badge.dataset.reaction = reaction; badge.addEventListener('click',() => choose(reaction)); + this.root.insertBefore(badge,this.picker); this.badges.set(reaction,{badge,emoji,label}); + } + this.picker.addEventListener('click',() => { + this.menu.hidden = !this.menu.hidden; this.picker.setAttribute('aria-expanded',String(!this.menu.hidden)); + if (!this.menu.hidden) this.menu.querySelector('button').focus(); + }); + this.root.addEventListener('keydown',event => { if (event.key === 'Escape') { this.close(); this.picker.focus(); event.stopPropagation(); } }); + this.root.addEventListener('focusout',event => { if (!this.root.contains(event.relatedTarget)) this.close(); }); + } + close() { this.menu.hidden = true; this.picker.setAttribute('aria-expanded','false'); } + render(items = [], busy = false) { + const signature = JSON.stringify([items,busy]); + if (signature === this.signature) return; + this.signature = signature; this.items = items; + for (const [reaction, {badge,emoji,label}] of this.badges) { + const item = items.find(item => item.reaction === reaction); + badge.hidden = !item?.count; + badge.textContent = `${emoji} ${item?.count || 0}`; + badge.setAttribute('aria-label',`${label}: ${item?.count || 0}${item?.mine ? ', ваша реакция' : ''}`); + badge.setAttribute('aria-pressed',String(Boolean(item?.mine))); + } + for (const button of this.root.querySelectorAll('button')) button.disabled = busy; + } +} diff --git a/src/Messenger.Api/wwwroot/showcase.css b/src/Messenger.Api/wwwroot/showcase.css new file mode 100644 index 00000000..f4f0e2e9 --- /dev/null +++ b/src/Messenger.Api/wwwroot/showcase.css @@ -0,0 +1,41 @@ +.cluster-section { margin-bottom: 18px; padding-bottom: 18px; border-bottom: 1px solid var(--border); } +.cluster-section h3 { margin: 0; font-size: 15px; color: #eeeadd; } +.cluster-live { display: inline-flex; align-items: center; gap: 5px; color: #b9d99f; font-size: 9px; letter-spacing: 1px; } +.cluster-live::before { content: ''; width: 5px; height: 5px; border-radius: 50%; background: currentColor; } +.cluster-status { font-size: 11px; color: var(--muted); line-height: 1.6; margin: 10px 0 14px; } +.cluster-graph { display: grid; justify-items: center; gap: 16px; position: relative; isolation: isolate; } +.cluster-graph::before { content: ''; position: absolute; top: 10px; bottom: 15px; width: 1px; background: #706b4360; z-index: -1; } +.cluster-node { background: #30312c; border: 1px solid #535447; border-radius: 11px; text-align: center; padding: 10px; transition: border-color .25s, background .25s; min-width: 0; } +.cluster-node strong { display: block; font-size: 11px; color: #ecebdc; } +.cluster-node span, .cluster-node small { display: block; font-size: 9px; margin-top: 5px; color: var(--muted); } +.cluster-browser, .cluster-balancer { width: 70%; } +.cluster-balancer[data-state=up] { border-color: #8e8552; } +.cluster-instances, .cluster-stores { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); width: 100%; gap: 8px; } +.cluster-instances, .cluster-stores { position: relative; } +.cluster-instances::before, .cluster-stores::before { content: ''; position: absolute; top: -8px; left: 25%; right: 25%; border-top: 1px solid #706b4360; } +.cluster-api, .cluster-stores .cluster-node { position: relative; } +.cluster-api::before, .cluster-stores .cluster-node::before { content: ''; position: absolute; top: -9px; left: 50%; height: 8px; border-left: 1px solid #706b4360; } +.cluster-api[data-state=up] span { color: #b9d99f; } +.cluster-node[data-state=down] { background: #3b302e; border-color: #a36154; } +.cluster-node[data-state=down] span { color: #ffb2a2; } +.cluster-node[data-state=unknown] { border-style: dashed; } +.cluster-api.just-served { border-color: var(--accent); background: #48442b; box-shadow: 0 0 0 3px #ffe1350b; } +.cluster-detail { font: 10px/1.7 ui-monospace,monospace; color: #dbd4a3; min-height: 34px; margin: 13px 0 8px; overflow-wrap: anywhere; } +.cluster-section .text-button { font-size: 11px; padding: 7px 0; } +.cluster-note { font-size: 10px; line-height: 1.65; color: var(--muted); } +.cluster-demo { border-top: 1px solid var(--border); margin-top: 10px; padding-top: 12px; } +.cluster-demo summary { color: var(--accent); font-size: 11px; cursor: pointer; } +.cluster-command { display: block; font: 10px/1.7 ui-monospace,monospace; color: #dfdabe; background: #252623; border: 1px solid var(--border); padding: 10px; border-radius: 9px; overflow-wrap: anywhere; user-select: all; margin: 10px 0; } +.reaction-controls { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; min-height: 25px; } +.message.mine .reaction-controls { justify-content: flex-end; } +.reaction-button, .reaction-picker { border: 1px solid #62624b; background: #30312b; color: #e6e2cd; border-radius: 16px; font-size: 12px; min-height: 26px; min-width: 32px; padding: 3px 8px; line-height: 1.4; } +.reaction-button[aria-pressed=true] { background: #ffe1351a; border-color: #b8aa4f; color: var(--accent); } +.reaction-button:hover, .reaction-picker:hover { border-color: var(--accent); } +.reaction-picker { color: var(--muted); font-size: 15px; opacity: .35; } +.message:hover .reaction-picker, .message:focus-within .reaction-picker { opacity: 1; } +.reaction-menu { display: flex; gap: 3px; background: #292a25; border: 1px solid #62624b; border-radius: 18px; padding: 3px; } +.reaction-menu button { background: transparent; border: 0; border-radius: 12px; padding: 5px 7px; font-size: 17px; } +.reaction-menu button:hover { background: #ffe13518; } +.reaction-error { font-size: 10px; color: var(--danger); margin: 0; } +@media (hover:none) { .reaction-picker { opacity: .8; } } +@media (prefers-reduced-motion:reduce) { .cluster-node { transition:none; } } diff --git a/student-tests/chunked_login.py b/student-tests/chunked_login.py new file mode 100644 index 00000000..bbf15c72 --- /dev/null +++ b/student-tests/chunked_login.py @@ -0,0 +1,14 @@ +import sys,base64,http.client,json +from pathlib import Path +sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests')) +from client import Client,check +c=Client('http://localhost:18085');actor=c.user('chunked') +registration=actor['registration'] +auth='Basic '+base64.b64encode((registration['username']+':'+registration['password']).encode()).decode() +for body,expected in (([],201),([b'x'],400)): + conn=http.client.HTTPConnection('localhost',18085,timeout=10) + conn.request('POST','/api/v1/auth/sessions',body=iter(body),headers={'Authorization':auth,'Transfer-Encoding':'chunked'},encode_chunked=True) + response=conn.getresponse();data=json.loads(response.read());conn.close() + check(response.status==expected,'chunked login status mismatch') + check(('token' in data) if expected==201 else data['error']['code']=='invalid_request','unexpected result') +print('PASS: empty chunked login accepted; nonempty chunked login rejected') diff --git a/student-tests/operations_panel.py b/student-tests/operations_panel.py new file mode 100644 index 00000000..c0687246 --- /dev/null +++ b/student-tests/operations_panel.py @@ -0,0 +1,38 @@ +import base64 +from support import * +# The operator session is supplied locally, never stored in evidence. +import os +password=os.environ.get('OPS_TEST_PASSWORD') +if not password:raise SystemExit('Set OPS_TEST_PASSWORD for the configured local operator') +c=Client('http://localhost:18085') +credential=base64.b64encode((os.environ.get('OPS_TEST_USERNAME','n1k0der')+':'+password).encode()).decode() +session=parsed(c,'POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential}) +owner={'id':session['user']['id'],'token':session['token']} +a,b,e,chat=c.bootstrap() +check(parsed(c,'GET','/api/ui/operator',actor=owner)['enabled'],'operator not enabled') +check(not parsed(c,'GET','/api/ui/operator',actor=a)['enabled'],'regular user promoted') +c.call('GET','/api/ui/operations',401) +c.call('GET','/api/ui/operations',404,actor=a) +snapshot=parsed(c,'GET','/api/ui/operations',actor=owner) +check(all(name in snapshot['counts'] for name in ('queued','processing','ready','failed')),'counts missing') +check(snapshot['services']['postgres'] and snapshot['services']['s3'] and snapshot['services']['broker'],'probes failed') +user=parsed(c,'GET','/api/ui/users/n1k0der',actor=a) +check(user['id']==owner['id'] and 'password_hash' not in user,'lookup unsafe') +c.call('GET','/api/ui/users/n1k0der',401) +attachment=upload(c,a,chat)['json'];ready(c,a,attachment) +path='/api/ui/operations/attachments/'+attachment['id']+'/replay' +c.call('POST',path,404,actor=a);c.call('POST',path,401);c.call('POST',path,409,actor=owner) +# A controlled failed-state fixture retains the original staging bytes for replay. +compose('stop','worker') +try: + attachment=upload(c,a,chat)['json'];id=job_id(attachment) + sql("UPDATE attachment_jobs SET state='failed',error_code='test_fixture' WHERE id='"+id+"'; UPDATE attachments SET status='failed',error_code='test_fixture' WHERE id='"+attachment['id']+"'") + path='/api/ui/operations/attachments/'+attachment['id']+'/replay' + c.call('POST',path,202,actor=owner);c.call('POST',path,409,actor=owner) + check(sql("SELECT replay_count FROM attachment_jobs WHERE id='"+id+"'")=='1','replay duplicated') +finally:compose('up','-d','--no-deps','worker') +def completed(): + result=c.call('GET','/api/v1/attachments/'+attachment['id'],[200,503],actor=a,schema='Attachment') + return result['json']['status']=='ready' if result['status']==200 else False +wait(completed) +print('PASS: operator-only metrics and replay; anonymous 401; regular user 404; exact login lookup; one replay completes with real worker') diff --git a/student-tests/overload_research.py b/student-tests/overload_research.py new file mode 100644 index 00000000..b315d190 --- /dev/null +++ b/student-tests/overload_research.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +from support import * +import random,threading,concurrent.futures,statistics,datetime,hashlib +from client import arguments,run + +def worker_sample(): + command='''printf 'memory ';cat /sys/fs/cgroup/memory.current; printf 'cpu ';awk '/^usage_usec/{print $2}' /sys/fs/cgroup/cpu.stat; printf 'rss '; for p in $(cat /sys/fs/cgroup/cgroup.procs); do if [ "$(cat /proc/$p/comm 2>/dev/null)" = dotnet ]; then awk '/^VmRSS:/{print $2}' /proc/$p/status 2>/dev/null; fi; done''' + lines=compose('exec','-T','worker','sh','-c',command).splitlines() + mem=int(lines[0].split()[1]);cpu=int(lines[1].split()[1]);rss=sum(int(x) for x in ('\n'.join(lines[2:]).replace('rss ','').split()))*1024 + return {'time':time.time(),'memory_bytes':mem,'cpu_usec':cpu,'rss_bytes':rss} +def main(): + args=arguments('Concurrency/prefetch overload research');c=Client(args.base_url,args.ca_file,timeout=100);a,b,e,chat=c.bootstrap() + rng=random.Random(202605) + large=Image.frombytes('RGB',(1024,1024),rng.randbytes(1024*1024*3));out=io.BytesIO();large.save(out,format='PNG',compress_level=1) + images={'small':(ROOT/'tests/fixtures/sample.png').read_bytes(),'large':out.getvalue()} + report={'hypothesis':'With fixed worker CPU, more simultaneous image processes need not improve throughput and can increase RSS; bounded admission protects the queue when the worker stops.', + 'profile':{'settings':[[1,1],[2,2],[4,4]],'repeats':3,'batch':8,'upload_clients':4,'worker_cpu':1,'worker_memory_limit_mib':1024,'warmup_images':1,'image_sizes_bytes':{k:len(v) for k,v in images.items()}},'runs':[]} + configurations=[(1,1),(2,2),(4,4)] + try: + wait(lambda:sql("SELECT count(*) FROM attachment_jobs WHERE state IN ('queued','processing')")=='0') + for repeat in range(3): + for label in (['small','large'] if repeat%2==0 else ['large','small']): + for concurrency,prefetch in configurations[repeat:]+configurations[:repeat]: + env={'WORKER_CONCURRENCY':str(concurrency),'WORKER_PREFETCH':str(prefetch)} + compose('up','-d','--no-deps','--force-recreate','worker',env=env) + wait(lambda:'Worker ready:' in compose('logs','--tail','5','worker'),40) + warm=upload(c,a,chat,images[label],name='warm.png')['json'];ready(c,a,warm) + samples=[];stop=threading.Event() + def poll(): + while not stop.is_set(): + try:samples.append(worker_sample()) + except Exception:pass + stop.wait(.2) + thread=threading.Thread(target=poll);thread.start();started=time.monotonic();requests=[] + def submit(index): + begin=time.monotonic();result=upload(c,a,chat,images[label],name=f'{label}-{repeat}-{index}.png') + requests.append({'upload_seconds':time.monotonic()-begin,'status':result['status']});return result['json'] + try: + with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:attachments=list(pool.map(submit,range(8))) + ids=','.join("'"+item['id']+"'" for item in attachments) + wait(lambda:sql('SELECT count(*) FROM attachment_jobs WHERE state=\'ready\' AND attachment_id IN ('+ids+')')=='8',180) + finally:stop.set();thread.join() + elapsed=time.monotonic()-started + values=json.loads(sql('''SELECT json_agg(json_build_object('id',j.id,'ready_seconds',extract(epoch from j.completed_at-j.created_at),'queue_lag_seconds',extract(epoch from (SELECT min(e.created_at) FROM attachment_job_events e WHERE e.job_id=j.id AND e.event='processing')-j.created_at),'attempts',j.attempts)) FROM attachment_jobs j WHERE j.attachment_id IN ('''+ids+')')) + check(all(row['attempts']==1 for row in values),'unplanned retries in comparison run') + row={'repeat':repeat+1,'image':label,'concurrency':concurrency,'prefetch':prefetch,'successful_jobs':8,'errors':0,'seconds':elapsed,'throughput':8/elapsed, + 'p95_ready_seconds':sorted(v['ready_seconds'] for v in values)[-1], 'p95_queue_lag_seconds':sorted(v['queue_lag_seconds'] for v in values)[-1], + 'peak_rss_bytes':max(v['rss_bytes'] for v in samples),'peak_cgroup_memory_bytes':max(v['memory_bytes'] for v in samples), + 'worker_cpu_seconds':(samples[-1]['cpu_usec']-samples[0]['cpu_usec'])/1e6,'requests':requests,'jobs':values,'samples':samples} + report['runs'].append(row) + (ROOT/'evidence/overload-progress.json').write_text(json.dumps(report,indent=2)+'\n') + print(f"run {len(report['runs'])}/18: {label}, {concurrency}/{prefetch}, {row['throughput']:.2f} ready/s",flush=True) + compose('stop','worker');compose('up','-d','--no-deps','--force-recreate','--wait','api',env={'PIPELINE_CAPACITY':'4'});wait_api(c) + def pressure(index):return upload(c,a,chat,name='pressure-'+str(index)+'.png',expected=[202,429]) + with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool:attempts=list(pool.map(pressure,range(8))) + accepted=[json.loads(r['bytes']) for r in attempts if r['status']==202];rejected=sum(r['status']==429 for r in attempts) + pending=int(sql("SELECT count(*) FROM attachment_jobs WHERE state IN ('queued','processing')")) + check(len(accepted)==4 and rejected==4 and pending==4,'admission bound was exceeded') + before=time.monotonic();compose('up','-d','--no-deps','--force-recreate','worker') + for att in accepted:ready(c,a,att) + report['backpressure']={'attempted':8,'accepted':4,'rejected_429':4,'pending_at_limit':pending,'all_accepted_recovered':True,'recovery_seconds':time.monotonic()-before} + report['environment']=subprocess.check_output(['docker','info','--format','{{.OSType}} {{.Architecture}} {{.NCPU}} CPUs {{.MemTotal}} bytes RAM']).decode().strip() + h=hashlib.sha256() + for source in sorted((ROOT/'src/Messenger.Api').rglob('*.cs')): + if 'obj' in source.parts or 'bin' in source.parts:continue + h.update(str(source.relative_to(ROOT)).encode());h.update(source.read_bytes()) + report['source_sha256']=h.hexdigest() + (ROOT/'evidence/overload.json').write_text(json.dumps(report,indent=2)+'\n') + lines=['# Очередь под нагрузкой','','Три повтора; одинаковые CPU/RAM worker; batch 8, четыре HTTP-клиента.','', + '| Файл | Concurrency / prefetch | Готовых/с, медиана | p95 до ready, s | p95 ожидания, s | RSS процессов, MiB |','| --- | --- | ---: | ---: | ---: | ---: |'] + for label in images: + for concurrency,prefetch in configurations: + group=[r for r in report['runs'] if r['image']==label and r['concurrency']==concurrency] + med=lambda key:statistics.median(r[key] for r in group) + lines.append(f"| {label} | {concurrency}/{prefetch} | {med('throughput'):.3f} | {med('p95_ready_seconds'):.3f} | {med('p95_queue_lag_seconds'):.3f} | {med('peak_rss_bytes')/1048576:.1f} |") + lines+=['','p95: nearest rank; при batch 8 это максимальная задержка. RSS: сумма VmRSS','процессов dotnet внутри worker, включая дочерние декодеры; общие страницы могут','считаться несколько раз. memory.current измерен отдельно. Период выборок 200 ms','плюс docker exec; короткие пики могут быть пропущены. Время включает загрузку','batch и ожидание последнего ready, без регистрации и прогрева. Queue lag: от','создания записи задания до первой processing. Время до ready — по часам PostgreSQL.','', + 'Backpressure: при остановленном worker лимит 4 принял ровно 4 из 8 файлов;','остальные получили 429. После запуска обработались все четыре принятых задания.','', + 'Исходные результаты: evidence/overload.json. Один Docker-host, небольшие batch,','параллельно могут работать другие локальные приложения. Эти значения не являются','пределом производительности сервера. Вывод сравнения относится к этому профилю.'] + lines+=['','![Результаты](../evidence/overload.png)','','В этом профиле для small один обработчик оказался быстрее двух и четырёх.','Для large вариант 2/2 дал лучшую медиану throughput и меньшую p95 готовности,','чем 1/1, при большем RSS. Четыре обработчика уменьшили время ожидания начала,','но увеличили полное время обработки и память. Поэтому по умолчанию оставлены','2/1: два параллельных обработчика с небольшим prefetch. Низкий prefetch','ограничивает число взятых сообщений; пара 2/1 отдельно не входит в сравнение,','поэтому не заявляется как измеренный оптимум.'] + (ROOT/'docs/OVERLOAD.md').write_text('\n'.join(lines)+'\n') + print('PASS: overload research and bounded admission',flush=True) + finally:compose('up','-d','--no-deps','--force-recreate','--wait','api','worker') +if __name__=='__main__':run(main) diff --git a/student-tests/pipeline_tests.py b/student-tests/pipeline_tests.py new file mode 100644 index 00000000..c9fcee22 --- /dev/null +++ b/student-tests/pipeline_tests.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +from support import * +import hashlib,concurrent.futures +from client import arguments,run + +def main(): + args=arguments('Pipeline durability, image variants and controlled failures');c=Client(args.base_url,args.ca_file,timeout=100) + a,b,e,chat=c.bootstrap();checks=[] + png_bytes=(ROOT/'tests/fixtures/sample.png').read_bytes() + upload(c,a,chat,b'bad',expected=415);upload(c,a,chat,png_bytes,mime='image/jpeg',expected=415) + upload(c,a,chat,b'x'*(10485760+1),expected=413) + upload(c,a,chat,png_bytes,name='../unsafe.png',expected=400) + other=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'Other','member_ids':[a['id']]},schema='Chat')['json'] + checks.append('HTTP size/MIME/corruption/filename validation remains compatible with Lab4') + faultenv={'PIPELINE_FAULT_FILE':'/run/faults/control.json','JOB_MAX_ATTEMPTS':'3'} + try: + compose('stop','worker','dispatcher') + att=upload(c,a,chat)['json'];id=job_id(att) + check(att['status']=='queued','upload is not a queued snapshot') + c.message(a,chat,'Queued image',attachments=[att['id']]);c.call('GET','/api/v1/attachments/'+att['id']+'/content',409,actor=b) + check(sql("SELECT published_at IS NULL FROM attachment_outbox WHERE job_id='"+id+"'")=='t','outbox not retained') + compose('restart','api');compose('restart','broker');wait_api(c) + compose('up','-d','--wait','dispatcher',env=faultenv) + wait(lambda:sql("SELECT published_at IS NOT NULL FROM attachment_outbox WHERE job_id='"+id+"'")=='t') + queue_before=compose('exec','-T','-e','RABBITMQ_CTL_ERL_ARGS=+S 1:1','broker','rabbitmqctl','list_queues','-p','messenger','name','messages_ready','--formatter=json') + compose('restart','broker');compose('up','-d','--wait','--wait-timeout','90','broker') + compose('up','-d','--no-deps','worker',env=faultenv);meta=ready(c,b,att) + check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=b)['bytes']==(ROOT/'tests/fixtures/sample.png').read_bytes(),'original changed') + checks.append('202 while worker stopped; durable outbox survives API restart; published queue survives broker restart; same ID becomes ready') + dimensions={v['name']:(v['width'],v['height']) for v in meta['variants']} + check(dimensions=={'thumbnail':(256,160),'crop':(128,128),'optimized':(320,200)},'wrong image dimensions') + for variant in meta['variants']: + data=c.call('GET','/api/v1/attachments/'+att['id']+'/content?variant='+variant['name'],actor=b)['bytes'] + image=Image.open(io.BytesIO(data));check(image.size==dimensions[variant['name']],'encoded image dimensions differ') + check(hashlib.sha256(data).hexdigest()==variant['sha256'] and len(data)==variant['size_bytes'],'variant checksum mismatch') + check(not image.getexif(),'EXIF was retained in a variant') + for endpoint in ('','/content?variant=thumbnail'):c.call('GET','/api/v1/attachments/'+att['id']+endpoint,404,actor=e) + oriented=upload(c,a,chat,image_bytes(320,200,orientation=6),'rotated.jpg','image/jpeg')['json'];om=ready(c,a,oriented) + check(next(v for v in om['variants'] if v['name']=='thumbnail')['width']==160,'EXIF orientation ignored') + small=upload(c,a,chat,image_bytes(64,32,alpha=True),'alpha.png')['json'];sm=ready(c,a,small) + thumb=c.call('GET','/api/v1/attachments/'+small['id']+'/content?variant=thumbnail',actor=a)['bytes'];im=Image.open(io.BytesIO(thumb)) + check(im.size==(64,32) and min(im.convert('RGB').getpixel((0,0)))>=250,'alpha is not white or small image upscaled') + checks.append('thumbnail/crop/WebP bytes and dimensions; no EXIF; orientation applied; transparent small image remains small with white background; private variants') + before=sql("SELECT attempts||':'||completed_at FROM attachment_jobs WHERE id='"+id+"'") + for _ in range(3):compose('exec','-T','dispatcher','dotnet','Messenger.Api.dll','--redeliver',id) + time.sleep(2);check(sql("SELECT attempts||':'||completed_at FROM attachment_jobs WHERE id='"+id+"'")==before,'ready duplicate reprocessed') + checks.append('three duplicate ready deliveries are no-op') + # Pause deterministically after all S3 writes, kill worker before result COMMIT/ack. + for point in ('after_objects','after_commit'): + compose('stop','worker') + interrupted=upload(c,a,chat,name=point+'.png')['json'];jid=job_id(interrupted);fault(point,jid) + compose('up','-d','--no-deps','--force-recreate','worker',env=faultenv);wait_fault() + compose('kill','-s','SIGKILL','worker');clear_fault() + compose('up','-d','--no-deps','worker',env=faultenv);ready(c,a,interrupted,130) + count=int(sql("SELECT count(*) FROM attachment_job_events WHERE job_id='"+jid+"' AND event='ready'")) + check(count==1,'completion was duplicated') + checks.append(point+': worker killed, same logical job recovered, one published result') + # Publish confirmed, dispatcher killed before marking the outbox. + compose('stop','dispatcher','worker');pub=upload(c,a,chat,name='publisher.png')['json'];pubid=job_id(pub);fault('after_publish',pubid) + compose('up','-d','--no-deps','--force-recreate','dispatcher',env=faultenv);wait_fault();compose('kill','-s','SIGKILL','dispatcher');clear_fault() + compose('up','-d','--no-deps','dispatcher','worker',env=faultenv);ready(c,a,pub) + check(sql("SELECT count(*) FROM attachment_job_events WHERE job_id='"+pubid+"' AND event='ready'")=='1','confirm-window duplicate effect') + checks.append('publisher confirm before outbox COMMIT crash recovers without lost task or duplicate effect') + # Permanent failure, without retrying invalid input forever. + compose('stop','worker');bad=upload(c,a,chat,name='invalid-stage.png')['json'];bid=job_id(bad) + staging=sql("SELECT staging_key FROM attachment_jobs WHERE id='"+bid+"'");s3('PUT',staging,b'broken') + compose('up','-d','--no-deps','worker',env=faultenv) + wait(lambda:sql("SELECT state FROM attachment_jobs WHERE id='"+bid+"'")=='failed') + check(sql("SELECT attempts FROM attachment_jobs WHERE id='"+bid+"'")=='1','permanent failure retried') + c.call('POST','/api/v1/chats/'+chat['id']+'/messages',409,actor=a,body={'text':'','attachment_ids':[bad['id']]},headers={'Idempotency-Key':uuid.uuid4().hex}) + checks.append('invalid staging checksum becomes failed after one attempt; failed attachment cannot be linked') + compose('stop','worker');huge=upload(c,a,chat,name='worker-pixels.png')['json'];hid=job_id(huge) + import struct,zlib + payload=bytearray(png_bytes);payload[16:24]=struct.pack('>II',10000,10000);payload[29:33]=struct.pack('>I',zlib.crc32(payload[12:29]));payload=bytes(payload) + staging=sql("SELECT staging_key FROM attachment_jobs WHERE id='"+hid+"'");s3('PUT',staging,payload) + sql("UPDATE attachments SET sha256='"+hashlib.sha256(payload).hexdigest()+"',size_bytes="+str(len(payload))+" WHERE id='"+huge['id']+"'") + compose('up','-d','--no-deps','worker',env=faultenv) + wait(lambda:sql("SELECT state FROM attachment_jobs WHERE id='"+hid+"'")=='failed') + check(sql("SELECT attempts||':'||error_code FROM attachment_jobs WHERE id='"+hid+"'")=='1:invalid_image','worker pixel guard failed') + checks.append('worker independently rejects >40 million pixels before bitmap allocation') + # Transient SQL failure after object writes: bounded retries, jitter/backoff, explicit replay. + sql("CREATE FUNCTION test_pipeline_failure() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.filename='retry-me.png' AND NEW.status='ready' THEN RAISE EXCEPTION 'injected'; END IF; RETURN NEW; END $$; CREATE TRIGGER test_pipeline_failure BEFORE UPDATE ON attachments FOR EACH ROW EXECUTE FUNCTION test_pipeline_failure();") + retry=upload(c,a,chat,name='retry-me.png')['json'];rid=job_id(retry) + wait(lambda:sql("SELECT state FROM attachment_jobs WHERE id='"+rid+"'")=='failed',120) + check(sql("SELECT attempts||':'||error_code FROM attachment_jobs WHERE id='"+rid+"'")=='3:retry_exhausted','retry limit failed') + sql('DROP TRIGGER test_pipeline_failure ON attachments; DROP FUNCTION test_pipeline_failure();') + compose('exec','-T','dispatcher','dotnet','Messenger.Api.dll','--replay',rid);ready(c,a,retry) + finished=sql("SELECT completed_at FROM attachment_jobs WHERE id='"+rid+"'") + compose('exec','-T','dispatcher','dotnet','Messenger.Api.dll','--replay',rid) + check(sql("SELECT completed_at FROM attachment_jobs WHERE id='"+rid+"'")==finished,'replay reran successful job') + checks.append('transient failure retries with bounded attempts then failed; explicit replay succeeds; replay of ready is no-op') + wait(lambda:sql("SELECT staging_deleted_at IS NOT NULL FROM attachment_jobs WHERE id='"+rid+"'")=='t') + check(sql("SELECT staging_deleted_at IS NULL FROM attachment_jobs WHERE id='"+bid+"'")=='t','failed staging was cleaned too soon') + checks.append('ready staging cleaned after durable publication; failed staging retained for replay') + report={'checks':checks,'broker_queue_before_restart':queue_before,'retry_test_max_attempts':3,'default_max_attempts':5} + (ROOT/'evidence/pipeline.json').write_text(json.dumps(report,indent=2)+'\n');print('PASS: '+str(len(checks))+' pipeline scenarios') + finally: + clear_fault() + sql('DROP TRIGGER IF EXISTS test_pipeline_failure ON attachments; DROP FUNCTION IF EXISTS test_pipeline_failure();') + compose('up','-d','--no-deps','--force-recreate','dispatcher','worker') +if __name__=='__main__':run(main) diff --git a/student-tests/processing_panel.py b/student-tests/processing_panel.py new file mode 100644 index 00000000..88ddca95 --- /dev/null +++ b/student-tests/processing_panel.py @@ -0,0 +1,15 @@ +from support import * +c=Client('http://localhost:18085') +a,b,e,chat=c.bootstrap() +attachment=upload(c,a,chat)['json'];meta=ready(c,b,attachment) +path='/api/v1/attachments/'+attachment['id']+'/processing' +for actor in (a,b): + job=parsed(c,'GET',path,actor=actor) + check(job['state']=='ready' and job['attempts']>=1,'job state missing') + check(job['completed_at'] and any(event['name']=='ready' for event in job['events']),'completion missing') + check('staging_key' not in job and 'lease_token' not in job,'internal details exposed') +c.call('GET',path,404,actor=e) +c.call('GET',path,401) +c.message(a,chat,'Изображение с результатами обработки',attachments=[attachment['id']]) +(ROOT/'.local/processing-user.json').write_text(json.dumps(a['registration'])) +print('PASS: processing states/events available to chat members; stranger 404; anonymous 401') diff --git a/student-tests/support.py b/student-tests/support.py new file mode 100644 index 00000000..6934f81b --- /dev/null +++ b/student-tests/support.py @@ -0,0 +1,57 @@ +import sys,subprocess,time,json,uuid,io +from pathlib import Path +ROOT=Path(__file__).resolve().parents[1] +sys.path.insert(0,str(ROOT/'tests')) +from client import Client,check +from PIL import Image + +def compose(*args,env=None,input=None,timeout=180): + import os + r=subprocess.run([str(ROOT/'scripts/compose.sh'),*args],cwd=ROOT,env={**os.environ,**(env or {})},input=input,capture_output=True,timeout=timeout) + check(r.returncode==0,'Infrastructure command failed: '+r.stderr.decode()[-500:]);return r.stdout.decode() +def sql(text):return compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-c',text).strip() +def parsed(c,method,path,expected=200,**kwargs): + result=c.call(method,path,expected,**kwargs);return json.loads(result['bytes']) +def upload(c,actor,chat,data=None,name='image.png',mime='image/png',expected=202): + if data is None:data=(ROOT/'tests/fixtures/sample.png').read_bytes() + from urllib.parse import quote + return c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename='+quote(name,safe=''),expected,actor=actor,raw=data,headers={'Content-Type':mime},schema='Attachment' if expected==202 else None) +def job_id(attachment):return sql("SELECT id FROM attachment_jobs WHERE attachment_id='"+attachment['id']+"'") +def wait(predicate,timeout=120): + deadline=time.monotonic()+timeout + while time.monotonic()