
    ,i6                         d dl mZmZmZ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 d dlmZ d dlmZ ded	z  d
efdZ G d de      Zy	)    )DictAnyListDefaultDict)defaultdict)APIView)Response)IsAuthenticatedIsAdminUserAllowAnystatus)current_appdNreturnc                     | xs i S )N )r   s    2/var/www/extractly/houslyspace/server_managment.py_safer      s    7N    c                       e Zd ZdZeZd Zy)CeleryTopologyViewz
    Returns current Celery topology:
    - workers: their queues, activity, basic stats
    - queues: reverse index queue -> workers
    - summary: counts
    c                 	   	 t         j                  j                  d      }	 t        |j                               }	 t        |j                               }	 t        |j                               }	 t        |j                               }		 t        |j                               }
	 t         j                  j                  d      }t               }|xs g D ]!  }|j                  |j!                                # i }t#        t$              }t               j'                  |j!                         |j!                         |j!                         |	j!                         |
j!                               }t)        |      D ]  }g }|j+                  |g       D ]V  }t-        |t.              r|j+                  d      n
t1        |      }|s2|j3                  |       ||   j3                  |       X |
j+                  |i       xs i }t-        |t.              r|j+                  di       ni }|j+                  d	      xs |j+                  d
      }||v t)        t        |            t5        |j+                  |g       xs g       t5        |j+                  |g       xs g       t5        |	j+                  |g       xs g       |j+                  d      |j+                  d      |j+                  d      |j+                  d      xs |j+                  d      ||j+                  d      xs |j+                  d      t-        |j+                  d      t.              r!|j+                  di       j+                  d      nd d||<    ||j7                         D ci c]  \  }}|t)        |       c}}t5        |      t9        d |j;                         D              t5        |      t9        d |j;                         D              t9        d |j;                         D              dd}t	        |t
        j<                        S # t        $ r*}t	        dd| it
        j                        cY d }~S d }~ww xY w# t        $ r i }Y >w xY w# t        $ r i }Y 6w xY w# t        $ r i }Y .w xY w# t        $ r i }	Y &w xY w# t        $ r i }
Y w xY w# t        $ r g }Y w xY wc c}}w )N   )timeoutdetailzinspect init error: r      namepoolzmax-concurrencyconcurrencypidhostnameplatformprefetch_countprefetch_multipliercelery_versionversionbroker	transport)alivequeuesactive_countreserved_countregistered_countr!   r"   r#   r%   r    r'   r(   c              3   ,   K   | ]  }|d    s	d  yw)r*      Nr   .0ws     r   	<genexpr>z)CeleryTopologyView.get.<locals>.<genexpr>m   s     $O0@1AgJQ0@s   
c              3   &   K   | ]	  }|d      yw)r,   Nr   r1   s     r   r4   z)CeleryTopologyView.get.<locals>.<genexpr>o   s     )VEU!N*;EU   c              3   &   K   | ]	  }|d      yw)r-   Nr   r1   s     r   r4   z)CeleryTopologyView.get.<locals>.<genexpr>p   s     +ZIYAA.>,?IYr6   )workers_totalalive_workersqueues_totaltasks_active_totaltasks_reserved_total)workersr+   summary)
celery_appcontrolinspect	Exceptionr	   r   HTTP_503_SERVICE_UNAVAILABLEr   active_queuesactivereserved
registeredstatspingsetupdatekeysr   listunionsortedget
isinstancedictstrappendlenitemssumvaluesHTTP_200_OK)selfrequestargskwargsinsperD   rE   rF   rG   rH   ping_resalive_nodesitemr=   queues_to_workers	all_nodesnodenode_queuesqr   
node_statsr   r    wsdatas                             r   rP   zCeleryTopologyView.get   s9   	p%%--a-8D
	!$"4"4"67M	4;;=)F	T]]_-H	t01J	$**,'E
	!))..q.9H e^^Dtyy{+ $
 .09DT9JEKK 2 2 4fkkmX]]_V`VeVeVginisisiuv	9%DK"&&tR0(21d(;quuV}Q&&t,%d+2248 1 4,2J1;J1M:>>&"-SUD((#45V9VK , [!12 #FJJtR$8$>B ?"%hll4&<&B"C$'
tR(@(FB$G!~~e,&NN:6&NN:6'1~~6F'G'p:>>ZoKp*%>>*:;Xz~~i?XKUV`VdVdemVnptKu*..26::;G{GDM &: 2C2I2I2KL2KBq&*}2KL!$W!$$O0@$O!O #$5 6&))VW^^EU)V&V(++ZIY+Z(Z

 V%7%788i  	pX)=aS'AB6KnKnoo	p  	M	
  	F	
  	H	
  	J	
  	E	  	H	V Ms    P6 Q, Q> R 1R" R4 % S S6	Q)?Q$Q)$Q),Q;:Q;>RRRR"R10R14SSSSN)__name__
__module____qualname____doc__r   permission_classesrP   r   r   r   r   r      s     #W9r   r   )typingr   r   r   r   collectionsr   rest_framework.viewsr   rest_framework.responser	   rest_framework.permissionsr
   r   r   rest_frameworkr   celeryr   r?   rR   r   r   r   r   r   <module>rw      sG    0 / # ( , M M ! ,TD[ T `9 `9r   