U
    	hS.                     @   s
  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZ ee
G dd dejZeeG d	d
 d
ejZeeG dd dejZG dd dejZeeG dd dejZeeG dd dejZdej_dej_dej_dS )z
Django Admin Configuration for Streams Application

This module configures the Django admin interface for stream management,
providing comprehensive administrative controls for channels, sessions,
configurations, and segments.
    )admin)format_html)reverse)timezone)ChannelVideoConfigurationAudioConfigurationStreamSession
HLSSegmentc                       s   e Zd ZdZdddddddgZdddgZddd	d
gZdddifdddifdddifdddifddddffZddiZddgZ	dgZ
dd Zde_de_dd Zd e_ fd!d"Z  ZS )#ChannelAdminz
    Admin interface for Channel model.
    
    Provides comprehensive management of streaming channels including
    configuration, status monitoring, and session management.
    nameslug	is_active
created_byget_active_session_statussegments_count
created_atdescriptionhls_urlzBasic Informationfields)r   r   r   r   zStream Configuration)r   output_directoryzHLS Settings)segment_durationmax_segmentszRetry Configuration)retry_attemptsretry_intervalZMetadata)r   collapser   classesr   
updated_atc                 C   s<   |  }|r4dddd|jd}td||j S tdS )z6Get the status of the active session for this channel.greenorangered)active
processingfailedgrayz"<span style="color: {};">{}</span>z3<span style="color: gray;">No Active Session</span>)get_active_sessiongetstatusr   title)selfobjZactive_sessioncolor r/   6/var/www/html/JingleDetector/src/apps/streams/admin.pyr   I   s     z&ChannelAdmin.get_active_session_statuszActive SessionZsessions__statusc                 C   s   t dd |j D }|S )z2Get the total number of segments for this channel.c                 s   s   | ]}|j  V  qd S )N)segmentscount).0sessionr/   r/   r0   	<genexpr>_   s   z.ChannelAdmin.segments_count.<locals>.<genexpr>)sumsessionsall)r,   r-   Ztotal_segmentsr/   r/   r0   r   ]   s    zChannelAdmin.segments_countzTotal Segmentsc                    s   t  |dS )z(Optimize queryset with prefetch_related.r   superget_querysetselect_relatedr,   request	__class__r/   r0   r;   g   s    zChannelAdmin.get_queryset)__name__
__module____qualname____doc__list_displaylist_filtersearch_fields	fieldsetsprepopulated_fieldsreadonly_fieldsorderingr   short_descriptionadmin_order_fieldr   r;   __classcell__r/   r/   r?   r0   r      s`   	              r   c                   @   sh   e Zd ZdZdddddddgZddddgZddgZd	d
difdd
difdd
difdd
diffZdgZdS )VideoConfigurationAdminz~
    Admin interface for VideoConfiguration model.
    
    Provides management of video encoding presets and parameters.
    r   
resolution
frame_ratecodecmin_bitratemax_bitratepresetConfiguration Identityr   r   zVideo Parameters)rP   aspect_ratiorQ   zBitrate Settings)rS   rT   zCodec Configuration)rR   rU   profilelevelN	rA   rB   rC   rD   rE   rF   rG   rH   rK   r/   r/   r/   r0   rO   l   sB               rO   c                   @   sZ   e Zd ZdZddddddgZddddgZdgZdd	d
ifdd	difdd	diffZdgZdS )AudioConfigurationAdminz~
    Admin interface for AudioConfiguration model.
    
    Provides management of audio encoding presets and parameters.
    r   rR   bitratesample_ratechannels	normalizerV   r   r   zAudio Quality)rR   r\   r]   r^   zProcessing Options)r_   NrZ   r/   r/   r/   r0   r[      s8             r[   c                   @   s2   e Zd ZdZeZdZddddddgZdd
dZd	S )HLSSegmentInlinez
    Inline admin for HLS segments within stream sessions.
    
    Provides a tabular view of segments belonging to a session.
    r   filenamesequence_numberduration	file_sizeprocessed_atis_availableNc                 C   s   dS )z&Disable adding segments through admin.Fr/   )r,   r>   r-   r/   r/   r0   has_add_permission   s    z#HLSSegmentInline.has_add_permission)N)	rA   rB   rC   rD   r
   modelextrarJ   rg   r/   r/   r/   r0   r`      s       r`   c                       s   e Zd ZdZdddddddgZdddd	d
gZdddgZdddifdddifdddifdddifddddffZddddddgZe	gZ
dgZdd  Zd!e_de_d"d# Zd$e_de_ fd%d&Z  ZS )'StreamSessionAdminz
    Admin interface for StreamSession model.
    
    Provides comprehensive monitoring and management of stream sessions
    including statistics, error tracking, and segment management.
    get_session_infochannelr*   
started_atduration_displaysegments_processederrors_countvideo_configaudio_configZchannel__nameZchannel__slug
process_idzSession Informationr   )rl   r*   rs   ZConfiguration)rq   rr   ZTiming)rm   ended_atZ
Statistics)ro   rp   
last_errorzAdditional Data)
statisticsr   r   r   r    rt   z-started_atc                 C   s<   dddddd |jd}tdt|jd	d
 ||j S )z"Get formatted session information.r!   r"   bluer#   r'   )r$   r%   	completedr&   	cancelledblack:<strong>{}</strong><br/><span style="color: {};">{}</span>N   )r)   r*   r   stridr+   )r,   r-   Zstatus_colorr/   r/   r0   rk   	  s     z#StreamSessionAdmin.get_session_infoSessionc                 C   sx   |  }|rtt| }|d }|d d }|d }|dkrR| d| d| dS |dkrj| d| dS | dS dS )z.Display session duration in a readable format.i  <   r   zh zm s-)rc   inttotal_seconds)r,   r-   rc   r   hoursminutessecondsr/   r/   r0   rn     s    
z#StreamSessionAdmin.duration_displayDurationc                    s   t  |dddS )&Optimize queryset with select_related.rl   rq   rr   r9   r=   r?   r/   r0   r;   2  s
      zStreamSessionAdmin.get_queryset)rA   rB   rC   rD   rE   rF   rG   rH   rJ   r`   inlinesrK   rk   rL   rM   rn   r;   rN   r/   r/   r?   r0   rj      sp   	                   rj   c                       s   e Zd ZdZdddddddgZddd	gZd
ddgZdddifdddifdddiffZdddgZdgZ	dd Z
de
_d
e
_dd Zde_de_ fddZ  ZS )HLSSegmentAdminz
    Admin interface for HLSSegment model.
    
    Provides detailed management of individual HLS segments
    including file information and availability status.
    get_segment_infor4   rb   rc   file_size_displayrf   re   session__channelra   Zsession__channel__namezSegment Informationr   )r4   ra   rb   zFile Details)	file_pathrc   rd   rf   
Processing)re   r   r    z-processed_atc                 C   s,   |j r
dnd}|j rdnd}td|j||S )z"Get formatted segment information.r!   r#   Z	AvailableZMissingr{   )rf   r   ra   )r,   r-   Zavailability_colorZavailability_textr/   r/   r0   r   g  s    z HLSSegmentAdmin.get_segment_infoZSegmentc                 C   sJ   |j rF|j }dD ](}|dk r0|dd|   S |d }q|ddS dS )z-Display file size in a human-readable format.)BKBMBGBg      @z.1f z TBr   )rd   )r,   r-   sizeunitr/   r/   r0   r   w  s    
z!HLSSegmentAdmin.file_size_displayz	File Sizerd   c                    s   t  |dS )r   r   r9   r=   r?   r/   r0   r;     s    zHLSSegmentAdmin.get_queryset)rA   rB   rC   rD   rE   rF   rG   rH   rJ   rK   r   rL   rM   r   r;   rN   r/   r/   r?   r0   r   9  sT   	              r   zStream Processor AdministrationzStream ProcessorzStream Management DashboardN)rD   Zdjango.contribr   django.utils.htmlr   django.urlsr   django.utilsr   Zapps.streams.modelsr   r   r   r	   r
   register
ModelAdminr   rO   r[   TabularInliner`   rj   r   sitesite_header
site_titleindex_titler/   r/   r/   r0   <module>   s&   X)&hQ