Fixed unreachable streams reporting stale online status indefinitely.

This commit is contained in:
2026-01-07 11:21:29 -05:00
parent 35086cb751
commit 625178a7ca
6 changed files with 216 additions and 53 deletions

View File

@@ -5,9 +5,18 @@
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
from dataclasses import dataclass
from enum import Enum
from typing import Optional, List
class StreamStatus(Enum):
"""Represents the status of a stream."""
ONLINE = "online"
OFFLINE = "offline"
UNKNOWN = "unknown"
@dataclass
class StreamState:
"""Represents the state of an Owncast stream."""
@@ -20,9 +29,34 @@ class StreamState:
failure_counter: int = 0
@property
def online(self) -> bool:
"""Returns True if the stream is currently online."""
return self.last_connect_time is not None
def status(self) -> StreamStatus:
"""Returns the stream status considering failure_counter."""
from .utils import UNKNOWN_STATUS_THRESHOLD
if self.failure_counter > UNKNOWN_STATUS_THRESHOLD:
return StreamStatus.UNKNOWN
elif self.last_connect_time is not None:
return StreamStatus.ONLINE
else:
return StreamStatus.OFFLINE
@classmethod
def from_api_response(cls, response: dict, domain: str) -> "StreamState":
"""
Creates a StreamState from an API response.
:param response: API response as a dictionary (camelCase keys)
:param domain: The stream domain
:return: StreamState instance
"""
from .utils import truncate, MAX_STREAM_TITLE_LENGTH
return cls(
domain=domain,
title=truncate(response.get("streamTitle", ""), MAX_STREAM_TITLE_LENGTH),
last_connect_time=response.get("lastConnectTime"),
last_disconnect_time=response.get("lastDisconnectTime"),
)
@classmethod
def from_db_row(cls, row: dict) -> "StreamState":
@@ -62,4 +96,13 @@ class StreamConfig:
:param response: API response as a dictionary
:return: StreamConfig instance
"""
return cls(name=response.get("name", ""), tags=response.get("tags", []))
from .utils import truncate, MAX_INSTANCE_TITLE_LENGTH, MAX_TAG_LENGTH
# Truncate instance name to max length
name = truncate(response.get("name", ""), MAX_INSTANCE_TITLE_LENGTH)
# Truncate each tag to max length
raw_tags = response.get("tags", [])
tags = [truncate(tag, MAX_TAG_LENGTH) for tag in raw_tags]
return cls(name=name, tags=tags)