Mejoras de estabilidad de stream IPTV y nuevo registro de fallos

- restream.py: eliminar -reconnect_streamed de ffmpeg para evitar rebobinados
  al reconectar (el proveedor re-enviaba desde keyframe anterior causando PTS
  backward). El loop de Python gestiona las reconexiones con conexión fresca.
- restream.py: eliminar _drain_queue del loop de reconexión para que el buffer
  de la cola cubra el tiempo de reconexión sin pantalla negra en el player.
- restream.py: añadir buffer_server_bytes/secs/capacity a stats() y método
  _log_stream_event() para persistir eventos de caída y recuperación en BD.
- models/stream_event.py: nuevo modelo StreamEvent para registro de fallos.
- database.py: registrar StreamEvent en init_db.
- api/admin/logs.py: nuevos endpoints GET/DELETE /logs/events.
- Dashboard.tsx: mostrar reserva de buffer del servidor (capacidad + estado).
- Logs.tsx: añadir pestaña "Fallos de stream" con tabla de eventos persistidos.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
KiraStream
2026-05-19 14:43:31 +00:00
commit b969b7e5af
65 changed files with 13279 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
from .provider import ProviderAccount
from .channel import Category, Channel, ChannelProviderMap, ContentType
from .user import User, UserCatalogEntry
from .epg import EpgSource
__all__ = [
"ProviderAccount",
"Category",
"Channel",
"ChannelProviderMap",
"ContentType",
"User",
"UserCatalogEntry",
"EpgSource",
]
+61
View File
@@ -0,0 +1,61 @@
import enum
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Enum, UniqueConstraint, func
from sqlalchemy.orm import relationship
from ..database import Base
class ContentType(str, enum.Enum):
live = "live"
movie = "movie"
series = "series"
class Category(Base):
__tablename__ = "categories"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(300), nullable=False)
type = Column(Enum(ContentType), nullable=False)
provider_category_id = Column(String(100), nullable=True)
provider_account_id = Column(Integer, ForeignKey("provider_accounts.id", ondelete="CASCADE"), nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
channels = relationship("Channel", back_populates="category")
catalog_entries = relationship("UserCatalogEntry", back_populates="category", cascade="all, delete-orphan")
__table_args__ = (
UniqueConstraint("provider_category_id", "provider_account_id", "type", name="uq_category_provider"),
)
class Channel(Base):
__tablename__ = "channels"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(500), nullable=False)
tvg_id = Column(String(200), nullable=True, index=True)
tvg_logo = Column(String(1000), nullable=True)
stream_id_at_provider = Column(String(100), nullable=False)
category_id = Column(Integer, ForeignKey("categories.id", ondelete="SET NULL"), nullable=True)
type = Column(Enum(ContentType), nullable=False, default=ContentType.live)
is_active = Column(Boolean, default=True, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
category = relationship("Category", back_populates="channels")
provider_maps = relationship("ChannelProviderMap", back_populates="channel", cascade="all, delete-orphan")
class ChannelProviderMap(Base):
__tablename__ = "channel_provider_map"
id = Column(Integer, primary_key=True, index=True)
channel_id = Column(Integer, ForeignKey("channels.id", ondelete="CASCADE"), nullable=False)
provider_account_id = Column(Integer, ForeignKey("provider_accounts.id", ondelete="CASCADE"), nullable=False)
stream_url = Column(String(2000), nullable=False)
channel = relationship("Channel", back_populates="provider_maps")
provider_account = relationship("ProviderAccount", back_populates="channel_maps")
__table_args__ = (
UniqueConstraint("channel_id", "provider_account_id", name="uq_channel_provider"),
)
+29
View File
@@ -0,0 +1,29 @@
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Enum, ForeignKey, func
from sqlalchemy.orm import relationship
from ..database import Base
from .channel import ContentType
class CustomCategory(Base):
__tablename__ = "custom_categories"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(200), nullable=False)
type = Column(Enum(ContentType), nullable=False)
position = Column(Integer, default=0, nullable=False)
is_visible = Column(Boolean, default=True, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
items = relationship("CustomCategoryItem", back_populates="category", cascade="all, delete-orphan")
class CustomCategoryItem(Base):
__tablename__ = "custom_category_items"
id = Column(Integer, primary_key=True, index=True)
custom_category_id = Column(Integer, ForeignKey("custom_categories.id"), nullable=False)
channel_id = Column(Integer, ForeignKey("channels.id"), nullable=False)
position = Column(Integer, default=0, nullable=False)
category = relationship("CustomCategory", back_populates="items")
channel = relationship("Channel")
+13
View File
@@ -0,0 +1,13 @@
from sqlalchemy import Column, Integer, String, Boolean, DateTime, func
from ..database import Base
class EpgSource(Base):
__tablename__ = "epg_sources"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(200), nullable=False)
url = Column(String(2000), nullable=False)
last_fetched_at = Column(DateTime(timezone=True), nullable=True)
is_active = Column(Boolean, default=True, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
+47
View File
@@ -0,0 +1,47 @@
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, func
from sqlalchemy.orm import relationship
from ..database import Base
class JellyfinConfig(Base):
__tablename__ = "jellyfin_configs"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(200), nullable=False)
url = Column(String(500), nullable=False)
api_key = Column(String(300), nullable=False)
is_active = Column(Boolean, default=True, nullable=False)
movies_library_id = Column(String(100), nullable=True)
series_library_id = Column(String(100), nullable=True)
movies_category_name = Column(String(200), default="Películas KiraTV", nullable=False)
series_category_name = Column(String(200), default="Series KiraTV", nullable=False)
last_sync_at = Column(DateTime(timezone=True), nullable=True)
# Dummy ProviderAccount auto-created to satisfy ChannelProviderMap FK
provider_account_id = Column(
Integer,
ForeignKey("provider_accounts.id", ondelete="SET NULL"),
nullable=True,
)
created_at = Column(DateTime(timezone=True), server_default=func.now())
items = relationship("JellyfinItem", back_populates="config", cascade="all, delete-orphan")
class JellyfinItem(Base):
__tablename__ = "jellyfin_items"
id = Column(Integer, primary_key=True, index=True)
jellyfin_config_id = Column(
Integer, ForeignKey("jellyfin_configs.id", ondelete="CASCADE"), nullable=False
)
channel_id = Column(
Integer, ForeignKey("channels.id", ondelete="CASCADE"), nullable=True
)
jellyfin_item_id = Column(String(100), nullable=False, index=True) # Jellyfin hex ID
item_type = Column(String(20), nullable=False) # "movie" | "series"
plot = Column(Text, nullable=True)
year = Column(Integer, nullable=True)
genres = Column(String(500), nullable=True)
rating = Column(String(20), nullable=True)
config = relationship("JellyfinConfig", back_populates="items")
+19
View File
@@ -0,0 +1,19 @@
from sqlalchemy import Column, Integer, BigInteger, String, DateTime, func
from ..database import Base
class ConnectionLog(Base):
__tablename__ = "connection_logs"
id = Column(BigInteger, primary_key=True, index=True)
user_id = Column(Integer, nullable=False, index=True)
username = Column(String(200), nullable=False, index=True)
channel_id = Column(Integer, nullable=True)
channel_name = Column(String(500), nullable=True)
content_type = Column(String(20), nullable=True) # live, movie, series
stream_id = Column(String(100), nullable=True)
started_at = Column(DateTime(timezone=True), server_default=func.now(), index=True)
ended_at = Column(DateTime(timezone=True), nullable=True)
duration_seconds = Column(Integer, nullable=True)
bytes_transferred = Column(BigInteger, default=0, nullable=False)
client_ip = Column(String(100), nullable=True)
+16
View File
@@ -0,0 +1,16 @@
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, UniqueConstraint, func
from ..database import Base
class NotificationDismissal(Base):
__tablename__ = "notification_dismissals"
id = Column(Integer, primary_key=True, index=True)
provider_id = Column(Integer, ForeignKey("provider_accounts.id", ondelete="CASCADE"), nullable=False)
category_id_str = Column(String(100), nullable=False)
category_type = Column(String(20), nullable=False)
dismissed_at = Column(DateTime(timezone=True), server_default=func.now())
__table_args__ = (
UniqueConstraint("provider_id", "category_id_str", "category_type", name="uq_dismissal"),
)
+41
View File
@@ -0,0 +1,41 @@
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, func
from sqlalchemy.orm import relationship
from ..database import Base
class ProviderAccount(Base):
__tablename__ = "provider_accounts"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(100), nullable=False)
base_url = Column(String(500), nullable=False)
username = Column(String(200), nullable=False)
password = Column(String(200), nullable=False)
max_connections = Column(Integer, default=1, nullable=False)
is_active = Column(Boolean, default=True, nullable=False)
last_sync_at = Column(DateTime(timezone=True), nullable=True)
expiry_date = Column(DateTime(timezone=True), nullable=True)
# 0 = disabled; 6/12/24 = sync every N hours automatically
auto_sync_hours = Column(Integer, default=0, nullable=False)
# JSON: {"live": ["cat_id", ...], "movie": [...], "series": [...]}
last_sync_categories = Column(Text, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
channel_maps = relationship("ChannelProviderMap", back_populates="provider_account", cascade="all, delete-orphan")
extra_urls = relationship("ProviderUrl", back_populates="provider_account", cascade="all, delete-orphan", order_by="ProviderUrl.priority")
class ProviderUrl(Base):
__tablename__ = "provider_urls"
id = Column(Integer, primary_key=True, index=True)
provider_account_id = Column(Integer, ForeignKey("provider_accounts.id", ondelete="CASCADE"), nullable=False, index=True)
url = Column(String(500), nullable=False)
priority = Column(Integer, default=0, nullable=False) # 0=primary, higher=lower priority
is_active = Column(Boolean, default=True, nullable=False)
status = Column(String(20), default="unknown", nullable=False) # unknown|ok|error|timeout
response_ms = Column(Integer, nullable=True)
last_checked_at = Column(DateTime(timezone=True), nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now())
provider_account = relationship("ProviderAccount", back_populates="extra_urls")
+16
View File
@@ -0,0 +1,16 @@
from sqlalchemy import Column, BigInteger, Integer, String, Text, DateTime, func
from ..database import Base
class StreamEvent(Base):
__tablename__ = "stream_events"
id = Column(BigInteger, primary_key=True, index=True)
channel_id = Column(Integer, nullable=True, index=True)
channel_name = Column(String(500), nullable=True)
provider_name = Column(String(200), nullable=True)
event_type = Column(String(50), nullable=False, index=True) # caida | recuperado
domain = Column(String(300), nullable=True)
error_message = Column(Text, nullable=True)
attempt_number = Column(Integer, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now(), index=True)
+66
View File
@@ -0,0 +1,66 @@
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, UniqueConstraint, func
from sqlalchemy.orm import relationship
import bcrypt as _bcrypt
from ..database import Base
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
username = Column(String(100), unique=True, nullable=False, index=True)
password_hash = Column(String(200), nullable=False)
max_connections = Column(Integer, default=1, nullable=False)
expiry_date = Column(DateTime(timezone=True), nullable=True)
is_active = Column(Boolean, default=True, nullable=False)
is_priority = Column(Boolean, default=False, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
catalog_entries = relationship("UserCatalogEntry", back_populates="user", cascade="all, delete-orphan")
provider_preferences = relationship("UserProviderPreference", back_populates="user", cascade="all, delete-orphan", order_by="UserProviderPreference.priority")
def set_password(self, password: str) -> None:
hashed = _bcrypt.hashpw(password.encode(), _bcrypt.gensalt())
self.password_hash = hashed.decode()
def verify_password(self, password: str) -> bool:
return _bcrypt.checkpw(password.encode(), self.password_hash.encode())
class UserCatalogEntry(Base):
__tablename__ = "user_catalog_entries"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
category_id = Column(Integer, ForeignKey("categories.id", ondelete="CASCADE"), nullable=False)
user = relationship("User", back_populates="catalog_entries")
category = relationship("Category", back_populates="catalog_entries")
class UserCustomCatalogEntry(Base):
__tablename__ = "user_custom_catalog_entries"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
custom_category_id = Column(Integer, ForeignKey("custom_categories.id", ondelete="CASCADE"), nullable=False)
__table_args__ = (
UniqueConstraint("user_id", "custom_category_id", name="uq_user_custom_cat"),
)
class UserProviderPreference(Base):
__tablename__ = "user_provider_preferences"
id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
provider_account_id = Column(Integer, ForeignKey("provider_accounts.id", ondelete="CASCADE"), nullable=False)
priority = Column(Integer, default=0, nullable=False)
user = relationship("User", back_populates="provider_preferences")
provider_account = relationship("ProviderAccount")
__table_args__ = (
UniqueConstraint("user_id", "provider_account_id", name="uq_user_provider_pref"),
)