diff --git a/apps/backend/drizzle/0016_dashboard_history.sql b/apps/backend/drizzle/0016_dashboard_history.sql new file mode 100644 index 0000000..bcaaaa6 --- /dev/null +++ b/apps/backend/drizzle/0016_dashboard_history.sql @@ -0,0 +1,10 @@ +CREATE TABLE `catalog_size_history` ( + `day` text NOT NULL, + `source_id` text NOT NULL, + `source_name` text NOT NULL, + `titles` integer NOT NULL, + `bytes` integer NOT NULL, + PRIMARY KEY(`day`, `source_id`) +); +--> statement-breakpoint +ALTER TABLE `downloads` ADD `imported_at` text; \ No newline at end of file diff --git a/apps/backend/drizzle/meta/0016_snapshot.json b/apps/backend/drizzle/meta/0016_snapshot.json new file mode 100644 index 0000000..6cb5fe6 --- /dev/null +++ b/apps/backend/drizzle/meta/0016_snapshot.json @@ -0,0 +1,1141 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "c816c0be-da5b-47a1-bec2-08a212729d66", + "prevId": "14e99ced-c742-4437-9940-60a0a5a6863a", + "tables": { + "api_keys": { + "name": "api_keys", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "key_hash": { + "name": "key_hash", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "expires_at": { + "name": "expires_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "api_keys_key_hash_unique": { + "name": "api_keys_key_hash_unique", + "columns": [ + "key_hash" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "catalog_size_history": { + "name": "catalog_size_history", + "columns": { + "day": { + "name": "day", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "source_id": { + "name": "source_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "source_name": { + "name": "source_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "titles": { + "name": "titles", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "bytes": { + "name": "bytes", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "catalog_size_history_day_source_id_pk": { + "columns": [ + "day", + "source_id" + ], + "name": "catalog_size_history_day_source_id_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "downloads": { + "name": "downloads", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "torrent_filename": { + "name": "torrent_filename", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "peer_id": { + "name": "peer_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "peer_name": { + "name": "peer_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "item_id": { + "name": "item_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "filename": { + "name": "filename", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "dest_path": { + "name": "dest_path", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "part_path": { + "name": "part_path", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "release_size": { + "name": "release_size", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "release_json": { + "name": "release_json", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "expected_bytes": { + "name": "expected_bytes", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "expected_bytes_source": { + "name": "expected_bytes_source", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "expected_bytes_mismatch": { + "name": "expected_bytes_mismatch", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "downloaded_bytes": { + "name": "downloaded_bytes", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "speed_bps": { + "name": "speed_bps", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "attempts": { + "name": "attempts", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "started_at": { + "name": "started_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "completed_at": { + "name": "completed_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "imported_at": { + "name": "imported_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "error_details": { + "name": "error_details", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_operation": { + "name": "last_operation", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'transfer'" + }, + "operation_failed": { + "name": "operation_failed", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "qb_category": { + "name": "qb_category", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "qb_source_server": { + "name": "qb_source_server", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "source_server_id": { + "name": "source_server_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "import_mode": { + "name": "import_mode", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "import_target": { + "name": "import_target", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "manual_import_command_id": { + "name": "manual_import_command_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "downloads_status_idx": { + "name": "downloads_status_idx", + "columns": [ + "status" + ], + "isUnique": false + }, + "downloads_updated_at_idx": { + "name": "downloads_updated_at_idx", + "columns": [ + "updated_at" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": { + "downloads_status_check": { + "name": "downloads_status_check", + "value": "\"downloads\".\"status\" in ('downloading', 'import_queued', 'imported', 'failed')" + }, + "downloads_expected_bytes_source_check": { + "name": "downloads_expected_bytes_source_check", + "value": "\"downloads\".\"expected_bytes_source\" is null or \"downloads\".\"expected_bytes_source\" in ('content_length', 'content_range', 'release_size')" + }, + "downloads_last_operation_check": { + "name": "downloads_last_operation_check", + "value": "\"downloads\".\"last_operation\" in ('transfer', 'import')" + } + } + }, + "managed_keys": { + "name": "managed_keys", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "key_hash": { + "name": "key_hash", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "server_id": { + "name": "server_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "managed_keys_key_hash_unique": { + "name": "managed_keys_key_hash_unique", + "columns": [ + "key_hash" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "own_catalog_meta": { + "name": "own_catalog_meta", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "generation": { + "name": "generation", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "own_catalog_releases": { + "name": "own_catalog_releases", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "source_id": { + "name": "source_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title_key": { + "name": "title_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "media_type": { + "name": "media_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "tmdb_id": { + "name": "tmdb_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "imdb_id": { + "name": "imdb_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tvdb_id": { + "name": "tvdb_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "season": { + "name": "season", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "episode": { + "name": "episode", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "data": { + "name": "data", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "own_catalog_releases_source_id_idx": { + "name": "own_catalog_releases_source_id_idx", + "columns": [ + "source_id" + ], + "isUnique": false + }, + "own_catalog_releases_title_key_idx": { + "name": "own_catalog_releases_title_key_idx", + "columns": [ + "title_key" + ], + "isUnique": false + }, + "own_catalog_releases_tmdb_id_idx": { + "name": "own_catalog_releases_tmdb_id_idx", + "columns": [ + "tmdb_id" + ], + "isUnique": false + }, + "own_catalog_releases_imdb_id_idx": { + "name": "own_catalog_releases_imdb_id_idx", + "columns": [ + "imdb_id" + ], + "isUnique": false + }, + "own_catalog_releases_tvdb_id_idx": { + "name": "own_catalog_releases_tvdb_id_idx", + "columns": [ + "tvdb_id" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": { + "own_catalog_releases_media_type_check": { + "name": "own_catalog_releases_media_type_check", + "value": "\"own_catalog_releases\".\"media_type\" in ('movie', 'tv')" + } + } + }, + "own_catalog_source_reads": { + "name": "own_catalog_source_reads", + "columns": { + "source_id": { + "name": "source_id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "read_at": { + "name": "read_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "own_catalog_source_titles": { + "name": "own_catalog_source_titles", + "columns": { + "source_id": { + "name": "source_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title_key": { + "name": "title_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "data": { + "name": "data", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "own_catalog_source_titles_source_id_title_key_pk": { + "columns": [ + "source_id", + "title_key" + ], + "name": "own_catalog_source_titles_source_id_title_key_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "own_catalog_titles": { + "name": "own_catalog_titles", + "columns": { + "title_key": { + "name": "title_key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "media_type": { + "name": "media_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "tmdb_id": { + "name": "tmdb_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "imdb_id": { + "name": "imdb_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tvdb_id": { + "name": "tvdb_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "release_count": { + "name": "release_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "total_size": { + "name": "total_size", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "resolutions": { + "name": "resolutions", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "poster_url": { + "name": "poster_url", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "backdrop_url": { + "name": "backdrop_url", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "genres": { + "name": "genres", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "revision": { + "name": "revision", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "deleted": { + "name": "deleted", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + } + }, + "indexes": { + "own_catalog_titles_revision_idx": { + "name": "own_catalog_titles_revision_idx", + "columns": [ + "revision" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": { + "own_catalog_titles_media_type_check": { + "name": "own_catalog_titles_media_type_check", + "value": "\"own_catalog_titles\".\"media_type\" in ('movie', 'tv')" + } + } + }, + "peer_catalog_sync": { + "name": "peer_catalog_sync", + "columns": { + "peer_id": { + "name": "peer_id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "revision": { + "name": "revision", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "generation": { + "name": "generation", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "synced_at": { + "name": "synced_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "peer_catalog_titles": { + "name": "peer_catalog_titles", + "columns": { + "peer_id": { + "name": "peer_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title_key": { + "name": "title_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "media_type": { + "name": "media_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "tmdb_id": { + "name": "tmdb_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "imdb_id": { + "name": "imdb_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "tvdb_id": { + "name": "tvdb_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "release_count": { + "name": "release_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "total_size": { + "name": "total_size", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "resolutions": { + "name": "resolutions", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "poster_url": { + "name": "poster_url", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "backdrop_url": { + "name": "backdrop_url", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "genres": { + "name": "genres", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "peer_catalog_titles_peer_id_title_key_pk": { + "columns": [ + "peer_id", + "title_key" + ], + "name": "peer_catalog_titles_peer_id_title_key_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": { + "peer_catalog_titles_media_type_check": { + "name": "peer_catalog_titles_media_type_check", + "value": "\"peer_catalog_titles\".\"media_type\" in ('movie', 'tv')" + } + } + }, + "tmdb_metadata": { + "name": "tmdb_metadata", + "columns": { + "media_type": { + "name": "media_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "tmdb_id": { + "name": "tmdb_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "data": { + "name": "data", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "fetched_at": { + "name": "fetched_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": { + "tmdb_metadata_media_type_tmdb_id_pk": { + "columns": [ + "media_type", + "tmdb_id" + ], + "name": "tmdb_metadata_media_type_tmdb_id_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": { + "tmdb_metadata_media_type_check": { + "name": "tmdb_metadata_media_type_check", + "value": "\"tmdb_metadata\".\"media_type\" in ('movie', 'tv')" + } + } + }, + "uploads": { + "name": "uploads", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "transfer_id": { + "name": "transfer_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "idempotency_key": { + "name": "idempotency_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "peer_name": { + "name": "peer_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "peer_key_id": { + "name": "peer_key_id", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "item_id": { + "name": "item_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "filename": { + "name": "filename", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "total_bytes": { + "name": "total_bytes", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "range_start": { + "name": "range_start", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "sent_bytes": { + "name": "sent_bytes", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "duration_ms": { + "name": "duration_ms", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "started_at": { + "name": "started_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "finished_at": { + "name": "finished_at", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "uploads_transfer_id_unique": { + "name": "uploads_transfer_id_unique", + "columns": [ + "transfer_id" + ], + "isUnique": true + }, + "uploads_idempotency_key_unique": { + "name": "uploads_idempotency_key_unique", + "columns": [ + "idempotency_key" + ], + "isUnique": true + }, + "uploads_status_idx": { + "name": "uploads_status_idx", + "columns": [ + "status" + ], + "isUnique": false + }, + "uploads_started_at_idx": { + "name": "uploads_started_at_idx", + "columns": [ + "started_at" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": { + "uploads_status_check": { + "name": "uploads_status_check", + "value": "\"uploads\".\"status\" in ('uploading', 'completed', 'unconfirmed')" + } + } + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/apps/backend/drizzle/meta/_journal.json b/apps/backend/drizzle/meta/_journal.json index a891d91..89db539 100644 --- a/apps/backend/drizzle/meta/_journal.json +++ b/apps/backend/drizzle/meta/_journal.json @@ -113,6 +113,13 @@ "when": 1794270647120, "tag": "0015_upload_transfers", "breakpoints": true + }, + { + "idx": 16, + "version": "6", + "when": 1794271033416, + "tag": "0016_dashboard_history", + "breakpoints": true } ] } \ No newline at end of file diff --git a/apps/backend/src/__tests__/qbittorrent-mapper.test.ts b/apps/backend/src/__tests__/qbittorrent-mapper.test.ts index b8bd474..2429d3d 100644 --- a/apps/backend/src/__tests__/qbittorrent-mapper.test.ts +++ b/apps/backend/src/__tests__/qbittorrent-mapper.test.ts @@ -30,6 +30,7 @@ function baseRecord(overrides: Partial = {}): DownloadRecord { startedAt: '2026-06-06T00:00:00.000Z', updatedAt: '2026-06-06T00:00:00.000Z', completedAt: null, + importedAt: null, error: null, errorCode: null, errorDetails: null, diff --git a/apps/backend/src/__tests__/stats.test.ts b/apps/backend/src/__tests__/stats.test.ts new file mode 100644 index 0000000..7ff807a --- /dev/null +++ b/apps/backend/src/__tests__/stats.test.ts @@ -0,0 +1,268 @@ +import type { NewDownloadRow, NewUploadRow } from '../database/schema' +import { Database } from 'bun:sqlite' +import { expect, test } from 'bun:test' +import { drizzle } from 'drizzle-orm/bun-sqlite' +import { runMigrations } from '../database/connection' +import * as schema from '../database/schema' +import { catalogSizeHistory, downloads, ownCatalogTitles, peerCatalogTitles, uploads } from '../database/schema' +import { StatsRepository } from '../modules/stats/stats.repository' + +const NOW = Date.parse('2026-11-08T12:00:00Z') + +function setup() { + const database = new Database(':memory:') + const db = drizzle({ client: database, schema }) + runMigrations(db) + return { + database, + db, + repo: new StatsRepository(db), + } +} + +function download(overrides: Partial): NewDownloadRow { + return { + torrentFilename: 'x.torrent', + peerId: 'peer-r', + peerName: 'Ranieri', + itemId: '1', + filename: 'Movie.mkv', + destPath: '/d/Movie.mkv', + partPath: '/d/Movie.mkv.part', + releaseSize: 1, + releaseJson: '{}', + status: 'imported', + downloadedBytes: 0, + startedAt: '2026-11-08T10:00:00Z', + updatedAt: '2026-11-08T10:00:00Z', + ...overrides, + } +} + +function upload(overrides: Partial): NewUploadRow { + return { + peerName: 'Roz', + peerKeyId: 7, + itemId: '1', + filename: 'Movie.mkv', + totalBytes: 1000, + transferId: crypto.randomUUID(), + idempotencyKey: crypto.randomUUID(), + status: 'completed', + startedAt: '2026-11-07T09:00:00Z', + updatedAt: '2026-11-07T09:00:00Z', + ...overrides, + } +} + +test('aggregates daily series and rankings in SQL', () => { + const { database, db, repo } = setup() + db.insert(downloads).values([ + download({ + downloadedBytes: 1000, + startedAt: '2026-11-08T10:00:00Z', + completedAt: '2026-11-08T10:00:10Z', + importedAt: '2026-11-08T10:01:10Z', + }), + download({ + peerId: 'peer-z', + peerName: 'Roz', + status: 'failed', + errorCode: 'PEER_UNREACHABLE', + downloadedBytes: 500, + }), + // Outside the window: counts toward lifetime peers, not the daily series. + download({ downloadedBytes: 7, completedAt: '2026-10-01T00:00:00Z' }), + // Same peer id renamed later: one entry, under its latest name. + download({ peerName: 'Ranieri (new)', downloadedBytes: 3, updatedAt: '2026-11-08T11:00:00Z' }), + ]).run() + db.insert(uploads).values([ + // Confirmed by a receipt: 400 bytes in 4 s. + upload({ sentBytes: 400, durationMs: 4000, finishedAt: '2026-11-07T09:00:04Z' }), + // No receipt, so nothing is known to have arrived. + upload({ status: 'unconfirmed' }), + // Another key with the same name stays a separate peer. + upload({ + peerKeyId: 8, + filename: 'Show.mkv', + sentBytes: 50, + durationMs: 1000, + finishedAt: '2026-11-07T10:00:00Z', + }), + ]).run() + + const stats = repo.dashboard('30d', NOW) + + expect(stats.bucketMs).toBe(86_400_000) + expect(stats.buckets).toHaveLength(30) + expect(stats.buckets[0]).toBe('2026-10-10T00:00:00.000Z') + expect(stats.buckets[29]).toBe('2026-11-08T00:00:00.000Z') + expect(stats.transfers[29]).toEqual({ + at: '2026-11-08T00:00:00.000Z', + downloadedBytes: 1000, + uploadedBytes: 0, + downloadBps: 100, + uploadBps: null, + }) + expect(stats.transfers[28]).toMatchObject({ uploadedBytes: 450, uploadBps: 90 }) + expect(stats.importLatency[29]).toEqual({ at: '2026-11-08T00:00:00.000Z', avgSeconds: 60, count: 1 }) + expect(stats.importSummary).toEqual({ + minSeconds: 60, + avgSeconds: 60, + maxSeconds: 60, + count: 1, + }) + expect(stats.peers.gaveToMe).toEqual([ + { id: 'peer-r', name: 'Ranieri (new)', bytes: 1010 }, + { id: 'peer-z', name: 'Roz', bytes: 500 }, + ]) + expect(stats.peers.tookFromMe).toEqual([ + { id: '7', name: 'Roz', bytes: 400 }, + { id: '8', name: 'Roz', bytes: 50 }, + ]) + expect(stats.failures).toEqual([ + { code: 'PEER_UNREACHABLE', count: 1 }, + ]) + expect(stats.topTitles[0]).toEqual({ + filename: 'Movie.mkv', + bytes: 400, + requests: 2, + peers: 1, + }) + database.close() +}) + +test('buckets a short range by its bucket width, a boundary timestamp in the later bucket', () => { + const { database, db, repo } = setup() + db.insert(downloads).values([ + // Exactly on the 11:58 boundary. + download({ + downloadedBytes: 10, + startedAt: '2026-11-08T11:57:00.000Z', + completedAt: '2026-11-08T11:58:00.000Z', + importedAt: '2026-11-08T11:58:30.000Z', + }), + download({ + downloadedBytes: 20, + startedAt: '2026-11-08T11:57:00.000Z', + completedAt: '2026-11-08T11:59:59.999Z', + importedAt: '2026-11-08T12:01:00.999Z', + }), + download({ downloadedBytes: 40, startedAt: '2026-11-08T11:56:00.000Z', completedAt: '2026-11-08T11:57:59.999Z' }), + // Before the hour: not in any bucket. + download({ downloadedBytes: 80, completedAt: '2026-11-08T10:00:00.000Z' }), + ]).run() + + const stats = repo.dashboard('1h', NOW) + + expect(stats.bucketMs).toBe(120_000) + expect(stats.buckets).toHaveLength(30) + expect(stats.buckets[0]).toBe('2026-11-08T11:02:00.000Z') + expect(stats.buckets[29]).toBe('2026-11-08T12:00:00.000Z') + expect(stats.transfers[28]).toMatchObject({ at: '2026-11-08T11:58:00.000Z', downloadedBytes: 30 }) + expect(stats.transfers[27]).toMatchObject({ downloadedBytes: 40 }) + expect(stats.transfers.reduce((sum, t) => sum + t.downloadedBytes, 0)).toBe(70) + // Single imports of 30 s and 61 s. + expect(stats.importSummary).toEqual({ + minSeconds: 30, + avgSeconds: 46, + maxSeconds: 61, + count: 2, + }) + database.close() +}) + +test('a short range carries the previous day\'s catalog reading', () => { + const { database, repo } = setup() + repo.recordCatalogSizes(new Map(), '2026-11-06') + repo.recordCatalogSizes(new Map(), '2026-11-07') + + const stats = repo.dashboard('1h', NOW) + + expect(stats.catalog.points.map(p => p.day)).toEqual(['2026-11-07']) + expect(stats.catalog.sources).toEqual([ + { id: 'self', name: 'This instance' }, + ]) + database.close() +}) + +test('records one catalog size row per source per day, zero for an empty catalog', () => { + const { database, db, repo } = setup() + const title = { + mediaType: 'movie' as const, + title: 'A', + releaseCount: 1, + resolutions: [1080], + updatedAt: 'x', + } + db.insert(ownCatalogTitles).values([ + { + ...title, + titleKey: 'a', + totalSize: 10, + revision: 1, + }, + { + ...title, + titleKey: 'b', + totalSize: 99, + revision: 2, + deleted: true, + }, + ]).run() + db.insert(peerCatalogTitles).values([ + { + ...title, + peerId: 'p1', + titleKey: 'a', + totalSize: 5, + }, + { + ...title, + peerId: 'p1', + titleKey: 'b', + totalSize: 6, + }, + { + ...title, + peerId: 'gone', + titleKey: 'a', + totalSize: 1, + }, + ]).run() + + repo.recordCatalogSizes(new Map([ + ['p1', 'Friend'], + ]), '2026-11-08') + // Same day again: p1 renamed, and p2 is configured with nothing cached. + repo.recordCatalogSizes(new Map([ + ['p1', 'Friend renamed'], + ['p2', 'Empty'], + ]), '2026-11-08') + + expect(db.select().from(catalogSizeHistory).all()).toEqual([ + { + day: '2026-11-08', + sourceId: 'self', + sourceName: 'This instance', + titles: 1, + bytes: 10, + }, + { + day: '2026-11-08', + sourceId: 'p1', + sourceName: 'Friend renamed', + titles: 2, + bytes: 11, + }, + { + day: '2026-11-08', + sourceId: 'p2', + sourceName: 'Empty', + titles: 0, + bytes: 0, + }, + ]) + expect(repo.catalogSizesSince('2026-11-09')).toEqual([]) + database.close() +}) diff --git a/apps/backend/src/database/schema.ts b/apps/backend/src/database/schema.ts index e8b0353..241a88c 100644 --- a/apps/backend/src/database/schema.ts +++ b/apps/backend/src/database/schema.ts @@ -40,6 +40,8 @@ export const downloads = sqliteTable('downloads', { startedAt: text('started_at').notNull(), updatedAt: text('updated_at').notNull(), completedAt: text('completed_at'), + // When *arr confirmed the import; with completedAt, how long the import took. + importedAt: text('imported_at'), error: text('error'), // A DownloadErrorCode (download-error-codes.ts). No check constraint, so new // codes don't need a migration. @@ -136,6 +138,21 @@ export const uploads = sqliteTable('uploads', { export type UploadRow = typeof uploads.$inferSelect export type NewUploadRow = typeof uploads.$inferInsert +// Catalog size per day: this instance's own catalog (sourceId `self`) and each +// peer's, written after every catalog sync. One row per source per UTC day; a +// later sync the same day overwrites it, so a day holds its last reading. +export const catalogSizeHistory = sqliteTable('catalog_size_history', { + day: text('day').notNull(), + sourceId: text('source_id').notNull(), + sourceName: text('source_name').notNull(), + titles: integer('titles').notNull(), + bytes: integer('bytes').notNull(), +}, t => [ + primaryKey({ + columns: [t.day, t.sourceId], + }), +]) + export type ApiKeyRow = typeof apiKeys.$inferSelect export type NewApiKeyRow = typeof apiKeys.$inferInsert diff --git a/apps/backend/src/index.ts b/apps/backend/src/index.ts index c7657dd..ed7df1c 100644 --- a/apps/backend/src/index.ts +++ b/apps/backend/src/index.ts @@ -24,6 +24,7 @@ import { ImportWatcher } from './modules/downloads/import-watcher' import { ManagedKeysRepository } from './modules/managed-keys/managed-keys.repository' import { ManagedApiKeys } from './modules/managed-keys/managed-keys.service' import { qbCategoryForServer } from './modules/qbittorrent/qbittorrent.mapper' +import { StatsRepository } from './modules/stats/stats.repository' import { UploadsRepository } from './modules/uploads/uploads.repository' import { UploadsService } from './modules/uploads/uploads.service' @@ -54,6 +55,7 @@ const managedKeysRepository = new ManagedKeysRepository(database.db) const catalogCacheRepository = new CatalogCacheRepository(database.db) const tmdbMetadataRepository = new TmdbMetadataRepository(database.db) const uploadsService = new UploadsService(new UploadsRepository(database.db)) +const statsRepository = new StatsRepository(database.db) // A receipt that never came (a lost POST) shouldn't leave an upload running forever. setInterval(() => uploadsService.expireUnconfirmed(), 60 * 60 * 1000).unref() @@ -82,6 +84,15 @@ const catalogSync = new CatalogSyncService({ peerCache: catalogCacheRepository, metadata: tmdbMetadataRepository, schedule: () => configService?.getCatalog().refreshSchedule ?? config.catalog.refreshSchedule, + // Record today's catalog sizes for the dashboard's history chart. + onSynced: () => { + try { + statsRepository.recordCatalogSizes(new Map(connectorManager.peers.map(p => [p.id, p.name ?? p.url]))) + } + catch (err) { + logger.warn({ err }, 'Could not record catalog sizes') + } + }, }) catalogSync.start() // A schedule edited in the UI applies without a restart. @@ -169,6 +180,7 @@ function startManagementServer() { downloadsService, importWatcher, uploadsService, + statsRepository, apiKeysRepository, catalogCacheRepository, tmdbMetadataRepository, diff --git a/apps/backend/src/management-app.ts b/apps/backend/src/management-app.ts index 12411e4..fece721 100644 --- a/apps/backend/src/management-app.ts +++ b/apps/backend/src/management-app.ts @@ -7,6 +7,7 @@ import type { ConfigService } from './modules/config/config.service' import type { DownloadsRepository } from './modules/downloads/downloads.repository' import type { DownloadsService } from './modules/downloads/downloads.service' import type { ImportWatcher } from './modules/downloads/import-watcher' +import type { StatsRepository } from './modules/stats/stats.repository' import type { UploadsService } from './modules/uploads/uploads.service' import { Hono } from 'hono' import { describeRoute, openAPIRouteHandler, resolver } from 'hono-openapi' @@ -42,6 +43,7 @@ export function getManagementApp(params: { downloadsService?: DownloadsService importWatcher?: ImportWatcher uploadsService?: UploadsService + statsRepository?: StatsRepository apiKeysRepository?: ApiKeysRepository catalogCacheRepository?: CatalogCacheRepository tmdbMetadataRepository?: TmdbMetadataRepository @@ -86,7 +88,7 @@ export function getManagementApp(params: { const configController = new ConfigController(params.connectors, params.configService) app.route('/config', getConfigRouter(configController)) - const statusController = new StatusController(params.connectors, params.downloadsRepository, params.uploadsService) + const statusController = new StatusController(params.connectors, params.downloadsRepository, params.uploadsService, params.statsRepository) app.route('/', getStatusRouter(statusController)) app.route('/downloads', getDownloadsManagementRouter(new DownloadsManagementController( diff --git a/apps/backend/src/modules/catalog/catalog-sync.service.ts b/apps/backend/src/modules/catalog/catalog-sync.service.ts index 88e9509..bf9a815 100644 --- a/apps/backend/src/modules/catalog/catalog-sync.service.ts +++ b/apps/backend/src/modules/catalog/catalog-sync.service.ts @@ -50,6 +50,8 @@ export interface CatalogSyncDeps { metadata?: Pick // The current cron expression; read again on every reschedule(). schedule: () => string + // Called after each successful full sync, e.g. to record catalog sizes. Must not throw. + onSynced?: () => void } /** @@ -207,6 +209,7 @@ export class CatalogSyncService { ]) }) this.syncedAt = new Date().toISOString() + this.deps.onSynced?.() } catch (err) { logger.error({ err }, 'Catalog sync failed') diff --git a/apps/backend/src/modules/downloads/downloads.repository.ts b/apps/backend/src/modules/downloads/downloads.repository.ts index f0e9dbf..fc650b4 100644 --- a/apps/backend/src/modules/downloads/downloads.repository.ts +++ b/apps/backend/src/modules/downloads/downloads.repository.ts @@ -29,6 +29,7 @@ export interface DownloadRecord { startedAt: string updatedAt: string completedAt: string | null + importedAt: string | null error: string | null errorCode: DownloadErrorCode | null errorDetails: FileSystemErrorDetails | null @@ -86,6 +87,7 @@ function toRecord(row: DownloadRow): DownloadRecord { startedAt: row.startedAt, updatedAt: row.updatedAt, completedAt: row.completedAt, + importedAt: row.importedAt, error: row.error, errorCode: (row.errorCode as DownloadErrorCode | null) ?? null, errorDetails: row.errorDetails ?? null, @@ -260,15 +262,17 @@ export class DownloadsRepository { // *arr finished importing the file into the library — terminal success. The row // is kept (not deleted) so the downloads list doubles as a history. markImported(id: number): void { + const timestamp = nowIso() this.db.update(downloads) .set({ status: 'imported', + importedAt: timestamp, lastOperation: 'import', operationFailed: false, error: null, errorCode: null, errorDetails: null, - updatedAt: nowIso(), + updatedAt: timestamp, }) .where(eq(downloads.id, id)) .run() diff --git a/apps/backend/src/modules/downloads/downloads.service.ts b/apps/backend/src/modules/downloads/downloads.service.ts index 2a2a7df..cdba950 100644 --- a/apps/backend/src/modules/downloads/downloads.service.ts +++ b/apps/backend/src/modules/downloads/downloads.service.ts @@ -150,6 +150,7 @@ export class DownloadsService { startedAt: '', updatedAt: '', completedAt: null, + importedAt: null, error: null, errorCode: null, errorDetails: null, diff --git a/apps/backend/src/modules/stats/stats.repository.ts b/apps/backend/src/modules/stats/stats.repository.ts new file mode 100644 index 0000000..130ae61 --- /dev/null +++ b/apps/backend/src/modules/stats/stats.repository.ts @@ -0,0 +1,254 @@ +import type { AppDatabase } from '../../database/connection' +import type { DashboardStats, RankedPeer, StatsRange } from './stats' +import { count, eq, gte, sql, sum } from 'drizzle-orm' +import { catalogSizeHistory, ownCatalogTitles, peerCatalogTitles } from '../../database/schema' +import { rangeBuckets, STATS_RANGES } from './stats' + +export const OWN_CATALOG_SOURCE_ID = 'self' + +const TOP_TITLES = 5 +// SQLite's julianday() counts days; this turns a difference into milliseconds. +const MS_PER_DAY = 86_400_000 + +export type CatalogSizePoint = typeof catalogSizeHistory.$inferSelect + +/** + * The dashboard's numbers. Everything is aggregated in SQLite: the download and + * upload history is kept forever and never loaded into memory for a chart. + */ +export class StatsRepository { + constructor(private readonly db: AppDatabase) {} + + /** + * Write today's catalog size for this instance and every configured peer + * (`peerNames` maps peer id to display name). A peer with no cached titles gets + * zero, so an emptied catalog reads as zero rather than a gap. Re-running the + * same day overwrites that day's row. + */ + recordCatalogSizes(peerNames: Map, day = new Date().toISOString().slice(0, 10)): void { + const own = this.db.select({ + titles: count(), + bytes: sum(ownCatalogTitles.totalSize), + }) + .from(ownCatalogTitles) + .where(eq(ownCatalogTitles.deleted, false)) + .get() + const peers = this.db.select({ + peerId: peerCatalogTitles.peerId, + titles: count(), + bytes: sum(peerCatalogTitles.totalSize), + }) + .from(peerCatalogTitles) + .groupBy(peerCatalogTitles.peerId) + .all() + const byPeer = new Map(peers.map(p => [p.peerId, p])) + + const rows = [ + { + day, + sourceId: OWN_CATALOG_SOURCE_ID, + sourceName: 'This instance', + titles: own?.titles ?? 0, + bytes: Number(own?.bytes ?? 0), + }, + ] + for (const [peerId, name] of peerNames) { + const peer = byPeer.get(peerId) + rows.push({ + day, + sourceId: peerId, + sourceName: name, + titles: peer?.titles ?? 0, + bytes: Number(peer?.bytes ?? 0), + }) + } + + this.db.insert(catalogSizeHistory) + .values(rows) + .onConflictDoUpdate({ + target: [catalogSizeHistory.day, catalogSizeHistory.sourceId], + set: { + sourceName: sql`excluded.source_name`, + titles: sql`excluded.titles`, + bytes: sql`excluded.bytes`, + }, + }) + .run() + } + + catalogSizesSince(day: string): CatalogSizePoint[] { + return this.db.select().from(catalogSizeHistory).where(gte(catalogSizeHistory.day, day)).orderBy(catalogSizeHistory.day).all() + } + + /** Everything GET /stats returns, for the window `range` ends at `now`. */ + dashboard(range: StatsRange, now = Date.now()): DashboardStats { + const { bucketMs } = STATS_RANGES[range] + const buckets = rangeBuckets(range, now) + const since = buckets[0]! + const sinceMs = Date.parse(since) + // Index of the bucket a timestamp column falls in, in whole milliseconds so a + // timestamp on a boundary can't round into the previous bucket. Integer + // division floors here: the where clauses only pass timestamps >= since. + const msOf = (column: string) => `cast(round(unixepoch(${column}, 'subsec') * 1000) as integer)` + const bucketOf = (column: string) => sql.raw(`(${msOf(column)} - ${sinceMs}) / ${bucketMs}`) + // Import time in seconds, from whole milliseconds so averages don't drift. + const importSeconds = sql.raw(`(${msOf('imported_at')} - ${msOf('completed_at')}) / 1000.0`) + + // Download volume and speed, bucketed by when the transfer finished. + const downloadBuckets = this.db.all<{ bucket: number, bytes: number, ms: number }>(sql` + select ${bucketOf('completed_at')} as bucket, + sum(downloaded_bytes) as bytes, + sum(round((julianday(completed_at) - julianday(started_at)) * ${MS_PER_DAY})) as ms + from downloads + where completed_at >= ${since} + group by bucket`) + // Upload volume and speed come from receipts; only confirmed uploads have bytes. + const uploadBuckets = this.db.all<{ bucket: number, bytes: number, timed: number, ms: number }>(sql` + select ${bucketOf('finished_at')} as bucket, + sum(sent_bytes) as bytes, + sum(case when status = 'completed' and duration_ms > 0 then sent_bytes else 0 end) as timed, + sum(case when status = 'completed' and duration_ms > 0 then duration_ms else 0 end) as ms + from uploads + where finished_at >= ${since} + group by bucket`) + const latencyBuckets = this.db.all<{ bucket: number, seconds: number, n: number }>(sql` + select ${bucketOf('imported_at')} as bucket, + avg(${importSeconds}) as seconds, + count(*) as n + from downloads + where imported_at >= ${since} and completed_at is not null + group by bucket`) + // Over the single imports, not the bucket averages. + const [latencySummary] = this.db.all<{ min: number | null, avg: number | null, max: number | null, n: number }>(sql` + select min(s) as min, avg(s) as avg, max(s) as max, count(*) as n + from ( + select ${importSeconds} as s + from downloads + where imported_at >= ${since} and completed_at is not null + )`) + + // Rankings group by stable ids; SQLite takes the bare `name` column from the + // row holding max(...), so each peer shows its latest name. + const gaveToMe = this.db.all<{ id: string, name: string, bytes: number, at: string }>(sql` + select peer_id as id, peer_name as name, sum(downloaded_bytes) as bytes, max(updated_at) as at + from downloads + group by peer_id + having bytes > 0 + order by bytes desc`) + const tookFromMe = this.db.all<{ id: number | null, name: string, bytes: number, at: string }>(sql` + select peer_key_id as id, peer_name as name, sum(sent_bytes) as bytes, max(started_at) as at + from uploads + group by peer_key_id + having bytes > 0 + order by bytes desc`) + const failures = this.db.all<{ code: string | null, n: number }>(sql` + select error_code as code, count(*) as n + from downloads + where status = 'failed' + group by error_code + order by n desc`) + const topTitles = this.db.all<{ filename: string, bytes: number, requests: number, peers: number }>(sql` + select filename, + sum(sent_bytes) as bytes, + count(*) as requests, + count(distinct coalesce(peer_key_id, -1)) as peers + from uploads + group by filename + having bytes > 0 + order by bytes desc + limit ${TOP_TITLES}`) + + const down = new Map(downloadBuckets.map(d => [d.bucket, d])) + const up = new Map(uploadBuckets.map(d => [d.bucket, d])) + const latency = new Map(latencyBuckets.map(d => [d.bucket, d])) + + // Catalog size is daily: take every day the window touches plus the day + // before, so a window that starts before today's sync still carries + // yesterday's reading forward. + const sources = new Map() + const points = [] + for (const p of this.catalogSizesSince(new Date(sinceMs - MS_PER_DAY).toISOString().slice(0, 10))) { + // Rows come oldest first, so the newest name wins. + sources.set(p.sourceId, p.sourceName) + points.push({ + day: p.day, + sourceId: p.sourceId, + titles: p.titles, + bytes: p.bytes, + }) + } + + const rate = (bytes: number, ms: number) => ms > 0 + ? (bytes * 1000) / ms + : null + const ranked = (rows: { id: string | number | null, name: string, bytes: number }[]): RankedPeer[] => + rows.map(r => ({ + id: r.id == null + ? 'master' + : String(r.id), + name: r.name, + bytes: Number(r.bytes), + })) + + return { + range, + bucketMs, + buckets, + transfers: buckets.map((at, i) => { + const d = down.get(i) + const u = up.get(i) + return { + at, + downloadedBytes: Number(d?.bytes ?? 0), + uploadedBytes: Number(u?.bytes ?? 0), + downloadBps: d + ? rate(Number(d.bytes), Number(d.ms)) + : null, + uploadBps: u + ? rate(Number(u.timed), Number(u.ms)) + : null, + } + }), + importLatency: buckets.map((at, i) => { + const l = latency.get(i) + return { + at, + avgSeconds: l + ? Math.round(Number(l.seconds)) + : null, + count: Number(l?.n ?? 0), + } + }), + importSummary: { + minSeconds: latencySummary?.min == null + ? null + : Math.round(latencySummary.min), + avgSeconds: latencySummary?.avg == null + ? null + : Math.round(latencySummary.avg), + maxSeconds: latencySummary?.max == null + ? null + : Math.round(latencySummary.max), + count: Number(latencySummary?.n ?? 0), + }, + peers: { + gaveToMe: ranked(gaveToMe), + tookFromMe: ranked(tookFromMe), + }, + failures: failures.map(f => ({ + code: f.code ?? 'UNKNOWN_ERROR', + count: Number(f.n), + })), + topTitles: topTitles.map(t => ({ + filename: t.filename, + bytes: Number(t.bytes), + requests: Number(t.requests), + peers: Number(t.peers), + })), + catalog: { + sources: [...sources].map(([id, name]) => ({ id, name })), + points, + }, + } + } +} diff --git a/apps/backend/src/modules/stats/stats.ts b/apps/backend/src/modules/stats/stats.ts new file mode 100644 index 0000000..bd74346 --- /dev/null +++ b/apps/backend/src/modules/stats/stats.ts @@ -0,0 +1,82 @@ +// The shape of GET /stats. Built in SQL by StatsRepository.dashboard(). + +export interface BucketTransfer { + /** Start of the bucket, ISO 8601. */ + at: string + downloadedBytes: number + uploadedBytes: number + /** Average over the bucket's finished transfers: their bytes over their wall time. */ + downloadBps: number | null + uploadBps: number | null +} + +/** + * A peer in a ranking. `id` is stable (the peer id for downloads, the API key id + * for uploads, `master` for the master key); `name` is its latest display name. + */ +export interface RankedPeer { + id: string + name: string + bytes: number +} + +export interface DashboardStats { + range: StatsRange + /** Width of one bucket in milliseconds. */ + bucketMs: number + /** Bucket starts (ISO 8601), oldest first; every series has one entry per bucket. */ + buckets: string[] + transfers: BucketTransfer[] + importLatency: { at: string, avgSeconds: number | null, count: number }[] + /** Fastest, average and slowest single import in the window; null without imports. */ + importSummary: { minSeconds: number | null, avgSeconds: number | null, maxSeconds: number | null, count: number } + /** Lifetime bytes per peer, largest first. */ + peers: { + gaveToMe: RankedPeer[] + tookFromMe: RankedPeer[] + } + /** Downloads that are failed right now, by error code, most common first. */ + failures: { code: string, count: number }[] + /** Files peers pulled most from this instance, by confirmed bytes sent. */ + topTitles: { filename: string, bytes: number, requests: number, peers: number }[] + /** Catalog size is recorded once per UTC day, so these points stay daily. */ + catalog: { + sources: { id: string, name: string }[] + points: { day: string, sourceId: string, titles: number, bytes: number }[] + } +} + +const SECOND_MS = 1000 +const MINUTE_MS = 60 * SECOND_MS +const HOUR_MS = 60 * MINUTE_MS +const DAY_MS = 24 * HOUR_MS + +/** The history windows the dashboard offers, and the bucket width for each. */ +export const STATS_RANGES = { + '5m': { spanMs: 5 * MINUTE_MS, bucketMs: 10 * SECOND_MS }, + '30m': { spanMs: 30 * MINUTE_MS, bucketMs: MINUTE_MS }, + '1h': { spanMs: HOUR_MS, bucketMs: 2 * MINUTE_MS }, + '24h': { spanMs: DAY_MS, bucketMs: HOUR_MS }, + '7d': { spanMs: 7 * DAY_MS, bucketMs: 6 * HOUR_MS }, + '30d': { spanMs: 30 * DAY_MS, bucketMs: DAY_MS }, +} as const + +export type StatsRange = keyof typeof STATS_RANGES + +export const DEFAULT_STATS_RANGE: StatsRange = '30d' + +export function isStatsRange(value: unknown): value is StatsRange { + return typeof value === 'string' && Object.hasOwn(STATS_RANGES, value) +} + +/** + * Bucket starts for `range`, oldest first, the last one holding `now`. Buckets + * are aligned to multiples of their width since the epoch, so daily buckets + * start at UTC midnight. + */ +export function rangeBuckets(range: StatsRange, now = Date.now()): string[] { + const { spanMs, bucketMs } = STATS_RANGES[range] + const count = spanMs / bucketMs + const last = Math.floor(now / bucketMs) * bucketMs + return Array.from({ length: count }, (_, i) => new Date(last - (count - 1 - i) * bucketMs).toISOString()) +} diff --git a/apps/backend/src/modules/status/status.controller.ts b/apps/backend/src/modules/status/status.controller.ts index 7dcad61..06ad3be 100644 --- a/apps/backend/src/modules/status/status.controller.ts +++ b/apps/backend/src/modules/status/status.controller.ts @@ -3,9 +3,12 @@ import type { ArrServerConnector } from '../../lib/servers/arr/base' import type { ServerConnector } from '../../lib/servers/base' import type { PeerConnector } from '../../lib/servers/peer' import type { DownloadRecord, DownloadsRepository } from '../downloads/downloads.repository' +import type { DashboardStats, StatsRange } from '../stats/stats' +import type { StatsRepository } from '../stats/stats.repository' import type { UploadRecord } from '../uploads/uploads.repository' import type { UploadsService } from '../uploads/uploads.service' import { DOWNLOAD_STATUSES, UPLOAD_STATUSES } from '../../database/schema' +import { rangeBuckets, STATS_RANGES } from '../stats/stats' // The management surface can expose richer detail than the public API: every // connector here carries its URL-derived `id` (the management UI keys edit/delete @@ -110,6 +113,7 @@ export class StatusController { private readonly connectors: { servers: ArrServerConnector[], peers: PeerConnector[], subscribe?: (subscriber: () => void) => () => void }, private readonly downloads?: DownloadsRepository, private readonly uploads?: UploadsService, + private readonly stats?: StatsRepository, ) {} private enrichUpload = (u: UploadRecord) => enrichUpload(u) @@ -205,6 +209,28 @@ export class StatusController { } } + /** Chart data for the dashboard: the history window `range` plus lifetime rankings. */ + getStats(range: StatsRange): DashboardStats { + // Aggregated in SQLite: the history is never loaded for a chart. + return this.stats?.dashboard(range) ?? { + range, + bucketMs: STATS_RANGES[range].bucketMs, + buckets: rangeBuckets(range), + importSummary: { + minSeconds: null, + avgSeconds: null, + maxSeconds: null, + count: 0, + }, + transfers: [], + importLatency: [], + peers: { gaveToMe: [], tookFromMe: [] }, + failures: [], + topTitles: [], + catalog: { sources: [], points: [] }, + } + } + listUploads() { const records = this.uploads?.list() ?? [] return { diff --git a/apps/backend/src/modules/status/status.router.ts b/apps/backend/src/modules/status/status.router.ts index 62002b6..b5a898e 100644 --- a/apps/backend/src/modules/status/status.router.ts +++ b/apps/backend/src/modules/status/status.router.ts @@ -2,6 +2,7 @@ import type { StatusController } from './status.controller' import { Hono } from 'hono' import { describeRoute } from 'hono-openapi' import { streamSnapshots } from '../../lib/sse' +import { DEFAULT_STATS_RANGE, isStatsRange, STATS_RANGES } from '../stats/stats' export function getStatusRouter(controller: StatusController) { const app = new Hono() @@ -91,5 +92,37 @@ export function getStatusRouter(controller: StatusController) { }, }), c => streamSnapshots(c, () => controller.listUploads(), subscriber => controller.subscribeUploads(subscriber))) + app.get('/stats', describeRoute({ + tags: ['Status'], + summary: 'Dashboard statistics', + description: 'Transfer volume, average speed and import latency over a history window, plus lifetime bytes per peer, current failures by error code, the five files peers pulled most, and the daily catalog size history. `range` picks the window and its bucket width: `5m` (10 s buckets), `30m` (1 min), `1h` (2 min), `24h` (1 h), `7d` (6 h) or `30d` (1 day, the default).', + parameters: [ + { + name: 'range', + in: 'query', + required: false, + schema: { + type: 'string', + enum: Object.keys(STATS_RANGES), + default: DEFAULT_STATS_RANGE, + }, + }, + ], + security: [ + { 'X-Management-Key': [] }, + ], + responses: { + 200: { + description: 'Dashboard statistics', + content: { 'application/json': {} }, + }, + }, + }), (c) => { + const requested = c.req.query('range') + return c.json(controller.getStats(isStatsRange(requested) + ? requested + : DEFAULT_STATS_RANGE)) + }) + return app } diff --git a/apps/ui/app/assets/css/main.css b/apps/ui/app/assets/css/main.css index 7286ec8..1873e90 100644 --- a/apps/ui/app/assets/css/main.css +++ b/apps/ui/app/assets/css/main.css @@ -7,3 +7,28 @@ with a system-mono fallback so an offline build still renders cleanly. */ --font-mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', 'Menlo', monospace; } + +/* Dashboard chart series, in fixed order: a series keeps its slot, never its rank. + The order is CVD-checked for adjacent pairs; dark mode has its own steps. + Downloads are slot 1 and uploads slot 2 everywhere. */ +:root { + --viz-1: #2a78d6; + --viz-2: #eb6834; + --viz-3: #1baf7a; + --viz-4: #eda100; + --viz-5: #e87ba4; + --viz-6: #008300; + --viz-7: #6250d6; + --viz-8: #e34948; +} + +.dark { + --viz-1: #3987e5; + --viz-2: #d95926; + --viz-3: #199e70; + --viz-4: #c98500; + --viz-5: #d55181; + --viz-6: #008300; + --viz-7: #9085e9; + --viz-8: #e66767; +} diff --git a/apps/ui/app/components/DashSection.vue b/apps/ui/app/components/DashSection.vue new file mode 100644 index 0000000..c995d90 --- /dev/null +++ b/apps/ui/app/components/DashSection.vue @@ -0,0 +1,29 @@ + + + diff --git a/apps/ui/app/components/LineChart.vue b/apps/ui/app/components/LineChart.vue new file mode 100644 index 0000000..68f870d --- /dev/null +++ b/apps/ui/app/components/LineChart.vue @@ -0,0 +1,189 @@ + + + diff --git a/apps/ui/app/components/MirrorChart.vue b/apps/ui/app/components/MirrorChart.vue new file mode 100644 index 0000000..77efd93 --- /dev/null +++ b/apps/ui/app/components/MirrorChart.vue @@ -0,0 +1,189 @@ + + + diff --git a/apps/ui/app/components/RankBars.vue b/apps/ui/app/components/RankBars.vue new file mode 100644 index 0000000..2b09c52 --- /dev/null +++ b/apps/ui/app/components/RankBars.vue @@ -0,0 +1,31 @@ + + + diff --git a/apps/ui/app/components/StackColumn.vue b/apps/ui/app/components/StackColumn.vue new file mode 100644 index 0000000..7622ddf --- /dev/null +++ b/apps/ui/app/components/StackColumn.vue @@ -0,0 +1,152 @@ + + + diff --git a/apps/ui/app/pages/index.vue b/apps/ui/app/pages/index.vue index a32e589..b917828 100644 --- a/apps/ui/app/pages/index.vue +++ b/apps/ui/app/pages/index.vue @@ -1,7 +1,9 @@